blob: 24c829e705d98ed5be29e8d741bf0305c48b455e [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"
Richard Hendersoncc37d982023-03-15 17:43:13 +000027#include "qemu/error-report.h"
Markus Armbrusterdb725812019-08-12 07:23:50 +020028#include "qemu/main-loop.h"
Marc-André Lureau903ef732017-09-11 18:59:25 +020029#include "hw/misc/vmcoreinfo.h"
Peter Xub7bc6b12021-09-22 12:20:09 -040030#include "migration/blocker.h"
Philippe Mathieu-Daudéac978772023-02-23 23:56:46 +010031#include "hw/core/cpu.h"
Viktor Prutyanov2da91b52018-05-17 19:23:39 +030032#include "win_dump.h"
Viktor Prutyanov2da91b52018-05-17 19:23:39 +030033
qiaonuohand12f57e2014-02-18 14:11:35 +080034#include <zlib.h>
35#ifdef CONFIG_LZO
36#include <lzo/lzo1x.h>
37#endif
38#ifdef CONFIG_SNAPPY
39#include <snappy-c.h>
40#endif
qiaonuohan4ab23a92014-02-18 14:11:37 +080041#ifndef ELF_MACHINE_UNAME
42#define ELF_MACHINE_UNAME "Unknown"
43#endif
qiaonuohand12f57e2014-02-18 14:11:35 +080044
Marc-André Lureau903ef732017-09-11 18:59:25 +020045#define MAX_GUEST_NOTE_SIZE (1 << 20) /* 1MB should be enough */
46
Peter Xub7bc6b12021-09-22 12:20:09 -040047static Error *dump_migration_blocker;
48
Marc-André Lureau903ef732017-09-11 18:59:25 +020049#define ELF_NOTE_SIZE(hdr_size, name_size, desc_size) \
50 ((DIV_ROUND_UP((hdr_size), 4) + \
51 DIV_ROUND_UP((name_size), 4) + \
52 DIV_ROUND_UP((desc_size), 4)) * 4)
53
Janosch Frank05bbaa502022-03-30 12:36:00 +000054static inline bool dump_is_64bit(DumpState *s)
55{
56 return s->dump_info.d_class == ELFCLASS64;
57}
58
Janosch Frankdddf7252022-08-11 12:10:58 +000059static inline bool dump_has_filter(DumpState *s)
60{
61 return s->filter_area_length > 0;
62}
63
Bharata B Raoacb0ef52014-05-19 19:57:44 +020064uint16_t cpu_to_dump16(DumpState *s, uint16_t val)
Wen Congyang783e9b42012-05-07 12:10:47 +080065{
Bharata B Raoacb0ef52014-05-19 19:57:44 +020066 if (s->dump_info.d_endian == ELFDATA2LSB) {
Wen Congyang783e9b42012-05-07 12:10:47 +080067 val = cpu_to_le16(val);
68 } else {
69 val = cpu_to_be16(val);
70 }
71
72 return val;
73}
74
Bharata B Raoacb0ef52014-05-19 19:57:44 +020075uint32_t cpu_to_dump32(DumpState *s, uint32_t val)
Wen Congyang783e9b42012-05-07 12:10:47 +080076{
Bharata B Raoacb0ef52014-05-19 19:57:44 +020077 if (s->dump_info.d_endian == ELFDATA2LSB) {
Wen Congyang783e9b42012-05-07 12:10:47 +080078 val = cpu_to_le32(val);
79 } else {
80 val = cpu_to_be32(val);
81 }
82
83 return val;
84}
85
Bharata B Raoacb0ef52014-05-19 19:57:44 +020086uint64_t cpu_to_dump64(DumpState *s, uint64_t val)
Wen Congyang783e9b42012-05-07 12:10:47 +080087{
Bharata B Raoacb0ef52014-05-19 19:57:44 +020088 if (s->dump_info.d_endian == ELFDATA2LSB) {
Wen Congyang783e9b42012-05-07 12:10:47 +080089 val = cpu_to_le64(val);
90 } else {
91 val = cpu_to_be64(val);
92 }
93
94 return val;
95}
96
Wen Congyang783e9b42012-05-07 12:10:47 +080097static int dump_cleanup(DumpState *s)
98{
Laszlo Ersek5ee163e2013-08-06 12:37:09 +020099 guest_phys_blocks_free(&s->guest_phys_blocks);
Wen Congyang783e9b42012-05-07 12:10:47 +0800100 memory_mapping_list_free(&s->list);
Chen Gang29282072014-08-03 23:28:56 +0800101 close(s->fd);
Marc-André Lureau903ef732017-09-11 18:59:25 +0200102 g_free(s->guest_note);
Markus Armbruster96afbc52023-10-31 11:45:28 +0100103 g_clear_pointer(&s->string_table_buf, g_array_unref);
Marc-André Lureau903ef732017-09-11 18:59:25 +0200104 s->guest_note = NULL;
Wen Congyang783e9b42012-05-07 12:10:47 +0800105 if (s->resume) {
Fam Zheng6796b402017-05-03 15:28:19 +0800106 if (s->detached) {
107 qemu_mutex_lock_iothread();
108 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800109 vm_start();
Fam Zheng6796b402017-05-03 15:28:19 +0800110 if (s->detached) {
111 qemu_mutex_unlock_iothread();
112 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800113 }
Steve Sistarec8a7fc52023-10-18 06:03:36 -0700114 migrate_del_blocker(&dump_migration_blocker);
Wen Congyang783e9b42012-05-07 12:10:47 +0800115
Chen Gang29282072014-08-03 23:28:56 +0800116 return 0;
Wen Congyang783e9b42012-05-07 12:10:47 +0800117}
118
qiaonuohanb5ba1cc2014-02-18 14:11:25 +0800119static int fd_write_vmcore(const void *buf, size_t size, void *opaque)
Wen Congyang783e9b42012-05-07 12:10:47 +0800120{
121 DumpState *s = opaque;
Luiz Capitulino2f616522012-09-21 13:17:55 -0300122 size_t written_size;
Wen Congyang783e9b42012-05-07 12:10:47 +0800123
Luiz Capitulino2f616522012-09-21 13:17:55 -0300124 written_size = qemu_write_full(s->fd, buf, size);
125 if (written_size != size) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200126 return -errno;
Wen Congyang783e9b42012-05-07 12:10:47 +0800127 }
128
129 return 0;
130}
131
Janosch Frank670e7692022-08-11 12:11:00 +0000132static void prepare_elf64_header(DumpState *s, Elf64_Ehdr *elf_header)
Wen Congyang783e9b42012-05-07 12:10:47 +0800133{
Janosch Frank046bc412022-04-07 09:48:24 +0000134 /*
135 * phnum in the elf header is 16 bit, if we have more segments we
136 * set phnum to PN_XNUM and write the real number of segments to a
137 * special section.
138 */
139 uint16_t phnum = MIN(s->phdr_num, PN_XNUM);
Wen Congyang783e9b42012-05-07 12:10:47 +0800140
Janosch Frank670e7692022-08-11 12:11:00 +0000141 memset(elf_header, 0, sizeof(Elf64_Ehdr));
142 memcpy(elf_header, ELFMAG, SELFMAG);
143 elf_header->e_ident[EI_CLASS] = ELFCLASS64;
144 elf_header->e_ident[EI_DATA] = s->dump_info.d_endian;
145 elf_header->e_ident[EI_VERSION] = EV_CURRENT;
146 elf_header->e_type = cpu_to_dump16(s, ET_CORE);
147 elf_header->e_machine = cpu_to_dump16(s, s->dump_info.d_machine);
148 elf_header->e_version = cpu_to_dump32(s, EV_CURRENT);
149 elf_header->e_ehsize = cpu_to_dump16(s, sizeof(elf_header));
150 elf_header->e_phoff = cpu_to_dump64(s, s->phdr_offset);
151 elf_header->e_phentsize = cpu_to_dump16(s, sizeof(Elf64_Phdr));
152 elf_header->e_phnum = cpu_to_dump16(s, phnum);
Janosch Frank9b722242022-10-17 11:32:10 +0000153 elf_header->e_shoff = cpu_to_dump64(s, s->shdr_offset);
154 elf_header->e_shentsize = cpu_to_dump16(s, sizeof(Elf64_Shdr));
155 elf_header->e_shnum = cpu_to_dump16(s, s->shdr_num);
156 elf_header->e_shstrndx = cpu_to_dump16(s, s->shdr_num - 1);
Wen Congyang783e9b42012-05-07 12:10:47 +0800157}
158
Janosch Frank670e7692022-08-11 12:11:00 +0000159static void prepare_elf32_header(DumpState *s, Elf32_Ehdr *elf_header)
Wen Congyang783e9b42012-05-07 12:10:47 +0800160{
Janosch Frank046bc412022-04-07 09:48:24 +0000161 /*
162 * phnum in the elf header is 16 bit, if we have more segments we
163 * set phnum to PN_XNUM and write the real number of segments to a
164 * special section.
165 */
166 uint16_t phnum = MIN(s->phdr_num, PN_XNUM);
Janosch Frank670e7692022-08-11 12:11:00 +0000167
168 memset(elf_header, 0, sizeof(Elf32_Ehdr));
169 memcpy(elf_header, ELFMAG, SELFMAG);
170 elf_header->e_ident[EI_CLASS] = ELFCLASS32;
171 elf_header->e_ident[EI_DATA] = s->dump_info.d_endian;
172 elf_header->e_ident[EI_VERSION] = EV_CURRENT;
173 elf_header->e_type = cpu_to_dump16(s, ET_CORE);
174 elf_header->e_machine = cpu_to_dump16(s, s->dump_info.d_machine);
175 elf_header->e_version = cpu_to_dump32(s, EV_CURRENT);
176 elf_header->e_ehsize = cpu_to_dump16(s, sizeof(elf_header));
177 elf_header->e_phoff = cpu_to_dump32(s, s->phdr_offset);
178 elf_header->e_phentsize = cpu_to_dump16(s, sizeof(Elf32_Phdr));
179 elf_header->e_phnum = cpu_to_dump16(s, phnum);
Janosch Frank9b722242022-10-17 11:32:10 +0000180 elf_header->e_shoff = cpu_to_dump32(s, s->shdr_offset);
181 elf_header->e_shentsize = cpu_to_dump16(s, sizeof(Elf32_Shdr));
182 elf_header->e_shnum = cpu_to_dump16(s, s->shdr_num);
183 elf_header->e_shstrndx = cpu_to_dump16(s, s->shdr_num - 1);
Janosch Frank670e7692022-08-11 12:11:00 +0000184}
185
186static void write_elf_header(DumpState *s, Error **errp)
187{
188 Elf32_Ehdr elf32_header;
189 Elf64_Ehdr elf64_header;
190 size_t header_size;
191 void *header_ptr;
Wen Congyang783e9b42012-05-07 12:10:47 +0800192 int ret;
Wen Congyang783e9b42012-05-07 12:10:47 +0800193
Janosch Frank9b722242022-10-17 11:32:10 +0000194 /* The NULL header and the shstrtab are always defined */
195 assert(s->shdr_num >= 2);
Janosch Frank670e7692022-08-11 12:11:00 +0000196 if (dump_is_64bit(s)) {
197 prepare_elf64_header(s, &elf64_header);
198 header_size = sizeof(elf64_header);
199 header_ptr = &elf64_header;
200 } else {
201 prepare_elf32_header(s, &elf32_header);
202 header_size = sizeof(elf32_header);
203 header_ptr = &elf32_header;
Wen Congyang783e9b42012-05-07 12:10:47 +0800204 }
205
Janosch Frank670e7692022-08-11 12:11:00 +0000206 ret = fd_write_vmcore(header_ptr, header_size, s);
Wen Congyang783e9b42012-05-07 12:10:47 +0800207 if (ret < 0) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200208 error_setg_errno(errp, -ret, "dump: failed to write elf header");
Wen Congyang783e9b42012-05-07 12:10:47 +0800209 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800210}
211
zhanghailiang4c7e2512014-10-09 14:13:11 +0800212static void write_elf64_load(DumpState *s, MemoryMapping *memory_mapping,
213 int phdr_index, hwaddr offset,
214 hwaddr filesz, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800215{
216 Elf64_Phdr phdr;
217 int ret;
Wen Congyang783e9b42012-05-07 12:10:47 +0800218
219 memset(&phdr, 0, sizeof(Elf64_Phdr));
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200220 phdr.p_type = cpu_to_dump32(s, PT_LOAD);
221 phdr.p_offset = cpu_to_dump64(s, offset);
222 phdr.p_paddr = cpu_to_dump64(s, memory_mapping->phys_addr);
223 phdr.p_filesz = cpu_to_dump64(s, filesz);
224 phdr.p_memsz = cpu_to_dump64(s, memory_mapping->length);
Jon Dorone17bebd2019-01-09 10:22:03 +0200225 phdr.p_vaddr = cpu_to_dump64(s, memory_mapping->virt_addr) ?: phdr.p_paddr;
Wen Congyang783e9b42012-05-07 12:10:47 +0800226
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200227 assert(memory_mapping->length >= filesz);
228
Wen Congyang783e9b42012-05-07 12:10:47 +0800229 ret = fd_write_vmcore(&phdr, sizeof(Elf64_Phdr), s);
230 if (ret < 0) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200231 error_setg_errno(errp, -ret,
232 "dump: failed to write program header table");
Wen Congyang783e9b42012-05-07 12:10:47 +0800233 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800234}
235
zhanghailiang4c7e2512014-10-09 14:13:11 +0800236static void write_elf32_load(DumpState *s, MemoryMapping *memory_mapping,
237 int phdr_index, hwaddr offset,
238 hwaddr filesz, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800239{
240 Elf32_Phdr phdr;
241 int ret;
Wen Congyang783e9b42012-05-07 12:10:47 +0800242
243 memset(&phdr, 0, sizeof(Elf32_Phdr));
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200244 phdr.p_type = cpu_to_dump32(s, PT_LOAD);
245 phdr.p_offset = cpu_to_dump32(s, offset);
246 phdr.p_paddr = cpu_to_dump32(s, memory_mapping->phys_addr);
247 phdr.p_filesz = cpu_to_dump32(s, filesz);
248 phdr.p_memsz = cpu_to_dump32(s, memory_mapping->length);
Jon Dorone17bebd2019-01-09 10:22:03 +0200249 phdr.p_vaddr =
250 cpu_to_dump32(s, memory_mapping->virt_addr) ?: phdr.p_paddr;
Wen Congyang783e9b42012-05-07 12:10:47 +0800251
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200252 assert(memory_mapping->length >= filesz);
253
Wen Congyang783e9b42012-05-07 12:10:47 +0800254 ret = fd_write_vmcore(&phdr, sizeof(Elf32_Phdr), s);
255 if (ret < 0) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200256 error_setg_errno(errp, -ret,
257 "dump: failed to write program header table");
Wen Congyang783e9b42012-05-07 12:10:47 +0800258 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800259}
260
Janosch Frank2341a942022-08-11 12:11:01 +0000261static void prepare_elf64_phdr_note(DumpState *s, Elf64_Phdr *phdr)
Wen Congyang783e9b42012-05-07 12:10:47 +0800262{
Janosch Frankbc7d5582022-03-30 12:36:01 +0000263 memset(phdr, 0, sizeof(*phdr));
264 phdr->p_type = cpu_to_dump32(s, PT_NOTE);
265 phdr->p_offset = cpu_to_dump64(s, s->note_offset);
266 phdr->p_paddr = 0;
267 phdr->p_filesz = cpu_to_dump64(s, s->note_size);
268 phdr->p_memsz = cpu_to_dump64(s, s->note_size);
269 phdr->p_vaddr = 0;
Wen Congyang783e9b42012-05-07 12:10:47 +0800270}
271
Paolo Bonzini0bc3cd62013-04-08 17:29:59 +0200272static inline int cpu_index(CPUState *cpu)
273{
274 return cpu->cpu_index + 1;
275}
276
Marc-André Lureau903ef732017-09-11 18:59:25 +0200277static void write_guest_note(WriteCoreDumpFunction f, DumpState *s,
278 Error **errp)
279{
280 int ret;
281
282 if (s->guest_note) {
283 ret = f(s->guest_note, s->guest_note_size, s);
284 if (ret < 0) {
285 error_setg(errp, "dump: failed to write guest note");
286 }
287 }
288}
289
zhanghailiang4c7e2512014-10-09 14:13:11 +0800290static void write_elf64_notes(WriteCoreDumpFunction f, DumpState *s,
291 Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800292{
Andreas Färber0d342822012-12-17 07:12:13 +0100293 CPUState *cpu;
Wen Congyang783e9b42012-05-07 12:10:47 +0800294 int ret;
295 int id;
296
Andreas Färberbdc44642013-06-24 23:50:24 +0200297 CPU_FOREACH(cpu) {
Andreas Färber0d342822012-12-17 07:12:13 +0100298 id = cpu_index(cpu);
qiaonuohan6a519912014-02-18 14:11:26 +0800299 ret = cpu_write_elf64_note(f, cpu, id, s);
Wen Congyang783e9b42012-05-07 12:10:47 +0800300 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800301 error_setg(errp, "dump: failed to write elf notes");
zhanghailiang4c7e2512014-10-09 14:13:11 +0800302 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800303 }
304 }
305
Andreas Färberbdc44642013-06-24 23:50:24 +0200306 CPU_FOREACH(cpu) {
qiaonuohan6a519912014-02-18 14:11:26 +0800307 ret = cpu_write_elf64_qemunote(f, cpu, s);
Wen Congyang783e9b42012-05-07 12:10:47 +0800308 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800309 error_setg(errp, "dump: failed to write CPU status");
zhanghailiang4c7e2512014-10-09 14:13:11 +0800310 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800311 }
312 }
Marc-André Lureau903ef732017-09-11 18:59:25 +0200313
314 write_guest_note(f, s, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800315}
316
Janosch Frank2341a942022-08-11 12:11:01 +0000317static void prepare_elf32_phdr_note(DumpState *s, Elf32_Phdr *phdr)
Wen Congyang783e9b42012-05-07 12:10:47 +0800318{
Janosch Frankbc7d5582022-03-30 12:36:01 +0000319 memset(phdr, 0, sizeof(*phdr));
320 phdr->p_type = cpu_to_dump32(s, PT_NOTE);
321 phdr->p_offset = cpu_to_dump32(s, s->note_offset);
322 phdr->p_paddr = 0;
323 phdr->p_filesz = cpu_to_dump32(s, s->note_size);
324 phdr->p_memsz = cpu_to_dump32(s, s->note_size);
325 phdr->p_vaddr = 0;
Wen Congyang783e9b42012-05-07 12:10:47 +0800326}
327
zhanghailiang4c7e2512014-10-09 14:13:11 +0800328static void write_elf32_notes(WriteCoreDumpFunction f, DumpState *s,
329 Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800330{
Andreas Färber0d342822012-12-17 07:12:13 +0100331 CPUState *cpu;
Wen Congyang783e9b42012-05-07 12:10:47 +0800332 int ret;
333 int id;
334
Andreas Färberbdc44642013-06-24 23:50:24 +0200335 CPU_FOREACH(cpu) {
Andreas Färber0d342822012-12-17 07:12:13 +0100336 id = cpu_index(cpu);
qiaonuohan6a519912014-02-18 14:11:26 +0800337 ret = cpu_write_elf32_note(f, cpu, id, s);
Wen Congyang783e9b42012-05-07 12:10:47 +0800338 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800339 error_setg(errp, "dump: failed to write elf notes");
zhanghailiang4c7e2512014-10-09 14:13:11 +0800340 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800341 }
342 }
343
Andreas Färberbdc44642013-06-24 23:50:24 +0200344 CPU_FOREACH(cpu) {
qiaonuohan6a519912014-02-18 14:11:26 +0800345 ret = cpu_write_elf32_qemunote(f, cpu, s);
Wen Congyang783e9b42012-05-07 12:10:47 +0800346 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800347 error_setg(errp, "dump: failed to write CPU status");
zhanghailiang4c7e2512014-10-09 14:13:11 +0800348 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800349 }
350 }
Marc-André Lureau903ef732017-09-11 18:59:25 +0200351
352 write_guest_note(f, s, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800353}
354
Janosch Frankbc7d5582022-03-30 12:36:01 +0000355static void write_elf_phdr_note(DumpState *s, Error **errp)
356{
Janosch Frankbc7d5582022-03-30 12:36:01 +0000357 Elf32_Phdr phdr32;
358 Elf64_Phdr phdr64;
359 void *phdr;
360 size_t size;
361 int ret;
362
363 if (dump_is_64bit(s)) {
Janosch Frank2341a942022-08-11 12:11:01 +0000364 prepare_elf64_phdr_note(s, &phdr64);
Janosch Frankbc7d5582022-03-30 12:36:01 +0000365 size = sizeof(phdr64);
366 phdr = &phdr64;
367 } else {
Janosch Frank2341a942022-08-11 12:11:01 +0000368 prepare_elf32_phdr_note(s, &phdr32);
Janosch Frankbc7d5582022-03-30 12:36:01 +0000369 size = sizeof(phdr32);
370 phdr = &phdr32;
371 }
372
373 ret = fd_write_vmcore(phdr, size, s);
374 if (ret < 0) {
375 error_setg_errno(errp, -ret,
376 "dump: failed to write program header table");
377 }
378}
379
Janosch Franke41ed292022-10-17 08:38:13 +0000380static void prepare_elf_section_hdr_zero(DumpState *s)
Wen Congyang783e9b42012-05-07 12:10:47 +0800381{
Janosch Franke41ed292022-10-17 08:38:13 +0000382 if (dump_is_64bit(s)) {
383 Elf64_Shdr *shdr64 = s->elf_section_hdrs;
384
385 shdr64->sh_info = cpu_to_dump32(s, s->phdr_num);
386 } else {
387 Elf32_Shdr *shdr32 = s->elf_section_hdrs;
388
389 shdr32->sh_info = cpu_to_dump32(s, s->phdr_num);
390 }
391}
392
Janosch Frank9b722242022-10-17 11:32:10 +0000393static void prepare_elf_section_hdr_string(DumpState *s, void *buff)
394{
395 uint64_t index = s->string_table_buf->len;
396 const char strtab[] = ".shstrtab";
397 Elf32_Shdr shdr32 = {};
398 Elf64_Shdr shdr64 = {};
399 int shdr_size;
400 void *shdr;
401
402 g_array_append_vals(s->string_table_buf, strtab, sizeof(strtab));
403 if (dump_is_64bit(s)) {
404 shdr_size = sizeof(Elf64_Shdr);
405 shdr64.sh_type = SHT_STRTAB;
406 shdr64.sh_offset = s->section_offset + s->elf_section_data_size;
407 shdr64.sh_name = index;
408 shdr64.sh_size = s->string_table_buf->len;
409 shdr = &shdr64;
410 } else {
411 shdr_size = sizeof(Elf32_Shdr);
412 shdr32.sh_type = SHT_STRTAB;
413 shdr32.sh_offset = s->section_offset + s->elf_section_data_size;
414 shdr32.sh_name = index;
415 shdr32.sh_size = s->string_table_buf->len;
416 shdr = &shdr32;
417 }
418 memcpy(buff, shdr, shdr_size);
419}
420
421static bool prepare_elf_section_hdrs(DumpState *s, Error **errp)
Janosch Franke41ed292022-10-17 08:38:13 +0000422{
423 size_t len, sizeof_shdr;
Janosch Frank9b722242022-10-17 11:32:10 +0000424 void *buff_hdr;
Janosch Franke41ed292022-10-17 08:38:13 +0000425
426 /*
427 * Section ordering:
428 * - HDR zero
Janosch Frank9b722242022-10-17 11:32:10 +0000429 * - Arch section hdrs
430 * - String table hdr
Janosch Franke41ed292022-10-17 08:38:13 +0000431 */
432 sizeof_shdr = dump_is_64bit(s) ? sizeof(Elf64_Shdr) : sizeof(Elf32_Shdr);
433 len = sizeof_shdr * s->shdr_num;
434 s->elf_section_hdrs = g_malloc0(len);
Janosch Frank9b722242022-10-17 11:32:10 +0000435 buff_hdr = s->elf_section_hdrs;
Janosch Franke41ed292022-10-17 08:38:13 +0000436
437 /*
438 * The first section header is ALWAYS a special initial section
439 * header.
440 *
441 * The header should be 0 with one exception being that if
442 * phdr_num is PN_XNUM then the sh_info field contains the real
443 * number of segment entries.
444 *
445 * As we zero allocate the buffer we will only need to modify
446 * sh_info for the PN_XNUM case.
447 */
448 if (s->phdr_num >= PN_XNUM) {
449 prepare_elf_section_hdr_zero(s);
450 }
Janosch Frank9b722242022-10-17 11:32:10 +0000451 buff_hdr += sizeof_shdr;
452
453 /* Add architecture defined section headers */
454 if (s->dump_info.arch_sections_write_hdr_fn
455 && s->shdr_num > 2) {
456 buff_hdr += s->dump_info.arch_sections_write_hdr_fn(s, buff_hdr);
457
458 if (s->shdr_num >= SHN_LORESERVE) {
459 error_setg_errno(errp, EINVAL,
460 "dump: too many architecture defined sections");
461 return false;
462 }
463 }
464
465 /*
466 * String table is the last section since strings are added via
467 * arch_sections_write_hdr().
468 */
469 prepare_elf_section_hdr_string(s, buff_hdr);
470 return true;
Janosch Franke41ed292022-10-17 08:38:13 +0000471}
472
473static void write_elf_section_headers(DumpState *s, Error **errp)
474{
475 size_t sizeof_shdr = dump_is_64bit(s) ? sizeof(Elf64_Shdr) : sizeof(Elf32_Shdr);
Wen Congyang783e9b42012-05-07 12:10:47 +0800476 int ret;
477
Janosch Frank9b722242022-10-17 11:32:10 +0000478 if (!prepare_elf_section_hdrs(s, errp)) {
479 return;
480 }
Janosch Franke41ed292022-10-17 08:38:13 +0000481
482 ret = fd_write_vmcore(s->elf_section_hdrs, s->shdr_num * sizeof_shdr, s);
483 if (ret < 0) {
484 error_setg_errno(errp, -ret, "dump: failed to write section headers");
Wen Congyang783e9b42012-05-07 12:10:47 +0800485 }
486
Janosch Franke41ed292022-10-17 08:38:13 +0000487 g_free(s->elf_section_hdrs);
Wen Congyang783e9b42012-05-07 12:10:47 +0800488}
489
Janosch Frank9b722242022-10-17 11:32:10 +0000490static void write_elf_sections(DumpState *s, Error **errp)
491{
492 int ret;
493
494 if (s->elf_section_data_size) {
495 /* Write architecture section data */
496 ret = fd_write_vmcore(s->elf_section_data,
497 s->elf_section_data_size, s);
498 if (ret < 0) {
499 error_setg_errno(errp, -ret,
500 "dump: failed to write architecture section data");
501 return;
502 }
503 }
504
505 /* Write string table */
506 ret = fd_write_vmcore(s->string_table_buf->data,
507 s->string_table_buf->len, s);
508 if (ret < 0) {
509 error_setg_errno(errp, -ret, "dump: failed to write string table data");
510 }
511}
512
zhanghailiang4c7e2512014-10-09 14:13:11 +0800513static void write_data(DumpState *s, void *buf, int length, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800514{
515 int ret;
516
517 ret = fd_write_vmcore(buf, length, s);
518 if (ret < 0) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200519 error_setg_errno(errp, -ret, "dump: failed to save memory");
Peter Xu2264c2c2016-02-18 13:16:53 +0800520 } else {
521 s->written_size += length;
Wen Congyang783e9b42012-05-07 12:10:47 +0800522 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800523}
524
zhanghailiang4c7e2512014-10-09 14:13:11 +0800525/* write the memory to vmcore. 1 page per I/O. */
526static void write_memory(DumpState *s, GuestPhysBlock *block, ram_addr_t start,
527 int64_t size, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800528{
Janosch Frank86a518b2022-03-30 12:35:55 +0000529 ERRP_GUARD();
Wen Congyang783e9b42012-05-07 12:10:47 +0800530 int64_t i;
Wen Congyang783e9b42012-05-07 12:10:47 +0800531
Andrew Jones8161bef2016-01-11 20:56:20 +0100532 for (i = 0; i < size / s->dump_info.page_size; i++) {
533 write_data(s, block->host_addr + start + i * s->dump_info.page_size,
Janosch Frank86a518b2022-03-30 12:35:55 +0000534 s->dump_info.page_size, errp);
535 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800536 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800537 }
538 }
539
Andrew Jones8161bef2016-01-11 20:56:20 +0100540 if ((size % s->dump_info.page_size) != 0) {
541 write_data(s, block->host_addr + start + i * s->dump_info.page_size,
Janosch Frank86a518b2022-03-30 12:35:55 +0000542 size % s->dump_info.page_size, errp);
543 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800544 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800545 }
546 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800547}
548
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200549/* get the memory's offset and size in the vmcore */
550static void get_offset_range(hwaddr phys_addr,
551 ram_addr_t mapping_length,
552 DumpState *s,
553 hwaddr *p_offset,
554 hwaddr *p_filesz)
Wen Congyang783e9b42012-05-07 12:10:47 +0800555{
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200556 GuestPhysBlock *block;
Avi Kivitya8170e52012-10-23 12:30:10 +0200557 hwaddr offset = s->memory_offset;
Wen Congyang783e9b42012-05-07 12:10:47 +0800558 int64_t size_in_block, start;
559
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200560 /* When the memory is not stored into vmcore, offset will be -1 */
561 *p_offset = -1;
562 *p_filesz = 0;
563
Janosch Frankdddf7252022-08-11 12:10:58 +0000564 if (dump_has_filter(s)) {
565 if (phys_addr < s->filter_area_begin ||
566 phys_addr >= s->filter_area_begin + s->filter_area_length) {
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200567 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800568 }
569 }
570
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200571 QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
Janosch Frankdddf7252022-08-11 12:10:58 +0000572 if (dump_has_filter(s)) {
573 if (block->target_start >= s->filter_area_begin + s->filter_area_length ||
574 block->target_end <= s->filter_area_begin) {
Wen Congyang783e9b42012-05-07 12:10:47 +0800575 /* This block is out of the range */
576 continue;
577 }
578
Janosch Frankdddf7252022-08-11 12:10:58 +0000579 if (s->filter_area_begin <= block->target_start) {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200580 start = block->target_start;
Wen Congyang783e9b42012-05-07 12:10:47 +0800581 } else {
Janosch Frankdddf7252022-08-11 12:10:58 +0000582 start = s->filter_area_begin;
Wen Congyang783e9b42012-05-07 12:10:47 +0800583 }
584
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200585 size_in_block = block->target_end - start;
Janosch Frankdddf7252022-08-11 12:10:58 +0000586 if (s->filter_area_begin + s->filter_area_length < block->target_end) {
587 size_in_block -= block->target_end - (s->filter_area_begin + s->filter_area_length);
Wen Congyang783e9b42012-05-07 12:10:47 +0800588 }
589 } else {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200590 start = block->target_start;
591 size_in_block = block->target_end - block->target_start;
Wen Congyang783e9b42012-05-07 12:10:47 +0800592 }
593
594 if (phys_addr >= start && phys_addr < start + size_in_block) {
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200595 *p_offset = phys_addr - start + offset;
596
597 /* The offset range mapped from the vmcore file must not spill over
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200598 * the GuestPhysBlock, clamp it. The rest of the mapping will be
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200599 * zero-filled in memory at load time; see
600 * <http://refspecs.linuxbase.org/elf/gabi4+/ch5.pheader.html>.
601 */
602 *p_filesz = phys_addr + mapping_length <= start + size_in_block ?
603 mapping_length :
604 size_in_block - (phys_addr - start);
605 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800606 }
607
608 offset += size_in_block;
609 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800610}
611
Janosch Frankafae6052022-08-11 12:10:55 +0000612static void write_elf_phdr_loads(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800613{
Janosch Frank86a518b2022-03-30 12:35:55 +0000614 ERRP_GUARD();
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200615 hwaddr offset, filesz;
Wen Congyang783e9b42012-05-07 12:10:47 +0800616 MemoryMapping *memory_mapping;
617 uint32_t phdr_index = 1;
Wen Congyang783e9b42012-05-07 12:10:47 +0800618
619 QTAILQ_FOREACH(memory_mapping, &s->list.head, next) {
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200620 get_offset_range(memory_mapping->phys_addr,
621 memory_mapping->length,
622 s, &offset, &filesz);
Janosch Frank05bbaa502022-03-30 12:36:00 +0000623 if (dump_is_64bit(s)) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800624 write_elf64_load(s, memory_mapping, phdr_index++, offset,
Janosch Frank86a518b2022-03-30 12:35:55 +0000625 filesz, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800626 } else {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800627 write_elf32_load(s, memory_mapping, phdr_index++, offset,
Janosch Frank86a518b2022-03-30 12:35:55 +0000628 filesz, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800629 }
630
Janosch Frank86a518b2022-03-30 12:35:55 +0000631 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800632 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800633 }
634
Janosch Frank046bc412022-04-07 09:48:24 +0000635 if (phdr_index >= s->phdr_num) {
Wen Congyang783e9b42012-05-07 12:10:47 +0800636 break;
637 }
638 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800639}
640
Janosch Frankc6812472022-03-30 12:36:03 +0000641static void write_elf_notes(DumpState *s, Error **errp)
642{
643 if (dump_is_64bit(s)) {
644 write_elf64_notes(fd_write_vmcore, s, errp);
645 } else {
646 write_elf32_notes(fd_write_vmcore, s, errp);
647 }
648}
649
Wen Congyang783e9b42012-05-07 12:10:47 +0800650/* write elf header, PT_NOTE and elf note to vmcore. */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800651static void dump_begin(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800652{
Janosch Frank86a518b2022-03-30 12:35:55 +0000653 ERRP_GUARD();
Wen Congyang783e9b42012-05-07 12:10:47 +0800654
655 /*
656 * the vmcore's format is:
657 * --------------
658 * | elf header |
659 * --------------
Janosch Frankcb415fd2022-10-17 08:38:14 +0000660 * | sctn_hdr |
661 * --------------
Wen Congyang783e9b42012-05-07 12:10:47 +0800662 * | PT_NOTE |
663 * --------------
664 * | PT_LOAD |
665 * --------------
666 * | ...... |
667 * --------------
668 * | PT_LOAD |
669 * --------------
Wen Congyang783e9b42012-05-07 12:10:47 +0800670 * | elf note |
671 * --------------
672 * | memory |
673 * --------------
674 *
675 * we only know where the memory is saved after we write elf note into
676 * vmcore.
677 */
678
679 /* write elf header to vmcore */
Janosch Frank670e7692022-08-11 12:11:00 +0000680 write_elf_header(s, errp);
Janosch Frank86a518b2022-03-30 12:35:55 +0000681 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800682 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800683 }
684
Janosch Frankcb415fd2022-10-17 08:38:14 +0000685 /* write section headers to vmcore */
686 write_elf_section_headers(s, errp);
687 if (*errp) {
688 return;
689 }
690
Janosch Frankbc7d5582022-03-30 12:36:01 +0000691 /* write PT_NOTE to vmcore */
692 write_elf_phdr_note(s, errp);
693 if (*errp) {
694 return;
695 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800696
Janosch Frankafae6052022-08-11 12:10:55 +0000697 /* write all PT_LOADs to vmcore */
698 write_elf_phdr_loads(s, errp);
Janosch Frank5ff2e5a2022-03-30 12:36:02 +0000699 if (*errp) {
700 return;
701 }
702
Janosch Frankc6812472022-03-30 12:36:03 +0000703 /* write notes to vmcore */
704 write_elf_notes(s, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800705}
706
Janosch Frank113d8f42022-10-17 08:38:22 +0000707int64_t dump_filtered_memblock_size(GuestPhysBlock *block,
708 int64_t filter_area_start,
709 int64_t filter_area_length)
Wen Congyang783e9b42012-05-07 12:10:47 +0800710{
Janosch Frank1e811302022-08-11 12:10:56 +0000711 int64_t size, left, right;
Wen Congyang783e9b42012-05-07 12:10:47 +0800712
Janosch Frank1e811302022-08-11 12:10:56 +0000713 /* No filter, return full size */
714 if (!filter_area_length) {
715 return block->target_end - block->target_start;
Wen Congyang783e9b42012-05-07 12:10:47 +0800716 }
Janosch Frank1e811302022-08-11 12:10:56 +0000717
718 /* calculate the overlapped region. */
719 left = MAX(filter_area_start, block->target_start);
720 right = MIN(filter_area_start + filter_area_length, block->target_end);
721 size = right - left;
722 size = size > 0 ? size : 0;
723
724 return size;
725}
726
Janosch Frank113d8f42022-10-17 08:38:22 +0000727int64_t dump_filtered_memblock_start(GuestPhysBlock *block,
728 int64_t filter_area_start,
729 int64_t filter_area_length)
Janosch Frank1e811302022-08-11 12:10:56 +0000730{
731 if (filter_area_length) {
732 /* return -1 if the block is not within filter area */
733 if (block->target_start >= filter_area_start + filter_area_length ||
734 block->target_end <= filter_area_start) {
735 return -1;
736 }
737
738 if (filter_area_start > block->target_start) {
739 return filter_area_start - block->target_start;
740 }
741 }
742
743 return 0;
Wen Congyang783e9b42012-05-07 12:10:47 +0800744}
745
746/* write all memory to vmcore */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800747static void dump_iterate(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800748{
Janosch Frank86a518b2022-03-30 12:35:55 +0000749 ERRP_GUARD();
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200750 GuestPhysBlock *block;
Janosch Frank1e811302022-08-11 12:10:56 +0000751 int64_t memblock_size, memblock_start;
Wen Congyang783e9b42012-05-07 12:10:47 +0800752
Janosch Frank1e811302022-08-11 12:10:56 +0000753 QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
Janosch Frankdddf7252022-08-11 12:10:58 +0000754 memblock_start = dump_filtered_memblock_start(block, s->filter_area_begin, s->filter_area_length);
Janosch Frank1e811302022-08-11 12:10:56 +0000755 if (memblock_start == -1) {
756 continue;
Wen Congyang783e9b42012-05-07 12:10:47 +0800757 }
Janosch Frank1e811302022-08-11 12:10:56 +0000758
Janosch Frankdddf7252022-08-11 12:10:58 +0000759 memblock_size = dump_filtered_memblock_size(block, s->filter_area_begin, s->filter_area_length);
Janosch Frank1e811302022-08-11 12:10:56 +0000760
761 /* Write the memory to file */
762 write_memory(s, block, memblock_start, memblock_size, errp);
Janosch Frank86a518b2022-03-30 12:35:55 +0000763 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800764 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800765 }
Janosch Frank1e811302022-08-11 12:10:56 +0000766 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800767}
768
Janosch Frank9b722242022-10-17 11:32:10 +0000769static void dump_end(DumpState *s, Error **errp)
770{
771 int rc;
Janosch Frank9b722242022-10-17 11:32:10 +0000772
773 if (s->elf_section_data_size) {
774 s->elf_section_data = g_malloc0(s->elf_section_data_size);
775 }
776
777 /* Adds the architecture defined section data to s->elf_section_data */
778 if (s->dump_info.arch_sections_write_fn &&
779 s->elf_section_data_size) {
780 rc = s->dump_info.arch_sections_write_fn(s, s->elf_section_data);
781 if (rc) {
782 error_setg_errno(errp, rc,
783 "dump: failed to get arch section data");
784 g_free(s->elf_section_data);
785 return;
786 }
787 }
788
789 /* write sections to vmcore */
790 write_elf_sections(s, errp);
791}
792
zhanghailiang4c7e2512014-10-09 14:13:11 +0800793static void create_vmcore(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800794{
Janosch Frank86a518b2022-03-30 12:35:55 +0000795 ERRP_GUARD();
Wen Congyang783e9b42012-05-07 12:10:47 +0800796
Janosch Frank86a518b2022-03-30 12:35:55 +0000797 dump_begin(s, errp);
798 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800799 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800800 }
801
Janosch Frank9b722242022-10-17 11:32:10 +0000802 /* Iterate over memory and dump it to file */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800803 dump_iterate(s, errp);
Janosch Frank9b722242022-10-17 11:32:10 +0000804 if (*errp) {
805 return;
806 }
807
808 /* Write the section data */
809 dump_end(s, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800810}
811
Stephen Brennan4d7dd4e2023-09-18 16:32:31 -0700812static int write_start_flat_header(DumpState *s)
qiaonuohanfda05382014-02-18 14:11:27 +0800813{
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200814 MakedumpfileHeader *mh;
qiaonuohanfda05382014-02-18 14:11:27 +0800815 int ret = 0;
816
Stephen Brennand43a01d2023-09-18 16:32:32 -0700817 if (s->kdump_raw) {
818 return 0;
819 }
820
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200821 QEMU_BUILD_BUG_ON(sizeof *mh > MAX_SIZE_MDF_HEADER);
822 mh = g_malloc0(MAX_SIZE_MDF_HEADER);
qiaonuohanfda05382014-02-18 14:11:27 +0800823
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200824 memcpy(mh->signature, MAKEDUMPFILE_SIGNATURE,
825 MIN(sizeof mh->signature, sizeof MAKEDUMPFILE_SIGNATURE));
qiaonuohanfda05382014-02-18 14:11:27 +0800826
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200827 mh->type = cpu_to_be64(TYPE_FLAT_HEADER);
828 mh->version = cpu_to_be64(VERSION_FLAT_HEADER);
qiaonuohanfda05382014-02-18 14:11:27 +0800829
830 size_t written_size;
Stephen Brennan4d7dd4e2023-09-18 16:32:31 -0700831 written_size = qemu_write_full(s->fd, mh, MAX_SIZE_MDF_HEADER);
qiaonuohanfda05382014-02-18 14:11:27 +0800832 if (written_size != MAX_SIZE_MDF_HEADER) {
833 ret = -1;
834 }
835
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200836 g_free(mh);
qiaonuohanfda05382014-02-18 14:11:27 +0800837 return ret;
838}
839
Stephen Brennan4d7dd4e2023-09-18 16:32:31 -0700840static int write_end_flat_header(DumpState *s)
qiaonuohanfda05382014-02-18 14:11:27 +0800841{
842 MakedumpfileDataHeader mdh;
843
Stephen Brennand43a01d2023-09-18 16:32:32 -0700844 if (s->kdump_raw) {
845 return 0;
846 }
847
qiaonuohanfda05382014-02-18 14:11:27 +0800848 mdh.offset = END_FLAG_FLAT_HEADER;
849 mdh.buf_size = END_FLAG_FLAT_HEADER;
850
851 size_t written_size;
Stephen Brennan4d7dd4e2023-09-18 16:32:31 -0700852 written_size = qemu_write_full(s->fd, &mdh, sizeof(mdh));
qiaonuohanfda05382014-02-18 14:11:27 +0800853 if (written_size != sizeof(mdh)) {
854 return -1;
855 }
856
857 return 0;
858}
859
Stephen Brennan4d7dd4e2023-09-18 16:32:31 -0700860static int write_buffer(DumpState *s, off_t offset, const void *buf, size_t size)
qiaonuohan5d31bab2014-02-18 14:11:28 +0800861{
862 size_t written_size;
863 MakedumpfileDataHeader mdh;
Stephen Brennand43a01d2023-09-18 16:32:32 -0700864 off_t seek_loc;
qiaonuohan5d31bab2014-02-18 14:11:28 +0800865
Stephen Brennand43a01d2023-09-18 16:32:32 -0700866 if (s->kdump_raw) {
867 seek_loc = lseek(s->fd, offset, SEEK_SET);
868 if (seek_loc == (off_t) -1) {
869 return -1;
870 }
871 } else {
872 mdh.offset = cpu_to_be64(offset);
873 mdh.buf_size = cpu_to_be64(size);
qiaonuohan5d31bab2014-02-18 14:11:28 +0800874
Stephen Brennand43a01d2023-09-18 16:32:32 -0700875 written_size = qemu_write_full(s->fd, &mdh, sizeof(mdh));
876 if (written_size != sizeof(mdh)) {
877 return -1;
878 }
qiaonuohan5d31bab2014-02-18 14:11:28 +0800879 }
880
Stephen Brennan4d7dd4e2023-09-18 16:32:31 -0700881 written_size = qemu_write_full(s->fd, buf, size);
qiaonuohan5d31bab2014-02-18 14:11:28 +0800882 if (written_size != size) {
883 return -1;
884 }
885
886 return 0;
887}
888
qiaonuohan4835ef72014-02-18 14:11:29 +0800889static int buf_write_note(const void *buf, size_t size, void *opaque)
890{
891 DumpState *s = opaque;
892
893 /* note_buf is not enough */
894 if (s->note_buf_offset + size > s->note_size) {
895 return -1;
896 }
897
898 memcpy(s->note_buf + s->note_buf_offset, buf, size);
899
900 s->note_buf_offset += size;
901
902 return 0;
903}
904
Marc-André Lureau903ef732017-09-11 18:59:25 +0200905/*
906 * This function retrieves various sizes from an elf header.
907 *
908 * @note has to be a valid ELF note. The return sizes are unmodified
909 * (not padded or rounded up to be multiple of 4).
910 */
911static void get_note_sizes(DumpState *s, const void *note,
912 uint64_t *note_head_size,
913 uint64_t *name_size,
914 uint64_t *desc_size)
915{
916 uint64_t note_head_sz;
917 uint64_t name_sz;
918 uint64_t desc_sz;
919
Janosch Frank05bbaa502022-03-30 12:36:00 +0000920 if (dump_is_64bit(s)) {
Marc-André Lureau903ef732017-09-11 18:59:25 +0200921 const Elf64_Nhdr *hdr = note;
922 note_head_sz = sizeof(Elf64_Nhdr);
Philippe Mathieu-Daudébb509d92022-12-16 08:28:32 +0100923 name_sz = cpu_to_dump64(s, hdr->n_namesz);
924 desc_sz = cpu_to_dump64(s, hdr->n_descsz);
Marc-André Lureau903ef732017-09-11 18:59:25 +0200925 } else {
926 const Elf32_Nhdr *hdr = note;
927 note_head_sz = sizeof(Elf32_Nhdr);
Philippe Mathieu-Daudébb509d92022-12-16 08:28:32 +0100928 name_sz = cpu_to_dump32(s, hdr->n_namesz);
929 desc_sz = cpu_to_dump32(s, hdr->n_descsz);
Marc-André Lureau903ef732017-09-11 18:59:25 +0200930 }
931
932 if (note_head_size) {
933 *note_head_size = note_head_sz;
934 }
935 if (name_size) {
936 *name_size = name_sz;
937 }
938 if (desc_size) {
939 *desc_size = desc_sz;
940 }
941}
942
Marc-André Lureaud9feb512017-09-11 18:59:26 +0200943static bool note_name_equal(DumpState *s,
944 const uint8_t *note, const char *name)
945{
946 int len = strlen(name) + 1;
947 uint64_t head_size, name_size;
948
949 get_note_sizes(s, note, &head_size, &name_size, NULL);
950 head_size = ROUND_UP(head_size, 4);
951
Marc-André Lureauc983ca82017-12-12 15:53:59 +0100952 return name_size == len && memcmp(note + head_size, name, len) == 0;
Marc-André Lureaud9feb512017-09-11 18:59:26 +0200953}
954
qiaonuohan298f1162014-02-18 14:11:32 +0800955/* write common header, sub header and elf note to vmcore */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800956static void create_header32(DumpState *s, Error **errp)
qiaonuohan298f1162014-02-18 14:11:32 +0800957{
Janosch Frank86a518b2022-03-30 12:35:55 +0000958 ERRP_GUARD();
qiaonuohan298f1162014-02-18 14:11:32 +0800959 DiskDumpHeader32 *dh = NULL;
960 KdumpSubHeader32 *kh = NULL;
961 size_t size;
qiaonuohan298f1162014-02-18 14:11:32 +0800962 uint32_t block_size;
963 uint32_t sub_hdr_size;
964 uint32_t bitmap_blocks;
965 uint32_t status = 0;
966 uint64_t offset_note;
967
968 /* write common header, the version of kdump-compressed format is 6th */
969 size = sizeof(DiskDumpHeader32);
970 dh = g_malloc0(size);
971
Eric Blake84c868f2018-03-27 15:21:51 -0500972 memcpy(dh->signature, KDUMP_SIGNATURE, SIG_LEN);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200973 dh->header_version = cpu_to_dump32(s, 6);
Andrew Jones8161bef2016-01-11 20:56:20 +0100974 block_size = s->dump_info.page_size;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200975 dh->block_size = cpu_to_dump32(s, block_size);
qiaonuohan298f1162014-02-18 14:11:32 +0800976 sub_hdr_size = sizeof(struct KdumpSubHeader32) + s->note_size;
977 sub_hdr_size = DIV_ROUND_UP(sub_hdr_size, block_size);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200978 dh->sub_hdr_size = cpu_to_dump32(s, sub_hdr_size);
qiaonuohan298f1162014-02-18 14:11:32 +0800979 /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200980 dh->max_mapnr = cpu_to_dump32(s, MIN(s->max_mapnr, UINT_MAX));
981 dh->nr_cpus = cpu_to_dump32(s, s->nr_cpus);
qiaonuohan298f1162014-02-18 14:11:32 +0800982 bitmap_blocks = DIV_ROUND_UP(s->len_dump_bitmap, block_size) * 2;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200983 dh->bitmap_blocks = cpu_to_dump32(s, bitmap_blocks);
qiaonuohan4ab23a92014-02-18 14:11:37 +0800984 strncpy(dh->utsname.machine, ELF_MACHINE_UNAME, sizeof(dh->utsname.machine));
qiaonuohan298f1162014-02-18 14:11:32 +0800985
986 if (s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) {
987 status |= DUMP_DH_COMPRESSED_ZLIB;
988 }
989#ifdef CONFIG_LZO
990 if (s->flag_compress & DUMP_DH_COMPRESSED_LZO) {
991 status |= DUMP_DH_COMPRESSED_LZO;
992 }
993#endif
994#ifdef CONFIG_SNAPPY
995 if (s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) {
996 status |= DUMP_DH_COMPRESSED_SNAPPY;
997 }
998#endif
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200999 dh->status = cpu_to_dump32(s, status);
qiaonuohan298f1162014-02-18 14:11:32 +08001000
Stephen Brennan4d7dd4e2023-09-18 16:32:31 -07001001 if (write_buffer(s, 0, dh, size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001002 error_setg(errp, "dump: failed to write disk dump header");
qiaonuohan298f1162014-02-18 14:11:32 +08001003 goto out;
1004 }
1005
1006 /* write sub header */
1007 size = sizeof(KdumpSubHeader32);
1008 kh = g_malloc0(size);
1009
1010 /* 64bit max_mapnr_64 */
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001011 kh->max_mapnr_64 = cpu_to_dump64(s, s->max_mapnr);
Andrew Jonesb6e05aa2016-01-11 20:56:21 +01001012 kh->phys_base = cpu_to_dump32(s, s->dump_info.phys_base);
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001013 kh->dump_level = cpu_to_dump32(s, DUMP_LEVEL);
qiaonuohan298f1162014-02-18 14:11:32 +08001014
1015 offset_note = DISKDUMP_HEADER_BLOCKS * block_size + size;
Marc-André Lureau9ada5752017-09-11 18:59:27 +02001016 if (s->guest_note &&
1017 note_name_equal(s, s->guest_note, "VMCOREINFO")) {
1018 uint64_t hsize, name_size, size_vmcoreinfo_desc, offset_vmcoreinfo;
1019
1020 get_note_sizes(s, s->guest_note,
1021 &hsize, &name_size, &size_vmcoreinfo_desc);
1022 offset_vmcoreinfo = offset_note + s->note_size - s->guest_note_size +
1023 (DIV_ROUND_UP(hsize, 4) + DIV_ROUND_UP(name_size, 4)) * 4;
1024 kh->offset_vmcoreinfo = cpu_to_dump64(s, offset_vmcoreinfo);
1025 kh->size_vmcoreinfo = cpu_to_dump32(s, size_vmcoreinfo_desc);
1026 }
1027
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001028 kh->offset_note = cpu_to_dump64(s, offset_note);
1029 kh->note_size = cpu_to_dump32(s, s->note_size);
qiaonuohan298f1162014-02-18 14:11:32 +08001030
Stephen Brennan4d7dd4e2023-09-18 16:32:31 -07001031 if (write_buffer(s, DISKDUMP_HEADER_BLOCKS *
qiaonuohan298f1162014-02-18 14:11:32 +08001032 block_size, kh, size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001033 error_setg(errp, "dump: failed to write kdump sub header");
qiaonuohan298f1162014-02-18 14:11:32 +08001034 goto out;
1035 }
1036
1037 /* write note */
1038 s->note_buf = g_malloc0(s->note_size);
1039 s->note_buf_offset = 0;
1040
1041 /* use s->note_buf to store notes temporarily */
Janosch Frank86a518b2022-03-30 12:35:55 +00001042 write_elf32_notes(buf_write_note, s, errp);
1043 if (*errp) {
qiaonuohan298f1162014-02-18 14:11:32 +08001044 goto out;
1045 }
Stephen Brennan4d7dd4e2023-09-18 16:32:31 -07001046 if (write_buffer(s, offset_note, s->note_buf,
qiaonuohan298f1162014-02-18 14:11:32 +08001047 s->note_size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001048 error_setg(errp, "dump: failed to write notes");
qiaonuohan298f1162014-02-18 14:11:32 +08001049 goto out;
1050 }
1051
1052 /* get offset of dump_bitmap */
1053 s->offset_dump_bitmap = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size) *
1054 block_size;
1055
1056 /* get offset of page */
1057 s->offset_page = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size + bitmap_blocks) *
1058 block_size;
1059
1060out:
1061 g_free(dh);
1062 g_free(kh);
1063 g_free(s->note_buf);
qiaonuohan298f1162014-02-18 14:11:32 +08001064}
1065
1066/* write common header, sub header and elf note to vmcore */
zhanghailiang4c7e2512014-10-09 14:13:11 +08001067static void create_header64(DumpState *s, Error **errp)
qiaonuohan298f1162014-02-18 14:11:32 +08001068{
Janosch Frank86a518b2022-03-30 12:35:55 +00001069 ERRP_GUARD();
qiaonuohan298f1162014-02-18 14:11:32 +08001070 DiskDumpHeader64 *dh = NULL;
1071 KdumpSubHeader64 *kh = NULL;
1072 size_t size;
qiaonuohan298f1162014-02-18 14:11:32 +08001073 uint32_t block_size;
1074 uint32_t sub_hdr_size;
1075 uint32_t bitmap_blocks;
1076 uint32_t status = 0;
1077 uint64_t offset_note;
1078
1079 /* write common header, the version of kdump-compressed format is 6th */
1080 size = sizeof(DiskDumpHeader64);
1081 dh = g_malloc0(size);
1082
Eric Blake84c868f2018-03-27 15:21:51 -05001083 memcpy(dh->signature, KDUMP_SIGNATURE, SIG_LEN);
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001084 dh->header_version = cpu_to_dump32(s, 6);
Andrew Jones8161bef2016-01-11 20:56:20 +01001085 block_size = s->dump_info.page_size;
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001086 dh->block_size = cpu_to_dump32(s, block_size);
qiaonuohan298f1162014-02-18 14:11:32 +08001087 sub_hdr_size = sizeof(struct KdumpSubHeader64) + s->note_size;
1088 sub_hdr_size = DIV_ROUND_UP(sub_hdr_size, block_size);
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001089 dh->sub_hdr_size = cpu_to_dump32(s, sub_hdr_size);
qiaonuohan298f1162014-02-18 14:11:32 +08001090 /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001091 dh->max_mapnr = cpu_to_dump32(s, MIN(s->max_mapnr, UINT_MAX));
1092 dh->nr_cpus = cpu_to_dump32(s, s->nr_cpus);
qiaonuohan298f1162014-02-18 14:11:32 +08001093 bitmap_blocks = DIV_ROUND_UP(s->len_dump_bitmap, block_size) * 2;
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001094 dh->bitmap_blocks = cpu_to_dump32(s, bitmap_blocks);
qiaonuohan4ab23a92014-02-18 14:11:37 +08001095 strncpy(dh->utsname.machine, ELF_MACHINE_UNAME, sizeof(dh->utsname.machine));
qiaonuohan298f1162014-02-18 14:11:32 +08001096
1097 if (s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) {
1098 status |= DUMP_DH_COMPRESSED_ZLIB;
1099 }
1100#ifdef CONFIG_LZO
1101 if (s->flag_compress & DUMP_DH_COMPRESSED_LZO) {
1102 status |= DUMP_DH_COMPRESSED_LZO;
1103 }
1104#endif
1105#ifdef CONFIG_SNAPPY
1106 if (s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) {
1107 status |= DUMP_DH_COMPRESSED_SNAPPY;
1108 }
1109#endif
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001110 dh->status = cpu_to_dump32(s, status);
qiaonuohan298f1162014-02-18 14:11:32 +08001111
Stephen Brennan4d7dd4e2023-09-18 16:32:31 -07001112 if (write_buffer(s, 0, dh, size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001113 error_setg(errp, "dump: failed to write disk dump header");
qiaonuohan298f1162014-02-18 14:11:32 +08001114 goto out;
1115 }
1116
1117 /* write sub header */
1118 size = sizeof(KdumpSubHeader64);
1119 kh = g_malloc0(size);
1120
1121 /* 64bit max_mapnr_64 */
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001122 kh->max_mapnr_64 = cpu_to_dump64(s, s->max_mapnr);
Andrew Jonesb6e05aa2016-01-11 20:56:21 +01001123 kh->phys_base = cpu_to_dump64(s, s->dump_info.phys_base);
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001124 kh->dump_level = cpu_to_dump32(s, DUMP_LEVEL);
qiaonuohan298f1162014-02-18 14:11:32 +08001125
1126 offset_note = DISKDUMP_HEADER_BLOCKS * block_size + size;
Marc-André Lureau9ada5752017-09-11 18:59:27 +02001127 if (s->guest_note &&
1128 note_name_equal(s, s->guest_note, "VMCOREINFO")) {
1129 uint64_t hsize, name_size, size_vmcoreinfo_desc, offset_vmcoreinfo;
1130
1131 get_note_sizes(s, s->guest_note,
1132 &hsize, &name_size, &size_vmcoreinfo_desc);
1133 offset_vmcoreinfo = offset_note + s->note_size - s->guest_note_size +
1134 (DIV_ROUND_UP(hsize, 4) + DIV_ROUND_UP(name_size, 4)) * 4;
1135 kh->offset_vmcoreinfo = cpu_to_dump64(s, offset_vmcoreinfo);
1136 kh->size_vmcoreinfo = cpu_to_dump64(s, size_vmcoreinfo_desc);
1137 }
1138
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001139 kh->offset_note = cpu_to_dump64(s, offset_note);
1140 kh->note_size = cpu_to_dump64(s, s->note_size);
qiaonuohan298f1162014-02-18 14:11:32 +08001141
Stephen Brennan4d7dd4e2023-09-18 16:32:31 -07001142 if (write_buffer(s, DISKDUMP_HEADER_BLOCKS *
qiaonuohan298f1162014-02-18 14:11:32 +08001143 block_size, kh, size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001144 error_setg(errp, "dump: failed to write kdump sub header");
qiaonuohan298f1162014-02-18 14:11:32 +08001145 goto out;
1146 }
1147
1148 /* write note */
1149 s->note_buf = g_malloc0(s->note_size);
1150 s->note_buf_offset = 0;
1151
1152 /* use s->note_buf to store notes temporarily */
Janosch Frank86a518b2022-03-30 12:35:55 +00001153 write_elf64_notes(buf_write_note, s, errp);
1154 if (*errp) {
qiaonuohan298f1162014-02-18 14:11:32 +08001155 goto out;
1156 }
1157
Stephen Brennan4d7dd4e2023-09-18 16:32:31 -07001158 if (write_buffer(s, offset_note, s->note_buf,
qiaonuohan298f1162014-02-18 14:11:32 +08001159 s->note_size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001160 error_setg(errp, "dump: failed to write notes");
qiaonuohan298f1162014-02-18 14:11:32 +08001161 goto out;
1162 }
1163
1164 /* get offset of dump_bitmap */
1165 s->offset_dump_bitmap = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size) *
1166 block_size;
1167
1168 /* get offset of page */
1169 s->offset_page = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size + bitmap_blocks) *
1170 block_size;
1171
1172out:
1173 g_free(dh);
1174 g_free(kh);
1175 g_free(s->note_buf);
qiaonuohan298f1162014-02-18 14:11:32 +08001176}
1177
zhanghailiang4c7e2512014-10-09 14:13:11 +08001178static void write_dump_header(DumpState *s, Error **errp)
qiaonuohan298f1162014-02-18 14:11:32 +08001179{
Janosch Frank05bbaa502022-03-30 12:36:00 +00001180 if (dump_is_64bit(s)) {
Markus Armbruster992861f2020-07-07 18:06:04 +02001181 create_header64(s, errp);
Janosch Frank05bbaa502022-03-30 12:36:00 +00001182 } else {
1183 create_header32(s, errp);
zhanghailiang4c7e2512014-10-09 14:13:11 +08001184 }
qiaonuohan298f1162014-02-18 14:11:32 +08001185}
1186
Andrew Jones8161bef2016-01-11 20:56:20 +01001187static size_t dump_bitmap_get_bufsize(DumpState *s)
1188{
1189 return s->dump_info.page_size;
1190}
1191
qiaonuohand0686c72014-02-18 14:11:33 +08001192/*
1193 * set dump_bitmap sequencely. the bit before last_pfn is not allowed to be
1194 * rewritten, so if need to set the first bit, set last_pfn and pfn to 0.
1195 * set_dump_bitmap will always leave the recently set bit un-sync. And setting
1196 * (last bit + sizeof(buf) * 8) to 0 will do flushing the content in buf into
1197 * vmcore, ie. synchronizing un-sync bit into vmcore.
1198 */
1199static int set_dump_bitmap(uint64_t last_pfn, uint64_t pfn, bool value,
1200 uint8_t *buf, DumpState *s)
1201{
1202 off_t old_offset, new_offset;
1203 off_t offset_bitmap1, offset_bitmap2;
1204 uint32_t byte, bit;
Andrew Jones8161bef2016-01-11 20:56:20 +01001205 size_t bitmap_bufsize = dump_bitmap_get_bufsize(s);
1206 size_t bits_per_buf = bitmap_bufsize * CHAR_BIT;
qiaonuohand0686c72014-02-18 14:11:33 +08001207
1208 /* should not set the previous place */
1209 assert(last_pfn <= pfn);
1210
1211 /*
1212 * if the bit needed to be set is not cached in buf, flush the data in buf
1213 * to vmcore firstly.
1214 * making new_offset be bigger than old_offset can also sync remained data
1215 * into vmcore.
1216 */
Andrew Jones8161bef2016-01-11 20:56:20 +01001217 old_offset = bitmap_bufsize * (last_pfn / bits_per_buf);
1218 new_offset = bitmap_bufsize * (pfn / bits_per_buf);
qiaonuohand0686c72014-02-18 14:11:33 +08001219
1220 while (old_offset < new_offset) {
1221 /* calculate the offset and write dump_bitmap */
1222 offset_bitmap1 = s->offset_dump_bitmap + old_offset;
Stephen Brennan4d7dd4e2023-09-18 16:32:31 -07001223 if (write_buffer(s, offset_bitmap1, buf,
Andrew Jones8161bef2016-01-11 20:56:20 +01001224 bitmap_bufsize) < 0) {
qiaonuohand0686c72014-02-18 14:11:33 +08001225 return -1;
1226 }
1227
1228 /* dump level 1 is chosen, so 1st and 2nd bitmap are same */
1229 offset_bitmap2 = s->offset_dump_bitmap + s->len_dump_bitmap +
1230 old_offset;
Stephen Brennan4d7dd4e2023-09-18 16:32:31 -07001231 if (write_buffer(s, offset_bitmap2, buf,
Andrew Jones8161bef2016-01-11 20:56:20 +01001232 bitmap_bufsize) < 0) {
qiaonuohand0686c72014-02-18 14:11:33 +08001233 return -1;
1234 }
1235
Andrew Jones8161bef2016-01-11 20:56:20 +01001236 memset(buf, 0, bitmap_bufsize);
1237 old_offset += bitmap_bufsize;
qiaonuohand0686c72014-02-18 14:11:33 +08001238 }
1239
1240 /* get the exact place of the bit in the buf, and set it */
Andrew Jones8161bef2016-01-11 20:56:20 +01001241 byte = (pfn % bits_per_buf) / CHAR_BIT;
1242 bit = (pfn % bits_per_buf) % CHAR_BIT;
qiaonuohand0686c72014-02-18 14:11:33 +08001243 if (value) {
1244 buf[byte] |= 1u << bit;
1245 } else {
1246 buf[byte] &= ~(1u << bit);
1247 }
1248
1249 return 0;
1250}
1251
Andrew Jones8161bef2016-01-11 20:56:20 +01001252static uint64_t dump_paddr_to_pfn(DumpState *s, uint64_t addr)
1253{
1254 int target_page_shift = ctz32(s->dump_info.page_size);
1255
1256 return (addr >> target_page_shift) - ARCH_PFN_OFFSET;
1257}
1258
1259static uint64_t dump_pfn_to_paddr(DumpState *s, uint64_t pfn)
1260{
1261 int target_page_shift = ctz32(s->dump_info.page_size);
1262
1263 return (pfn + ARCH_PFN_OFFSET) << target_page_shift;
1264}
1265
qiaonuohand0686c72014-02-18 14:11:33 +08001266/*
Marc-André Lureau94d78842022-09-05 16:06:21 +04001267 * Return the page frame number and the page content in *bufptr. bufptr can be
1268 * NULL. If not NULL, *bufptr must contains a target page size of pre-allocated
1269 * memory. This is not necessarily the memory returned.
qiaonuohand0686c72014-02-18 14:11:33 +08001270 */
1271static bool get_next_page(GuestPhysBlock **blockptr, uint64_t *pfnptr,
1272 uint8_t **bufptr, DumpState *s)
1273{
1274 GuestPhysBlock *block = *blockptr;
Marc-André Lureau94d78842022-09-05 16:06:21 +04001275 uint32_t page_size = s->dump_info.page_size;
1276 uint8_t *buf = NULL, *hbuf;
1277 hwaddr addr;
qiaonuohand0686c72014-02-18 14:11:33 +08001278
1279 /* block == NULL means the start of the iteration */
1280 if (!block) {
1281 block = QTAILQ_FIRST(&s->guest_phys_blocks.head);
1282 *blockptr = block;
Marc-André Lureau08df3432022-08-25 12:40:12 +04001283 addr = block->target_start;
Marc-André Lureau94d78842022-09-05 16:06:21 +04001284 *pfnptr = dump_paddr_to_pfn(s, addr);
Marc-André Lureau08df3432022-08-25 12:40:12 +04001285 } else {
Marc-André Lureau94d78842022-09-05 16:06:21 +04001286 *pfnptr += 1;
1287 addr = dump_pfn_to_paddr(s, *pfnptr);
qiaonuohand0686c72014-02-18 14:11:33 +08001288 }
Marc-André Lureau08df3432022-08-25 12:40:12 +04001289 assert(block != NULL);
qiaonuohand0686c72014-02-18 14:11:33 +08001290
Marc-André Lureau94d78842022-09-05 16:06:21 +04001291 while (1) {
1292 if (addr >= block->target_start && addr < block->target_end) {
1293 size_t n = MIN(block->target_end - addr, page_size - addr % page_size);
1294 hbuf = block->host_addr + (addr - block->target_start);
1295 if (!buf) {
1296 if (n == page_size) {
1297 /* this is a whole target page, go for it */
1298 assert(addr % page_size == 0);
1299 buf = hbuf;
1300 break;
1301 } else if (bufptr) {
1302 assert(*bufptr);
1303 buf = *bufptr;
1304 memset(buf, 0, page_size);
1305 } else {
1306 return true;
1307 }
1308 }
1309
1310 memcpy(buf + addr % page_size, hbuf, n);
1311 addr += n;
Dongli Zhang8a646092023-07-12 22:58:19 -07001312 if (addr % page_size == 0 || addr >= block->target_end) {
1313 /* we filled up the page or the current block is finished */
Marc-André Lureau94d78842022-09-05 16:06:21 +04001314 break;
1315 }
1316 } else {
1317 /* the next page is in the next block */
1318 *blockptr = block = QTAILQ_NEXT(block, next);
1319 if (!block) {
1320 break;
1321 }
1322
1323 addr = block->target_start;
1324 /* are we still in the same page? */
1325 if (dump_paddr_to_pfn(s, addr) != *pfnptr) {
1326 if (buf) {
1327 /* no, but we already filled something earlier, return it */
1328 break;
1329 } else {
1330 /* else continue from there */
1331 *pfnptr = dump_paddr_to_pfn(s, addr);
1332 }
1333 }
qiaonuohand0686c72014-02-18 14:11:33 +08001334 }
qiaonuohand0686c72014-02-18 14:11:33 +08001335 }
1336
1337 if (bufptr) {
1338 *bufptr = buf;
1339 }
1340
Marc-André Lureau94d78842022-09-05 16:06:21 +04001341 return buf != NULL;
qiaonuohand0686c72014-02-18 14:11:33 +08001342}
1343
zhanghailiang4c7e2512014-10-09 14:13:11 +08001344static void write_dump_bitmap(DumpState *s, Error **errp)
qiaonuohand0686c72014-02-18 14:11:33 +08001345{
1346 int ret = 0;
1347 uint64_t last_pfn, pfn;
1348 void *dump_bitmap_buf;
1349 size_t num_dumpable;
1350 GuestPhysBlock *block_iter = NULL;
Andrew Jones8161bef2016-01-11 20:56:20 +01001351 size_t bitmap_bufsize = dump_bitmap_get_bufsize(s);
1352 size_t bits_per_buf = bitmap_bufsize * CHAR_BIT;
qiaonuohand0686c72014-02-18 14:11:33 +08001353
1354 /* dump_bitmap_buf is used to store dump_bitmap temporarily */
Andrew Jones8161bef2016-01-11 20:56:20 +01001355 dump_bitmap_buf = g_malloc0(bitmap_bufsize);
qiaonuohand0686c72014-02-18 14:11:33 +08001356
1357 num_dumpable = 0;
1358 last_pfn = 0;
1359
1360 /*
1361 * exam memory page by page, and set the bit in dump_bitmap corresponded
1362 * to the existing page.
1363 */
1364 while (get_next_page(&block_iter, &pfn, NULL, s)) {
1365 ret = set_dump_bitmap(last_pfn, pfn, true, dump_bitmap_buf, s);
1366 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001367 error_setg(errp, "dump: failed to set dump_bitmap");
qiaonuohand0686c72014-02-18 14:11:33 +08001368 goto out;
1369 }
1370
1371 last_pfn = pfn;
1372 num_dumpable++;
1373 }
1374
1375 /*
1376 * set_dump_bitmap will always leave the recently set bit un-sync. Here we
Andrew Jones8161bef2016-01-11 20:56:20 +01001377 * set the remaining bits from last_pfn to the end of the bitmap buffer to
1378 * 0. With those set, the un-sync bit will be synchronized into the vmcore.
qiaonuohand0686c72014-02-18 14:11:33 +08001379 */
1380 if (num_dumpable > 0) {
Andrew Jones8161bef2016-01-11 20:56:20 +01001381 ret = set_dump_bitmap(last_pfn, last_pfn + bits_per_buf, false,
qiaonuohand0686c72014-02-18 14:11:33 +08001382 dump_bitmap_buf, s);
1383 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001384 error_setg(errp, "dump: failed to sync dump_bitmap");
qiaonuohand0686c72014-02-18 14:11:33 +08001385 goto out;
1386 }
1387 }
1388
1389 /* number of dumpable pages that will be dumped later */
1390 s->num_dumpable = num_dumpable;
1391
1392out:
1393 g_free(dump_bitmap_buf);
qiaonuohand0686c72014-02-18 14:11:33 +08001394}
1395
qiaonuohan64cfba62014-02-18 14:11:34 +08001396static void prepare_data_cache(DataCache *data_cache, DumpState *s,
1397 off_t offset)
1398{
Stephen Brennan4d7dd4e2023-09-18 16:32:31 -07001399 data_cache->state = s;
qiaonuohan64cfba62014-02-18 14:11:34 +08001400 data_cache->data_size = 0;
Andrew Jones8161bef2016-01-11 20:56:20 +01001401 data_cache->buf_size = 4 * dump_bitmap_get_bufsize(s);
1402 data_cache->buf = g_malloc0(data_cache->buf_size);
qiaonuohan64cfba62014-02-18 14:11:34 +08001403 data_cache->offset = offset;
1404}
1405
1406static int write_cache(DataCache *dc, const void *buf, size_t size,
1407 bool flag_sync)
1408{
1409 /*
1410 * dc->buf_size should not be less than size, otherwise dc will never be
1411 * enough
1412 */
1413 assert(size <= dc->buf_size);
1414
1415 /*
1416 * if flag_sync is set, synchronize data in dc->buf into vmcore.
1417 * otherwise check if the space is enough for caching data in buf, if not,
Stephen Brennan4d7dd4e2023-09-18 16:32:31 -07001418 * write the data in dc->buf to dc->state->fd and reset dc->buf
qiaonuohan64cfba62014-02-18 14:11:34 +08001419 */
1420 if ((!flag_sync && dc->data_size + size > dc->buf_size) ||
1421 (flag_sync && dc->data_size > 0)) {
Stephen Brennan4d7dd4e2023-09-18 16:32:31 -07001422 if (write_buffer(dc->state, dc->offset, dc->buf, dc->data_size) < 0) {
qiaonuohan64cfba62014-02-18 14:11:34 +08001423 return -1;
1424 }
1425
1426 dc->offset += dc->data_size;
1427 dc->data_size = 0;
1428 }
1429
1430 if (!flag_sync) {
1431 memcpy(dc->buf + dc->data_size, buf, size);
1432 dc->data_size += size;
1433 }
1434
1435 return 0;
1436}
1437
1438static void free_data_cache(DataCache *data_cache)
1439{
1440 g_free(data_cache->buf);
1441}
1442
qiaonuohand12f57e2014-02-18 14:11:35 +08001443static size_t get_len_buf_out(size_t page_size, uint32_t flag_compress)
1444{
Laszlo Ersekb87ef352014-05-20 13:42:53 +02001445 switch (flag_compress) {
1446 case DUMP_DH_COMPRESSED_ZLIB:
1447 return compressBound(page_size);
qiaonuohand12f57e2014-02-18 14:11:35 +08001448
Laszlo Ersekb87ef352014-05-20 13:42:53 +02001449 case DUMP_DH_COMPRESSED_LZO:
1450 /*
1451 * LZO will expand incompressible data by a little amount. Please check
1452 * the following URL to see the expansion calculation:
1453 * http://www.oberhumer.com/opensource/lzo/lzofaq.php
1454 */
1455 return page_size + page_size / 16 + 64 + 3;
qiaonuohand12f57e2014-02-18 14:11:35 +08001456
1457#ifdef CONFIG_SNAPPY
Laszlo Ersekb87ef352014-05-20 13:42:53 +02001458 case DUMP_DH_COMPRESSED_SNAPPY:
1459 return snappy_max_compressed_length(page_size);
qiaonuohand12f57e2014-02-18 14:11:35 +08001460#endif
Laszlo Ersekb87ef352014-05-20 13:42:53 +02001461 }
1462 return 0;
qiaonuohand12f57e2014-02-18 14:11:35 +08001463}
1464
zhanghailiang4c7e2512014-10-09 14:13:11 +08001465static void write_dump_pages(DumpState *s, Error **errp)
qiaonuohand12f57e2014-02-18 14:11:35 +08001466{
1467 int ret = 0;
1468 DataCache page_desc, page_data;
1469 size_t len_buf_out, size_out;
1470#ifdef CONFIG_LZO
1471 lzo_bytep wrkmem = NULL;
1472#endif
1473 uint8_t *buf_out = NULL;
1474 off_t offset_desc, offset_data;
1475 PageDescriptor pd, pd_zero;
1476 uint8_t *buf;
qiaonuohand12f57e2014-02-18 14:11:35 +08001477 GuestPhysBlock *block_iter = NULL;
1478 uint64_t pfn_iter;
Marc-André Lureau94d78842022-09-05 16:06:21 +04001479 g_autofree uint8_t *page = NULL;
qiaonuohand12f57e2014-02-18 14:11:35 +08001480
1481 /* get offset of page_desc and page_data in dump file */
1482 offset_desc = s->offset_page;
1483 offset_data = offset_desc + sizeof(PageDescriptor) * s->num_dumpable;
1484
1485 prepare_data_cache(&page_desc, s, offset_desc);
1486 prepare_data_cache(&page_data, s, offset_data);
1487
1488 /* prepare buffer to store compressed data */
Andrew Jones8161bef2016-01-11 20:56:20 +01001489 len_buf_out = get_len_buf_out(s->dump_info.page_size, s->flag_compress);
Laszlo Ersekb87ef352014-05-20 13:42:53 +02001490 assert(len_buf_out != 0);
qiaonuohand12f57e2014-02-18 14:11:35 +08001491
1492#ifdef CONFIG_LZO
1493 wrkmem = g_malloc(LZO1X_1_MEM_COMPRESS);
1494#endif
1495
1496 buf_out = g_malloc(len_buf_out);
1497
1498 /*
1499 * init zero page's page_desc and page_data, because every zero page
1500 * uses the same page_data
1501 */
Andrew Jones8161bef2016-01-11 20:56:20 +01001502 pd_zero.size = cpu_to_dump32(s, s->dump_info.page_size);
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001503 pd_zero.flags = cpu_to_dump32(s, 0);
1504 pd_zero.offset = cpu_to_dump64(s, offset_data);
1505 pd_zero.page_flags = cpu_to_dump64(s, 0);
Andrew Jones8161bef2016-01-11 20:56:20 +01001506 buf = g_malloc0(s->dump_info.page_size);
1507 ret = write_cache(&page_data, buf, s->dump_info.page_size, false);
qiaonuohand12f57e2014-02-18 14:11:35 +08001508 g_free(buf);
1509 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001510 error_setg(errp, "dump: failed to write page data (zero page)");
qiaonuohand12f57e2014-02-18 14:11:35 +08001511 goto out;
1512 }
1513
Andrew Jones8161bef2016-01-11 20:56:20 +01001514 offset_data += s->dump_info.page_size;
Marc-André Lureau94d78842022-09-05 16:06:21 +04001515 page = g_malloc(s->dump_info.page_size);
qiaonuohand12f57e2014-02-18 14:11:35 +08001516
1517 /*
1518 * dump memory to vmcore page by page. zero page will all be resided in the
1519 * first page of page section
1520 */
Marc-André Lureau94d78842022-09-05 16:06:21 +04001521 for (buf = page; get_next_page(&block_iter, &pfn_iter, &buf, s); buf = page) {
qiaonuohand12f57e2014-02-18 14:11:35 +08001522 /* check zero page */
Juan Quintelaf13f22b2021-11-18 15:57:44 +01001523 if (buffer_is_zero(buf, s->dump_info.page_size)) {
qiaonuohand12f57e2014-02-18 14:11:35 +08001524 ret = write_cache(&page_desc, &pd_zero, sizeof(PageDescriptor),
1525 false);
1526 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001527 error_setg(errp, "dump: failed to write page desc");
qiaonuohand12f57e2014-02-18 14:11:35 +08001528 goto out;
1529 }
1530 } else {
1531 /*
1532 * not zero page, then:
1533 * 1. compress the page
1534 * 2. write the compressed page into the cache of page_data
1535 * 3. get page desc of the compressed page and write it into the
1536 * cache of page_desc
1537 *
1538 * only one compression format will be used here, for
1539 * s->flag_compress is set. But when compression fails to work,
1540 * we fall back to save in plaintext.
1541 */
1542 size_out = len_buf_out;
1543 if ((s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) &&
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001544 (compress2(buf_out, (uLongf *)&size_out, buf,
Andrew Jones8161bef2016-01-11 20:56:20 +01001545 s->dump_info.page_size, Z_BEST_SPEED) == Z_OK) &&
1546 (size_out < s->dump_info.page_size)) {
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001547 pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_ZLIB);
1548 pd.size = cpu_to_dump32(s, size_out);
qiaonuohand12f57e2014-02-18 14:11:35 +08001549
1550 ret = write_cache(&page_data, buf_out, size_out, false);
1551 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001552 error_setg(errp, "dump: failed to write page data");
qiaonuohand12f57e2014-02-18 14:11:35 +08001553 goto out;
1554 }
1555#ifdef CONFIG_LZO
1556 } else if ((s->flag_compress & DUMP_DH_COMPRESSED_LZO) &&
Andrew Jones8161bef2016-01-11 20:56:20 +01001557 (lzo1x_1_compress(buf, s->dump_info.page_size, buf_out,
qiaonuohand12f57e2014-02-18 14:11:35 +08001558 (lzo_uint *)&size_out, wrkmem) == LZO_E_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_LZO);
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#ifdef CONFIG_SNAPPY
1570 } else if ((s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) &&
Andrew Jones8161bef2016-01-11 20:56:20 +01001571 (snappy_compress((char *)buf, s->dump_info.page_size,
qiaonuohand12f57e2014-02-18 14:11:35 +08001572 (char *)buf_out, &size_out) == SNAPPY_OK) &&
Andrew Jones8161bef2016-01-11 20:56:20 +01001573 (size_out < s->dump_info.page_size)) {
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001574 pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_SNAPPY);
1575 pd.size = cpu_to_dump32(s, size_out);
qiaonuohand12f57e2014-02-18 14:11:35 +08001576
1577 ret = write_cache(&page_data, buf_out, size_out, false);
1578 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001579 error_setg(errp, "dump: failed to write page data");
qiaonuohand12f57e2014-02-18 14:11:35 +08001580 goto out;
1581 }
1582#endif
1583 } else {
1584 /*
1585 * fall back to save in plaintext, size_out should be
Andrew Jones8161bef2016-01-11 20:56:20 +01001586 * assigned the target's page size
qiaonuohand12f57e2014-02-18 14:11:35 +08001587 */
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001588 pd.flags = cpu_to_dump32(s, 0);
Andrew Jones8161bef2016-01-11 20:56:20 +01001589 size_out = s->dump_info.page_size;
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001590 pd.size = cpu_to_dump32(s, size_out);
qiaonuohand12f57e2014-02-18 14:11:35 +08001591
Andrew Jones8161bef2016-01-11 20:56:20 +01001592 ret = write_cache(&page_data, buf,
1593 s->dump_info.page_size, false);
qiaonuohand12f57e2014-02-18 14:11:35 +08001594 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001595 error_setg(errp, "dump: failed to write page data");
qiaonuohand12f57e2014-02-18 14:11:35 +08001596 goto out;
1597 }
1598 }
1599
1600 /* get and write page desc here */
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001601 pd.page_flags = cpu_to_dump64(s, 0);
1602 pd.offset = cpu_to_dump64(s, offset_data);
qiaonuohand12f57e2014-02-18 14:11:35 +08001603 offset_data += size_out;
1604
1605 ret = write_cache(&page_desc, &pd, sizeof(PageDescriptor), false);
1606 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001607 error_setg(errp, "dump: failed to write page desc");
qiaonuohand12f57e2014-02-18 14:11:35 +08001608 goto out;
1609 }
1610 }
Peter Xu2264c2c2016-02-18 13:16:53 +08001611 s->written_size += s->dump_info.page_size;
qiaonuohand12f57e2014-02-18 14:11:35 +08001612 }
1613
1614 ret = write_cache(&page_desc, NULL, 0, true);
1615 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001616 error_setg(errp, "dump: failed to sync cache for page_desc");
qiaonuohand12f57e2014-02-18 14:11:35 +08001617 goto out;
1618 }
1619 ret = write_cache(&page_data, NULL, 0, true);
1620 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001621 error_setg(errp, "dump: failed to sync cache for page_data");
qiaonuohand12f57e2014-02-18 14:11:35 +08001622 goto out;
1623 }
1624
1625out:
1626 free_data_cache(&page_desc);
1627 free_data_cache(&page_data);
1628
1629#ifdef CONFIG_LZO
1630 g_free(wrkmem);
1631#endif
1632
1633 g_free(buf_out);
qiaonuohand12f57e2014-02-18 14:11:35 +08001634}
1635
zhanghailiang4c7e2512014-10-09 14:13:11 +08001636static void create_kdump_vmcore(DumpState *s, Error **errp)
qiaonuohanb53ccc32014-02-18 14:11:36 +08001637{
Janosch Frank86a518b2022-03-30 12:35:55 +00001638 ERRP_GUARD();
qiaonuohanb53ccc32014-02-18 14:11:36 +08001639 int ret;
1640
1641 /*
1642 * the kdump-compressed format is:
1643 * File offset
1644 * +------------------------------------------+ 0x0
1645 * | main header (struct disk_dump_header) |
1646 * |------------------------------------------+ block 1
1647 * | sub header (struct kdump_sub_header) |
1648 * |------------------------------------------+ block 2
1649 * | 1st-dump_bitmap |
1650 * |------------------------------------------+ block 2 + X blocks
1651 * | 2nd-dump_bitmap | (aligned by block)
1652 * |------------------------------------------+ block 2 + 2 * X blocks
1653 * | page desc for pfn 0 (struct page_desc) | (aligned by block)
1654 * | page desc for pfn 1 (struct page_desc) |
1655 * | : |
1656 * |------------------------------------------| (not aligned by block)
1657 * | page data (pfn 0) |
1658 * | page data (pfn 1) |
1659 * | : |
1660 * +------------------------------------------+
1661 */
1662
Stephen Brennan4d7dd4e2023-09-18 16:32:31 -07001663 ret = write_start_flat_header(s);
qiaonuohanb53ccc32014-02-18 14:11:36 +08001664 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001665 error_setg(errp, "dump: failed to write start flat header");
zhanghailiang4c7e2512014-10-09 14:13:11 +08001666 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001667 }
1668
Janosch Frank86a518b2022-03-30 12:35:55 +00001669 write_dump_header(s, errp);
1670 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +08001671 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001672 }
1673
Janosch Frank86a518b2022-03-30 12:35:55 +00001674 write_dump_bitmap(s, errp);
1675 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +08001676 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001677 }
1678
Janosch Frank86a518b2022-03-30 12:35:55 +00001679 write_dump_pages(s, errp);
1680 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +08001681 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001682 }
1683
Stephen Brennan4d7dd4e2023-09-18 16:32:31 -07001684 ret = write_end_flat_header(s);
qiaonuohanb53ccc32014-02-18 14:11:36 +08001685 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001686 error_setg(errp, "dump: failed to write end flat header");
zhanghailiang4c7e2512014-10-09 14:13:11 +08001687 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001688 }
qiaonuohanb53ccc32014-02-18 14:11:36 +08001689}
1690
Janosch Frank0c2994a2022-08-11 12:10:57 +00001691static int validate_start_block(DumpState *s)
Wen Congyang783e9b42012-05-07 12:10:47 +08001692{
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001693 GuestPhysBlock *block;
Wen Congyang783e9b42012-05-07 12:10:47 +08001694
Janosch Frankdddf7252022-08-11 12:10:58 +00001695 if (!dump_has_filter(s)) {
Wen Congyang783e9b42012-05-07 12:10:47 +08001696 return 0;
1697 }
1698
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001699 QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
Janosch Frank0c2994a2022-08-11 12:10:57 +00001700 /* This block is out of the range */
Janosch Frankdddf7252022-08-11 12:10:58 +00001701 if (block->target_start >= s->filter_area_begin + s->filter_area_length ||
1702 block->target_end <= s->filter_area_begin) {
Wen Congyang783e9b42012-05-07 12:10:47 +08001703 continue;
1704 }
Janosch Frank0c2994a2022-08-11 12:10:57 +00001705 return 0;
1706 }
Wen Congyang783e9b42012-05-07 12:10:47 +08001707
1708 return -1;
1709}
1710
qiaonuohan7aad2482014-02-18 14:11:31 +08001711static void get_max_mapnr(DumpState *s)
1712{
1713 GuestPhysBlock *last_block;
1714
Paolo Bonzinieae3eb32018-12-06 13:10:34 +01001715 last_block = QTAILQ_LAST(&s->guest_phys_blocks.head);
Andrew Jones8161bef2016-01-11 20:56:20 +01001716 s->max_mapnr = dump_paddr_to_pfn(s, last_block->target_end);
qiaonuohan7aad2482014-02-18 14:11:31 +08001717}
1718
Peter Xubaf28f52016-02-18 13:16:48 +08001719static DumpState dump_state_global = { .status = DUMP_STATUS_NONE };
1720
1721static void dump_state_prepare(DumpState *s)
1722{
1723 /* zero the struct, setting status to active */
1724 *s = (DumpState) { .status = DUMP_STATUS_ACTIVE };
1725}
1726
Marc-André Lureau544803c2022-03-23 19:57:31 +04001727bool qemu_system_dump_in_progress(void)
Peter Xu65d64f32016-02-18 13:16:49 +08001728{
1729 DumpState *state = &dump_state_global;
Stefan Hajnoczid73415a2020-09-23 11:56:46 +01001730 return (qatomic_read(&state->status) == DUMP_STATUS_ACTIVE);
Peter Xu65d64f32016-02-18 13:16:49 +08001731}
1732
Janosch Frankc370d532022-08-11 12:10:59 +00001733/*
1734 * calculate total size of memory to be dumped (taking filter into
1735 * account.)
1736 */
Peter Xu2264c2c2016-02-18 13:16:53 +08001737static int64_t dump_calculate_size(DumpState *s)
1738{
1739 GuestPhysBlock *block;
Janosch Frankc370d532022-08-11 12:10:59 +00001740 int64_t total = 0;
Peter Xu2264c2c2016-02-18 13:16:53 +08001741
1742 QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
Janosch Frankc370d532022-08-11 12:10:59 +00001743 total += dump_filtered_memblock_size(block,
1744 s->filter_area_begin,
1745 s->filter_area_length);
Peter Xu2264c2c2016-02-18 13:16:53 +08001746 }
1747
1748 return total;
1749}
1750
Marc-André Lureaud9feb512017-09-11 18:59:26 +02001751static void vmcoreinfo_update_phys_base(DumpState *s)
1752{
1753 uint64_t size, note_head_size, name_size, phys_base;
1754 char **lines;
1755 uint8_t *vmci;
1756 size_t i;
1757
1758 if (!note_name_equal(s, s->guest_note, "VMCOREINFO")) {
1759 return;
1760 }
1761
1762 get_note_sizes(s, s->guest_note, &note_head_size, &name_size, &size);
1763 note_head_size = ROUND_UP(note_head_size, 4);
1764
1765 vmci = s->guest_note + note_head_size + ROUND_UP(name_size, 4);
1766 *(vmci + size) = '\0';
1767
1768 lines = g_strsplit((char *)vmci, "\n", -1);
1769 for (i = 0; lines[i]; i++) {
Wei Huang68cbecf2018-03-09 12:03:23 -05001770 const char *prefix = NULL;
1771
1772 if (s->dump_info.d_machine == EM_X86_64) {
1773 prefix = "NUMBER(phys_base)=";
1774 } else if (s->dump_info.d_machine == EM_AARCH64) {
1775 prefix = "NUMBER(PHYS_OFFSET)=";
1776 }
1777
1778 if (prefix && g_str_has_prefix(lines[i], prefix)) {
1779 if (qemu_strtou64(lines[i] + strlen(prefix), NULL, 16,
Marc-André Lureaud9feb512017-09-11 18:59:26 +02001780 &phys_base) < 0) {
Wei Huang68cbecf2018-03-09 12:03:23 -05001781 warn_report("Failed to read %s", prefix);
Marc-André Lureaud9feb512017-09-11 18:59:26 +02001782 } else {
1783 s->dump_info.phys_base = phys_base;
1784 }
1785 break;
1786 }
1787 }
1788
1789 g_strfreev(lines);
1790}
1791
zhanghailiang4c7e2512014-10-09 14:13:11 +08001792static void dump_init(DumpState *s, int fd, bool has_format,
1793 DumpGuestMemoryFormat format, bool paging, bool has_filter,
Stephen Brennand43a01d2023-09-18 16:32:32 -07001794 int64_t begin, int64_t length, bool kdump_raw,
1795 Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +08001796{
Janosch Frank86a518b2022-03-30 12:35:55 +00001797 ERRP_GUARD();
Marc-André Lureau903ef732017-09-11 18:59:25 +02001798 VMCoreInfoState *vmci = vmcoreinfo_find();
Andreas Färber182735e2013-05-29 22:29:20 +02001799 CPUState *cpu;
Wen Congyang783e9b42012-05-07 12:10:47 +08001800 int nr_cpus;
1801 int ret;
1802
Peter Xuca1fc8c2016-02-18 13:16:50 +08001803 s->has_format = has_format;
1804 s->format = format;
Peter Xu2264c2c2016-02-18 13:16:53 +08001805 s->written_size = 0;
Stephen Brennand43a01d2023-09-18 16:32:32 -07001806 s->kdump_raw = kdump_raw;
Peter Xuca1fc8c2016-02-18 13:16:50 +08001807
qiaonuohanb53ccc32014-02-18 14:11:36 +08001808 /* kdump-compressed is conflict with paging and filter */
1809 if (has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
1810 assert(!paging && !has_filter);
1811 }
1812
Wen Congyang783e9b42012-05-07 12:10:47 +08001813 if (runstate_is_running()) {
1814 vm_stop(RUN_STATE_SAVE_VM);
1815 s->resume = true;
1816 } else {
1817 s->resume = false;
1818 }
1819
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001820 /* If we use KVM, we should synchronize the registers before we get dump
1821 * info or physmap info.
Wen Congyang783e9b42012-05-07 12:10:47 +08001822 */
Andreas Färber1b3509c2013-06-09 16:48:29 +02001823 cpu_synchronize_all_states();
Wen Congyang783e9b42012-05-07 12:10:47 +08001824 nr_cpus = 0;
Andreas Färberbdc44642013-06-24 23:50:24 +02001825 CPU_FOREACH(cpu) {
Wen Congyang783e9b42012-05-07 12:10:47 +08001826 nr_cpus++;
1827 }
1828
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001829 s->fd = fd;
Janosch Frankdddf7252022-08-11 12:10:58 +00001830 if (has_filter && !length) {
1831 error_setg(errp, QERR_INVALID_PARAMETER, "length");
1832 goto cleanup;
1833 }
1834 s->filter_area_begin = begin;
1835 s->filter_area_length = length;
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001836
Janosch Frank9b722242022-10-17 11:32:10 +00001837 /* First index is 0, it's the special null name */
1838 s->string_table_buf = g_array_new(FALSE, TRUE, 1);
1839 /*
1840 * Allocate the null name, due to the clearing option set to true
1841 * it will be 0.
1842 */
1843 g_array_set_size(s->string_table_buf, 1);
1844
Chen Gang29282072014-08-03 23:28:56 +08001845 memory_mapping_list_init(&s->list);
1846
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001847 guest_phys_blocks_init(&s->guest_phys_blocks);
Laszlo Ersekc5d7f602013-08-06 12:37:10 +02001848 guest_phys_blocks_append(&s->guest_phys_blocks);
Peter Xu2264c2c2016-02-18 13:16:53 +08001849 s->total_size = dump_calculate_size(s);
1850#ifdef DEBUG_DUMP_GUEST_MEMORY
1851 fprintf(stderr, "DUMP: total memory to dump: %lu\n", s->total_size);
1852#endif
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001853
Cornelia Huckd1e69942017-09-13 16:20:35 +02001854 /* it does not make sense to dump non-existent memory */
1855 if (!s->total_size) {
1856 error_setg(errp, "dump: no guest memory to dump");
1857 goto cleanup;
1858 }
1859
Janosch Frank0c2994a2022-08-11 12:10:57 +00001860 /* Is the filter filtering everything? */
1861 if (validate_start_block(s) == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001862 error_setg(errp, QERR_INVALID_PARAMETER, "begin");
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001863 goto cleanup;
1864 }
1865
1866 /* get dump info: endian, class and architecture.
1867 * If the target architecture is not supported, cpu_get_dump_info() will
1868 * return -1.
1869 */
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001870 ret = cpu_get_dump_info(&s->dump_info, &s->guest_phys_blocks);
Wen Congyang783e9b42012-05-07 12:10:47 +08001871 if (ret < 0) {
Markus Armbrusterf969c622023-02-07 08:51:05 +01001872 error_setg(errp,
1873 "dumping guest memory is not supported on this target");
Wen Congyang783e9b42012-05-07 12:10:47 +08001874 goto cleanup;
1875 }
1876
Andrew Jones8161bef2016-01-11 20:56:20 +01001877 if (!s->dump_info.page_size) {
Philippe Mathieu-Daudéc5d40b22023-02-23 23:38:59 +01001878 s->dump_info.page_size = qemu_target_page_size();
Andrew Jones8161bef2016-01-11 20:56:20 +01001879 }
1880
Paolo Bonzini4720bd02012-06-07 08:48:09 +02001881 s->note_size = cpu_get_note_size(s->dump_info.d_class,
1882 s->dump_info.d_machine, nr_cpus);
Markus Armbrusterf1a46972023-02-07 08:51:06 +01001883 assert(s->note_size >= 0);
Paolo Bonzini4720bd02012-06-07 08:48:09 +02001884
Marc-André Lureau903ef732017-09-11 18:59:25 +02001885 /*
Marc-André Lureaud9feb512017-09-11 18:59:26 +02001886 * The goal of this block is to (a) update the previously guessed
1887 * phys_base, (b) copy the guest note out of the guest.
1888 * Failure to do so is not fatal for dumping.
Marc-André Lureau903ef732017-09-11 18:59:25 +02001889 */
1890 if (vmci) {
1891 uint64_t addr, note_head_size, name_size, desc_size;
1892 uint32_t size;
Thomas Huth71efffb2023-10-04 15:13:38 +02001893 uint16_t guest_format;
Marc-André Lureau903ef732017-09-11 18:59:25 +02001894
Janosch Frank05bbaa502022-03-30 12:36:00 +00001895 note_head_size = dump_is_64bit(s) ?
1896 sizeof(Elf64_Nhdr) : sizeof(Elf32_Nhdr);
Marc-André Lureau903ef732017-09-11 18:59:25 +02001897
Thomas Huth71efffb2023-10-04 15:13:38 +02001898 guest_format = le16_to_cpu(vmci->vmcoreinfo.guest_format);
Marc-André Lureau903ef732017-09-11 18:59:25 +02001899 size = le32_to_cpu(vmci->vmcoreinfo.size);
1900 addr = le64_to_cpu(vmci->vmcoreinfo.paddr);
1901 if (!vmci->has_vmcoreinfo) {
1902 warn_report("guest note is not present");
1903 } else if (size < note_head_size || size > MAX_GUEST_NOTE_SIZE) {
1904 warn_report("guest note size is invalid: %" PRIu32, size);
Thomas Huth71efffb2023-10-04 15:13:38 +02001905 } else if (guest_format != FW_CFG_VMCOREINFO_FORMAT_ELF) {
1906 warn_report("guest note format is unsupported: %" PRIu16, guest_format);
Marc-André Lureau903ef732017-09-11 18:59:25 +02001907 } else {
1908 s->guest_note = g_malloc(size + 1); /* +1 for adding \0 */
1909 cpu_physical_memory_read(addr, s->guest_note, size);
1910
1911 get_note_sizes(s, s->guest_note, NULL, &name_size, &desc_size);
1912 s->guest_note_size = ELF_NOTE_SIZE(note_head_size, name_size,
1913 desc_size);
1914 if (name_size > MAX_GUEST_NOTE_SIZE ||
1915 desc_size > MAX_GUEST_NOTE_SIZE ||
1916 s->guest_note_size > size) {
1917 warn_report("Invalid guest note header");
1918 g_free(s->guest_note);
1919 s->guest_note = NULL;
1920 } else {
Marc-André Lureaud9feb512017-09-11 18:59:26 +02001921 vmcoreinfo_update_phys_base(s);
Marc-André Lureau903ef732017-09-11 18:59:25 +02001922 s->note_size += s->guest_note_size;
1923 }
1924 }
1925 }
1926
Wen Congyang783e9b42012-05-07 12:10:47 +08001927 /* get memory mapping */
Wen Congyang783e9b42012-05-07 12:10:47 +08001928 if (paging) {
Janosch Frank86a518b2022-03-30 12:35:55 +00001929 qemu_get_guest_memory_mapping(&s->list, &s->guest_phys_blocks, errp);
1930 if (*errp) {
Andreas Färber11ed09c2013-05-29 21:54:03 +02001931 goto cleanup;
1932 }
Wen Congyang783e9b42012-05-07 12:10:47 +08001933 } else {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001934 qemu_get_guest_simple_memory_mapping(&s->list, &s->guest_phys_blocks);
Wen Congyang783e9b42012-05-07 12:10:47 +08001935 }
1936
qiaonuohan7aad2482014-02-18 14:11:31 +08001937 s->nr_cpus = nr_cpus;
qiaonuohan7aad2482014-02-18 14:11:31 +08001938
1939 get_max_mapnr(s);
1940
1941 uint64_t tmp;
Andrew Jones8161bef2016-01-11 20:56:20 +01001942 tmp = DIV_ROUND_UP(DIV_ROUND_UP(s->max_mapnr, CHAR_BIT),
1943 s->dump_info.page_size);
1944 s->len_dump_bitmap = tmp * s->dump_info.page_size;
qiaonuohan7aad2482014-02-18 14:11:31 +08001945
qiaonuohanb53ccc32014-02-18 14:11:36 +08001946 /* init for kdump-compressed format */
1947 if (has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
1948 switch (format) {
1949 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB:
1950 s->flag_compress = DUMP_DH_COMPRESSED_ZLIB;
1951 break;
1952
1953 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO:
Laszlo Ersekc998acb2014-05-20 13:42:22 +02001954#ifdef CONFIG_LZO
1955 if (lzo_init() != LZO_E_OK) {
1956 error_setg(errp, "failed to initialize the LZO library");
1957 goto cleanup;
1958 }
1959#endif
qiaonuohanb53ccc32014-02-18 14:11:36 +08001960 s->flag_compress = DUMP_DH_COMPRESSED_LZO;
1961 break;
1962
1963 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY:
1964 s->flag_compress = DUMP_DH_COMPRESSED_SNAPPY;
1965 break;
1966
1967 default:
1968 s->flag_compress = 0;
1969 }
1970
zhanghailiang4c7e2512014-10-09 14:13:11 +08001971 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001972 }
1973
Janosch Frankdddf7252022-08-11 12:10:58 +00001974 if (dump_has_filter(s)) {
1975 memory_mapping_filter(&s->list, s->filter_area_begin, s->filter_area_length);
Wen Congyang783e9b42012-05-07 12:10:47 +08001976 }
1977
1978 /*
Janosch Frank9b722242022-10-17 11:32:10 +00001979 * The first section header is always a special one in which most
1980 * fields are 0. The section header string table is also always
1981 * set.
Wen Congyang783e9b42012-05-07 12:10:47 +08001982 */
Janosch Frank9b722242022-10-17 11:32:10 +00001983 s->shdr_num = 2;
Wen Congyang783e9b42012-05-07 12:10:47 +08001984
Janosch Frank9b722242022-10-17 11:32:10 +00001985 /*
1986 * Adds the number of architecture sections to shdr_num and sets
1987 * elf_section_data_size so we know the offsets and sizes of all
1988 * parts.
1989 */
1990 if (s->dump_info.arch_sections_add_fn) {
1991 s->dump_info.arch_sections_add_fn(s);
Wen Congyang783e9b42012-05-07 12:10:47 +08001992 }
1993
Janosch Frank9b722242022-10-17 11:32:10 +00001994 /*
1995 * calculate shdr_num so we know the offsets and sizes of all
1996 * parts.
1997 * Calculate phdr_num
1998 *
1999 * The absolute maximum amount of phdrs is UINT32_MAX - 1 as
2000 * sh_info is 32 bit. There's special handling once we go over
2001 * UINT16_MAX - 1 but that is handled in the ehdr and section
2002 * code.
2003 */
2004 s->phdr_num = 1; /* Reserve PT_NOTE */
2005 if (s->list.num <= UINT32_MAX - 1) {
2006 s->phdr_num += s->list.num;
2007 } else {
2008 s->phdr_num = UINT32_MAX;
2009 }
2010
2011 /*
2012 * Now that the number of section and program headers is known we
2013 * can calculate the offsets of the headers and data.
2014 */
Janosch Frank05bbaa502022-03-30 12:36:00 +00002015 if (dump_is_64bit(s)) {
Janosch Frankcb415fd2022-10-17 08:38:14 +00002016 s->shdr_offset = sizeof(Elf64_Ehdr);
2017 s->phdr_offset = s->shdr_offset + sizeof(Elf64_Shdr) * s->shdr_num;
2018 s->note_offset = s->phdr_offset + sizeof(Elf64_Phdr) * s->phdr_num;
Wen Congyang783e9b42012-05-07 12:10:47 +08002019 } else {
Janosch Frankcb415fd2022-10-17 08:38:14 +00002020 s->shdr_offset = sizeof(Elf32_Ehdr);
2021 s->phdr_offset = s->shdr_offset + sizeof(Elf32_Shdr) * s->shdr_num;
2022 s->note_offset = s->phdr_offset + sizeof(Elf32_Phdr) * s->phdr_num;
Wen Congyang783e9b42012-05-07 12:10:47 +08002023 }
Janosch Frank13fd4172022-10-17 08:38:16 +00002024 s->memory_offset = s->note_offset + s->note_size;
2025 s->section_offset = s->memory_offset + s->total_size;
Wen Congyang783e9b42012-05-07 12:10:47 +08002026
zhanghailiang4c7e2512014-10-09 14:13:11 +08002027 return;
Wen Congyang783e9b42012-05-07 12:10:47 +08002028
2029cleanup:
Chen Gang29282072014-08-03 23:28:56 +08002030 dump_cleanup(s);
Wen Congyang783e9b42012-05-07 12:10:47 +08002031}
2032
Peter Xuca1fc8c2016-02-18 13:16:50 +08002033/* this operation might be time consuming. */
2034static void dump_process(DumpState *s, Error **errp)
2035{
Janosch Frank86a518b2022-03-30 12:35:55 +00002036 ERRP_GUARD();
Peter Xud42a0d12016-02-18 13:16:56 +08002037 DumpQueryResult *result = NULL;
Peter Xuca1fc8c2016-02-18 13:16:50 +08002038
Viktor Prutyanov2da91b52018-05-17 19:23:39 +03002039 if (s->has_format && s->format == DUMP_GUEST_MEMORY_FORMAT_WIN_DMP) {
Janosch Frank86a518b2022-03-30 12:35:55 +00002040 create_win_dump(s, errp);
Viktor Prutyanov2da91b52018-05-17 19:23:39 +03002041 } else if (s->has_format && s->format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
Janosch Frank86a518b2022-03-30 12:35:55 +00002042 create_kdump_vmcore(s, errp);
Peter Xuca1fc8c2016-02-18 13:16:50 +08002043 } else {
Janosch Frank86a518b2022-03-30 12:35:55 +00002044 create_vmcore(s, errp);
Peter Xuca1fc8c2016-02-18 13:16:50 +08002045 }
2046
Peter Xu39ba2ea2016-02-18 13:16:54 +08002047 /* make sure status is written after written_size updates */
2048 smp_wmb();
Stefan Hajnoczid73415a2020-09-23 11:56:46 +01002049 qatomic_set(&s->status,
Janosch Frank86a518b2022-03-30 12:35:55 +00002050 (*errp ? DUMP_STATUS_FAILED : DUMP_STATUS_COMPLETED));
Peter Xuca1fc8c2016-02-18 13:16:50 +08002051
Peter Xud42a0d12016-02-18 13:16:56 +08002052 /* send DUMP_COMPLETED message (unconditionally) */
2053 result = qmp_query_dump(NULL);
2054 /* should never fail */
2055 assert(result);
Markus Armbrusterd4f8bdc2022-11-04 17:06:55 +01002056 qapi_event_send_dump_completed(result,
2057 *errp ? error_get_pretty(*errp) : NULL);
Peter Xud42a0d12016-02-18 13:16:56 +08002058 qapi_free_DumpQueryResult(result);
2059
Peter Xuca1fc8c2016-02-18 13:16:50 +08002060 dump_cleanup(s);
2061}
2062
Peter Xu1fbeff72016-02-18 13:16:52 +08002063static void *dump_thread(void *data)
2064{
Peter Xu1fbeff72016-02-18 13:16:52 +08002065 DumpState *s = (DumpState *)data;
Alberto Garciac3a8fe32017-08-29 15:08:36 +03002066 dump_process(s, NULL);
Peter Xu1fbeff72016-02-18 13:16:52 +08002067 return NULL;
2068}
2069
Peter Xu39ba2ea2016-02-18 13:16:54 +08002070DumpQueryResult *qmp_query_dump(Error **errp)
2071{
2072 DumpQueryResult *result = g_new(DumpQueryResult, 1);
2073 DumpState *state = &dump_state_global;
Stefan Hajnoczid73415a2020-09-23 11:56:46 +01002074 result->status = qatomic_read(&state->status);
Peter Xu39ba2ea2016-02-18 13:16:54 +08002075 /* make sure we are reading status and written_size in order */
2076 smp_rmb();
2077 result->completed = state->written_size;
2078 result->total = state->total_size;
2079 return result;
2080}
2081
Markus Armbruster8beaeed2023-10-31 11:45:27 +01002082void qmp_dump_guest_memory(bool paging, const char *protocol,
Peter Xu228de9c2016-02-18 13:16:47 +08002083 bool has_detach, bool detach,
Markus Armbruster8beaeed2023-10-31 11:45:27 +01002084 bool has_begin, int64_t begin,
2085 bool has_length, int64_t length,
2086 bool has_format, DumpGuestMemoryFormat format,
2087 Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +08002088{
Janosch Frank86a518b2022-03-30 12:35:55 +00002089 ERRP_GUARD();
Wen Congyang783e9b42012-05-07 12:10:47 +08002090 const char *p;
2091 int fd = -1;
2092 DumpState *s;
Peter Xu1fbeff72016-02-18 13:16:52 +08002093 bool detach_p = false;
Stephen Brennane6549192023-09-18 16:32:33 -07002094 bool kdump_raw = false;
Wen Congyang783e9b42012-05-07 12:10:47 +08002095
Peter Xu63e27f22016-02-18 13:16:51 +08002096 if (runstate_check(RUN_STATE_INMIGRATE)) {
2097 error_setg(errp, "Dump not allowed during incoming migration.");
2098 return;
2099 }
2100
Peter Xu65d64f32016-02-18 13:16:49 +08002101 /* if there is a dump in background, we should wait until the dump
2102 * finished */
Marc-André Lureau544803c2022-03-23 19:57:31 +04002103 if (qemu_system_dump_in_progress()) {
Peter Xu65d64f32016-02-18 13:16:49 +08002104 error_setg(errp, "There is a dump in process, please wait.");
2105 return;
2106 }
2107
qiaonuohanb53ccc32014-02-18 14:11:36 +08002108 /*
Stephen Brennane6549192023-09-18 16:32:33 -07002109 * externally, we represent kdump-raw-* as separate formats, but internally
2110 * they are handled the same, except for the "raw" flag
2111 */
2112 if (has_format) {
2113 switch (format) {
2114 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_RAW_ZLIB:
2115 format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB;
2116 kdump_raw = true;
2117 break;
2118 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_RAW_LZO:
2119 format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO;
2120 kdump_raw = true;
2121 break;
2122 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_RAW_SNAPPY:
2123 format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY;
2124 kdump_raw = true;
2125 break;
2126 default:
2127 break;
2128 }
2129 }
2130
2131 /*
qiaonuohanb53ccc32014-02-18 14:11:36 +08002132 * kdump-compressed format need the whole memory dumped, so paging or
2133 * filter is not supported here.
2134 */
2135 if ((has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) &&
2136 (paging || has_begin || has_length)) {
2137 error_setg(errp, "kdump-compressed format doesn't support paging or "
2138 "filter");
2139 return;
2140 }
Wen Congyang783e9b42012-05-07 12:10:47 +08002141 if (has_begin && !has_length) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002142 error_setg(errp, QERR_MISSING_PARAMETER, "length");
Wen Congyang783e9b42012-05-07 12:10:47 +08002143 return;
2144 }
2145 if (!has_begin && has_length) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002146 error_setg(errp, QERR_MISSING_PARAMETER, "begin");
Wen Congyang783e9b42012-05-07 12:10:47 +08002147 return;
2148 }
Peter Xu1fbeff72016-02-18 13:16:52 +08002149 if (has_detach) {
2150 detach_p = detach;
2151 }
Wen Congyang783e9b42012-05-07 12:10:47 +08002152
qiaonuohanb53ccc32014-02-18 14:11:36 +08002153 /* check whether lzo/snappy is supported */
2154#ifndef CONFIG_LZO
2155 if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO) {
2156 error_setg(errp, "kdump-lzo is not available now");
2157 return;
2158 }
2159#endif
2160
2161#ifndef CONFIG_SNAPPY
2162 if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY) {
2163 error_setg(errp, "kdump-snappy is not available now");
2164 return;
2165 }
2166#endif
2167
Philippe Mathieu-Daudéefc31462023-02-23 23:58:16 +01002168 if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_WIN_DMP
2169 && !win_dump_available(errp)) {
Viktor Prutyanov2da91b52018-05-17 19:23:39 +03002170 return;
2171 }
Viktor Prutyanov2da91b52018-05-17 19:23:39 +03002172
Wen Congyang783e9b42012-05-07 12:10:47 +08002173#if !defined(WIN32)
Markus Armbruster8beaeed2023-10-31 11:45:27 +01002174 if (strstart(protocol, "fd:", &p)) {
Kevin Wolf947e4742020-10-05 17:58:44 +02002175 fd = monitor_get_fd(monitor_cur(), p, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +08002176 if (fd == -1) {
Wen Congyang783e9b42012-05-07 12:10:47 +08002177 return;
2178 }
2179 }
2180#endif
2181
Markus Armbruster8beaeed2023-10-31 11:45:27 +01002182 if (strstart(protocol, "file:", &p)) {
Daniel P. Berrangé448058a2020-07-21 13:25:21 +01002183 fd = qemu_open_old(p, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, S_IRUSR);
Wen Congyang783e9b42012-05-07 12:10:47 +08002184 if (fd < 0) {
Luiz Capitulino75817662013-06-07 14:36:01 -04002185 error_setg_file_open(errp, errno, p);
Wen Congyang783e9b42012-05-07 12:10:47 +08002186 return;
2187 }
2188 }
2189
2190 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002191 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
Wen Congyang783e9b42012-05-07 12:10:47 +08002192 return;
2193 }
Stephen Brennane6549192023-09-18 16:32:33 -07002194 if (kdump_raw && lseek(fd, 0, SEEK_CUR) == (off_t) -1) {
2195 error_setg(errp, "kdump-raw formats require a seekable file");
2196 return;
2197 }
Wen Congyang783e9b42012-05-07 12:10:47 +08002198
Peter Xub7bc6b12021-09-22 12:20:09 -04002199 if (!dump_migration_blocker) {
2200 error_setg(&dump_migration_blocker,
2201 "Live migration disabled: dump-guest-memory in progress");
2202 }
2203
2204 /*
2205 * Allows even for -only-migratable, but forbid migration during the
2206 * process of dump guest memory.
2207 */
Steve Sistarec8a7fc52023-10-18 06:03:36 -07002208 if (migrate_add_blocker_internal(&dump_migration_blocker, errp)) {
Peter Xub7bc6b12021-09-22 12:20:09 -04002209 /* Remember to release the fd before passing it over to dump state */
2210 close(fd);
2211 return;
2212 }
2213
Peter Xubaf28f52016-02-18 13:16:48 +08002214 s = &dump_state_global;
2215 dump_state_prepare(s);
Wen Congyang783e9b42012-05-07 12:10:47 +08002216
zhanghailiang4c7e2512014-10-09 14:13:11 +08002217 dump_init(s, fd, has_format, format, paging, has_begin,
Stephen Brennane6549192023-09-18 16:32:33 -07002218 begin, length, kdump_raw, errp);
Janosch Frank86a518b2022-03-30 12:35:55 +00002219 if (*errp) {
Stefan Hajnoczid73415a2020-09-23 11:56:46 +01002220 qatomic_set(&s->status, DUMP_STATUS_FAILED);
Wen Congyang783e9b42012-05-07 12:10:47 +08002221 return;
2222 }
2223
Peter Xu1fbeff72016-02-18 13:16:52 +08002224 if (detach_p) {
2225 /* detached dump */
Fam Zheng6796b402017-05-03 15:28:19 +08002226 s->detached = true;
Peter Xu1fbeff72016-02-18 13:16:52 +08002227 qemu_thread_create(&s->dump_thread, "dump_thread", dump_thread,
2228 s, QEMU_THREAD_DETACHED);
2229 } else {
2230 /* sync dump */
2231 dump_process(s, errp);
2232 }
Wen Congyang783e9b42012-05-07 12:10:47 +08002233}
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002234
2235DumpGuestMemoryCapability *qmp_query_dump_guest_memory_capability(Error **errp)
2236{
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002237 DumpGuestMemoryCapability *cap =
Markus Armbrusterb21e2382022-03-15 15:41:56 +01002238 g_new0(DumpGuestMemoryCapability, 1);
Eric Blake95b3a8c2021-01-13 16:10:13 -06002239 DumpGuestMemoryFormatList **tail = &cap->formats;
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002240
2241 /* elf is always available */
Eric Blake95b3a8c2021-01-13 16:10:13 -06002242 QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_ELF);
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002243
2244 /* kdump-zlib is always available */
Eric Blake95b3a8c2021-01-13 16:10:13 -06002245 QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB);
Stephen Brennane6549192023-09-18 16:32:33 -07002246 QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_KDUMP_RAW_ZLIB);
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002247
2248 /* add new item if kdump-lzo is available */
2249#ifdef CONFIG_LZO
Eric Blake95b3a8c2021-01-13 16:10:13 -06002250 QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO);
Stephen Brennane6549192023-09-18 16:32:33 -07002251 QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_KDUMP_RAW_LZO);
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002252#endif
2253
2254 /* add new item if kdump-snappy is available */
2255#ifdef CONFIG_SNAPPY
Eric Blake95b3a8c2021-01-13 16:10:13 -06002256 QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY);
Stephen Brennane6549192023-09-18 16:32:33 -07002257 QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_KDUMP_RAW_SNAPPY);
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002258#endif
2259
Philippe Mathieu-Daudéefc31462023-02-23 23:58:16 +01002260 if (win_dump_available(NULL)) {
2261 QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_WIN_DMP);
2262 }
Viktor Prutyanov2da91b52018-05-17 19:23:39 +03002263
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002264 return cap;
2265}