blob: da631298250176f02cac1edacfe4cc5b5fb757dd [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"
Wen Congyang783e9b42012-05-07 12:10:47 +080031
Viktor Prutyanov2da91b52018-05-17 19:23:39 +030032#ifdef TARGET_X86_64
33#include "win_dump.h"
34#endif
35
qiaonuohand12f57e2014-02-18 14:11:35 +080036#include <zlib.h>
37#ifdef CONFIG_LZO
38#include <lzo/lzo1x.h>
39#endif
40#ifdef CONFIG_SNAPPY
41#include <snappy-c.h>
42#endif
qiaonuohan4ab23a92014-02-18 14:11:37 +080043#ifndef ELF_MACHINE_UNAME
44#define ELF_MACHINE_UNAME "Unknown"
45#endif
qiaonuohand12f57e2014-02-18 14:11:35 +080046
Marc-André Lureau903ef732017-09-11 18:59:25 +020047#define MAX_GUEST_NOTE_SIZE (1 << 20) /* 1MB should be enough */
48
Peter Xub7bc6b12021-09-22 12:20:09 -040049static Error *dump_migration_blocker;
50
Marc-André Lureau903ef732017-09-11 18:59:25 +020051#define ELF_NOTE_SIZE(hdr_size, name_size, desc_size) \
52 ((DIV_ROUND_UP((hdr_size), 4) + \
53 DIV_ROUND_UP((name_size), 4) + \
54 DIV_ROUND_UP((desc_size), 4)) * 4)
55
Janosch Frank05bbaa502022-03-30 12:36:00 +000056static inline bool dump_is_64bit(DumpState *s)
57{
58 return s->dump_info.d_class == ELFCLASS64;
59}
60
Janosch Frankdddf7252022-08-11 12:10:58 +000061static inline bool dump_has_filter(DumpState *s)
62{
63 return s->filter_area_length > 0;
64}
65
Bharata B Raoacb0ef52014-05-19 19:57:44 +020066uint16_t cpu_to_dump16(DumpState *s, uint16_t val)
Wen Congyang783e9b42012-05-07 12:10:47 +080067{
Bharata B Raoacb0ef52014-05-19 19:57:44 +020068 if (s->dump_info.d_endian == ELFDATA2LSB) {
Wen Congyang783e9b42012-05-07 12:10:47 +080069 val = cpu_to_le16(val);
70 } else {
71 val = cpu_to_be16(val);
72 }
73
74 return val;
75}
76
Bharata B Raoacb0ef52014-05-19 19:57:44 +020077uint32_t cpu_to_dump32(DumpState *s, uint32_t val)
Wen Congyang783e9b42012-05-07 12:10:47 +080078{
Bharata B Raoacb0ef52014-05-19 19:57:44 +020079 if (s->dump_info.d_endian == ELFDATA2LSB) {
Wen Congyang783e9b42012-05-07 12:10:47 +080080 val = cpu_to_le32(val);
81 } else {
82 val = cpu_to_be32(val);
83 }
84
85 return val;
86}
87
Bharata B Raoacb0ef52014-05-19 19:57:44 +020088uint64_t cpu_to_dump64(DumpState *s, uint64_t val)
Wen Congyang783e9b42012-05-07 12:10:47 +080089{
Bharata B Raoacb0ef52014-05-19 19:57:44 +020090 if (s->dump_info.d_endian == ELFDATA2LSB) {
Wen Congyang783e9b42012-05-07 12:10:47 +080091 val = cpu_to_le64(val);
92 } else {
93 val = cpu_to_be64(val);
94 }
95
96 return val;
97}
98
Wen Congyang783e9b42012-05-07 12:10:47 +080099static int dump_cleanup(DumpState *s)
100{
Laszlo Ersek5ee163e2013-08-06 12:37:09 +0200101 guest_phys_blocks_free(&s->guest_phys_blocks);
Wen Congyang783e9b42012-05-07 12:10:47 +0800102 memory_mapping_list_free(&s->list);
Chen Gang29282072014-08-03 23:28:56 +0800103 close(s->fd);
Marc-André Lureau903ef732017-09-11 18:59:25 +0200104 g_free(s->guest_note);
Janosch Frank9b722242022-10-17 11:32:10 +0000105 g_array_unref(s->string_table_buf);
Marc-André Lureau903ef732017-09-11 18:59:25 +0200106 s->guest_note = NULL;
Wen Congyang783e9b42012-05-07 12:10:47 +0800107 if (s->resume) {
Fam Zheng6796b402017-05-03 15:28:19 +0800108 if (s->detached) {
109 qemu_mutex_lock_iothread();
110 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800111 vm_start();
Fam Zheng6796b402017-05-03 15:28:19 +0800112 if (s->detached) {
113 qemu_mutex_unlock_iothread();
114 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800115 }
Peter Xub7bc6b12021-09-22 12:20:09 -0400116 migrate_del_blocker(dump_migration_blocker);
Wen Congyang783e9b42012-05-07 12:10:47 +0800117
Chen Gang29282072014-08-03 23:28:56 +0800118 return 0;
Wen Congyang783e9b42012-05-07 12:10:47 +0800119}
120
qiaonuohanb5ba1cc2014-02-18 14:11:25 +0800121static int fd_write_vmcore(const void *buf, size_t size, void *opaque)
Wen Congyang783e9b42012-05-07 12:10:47 +0800122{
123 DumpState *s = opaque;
Luiz Capitulino2f616522012-09-21 13:17:55 -0300124 size_t written_size;
Wen Congyang783e9b42012-05-07 12:10:47 +0800125
Luiz Capitulino2f616522012-09-21 13:17:55 -0300126 written_size = qemu_write_full(s->fd, buf, size);
127 if (written_size != size) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200128 return -errno;
Wen Congyang783e9b42012-05-07 12:10:47 +0800129 }
130
131 return 0;
132}
133
Janosch Frank670e7692022-08-11 12:11:00 +0000134static void prepare_elf64_header(DumpState *s, Elf64_Ehdr *elf_header)
Wen Congyang783e9b42012-05-07 12:10:47 +0800135{
Janosch Frank046bc412022-04-07 09:48:24 +0000136 /*
137 * phnum in the elf header is 16 bit, if we have more segments we
138 * set phnum to PN_XNUM and write the real number of segments to a
139 * special section.
140 */
141 uint16_t phnum = MIN(s->phdr_num, PN_XNUM);
Wen Congyang783e9b42012-05-07 12:10:47 +0800142
Janosch Frank670e7692022-08-11 12:11:00 +0000143 memset(elf_header, 0, sizeof(Elf64_Ehdr));
144 memcpy(elf_header, ELFMAG, SELFMAG);
145 elf_header->e_ident[EI_CLASS] = ELFCLASS64;
146 elf_header->e_ident[EI_DATA] = s->dump_info.d_endian;
147 elf_header->e_ident[EI_VERSION] = EV_CURRENT;
148 elf_header->e_type = cpu_to_dump16(s, ET_CORE);
149 elf_header->e_machine = cpu_to_dump16(s, s->dump_info.d_machine);
150 elf_header->e_version = cpu_to_dump32(s, EV_CURRENT);
151 elf_header->e_ehsize = cpu_to_dump16(s, sizeof(elf_header));
152 elf_header->e_phoff = cpu_to_dump64(s, s->phdr_offset);
153 elf_header->e_phentsize = cpu_to_dump16(s, sizeof(Elf64_Phdr));
154 elf_header->e_phnum = cpu_to_dump16(s, phnum);
Janosch Frank9b722242022-10-17 11:32:10 +0000155 elf_header->e_shoff = cpu_to_dump64(s, s->shdr_offset);
156 elf_header->e_shentsize = cpu_to_dump16(s, sizeof(Elf64_Shdr));
157 elf_header->e_shnum = cpu_to_dump16(s, s->shdr_num);
158 elf_header->e_shstrndx = cpu_to_dump16(s, s->shdr_num - 1);
Wen Congyang783e9b42012-05-07 12:10:47 +0800159}
160
Janosch Frank670e7692022-08-11 12:11:00 +0000161static void prepare_elf32_header(DumpState *s, Elf32_Ehdr *elf_header)
Wen Congyang783e9b42012-05-07 12:10:47 +0800162{
Janosch Frank046bc412022-04-07 09:48:24 +0000163 /*
164 * phnum in the elf header is 16 bit, if we have more segments we
165 * set phnum to PN_XNUM and write the real number of segments to a
166 * special section.
167 */
168 uint16_t phnum = MIN(s->phdr_num, PN_XNUM);
Janosch Frank670e7692022-08-11 12:11:00 +0000169
170 memset(elf_header, 0, sizeof(Elf32_Ehdr));
171 memcpy(elf_header, ELFMAG, SELFMAG);
172 elf_header->e_ident[EI_CLASS] = ELFCLASS32;
173 elf_header->e_ident[EI_DATA] = s->dump_info.d_endian;
174 elf_header->e_ident[EI_VERSION] = EV_CURRENT;
175 elf_header->e_type = cpu_to_dump16(s, ET_CORE);
176 elf_header->e_machine = cpu_to_dump16(s, s->dump_info.d_machine);
177 elf_header->e_version = cpu_to_dump32(s, EV_CURRENT);
178 elf_header->e_ehsize = cpu_to_dump16(s, sizeof(elf_header));
179 elf_header->e_phoff = cpu_to_dump32(s, s->phdr_offset);
180 elf_header->e_phentsize = cpu_to_dump16(s, sizeof(Elf32_Phdr));
181 elf_header->e_phnum = cpu_to_dump16(s, phnum);
Janosch Frank9b722242022-10-17 11:32:10 +0000182 elf_header->e_shoff = cpu_to_dump32(s, s->shdr_offset);
183 elf_header->e_shentsize = cpu_to_dump16(s, sizeof(Elf32_Shdr));
184 elf_header->e_shnum = cpu_to_dump16(s, s->shdr_num);
185 elf_header->e_shstrndx = cpu_to_dump16(s, s->shdr_num - 1);
Janosch Frank670e7692022-08-11 12:11:00 +0000186}
187
188static void write_elf_header(DumpState *s, Error **errp)
189{
190 Elf32_Ehdr elf32_header;
191 Elf64_Ehdr elf64_header;
192 size_t header_size;
193 void *header_ptr;
Wen Congyang783e9b42012-05-07 12:10:47 +0800194 int ret;
Wen Congyang783e9b42012-05-07 12:10:47 +0800195
Janosch Frank9b722242022-10-17 11:32:10 +0000196 /* The NULL header and the shstrtab are always defined */
197 assert(s->shdr_num >= 2);
Janosch Frank670e7692022-08-11 12:11:00 +0000198 if (dump_is_64bit(s)) {
199 prepare_elf64_header(s, &elf64_header);
200 header_size = sizeof(elf64_header);
201 header_ptr = &elf64_header;
202 } else {
203 prepare_elf32_header(s, &elf32_header);
204 header_size = sizeof(elf32_header);
205 header_ptr = &elf32_header;
Wen Congyang783e9b42012-05-07 12:10:47 +0800206 }
207
Janosch Frank670e7692022-08-11 12:11:00 +0000208 ret = fd_write_vmcore(header_ptr, header_size, s);
Wen Congyang783e9b42012-05-07 12:10:47 +0800209 if (ret < 0) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200210 error_setg_errno(errp, -ret, "dump: failed to write elf header");
Wen Congyang783e9b42012-05-07 12:10:47 +0800211 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800212}
213
zhanghailiang4c7e2512014-10-09 14:13:11 +0800214static void write_elf64_load(DumpState *s, MemoryMapping *memory_mapping,
215 int phdr_index, hwaddr offset,
216 hwaddr filesz, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800217{
218 Elf64_Phdr phdr;
219 int ret;
Wen Congyang783e9b42012-05-07 12:10:47 +0800220
221 memset(&phdr, 0, sizeof(Elf64_Phdr));
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200222 phdr.p_type = cpu_to_dump32(s, PT_LOAD);
223 phdr.p_offset = cpu_to_dump64(s, offset);
224 phdr.p_paddr = cpu_to_dump64(s, memory_mapping->phys_addr);
225 phdr.p_filesz = cpu_to_dump64(s, filesz);
226 phdr.p_memsz = cpu_to_dump64(s, memory_mapping->length);
Jon Dorone17bebd2019-01-09 10:22:03 +0200227 phdr.p_vaddr = cpu_to_dump64(s, memory_mapping->virt_addr) ?: phdr.p_paddr;
Wen Congyang783e9b42012-05-07 12:10:47 +0800228
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200229 assert(memory_mapping->length >= filesz);
230
Wen Congyang783e9b42012-05-07 12:10:47 +0800231 ret = fd_write_vmcore(&phdr, sizeof(Elf64_Phdr), s);
232 if (ret < 0) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200233 error_setg_errno(errp, -ret,
234 "dump: failed to write program header table");
Wen Congyang783e9b42012-05-07 12:10:47 +0800235 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800236}
237
zhanghailiang4c7e2512014-10-09 14:13:11 +0800238static void write_elf32_load(DumpState *s, MemoryMapping *memory_mapping,
239 int phdr_index, hwaddr offset,
240 hwaddr filesz, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800241{
242 Elf32_Phdr phdr;
243 int ret;
Wen Congyang783e9b42012-05-07 12:10:47 +0800244
245 memset(&phdr, 0, sizeof(Elf32_Phdr));
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200246 phdr.p_type = cpu_to_dump32(s, PT_LOAD);
247 phdr.p_offset = cpu_to_dump32(s, offset);
248 phdr.p_paddr = cpu_to_dump32(s, memory_mapping->phys_addr);
249 phdr.p_filesz = cpu_to_dump32(s, filesz);
250 phdr.p_memsz = cpu_to_dump32(s, memory_mapping->length);
Jon Dorone17bebd2019-01-09 10:22:03 +0200251 phdr.p_vaddr =
252 cpu_to_dump32(s, memory_mapping->virt_addr) ?: phdr.p_paddr;
Wen Congyang783e9b42012-05-07 12:10:47 +0800253
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200254 assert(memory_mapping->length >= filesz);
255
Wen Congyang783e9b42012-05-07 12:10:47 +0800256 ret = fd_write_vmcore(&phdr, sizeof(Elf32_Phdr), s);
257 if (ret < 0) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200258 error_setg_errno(errp, -ret,
259 "dump: failed to write program header table");
Wen Congyang783e9b42012-05-07 12:10:47 +0800260 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800261}
262
Janosch Frank2341a942022-08-11 12:11:01 +0000263static void prepare_elf64_phdr_note(DumpState *s, Elf64_Phdr *phdr)
Wen Congyang783e9b42012-05-07 12:10:47 +0800264{
Janosch Frankbc7d5582022-03-30 12:36:01 +0000265 memset(phdr, 0, sizeof(*phdr));
266 phdr->p_type = cpu_to_dump32(s, PT_NOTE);
267 phdr->p_offset = cpu_to_dump64(s, s->note_offset);
268 phdr->p_paddr = 0;
269 phdr->p_filesz = cpu_to_dump64(s, s->note_size);
270 phdr->p_memsz = cpu_to_dump64(s, s->note_size);
271 phdr->p_vaddr = 0;
Wen Congyang783e9b42012-05-07 12:10:47 +0800272}
273
Paolo Bonzini0bc3cd62013-04-08 17:29:59 +0200274static inline int cpu_index(CPUState *cpu)
275{
276 return cpu->cpu_index + 1;
277}
278
Marc-André Lureau903ef732017-09-11 18:59:25 +0200279static void write_guest_note(WriteCoreDumpFunction f, DumpState *s,
280 Error **errp)
281{
282 int ret;
283
284 if (s->guest_note) {
285 ret = f(s->guest_note, s->guest_note_size, s);
286 if (ret < 0) {
287 error_setg(errp, "dump: failed to write guest note");
288 }
289 }
290}
291
zhanghailiang4c7e2512014-10-09 14:13:11 +0800292static void write_elf64_notes(WriteCoreDumpFunction f, DumpState *s,
293 Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800294{
Andreas Färber0d342822012-12-17 07:12:13 +0100295 CPUState *cpu;
Wen Congyang783e9b42012-05-07 12:10:47 +0800296 int ret;
297 int id;
298
Andreas Färberbdc44642013-06-24 23:50:24 +0200299 CPU_FOREACH(cpu) {
Andreas Färber0d342822012-12-17 07:12:13 +0100300 id = cpu_index(cpu);
qiaonuohan6a519912014-02-18 14:11:26 +0800301 ret = cpu_write_elf64_note(f, cpu, id, s);
Wen Congyang783e9b42012-05-07 12:10:47 +0800302 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800303 error_setg(errp, "dump: failed to write elf notes");
zhanghailiang4c7e2512014-10-09 14:13:11 +0800304 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800305 }
306 }
307
Andreas Färberbdc44642013-06-24 23:50:24 +0200308 CPU_FOREACH(cpu) {
qiaonuohan6a519912014-02-18 14:11:26 +0800309 ret = cpu_write_elf64_qemunote(f, cpu, s);
Wen Congyang783e9b42012-05-07 12:10:47 +0800310 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800311 error_setg(errp, "dump: failed to write CPU status");
zhanghailiang4c7e2512014-10-09 14:13:11 +0800312 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800313 }
314 }
Marc-André Lureau903ef732017-09-11 18:59:25 +0200315
316 write_guest_note(f, s, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800317}
318
Janosch Frank2341a942022-08-11 12:11:01 +0000319static void prepare_elf32_phdr_note(DumpState *s, Elf32_Phdr *phdr)
Wen Congyang783e9b42012-05-07 12:10:47 +0800320{
Janosch Frankbc7d5582022-03-30 12:36:01 +0000321 memset(phdr, 0, sizeof(*phdr));
322 phdr->p_type = cpu_to_dump32(s, PT_NOTE);
323 phdr->p_offset = cpu_to_dump32(s, s->note_offset);
324 phdr->p_paddr = 0;
325 phdr->p_filesz = cpu_to_dump32(s, s->note_size);
326 phdr->p_memsz = cpu_to_dump32(s, s->note_size);
327 phdr->p_vaddr = 0;
Wen Congyang783e9b42012-05-07 12:10:47 +0800328}
329
zhanghailiang4c7e2512014-10-09 14:13:11 +0800330static void write_elf32_notes(WriteCoreDumpFunction f, DumpState *s,
331 Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800332{
Andreas Färber0d342822012-12-17 07:12:13 +0100333 CPUState *cpu;
Wen Congyang783e9b42012-05-07 12:10:47 +0800334 int ret;
335 int id;
336
Andreas Färberbdc44642013-06-24 23:50:24 +0200337 CPU_FOREACH(cpu) {
Andreas Färber0d342822012-12-17 07:12:13 +0100338 id = cpu_index(cpu);
qiaonuohan6a519912014-02-18 14:11:26 +0800339 ret = cpu_write_elf32_note(f, cpu, id, s);
Wen Congyang783e9b42012-05-07 12:10:47 +0800340 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800341 error_setg(errp, "dump: failed to write elf notes");
zhanghailiang4c7e2512014-10-09 14:13:11 +0800342 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800343 }
344 }
345
Andreas Färberbdc44642013-06-24 23:50:24 +0200346 CPU_FOREACH(cpu) {
qiaonuohan6a519912014-02-18 14:11:26 +0800347 ret = cpu_write_elf32_qemunote(f, cpu, s);
Wen Congyang783e9b42012-05-07 12:10:47 +0800348 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800349 error_setg(errp, "dump: failed to write CPU status");
zhanghailiang4c7e2512014-10-09 14:13:11 +0800350 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800351 }
352 }
Marc-André Lureau903ef732017-09-11 18:59:25 +0200353
354 write_guest_note(f, s, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800355}
356
Janosch Frankbc7d5582022-03-30 12:36:01 +0000357static void write_elf_phdr_note(DumpState *s, Error **errp)
358{
Janosch Frankbc7d5582022-03-30 12:36:01 +0000359 Elf32_Phdr phdr32;
360 Elf64_Phdr phdr64;
361 void *phdr;
362 size_t size;
363 int ret;
364
365 if (dump_is_64bit(s)) {
Janosch Frank2341a942022-08-11 12:11:01 +0000366 prepare_elf64_phdr_note(s, &phdr64);
Janosch Frankbc7d5582022-03-30 12:36:01 +0000367 size = sizeof(phdr64);
368 phdr = &phdr64;
369 } else {
Janosch Frank2341a942022-08-11 12:11:01 +0000370 prepare_elf32_phdr_note(s, &phdr32);
Janosch Frankbc7d5582022-03-30 12:36:01 +0000371 size = sizeof(phdr32);
372 phdr = &phdr32;
373 }
374
375 ret = fd_write_vmcore(phdr, size, s);
376 if (ret < 0) {
377 error_setg_errno(errp, -ret,
378 "dump: failed to write program header table");
379 }
380}
381
Janosch Franke41ed292022-10-17 08:38:13 +0000382static void prepare_elf_section_hdr_zero(DumpState *s)
Wen Congyang783e9b42012-05-07 12:10:47 +0800383{
Janosch Franke41ed292022-10-17 08:38:13 +0000384 if (dump_is_64bit(s)) {
385 Elf64_Shdr *shdr64 = s->elf_section_hdrs;
386
387 shdr64->sh_info = cpu_to_dump32(s, s->phdr_num);
388 } else {
389 Elf32_Shdr *shdr32 = s->elf_section_hdrs;
390
391 shdr32->sh_info = cpu_to_dump32(s, s->phdr_num);
392 }
393}
394
Janosch Frank9b722242022-10-17 11:32:10 +0000395static void prepare_elf_section_hdr_string(DumpState *s, void *buff)
396{
397 uint64_t index = s->string_table_buf->len;
398 const char strtab[] = ".shstrtab";
399 Elf32_Shdr shdr32 = {};
400 Elf64_Shdr shdr64 = {};
401 int shdr_size;
402 void *shdr;
403
404 g_array_append_vals(s->string_table_buf, strtab, sizeof(strtab));
405 if (dump_is_64bit(s)) {
406 shdr_size = sizeof(Elf64_Shdr);
407 shdr64.sh_type = SHT_STRTAB;
408 shdr64.sh_offset = s->section_offset + s->elf_section_data_size;
409 shdr64.sh_name = index;
410 shdr64.sh_size = s->string_table_buf->len;
411 shdr = &shdr64;
412 } else {
413 shdr_size = sizeof(Elf32_Shdr);
414 shdr32.sh_type = SHT_STRTAB;
415 shdr32.sh_offset = s->section_offset + s->elf_section_data_size;
416 shdr32.sh_name = index;
417 shdr32.sh_size = s->string_table_buf->len;
418 shdr = &shdr32;
419 }
420 memcpy(buff, shdr, shdr_size);
421}
422
423static bool prepare_elf_section_hdrs(DumpState *s, Error **errp)
Janosch Franke41ed292022-10-17 08:38:13 +0000424{
425 size_t len, sizeof_shdr;
Janosch Frank9b722242022-10-17 11:32:10 +0000426 void *buff_hdr;
Janosch Franke41ed292022-10-17 08:38:13 +0000427
428 /*
429 * Section ordering:
430 * - HDR zero
Janosch Frank9b722242022-10-17 11:32:10 +0000431 * - Arch section hdrs
432 * - String table hdr
Janosch Franke41ed292022-10-17 08:38:13 +0000433 */
434 sizeof_shdr = dump_is_64bit(s) ? sizeof(Elf64_Shdr) : sizeof(Elf32_Shdr);
435 len = sizeof_shdr * s->shdr_num;
436 s->elf_section_hdrs = g_malloc0(len);
Janosch Frank9b722242022-10-17 11:32:10 +0000437 buff_hdr = s->elf_section_hdrs;
Janosch Franke41ed292022-10-17 08:38:13 +0000438
439 /*
440 * The first section header is ALWAYS a special initial section
441 * header.
442 *
443 * The header should be 0 with one exception being that if
444 * phdr_num is PN_XNUM then the sh_info field contains the real
445 * number of segment entries.
446 *
447 * As we zero allocate the buffer we will only need to modify
448 * sh_info for the PN_XNUM case.
449 */
450 if (s->phdr_num >= PN_XNUM) {
451 prepare_elf_section_hdr_zero(s);
452 }
Janosch Frank9b722242022-10-17 11:32:10 +0000453 buff_hdr += sizeof_shdr;
454
455 /* Add architecture defined section headers */
456 if (s->dump_info.arch_sections_write_hdr_fn
457 && s->shdr_num > 2) {
458 buff_hdr += s->dump_info.arch_sections_write_hdr_fn(s, buff_hdr);
459
460 if (s->shdr_num >= SHN_LORESERVE) {
461 error_setg_errno(errp, EINVAL,
462 "dump: too many architecture defined sections");
463 return false;
464 }
465 }
466
467 /*
468 * String table is the last section since strings are added via
469 * arch_sections_write_hdr().
470 */
471 prepare_elf_section_hdr_string(s, buff_hdr);
472 return true;
Janosch Franke41ed292022-10-17 08:38:13 +0000473}
474
475static void write_elf_section_headers(DumpState *s, Error **errp)
476{
477 size_t sizeof_shdr = dump_is_64bit(s) ? sizeof(Elf64_Shdr) : sizeof(Elf32_Shdr);
Wen Congyang783e9b42012-05-07 12:10:47 +0800478 int ret;
479
Janosch Frank9b722242022-10-17 11:32:10 +0000480 if (!prepare_elf_section_hdrs(s, errp)) {
481 return;
482 }
Janosch Franke41ed292022-10-17 08:38:13 +0000483
484 ret = fd_write_vmcore(s->elf_section_hdrs, s->shdr_num * sizeof_shdr, s);
485 if (ret < 0) {
486 error_setg_errno(errp, -ret, "dump: failed to write section headers");
Wen Congyang783e9b42012-05-07 12:10:47 +0800487 }
488
Janosch Franke41ed292022-10-17 08:38:13 +0000489 g_free(s->elf_section_hdrs);
Wen Congyang783e9b42012-05-07 12:10:47 +0800490}
491
Janosch Frank9b722242022-10-17 11:32:10 +0000492static void write_elf_sections(DumpState *s, Error **errp)
493{
494 int ret;
495
496 if (s->elf_section_data_size) {
497 /* Write architecture section data */
498 ret = fd_write_vmcore(s->elf_section_data,
499 s->elf_section_data_size, s);
500 if (ret < 0) {
501 error_setg_errno(errp, -ret,
502 "dump: failed to write architecture section data");
503 return;
504 }
505 }
506
507 /* Write string table */
508 ret = fd_write_vmcore(s->string_table_buf->data,
509 s->string_table_buf->len, s);
510 if (ret < 0) {
511 error_setg_errno(errp, -ret, "dump: failed to write string table data");
512 }
513}
514
zhanghailiang4c7e2512014-10-09 14:13:11 +0800515static void write_data(DumpState *s, void *buf, int length, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800516{
517 int ret;
518
519 ret = fd_write_vmcore(buf, length, s);
520 if (ret < 0) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200521 error_setg_errno(errp, -ret, "dump: failed to save memory");
Peter Xu2264c2c2016-02-18 13:16:53 +0800522 } else {
523 s->written_size += length;
Wen Congyang783e9b42012-05-07 12:10:47 +0800524 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800525}
526
zhanghailiang4c7e2512014-10-09 14:13:11 +0800527/* write the memory to vmcore. 1 page per I/O. */
528static void write_memory(DumpState *s, GuestPhysBlock *block, ram_addr_t start,
529 int64_t size, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800530{
Janosch Frank86a518b2022-03-30 12:35:55 +0000531 ERRP_GUARD();
Wen Congyang783e9b42012-05-07 12:10:47 +0800532 int64_t i;
Wen Congyang783e9b42012-05-07 12:10:47 +0800533
Andrew Jones8161bef2016-01-11 20:56:20 +0100534 for (i = 0; i < size / s->dump_info.page_size; i++) {
535 write_data(s, block->host_addr + start + i * s->dump_info.page_size,
Janosch Frank86a518b2022-03-30 12:35:55 +0000536 s->dump_info.page_size, errp);
537 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800538 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800539 }
540 }
541
Andrew Jones8161bef2016-01-11 20:56:20 +0100542 if ((size % s->dump_info.page_size) != 0) {
543 write_data(s, block->host_addr + start + i * s->dump_info.page_size,
Janosch Frank86a518b2022-03-30 12:35:55 +0000544 size % s->dump_info.page_size, errp);
545 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800546 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800547 }
548 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800549}
550
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200551/* get the memory's offset and size in the vmcore */
552static void get_offset_range(hwaddr phys_addr,
553 ram_addr_t mapping_length,
554 DumpState *s,
555 hwaddr *p_offset,
556 hwaddr *p_filesz)
Wen Congyang783e9b42012-05-07 12:10:47 +0800557{
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200558 GuestPhysBlock *block;
Avi Kivitya8170e52012-10-23 12:30:10 +0200559 hwaddr offset = s->memory_offset;
Wen Congyang783e9b42012-05-07 12:10:47 +0800560 int64_t size_in_block, start;
561
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200562 /* When the memory is not stored into vmcore, offset will be -1 */
563 *p_offset = -1;
564 *p_filesz = 0;
565
Janosch Frankdddf7252022-08-11 12:10:58 +0000566 if (dump_has_filter(s)) {
567 if (phys_addr < s->filter_area_begin ||
568 phys_addr >= s->filter_area_begin + s->filter_area_length) {
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200569 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800570 }
571 }
572
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200573 QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
Janosch Frankdddf7252022-08-11 12:10:58 +0000574 if (dump_has_filter(s)) {
575 if (block->target_start >= s->filter_area_begin + s->filter_area_length ||
576 block->target_end <= s->filter_area_begin) {
Wen Congyang783e9b42012-05-07 12:10:47 +0800577 /* This block is out of the range */
578 continue;
579 }
580
Janosch Frankdddf7252022-08-11 12:10:58 +0000581 if (s->filter_area_begin <= block->target_start) {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200582 start = block->target_start;
Wen Congyang783e9b42012-05-07 12:10:47 +0800583 } else {
Janosch Frankdddf7252022-08-11 12:10:58 +0000584 start = s->filter_area_begin;
Wen Congyang783e9b42012-05-07 12:10:47 +0800585 }
586
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200587 size_in_block = block->target_end - start;
Janosch Frankdddf7252022-08-11 12:10:58 +0000588 if (s->filter_area_begin + s->filter_area_length < block->target_end) {
589 size_in_block -= block->target_end - (s->filter_area_begin + s->filter_area_length);
Wen Congyang783e9b42012-05-07 12:10:47 +0800590 }
591 } else {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200592 start = block->target_start;
593 size_in_block = block->target_end - block->target_start;
Wen Congyang783e9b42012-05-07 12:10:47 +0800594 }
595
596 if (phys_addr >= start && phys_addr < start + size_in_block) {
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200597 *p_offset = phys_addr - start + offset;
598
599 /* The offset range mapped from the vmcore file must not spill over
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200600 * the GuestPhysBlock, clamp it. The rest of the mapping will be
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200601 * zero-filled in memory at load time; see
602 * <http://refspecs.linuxbase.org/elf/gabi4+/ch5.pheader.html>.
603 */
604 *p_filesz = phys_addr + mapping_length <= start + size_in_block ?
605 mapping_length :
606 size_in_block - (phys_addr - start);
607 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800608 }
609
610 offset += size_in_block;
611 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800612}
613
Janosch Frankafae6052022-08-11 12:10:55 +0000614static void write_elf_phdr_loads(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800615{
Janosch Frank86a518b2022-03-30 12:35:55 +0000616 ERRP_GUARD();
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200617 hwaddr offset, filesz;
Wen Congyang783e9b42012-05-07 12:10:47 +0800618 MemoryMapping *memory_mapping;
619 uint32_t phdr_index = 1;
Wen Congyang783e9b42012-05-07 12:10:47 +0800620
621 QTAILQ_FOREACH(memory_mapping, &s->list.head, next) {
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200622 get_offset_range(memory_mapping->phys_addr,
623 memory_mapping->length,
624 s, &offset, &filesz);
Janosch Frank05bbaa502022-03-30 12:36:00 +0000625 if (dump_is_64bit(s)) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800626 write_elf64_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 } else {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800629 write_elf32_load(s, memory_mapping, phdr_index++, offset,
Janosch Frank86a518b2022-03-30 12:35:55 +0000630 filesz, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800631 }
632
Janosch Frank86a518b2022-03-30 12:35:55 +0000633 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800634 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800635 }
636
Janosch Frank046bc412022-04-07 09:48:24 +0000637 if (phdr_index >= s->phdr_num) {
Wen Congyang783e9b42012-05-07 12:10:47 +0800638 break;
639 }
640 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800641}
642
Janosch Frankc6812472022-03-30 12:36:03 +0000643static void write_elf_notes(DumpState *s, Error **errp)
644{
645 if (dump_is_64bit(s)) {
646 write_elf64_notes(fd_write_vmcore, s, errp);
647 } else {
648 write_elf32_notes(fd_write_vmcore, s, errp);
649 }
650}
651
Wen Congyang783e9b42012-05-07 12:10:47 +0800652/* write elf header, PT_NOTE and elf note to vmcore. */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800653static void dump_begin(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800654{
Janosch Frank86a518b2022-03-30 12:35:55 +0000655 ERRP_GUARD();
Wen Congyang783e9b42012-05-07 12:10:47 +0800656
657 /*
658 * the vmcore's format is:
659 * --------------
660 * | elf header |
661 * --------------
Janosch Frankcb415fd2022-10-17 08:38:14 +0000662 * | sctn_hdr |
663 * --------------
Wen Congyang783e9b42012-05-07 12:10:47 +0800664 * | PT_NOTE |
665 * --------------
666 * | PT_LOAD |
667 * --------------
668 * | ...... |
669 * --------------
670 * | PT_LOAD |
671 * --------------
Wen Congyang783e9b42012-05-07 12:10:47 +0800672 * | elf note |
673 * --------------
674 * | memory |
675 * --------------
676 *
677 * we only know where the memory is saved after we write elf note into
678 * vmcore.
679 */
680
681 /* write elf header to vmcore */
Janosch Frank670e7692022-08-11 12:11:00 +0000682 write_elf_header(s, errp);
Janosch Frank86a518b2022-03-30 12:35:55 +0000683 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800684 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800685 }
686
Janosch Frankcb415fd2022-10-17 08:38:14 +0000687 /* write section headers to vmcore */
688 write_elf_section_headers(s, errp);
689 if (*errp) {
690 return;
691 }
692
Janosch Frankbc7d5582022-03-30 12:36:01 +0000693 /* write PT_NOTE to vmcore */
694 write_elf_phdr_note(s, errp);
695 if (*errp) {
696 return;
697 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800698
Janosch Frankafae6052022-08-11 12:10:55 +0000699 /* write all PT_LOADs to vmcore */
700 write_elf_phdr_loads(s, errp);
Janosch Frank5ff2e5a2022-03-30 12:36:02 +0000701 if (*errp) {
702 return;
703 }
704
Janosch Frankc6812472022-03-30 12:36:03 +0000705 /* write notes to vmcore */
706 write_elf_notes(s, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800707}
708
Janosch Frank113d8f42022-10-17 08:38:22 +0000709int64_t dump_filtered_memblock_size(GuestPhysBlock *block,
710 int64_t filter_area_start,
711 int64_t filter_area_length)
Wen Congyang783e9b42012-05-07 12:10:47 +0800712{
Janosch Frank1e811302022-08-11 12:10:56 +0000713 int64_t size, left, right;
Wen Congyang783e9b42012-05-07 12:10:47 +0800714
Janosch Frank1e811302022-08-11 12:10:56 +0000715 /* No filter, return full size */
716 if (!filter_area_length) {
717 return block->target_end - block->target_start;
Wen Congyang783e9b42012-05-07 12:10:47 +0800718 }
Janosch Frank1e811302022-08-11 12:10:56 +0000719
720 /* calculate the overlapped region. */
721 left = MAX(filter_area_start, block->target_start);
722 right = MIN(filter_area_start + filter_area_length, block->target_end);
723 size = right - left;
724 size = size > 0 ? size : 0;
725
726 return size;
727}
728
Janosch Frank113d8f42022-10-17 08:38:22 +0000729int64_t dump_filtered_memblock_start(GuestPhysBlock *block,
730 int64_t filter_area_start,
731 int64_t filter_area_length)
Janosch Frank1e811302022-08-11 12:10:56 +0000732{
733 if (filter_area_length) {
734 /* return -1 if the block is not within filter area */
735 if (block->target_start >= filter_area_start + filter_area_length ||
736 block->target_end <= filter_area_start) {
737 return -1;
738 }
739
740 if (filter_area_start > block->target_start) {
741 return filter_area_start - block->target_start;
742 }
743 }
744
745 return 0;
Wen Congyang783e9b42012-05-07 12:10:47 +0800746}
747
748/* write all memory to vmcore */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800749static void dump_iterate(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800750{
Janosch Frank86a518b2022-03-30 12:35:55 +0000751 ERRP_GUARD();
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200752 GuestPhysBlock *block;
Janosch Frank1e811302022-08-11 12:10:56 +0000753 int64_t memblock_size, memblock_start;
Wen Congyang783e9b42012-05-07 12:10:47 +0800754
Janosch Frank1e811302022-08-11 12:10:56 +0000755 QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
Janosch Frankdddf7252022-08-11 12:10:58 +0000756 memblock_start = dump_filtered_memblock_start(block, s->filter_area_begin, s->filter_area_length);
Janosch Frank1e811302022-08-11 12:10:56 +0000757 if (memblock_start == -1) {
758 continue;
Wen Congyang783e9b42012-05-07 12:10:47 +0800759 }
Janosch Frank1e811302022-08-11 12:10:56 +0000760
Janosch Frankdddf7252022-08-11 12:10:58 +0000761 memblock_size = dump_filtered_memblock_size(block, s->filter_area_begin, s->filter_area_length);
Janosch Frank1e811302022-08-11 12:10:56 +0000762
763 /* Write the memory to file */
764 write_memory(s, block, memblock_start, memblock_size, errp);
Janosch Frank86a518b2022-03-30 12:35:55 +0000765 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800766 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800767 }
Janosch Frank1e811302022-08-11 12:10:56 +0000768 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800769}
770
Janosch Frank9b722242022-10-17 11:32:10 +0000771static void dump_end(DumpState *s, Error **errp)
772{
773 int rc;
Janosch Frank9b722242022-10-17 11:32:10 +0000774
775 if (s->elf_section_data_size) {
776 s->elf_section_data = g_malloc0(s->elf_section_data_size);
777 }
778
779 /* Adds the architecture defined section data to s->elf_section_data */
780 if (s->dump_info.arch_sections_write_fn &&
781 s->elf_section_data_size) {
782 rc = s->dump_info.arch_sections_write_fn(s, s->elf_section_data);
783 if (rc) {
784 error_setg_errno(errp, rc,
785 "dump: failed to get arch section data");
786 g_free(s->elf_section_data);
787 return;
788 }
789 }
790
791 /* write sections to vmcore */
792 write_elf_sections(s, errp);
793}
794
zhanghailiang4c7e2512014-10-09 14:13:11 +0800795static void create_vmcore(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800796{
Janosch Frank86a518b2022-03-30 12:35:55 +0000797 ERRP_GUARD();
Wen Congyang783e9b42012-05-07 12:10:47 +0800798
Janosch Frank86a518b2022-03-30 12:35:55 +0000799 dump_begin(s, errp);
800 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800801 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800802 }
803
Janosch Frank9b722242022-10-17 11:32:10 +0000804 /* Iterate over memory and dump it to file */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800805 dump_iterate(s, errp);
Janosch Frank9b722242022-10-17 11:32:10 +0000806 if (*errp) {
807 return;
808 }
809
810 /* Write the section data */
811 dump_end(s, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800812}
813
qiaonuohanfda05382014-02-18 14:11:27 +0800814static int write_start_flat_header(int fd)
815{
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200816 MakedumpfileHeader *mh;
qiaonuohanfda05382014-02-18 14:11:27 +0800817 int ret = 0;
818
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200819 QEMU_BUILD_BUG_ON(sizeof *mh > MAX_SIZE_MDF_HEADER);
820 mh = g_malloc0(MAX_SIZE_MDF_HEADER);
qiaonuohanfda05382014-02-18 14:11:27 +0800821
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200822 memcpy(mh->signature, MAKEDUMPFILE_SIGNATURE,
823 MIN(sizeof mh->signature, sizeof MAKEDUMPFILE_SIGNATURE));
qiaonuohanfda05382014-02-18 14:11:27 +0800824
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200825 mh->type = cpu_to_be64(TYPE_FLAT_HEADER);
826 mh->version = cpu_to_be64(VERSION_FLAT_HEADER);
qiaonuohanfda05382014-02-18 14:11:27 +0800827
828 size_t written_size;
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200829 written_size = qemu_write_full(fd, mh, MAX_SIZE_MDF_HEADER);
qiaonuohanfda05382014-02-18 14:11:27 +0800830 if (written_size != MAX_SIZE_MDF_HEADER) {
831 ret = -1;
832 }
833
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200834 g_free(mh);
qiaonuohanfda05382014-02-18 14:11:27 +0800835 return ret;
836}
837
838static int write_end_flat_header(int fd)
839{
840 MakedumpfileDataHeader mdh;
841
842 mdh.offset = END_FLAG_FLAT_HEADER;
843 mdh.buf_size = END_FLAG_FLAT_HEADER;
844
845 size_t written_size;
846 written_size = qemu_write_full(fd, &mdh, sizeof(mdh));
847 if (written_size != sizeof(mdh)) {
848 return -1;
849 }
850
851 return 0;
852}
853
qiaonuohan5d31bab2014-02-18 14:11:28 +0800854static int write_buffer(int fd, off_t offset, const void *buf, size_t size)
855{
856 size_t written_size;
857 MakedumpfileDataHeader mdh;
858
859 mdh.offset = cpu_to_be64(offset);
860 mdh.buf_size = cpu_to_be64(size);
861
862 written_size = qemu_write_full(fd, &mdh, sizeof(mdh));
863 if (written_size != sizeof(mdh)) {
864 return -1;
865 }
866
867 written_size = qemu_write_full(fd, buf, size);
868 if (written_size != size) {
869 return -1;
870 }
871
872 return 0;
873}
874
qiaonuohan4835ef72014-02-18 14:11:29 +0800875static int buf_write_note(const void *buf, size_t size, void *opaque)
876{
877 DumpState *s = opaque;
878
879 /* note_buf is not enough */
880 if (s->note_buf_offset + size > s->note_size) {
881 return -1;
882 }
883
884 memcpy(s->note_buf + s->note_buf_offset, buf, size);
885
886 s->note_buf_offset += size;
887
888 return 0;
889}
890
Marc-André Lureau903ef732017-09-11 18:59:25 +0200891/*
892 * This function retrieves various sizes from an elf header.
893 *
894 * @note has to be a valid ELF note. The return sizes are unmodified
895 * (not padded or rounded up to be multiple of 4).
896 */
897static void get_note_sizes(DumpState *s, const void *note,
898 uint64_t *note_head_size,
899 uint64_t *name_size,
900 uint64_t *desc_size)
901{
902 uint64_t note_head_sz;
903 uint64_t name_sz;
904 uint64_t desc_sz;
905
Janosch Frank05bbaa502022-03-30 12:36:00 +0000906 if (dump_is_64bit(s)) {
Marc-André Lureau903ef732017-09-11 18:59:25 +0200907 const Elf64_Nhdr *hdr = note;
908 note_head_sz = sizeof(Elf64_Nhdr);
Philippe Mathieu-Daudébb509d92022-12-16 08:28:32 +0100909 name_sz = cpu_to_dump64(s, hdr->n_namesz);
910 desc_sz = cpu_to_dump64(s, hdr->n_descsz);
Marc-André Lureau903ef732017-09-11 18:59:25 +0200911 } else {
912 const Elf32_Nhdr *hdr = note;
913 note_head_sz = sizeof(Elf32_Nhdr);
Philippe Mathieu-Daudébb509d92022-12-16 08:28:32 +0100914 name_sz = cpu_to_dump32(s, hdr->n_namesz);
915 desc_sz = cpu_to_dump32(s, hdr->n_descsz);
Marc-André Lureau903ef732017-09-11 18:59:25 +0200916 }
917
918 if (note_head_size) {
919 *note_head_size = note_head_sz;
920 }
921 if (name_size) {
922 *name_size = name_sz;
923 }
924 if (desc_size) {
925 *desc_size = desc_sz;
926 }
927}
928
Marc-André Lureaud9feb512017-09-11 18:59:26 +0200929static bool note_name_equal(DumpState *s,
930 const uint8_t *note, const char *name)
931{
932 int len = strlen(name) + 1;
933 uint64_t head_size, name_size;
934
935 get_note_sizes(s, note, &head_size, &name_size, NULL);
936 head_size = ROUND_UP(head_size, 4);
937
Marc-André Lureauc983ca82017-12-12 15:53:59 +0100938 return name_size == len && memcmp(note + head_size, name, len) == 0;
Marc-André Lureaud9feb512017-09-11 18:59:26 +0200939}
940
qiaonuohan298f1162014-02-18 14:11:32 +0800941/* write common header, sub header and elf note to vmcore */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800942static void create_header32(DumpState *s, Error **errp)
qiaonuohan298f1162014-02-18 14:11:32 +0800943{
Janosch Frank86a518b2022-03-30 12:35:55 +0000944 ERRP_GUARD();
qiaonuohan298f1162014-02-18 14:11:32 +0800945 DiskDumpHeader32 *dh = NULL;
946 KdumpSubHeader32 *kh = NULL;
947 size_t size;
qiaonuohan298f1162014-02-18 14:11:32 +0800948 uint32_t block_size;
949 uint32_t sub_hdr_size;
950 uint32_t bitmap_blocks;
951 uint32_t status = 0;
952 uint64_t offset_note;
953
954 /* write common header, the version of kdump-compressed format is 6th */
955 size = sizeof(DiskDumpHeader32);
956 dh = g_malloc0(size);
957
Eric Blake84c868f2018-03-27 15:21:51 -0500958 memcpy(dh->signature, KDUMP_SIGNATURE, SIG_LEN);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200959 dh->header_version = cpu_to_dump32(s, 6);
Andrew Jones8161bef2016-01-11 20:56:20 +0100960 block_size = s->dump_info.page_size;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200961 dh->block_size = cpu_to_dump32(s, block_size);
qiaonuohan298f1162014-02-18 14:11:32 +0800962 sub_hdr_size = sizeof(struct KdumpSubHeader32) + s->note_size;
963 sub_hdr_size = DIV_ROUND_UP(sub_hdr_size, block_size);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200964 dh->sub_hdr_size = cpu_to_dump32(s, sub_hdr_size);
qiaonuohan298f1162014-02-18 14:11:32 +0800965 /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200966 dh->max_mapnr = cpu_to_dump32(s, MIN(s->max_mapnr, UINT_MAX));
967 dh->nr_cpus = cpu_to_dump32(s, s->nr_cpus);
qiaonuohan298f1162014-02-18 14:11:32 +0800968 bitmap_blocks = DIV_ROUND_UP(s->len_dump_bitmap, block_size) * 2;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200969 dh->bitmap_blocks = cpu_to_dump32(s, bitmap_blocks);
qiaonuohan4ab23a92014-02-18 14:11:37 +0800970 strncpy(dh->utsname.machine, ELF_MACHINE_UNAME, sizeof(dh->utsname.machine));
qiaonuohan298f1162014-02-18 14:11:32 +0800971
972 if (s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) {
973 status |= DUMP_DH_COMPRESSED_ZLIB;
974 }
975#ifdef CONFIG_LZO
976 if (s->flag_compress & DUMP_DH_COMPRESSED_LZO) {
977 status |= DUMP_DH_COMPRESSED_LZO;
978 }
979#endif
980#ifdef CONFIG_SNAPPY
981 if (s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) {
982 status |= DUMP_DH_COMPRESSED_SNAPPY;
983 }
984#endif
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200985 dh->status = cpu_to_dump32(s, status);
qiaonuohan298f1162014-02-18 14:11:32 +0800986
987 if (write_buffer(s->fd, 0, dh, size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800988 error_setg(errp, "dump: failed to write disk dump header");
qiaonuohan298f1162014-02-18 14:11:32 +0800989 goto out;
990 }
991
992 /* write sub header */
993 size = sizeof(KdumpSubHeader32);
994 kh = g_malloc0(size);
995
996 /* 64bit max_mapnr_64 */
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200997 kh->max_mapnr_64 = cpu_to_dump64(s, s->max_mapnr);
Andrew Jonesb6e05aa2016-01-11 20:56:21 +0100998 kh->phys_base = cpu_to_dump32(s, s->dump_info.phys_base);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200999 kh->dump_level = cpu_to_dump32(s, DUMP_LEVEL);
qiaonuohan298f1162014-02-18 14:11:32 +08001000
1001 offset_note = DISKDUMP_HEADER_BLOCKS * block_size + size;
Marc-André Lureau9ada5752017-09-11 18:59:27 +02001002 if (s->guest_note &&
1003 note_name_equal(s, s->guest_note, "VMCOREINFO")) {
1004 uint64_t hsize, name_size, size_vmcoreinfo_desc, offset_vmcoreinfo;
1005
1006 get_note_sizes(s, s->guest_note,
1007 &hsize, &name_size, &size_vmcoreinfo_desc);
1008 offset_vmcoreinfo = offset_note + s->note_size - s->guest_note_size +
1009 (DIV_ROUND_UP(hsize, 4) + DIV_ROUND_UP(name_size, 4)) * 4;
1010 kh->offset_vmcoreinfo = cpu_to_dump64(s, offset_vmcoreinfo);
1011 kh->size_vmcoreinfo = cpu_to_dump32(s, size_vmcoreinfo_desc);
1012 }
1013
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001014 kh->offset_note = cpu_to_dump64(s, offset_note);
1015 kh->note_size = cpu_to_dump32(s, s->note_size);
qiaonuohan298f1162014-02-18 14:11:32 +08001016
1017 if (write_buffer(s->fd, DISKDUMP_HEADER_BLOCKS *
1018 block_size, kh, size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001019 error_setg(errp, "dump: failed to write kdump sub header");
qiaonuohan298f1162014-02-18 14:11:32 +08001020 goto out;
1021 }
1022
1023 /* write note */
1024 s->note_buf = g_malloc0(s->note_size);
1025 s->note_buf_offset = 0;
1026
1027 /* use s->note_buf to store notes temporarily */
Janosch Frank86a518b2022-03-30 12:35:55 +00001028 write_elf32_notes(buf_write_note, s, errp);
1029 if (*errp) {
qiaonuohan298f1162014-02-18 14:11:32 +08001030 goto out;
1031 }
qiaonuohan298f1162014-02-18 14:11:32 +08001032 if (write_buffer(s->fd, offset_note, s->note_buf,
1033 s->note_size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001034 error_setg(errp, "dump: failed to write notes");
qiaonuohan298f1162014-02-18 14:11:32 +08001035 goto out;
1036 }
1037
1038 /* get offset of dump_bitmap */
1039 s->offset_dump_bitmap = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size) *
1040 block_size;
1041
1042 /* get offset of page */
1043 s->offset_page = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size + bitmap_blocks) *
1044 block_size;
1045
1046out:
1047 g_free(dh);
1048 g_free(kh);
1049 g_free(s->note_buf);
qiaonuohan298f1162014-02-18 14:11:32 +08001050}
1051
1052/* write common header, sub header and elf note to vmcore */
zhanghailiang4c7e2512014-10-09 14:13:11 +08001053static void create_header64(DumpState *s, Error **errp)
qiaonuohan298f1162014-02-18 14:11:32 +08001054{
Janosch Frank86a518b2022-03-30 12:35:55 +00001055 ERRP_GUARD();
qiaonuohan298f1162014-02-18 14:11:32 +08001056 DiskDumpHeader64 *dh = NULL;
1057 KdumpSubHeader64 *kh = NULL;
1058 size_t size;
qiaonuohan298f1162014-02-18 14:11:32 +08001059 uint32_t block_size;
1060 uint32_t sub_hdr_size;
1061 uint32_t bitmap_blocks;
1062 uint32_t status = 0;
1063 uint64_t offset_note;
1064
1065 /* write common header, the version of kdump-compressed format is 6th */
1066 size = sizeof(DiskDumpHeader64);
1067 dh = g_malloc0(size);
1068
Eric Blake84c868f2018-03-27 15:21:51 -05001069 memcpy(dh->signature, KDUMP_SIGNATURE, SIG_LEN);
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001070 dh->header_version = cpu_to_dump32(s, 6);
Andrew Jones8161bef2016-01-11 20:56:20 +01001071 block_size = s->dump_info.page_size;
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001072 dh->block_size = cpu_to_dump32(s, block_size);
qiaonuohan298f1162014-02-18 14:11:32 +08001073 sub_hdr_size = sizeof(struct KdumpSubHeader64) + s->note_size;
1074 sub_hdr_size = DIV_ROUND_UP(sub_hdr_size, block_size);
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001075 dh->sub_hdr_size = cpu_to_dump32(s, sub_hdr_size);
qiaonuohan298f1162014-02-18 14:11:32 +08001076 /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001077 dh->max_mapnr = cpu_to_dump32(s, MIN(s->max_mapnr, UINT_MAX));
1078 dh->nr_cpus = cpu_to_dump32(s, s->nr_cpus);
qiaonuohan298f1162014-02-18 14:11:32 +08001079 bitmap_blocks = DIV_ROUND_UP(s->len_dump_bitmap, block_size) * 2;
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001080 dh->bitmap_blocks = cpu_to_dump32(s, bitmap_blocks);
qiaonuohan4ab23a92014-02-18 14:11:37 +08001081 strncpy(dh->utsname.machine, ELF_MACHINE_UNAME, sizeof(dh->utsname.machine));
qiaonuohan298f1162014-02-18 14:11:32 +08001082
1083 if (s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) {
1084 status |= DUMP_DH_COMPRESSED_ZLIB;
1085 }
1086#ifdef CONFIG_LZO
1087 if (s->flag_compress & DUMP_DH_COMPRESSED_LZO) {
1088 status |= DUMP_DH_COMPRESSED_LZO;
1089 }
1090#endif
1091#ifdef CONFIG_SNAPPY
1092 if (s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) {
1093 status |= DUMP_DH_COMPRESSED_SNAPPY;
1094 }
1095#endif
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001096 dh->status = cpu_to_dump32(s, status);
qiaonuohan298f1162014-02-18 14:11:32 +08001097
1098 if (write_buffer(s->fd, 0, dh, size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001099 error_setg(errp, "dump: failed to write disk dump header");
qiaonuohan298f1162014-02-18 14:11:32 +08001100 goto out;
1101 }
1102
1103 /* write sub header */
1104 size = sizeof(KdumpSubHeader64);
1105 kh = g_malloc0(size);
1106
1107 /* 64bit max_mapnr_64 */
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001108 kh->max_mapnr_64 = cpu_to_dump64(s, s->max_mapnr);
Andrew Jonesb6e05aa2016-01-11 20:56:21 +01001109 kh->phys_base = cpu_to_dump64(s, s->dump_info.phys_base);
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001110 kh->dump_level = cpu_to_dump32(s, DUMP_LEVEL);
qiaonuohan298f1162014-02-18 14:11:32 +08001111
1112 offset_note = DISKDUMP_HEADER_BLOCKS * block_size + size;
Marc-André Lureau9ada5752017-09-11 18:59:27 +02001113 if (s->guest_note &&
1114 note_name_equal(s, s->guest_note, "VMCOREINFO")) {
1115 uint64_t hsize, name_size, size_vmcoreinfo_desc, offset_vmcoreinfo;
1116
1117 get_note_sizes(s, s->guest_note,
1118 &hsize, &name_size, &size_vmcoreinfo_desc);
1119 offset_vmcoreinfo = offset_note + s->note_size - s->guest_note_size +
1120 (DIV_ROUND_UP(hsize, 4) + DIV_ROUND_UP(name_size, 4)) * 4;
1121 kh->offset_vmcoreinfo = cpu_to_dump64(s, offset_vmcoreinfo);
1122 kh->size_vmcoreinfo = cpu_to_dump64(s, size_vmcoreinfo_desc);
1123 }
1124
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001125 kh->offset_note = cpu_to_dump64(s, offset_note);
1126 kh->note_size = cpu_to_dump64(s, s->note_size);
qiaonuohan298f1162014-02-18 14:11:32 +08001127
1128 if (write_buffer(s->fd, DISKDUMP_HEADER_BLOCKS *
1129 block_size, kh, size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001130 error_setg(errp, "dump: failed to write kdump sub header");
qiaonuohan298f1162014-02-18 14:11:32 +08001131 goto out;
1132 }
1133
1134 /* write note */
1135 s->note_buf = g_malloc0(s->note_size);
1136 s->note_buf_offset = 0;
1137
1138 /* use s->note_buf to store notes temporarily */
Janosch Frank86a518b2022-03-30 12:35:55 +00001139 write_elf64_notes(buf_write_note, s, errp);
1140 if (*errp) {
qiaonuohan298f1162014-02-18 14:11:32 +08001141 goto out;
1142 }
1143
1144 if (write_buffer(s->fd, offset_note, s->note_buf,
1145 s->note_size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001146 error_setg(errp, "dump: failed to write notes");
qiaonuohan298f1162014-02-18 14:11:32 +08001147 goto out;
1148 }
1149
1150 /* get offset of dump_bitmap */
1151 s->offset_dump_bitmap = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size) *
1152 block_size;
1153
1154 /* get offset of page */
1155 s->offset_page = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size + bitmap_blocks) *
1156 block_size;
1157
1158out:
1159 g_free(dh);
1160 g_free(kh);
1161 g_free(s->note_buf);
qiaonuohan298f1162014-02-18 14:11:32 +08001162}
1163
zhanghailiang4c7e2512014-10-09 14:13:11 +08001164static void write_dump_header(DumpState *s, Error **errp)
qiaonuohan298f1162014-02-18 14:11:32 +08001165{
Janosch Frank05bbaa502022-03-30 12:36:00 +00001166 if (dump_is_64bit(s)) {
Markus Armbruster992861f2020-07-07 18:06:04 +02001167 create_header64(s, errp);
Janosch Frank05bbaa502022-03-30 12:36:00 +00001168 } else {
1169 create_header32(s, errp);
zhanghailiang4c7e2512014-10-09 14:13:11 +08001170 }
qiaonuohan298f1162014-02-18 14:11:32 +08001171}
1172
Andrew Jones8161bef2016-01-11 20:56:20 +01001173static size_t dump_bitmap_get_bufsize(DumpState *s)
1174{
1175 return s->dump_info.page_size;
1176}
1177
qiaonuohand0686c72014-02-18 14:11:33 +08001178/*
1179 * set dump_bitmap sequencely. the bit before last_pfn is not allowed to be
1180 * rewritten, so if need to set the first bit, set last_pfn and pfn to 0.
1181 * set_dump_bitmap will always leave the recently set bit un-sync. And setting
1182 * (last bit + sizeof(buf) * 8) to 0 will do flushing the content in buf into
1183 * vmcore, ie. synchronizing un-sync bit into vmcore.
1184 */
1185static int set_dump_bitmap(uint64_t last_pfn, uint64_t pfn, bool value,
1186 uint8_t *buf, DumpState *s)
1187{
1188 off_t old_offset, new_offset;
1189 off_t offset_bitmap1, offset_bitmap2;
1190 uint32_t byte, bit;
Andrew Jones8161bef2016-01-11 20:56:20 +01001191 size_t bitmap_bufsize = dump_bitmap_get_bufsize(s);
1192 size_t bits_per_buf = bitmap_bufsize * CHAR_BIT;
qiaonuohand0686c72014-02-18 14:11:33 +08001193
1194 /* should not set the previous place */
1195 assert(last_pfn <= pfn);
1196
1197 /*
1198 * if the bit needed to be set is not cached in buf, flush the data in buf
1199 * to vmcore firstly.
1200 * making new_offset be bigger than old_offset can also sync remained data
1201 * into vmcore.
1202 */
Andrew Jones8161bef2016-01-11 20:56:20 +01001203 old_offset = bitmap_bufsize * (last_pfn / bits_per_buf);
1204 new_offset = bitmap_bufsize * (pfn / bits_per_buf);
qiaonuohand0686c72014-02-18 14:11:33 +08001205
1206 while (old_offset < new_offset) {
1207 /* calculate the offset and write dump_bitmap */
1208 offset_bitmap1 = s->offset_dump_bitmap + old_offset;
1209 if (write_buffer(s->fd, offset_bitmap1, buf,
Andrew Jones8161bef2016-01-11 20:56:20 +01001210 bitmap_bufsize) < 0) {
qiaonuohand0686c72014-02-18 14:11:33 +08001211 return -1;
1212 }
1213
1214 /* dump level 1 is chosen, so 1st and 2nd bitmap are same */
1215 offset_bitmap2 = s->offset_dump_bitmap + s->len_dump_bitmap +
1216 old_offset;
1217 if (write_buffer(s->fd, offset_bitmap2, buf,
Andrew Jones8161bef2016-01-11 20:56:20 +01001218 bitmap_bufsize) < 0) {
qiaonuohand0686c72014-02-18 14:11:33 +08001219 return -1;
1220 }
1221
Andrew Jones8161bef2016-01-11 20:56:20 +01001222 memset(buf, 0, bitmap_bufsize);
1223 old_offset += bitmap_bufsize;
qiaonuohand0686c72014-02-18 14:11:33 +08001224 }
1225
1226 /* get the exact place of the bit in the buf, and set it */
Andrew Jones8161bef2016-01-11 20:56:20 +01001227 byte = (pfn % bits_per_buf) / CHAR_BIT;
1228 bit = (pfn % bits_per_buf) % CHAR_BIT;
qiaonuohand0686c72014-02-18 14:11:33 +08001229 if (value) {
1230 buf[byte] |= 1u << bit;
1231 } else {
1232 buf[byte] &= ~(1u << bit);
1233 }
1234
1235 return 0;
1236}
1237
Andrew Jones8161bef2016-01-11 20:56:20 +01001238static uint64_t dump_paddr_to_pfn(DumpState *s, uint64_t addr)
1239{
1240 int target_page_shift = ctz32(s->dump_info.page_size);
1241
1242 return (addr >> target_page_shift) - ARCH_PFN_OFFSET;
1243}
1244
1245static uint64_t dump_pfn_to_paddr(DumpState *s, uint64_t pfn)
1246{
1247 int target_page_shift = ctz32(s->dump_info.page_size);
1248
1249 return (pfn + ARCH_PFN_OFFSET) << target_page_shift;
1250}
1251
qiaonuohand0686c72014-02-18 14:11:33 +08001252/*
Marc-André Lureau94d78842022-09-05 16:06:21 +04001253 * Return the page frame number and the page content in *bufptr. bufptr can be
1254 * NULL. If not NULL, *bufptr must contains a target page size of pre-allocated
1255 * memory. This is not necessarily the memory returned.
qiaonuohand0686c72014-02-18 14:11:33 +08001256 */
1257static bool get_next_page(GuestPhysBlock **blockptr, uint64_t *pfnptr,
1258 uint8_t **bufptr, DumpState *s)
1259{
1260 GuestPhysBlock *block = *blockptr;
Marc-André Lureau94d78842022-09-05 16:06:21 +04001261 uint32_t page_size = s->dump_info.page_size;
1262 uint8_t *buf = NULL, *hbuf;
1263 hwaddr addr;
qiaonuohand0686c72014-02-18 14:11:33 +08001264
1265 /* block == NULL means the start of the iteration */
1266 if (!block) {
1267 block = QTAILQ_FIRST(&s->guest_phys_blocks.head);
1268 *blockptr = block;
Marc-André Lureau08df3432022-08-25 12:40:12 +04001269 addr = block->target_start;
Marc-André Lureau94d78842022-09-05 16:06:21 +04001270 *pfnptr = dump_paddr_to_pfn(s, addr);
Marc-André Lureau08df3432022-08-25 12:40:12 +04001271 } else {
Marc-André Lureau94d78842022-09-05 16:06:21 +04001272 *pfnptr += 1;
1273 addr = dump_pfn_to_paddr(s, *pfnptr);
qiaonuohand0686c72014-02-18 14:11:33 +08001274 }
Marc-André Lureau08df3432022-08-25 12:40:12 +04001275 assert(block != NULL);
qiaonuohand0686c72014-02-18 14:11:33 +08001276
Marc-André Lureau94d78842022-09-05 16:06:21 +04001277 while (1) {
1278 if (addr >= block->target_start && addr < block->target_end) {
1279 size_t n = MIN(block->target_end - addr, page_size - addr % page_size);
1280 hbuf = block->host_addr + (addr - block->target_start);
1281 if (!buf) {
1282 if (n == page_size) {
1283 /* this is a whole target page, go for it */
1284 assert(addr % page_size == 0);
1285 buf = hbuf;
1286 break;
1287 } else if (bufptr) {
1288 assert(*bufptr);
1289 buf = *bufptr;
1290 memset(buf, 0, page_size);
1291 } else {
1292 return true;
1293 }
1294 }
1295
1296 memcpy(buf + addr % page_size, hbuf, n);
1297 addr += n;
1298 if (addr % page_size == 0) {
1299 /* we filled up the page */
1300 break;
1301 }
1302 } else {
1303 /* the next page is in the next block */
1304 *blockptr = block = QTAILQ_NEXT(block, next);
1305 if (!block) {
1306 break;
1307 }
1308
1309 addr = block->target_start;
1310 /* are we still in the same page? */
1311 if (dump_paddr_to_pfn(s, addr) != *pfnptr) {
1312 if (buf) {
1313 /* no, but we already filled something earlier, return it */
1314 break;
1315 } else {
1316 /* else continue from there */
1317 *pfnptr = dump_paddr_to_pfn(s, addr);
1318 }
1319 }
qiaonuohand0686c72014-02-18 14:11:33 +08001320 }
qiaonuohand0686c72014-02-18 14:11:33 +08001321 }
1322
1323 if (bufptr) {
1324 *bufptr = buf;
1325 }
1326
Marc-André Lureau94d78842022-09-05 16:06:21 +04001327 return buf != NULL;
qiaonuohand0686c72014-02-18 14:11:33 +08001328}
1329
zhanghailiang4c7e2512014-10-09 14:13:11 +08001330static void write_dump_bitmap(DumpState *s, Error **errp)
qiaonuohand0686c72014-02-18 14:11:33 +08001331{
1332 int ret = 0;
1333 uint64_t last_pfn, pfn;
1334 void *dump_bitmap_buf;
1335 size_t num_dumpable;
1336 GuestPhysBlock *block_iter = NULL;
Andrew Jones8161bef2016-01-11 20:56:20 +01001337 size_t bitmap_bufsize = dump_bitmap_get_bufsize(s);
1338 size_t bits_per_buf = bitmap_bufsize * CHAR_BIT;
qiaonuohand0686c72014-02-18 14:11:33 +08001339
1340 /* dump_bitmap_buf is used to store dump_bitmap temporarily */
Andrew Jones8161bef2016-01-11 20:56:20 +01001341 dump_bitmap_buf = g_malloc0(bitmap_bufsize);
qiaonuohand0686c72014-02-18 14:11:33 +08001342
1343 num_dumpable = 0;
1344 last_pfn = 0;
1345
1346 /*
1347 * exam memory page by page, and set the bit in dump_bitmap corresponded
1348 * to the existing page.
1349 */
1350 while (get_next_page(&block_iter, &pfn, NULL, s)) {
1351 ret = set_dump_bitmap(last_pfn, pfn, true, dump_bitmap_buf, s);
1352 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001353 error_setg(errp, "dump: failed to set dump_bitmap");
qiaonuohand0686c72014-02-18 14:11:33 +08001354 goto out;
1355 }
1356
1357 last_pfn = pfn;
1358 num_dumpable++;
1359 }
1360
1361 /*
1362 * set_dump_bitmap will always leave the recently set bit un-sync. Here we
Andrew Jones8161bef2016-01-11 20:56:20 +01001363 * set the remaining bits from last_pfn to the end of the bitmap buffer to
1364 * 0. With those set, the un-sync bit will be synchronized into the vmcore.
qiaonuohand0686c72014-02-18 14:11:33 +08001365 */
1366 if (num_dumpable > 0) {
Andrew Jones8161bef2016-01-11 20:56:20 +01001367 ret = set_dump_bitmap(last_pfn, last_pfn + bits_per_buf, false,
qiaonuohand0686c72014-02-18 14:11:33 +08001368 dump_bitmap_buf, s);
1369 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001370 error_setg(errp, "dump: failed to sync dump_bitmap");
qiaonuohand0686c72014-02-18 14:11:33 +08001371 goto out;
1372 }
1373 }
1374
1375 /* number of dumpable pages that will be dumped later */
1376 s->num_dumpable = num_dumpable;
1377
1378out:
1379 g_free(dump_bitmap_buf);
qiaonuohand0686c72014-02-18 14:11:33 +08001380}
1381
qiaonuohan64cfba62014-02-18 14:11:34 +08001382static void prepare_data_cache(DataCache *data_cache, DumpState *s,
1383 off_t offset)
1384{
1385 data_cache->fd = s->fd;
1386 data_cache->data_size = 0;
Andrew Jones8161bef2016-01-11 20:56:20 +01001387 data_cache->buf_size = 4 * dump_bitmap_get_bufsize(s);
1388 data_cache->buf = g_malloc0(data_cache->buf_size);
qiaonuohan64cfba62014-02-18 14:11:34 +08001389 data_cache->offset = offset;
1390}
1391
1392static int write_cache(DataCache *dc, const void *buf, size_t size,
1393 bool flag_sync)
1394{
1395 /*
1396 * dc->buf_size should not be less than size, otherwise dc will never be
1397 * enough
1398 */
1399 assert(size <= dc->buf_size);
1400
1401 /*
1402 * if flag_sync is set, synchronize data in dc->buf into vmcore.
1403 * otherwise check if the space is enough for caching data in buf, if not,
1404 * write the data in dc->buf to dc->fd and reset dc->buf
1405 */
1406 if ((!flag_sync && dc->data_size + size > dc->buf_size) ||
1407 (flag_sync && dc->data_size > 0)) {
1408 if (write_buffer(dc->fd, dc->offset, dc->buf, dc->data_size) < 0) {
1409 return -1;
1410 }
1411
1412 dc->offset += dc->data_size;
1413 dc->data_size = 0;
1414 }
1415
1416 if (!flag_sync) {
1417 memcpy(dc->buf + dc->data_size, buf, size);
1418 dc->data_size += size;
1419 }
1420
1421 return 0;
1422}
1423
1424static void free_data_cache(DataCache *data_cache)
1425{
1426 g_free(data_cache->buf);
1427}
1428
qiaonuohand12f57e2014-02-18 14:11:35 +08001429static size_t get_len_buf_out(size_t page_size, uint32_t flag_compress)
1430{
Laszlo Ersekb87ef352014-05-20 13:42:53 +02001431 switch (flag_compress) {
1432 case DUMP_DH_COMPRESSED_ZLIB:
1433 return compressBound(page_size);
qiaonuohand12f57e2014-02-18 14:11:35 +08001434
Laszlo Ersekb87ef352014-05-20 13:42:53 +02001435 case DUMP_DH_COMPRESSED_LZO:
1436 /*
1437 * LZO will expand incompressible data by a little amount. Please check
1438 * the following URL to see the expansion calculation:
1439 * http://www.oberhumer.com/opensource/lzo/lzofaq.php
1440 */
1441 return page_size + page_size / 16 + 64 + 3;
qiaonuohand12f57e2014-02-18 14:11:35 +08001442
1443#ifdef CONFIG_SNAPPY
Laszlo Ersekb87ef352014-05-20 13:42:53 +02001444 case DUMP_DH_COMPRESSED_SNAPPY:
1445 return snappy_max_compressed_length(page_size);
qiaonuohand12f57e2014-02-18 14:11:35 +08001446#endif
Laszlo Ersekb87ef352014-05-20 13:42:53 +02001447 }
1448 return 0;
qiaonuohand12f57e2014-02-18 14:11:35 +08001449}
1450
zhanghailiang4c7e2512014-10-09 14:13:11 +08001451static void write_dump_pages(DumpState *s, Error **errp)
qiaonuohand12f57e2014-02-18 14:11:35 +08001452{
1453 int ret = 0;
1454 DataCache page_desc, page_data;
1455 size_t len_buf_out, size_out;
1456#ifdef CONFIG_LZO
1457 lzo_bytep wrkmem = NULL;
1458#endif
1459 uint8_t *buf_out = NULL;
1460 off_t offset_desc, offset_data;
1461 PageDescriptor pd, pd_zero;
1462 uint8_t *buf;
qiaonuohand12f57e2014-02-18 14:11:35 +08001463 GuestPhysBlock *block_iter = NULL;
1464 uint64_t pfn_iter;
Marc-André Lureau94d78842022-09-05 16:06:21 +04001465 g_autofree uint8_t *page = NULL;
qiaonuohand12f57e2014-02-18 14:11:35 +08001466
1467 /* get offset of page_desc and page_data in dump file */
1468 offset_desc = s->offset_page;
1469 offset_data = offset_desc + sizeof(PageDescriptor) * s->num_dumpable;
1470
1471 prepare_data_cache(&page_desc, s, offset_desc);
1472 prepare_data_cache(&page_data, s, offset_data);
1473
1474 /* prepare buffer to store compressed data */
Andrew Jones8161bef2016-01-11 20:56:20 +01001475 len_buf_out = get_len_buf_out(s->dump_info.page_size, s->flag_compress);
Laszlo Ersekb87ef352014-05-20 13:42:53 +02001476 assert(len_buf_out != 0);
qiaonuohand12f57e2014-02-18 14:11:35 +08001477
1478#ifdef CONFIG_LZO
1479 wrkmem = g_malloc(LZO1X_1_MEM_COMPRESS);
1480#endif
1481
1482 buf_out = g_malloc(len_buf_out);
1483
1484 /*
1485 * init zero page's page_desc and page_data, because every zero page
1486 * uses the same page_data
1487 */
Andrew Jones8161bef2016-01-11 20:56:20 +01001488 pd_zero.size = cpu_to_dump32(s, s->dump_info.page_size);
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001489 pd_zero.flags = cpu_to_dump32(s, 0);
1490 pd_zero.offset = cpu_to_dump64(s, offset_data);
1491 pd_zero.page_flags = cpu_to_dump64(s, 0);
Andrew Jones8161bef2016-01-11 20:56:20 +01001492 buf = g_malloc0(s->dump_info.page_size);
1493 ret = write_cache(&page_data, buf, s->dump_info.page_size, false);
qiaonuohand12f57e2014-02-18 14:11:35 +08001494 g_free(buf);
1495 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001496 error_setg(errp, "dump: failed to write page data (zero page)");
qiaonuohand12f57e2014-02-18 14:11:35 +08001497 goto out;
1498 }
1499
Andrew Jones8161bef2016-01-11 20:56:20 +01001500 offset_data += s->dump_info.page_size;
Marc-André Lureau94d78842022-09-05 16:06:21 +04001501 page = g_malloc(s->dump_info.page_size);
qiaonuohand12f57e2014-02-18 14:11:35 +08001502
1503 /*
1504 * dump memory to vmcore page by page. zero page will all be resided in the
1505 * first page of page section
1506 */
Marc-André Lureau94d78842022-09-05 16:06:21 +04001507 for (buf = page; get_next_page(&block_iter, &pfn_iter, &buf, s); buf = page) {
qiaonuohand12f57e2014-02-18 14:11:35 +08001508 /* check zero page */
Juan Quintelaf13f22b2021-11-18 15:57:44 +01001509 if (buffer_is_zero(buf, s->dump_info.page_size)) {
qiaonuohand12f57e2014-02-18 14:11:35 +08001510 ret = write_cache(&page_desc, &pd_zero, sizeof(PageDescriptor),
1511 false);
1512 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001513 error_setg(errp, "dump: failed to write page desc");
qiaonuohand12f57e2014-02-18 14:11:35 +08001514 goto out;
1515 }
1516 } else {
1517 /*
1518 * not zero page, then:
1519 * 1. compress the page
1520 * 2. write the compressed page into the cache of page_data
1521 * 3. get page desc of the compressed page and write it into the
1522 * cache of page_desc
1523 *
1524 * only one compression format will be used here, for
1525 * s->flag_compress is set. But when compression fails to work,
1526 * we fall back to save in plaintext.
1527 */
1528 size_out = len_buf_out;
1529 if ((s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) &&
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001530 (compress2(buf_out, (uLongf *)&size_out, buf,
Andrew Jones8161bef2016-01-11 20:56:20 +01001531 s->dump_info.page_size, Z_BEST_SPEED) == Z_OK) &&
1532 (size_out < s->dump_info.page_size)) {
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001533 pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_ZLIB);
1534 pd.size = cpu_to_dump32(s, size_out);
qiaonuohand12f57e2014-02-18 14:11:35 +08001535
1536 ret = write_cache(&page_data, buf_out, size_out, false);
1537 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001538 error_setg(errp, "dump: failed to write page data");
qiaonuohand12f57e2014-02-18 14:11:35 +08001539 goto out;
1540 }
1541#ifdef CONFIG_LZO
1542 } else if ((s->flag_compress & DUMP_DH_COMPRESSED_LZO) &&
Andrew Jones8161bef2016-01-11 20:56:20 +01001543 (lzo1x_1_compress(buf, s->dump_info.page_size, buf_out,
qiaonuohand12f57e2014-02-18 14:11:35 +08001544 (lzo_uint *)&size_out, wrkmem) == LZO_E_OK) &&
Andrew Jones8161bef2016-01-11 20:56:20 +01001545 (size_out < s->dump_info.page_size)) {
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001546 pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_LZO);
1547 pd.size = cpu_to_dump32(s, size_out);
qiaonuohand12f57e2014-02-18 14:11:35 +08001548
1549 ret = write_cache(&page_data, buf_out, size_out, false);
1550 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001551 error_setg(errp, "dump: failed to write page data");
qiaonuohand12f57e2014-02-18 14:11:35 +08001552 goto out;
1553 }
1554#endif
1555#ifdef CONFIG_SNAPPY
1556 } else if ((s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) &&
Andrew Jones8161bef2016-01-11 20:56:20 +01001557 (snappy_compress((char *)buf, s->dump_info.page_size,
qiaonuohand12f57e2014-02-18 14:11:35 +08001558 (char *)buf_out, &size_out) == SNAPPY_OK) &&
Andrew Jones8161bef2016-01-11 20:56:20 +01001559 (size_out < s->dump_info.page_size)) {
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001560 pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_SNAPPY);
1561 pd.size = cpu_to_dump32(s, size_out);
qiaonuohand12f57e2014-02-18 14:11:35 +08001562
1563 ret = write_cache(&page_data, buf_out, size_out, false);
1564 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001565 error_setg(errp, "dump: failed to write page data");
qiaonuohand12f57e2014-02-18 14:11:35 +08001566 goto out;
1567 }
1568#endif
1569 } else {
1570 /*
1571 * fall back to save in plaintext, size_out should be
Andrew Jones8161bef2016-01-11 20:56:20 +01001572 * assigned the target's page size
qiaonuohand12f57e2014-02-18 14:11:35 +08001573 */
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001574 pd.flags = cpu_to_dump32(s, 0);
Andrew Jones8161bef2016-01-11 20:56:20 +01001575 size_out = s->dump_info.page_size;
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001576 pd.size = cpu_to_dump32(s, size_out);
qiaonuohand12f57e2014-02-18 14:11:35 +08001577
Andrew Jones8161bef2016-01-11 20:56:20 +01001578 ret = write_cache(&page_data, buf,
1579 s->dump_info.page_size, false);
qiaonuohand12f57e2014-02-18 14:11:35 +08001580 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001581 error_setg(errp, "dump: failed to write page data");
qiaonuohand12f57e2014-02-18 14:11:35 +08001582 goto out;
1583 }
1584 }
1585
1586 /* get and write page desc here */
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001587 pd.page_flags = cpu_to_dump64(s, 0);
1588 pd.offset = cpu_to_dump64(s, offset_data);
qiaonuohand12f57e2014-02-18 14:11:35 +08001589 offset_data += size_out;
1590
1591 ret = write_cache(&page_desc, &pd, sizeof(PageDescriptor), false);
1592 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001593 error_setg(errp, "dump: failed to write page desc");
qiaonuohand12f57e2014-02-18 14:11:35 +08001594 goto out;
1595 }
1596 }
Peter Xu2264c2c2016-02-18 13:16:53 +08001597 s->written_size += s->dump_info.page_size;
qiaonuohand12f57e2014-02-18 14:11:35 +08001598 }
1599
1600 ret = write_cache(&page_desc, NULL, 0, true);
1601 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001602 error_setg(errp, "dump: failed to sync cache for page_desc");
qiaonuohand12f57e2014-02-18 14:11:35 +08001603 goto out;
1604 }
1605 ret = write_cache(&page_data, NULL, 0, true);
1606 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001607 error_setg(errp, "dump: failed to sync cache for page_data");
qiaonuohand12f57e2014-02-18 14:11:35 +08001608 goto out;
1609 }
1610
1611out:
1612 free_data_cache(&page_desc);
1613 free_data_cache(&page_data);
1614
1615#ifdef CONFIG_LZO
1616 g_free(wrkmem);
1617#endif
1618
1619 g_free(buf_out);
qiaonuohand12f57e2014-02-18 14:11:35 +08001620}
1621
zhanghailiang4c7e2512014-10-09 14:13:11 +08001622static void create_kdump_vmcore(DumpState *s, Error **errp)
qiaonuohanb53ccc32014-02-18 14:11:36 +08001623{
Janosch Frank86a518b2022-03-30 12:35:55 +00001624 ERRP_GUARD();
qiaonuohanb53ccc32014-02-18 14:11:36 +08001625 int ret;
1626
1627 /*
1628 * the kdump-compressed format is:
1629 * File offset
1630 * +------------------------------------------+ 0x0
1631 * | main header (struct disk_dump_header) |
1632 * |------------------------------------------+ block 1
1633 * | sub header (struct kdump_sub_header) |
1634 * |------------------------------------------+ block 2
1635 * | 1st-dump_bitmap |
1636 * |------------------------------------------+ block 2 + X blocks
1637 * | 2nd-dump_bitmap | (aligned by block)
1638 * |------------------------------------------+ block 2 + 2 * X blocks
1639 * | page desc for pfn 0 (struct page_desc) | (aligned by block)
1640 * | page desc for pfn 1 (struct page_desc) |
1641 * | : |
1642 * |------------------------------------------| (not aligned by block)
1643 * | page data (pfn 0) |
1644 * | page data (pfn 1) |
1645 * | : |
1646 * +------------------------------------------+
1647 */
1648
1649 ret = write_start_flat_header(s->fd);
1650 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001651 error_setg(errp, "dump: failed to write start flat header");
zhanghailiang4c7e2512014-10-09 14:13:11 +08001652 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001653 }
1654
Janosch Frank86a518b2022-03-30 12:35:55 +00001655 write_dump_header(s, errp);
1656 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +08001657 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001658 }
1659
Janosch Frank86a518b2022-03-30 12:35:55 +00001660 write_dump_bitmap(s, errp);
1661 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +08001662 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001663 }
1664
Janosch Frank86a518b2022-03-30 12:35:55 +00001665 write_dump_pages(s, errp);
1666 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +08001667 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001668 }
1669
1670 ret = write_end_flat_header(s->fd);
1671 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001672 error_setg(errp, "dump: failed to write end flat header");
zhanghailiang4c7e2512014-10-09 14:13:11 +08001673 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001674 }
qiaonuohanb53ccc32014-02-18 14:11:36 +08001675}
1676
Janosch Frank0c2994a2022-08-11 12:10:57 +00001677static int validate_start_block(DumpState *s)
Wen Congyang783e9b42012-05-07 12:10:47 +08001678{
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001679 GuestPhysBlock *block;
Wen Congyang783e9b42012-05-07 12:10:47 +08001680
Janosch Frankdddf7252022-08-11 12:10:58 +00001681 if (!dump_has_filter(s)) {
Wen Congyang783e9b42012-05-07 12:10:47 +08001682 return 0;
1683 }
1684
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001685 QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
Janosch Frank0c2994a2022-08-11 12:10:57 +00001686 /* This block is out of the range */
Janosch Frankdddf7252022-08-11 12:10:58 +00001687 if (block->target_start >= s->filter_area_begin + s->filter_area_length ||
1688 block->target_end <= s->filter_area_begin) {
Wen Congyang783e9b42012-05-07 12:10:47 +08001689 continue;
1690 }
Janosch Frank0c2994a2022-08-11 12:10:57 +00001691 return 0;
1692 }
Wen Congyang783e9b42012-05-07 12:10:47 +08001693
1694 return -1;
1695}
1696
qiaonuohan7aad2482014-02-18 14:11:31 +08001697static void get_max_mapnr(DumpState *s)
1698{
1699 GuestPhysBlock *last_block;
1700
Paolo Bonzinieae3eb32018-12-06 13:10:34 +01001701 last_block = QTAILQ_LAST(&s->guest_phys_blocks.head);
Andrew Jones8161bef2016-01-11 20:56:20 +01001702 s->max_mapnr = dump_paddr_to_pfn(s, last_block->target_end);
qiaonuohan7aad2482014-02-18 14:11:31 +08001703}
1704
Peter Xubaf28f52016-02-18 13:16:48 +08001705static DumpState dump_state_global = { .status = DUMP_STATUS_NONE };
1706
1707static void dump_state_prepare(DumpState *s)
1708{
1709 /* zero the struct, setting status to active */
1710 *s = (DumpState) { .status = DUMP_STATUS_ACTIVE };
1711}
1712
Marc-André Lureau544803c2022-03-23 19:57:31 +04001713bool qemu_system_dump_in_progress(void)
Peter Xu65d64f32016-02-18 13:16:49 +08001714{
1715 DumpState *state = &dump_state_global;
Stefan Hajnoczid73415a2020-09-23 11:56:46 +01001716 return (qatomic_read(&state->status) == DUMP_STATUS_ACTIVE);
Peter Xu65d64f32016-02-18 13:16:49 +08001717}
1718
Janosch Frankc370d532022-08-11 12:10:59 +00001719/*
1720 * calculate total size of memory to be dumped (taking filter into
1721 * account.)
1722 */
Peter Xu2264c2c2016-02-18 13:16:53 +08001723static int64_t dump_calculate_size(DumpState *s)
1724{
1725 GuestPhysBlock *block;
Janosch Frankc370d532022-08-11 12:10:59 +00001726 int64_t total = 0;
Peter Xu2264c2c2016-02-18 13:16:53 +08001727
1728 QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
Janosch Frankc370d532022-08-11 12:10:59 +00001729 total += dump_filtered_memblock_size(block,
1730 s->filter_area_begin,
1731 s->filter_area_length);
Peter Xu2264c2c2016-02-18 13:16:53 +08001732 }
1733
1734 return total;
1735}
1736
Marc-André Lureaud9feb512017-09-11 18:59:26 +02001737static void vmcoreinfo_update_phys_base(DumpState *s)
1738{
1739 uint64_t size, note_head_size, name_size, phys_base;
1740 char **lines;
1741 uint8_t *vmci;
1742 size_t i;
1743
1744 if (!note_name_equal(s, s->guest_note, "VMCOREINFO")) {
1745 return;
1746 }
1747
1748 get_note_sizes(s, s->guest_note, &note_head_size, &name_size, &size);
1749 note_head_size = ROUND_UP(note_head_size, 4);
1750
1751 vmci = s->guest_note + note_head_size + ROUND_UP(name_size, 4);
1752 *(vmci + size) = '\0';
1753
1754 lines = g_strsplit((char *)vmci, "\n", -1);
1755 for (i = 0; lines[i]; i++) {
Wei Huang68cbecf2018-03-09 12:03:23 -05001756 const char *prefix = NULL;
1757
1758 if (s->dump_info.d_machine == EM_X86_64) {
1759 prefix = "NUMBER(phys_base)=";
1760 } else if (s->dump_info.d_machine == EM_AARCH64) {
1761 prefix = "NUMBER(PHYS_OFFSET)=";
1762 }
1763
1764 if (prefix && g_str_has_prefix(lines[i], prefix)) {
1765 if (qemu_strtou64(lines[i] + strlen(prefix), NULL, 16,
Marc-André Lureaud9feb512017-09-11 18:59:26 +02001766 &phys_base) < 0) {
Wei Huang68cbecf2018-03-09 12:03:23 -05001767 warn_report("Failed to read %s", prefix);
Marc-André Lureaud9feb512017-09-11 18:59:26 +02001768 } else {
1769 s->dump_info.phys_base = phys_base;
1770 }
1771 break;
1772 }
1773 }
1774
1775 g_strfreev(lines);
1776}
1777
zhanghailiang4c7e2512014-10-09 14:13:11 +08001778static void dump_init(DumpState *s, int fd, bool has_format,
1779 DumpGuestMemoryFormat format, bool paging, bool has_filter,
1780 int64_t begin, int64_t length, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +08001781{
Janosch Frank86a518b2022-03-30 12:35:55 +00001782 ERRP_GUARD();
Marc-André Lureau903ef732017-09-11 18:59:25 +02001783 VMCoreInfoState *vmci = vmcoreinfo_find();
Andreas Färber182735e2013-05-29 22:29:20 +02001784 CPUState *cpu;
Wen Congyang783e9b42012-05-07 12:10:47 +08001785 int nr_cpus;
1786 int ret;
1787
Peter Xuca1fc8c2016-02-18 13:16:50 +08001788 s->has_format = has_format;
1789 s->format = format;
Peter Xu2264c2c2016-02-18 13:16:53 +08001790 s->written_size = 0;
Peter Xuca1fc8c2016-02-18 13:16:50 +08001791
qiaonuohanb53ccc32014-02-18 14:11:36 +08001792 /* kdump-compressed is conflict with paging and filter */
1793 if (has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
1794 assert(!paging && !has_filter);
1795 }
1796
Wen Congyang783e9b42012-05-07 12:10:47 +08001797 if (runstate_is_running()) {
1798 vm_stop(RUN_STATE_SAVE_VM);
1799 s->resume = true;
1800 } else {
1801 s->resume = false;
1802 }
1803
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001804 /* If we use KVM, we should synchronize the registers before we get dump
1805 * info or physmap info.
Wen Congyang783e9b42012-05-07 12:10:47 +08001806 */
Andreas Färber1b3509c2013-06-09 16:48:29 +02001807 cpu_synchronize_all_states();
Wen Congyang783e9b42012-05-07 12:10:47 +08001808 nr_cpus = 0;
Andreas Färberbdc44642013-06-24 23:50:24 +02001809 CPU_FOREACH(cpu) {
Wen Congyang783e9b42012-05-07 12:10:47 +08001810 nr_cpus++;
1811 }
1812
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001813 s->fd = fd;
Janosch Frankdddf7252022-08-11 12:10:58 +00001814 if (has_filter && !length) {
1815 error_setg(errp, QERR_INVALID_PARAMETER, "length");
1816 goto cleanup;
1817 }
1818 s->filter_area_begin = begin;
1819 s->filter_area_length = length;
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001820
Janosch Frank9b722242022-10-17 11:32:10 +00001821 /* First index is 0, it's the special null name */
1822 s->string_table_buf = g_array_new(FALSE, TRUE, 1);
1823 /*
1824 * Allocate the null name, due to the clearing option set to true
1825 * it will be 0.
1826 */
1827 g_array_set_size(s->string_table_buf, 1);
1828
Chen Gang29282072014-08-03 23:28:56 +08001829 memory_mapping_list_init(&s->list);
1830
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001831 guest_phys_blocks_init(&s->guest_phys_blocks);
Laszlo Ersekc5d7f602013-08-06 12:37:10 +02001832 guest_phys_blocks_append(&s->guest_phys_blocks);
Peter Xu2264c2c2016-02-18 13:16:53 +08001833 s->total_size = dump_calculate_size(s);
1834#ifdef DEBUG_DUMP_GUEST_MEMORY
1835 fprintf(stderr, "DUMP: total memory to dump: %lu\n", s->total_size);
1836#endif
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001837
Cornelia Huckd1e69942017-09-13 16:20:35 +02001838 /* it does not make sense to dump non-existent memory */
1839 if (!s->total_size) {
1840 error_setg(errp, "dump: no guest memory to dump");
1841 goto cleanup;
1842 }
1843
Janosch Frank0c2994a2022-08-11 12:10:57 +00001844 /* Is the filter filtering everything? */
1845 if (validate_start_block(s) == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001846 error_setg(errp, QERR_INVALID_PARAMETER, "begin");
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001847 goto cleanup;
1848 }
1849
1850 /* get dump info: endian, class and architecture.
1851 * If the target architecture is not supported, cpu_get_dump_info() will
1852 * return -1.
1853 */
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001854 ret = cpu_get_dump_info(&s->dump_info, &s->guest_phys_blocks);
Wen Congyang783e9b42012-05-07 12:10:47 +08001855 if (ret < 0) {
Markus Armbrusterf969c622023-02-07 08:51:05 +01001856 error_setg(errp,
1857 "dumping guest memory is not supported on this target");
Wen Congyang783e9b42012-05-07 12:10:47 +08001858 goto cleanup;
1859 }
1860
Andrew Jones8161bef2016-01-11 20:56:20 +01001861 if (!s->dump_info.page_size) {
Philippe Mathieu-Daudéc5d40b22023-02-23 23:38:59 +01001862 s->dump_info.page_size = qemu_target_page_size();
Andrew Jones8161bef2016-01-11 20:56:20 +01001863 }
1864
Paolo Bonzini4720bd02012-06-07 08:48:09 +02001865 s->note_size = cpu_get_note_size(s->dump_info.d_class,
1866 s->dump_info.d_machine, nr_cpus);
Markus Armbrusterf1a46972023-02-07 08:51:06 +01001867 assert(s->note_size >= 0);
Paolo Bonzini4720bd02012-06-07 08:48:09 +02001868
Marc-André Lureau903ef732017-09-11 18:59:25 +02001869 /*
Marc-André Lureaud9feb512017-09-11 18:59:26 +02001870 * The goal of this block is to (a) update the previously guessed
1871 * phys_base, (b) copy the guest note out of the guest.
1872 * Failure to do so is not fatal for dumping.
Marc-André Lureau903ef732017-09-11 18:59:25 +02001873 */
1874 if (vmci) {
1875 uint64_t addr, note_head_size, name_size, desc_size;
1876 uint32_t size;
1877 uint16_t format;
1878
Janosch Frank05bbaa502022-03-30 12:36:00 +00001879 note_head_size = dump_is_64bit(s) ?
1880 sizeof(Elf64_Nhdr) : sizeof(Elf32_Nhdr);
Marc-André Lureau903ef732017-09-11 18:59:25 +02001881
1882 format = le16_to_cpu(vmci->vmcoreinfo.guest_format);
1883 size = le32_to_cpu(vmci->vmcoreinfo.size);
1884 addr = le64_to_cpu(vmci->vmcoreinfo.paddr);
1885 if (!vmci->has_vmcoreinfo) {
1886 warn_report("guest note is not present");
1887 } else if (size < note_head_size || size > MAX_GUEST_NOTE_SIZE) {
1888 warn_report("guest note size is invalid: %" PRIu32, size);
Marc-André Lureau5be5df72018-08-17 17:59:10 +02001889 } else if (format != FW_CFG_VMCOREINFO_FORMAT_ELF) {
Marc-André Lureau903ef732017-09-11 18:59:25 +02001890 warn_report("guest note format is unsupported: %" PRIu16, format);
1891 } else {
1892 s->guest_note = g_malloc(size + 1); /* +1 for adding \0 */
1893 cpu_physical_memory_read(addr, s->guest_note, size);
1894
1895 get_note_sizes(s, s->guest_note, NULL, &name_size, &desc_size);
1896 s->guest_note_size = ELF_NOTE_SIZE(note_head_size, name_size,
1897 desc_size);
1898 if (name_size > MAX_GUEST_NOTE_SIZE ||
1899 desc_size > MAX_GUEST_NOTE_SIZE ||
1900 s->guest_note_size > size) {
1901 warn_report("Invalid guest note header");
1902 g_free(s->guest_note);
1903 s->guest_note = NULL;
1904 } else {
Marc-André Lureaud9feb512017-09-11 18:59:26 +02001905 vmcoreinfo_update_phys_base(s);
Marc-André Lureau903ef732017-09-11 18:59:25 +02001906 s->note_size += s->guest_note_size;
1907 }
1908 }
1909 }
1910
Wen Congyang783e9b42012-05-07 12:10:47 +08001911 /* get memory mapping */
Wen Congyang783e9b42012-05-07 12:10:47 +08001912 if (paging) {
Janosch Frank86a518b2022-03-30 12:35:55 +00001913 qemu_get_guest_memory_mapping(&s->list, &s->guest_phys_blocks, errp);
1914 if (*errp) {
Andreas Färber11ed09c2013-05-29 21:54:03 +02001915 goto cleanup;
1916 }
Wen Congyang783e9b42012-05-07 12:10:47 +08001917 } else {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001918 qemu_get_guest_simple_memory_mapping(&s->list, &s->guest_phys_blocks);
Wen Congyang783e9b42012-05-07 12:10:47 +08001919 }
1920
qiaonuohan7aad2482014-02-18 14:11:31 +08001921 s->nr_cpus = nr_cpus;
qiaonuohan7aad2482014-02-18 14:11:31 +08001922
1923 get_max_mapnr(s);
1924
1925 uint64_t tmp;
Andrew Jones8161bef2016-01-11 20:56:20 +01001926 tmp = DIV_ROUND_UP(DIV_ROUND_UP(s->max_mapnr, CHAR_BIT),
1927 s->dump_info.page_size);
1928 s->len_dump_bitmap = tmp * s->dump_info.page_size;
qiaonuohan7aad2482014-02-18 14:11:31 +08001929
qiaonuohanb53ccc32014-02-18 14:11:36 +08001930 /* init for kdump-compressed format */
1931 if (has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
1932 switch (format) {
1933 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB:
1934 s->flag_compress = DUMP_DH_COMPRESSED_ZLIB;
1935 break;
1936
1937 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO:
Laszlo Ersekc998acb2014-05-20 13:42:22 +02001938#ifdef CONFIG_LZO
1939 if (lzo_init() != LZO_E_OK) {
1940 error_setg(errp, "failed to initialize the LZO library");
1941 goto cleanup;
1942 }
1943#endif
qiaonuohanb53ccc32014-02-18 14:11:36 +08001944 s->flag_compress = DUMP_DH_COMPRESSED_LZO;
1945 break;
1946
1947 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY:
1948 s->flag_compress = DUMP_DH_COMPRESSED_SNAPPY;
1949 break;
1950
1951 default:
1952 s->flag_compress = 0;
1953 }
1954
zhanghailiang4c7e2512014-10-09 14:13:11 +08001955 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001956 }
1957
Janosch Frankdddf7252022-08-11 12:10:58 +00001958 if (dump_has_filter(s)) {
1959 memory_mapping_filter(&s->list, s->filter_area_begin, s->filter_area_length);
Wen Congyang783e9b42012-05-07 12:10:47 +08001960 }
1961
1962 /*
Janosch Frank9b722242022-10-17 11:32:10 +00001963 * The first section header is always a special one in which most
1964 * fields are 0. The section header string table is also always
1965 * set.
Wen Congyang783e9b42012-05-07 12:10:47 +08001966 */
Janosch Frank9b722242022-10-17 11:32:10 +00001967 s->shdr_num = 2;
Wen Congyang783e9b42012-05-07 12:10:47 +08001968
Janosch Frank9b722242022-10-17 11:32:10 +00001969 /*
1970 * Adds the number of architecture sections to shdr_num and sets
1971 * elf_section_data_size so we know the offsets and sizes of all
1972 * parts.
1973 */
1974 if (s->dump_info.arch_sections_add_fn) {
1975 s->dump_info.arch_sections_add_fn(s);
Wen Congyang783e9b42012-05-07 12:10:47 +08001976 }
1977
Janosch Frank9b722242022-10-17 11:32:10 +00001978 /*
1979 * calculate shdr_num so we know the offsets and sizes of all
1980 * parts.
1981 * Calculate phdr_num
1982 *
1983 * The absolute maximum amount of phdrs is UINT32_MAX - 1 as
1984 * sh_info is 32 bit. There's special handling once we go over
1985 * UINT16_MAX - 1 but that is handled in the ehdr and section
1986 * code.
1987 */
1988 s->phdr_num = 1; /* Reserve PT_NOTE */
1989 if (s->list.num <= UINT32_MAX - 1) {
1990 s->phdr_num += s->list.num;
1991 } else {
1992 s->phdr_num = UINT32_MAX;
1993 }
1994
1995 /*
1996 * Now that the number of section and program headers is known we
1997 * can calculate the offsets of the headers and data.
1998 */
Janosch Frank05bbaa502022-03-30 12:36:00 +00001999 if (dump_is_64bit(s)) {
Janosch Frankcb415fd2022-10-17 08:38:14 +00002000 s->shdr_offset = sizeof(Elf64_Ehdr);
2001 s->phdr_offset = s->shdr_offset + sizeof(Elf64_Shdr) * s->shdr_num;
2002 s->note_offset = s->phdr_offset + sizeof(Elf64_Phdr) * s->phdr_num;
Wen Congyang783e9b42012-05-07 12:10:47 +08002003 } else {
Janosch Frankcb415fd2022-10-17 08:38:14 +00002004 s->shdr_offset = sizeof(Elf32_Ehdr);
2005 s->phdr_offset = s->shdr_offset + sizeof(Elf32_Shdr) * s->shdr_num;
2006 s->note_offset = s->phdr_offset + sizeof(Elf32_Phdr) * s->phdr_num;
Wen Congyang783e9b42012-05-07 12:10:47 +08002007 }
Janosch Frank13fd4172022-10-17 08:38:16 +00002008 s->memory_offset = s->note_offset + s->note_size;
2009 s->section_offset = s->memory_offset + s->total_size;
Wen Congyang783e9b42012-05-07 12:10:47 +08002010
zhanghailiang4c7e2512014-10-09 14:13:11 +08002011 return;
Wen Congyang783e9b42012-05-07 12:10:47 +08002012
2013cleanup:
Chen Gang29282072014-08-03 23:28:56 +08002014 dump_cleanup(s);
Wen Congyang783e9b42012-05-07 12:10:47 +08002015}
2016
Peter Xuca1fc8c2016-02-18 13:16:50 +08002017/* this operation might be time consuming. */
2018static void dump_process(DumpState *s, Error **errp)
2019{
Janosch Frank86a518b2022-03-30 12:35:55 +00002020 ERRP_GUARD();
Peter Xud42a0d12016-02-18 13:16:56 +08002021 DumpQueryResult *result = NULL;
Peter Xuca1fc8c2016-02-18 13:16:50 +08002022
Viktor Prutyanov2da91b52018-05-17 19:23:39 +03002023 if (s->has_format && s->format == DUMP_GUEST_MEMORY_FORMAT_WIN_DMP) {
2024#ifdef TARGET_X86_64
Janosch Frank86a518b2022-03-30 12:35:55 +00002025 create_win_dump(s, errp);
Viktor Prutyanov2da91b52018-05-17 19:23:39 +03002026#endif
2027 } else if (s->has_format && s->format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
Janosch Frank86a518b2022-03-30 12:35:55 +00002028 create_kdump_vmcore(s, errp);
Peter Xuca1fc8c2016-02-18 13:16:50 +08002029 } else {
Janosch Frank86a518b2022-03-30 12:35:55 +00002030 create_vmcore(s, errp);
Peter Xuca1fc8c2016-02-18 13:16:50 +08002031 }
2032
Peter Xu39ba2ea2016-02-18 13:16:54 +08002033 /* make sure status is written after written_size updates */
2034 smp_wmb();
Stefan Hajnoczid73415a2020-09-23 11:56:46 +01002035 qatomic_set(&s->status,
Janosch Frank86a518b2022-03-30 12:35:55 +00002036 (*errp ? DUMP_STATUS_FAILED : DUMP_STATUS_COMPLETED));
Peter Xuca1fc8c2016-02-18 13:16:50 +08002037
Peter Xud42a0d12016-02-18 13:16:56 +08002038 /* send DUMP_COMPLETED message (unconditionally) */
2039 result = qmp_query_dump(NULL);
2040 /* should never fail */
2041 assert(result);
Markus Armbrusterd4f8bdc2022-11-04 17:06:55 +01002042 qapi_event_send_dump_completed(result,
2043 *errp ? error_get_pretty(*errp) : NULL);
Peter Xud42a0d12016-02-18 13:16:56 +08002044 qapi_free_DumpQueryResult(result);
2045
Peter Xuca1fc8c2016-02-18 13:16:50 +08002046 dump_cleanup(s);
2047}
2048
Peter Xu1fbeff72016-02-18 13:16:52 +08002049static void *dump_thread(void *data)
2050{
Peter Xu1fbeff72016-02-18 13:16:52 +08002051 DumpState *s = (DumpState *)data;
Alberto Garciac3a8fe32017-08-29 15:08:36 +03002052 dump_process(s, NULL);
Peter Xu1fbeff72016-02-18 13:16:52 +08002053 return NULL;
2054}
2055
Peter Xu39ba2ea2016-02-18 13:16:54 +08002056DumpQueryResult *qmp_query_dump(Error **errp)
2057{
2058 DumpQueryResult *result = g_new(DumpQueryResult, 1);
2059 DumpState *state = &dump_state_global;
Stefan Hajnoczid73415a2020-09-23 11:56:46 +01002060 result->status = qatomic_read(&state->status);
Peter Xu39ba2ea2016-02-18 13:16:54 +08002061 /* make sure we are reading status and written_size in order */
2062 smp_rmb();
2063 result->completed = state->written_size;
2064 result->total = state->total_size;
2065 return result;
2066}
2067
Peter Xu228de9c2016-02-18 13:16:47 +08002068void qmp_dump_guest_memory(bool paging, const char *file,
2069 bool has_detach, bool detach,
2070 bool has_begin, int64_t begin, bool has_length,
qiaonuohanb53ccc32014-02-18 14:11:36 +08002071 int64_t length, bool has_format,
2072 DumpGuestMemoryFormat format, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +08002073{
Janosch Frank86a518b2022-03-30 12:35:55 +00002074 ERRP_GUARD();
Wen Congyang783e9b42012-05-07 12:10:47 +08002075 const char *p;
2076 int fd = -1;
2077 DumpState *s;
Peter Xu1fbeff72016-02-18 13:16:52 +08002078 bool detach_p = false;
Wen Congyang783e9b42012-05-07 12:10:47 +08002079
Peter Xu63e27f22016-02-18 13:16:51 +08002080 if (runstate_check(RUN_STATE_INMIGRATE)) {
2081 error_setg(errp, "Dump not allowed during incoming migration.");
2082 return;
2083 }
2084
Peter Xu65d64f32016-02-18 13:16:49 +08002085 /* if there is a dump in background, we should wait until the dump
2086 * finished */
Marc-André Lureau544803c2022-03-23 19:57:31 +04002087 if (qemu_system_dump_in_progress()) {
Peter Xu65d64f32016-02-18 13:16:49 +08002088 error_setg(errp, "There is a dump in process, please wait.");
2089 return;
2090 }
2091
qiaonuohanb53ccc32014-02-18 14:11:36 +08002092 /*
2093 * kdump-compressed format need the whole memory dumped, so paging or
2094 * filter is not supported here.
2095 */
2096 if ((has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) &&
2097 (paging || has_begin || has_length)) {
2098 error_setg(errp, "kdump-compressed format doesn't support paging or "
2099 "filter");
2100 return;
2101 }
Wen Congyang783e9b42012-05-07 12:10:47 +08002102 if (has_begin && !has_length) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002103 error_setg(errp, QERR_MISSING_PARAMETER, "length");
Wen Congyang783e9b42012-05-07 12:10:47 +08002104 return;
2105 }
2106 if (!has_begin && has_length) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002107 error_setg(errp, QERR_MISSING_PARAMETER, "begin");
Wen Congyang783e9b42012-05-07 12:10:47 +08002108 return;
2109 }
Peter Xu1fbeff72016-02-18 13:16:52 +08002110 if (has_detach) {
2111 detach_p = detach;
2112 }
Wen Congyang783e9b42012-05-07 12:10:47 +08002113
qiaonuohanb53ccc32014-02-18 14:11:36 +08002114 /* check whether lzo/snappy is supported */
2115#ifndef CONFIG_LZO
2116 if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO) {
2117 error_setg(errp, "kdump-lzo is not available now");
2118 return;
2119 }
2120#endif
2121
2122#ifndef CONFIG_SNAPPY
2123 if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY) {
2124 error_setg(errp, "kdump-snappy is not available now");
2125 return;
2126 }
2127#endif
2128
Viktor Prutyanov2da91b52018-05-17 19:23:39 +03002129#ifndef TARGET_X86_64
2130 if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_WIN_DMP) {
2131 error_setg(errp, "Windows dump is only available for x86-64");
2132 return;
2133 }
2134#endif
2135
Wen Congyang783e9b42012-05-07 12:10:47 +08002136#if !defined(WIN32)
2137 if (strstart(file, "fd:", &p)) {
Kevin Wolf947e4742020-10-05 17:58:44 +02002138 fd = monitor_get_fd(monitor_cur(), p, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +08002139 if (fd == -1) {
Wen Congyang783e9b42012-05-07 12:10:47 +08002140 return;
2141 }
2142 }
2143#endif
2144
2145 if (strstart(file, "file:", &p)) {
Daniel P. Berrangé448058a2020-07-21 13:25:21 +01002146 fd = qemu_open_old(p, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, S_IRUSR);
Wen Congyang783e9b42012-05-07 12:10:47 +08002147 if (fd < 0) {
Luiz Capitulino75817662013-06-07 14:36:01 -04002148 error_setg_file_open(errp, errno, p);
Wen Congyang783e9b42012-05-07 12:10:47 +08002149 return;
2150 }
2151 }
2152
2153 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002154 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
Wen Congyang783e9b42012-05-07 12:10:47 +08002155 return;
2156 }
2157
Peter Xub7bc6b12021-09-22 12:20:09 -04002158 if (!dump_migration_blocker) {
2159 error_setg(&dump_migration_blocker,
2160 "Live migration disabled: dump-guest-memory in progress");
2161 }
2162
2163 /*
2164 * Allows even for -only-migratable, but forbid migration during the
2165 * process of dump guest memory.
2166 */
2167 if (migrate_add_blocker_internal(dump_migration_blocker, errp)) {
2168 /* Remember to release the fd before passing it over to dump state */
2169 close(fd);
2170 return;
2171 }
2172
Peter Xubaf28f52016-02-18 13:16:48 +08002173 s = &dump_state_global;
2174 dump_state_prepare(s);
Wen Congyang783e9b42012-05-07 12:10:47 +08002175
zhanghailiang4c7e2512014-10-09 14:13:11 +08002176 dump_init(s, fd, has_format, format, paging, has_begin,
Janosch Frank86a518b2022-03-30 12:35:55 +00002177 begin, length, errp);
2178 if (*errp) {
Stefan Hajnoczid73415a2020-09-23 11:56:46 +01002179 qatomic_set(&s->status, DUMP_STATUS_FAILED);
Wen Congyang783e9b42012-05-07 12:10:47 +08002180 return;
2181 }
2182
Peter Xu1fbeff72016-02-18 13:16:52 +08002183 if (detach_p) {
2184 /* detached dump */
Fam Zheng6796b402017-05-03 15:28:19 +08002185 s->detached = true;
Peter Xu1fbeff72016-02-18 13:16:52 +08002186 qemu_thread_create(&s->dump_thread, "dump_thread", dump_thread,
2187 s, QEMU_THREAD_DETACHED);
2188 } else {
2189 /* sync dump */
2190 dump_process(s, errp);
2191 }
Wen Congyang783e9b42012-05-07 12:10:47 +08002192}
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002193
2194DumpGuestMemoryCapability *qmp_query_dump_guest_memory_capability(Error **errp)
2195{
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002196 DumpGuestMemoryCapability *cap =
Markus Armbrusterb21e2382022-03-15 15:41:56 +01002197 g_new0(DumpGuestMemoryCapability, 1);
Eric Blake95b3a8c2021-01-13 16:10:13 -06002198 DumpGuestMemoryFormatList **tail = &cap->formats;
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002199
2200 /* elf is always available */
Eric Blake95b3a8c2021-01-13 16:10:13 -06002201 QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_ELF);
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002202
2203 /* kdump-zlib is always available */
Eric Blake95b3a8c2021-01-13 16:10:13 -06002204 QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB);
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002205
2206 /* add new item if kdump-lzo is available */
2207#ifdef CONFIG_LZO
Eric Blake95b3a8c2021-01-13 16:10:13 -06002208 QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO);
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002209#endif
2210
2211 /* add new item if kdump-snappy is available */
2212#ifdef CONFIG_SNAPPY
Eric Blake95b3a8c2021-01-13 16:10:13 -06002213 QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY);
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002214#endif
2215
Viktor Prutyanov2da91b52018-05-17 19:23:39 +03002216 /* Windows dump is available only if target is x86_64 */
2217#ifdef TARGET_X86_64
Eric Blake95b3a8c2021-01-13 16:10:13 -06002218 QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_WIN_DMP);
Viktor Prutyanov2da91b52018-05-17 19:23:39 +03002219#endif
2220
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002221 return cap;
2222}