blob: 15bbcc0c6192822cf920fcb7d60eb7d2cfad0952 [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"
Philippe Mathieu-Daudé32cad1f2024-12-03 15:20:13 +010020#include "system/dump.h"
21#include "system/runstate.h"
22#include "system/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"
Yao Xingtao1aef4482024-07-22 00:07:41 -040033#include "qemu/range.h"
Viktor Prutyanov2da91b52018-05-17 19:23:39 +030034
qiaonuohand12f57e2014-02-18 14:11:35 +080035#include <zlib.h>
36#ifdef CONFIG_LZO
37#include <lzo/lzo1x.h>
38#endif
39#ifdef CONFIG_SNAPPY
40#include <snappy-c.h>
41#endif
qiaonuohan4ab23a92014-02-18 14:11:37 +080042#ifndef ELF_MACHINE_UNAME
43#define ELF_MACHINE_UNAME "Unknown"
44#endif
qiaonuohand12f57e2014-02-18 14:11:35 +080045
Marc-André Lureau903ef732017-09-11 18:59:25 +020046#define MAX_GUEST_NOTE_SIZE (1 << 20) /* 1MB should be enough */
47
Peter Xub7bc6b12021-09-22 12:20:09 -040048static Error *dump_migration_blocker;
49
Marc-André Lureau903ef732017-09-11 18:59:25 +020050#define ELF_NOTE_SIZE(hdr_size, name_size, desc_size) \
51 ((DIV_ROUND_UP((hdr_size), 4) + \
52 DIV_ROUND_UP((name_size), 4) + \
53 DIV_ROUND_UP((desc_size), 4)) * 4)
54
Janosch Frank05bbaa502022-03-30 12:36:00 +000055static inline bool dump_is_64bit(DumpState *s)
56{
57 return s->dump_info.d_class == ELFCLASS64;
58}
59
Janosch Frankdddf7252022-08-11 12:10:58 +000060static inline bool dump_has_filter(DumpState *s)
61{
62 return s->filter_area_length > 0;
63}
64
Bharata B Raoacb0ef52014-05-19 19:57:44 +020065uint16_t cpu_to_dump16(DumpState *s, uint16_t val)
Wen Congyang783e9b42012-05-07 12:10:47 +080066{
Bharata B Raoacb0ef52014-05-19 19:57:44 +020067 if (s->dump_info.d_endian == ELFDATA2LSB) {
Wen Congyang783e9b42012-05-07 12:10:47 +080068 val = cpu_to_le16(val);
69 } else {
70 val = cpu_to_be16(val);
71 }
72
73 return val;
74}
75
Bharata B Raoacb0ef52014-05-19 19:57:44 +020076uint32_t cpu_to_dump32(DumpState *s, uint32_t val)
Wen Congyang783e9b42012-05-07 12:10:47 +080077{
Bharata B Raoacb0ef52014-05-19 19:57:44 +020078 if (s->dump_info.d_endian == ELFDATA2LSB) {
Wen Congyang783e9b42012-05-07 12:10:47 +080079 val = cpu_to_le32(val);
80 } else {
81 val = cpu_to_be32(val);
82 }
83
84 return val;
85}
86
Bharata B Raoacb0ef52014-05-19 19:57:44 +020087uint64_t cpu_to_dump64(DumpState *s, uint64_t val)
Wen Congyang783e9b42012-05-07 12:10:47 +080088{
Bharata B Raoacb0ef52014-05-19 19:57:44 +020089 if (s->dump_info.d_endian == ELFDATA2LSB) {
Wen Congyang783e9b42012-05-07 12:10:47 +080090 val = cpu_to_le64(val);
91 } else {
92 val = cpu_to_be64(val);
93 }
94
95 return val;
96}
97
Wen Congyang783e9b42012-05-07 12:10:47 +080098static int dump_cleanup(DumpState *s)
99{
Janosch Franke72629e2023-11-09 12:04:42 +0000100 if (s->dump_info.arch_cleanup_fn) {
101 s->dump_info.arch_cleanup_fn(s);
102 }
103
Laszlo Ersek5ee163e2013-08-06 12:37:09 +0200104 guest_phys_blocks_free(&s->guest_phys_blocks);
Wen Congyang783e9b42012-05-07 12:10:47 +0800105 memory_mapping_list_free(&s->list);
Chen Gang29282072014-08-03 23:28:56 +0800106 close(s->fd);
Marc-André Lureau903ef732017-09-11 18:59:25 +0200107 g_free(s->guest_note);
Markus Armbruster96afbc52023-10-31 11:45:28 +0100108 g_clear_pointer(&s->string_table_buf, g_array_unref);
Marc-André Lureau903ef732017-09-11 18:59:25 +0200109 s->guest_note = NULL;
Wen Congyang783e9b42012-05-07 12:10:47 +0800110 if (s->resume) {
Fam Zheng6796b402017-05-03 15:28:19 +0800111 if (s->detached) {
Stefan Hajnoczi195801d2024-01-02 10:35:25 -0500112 bql_lock();
Fam Zheng6796b402017-05-03 15:28:19 +0800113 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800114 vm_start();
Fam Zheng6796b402017-05-03 15:28:19 +0800115 if (s->detached) {
Stefan Hajnoczi195801d2024-01-02 10:35:25 -0500116 bql_unlock();
Fam Zheng6796b402017-05-03 15:28:19 +0800117 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800118 }
Steve Sistarec8a7fc52023-10-18 06:03:36 -0700119 migrate_del_blocker(&dump_migration_blocker);
Wen Congyang783e9b42012-05-07 12:10:47 +0800120
Chen Gang29282072014-08-03 23:28:56 +0800121 return 0;
Wen Congyang783e9b42012-05-07 12:10:47 +0800122}
123
qiaonuohanb5ba1cc2014-02-18 14:11:25 +0800124static int fd_write_vmcore(const void *buf, size_t size, void *opaque)
Wen Congyang783e9b42012-05-07 12:10:47 +0800125{
126 DumpState *s = opaque;
Luiz Capitulino2f616522012-09-21 13:17:55 -0300127 size_t written_size;
Wen Congyang783e9b42012-05-07 12:10:47 +0800128
Luiz Capitulino2f616522012-09-21 13:17:55 -0300129 written_size = qemu_write_full(s->fd, buf, size);
130 if (written_size != size) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200131 return -errno;
Wen Congyang783e9b42012-05-07 12:10:47 +0800132 }
133
134 return 0;
135}
136
Janosch Frank670e7692022-08-11 12:11:00 +0000137static void prepare_elf64_header(DumpState *s, Elf64_Ehdr *elf_header)
Wen Congyang783e9b42012-05-07 12:10:47 +0800138{
Janosch Frank046bc412022-04-07 09:48:24 +0000139 /*
140 * phnum in the elf header is 16 bit, if we have more segments we
141 * set phnum to PN_XNUM and write the real number of segments to a
142 * special section.
143 */
144 uint16_t phnum = MIN(s->phdr_num, PN_XNUM);
Wen Congyang783e9b42012-05-07 12:10:47 +0800145
Janosch Frank670e7692022-08-11 12:11:00 +0000146 memset(elf_header, 0, sizeof(Elf64_Ehdr));
147 memcpy(elf_header, ELFMAG, SELFMAG);
148 elf_header->e_ident[EI_CLASS] = ELFCLASS64;
149 elf_header->e_ident[EI_DATA] = s->dump_info.d_endian;
150 elf_header->e_ident[EI_VERSION] = EV_CURRENT;
151 elf_header->e_type = cpu_to_dump16(s, ET_CORE);
152 elf_header->e_machine = cpu_to_dump16(s, s->dump_info.d_machine);
153 elf_header->e_version = cpu_to_dump32(s, EV_CURRENT);
154 elf_header->e_ehsize = cpu_to_dump16(s, sizeof(elf_header));
155 elf_header->e_phoff = cpu_to_dump64(s, s->phdr_offset);
156 elf_header->e_phentsize = cpu_to_dump16(s, sizeof(Elf64_Phdr));
157 elf_header->e_phnum = cpu_to_dump16(s, phnum);
Janosch Frank9b722242022-10-17 11:32:10 +0000158 elf_header->e_shoff = cpu_to_dump64(s, s->shdr_offset);
159 elf_header->e_shentsize = cpu_to_dump16(s, sizeof(Elf64_Shdr));
160 elf_header->e_shnum = cpu_to_dump16(s, s->shdr_num);
161 elf_header->e_shstrndx = cpu_to_dump16(s, s->shdr_num - 1);
Wen Congyang783e9b42012-05-07 12:10:47 +0800162}
163
Janosch Frank670e7692022-08-11 12:11:00 +0000164static void prepare_elf32_header(DumpState *s, Elf32_Ehdr *elf_header)
Wen Congyang783e9b42012-05-07 12:10:47 +0800165{
Janosch Frank046bc412022-04-07 09:48:24 +0000166 /*
167 * phnum in the elf header is 16 bit, if we have more segments we
168 * set phnum to PN_XNUM and write the real number of segments to a
169 * special section.
170 */
171 uint16_t phnum = MIN(s->phdr_num, PN_XNUM);
Janosch Frank670e7692022-08-11 12:11:00 +0000172
173 memset(elf_header, 0, sizeof(Elf32_Ehdr));
174 memcpy(elf_header, ELFMAG, SELFMAG);
175 elf_header->e_ident[EI_CLASS] = ELFCLASS32;
176 elf_header->e_ident[EI_DATA] = s->dump_info.d_endian;
177 elf_header->e_ident[EI_VERSION] = EV_CURRENT;
178 elf_header->e_type = cpu_to_dump16(s, ET_CORE);
179 elf_header->e_machine = cpu_to_dump16(s, s->dump_info.d_machine);
180 elf_header->e_version = cpu_to_dump32(s, EV_CURRENT);
181 elf_header->e_ehsize = cpu_to_dump16(s, sizeof(elf_header));
182 elf_header->e_phoff = cpu_to_dump32(s, s->phdr_offset);
183 elf_header->e_phentsize = cpu_to_dump16(s, sizeof(Elf32_Phdr));
184 elf_header->e_phnum = cpu_to_dump16(s, phnum);
Janosch Frank9b722242022-10-17 11:32:10 +0000185 elf_header->e_shoff = cpu_to_dump32(s, s->shdr_offset);
186 elf_header->e_shentsize = cpu_to_dump16(s, sizeof(Elf32_Shdr));
187 elf_header->e_shnum = cpu_to_dump16(s, s->shdr_num);
188 elf_header->e_shstrndx = cpu_to_dump16(s, s->shdr_num - 1);
Janosch Frank670e7692022-08-11 12:11:00 +0000189}
190
191static void write_elf_header(DumpState *s, Error **errp)
192{
193 Elf32_Ehdr elf32_header;
194 Elf64_Ehdr elf64_header;
195 size_t header_size;
196 void *header_ptr;
Wen Congyang783e9b42012-05-07 12:10:47 +0800197 int ret;
Wen Congyang783e9b42012-05-07 12:10:47 +0800198
Janosch Frank9b722242022-10-17 11:32:10 +0000199 /* The NULL header and the shstrtab are always defined */
200 assert(s->shdr_num >= 2);
Janosch Frank670e7692022-08-11 12:11:00 +0000201 if (dump_is_64bit(s)) {
202 prepare_elf64_header(s, &elf64_header);
203 header_size = sizeof(elf64_header);
204 header_ptr = &elf64_header;
205 } else {
206 prepare_elf32_header(s, &elf32_header);
207 header_size = sizeof(elf32_header);
208 header_ptr = &elf32_header;
Wen Congyang783e9b42012-05-07 12:10:47 +0800209 }
210
Janosch Frank670e7692022-08-11 12:11:00 +0000211 ret = fd_write_vmcore(header_ptr, header_size, s);
Wen Congyang783e9b42012-05-07 12:10:47 +0800212 if (ret < 0) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200213 error_setg_errno(errp, -ret, "dump: failed to write elf header");
Wen Congyang783e9b42012-05-07 12:10:47 +0800214 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800215}
216
zhanghailiang4c7e2512014-10-09 14:13:11 +0800217static void write_elf64_load(DumpState *s, MemoryMapping *memory_mapping,
218 int phdr_index, hwaddr offset,
219 hwaddr filesz, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800220{
221 Elf64_Phdr phdr;
222 int ret;
Wen Congyang783e9b42012-05-07 12:10:47 +0800223
224 memset(&phdr, 0, sizeof(Elf64_Phdr));
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200225 phdr.p_type = cpu_to_dump32(s, PT_LOAD);
226 phdr.p_offset = cpu_to_dump64(s, offset);
227 phdr.p_paddr = cpu_to_dump64(s, memory_mapping->phys_addr);
228 phdr.p_filesz = cpu_to_dump64(s, filesz);
229 phdr.p_memsz = cpu_to_dump64(s, memory_mapping->length);
Jon Dorone17bebd2019-01-09 10:22:03 +0200230 phdr.p_vaddr = cpu_to_dump64(s, memory_mapping->virt_addr) ?: phdr.p_paddr;
Wen Congyang783e9b42012-05-07 12:10:47 +0800231
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200232 assert(memory_mapping->length >= filesz);
233
Wen Congyang783e9b42012-05-07 12:10:47 +0800234 ret = fd_write_vmcore(&phdr, sizeof(Elf64_Phdr), s);
235 if (ret < 0) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200236 error_setg_errno(errp, -ret,
237 "dump: failed to write program header table");
Wen Congyang783e9b42012-05-07 12:10:47 +0800238 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800239}
240
zhanghailiang4c7e2512014-10-09 14:13:11 +0800241static void write_elf32_load(DumpState *s, MemoryMapping *memory_mapping,
242 int phdr_index, hwaddr offset,
243 hwaddr filesz, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800244{
245 Elf32_Phdr phdr;
246 int ret;
Wen Congyang783e9b42012-05-07 12:10:47 +0800247
248 memset(&phdr, 0, sizeof(Elf32_Phdr));
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200249 phdr.p_type = cpu_to_dump32(s, PT_LOAD);
250 phdr.p_offset = cpu_to_dump32(s, offset);
251 phdr.p_paddr = cpu_to_dump32(s, memory_mapping->phys_addr);
252 phdr.p_filesz = cpu_to_dump32(s, filesz);
253 phdr.p_memsz = cpu_to_dump32(s, memory_mapping->length);
Jon Dorone17bebd2019-01-09 10:22:03 +0200254 phdr.p_vaddr =
255 cpu_to_dump32(s, memory_mapping->virt_addr) ?: phdr.p_paddr;
Wen Congyang783e9b42012-05-07 12:10:47 +0800256
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200257 assert(memory_mapping->length >= filesz);
258
Wen Congyang783e9b42012-05-07 12:10:47 +0800259 ret = fd_write_vmcore(&phdr, sizeof(Elf32_Phdr), s);
260 if (ret < 0) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200261 error_setg_errno(errp, -ret,
262 "dump: failed to write program header table");
Wen Congyang783e9b42012-05-07 12:10:47 +0800263 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800264}
265
Janosch Frank2341a942022-08-11 12:11:01 +0000266static void prepare_elf64_phdr_note(DumpState *s, Elf64_Phdr *phdr)
Wen Congyang783e9b42012-05-07 12:10:47 +0800267{
Janosch Frankbc7d5582022-03-30 12:36:01 +0000268 memset(phdr, 0, sizeof(*phdr));
269 phdr->p_type = cpu_to_dump32(s, PT_NOTE);
270 phdr->p_offset = cpu_to_dump64(s, s->note_offset);
271 phdr->p_paddr = 0;
272 phdr->p_filesz = cpu_to_dump64(s, s->note_size);
273 phdr->p_memsz = cpu_to_dump64(s, s->note_size);
274 phdr->p_vaddr = 0;
Wen Congyang783e9b42012-05-07 12:10:47 +0800275}
276
Paolo Bonzini0bc3cd62013-04-08 17:29:59 +0200277static inline int cpu_index(CPUState *cpu)
278{
279 return cpu->cpu_index + 1;
280}
281
Marc-André Lureau903ef732017-09-11 18:59:25 +0200282static void write_guest_note(WriteCoreDumpFunction f, DumpState *s,
283 Error **errp)
284{
285 int ret;
286
287 if (s->guest_note) {
288 ret = f(s->guest_note, s->guest_note_size, s);
289 if (ret < 0) {
290 error_setg(errp, "dump: failed to write guest note");
291 }
292 }
293}
294
zhanghailiang4c7e2512014-10-09 14:13:11 +0800295static void write_elf64_notes(WriteCoreDumpFunction f, DumpState *s,
296 Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800297{
Andreas Färber0d342822012-12-17 07:12:13 +0100298 CPUState *cpu;
Wen Congyang783e9b42012-05-07 12:10:47 +0800299 int ret;
300 int id;
301
Andreas Färberbdc44642013-06-24 23:50:24 +0200302 CPU_FOREACH(cpu) {
Andreas Färber0d342822012-12-17 07:12:13 +0100303 id = cpu_index(cpu);
qiaonuohan6a519912014-02-18 14:11:26 +0800304 ret = cpu_write_elf64_note(f, cpu, id, s);
Wen Congyang783e9b42012-05-07 12:10:47 +0800305 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800306 error_setg(errp, "dump: failed to write elf notes");
zhanghailiang4c7e2512014-10-09 14:13:11 +0800307 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800308 }
309 }
310
Andreas Färberbdc44642013-06-24 23:50:24 +0200311 CPU_FOREACH(cpu) {
qiaonuohan6a519912014-02-18 14:11:26 +0800312 ret = cpu_write_elf64_qemunote(f, cpu, s);
Wen Congyang783e9b42012-05-07 12:10:47 +0800313 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800314 error_setg(errp, "dump: failed to write CPU status");
zhanghailiang4c7e2512014-10-09 14:13:11 +0800315 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800316 }
317 }
Marc-André Lureau903ef732017-09-11 18:59:25 +0200318
319 write_guest_note(f, s, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800320}
321
Janosch Frank2341a942022-08-11 12:11:01 +0000322static void prepare_elf32_phdr_note(DumpState *s, Elf32_Phdr *phdr)
Wen Congyang783e9b42012-05-07 12:10:47 +0800323{
Janosch Frankbc7d5582022-03-30 12:36:01 +0000324 memset(phdr, 0, sizeof(*phdr));
325 phdr->p_type = cpu_to_dump32(s, PT_NOTE);
326 phdr->p_offset = cpu_to_dump32(s, s->note_offset);
327 phdr->p_paddr = 0;
328 phdr->p_filesz = cpu_to_dump32(s, s->note_size);
329 phdr->p_memsz = cpu_to_dump32(s, s->note_size);
330 phdr->p_vaddr = 0;
Wen Congyang783e9b42012-05-07 12:10:47 +0800331}
332
zhanghailiang4c7e2512014-10-09 14:13:11 +0800333static void write_elf32_notes(WriteCoreDumpFunction f, DumpState *s,
334 Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800335{
Andreas Färber0d342822012-12-17 07:12:13 +0100336 CPUState *cpu;
Wen Congyang783e9b42012-05-07 12:10:47 +0800337 int ret;
338 int id;
339
Andreas Färberbdc44642013-06-24 23:50:24 +0200340 CPU_FOREACH(cpu) {
Andreas Färber0d342822012-12-17 07:12:13 +0100341 id = cpu_index(cpu);
qiaonuohan6a519912014-02-18 14:11:26 +0800342 ret = cpu_write_elf32_note(f, cpu, id, s);
Wen Congyang783e9b42012-05-07 12:10:47 +0800343 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800344 error_setg(errp, "dump: failed to write elf notes");
zhanghailiang4c7e2512014-10-09 14:13:11 +0800345 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800346 }
347 }
348
Andreas Färberbdc44642013-06-24 23:50:24 +0200349 CPU_FOREACH(cpu) {
qiaonuohan6a519912014-02-18 14:11:26 +0800350 ret = cpu_write_elf32_qemunote(f, cpu, s);
Wen Congyang783e9b42012-05-07 12:10:47 +0800351 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800352 error_setg(errp, "dump: failed to write CPU status");
zhanghailiang4c7e2512014-10-09 14:13:11 +0800353 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800354 }
355 }
Marc-André Lureau903ef732017-09-11 18:59:25 +0200356
357 write_guest_note(f, s, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800358}
359
Janosch Frankbc7d5582022-03-30 12:36:01 +0000360static void write_elf_phdr_note(DumpState *s, Error **errp)
361{
Janosch Frankbc7d5582022-03-30 12:36:01 +0000362 Elf32_Phdr phdr32;
363 Elf64_Phdr phdr64;
364 void *phdr;
365 size_t size;
366 int ret;
367
368 if (dump_is_64bit(s)) {
Janosch Frank2341a942022-08-11 12:11:01 +0000369 prepare_elf64_phdr_note(s, &phdr64);
Janosch Frankbc7d5582022-03-30 12:36:01 +0000370 size = sizeof(phdr64);
371 phdr = &phdr64;
372 } else {
Janosch Frank2341a942022-08-11 12:11:01 +0000373 prepare_elf32_phdr_note(s, &phdr32);
Janosch Frankbc7d5582022-03-30 12:36:01 +0000374 size = sizeof(phdr32);
375 phdr = &phdr32;
376 }
377
378 ret = fd_write_vmcore(phdr, size, s);
379 if (ret < 0) {
380 error_setg_errno(errp, -ret,
381 "dump: failed to write program header table");
382 }
383}
384
Janosch Franke41ed292022-10-17 08:38:13 +0000385static void prepare_elf_section_hdr_zero(DumpState *s)
Wen Congyang783e9b42012-05-07 12:10:47 +0800386{
Janosch Franke41ed292022-10-17 08:38:13 +0000387 if (dump_is_64bit(s)) {
388 Elf64_Shdr *shdr64 = s->elf_section_hdrs;
389
390 shdr64->sh_info = cpu_to_dump32(s, s->phdr_num);
391 } else {
392 Elf32_Shdr *shdr32 = s->elf_section_hdrs;
393
394 shdr32->sh_info = cpu_to_dump32(s, s->phdr_num);
395 }
396}
397
Janosch Frank9b722242022-10-17 11:32:10 +0000398static void prepare_elf_section_hdr_string(DumpState *s, void *buff)
399{
400 uint64_t index = s->string_table_buf->len;
401 const char strtab[] = ".shstrtab";
402 Elf32_Shdr shdr32 = {};
403 Elf64_Shdr shdr64 = {};
404 int shdr_size;
405 void *shdr;
406
407 g_array_append_vals(s->string_table_buf, strtab, sizeof(strtab));
408 if (dump_is_64bit(s)) {
409 shdr_size = sizeof(Elf64_Shdr);
410 shdr64.sh_type = SHT_STRTAB;
411 shdr64.sh_offset = s->section_offset + s->elf_section_data_size;
412 shdr64.sh_name = index;
413 shdr64.sh_size = s->string_table_buf->len;
414 shdr = &shdr64;
415 } else {
416 shdr_size = sizeof(Elf32_Shdr);
417 shdr32.sh_type = SHT_STRTAB;
418 shdr32.sh_offset = s->section_offset + s->elf_section_data_size;
419 shdr32.sh_name = index;
420 shdr32.sh_size = s->string_table_buf->len;
421 shdr = &shdr32;
422 }
423 memcpy(buff, shdr, shdr_size);
424}
425
426static bool prepare_elf_section_hdrs(DumpState *s, Error **errp)
Janosch Franke41ed292022-10-17 08:38:13 +0000427{
428 size_t len, sizeof_shdr;
Janosch Frank9b722242022-10-17 11:32:10 +0000429 void *buff_hdr;
Janosch Franke41ed292022-10-17 08:38:13 +0000430
431 /*
432 * Section ordering:
433 * - HDR zero
Janosch Frank9b722242022-10-17 11:32:10 +0000434 * - Arch section hdrs
435 * - String table hdr
Janosch Franke41ed292022-10-17 08:38:13 +0000436 */
437 sizeof_shdr = dump_is_64bit(s) ? sizeof(Elf64_Shdr) : sizeof(Elf32_Shdr);
438 len = sizeof_shdr * s->shdr_num;
439 s->elf_section_hdrs = g_malloc0(len);
Janosch Frank9b722242022-10-17 11:32:10 +0000440 buff_hdr = s->elf_section_hdrs;
Janosch Franke41ed292022-10-17 08:38:13 +0000441
442 /*
443 * The first section header is ALWAYS a special initial section
444 * header.
445 *
446 * The header should be 0 with one exception being that if
447 * phdr_num is PN_XNUM then the sh_info field contains the real
448 * number of segment entries.
449 *
450 * As we zero allocate the buffer we will only need to modify
451 * sh_info for the PN_XNUM case.
452 */
453 if (s->phdr_num >= PN_XNUM) {
454 prepare_elf_section_hdr_zero(s);
455 }
Janosch Frank9b722242022-10-17 11:32:10 +0000456 buff_hdr += sizeof_shdr;
457
458 /* Add architecture defined section headers */
459 if (s->dump_info.arch_sections_write_hdr_fn
460 && s->shdr_num > 2) {
461 buff_hdr += s->dump_info.arch_sections_write_hdr_fn(s, buff_hdr);
462
463 if (s->shdr_num >= SHN_LORESERVE) {
464 error_setg_errno(errp, EINVAL,
465 "dump: too many architecture defined sections");
466 return false;
467 }
468 }
469
470 /*
471 * String table is the last section since strings are added via
472 * arch_sections_write_hdr().
473 */
474 prepare_elf_section_hdr_string(s, buff_hdr);
475 return true;
Janosch Franke41ed292022-10-17 08:38:13 +0000476}
477
478static void write_elf_section_headers(DumpState *s, Error **errp)
479{
480 size_t sizeof_shdr = dump_is_64bit(s) ? sizeof(Elf64_Shdr) : sizeof(Elf32_Shdr);
Wen Congyang783e9b42012-05-07 12:10:47 +0800481 int ret;
482
Janosch Frank9b722242022-10-17 11:32:10 +0000483 if (!prepare_elf_section_hdrs(s, errp)) {
484 return;
485 }
Janosch Franke41ed292022-10-17 08:38:13 +0000486
487 ret = fd_write_vmcore(s->elf_section_hdrs, s->shdr_num * sizeof_shdr, s);
488 if (ret < 0) {
489 error_setg_errno(errp, -ret, "dump: failed to write section headers");
Wen Congyang783e9b42012-05-07 12:10:47 +0800490 }
491
Janosch Franke41ed292022-10-17 08:38:13 +0000492 g_free(s->elf_section_hdrs);
Wen Congyang783e9b42012-05-07 12:10:47 +0800493}
494
Janosch Frank9b722242022-10-17 11:32:10 +0000495static void write_elf_sections(DumpState *s, Error **errp)
496{
497 int ret;
498
499 if (s->elf_section_data_size) {
500 /* Write architecture section data */
501 ret = fd_write_vmcore(s->elf_section_data,
502 s->elf_section_data_size, s);
503 if (ret < 0) {
504 error_setg_errno(errp, -ret,
505 "dump: failed to write architecture section data");
506 return;
507 }
508 }
509
510 /* Write string table */
511 ret = fd_write_vmcore(s->string_table_buf->data,
512 s->string_table_buf->len, s);
513 if (ret < 0) {
514 error_setg_errno(errp, -ret, "dump: failed to write string table data");
515 }
516}
517
zhanghailiang4c7e2512014-10-09 14:13:11 +0800518static void write_data(DumpState *s, void *buf, int length, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800519{
520 int ret;
521
522 ret = fd_write_vmcore(buf, length, s);
523 if (ret < 0) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200524 error_setg_errno(errp, -ret, "dump: failed to save memory");
Peter Xu2264c2c2016-02-18 13:16:53 +0800525 } else {
526 s->written_size += length;
Wen Congyang783e9b42012-05-07 12:10:47 +0800527 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800528}
529
zhanghailiang4c7e2512014-10-09 14:13:11 +0800530/* write the memory to vmcore. 1 page per I/O. */
531static void write_memory(DumpState *s, GuestPhysBlock *block, ram_addr_t start,
532 int64_t size, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800533{
Janosch Frank86a518b2022-03-30 12:35:55 +0000534 ERRP_GUARD();
Wen Congyang783e9b42012-05-07 12:10:47 +0800535 int64_t i;
Wen Congyang783e9b42012-05-07 12:10:47 +0800536
Andrew Jones8161bef2016-01-11 20:56:20 +0100537 for (i = 0; i < size / s->dump_info.page_size; i++) {
538 write_data(s, block->host_addr + start + i * s->dump_info.page_size,
Janosch Frank86a518b2022-03-30 12:35:55 +0000539 s->dump_info.page_size, errp);
540 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800541 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800542 }
543 }
544
Andrew Jones8161bef2016-01-11 20:56:20 +0100545 if ((size % s->dump_info.page_size) != 0) {
546 write_data(s, block->host_addr + start + i * s->dump_info.page_size,
Janosch Frank86a518b2022-03-30 12:35:55 +0000547 size % s->dump_info.page_size, errp);
548 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800549 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800550 }
551 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800552}
553
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200554/* get the memory's offset and size in the vmcore */
555static void get_offset_range(hwaddr phys_addr,
556 ram_addr_t mapping_length,
557 DumpState *s,
558 hwaddr *p_offset,
559 hwaddr *p_filesz)
Wen Congyang783e9b42012-05-07 12:10:47 +0800560{
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200561 GuestPhysBlock *block;
Avi Kivitya8170e52012-10-23 12:30:10 +0200562 hwaddr offset = s->memory_offset;
Wen Congyang783e9b42012-05-07 12:10:47 +0800563 int64_t size_in_block, start;
564
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200565 /* When the memory is not stored into vmcore, offset will be -1 */
566 *p_offset = -1;
567 *p_filesz = 0;
568
Janosch Frankdddf7252022-08-11 12:10:58 +0000569 if (dump_has_filter(s)) {
570 if (phys_addr < s->filter_area_begin ||
571 phys_addr >= s->filter_area_begin + s->filter_area_length) {
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200572 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800573 }
574 }
575
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200576 QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
Janosch Frankdddf7252022-08-11 12:10:58 +0000577 if (dump_has_filter(s)) {
Yao Xingtao1aef4482024-07-22 00:07:41 -0400578 if (!ranges_overlap(block->target_start,
579 block->target_end - block->target_start,
580 s->filter_area_begin,
581 s->filter_area_length)) {
Wen Congyang783e9b42012-05-07 12:10:47 +0800582 /* This block is out of the range */
583 continue;
584 }
585
Janosch Frankdddf7252022-08-11 12:10:58 +0000586 if (s->filter_area_begin <= block->target_start) {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200587 start = block->target_start;
Wen Congyang783e9b42012-05-07 12:10:47 +0800588 } else {
Janosch Frankdddf7252022-08-11 12:10:58 +0000589 start = s->filter_area_begin;
Wen Congyang783e9b42012-05-07 12:10:47 +0800590 }
591
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200592 size_in_block = block->target_end - start;
Janosch Frankdddf7252022-08-11 12:10:58 +0000593 if (s->filter_area_begin + s->filter_area_length < block->target_end) {
594 size_in_block -= block->target_end - (s->filter_area_begin + s->filter_area_length);
Wen Congyang783e9b42012-05-07 12:10:47 +0800595 }
596 } else {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200597 start = block->target_start;
598 size_in_block = block->target_end - block->target_start;
Wen Congyang783e9b42012-05-07 12:10:47 +0800599 }
600
601 if (phys_addr >= start && phys_addr < start + size_in_block) {
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200602 *p_offset = phys_addr - start + offset;
603
604 /* The offset range mapped from the vmcore file must not spill over
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200605 * the GuestPhysBlock, clamp it. The rest of the mapping will be
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200606 * zero-filled in memory at load time; see
607 * <http://refspecs.linuxbase.org/elf/gabi4+/ch5.pheader.html>.
608 */
609 *p_filesz = phys_addr + mapping_length <= start + size_in_block ?
610 mapping_length :
611 size_in_block - (phys_addr - start);
612 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800613 }
614
615 offset += size_in_block;
616 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800617}
618
Janosch Frankafae6052022-08-11 12:10:55 +0000619static void write_elf_phdr_loads(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800620{
Janosch Frank86a518b2022-03-30 12:35:55 +0000621 ERRP_GUARD();
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200622 hwaddr offset, filesz;
Wen Congyang783e9b42012-05-07 12:10:47 +0800623 MemoryMapping *memory_mapping;
624 uint32_t phdr_index = 1;
Wen Congyang783e9b42012-05-07 12:10:47 +0800625
626 QTAILQ_FOREACH(memory_mapping, &s->list.head, next) {
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200627 get_offset_range(memory_mapping->phys_addr,
628 memory_mapping->length,
629 s, &offset, &filesz);
Janosch Frank05bbaa502022-03-30 12:36:00 +0000630 if (dump_is_64bit(s)) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800631 write_elf64_load(s, memory_mapping, phdr_index++, offset,
Janosch Frank86a518b2022-03-30 12:35:55 +0000632 filesz, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800633 } else {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800634 write_elf32_load(s, memory_mapping, phdr_index++, offset,
Janosch Frank86a518b2022-03-30 12:35:55 +0000635 filesz, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800636 }
637
Janosch Frank86a518b2022-03-30 12:35:55 +0000638 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800639 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800640 }
641
Janosch Frank046bc412022-04-07 09:48:24 +0000642 if (phdr_index >= s->phdr_num) {
Wen Congyang783e9b42012-05-07 12:10:47 +0800643 break;
644 }
645 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800646}
647
Janosch Frankc6812472022-03-30 12:36:03 +0000648static void write_elf_notes(DumpState *s, Error **errp)
649{
650 if (dump_is_64bit(s)) {
651 write_elf64_notes(fd_write_vmcore, s, errp);
652 } else {
653 write_elf32_notes(fd_write_vmcore, s, errp);
654 }
655}
656
Wen Congyang783e9b42012-05-07 12:10:47 +0800657/* write elf header, PT_NOTE and elf note to vmcore. */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800658static void dump_begin(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800659{
Janosch Frank86a518b2022-03-30 12:35:55 +0000660 ERRP_GUARD();
Wen Congyang783e9b42012-05-07 12:10:47 +0800661
662 /*
663 * the vmcore's format is:
664 * --------------
665 * | elf header |
666 * --------------
Janosch Frankcb415fd2022-10-17 08:38:14 +0000667 * | sctn_hdr |
668 * --------------
Wen Congyang783e9b42012-05-07 12:10:47 +0800669 * | PT_NOTE |
670 * --------------
671 * | PT_LOAD |
672 * --------------
673 * | ...... |
674 * --------------
675 * | PT_LOAD |
676 * --------------
Wen Congyang783e9b42012-05-07 12:10:47 +0800677 * | elf note |
678 * --------------
679 * | memory |
680 * --------------
681 *
682 * we only know where the memory is saved after we write elf note into
683 * vmcore.
684 */
685
686 /* write elf header to vmcore */
Janosch Frank670e7692022-08-11 12:11:00 +0000687 write_elf_header(s, errp);
Janosch Frank86a518b2022-03-30 12:35:55 +0000688 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800689 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800690 }
691
Janosch Frankcb415fd2022-10-17 08:38:14 +0000692 /* write section headers to vmcore */
693 write_elf_section_headers(s, errp);
694 if (*errp) {
695 return;
696 }
697
Janosch Frankbc7d5582022-03-30 12:36:01 +0000698 /* write PT_NOTE to vmcore */
699 write_elf_phdr_note(s, errp);
700 if (*errp) {
701 return;
702 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800703
Janosch Frankafae6052022-08-11 12:10:55 +0000704 /* write all PT_LOADs to vmcore */
705 write_elf_phdr_loads(s, errp);
Janosch Frank5ff2e5a2022-03-30 12:36:02 +0000706 if (*errp) {
707 return;
708 }
709
Janosch Frankc6812472022-03-30 12:36:03 +0000710 /* write notes to vmcore */
711 write_elf_notes(s, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800712}
713
Janosch Frank113d8f42022-10-17 08:38:22 +0000714int64_t dump_filtered_memblock_size(GuestPhysBlock *block,
715 int64_t filter_area_start,
716 int64_t filter_area_length)
Wen Congyang783e9b42012-05-07 12:10:47 +0800717{
Janosch Frank1e811302022-08-11 12:10:56 +0000718 int64_t size, left, right;
Wen Congyang783e9b42012-05-07 12:10:47 +0800719
Janosch Frank1e811302022-08-11 12:10:56 +0000720 /* No filter, return full size */
721 if (!filter_area_length) {
722 return block->target_end - block->target_start;
Wen Congyang783e9b42012-05-07 12:10:47 +0800723 }
Janosch Frank1e811302022-08-11 12:10:56 +0000724
725 /* calculate the overlapped region. */
726 left = MAX(filter_area_start, block->target_start);
727 right = MIN(filter_area_start + filter_area_length, block->target_end);
728 size = right - left;
729 size = size > 0 ? size : 0;
730
731 return size;
732}
733
Janosch Frank113d8f42022-10-17 08:38:22 +0000734int64_t dump_filtered_memblock_start(GuestPhysBlock *block,
735 int64_t filter_area_start,
736 int64_t filter_area_length)
Janosch Frank1e811302022-08-11 12:10:56 +0000737{
738 if (filter_area_length) {
739 /* return -1 if the block is not within filter area */
Yao Xingtao1aef4482024-07-22 00:07:41 -0400740 if (!ranges_overlap(block->target_start,
741 block->target_end - block->target_start,
742 filter_area_start, filter_area_length)) {
Janosch Frank1e811302022-08-11 12:10:56 +0000743 return -1;
744 }
745
746 if (filter_area_start > block->target_start) {
747 return filter_area_start - block->target_start;
748 }
749 }
750
751 return 0;
Wen Congyang783e9b42012-05-07 12:10:47 +0800752}
753
754/* write all memory to vmcore */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800755static void dump_iterate(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800756{
Janosch Frank86a518b2022-03-30 12:35:55 +0000757 ERRP_GUARD();
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200758 GuestPhysBlock *block;
Janosch Frank1e811302022-08-11 12:10:56 +0000759 int64_t memblock_size, memblock_start;
Wen Congyang783e9b42012-05-07 12:10:47 +0800760
Janosch Frank1e811302022-08-11 12:10:56 +0000761 QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
Janosch Frankdddf7252022-08-11 12:10:58 +0000762 memblock_start = dump_filtered_memblock_start(block, s->filter_area_begin, s->filter_area_length);
Janosch Frank1e811302022-08-11 12:10:56 +0000763 if (memblock_start == -1) {
764 continue;
Wen Congyang783e9b42012-05-07 12:10:47 +0800765 }
Janosch Frank1e811302022-08-11 12:10:56 +0000766
Janosch Frankdddf7252022-08-11 12:10:58 +0000767 memblock_size = dump_filtered_memblock_size(block, s->filter_area_begin, s->filter_area_length);
Janosch Frank1e811302022-08-11 12:10:56 +0000768
769 /* Write the memory to file */
770 write_memory(s, block, memblock_start, memblock_size, errp);
Janosch Frank86a518b2022-03-30 12:35:55 +0000771 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800772 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800773 }
Janosch Frank1e811302022-08-11 12:10:56 +0000774 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800775}
776
Janosch Frank9b722242022-10-17 11:32:10 +0000777static void dump_end(DumpState *s, Error **errp)
778{
779 int rc;
Janosch Frank9b722242022-10-17 11:32:10 +0000780
781 if (s->elf_section_data_size) {
782 s->elf_section_data = g_malloc0(s->elf_section_data_size);
783 }
784
785 /* Adds the architecture defined section data to s->elf_section_data */
786 if (s->dump_info.arch_sections_write_fn &&
787 s->elf_section_data_size) {
788 rc = s->dump_info.arch_sections_write_fn(s, s->elf_section_data);
789 if (rc) {
790 error_setg_errno(errp, rc,
791 "dump: failed to get arch section data");
792 g_free(s->elf_section_data);
793 return;
794 }
795 }
796
797 /* write sections to vmcore */
798 write_elf_sections(s, errp);
799}
800
zhanghailiang4c7e2512014-10-09 14:13:11 +0800801static void create_vmcore(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800802{
Janosch Frank86a518b2022-03-30 12:35:55 +0000803 ERRP_GUARD();
Wen Congyang783e9b42012-05-07 12:10:47 +0800804
Janosch Frank86a518b2022-03-30 12:35:55 +0000805 dump_begin(s, errp);
806 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800807 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800808 }
809
Janosch Frank9b722242022-10-17 11:32:10 +0000810 /* Iterate over memory and dump it to file */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800811 dump_iterate(s, errp);
Janosch Frank9b722242022-10-17 11:32:10 +0000812 if (*errp) {
813 return;
814 }
815
816 /* Write the section data */
817 dump_end(s, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800818}
819
Stephen Brennan4d7dd4e2023-09-18 16:32:31 -0700820static int write_start_flat_header(DumpState *s)
qiaonuohanfda05382014-02-18 14:11:27 +0800821{
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200822 MakedumpfileHeader *mh;
qiaonuohanfda05382014-02-18 14:11:27 +0800823 int ret = 0;
824
Stephen Brennand43a01d2023-09-18 16:32:32 -0700825 if (s->kdump_raw) {
826 return 0;
827 }
828
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200829 QEMU_BUILD_BUG_ON(sizeof *mh > MAX_SIZE_MDF_HEADER);
830 mh = g_malloc0(MAX_SIZE_MDF_HEADER);
qiaonuohanfda05382014-02-18 14:11:27 +0800831
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200832 memcpy(mh->signature, MAKEDUMPFILE_SIGNATURE,
833 MIN(sizeof mh->signature, sizeof MAKEDUMPFILE_SIGNATURE));
qiaonuohanfda05382014-02-18 14:11:27 +0800834
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200835 mh->type = cpu_to_be64(TYPE_FLAT_HEADER);
836 mh->version = cpu_to_be64(VERSION_FLAT_HEADER);
qiaonuohanfda05382014-02-18 14:11:27 +0800837
838 size_t written_size;
Stephen Brennan4d7dd4e2023-09-18 16:32:31 -0700839 written_size = qemu_write_full(s->fd, mh, MAX_SIZE_MDF_HEADER);
qiaonuohanfda05382014-02-18 14:11:27 +0800840 if (written_size != MAX_SIZE_MDF_HEADER) {
841 ret = -1;
842 }
843
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200844 g_free(mh);
qiaonuohanfda05382014-02-18 14:11:27 +0800845 return ret;
846}
847
Stephen Brennan4d7dd4e2023-09-18 16:32:31 -0700848static int write_end_flat_header(DumpState *s)
qiaonuohanfda05382014-02-18 14:11:27 +0800849{
850 MakedumpfileDataHeader mdh;
851
Stephen Brennand43a01d2023-09-18 16:32:32 -0700852 if (s->kdump_raw) {
853 return 0;
854 }
855
qiaonuohanfda05382014-02-18 14:11:27 +0800856 mdh.offset = END_FLAG_FLAT_HEADER;
857 mdh.buf_size = END_FLAG_FLAT_HEADER;
858
859 size_t written_size;
Stephen Brennan4d7dd4e2023-09-18 16:32:31 -0700860 written_size = qemu_write_full(s->fd, &mdh, sizeof(mdh));
qiaonuohanfda05382014-02-18 14:11:27 +0800861 if (written_size != sizeof(mdh)) {
862 return -1;
863 }
864
865 return 0;
866}
867
Stephen Brennan4d7dd4e2023-09-18 16:32:31 -0700868static int write_buffer(DumpState *s, off_t offset, const void *buf, size_t size)
qiaonuohan5d31bab2014-02-18 14:11:28 +0800869{
870 size_t written_size;
871 MakedumpfileDataHeader mdh;
Stephen Brennand43a01d2023-09-18 16:32:32 -0700872 off_t seek_loc;
qiaonuohan5d31bab2014-02-18 14:11:28 +0800873
Stephen Brennand43a01d2023-09-18 16:32:32 -0700874 if (s->kdump_raw) {
875 seek_loc = lseek(s->fd, offset, SEEK_SET);
876 if (seek_loc == (off_t) -1) {
877 return -1;
878 }
879 } else {
880 mdh.offset = cpu_to_be64(offset);
881 mdh.buf_size = cpu_to_be64(size);
qiaonuohan5d31bab2014-02-18 14:11:28 +0800882
Stephen Brennand43a01d2023-09-18 16:32:32 -0700883 written_size = qemu_write_full(s->fd, &mdh, sizeof(mdh));
884 if (written_size != sizeof(mdh)) {
885 return -1;
886 }
qiaonuohan5d31bab2014-02-18 14:11:28 +0800887 }
888
Stephen Brennan4d7dd4e2023-09-18 16:32:31 -0700889 written_size = qemu_write_full(s->fd, buf, size);
qiaonuohan5d31bab2014-02-18 14:11:28 +0800890 if (written_size != size) {
891 return -1;
892 }
893
894 return 0;
895}
896
qiaonuohan4835ef72014-02-18 14:11:29 +0800897static int buf_write_note(const void *buf, size_t size, void *opaque)
898{
899 DumpState *s = opaque;
900
901 /* note_buf is not enough */
902 if (s->note_buf_offset + size > s->note_size) {
903 return -1;
904 }
905
906 memcpy(s->note_buf + s->note_buf_offset, buf, size);
907
908 s->note_buf_offset += size;
909
910 return 0;
911}
912
Marc-André Lureau903ef732017-09-11 18:59:25 +0200913/*
914 * This function retrieves various sizes from an elf header.
915 *
916 * @note has to be a valid ELF note. The return sizes are unmodified
917 * (not padded or rounded up to be multiple of 4).
918 */
919static void get_note_sizes(DumpState *s, const void *note,
920 uint64_t *note_head_size,
921 uint64_t *name_size,
922 uint64_t *desc_size)
923{
924 uint64_t note_head_sz;
925 uint64_t name_sz;
926 uint64_t desc_sz;
927
Janosch Frank05bbaa502022-03-30 12:36:00 +0000928 if (dump_is_64bit(s)) {
Marc-André Lureau903ef732017-09-11 18:59:25 +0200929 const Elf64_Nhdr *hdr = note;
930 note_head_sz = sizeof(Elf64_Nhdr);
Philippe Mathieu-Daudébb509d92022-12-16 08:28:32 +0100931 name_sz = cpu_to_dump64(s, hdr->n_namesz);
932 desc_sz = cpu_to_dump64(s, hdr->n_descsz);
Marc-André Lureau903ef732017-09-11 18:59:25 +0200933 } else {
934 const Elf32_Nhdr *hdr = note;
935 note_head_sz = sizeof(Elf32_Nhdr);
Philippe Mathieu-Daudébb509d92022-12-16 08:28:32 +0100936 name_sz = cpu_to_dump32(s, hdr->n_namesz);
937 desc_sz = cpu_to_dump32(s, hdr->n_descsz);
Marc-André Lureau903ef732017-09-11 18:59:25 +0200938 }
939
940 if (note_head_size) {
941 *note_head_size = note_head_sz;
942 }
943 if (name_size) {
944 *name_size = name_sz;
945 }
946 if (desc_size) {
947 *desc_size = desc_sz;
948 }
949}
950
Marc-André Lureaud9feb512017-09-11 18:59:26 +0200951static bool note_name_equal(DumpState *s,
952 const uint8_t *note, const char *name)
953{
954 int len = strlen(name) + 1;
955 uint64_t head_size, name_size;
956
957 get_note_sizes(s, note, &head_size, &name_size, NULL);
958 head_size = ROUND_UP(head_size, 4);
959
Marc-André Lureauc983ca82017-12-12 15:53:59 +0100960 return name_size == len && memcmp(note + head_size, name, len) == 0;
Marc-André Lureaud9feb512017-09-11 18:59:26 +0200961}
962
qiaonuohan298f1162014-02-18 14:11:32 +0800963/* write common header, sub header and elf note to vmcore */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800964static void create_header32(DumpState *s, Error **errp)
qiaonuohan298f1162014-02-18 14:11:32 +0800965{
Janosch Frank86a518b2022-03-30 12:35:55 +0000966 ERRP_GUARD();
qiaonuohan298f1162014-02-18 14:11:32 +0800967 DiskDumpHeader32 *dh = NULL;
968 KdumpSubHeader32 *kh = NULL;
969 size_t size;
qiaonuohan298f1162014-02-18 14:11:32 +0800970 uint32_t block_size;
971 uint32_t sub_hdr_size;
972 uint32_t bitmap_blocks;
973 uint32_t status = 0;
974 uint64_t offset_note;
975
976 /* write common header, the version of kdump-compressed format is 6th */
977 size = sizeof(DiskDumpHeader32);
978 dh = g_malloc0(size);
979
Eric Blake84c868f2018-03-27 15:21:51 -0500980 memcpy(dh->signature, KDUMP_SIGNATURE, SIG_LEN);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200981 dh->header_version = cpu_to_dump32(s, 6);
Andrew Jones8161bef2016-01-11 20:56:20 +0100982 block_size = s->dump_info.page_size;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200983 dh->block_size = cpu_to_dump32(s, block_size);
qiaonuohan298f1162014-02-18 14:11:32 +0800984 sub_hdr_size = sizeof(struct KdumpSubHeader32) + s->note_size;
985 sub_hdr_size = DIV_ROUND_UP(sub_hdr_size, block_size);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200986 dh->sub_hdr_size = cpu_to_dump32(s, sub_hdr_size);
qiaonuohan298f1162014-02-18 14:11:32 +0800987 /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200988 dh->max_mapnr = cpu_to_dump32(s, MIN(s->max_mapnr, UINT_MAX));
989 dh->nr_cpus = cpu_to_dump32(s, s->nr_cpus);
qiaonuohan298f1162014-02-18 14:11:32 +0800990 bitmap_blocks = DIV_ROUND_UP(s->len_dump_bitmap, block_size) * 2;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200991 dh->bitmap_blocks = cpu_to_dump32(s, bitmap_blocks);
qiaonuohan4ab23a92014-02-18 14:11:37 +0800992 strncpy(dh->utsname.machine, ELF_MACHINE_UNAME, sizeof(dh->utsname.machine));
qiaonuohan298f1162014-02-18 14:11:32 +0800993
994 if (s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) {
995 status |= DUMP_DH_COMPRESSED_ZLIB;
996 }
997#ifdef CONFIG_LZO
998 if (s->flag_compress & DUMP_DH_COMPRESSED_LZO) {
999 status |= DUMP_DH_COMPRESSED_LZO;
1000 }
1001#endif
1002#ifdef CONFIG_SNAPPY
1003 if (s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) {
1004 status |= DUMP_DH_COMPRESSED_SNAPPY;
1005 }
1006#endif
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001007 dh->status = cpu_to_dump32(s, status);
qiaonuohan298f1162014-02-18 14:11:32 +08001008
Stephen Brennan4d7dd4e2023-09-18 16:32:31 -07001009 if (write_buffer(s, 0, dh, size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001010 error_setg(errp, "dump: failed to write disk dump header");
qiaonuohan298f1162014-02-18 14:11:32 +08001011 goto out;
1012 }
1013
1014 /* write sub header */
1015 size = sizeof(KdumpSubHeader32);
1016 kh = g_malloc0(size);
1017
1018 /* 64bit max_mapnr_64 */
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001019 kh->max_mapnr_64 = cpu_to_dump64(s, s->max_mapnr);
Andrew Jonesb6e05aa2016-01-11 20:56:21 +01001020 kh->phys_base = cpu_to_dump32(s, s->dump_info.phys_base);
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001021 kh->dump_level = cpu_to_dump32(s, DUMP_LEVEL);
qiaonuohan298f1162014-02-18 14:11:32 +08001022
1023 offset_note = DISKDUMP_HEADER_BLOCKS * block_size + size;
Marc-André Lureau9ada5752017-09-11 18:59:27 +02001024 if (s->guest_note &&
1025 note_name_equal(s, s->guest_note, "VMCOREINFO")) {
1026 uint64_t hsize, name_size, size_vmcoreinfo_desc, offset_vmcoreinfo;
1027
1028 get_note_sizes(s, s->guest_note,
1029 &hsize, &name_size, &size_vmcoreinfo_desc);
1030 offset_vmcoreinfo = offset_note + s->note_size - s->guest_note_size +
1031 (DIV_ROUND_UP(hsize, 4) + DIV_ROUND_UP(name_size, 4)) * 4;
1032 kh->offset_vmcoreinfo = cpu_to_dump64(s, offset_vmcoreinfo);
1033 kh->size_vmcoreinfo = cpu_to_dump32(s, size_vmcoreinfo_desc);
1034 }
1035
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001036 kh->offset_note = cpu_to_dump64(s, offset_note);
1037 kh->note_size = cpu_to_dump32(s, s->note_size);
qiaonuohan298f1162014-02-18 14:11:32 +08001038
Stephen Brennan4d7dd4e2023-09-18 16:32:31 -07001039 if (write_buffer(s, DISKDUMP_HEADER_BLOCKS *
qiaonuohan298f1162014-02-18 14:11:32 +08001040 block_size, kh, size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001041 error_setg(errp, "dump: failed to write kdump sub header");
qiaonuohan298f1162014-02-18 14:11:32 +08001042 goto out;
1043 }
1044
1045 /* write note */
1046 s->note_buf = g_malloc0(s->note_size);
1047 s->note_buf_offset = 0;
1048
1049 /* use s->note_buf to store notes temporarily */
Janosch Frank86a518b2022-03-30 12:35:55 +00001050 write_elf32_notes(buf_write_note, s, errp);
1051 if (*errp) {
qiaonuohan298f1162014-02-18 14:11:32 +08001052 goto out;
1053 }
Stephen Brennan4d7dd4e2023-09-18 16:32:31 -07001054 if (write_buffer(s, offset_note, s->note_buf,
qiaonuohan298f1162014-02-18 14:11:32 +08001055 s->note_size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001056 error_setg(errp, "dump: failed to write notes");
qiaonuohan298f1162014-02-18 14:11:32 +08001057 goto out;
1058 }
1059
1060 /* get offset of dump_bitmap */
1061 s->offset_dump_bitmap = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size) *
1062 block_size;
1063
1064 /* get offset of page */
1065 s->offset_page = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size + bitmap_blocks) *
1066 block_size;
1067
1068out:
1069 g_free(dh);
1070 g_free(kh);
1071 g_free(s->note_buf);
qiaonuohan298f1162014-02-18 14:11:32 +08001072}
1073
1074/* write common header, sub header and elf note to vmcore */
zhanghailiang4c7e2512014-10-09 14:13:11 +08001075static void create_header64(DumpState *s, Error **errp)
qiaonuohan298f1162014-02-18 14:11:32 +08001076{
Janosch Frank86a518b2022-03-30 12:35:55 +00001077 ERRP_GUARD();
qiaonuohan298f1162014-02-18 14:11:32 +08001078 DiskDumpHeader64 *dh = NULL;
1079 KdumpSubHeader64 *kh = NULL;
1080 size_t size;
qiaonuohan298f1162014-02-18 14:11:32 +08001081 uint32_t block_size;
1082 uint32_t sub_hdr_size;
1083 uint32_t bitmap_blocks;
1084 uint32_t status = 0;
1085 uint64_t offset_note;
1086
1087 /* write common header, the version of kdump-compressed format is 6th */
1088 size = sizeof(DiskDumpHeader64);
1089 dh = g_malloc0(size);
1090
Eric Blake84c868f2018-03-27 15:21:51 -05001091 memcpy(dh->signature, KDUMP_SIGNATURE, SIG_LEN);
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001092 dh->header_version = cpu_to_dump32(s, 6);
Andrew Jones8161bef2016-01-11 20:56:20 +01001093 block_size = s->dump_info.page_size;
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001094 dh->block_size = cpu_to_dump32(s, block_size);
qiaonuohan298f1162014-02-18 14:11:32 +08001095 sub_hdr_size = sizeof(struct KdumpSubHeader64) + s->note_size;
1096 sub_hdr_size = DIV_ROUND_UP(sub_hdr_size, block_size);
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001097 dh->sub_hdr_size = cpu_to_dump32(s, sub_hdr_size);
qiaonuohan298f1162014-02-18 14:11:32 +08001098 /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001099 dh->max_mapnr = cpu_to_dump32(s, MIN(s->max_mapnr, UINT_MAX));
1100 dh->nr_cpus = cpu_to_dump32(s, s->nr_cpus);
qiaonuohan298f1162014-02-18 14:11:32 +08001101 bitmap_blocks = DIV_ROUND_UP(s->len_dump_bitmap, block_size) * 2;
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001102 dh->bitmap_blocks = cpu_to_dump32(s, bitmap_blocks);
qiaonuohan4ab23a92014-02-18 14:11:37 +08001103 strncpy(dh->utsname.machine, ELF_MACHINE_UNAME, sizeof(dh->utsname.machine));
qiaonuohan298f1162014-02-18 14:11:32 +08001104
1105 if (s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) {
1106 status |= DUMP_DH_COMPRESSED_ZLIB;
1107 }
1108#ifdef CONFIG_LZO
1109 if (s->flag_compress & DUMP_DH_COMPRESSED_LZO) {
1110 status |= DUMP_DH_COMPRESSED_LZO;
1111 }
1112#endif
1113#ifdef CONFIG_SNAPPY
1114 if (s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) {
1115 status |= DUMP_DH_COMPRESSED_SNAPPY;
1116 }
1117#endif
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001118 dh->status = cpu_to_dump32(s, status);
qiaonuohan298f1162014-02-18 14:11:32 +08001119
Stephen Brennan4d7dd4e2023-09-18 16:32:31 -07001120 if (write_buffer(s, 0, dh, size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001121 error_setg(errp, "dump: failed to write disk dump header");
qiaonuohan298f1162014-02-18 14:11:32 +08001122 goto out;
1123 }
1124
1125 /* write sub header */
1126 size = sizeof(KdumpSubHeader64);
1127 kh = g_malloc0(size);
1128
1129 /* 64bit max_mapnr_64 */
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001130 kh->max_mapnr_64 = cpu_to_dump64(s, s->max_mapnr);
Andrew Jonesb6e05aa2016-01-11 20:56:21 +01001131 kh->phys_base = cpu_to_dump64(s, s->dump_info.phys_base);
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001132 kh->dump_level = cpu_to_dump32(s, DUMP_LEVEL);
qiaonuohan298f1162014-02-18 14:11:32 +08001133
1134 offset_note = DISKDUMP_HEADER_BLOCKS * block_size + size;
Marc-André Lureau9ada5752017-09-11 18:59:27 +02001135 if (s->guest_note &&
1136 note_name_equal(s, s->guest_note, "VMCOREINFO")) {
1137 uint64_t hsize, name_size, size_vmcoreinfo_desc, offset_vmcoreinfo;
1138
1139 get_note_sizes(s, s->guest_note,
1140 &hsize, &name_size, &size_vmcoreinfo_desc);
1141 offset_vmcoreinfo = offset_note + s->note_size - s->guest_note_size +
1142 (DIV_ROUND_UP(hsize, 4) + DIV_ROUND_UP(name_size, 4)) * 4;
1143 kh->offset_vmcoreinfo = cpu_to_dump64(s, offset_vmcoreinfo);
1144 kh->size_vmcoreinfo = cpu_to_dump64(s, size_vmcoreinfo_desc);
1145 }
1146
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001147 kh->offset_note = cpu_to_dump64(s, offset_note);
1148 kh->note_size = cpu_to_dump64(s, s->note_size);
qiaonuohan298f1162014-02-18 14:11:32 +08001149
Stephen Brennan4d7dd4e2023-09-18 16:32:31 -07001150 if (write_buffer(s, DISKDUMP_HEADER_BLOCKS *
qiaonuohan298f1162014-02-18 14:11:32 +08001151 block_size, kh, size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001152 error_setg(errp, "dump: failed to write kdump sub header");
qiaonuohan298f1162014-02-18 14:11:32 +08001153 goto out;
1154 }
1155
1156 /* write note */
1157 s->note_buf = g_malloc0(s->note_size);
1158 s->note_buf_offset = 0;
1159
1160 /* use s->note_buf to store notes temporarily */
Janosch Frank86a518b2022-03-30 12:35:55 +00001161 write_elf64_notes(buf_write_note, s, errp);
1162 if (*errp) {
qiaonuohan298f1162014-02-18 14:11:32 +08001163 goto out;
1164 }
1165
Stephen Brennan4d7dd4e2023-09-18 16:32:31 -07001166 if (write_buffer(s, offset_note, s->note_buf,
qiaonuohan298f1162014-02-18 14:11:32 +08001167 s->note_size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001168 error_setg(errp, "dump: failed to write notes");
qiaonuohan298f1162014-02-18 14:11:32 +08001169 goto out;
1170 }
1171
1172 /* get offset of dump_bitmap */
1173 s->offset_dump_bitmap = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size) *
1174 block_size;
1175
1176 /* get offset of page */
1177 s->offset_page = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size + bitmap_blocks) *
1178 block_size;
1179
1180out:
1181 g_free(dh);
1182 g_free(kh);
1183 g_free(s->note_buf);
qiaonuohan298f1162014-02-18 14:11:32 +08001184}
1185
zhanghailiang4c7e2512014-10-09 14:13:11 +08001186static void write_dump_header(DumpState *s, Error **errp)
qiaonuohan298f1162014-02-18 14:11:32 +08001187{
Janosch Frank05bbaa502022-03-30 12:36:00 +00001188 if (dump_is_64bit(s)) {
Markus Armbruster992861f2020-07-07 18:06:04 +02001189 create_header64(s, errp);
Janosch Frank05bbaa502022-03-30 12:36:00 +00001190 } else {
1191 create_header32(s, errp);
zhanghailiang4c7e2512014-10-09 14:13:11 +08001192 }
qiaonuohan298f1162014-02-18 14:11:32 +08001193}
1194
Andrew Jones8161bef2016-01-11 20:56:20 +01001195static size_t dump_bitmap_get_bufsize(DumpState *s)
1196{
1197 return s->dump_info.page_size;
1198}
1199
qiaonuohand0686c72014-02-18 14:11:33 +08001200/*
1201 * set dump_bitmap sequencely. the bit before last_pfn is not allowed to be
1202 * rewritten, so if need to set the first bit, set last_pfn and pfn to 0.
1203 * set_dump_bitmap will always leave the recently set bit un-sync. And setting
1204 * (last bit + sizeof(buf) * 8) to 0 will do flushing the content in buf into
1205 * vmcore, ie. synchronizing un-sync bit into vmcore.
1206 */
1207static int set_dump_bitmap(uint64_t last_pfn, uint64_t pfn, bool value,
1208 uint8_t *buf, DumpState *s)
1209{
1210 off_t old_offset, new_offset;
1211 off_t offset_bitmap1, offset_bitmap2;
1212 uint32_t byte, bit;
Andrew Jones8161bef2016-01-11 20:56:20 +01001213 size_t bitmap_bufsize = dump_bitmap_get_bufsize(s);
1214 size_t bits_per_buf = bitmap_bufsize * CHAR_BIT;
qiaonuohand0686c72014-02-18 14:11:33 +08001215
1216 /* should not set the previous place */
1217 assert(last_pfn <= pfn);
1218
1219 /*
1220 * if the bit needed to be set is not cached in buf, flush the data in buf
1221 * to vmcore firstly.
1222 * making new_offset be bigger than old_offset can also sync remained data
1223 * into vmcore.
1224 */
Andrew Jones8161bef2016-01-11 20:56:20 +01001225 old_offset = bitmap_bufsize * (last_pfn / bits_per_buf);
1226 new_offset = bitmap_bufsize * (pfn / bits_per_buf);
qiaonuohand0686c72014-02-18 14:11:33 +08001227
1228 while (old_offset < new_offset) {
1229 /* calculate the offset and write dump_bitmap */
1230 offset_bitmap1 = s->offset_dump_bitmap + old_offset;
Stephen Brennan4d7dd4e2023-09-18 16:32:31 -07001231 if (write_buffer(s, offset_bitmap1, buf,
Andrew Jones8161bef2016-01-11 20:56:20 +01001232 bitmap_bufsize) < 0) {
qiaonuohand0686c72014-02-18 14:11:33 +08001233 return -1;
1234 }
1235
1236 /* dump level 1 is chosen, so 1st and 2nd bitmap are same */
1237 offset_bitmap2 = s->offset_dump_bitmap + s->len_dump_bitmap +
1238 old_offset;
Stephen Brennan4d7dd4e2023-09-18 16:32:31 -07001239 if (write_buffer(s, offset_bitmap2, buf,
Andrew Jones8161bef2016-01-11 20:56:20 +01001240 bitmap_bufsize) < 0) {
qiaonuohand0686c72014-02-18 14:11:33 +08001241 return -1;
1242 }
1243
Andrew Jones8161bef2016-01-11 20:56:20 +01001244 memset(buf, 0, bitmap_bufsize);
1245 old_offset += bitmap_bufsize;
qiaonuohand0686c72014-02-18 14:11:33 +08001246 }
1247
1248 /* get the exact place of the bit in the buf, and set it */
Andrew Jones8161bef2016-01-11 20:56:20 +01001249 byte = (pfn % bits_per_buf) / CHAR_BIT;
1250 bit = (pfn % bits_per_buf) % CHAR_BIT;
qiaonuohand0686c72014-02-18 14:11:33 +08001251 if (value) {
1252 buf[byte] |= 1u << bit;
1253 } else {
1254 buf[byte] &= ~(1u << bit);
1255 }
1256
1257 return 0;
1258}
1259
Andrew Jones8161bef2016-01-11 20:56:20 +01001260static uint64_t dump_paddr_to_pfn(DumpState *s, uint64_t addr)
1261{
1262 int target_page_shift = ctz32(s->dump_info.page_size);
1263
1264 return (addr >> target_page_shift) - ARCH_PFN_OFFSET;
1265}
1266
1267static uint64_t dump_pfn_to_paddr(DumpState *s, uint64_t pfn)
1268{
1269 int target_page_shift = ctz32(s->dump_info.page_size);
1270
1271 return (pfn + ARCH_PFN_OFFSET) << target_page_shift;
1272}
1273
qiaonuohand0686c72014-02-18 14:11:33 +08001274/*
Marc-André Lureau94d78842022-09-05 16:06:21 +04001275 * Return the page frame number and the page content in *bufptr. bufptr can be
1276 * NULL. If not NULL, *bufptr must contains a target page size of pre-allocated
1277 * memory. This is not necessarily the memory returned.
qiaonuohand0686c72014-02-18 14:11:33 +08001278 */
1279static bool get_next_page(GuestPhysBlock **blockptr, uint64_t *pfnptr,
1280 uint8_t **bufptr, DumpState *s)
1281{
1282 GuestPhysBlock *block = *blockptr;
Marc-André Lureau94d78842022-09-05 16:06:21 +04001283 uint32_t page_size = s->dump_info.page_size;
1284 uint8_t *buf = NULL, *hbuf;
1285 hwaddr addr;
qiaonuohand0686c72014-02-18 14:11:33 +08001286
1287 /* block == NULL means the start of the iteration */
1288 if (!block) {
1289 block = QTAILQ_FIRST(&s->guest_phys_blocks.head);
1290 *blockptr = block;
Marc-André Lureau08df3432022-08-25 12:40:12 +04001291 addr = block->target_start;
Marc-André Lureau94d78842022-09-05 16:06:21 +04001292 *pfnptr = dump_paddr_to_pfn(s, addr);
Marc-André Lureau08df3432022-08-25 12:40:12 +04001293 } else {
Marc-André Lureau94d78842022-09-05 16:06:21 +04001294 *pfnptr += 1;
1295 addr = dump_pfn_to_paddr(s, *pfnptr);
qiaonuohand0686c72014-02-18 14:11:33 +08001296 }
Marc-André Lureau08df3432022-08-25 12:40:12 +04001297 assert(block != NULL);
qiaonuohand0686c72014-02-18 14:11:33 +08001298
Marc-André Lureau94d78842022-09-05 16:06:21 +04001299 while (1) {
1300 if (addr >= block->target_start && addr < block->target_end) {
1301 size_t n = MIN(block->target_end - addr, page_size - addr % page_size);
1302 hbuf = block->host_addr + (addr - block->target_start);
1303 if (!buf) {
1304 if (n == page_size) {
1305 /* this is a whole target page, go for it */
1306 assert(addr % page_size == 0);
1307 buf = hbuf;
1308 break;
1309 } else if (bufptr) {
1310 assert(*bufptr);
1311 buf = *bufptr;
1312 memset(buf, 0, page_size);
1313 } else {
1314 return true;
1315 }
1316 }
1317
1318 memcpy(buf + addr % page_size, hbuf, n);
1319 addr += n;
Dongli Zhang8a646092023-07-12 22:58:19 -07001320 if (addr % page_size == 0 || addr >= block->target_end) {
1321 /* we filled up the page or the current block is finished */
Marc-André Lureau94d78842022-09-05 16:06:21 +04001322 break;
1323 }
1324 } else {
1325 /* the next page is in the next block */
1326 *blockptr = block = QTAILQ_NEXT(block, next);
1327 if (!block) {
1328 break;
1329 }
1330
1331 addr = block->target_start;
1332 /* are we still in the same page? */
1333 if (dump_paddr_to_pfn(s, addr) != *pfnptr) {
1334 if (buf) {
1335 /* no, but we already filled something earlier, return it */
1336 break;
1337 } else {
1338 /* else continue from there */
1339 *pfnptr = dump_paddr_to_pfn(s, addr);
1340 }
1341 }
qiaonuohand0686c72014-02-18 14:11:33 +08001342 }
qiaonuohand0686c72014-02-18 14:11:33 +08001343 }
1344
1345 if (bufptr) {
1346 *bufptr = buf;
1347 }
1348
Marc-André Lureau94d78842022-09-05 16:06:21 +04001349 return buf != NULL;
qiaonuohand0686c72014-02-18 14:11:33 +08001350}
1351
zhanghailiang4c7e2512014-10-09 14:13:11 +08001352static void write_dump_bitmap(DumpState *s, Error **errp)
qiaonuohand0686c72014-02-18 14:11:33 +08001353{
1354 int ret = 0;
1355 uint64_t last_pfn, pfn;
1356 void *dump_bitmap_buf;
1357 size_t num_dumpable;
1358 GuestPhysBlock *block_iter = NULL;
Andrew Jones8161bef2016-01-11 20:56:20 +01001359 size_t bitmap_bufsize = dump_bitmap_get_bufsize(s);
1360 size_t bits_per_buf = bitmap_bufsize * CHAR_BIT;
qiaonuohand0686c72014-02-18 14:11:33 +08001361
1362 /* dump_bitmap_buf is used to store dump_bitmap temporarily */
Andrew Jones8161bef2016-01-11 20:56:20 +01001363 dump_bitmap_buf = g_malloc0(bitmap_bufsize);
qiaonuohand0686c72014-02-18 14:11:33 +08001364
1365 num_dumpable = 0;
1366 last_pfn = 0;
1367
1368 /*
1369 * exam memory page by page, and set the bit in dump_bitmap corresponded
1370 * to the existing page.
1371 */
1372 while (get_next_page(&block_iter, &pfn, NULL, s)) {
1373 ret = set_dump_bitmap(last_pfn, pfn, true, dump_bitmap_buf, s);
1374 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001375 error_setg(errp, "dump: failed to set dump_bitmap");
qiaonuohand0686c72014-02-18 14:11:33 +08001376 goto out;
1377 }
1378
1379 last_pfn = pfn;
1380 num_dumpable++;
1381 }
1382
1383 /*
1384 * set_dump_bitmap will always leave the recently set bit un-sync. Here we
Andrew Jones8161bef2016-01-11 20:56:20 +01001385 * set the remaining bits from last_pfn to the end of the bitmap buffer to
1386 * 0. With those set, the un-sync bit will be synchronized into the vmcore.
qiaonuohand0686c72014-02-18 14:11:33 +08001387 */
1388 if (num_dumpable > 0) {
Andrew Jones8161bef2016-01-11 20:56:20 +01001389 ret = set_dump_bitmap(last_pfn, last_pfn + bits_per_buf, false,
qiaonuohand0686c72014-02-18 14:11:33 +08001390 dump_bitmap_buf, s);
1391 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001392 error_setg(errp, "dump: failed to sync dump_bitmap");
qiaonuohand0686c72014-02-18 14:11:33 +08001393 goto out;
1394 }
1395 }
1396
1397 /* number of dumpable pages that will be dumped later */
1398 s->num_dumpable = num_dumpable;
1399
1400out:
1401 g_free(dump_bitmap_buf);
qiaonuohand0686c72014-02-18 14:11:33 +08001402}
1403
qiaonuohan64cfba62014-02-18 14:11:34 +08001404static void prepare_data_cache(DataCache *data_cache, DumpState *s,
1405 off_t offset)
1406{
Stephen Brennan4d7dd4e2023-09-18 16:32:31 -07001407 data_cache->state = s;
qiaonuohan64cfba62014-02-18 14:11:34 +08001408 data_cache->data_size = 0;
Andrew Jones8161bef2016-01-11 20:56:20 +01001409 data_cache->buf_size = 4 * dump_bitmap_get_bufsize(s);
1410 data_cache->buf = g_malloc0(data_cache->buf_size);
qiaonuohan64cfba62014-02-18 14:11:34 +08001411 data_cache->offset = offset;
1412}
1413
1414static int write_cache(DataCache *dc, const void *buf, size_t size,
1415 bool flag_sync)
1416{
1417 /*
1418 * dc->buf_size should not be less than size, otherwise dc will never be
1419 * enough
1420 */
1421 assert(size <= dc->buf_size);
1422
1423 /*
1424 * if flag_sync is set, synchronize data in dc->buf into vmcore.
1425 * otherwise check if the space is enough for caching data in buf, if not,
Stephen Brennan4d7dd4e2023-09-18 16:32:31 -07001426 * write the data in dc->buf to dc->state->fd and reset dc->buf
qiaonuohan64cfba62014-02-18 14:11:34 +08001427 */
1428 if ((!flag_sync && dc->data_size + size > dc->buf_size) ||
1429 (flag_sync && dc->data_size > 0)) {
Stephen Brennan4d7dd4e2023-09-18 16:32:31 -07001430 if (write_buffer(dc->state, dc->offset, dc->buf, dc->data_size) < 0) {
qiaonuohan64cfba62014-02-18 14:11:34 +08001431 return -1;
1432 }
1433
1434 dc->offset += dc->data_size;
1435 dc->data_size = 0;
1436 }
1437
1438 if (!flag_sync) {
1439 memcpy(dc->buf + dc->data_size, buf, size);
1440 dc->data_size += size;
1441 }
1442
1443 return 0;
1444}
1445
1446static void free_data_cache(DataCache *data_cache)
1447{
1448 g_free(data_cache->buf);
1449}
1450
qiaonuohand12f57e2014-02-18 14:11:35 +08001451static size_t get_len_buf_out(size_t page_size, uint32_t flag_compress)
1452{
Laszlo Ersekb87ef352014-05-20 13:42:53 +02001453 switch (flag_compress) {
1454 case DUMP_DH_COMPRESSED_ZLIB:
1455 return compressBound(page_size);
qiaonuohand12f57e2014-02-18 14:11:35 +08001456
Laszlo Ersekb87ef352014-05-20 13:42:53 +02001457 case DUMP_DH_COMPRESSED_LZO:
1458 /*
1459 * LZO will expand incompressible data by a little amount. Please check
1460 * the following URL to see the expansion calculation:
1461 * http://www.oberhumer.com/opensource/lzo/lzofaq.php
1462 */
1463 return page_size + page_size / 16 + 64 + 3;
qiaonuohand12f57e2014-02-18 14:11:35 +08001464
1465#ifdef CONFIG_SNAPPY
Laszlo Ersekb87ef352014-05-20 13:42:53 +02001466 case DUMP_DH_COMPRESSED_SNAPPY:
1467 return snappy_max_compressed_length(page_size);
qiaonuohand12f57e2014-02-18 14:11:35 +08001468#endif
Laszlo Ersekb87ef352014-05-20 13:42:53 +02001469 }
1470 return 0;
qiaonuohand12f57e2014-02-18 14:11:35 +08001471}
1472
zhanghailiang4c7e2512014-10-09 14:13:11 +08001473static void write_dump_pages(DumpState *s, Error **errp)
qiaonuohand12f57e2014-02-18 14:11:35 +08001474{
1475 int ret = 0;
1476 DataCache page_desc, page_data;
1477 size_t len_buf_out, size_out;
1478#ifdef CONFIG_LZO
1479 lzo_bytep wrkmem = NULL;
1480#endif
1481 uint8_t *buf_out = NULL;
1482 off_t offset_desc, offset_data;
1483 PageDescriptor pd, pd_zero;
1484 uint8_t *buf;
qiaonuohand12f57e2014-02-18 14:11:35 +08001485 GuestPhysBlock *block_iter = NULL;
1486 uint64_t pfn_iter;
Marc-André Lureau94d78842022-09-05 16:06:21 +04001487 g_autofree uint8_t *page = NULL;
qiaonuohand12f57e2014-02-18 14:11:35 +08001488
1489 /* get offset of page_desc and page_data in dump file */
1490 offset_desc = s->offset_page;
1491 offset_data = offset_desc + sizeof(PageDescriptor) * s->num_dumpable;
1492
1493 prepare_data_cache(&page_desc, s, offset_desc);
1494 prepare_data_cache(&page_data, s, offset_data);
1495
1496 /* prepare buffer to store compressed data */
Andrew Jones8161bef2016-01-11 20:56:20 +01001497 len_buf_out = get_len_buf_out(s->dump_info.page_size, s->flag_compress);
Laszlo Ersekb87ef352014-05-20 13:42:53 +02001498 assert(len_buf_out != 0);
qiaonuohand12f57e2014-02-18 14:11:35 +08001499
1500#ifdef CONFIG_LZO
1501 wrkmem = g_malloc(LZO1X_1_MEM_COMPRESS);
1502#endif
1503
1504 buf_out = g_malloc(len_buf_out);
1505
1506 /*
1507 * init zero page's page_desc and page_data, because every zero page
1508 * uses the same page_data
1509 */
Andrew Jones8161bef2016-01-11 20:56:20 +01001510 pd_zero.size = cpu_to_dump32(s, s->dump_info.page_size);
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001511 pd_zero.flags = cpu_to_dump32(s, 0);
1512 pd_zero.offset = cpu_to_dump64(s, offset_data);
1513 pd_zero.page_flags = cpu_to_dump64(s, 0);
Andrew Jones8161bef2016-01-11 20:56:20 +01001514 buf = g_malloc0(s->dump_info.page_size);
1515 ret = write_cache(&page_data, buf, s->dump_info.page_size, false);
qiaonuohand12f57e2014-02-18 14:11:35 +08001516 g_free(buf);
1517 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001518 error_setg(errp, "dump: failed to write page data (zero page)");
qiaonuohand12f57e2014-02-18 14:11:35 +08001519 goto out;
1520 }
1521
Andrew Jones8161bef2016-01-11 20:56:20 +01001522 offset_data += s->dump_info.page_size;
Marc-André Lureau94d78842022-09-05 16:06:21 +04001523 page = g_malloc(s->dump_info.page_size);
qiaonuohand12f57e2014-02-18 14:11:35 +08001524
1525 /*
1526 * dump memory to vmcore page by page. zero page will all be resided in the
1527 * first page of page section
1528 */
Marc-André Lureau94d78842022-09-05 16:06:21 +04001529 for (buf = page; get_next_page(&block_iter, &pfn_iter, &buf, s); buf = page) {
qiaonuohand12f57e2014-02-18 14:11:35 +08001530 /* check zero page */
Juan Quintelaf13f22b2021-11-18 15:57:44 +01001531 if (buffer_is_zero(buf, s->dump_info.page_size)) {
qiaonuohand12f57e2014-02-18 14:11:35 +08001532 ret = write_cache(&page_desc, &pd_zero, sizeof(PageDescriptor),
1533 false);
1534 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001535 error_setg(errp, "dump: failed to write page desc");
qiaonuohand12f57e2014-02-18 14:11:35 +08001536 goto out;
1537 }
1538 } else {
1539 /*
1540 * not zero page, then:
1541 * 1. compress the page
1542 * 2. write the compressed page into the cache of page_data
1543 * 3. get page desc of the compressed page and write it into the
1544 * cache of page_desc
1545 *
1546 * only one compression format will be used here, for
1547 * s->flag_compress is set. But when compression fails to work,
1548 * we fall back to save in plaintext.
1549 */
1550 size_out = len_buf_out;
1551 if ((s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) &&
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001552 (compress2(buf_out, (uLongf *)&size_out, buf,
Andrew Jones8161bef2016-01-11 20:56:20 +01001553 s->dump_info.page_size, Z_BEST_SPEED) == Z_OK) &&
1554 (size_out < s->dump_info.page_size)) {
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001555 pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_ZLIB);
1556 pd.size = cpu_to_dump32(s, size_out);
qiaonuohand12f57e2014-02-18 14:11:35 +08001557
1558 ret = write_cache(&page_data, buf_out, size_out, false);
1559 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001560 error_setg(errp, "dump: failed to write page data");
qiaonuohand12f57e2014-02-18 14:11:35 +08001561 goto out;
1562 }
1563#ifdef CONFIG_LZO
1564 } else if ((s->flag_compress & DUMP_DH_COMPRESSED_LZO) &&
Andrew Jones8161bef2016-01-11 20:56:20 +01001565 (lzo1x_1_compress(buf, s->dump_info.page_size, buf_out,
qiaonuohand12f57e2014-02-18 14:11:35 +08001566 (lzo_uint *)&size_out, wrkmem) == LZO_E_OK) &&
Andrew Jones8161bef2016-01-11 20:56:20 +01001567 (size_out < s->dump_info.page_size)) {
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001568 pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_LZO);
1569 pd.size = cpu_to_dump32(s, size_out);
qiaonuohand12f57e2014-02-18 14:11:35 +08001570
1571 ret = write_cache(&page_data, buf_out, size_out, false);
1572 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001573 error_setg(errp, "dump: failed to write page data");
qiaonuohand12f57e2014-02-18 14:11:35 +08001574 goto out;
1575 }
1576#endif
1577#ifdef CONFIG_SNAPPY
1578 } else if ((s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) &&
Andrew Jones8161bef2016-01-11 20:56:20 +01001579 (snappy_compress((char *)buf, s->dump_info.page_size,
qiaonuohand12f57e2014-02-18 14:11:35 +08001580 (char *)buf_out, &size_out) == SNAPPY_OK) &&
Andrew Jones8161bef2016-01-11 20:56:20 +01001581 (size_out < s->dump_info.page_size)) {
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001582 pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_SNAPPY);
1583 pd.size = cpu_to_dump32(s, size_out);
qiaonuohand12f57e2014-02-18 14:11:35 +08001584
1585 ret = write_cache(&page_data, buf_out, size_out, false);
1586 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001587 error_setg(errp, "dump: failed to write page data");
qiaonuohand12f57e2014-02-18 14:11:35 +08001588 goto out;
1589 }
1590#endif
1591 } else {
1592 /*
1593 * fall back to save in plaintext, size_out should be
Andrew Jones8161bef2016-01-11 20:56:20 +01001594 * assigned the target's page size
qiaonuohand12f57e2014-02-18 14:11:35 +08001595 */
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001596 pd.flags = cpu_to_dump32(s, 0);
Andrew Jones8161bef2016-01-11 20:56:20 +01001597 size_out = s->dump_info.page_size;
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001598 pd.size = cpu_to_dump32(s, size_out);
qiaonuohand12f57e2014-02-18 14:11:35 +08001599
Andrew Jones8161bef2016-01-11 20:56:20 +01001600 ret = write_cache(&page_data, buf,
1601 s->dump_info.page_size, false);
qiaonuohand12f57e2014-02-18 14:11:35 +08001602 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001603 error_setg(errp, "dump: failed to write page data");
qiaonuohand12f57e2014-02-18 14:11:35 +08001604 goto out;
1605 }
1606 }
1607
1608 /* get and write page desc here */
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001609 pd.page_flags = cpu_to_dump64(s, 0);
1610 pd.offset = cpu_to_dump64(s, offset_data);
qiaonuohand12f57e2014-02-18 14:11:35 +08001611 offset_data += size_out;
1612
1613 ret = write_cache(&page_desc, &pd, sizeof(PageDescriptor), false);
1614 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001615 error_setg(errp, "dump: failed to write page desc");
qiaonuohand12f57e2014-02-18 14:11:35 +08001616 goto out;
1617 }
1618 }
Peter Xu2264c2c2016-02-18 13:16:53 +08001619 s->written_size += s->dump_info.page_size;
qiaonuohand12f57e2014-02-18 14:11:35 +08001620 }
1621
1622 ret = write_cache(&page_desc, NULL, 0, true);
1623 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001624 error_setg(errp, "dump: failed to sync cache for page_desc");
qiaonuohand12f57e2014-02-18 14:11:35 +08001625 goto out;
1626 }
1627 ret = write_cache(&page_data, NULL, 0, true);
1628 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001629 error_setg(errp, "dump: failed to sync cache for page_data");
qiaonuohand12f57e2014-02-18 14:11:35 +08001630 goto out;
1631 }
1632
1633out:
1634 free_data_cache(&page_desc);
1635 free_data_cache(&page_data);
1636
1637#ifdef CONFIG_LZO
1638 g_free(wrkmem);
1639#endif
1640
1641 g_free(buf_out);
qiaonuohand12f57e2014-02-18 14:11:35 +08001642}
1643
zhanghailiang4c7e2512014-10-09 14:13:11 +08001644static void create_kdump_vmcore(DumpState *s, Error **errp)
qiaonuohanb53ccc32014-02-18 14:11:36 +08001645{
Janosch Frank86a518b2022-03-30 12:35:55 +00001646 ERRP_GUARD();
qiaonuohanb53ccc32014-02-18 14:11:36 +08001647 int ret;
1648
1649 /*
1650 * the kdump-compressed format is:
1651 * File offset
1652 * +------------------------------------------+ 0x0
1653 * | main header (struct disk_dump_header) |
1654 * |------------------------------------------+ block 1
1655 * | sub header (struct kdump_sub_header) |
1656 * |------------------------------------------+ block 2
1657 * | 1st-dump_bitmap |
1658 * |------------------------------------------+ block 2 + X blocks
1659 * | 2nd-dump_bitmap | (aligned by block)
1660 * |------------------------------------------+ block 2 + 2 * X blocks
1661 * | page desc for pfn 0 (struct page_desc) | (aligned by block)
1662 * | page desc for pfn 1 (struct page_desc) |
1663 * | : |
1664 * |------------------------------------------| (not aligned by block)
1665 * | page data (pfn 0) |
1666 * | page data (pfn 1) |
1667 * | : |
1668 * +------------------------------------------+
1669 */
1670
Stephen Brennan4d7dd4e2023-09-18 16:32:31 -07001671 ret = write_start_flat_header(s);
qiaonuohanb53ccc32014-02-18 14:11:36 +08001672 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001673 error_setg(errp, "dump: failed to write start flat header");
zhanghailiang4c7e2512014-10-09 14:13:11 +08001674 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001675 }
1676
Janosch Frank86a518b2022-03-30 12:35:55 +00001677 write_dump_header(s, errp);
1678 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +08001679 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001680 }
1681
Janosch Frank86a518b2022-03-30 12:35:55 +00001682 write_dump_bitmap(s, errp);
1683 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +08001684 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001685 }
1686
Janosch Frank86a518b2022-03-30 12:35:55 +00001687 write_dump_pages(s, errp);
1688 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +08001689 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001690 }
1691
Stephen Brennan4d7dd4e2023-09-18 16:32:31 -07001692 ret = write_end_flat_header(s);
qiaonuohanb53ccc32014-02-18 14:11:36 +08001693 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001694 error_setg(errp, "dump: failed to write end flat header");
zhanghailiang4c7e2512014-10-09 14:13:11 +08001695 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001696 }
qiaonuohanb53ccc32014-02-18 14:11:36 +08001697}
1698
qiaonuohan7aad2482014-02-18 14:11:31 +08001699static void get_max_mapnr(DumpState *s)
1700{
1701 GuestPhysBlock *last_block;
1702
Paolo Bonzinieae3eb32018-12-06 13:10:34 +01001703 last_block = QTAILQ_LAST(&s->guest_phys_blocks.head);
Andrew Jones8161bef2016-01-11 20:56:20 +01001704 s->max_mapnr = dump_paddr_to_pfn(s, last_block->target_end);
qiaonuohan7aad2482014-02-18 14:11:31 +08001705}
1706
Peter Xubaf28f52016-02-18 13:16:48 +08001707static DumpState dump_state_global = { .status = DUMP_STATUS_NONE };
1708
1709static void dump_state_prepare(DumpState *s)
1710{
1711 /* zero the struct, setting status to active */
1712 *s = (DumpState) { .status = DUMP_STATUS_ACTIVE };
1713}
1714
Marc-André Lureau544803c2022-03-23 19:57:31 +04001715bool qemu_system_dump_in_progress(void)
Peter Xu65d64f32016-02-18 13:16:49 +08001716{
1717 DumpState *state = &dump_state_global;
Stefan Hajnoczid73415a2020-09-23 11:56:46 +01001718 return (qatomic_read(&state->status) == DUMP_STATUS_ACTIVE);
Peter Xu65d64f32016-02-18 13:16:49 +08001719}
1720
Janosch Frankc370d532022-08-11 12:10:59 +00001721/*
1722 * calculate total size of memory to be dumped (taking filter into
1723 * account.)
1724 */
Peter Xu2264c2c2016-02-18 13:16:53 +08001725static int64_t dump_calculate_size(DumpState *s)
1726{
1727 GuestPhysBlock *block;
Janosch Frankc370d532022-08-11 12:10:59 +00001728 int64_t total = 0;
Peter Xu2264c2c2016-02-18 13:16:53 +08001729
1730 QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
Janosch Frankc370d532022-08-11 12:10:59 +00001731 total += dump_filtered_memblock_size(block,
1732 s->filter_area_begin,
1733 s->filter_area_length);
Peter Xu2264c2c2016-02-18 13:16:53 +08001734 }
1735
1736 return total;
1737}
1738
Marc-André Lureaud9feb512017-09-11 18:59:26 +02001739static void vmcoreinfo_update_phys_base(DumpState *s)
1740{
1741 uint64_t size, note_head_size, name_size, phys_base;
1742 char **lines;
1743 uint8_t *vmci;
1744 size_t i;
1745
1746 if (!note_name_equal(s, s->guest_note, "VMCOREINFO")) {
1747 return;
1748 }
1749
1750 get_note_sizes(s, s->guest_note, &note_head_size, &name_size, &size);
1751 note_head_size = ROUND_UP(note_head_size, 4);
1752
1753 vmci = s->guest_note + note_head_size + ROUND_UP(name_size, 4);
1754 *(vmci + size) = '\0';
1755
1756 lines = g_strsplit((char *)vmci, "\n", -1);
1757 for (i = 0; lines[i]; i++) {
Wei Huang68cbecf2018-03-09 12:03:23 -05001758 const char *prefix = NULL;
1759
1760 if (s->dump_info.d_machine == EM_X86_64) {
1761 prefix = "NUMBER(phys_base)=";
1762 } else if (s->dump_info.d_machine == EM_AARCH64) {
1763 prefix = "NUMBER(PHYS_OFFSET)=";
1764 }
1765
1766 if (prefix && g_str_has_prefix(lines[i], prefix)) {
1767 if (qemu_strtou64(lines[i] + strlen(prefix), NULL, 16,
Marc-André Lureaud9feb512017-09-11 18:59:26 +02001768 &phys_base) < 0) {
Wei Huang68cbecf2018-03-09 12:03:23 -05001769 warn_report("Failed to read %s", prefix);
Marc-André Lureaud9feb512017-09-11 18:59:26 +02001770 } else {
1771 s->dump_info.phys_base = phys_base;
1772 }
1773 break;
1774 }
1775 }
1776
1777 g_strfreev(lines);
1778}
1779
zhanghailiang4c7e2512014-10-09 14:13:11 +08001780static void dump_init(DumpState *s, int fd, bool has_format,
1781 DumpGuestMemoryFormat format, bool paging, bool has_filter,
Stephen Brennand43a01d2023-09-18 16:32:32 -07001782 int64_t begin, int64_t length, bool kdump_raw,
1783 Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +08001784{
Janosch Frank86a518b2022-03-30 12:35:55 +00001785 ERRP_GUARD();
Marc-André Lureau903ef732017-09-11 18:59:25 +02001786 VMCoreInfoState *vmci = vmcoreinfo_find();
Andreas Färber182735e2013-05-29 22:29:20 +02001787 CPUState *cpu;
Wen Congyang783e9b42012-05-07 12:10:47 +08001788 int nr_cpus;
1789 int ret;
1790
Peter Xuca1fc8c2016-02-18 13:16:50 +08001791 s->has_format = has_format;
1792 s->format = format;
Peter Xu2264c2c2016-02-18 13:16:53 +08001793 s->written_size = 0;
Stephen Brennand43a01d2023-09-18 16:32:32 -07001794 s->kdump_raw = kdump_raw;
Peter Xuca1fc8c2016-02-18 13:16:50 +08001795
qiaonuohanb53ccc32014-02-18 14:11:36 +08001796 /* kdump-compressed is conflict with paging and filter */
1797 if (has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
1798 assert(!paging && !has_filter);
1799 }
1800
Wen Congyang783e9b42012-05-07 12:10:47 +08001801 if (runstate_is_running()) {
1802 vm_stop(RUN_STATE_SAVE_VM);
1803 s->resume = true;
1804 } else {
1805 s->resume = false;
1806 }
1807
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001808 /* If we use KVM, we should synchronize the registers before we get dump
1809 * info or physmap info.
Wen Congyang783e9b42012-05-07 12:10:47 +08001810 */
Andreas Färber1b3509c2013-06-09 16:48:29 +02001811 cpu_synchronize_all_states();
Wen Congyang783e9b42012-05-07 12:10:47 +08001812 nr_cpus = 0;
Andreas Färberbdc44642013-06-24 23:50:24 +02001813 CPU_FOREACH(cpu) {
Wen Congyang783e9b42012-05-07 12:10:47 +08001814 nr_cpus++;
1815 }
1816
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001817 s->fd = fd;
Janosch Frankdddf7252022-08-11 12:10:58 +00001818 if (has_filter && !length) {
Markus Armbruster28035be2023-10-31 11:45:30 +01001819 error_setg(errp, "parameter 'length' expects a non-zero size");
Janosch Frankdddf7252022-08-11 12:10:58 +00001820 goto cleanup;
1821 }
1822 s->filter_area_begin = begin;
1823 s->filter_area_length = length;
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001824
Janosch Frank9b722242022-10-17 11:32:10 +00001825 /* First index is 0, it's the special null name */
1826 s->string_table_buf = g_array_new(FALSE, TRUE, 1);
1827 /*
1828 * Allocate the null name, due to the clearing option set to true
1829 * it will be 0.
1830 */
1831 g_array_set_size(s->string_table_buf, 1);
1832
Chen Gang29282072014-08-03 23:28:56 +08001833 memory_mapping_list_init(&s->list);
1834
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001835 guest_phys_blocks_init(&s->guest_phys_blocks);
Laszlo Ersekc5d7f602013-08-06 12:37:10 +02001836 guest_phys_blocks_append(&s->guest_phys_blocks);
Peter Xu2264c2c2016-02-18 13:16:53 +08001837 s->total_size = dump_calculate_size(s);
1838#ifdef DEBUG_DUMP_GUEST_MEMORY
1839 fprintf(stderr, "DUMP: total memory to dump: %lu\n", s->total_size);
1840#endif
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001841
Cornelia Huckd1e69942017-09-13 16:20:35 +02001842 /* it does not make sense to dump non-existent memory */
1843 if (!s->total_size) {
1844 error_setg(errp, "dump: no guest memory to dump");
1845 goto cleanup;
1846 }
1847
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001848 /* get dump info: endian, class and architecture.
1849 * If the target architecture is not supported, cpu_get_dump_info() will
1850 * return -1.
1851 */
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001852 ret = cpu_get_dump_info(&s->dump_info, &s->guest_phys_blocks);
Wen Congyang783e9b42012-05-07 12:10:47 +08001853 if (ret < 0) {
Markus Armbrusterf969c622023-02-07 08:51:05 +01001854 error_setg(errp,
1855 "dumping guest memory is not supported on this target");
Wen Congyang783e9b42012-05-07 12:10:47 +08001856 goto cleanup;
1857 }
1858
Andrew Jones8161bef2016-01-11 20:56:20 +01001859 if (!s->dump_info.page_size) {
Philippe Mathieu-Daudéc5d40b22023-02-23 23:38:59 +01001860 s->dump_info.page_size = qemu_target_page_size();
Andrew Jones8161bef2016-01-11 20:56:20 +01001861 }
1862
Paolo Bonzini4720bd02012-06-07 08:48:09 +02001863 s->note_size = cpu_get_note_size(s->dump_info.d_class,
1864 s->dump_info.d_machine, nr_cpus);
Markus Armbrusterf1a46972023-02-07 08:51:06 +01001865 assert(s->note_size >= 0);
Paolo Bonzini4720bd02012-06-07 08:48:09 +02001866
Marc-André Lureau903ef732017-09-11 18:59:25 +02001867 /*
Marc-André Lureaud9feb512017-09-11 18:59:26 +02001868 * The goal of this block is to (a) update the previously guessed
1869 * phys_base, (b) copy the guest note out of the guest.
1870 * Failure to do so is not fatal for dumping.
Marc-André Lureau903ef732017-09-11 18:59:25 +02001871 */
1872 if (vmci) {
1873 uint64_t addr, note_head_size, name_size, desc_size;
1874 uint32_t size;
Thomas Huth71efffb2023-10-04 15:13:38 +02001875 uint16_t guest_format;
Marc-André Lureau903ef732017-09-11 18:59:25 +02001876
Janosch Frank05bbaa502022-03-30 12:36:00 +00001877 note_head_size = dump_is_64bit(s) ?
1878 sizeof(Elf64_Nhdr) : sizeof(Elf32_Nhdr);
Marc-André Lureau903ef732017-09-11 18:59:25 +02001879
Thomas Huth71efffb2023-10-04 15:13:38 +02001880 guest_format = le16_to_cpu(vmci->vmcoreinfo.guest_format);
Marc-André Lureau903ef732017-09-11 18:59:25 +02001881 size = le32_to_cpu(vmci->vmcoreinfo.size);
1882 addr = le64_to_cpu(vmci->vmcoreinfo.paddr);
1883 if (!vmci->has_vmcoreinfo) {
1884 warn_report("guest note is not present");
1885 } else if (size < note_head_size || size > MAX_GUEST_NOTE_SIZE) {
1886 warn_report("guest note size is invalid: %" PRIu32, size);
Thomas Huth71efffb2023-10-04 15:13:38 +02001887 } else if (guest_format != FW_CFG_VMCOREINFO_FORMAT_ELF) {
1888 warn_report("guest note format is unsupported: %" PRIu16, guest_format);
Marc-André Lureau903ef732017-09-11 18:59:25 +02001889 } else {
1890 s->guest_note = g_malloc(size + 1); /* +1 for adding \0 */
1891 cpu_physical_memory_read(addr, s->guest_note, size);
1892
1893 get_note_sizes(s, s->guest_note, NULL, &name_size, &desc_size);
1894 s->guest_note_size = ELF_NOTE_SIZE(note_head_size, name_size,
1895 desc_size);
1896 if (name_size > MAX_GUEST_NOTE_SIZE ||
1897 desc_size > MAX_GUEST_NOTE_SIZE ||
1898 s->guest_note_size > size) {
1899 warn_report("Invalid guest note header");
1900 g_free(s->guest_note);
1901 s->guest_note = NULL;
1902 } else {
Marc-André Lureaud9feb512017-09-11 18:59:26 +02001903 vmcoreinfo_update_phys_base(s);
Marc-André Lureau903ef732017-09-11 18:59:25 +02001904 s->note_size += s->guest_note_size;
1905 }
1906 }
1907 }
1908
Wen Congyang783e9b42012-05-07 12:10:47 +08001909 /* get memory mapping */
Wen Congyang783e9b42012-05-07 12:10:47 +08001910 if (paging) {
Janosch Frank86a518b2022-03-30 12:35:55 +00001911 qemu_get_guest_memory_mapping(&s->list, &s->guest_phys_blocks, errp);
1912 if (*errp) {
Andreas Färber11ed09c2013-05-29 21:54:03 +02001913 goto cleanup;
1914 }
Wen Congyang783e9b42012-05-07 12:10:47 +08001915 } else {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001916 qemu_get_guest_simple_memory_mapping(&s->list, &s->guest_phys_blocks);
Wen Congyang783e9b42012-05-07 12:10:47 +08001917 }
1918
qiaonuohan7aad2482014-02-18 14:11:31 +08001919 s->nr_cpus = nr_cpus;
qiaonuohan7aad2482014-02-18 14:11:31 +08001920
1921 get_max_mapnr(s);
1922
1923 uint64_t tmp;
Andrew Jones8161bef2016-01-11 20:56:20 +01001924 tmp = DIV_ROUND_UP(DIV_ROUND_UP(s->max_mapnr, CHAR_BIT),
1925 s->dump_info.page_size);
1926 s->len_dump_bitmap = tmp * s->dump_info.page_size;
qiaonuohan7aad2482014-02-18 14:11:31 +08001927
qiaonuohanb53ccc32014-02-18 14:11:36 +08001928 /* init for kdump-compressed format */
1929 if (has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
1930 switch (format) {
1931 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB:
1932 s->flag_compress = DUMP_DH_COMPRESSED_ZLIB;
1933 break;
1934
1935 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO:
Laszlo Ersekc998acb2014-05-20 13:42:22 +02001936#ifdef CONFIG_LZO
1937 if (lzo_init() != LZO_E_OK) {
1938 error_setg(errp, "failed to initialize the LZO library");
1939 goto cleanup;
1940 }
1941#endif
qiaonuohanb53ccc32014-02-18 14:11:36 +08001942 s->flag_compress = DUMP_DH_COMPRESSED_LZO;
1943 break;
1944
1945 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY:
1946 s->flag_compress = DUMP_DH_COMPRESSED_SNAPPY;
1947 break;
1948
1949 default:
1950 s->flag_compress = 0;
1951 }
1952
zhanghailiang4c7e2512014-10-09 14:13:11 +08001953 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001954 }
1955
Janosch Frankdddf7252022-08-11 12:10:58 +00001956 if (dump_has_filter(s)) {
1957 memory_mapping_filter(&s->list, s->filter_area_begin, s->filter_area_length);
Wen Congyang783e9b42012-05-07 12:10:47 +08001958 }
1959
1960 /*
Janosch Frank9b722242022-10-17 11:32:10 +00001961 * The first section header is always a special one in which most
1962 * fields are 0. The section header string table is also always
1963 * set.
Wen Congyang783e9b42012-05-07 12:10:47 +08001964 */
Janosch Frank9b722242022-10-17 11:32:10 +00001965 s->shdr_num = 2;
Wen Congyang783e9b42012-05-07 12:10:47 +08001966
Janosch Frank9b722242022-10-17 11:32:10 +00001967 /*
1968 * Adds the number of architecture sections to shdr_num and sets
1969 * elf_section_data_size so we know the offsets and sizes of all
1970 * parts.
1971 */
1972 if (s->dump_info.arch_sections_add_fn) {
1973 s->dump_info.arch_sections_add_fn(s);
Wen Congyang783e9b42012-05-07 12:10:47 +08001974 }
1975
Janosch Frank9b722242022-10-17 11:32:10 +00001976 /*
1977 * calculate shdr_num so we know the offsets and sizes of all
1978 * parts.
1979 * Calculate phdr_num
1980 *
1981 * The absolute maximum amount of phdrs is UINT32_MAX - 1 as
1982 * sh_info is 32 bit. There's special handling once we go over
1983 * UINT16_MAX - 1 but that is handled in the ehdr and section
1984 * code.
1985 */
1986 s->phdr_num = 1; /* Reserve PT_NOTE */
1987 if (s->list.num <= UINT32_MAX - 1) {
1988 s->phdr_num += s->list.num;
1989 } else {
1990 s->phdr_num = UINT32_MAX;
1991 }
1992
1993 /*
1994 * Now that the number of section and program headers is known we
1995 * can calculate the offsets of the headers and data.
1996 */
Janosch Frank05bbaa502022-03-30 12:36:00 +00001997 if (dump_is_64bit(s)) {
Janosch Frankcb415fd2022-10-17 08:38:14 +00001998 s->shdr_offset = sizeof(Elf64_Ehdr);
1999 s->phdr_offset = s->shdr_offset + sizeof(Elf64_Shdr) * s->shdr_num;
2000 s->note_offset = s->phdr_offset + sizeof(Elf64_Phdr) * s->phdr_num;
Wen Congyang783e9b42012-05-07 12:10:47 +08002001 } else {
Janosch Frankcb415fd2022-10-17 08:38:14 +00002002 s->shdr_offset = sizeof(Elf32_Ehdr);
2003 s->phdr_offset = s->shdr_offset + sizeof(Elf32_Shdr) * s->shdr_num;
2004 s->note_offset = s->phdr_offset + sizeof(Elf32_Phdr) * s->phdr_num;
Wen Congyang783e9b42012-05-07 12:10:47 +08002005 }
Janosch Frank13fd4172022-10-17 08:38:16 +00002006 s->memory_offset = s->note_offset + s->note_size;
2007 s->section_offset = s->memory_offset + s->total_size;
Wen Congyang783e9b42012-05-07 12:10:47 +08002008
zhanghailiang4c7e2512014-10-09 14:13:11 +08002009 return;
Wen Congyang783e9b42012-05-07 12:10:47 +08002010
2011cleanup:
Chen Gang29282072014-08-03 23:28:56 +08002012 dump_cleanup(s);
Wen Congyang783e9b42012-05-07 12:10:47 +08002013}
2014
Peter Xuca1fc8c2016-02-18 13:16:50 +08002015/* this operation might be time consuming. */
2016static void dump_process(DumpState *s, Error **errp)
2017{
Janosch Frank86a518b2022-03-30 12:35:55 +00002018 ERRP_GUARD();
Peter Xud42a0d12016-02-18 13:16:56 +08002019 DumpQueryResult *result = NULL;
Peter Xuca1fc8c2016-02-18 13:16:50 +08002020
Viktor Prutyanov2da91b52018-05-17 19:23:39 +03002021 if (s->has_format && s->format == DUMP_GUEST_MEMORY_FORMAT_WIN_DMP) {
Janosch Frank86a518b2022-03-30 12:35:55 +00002022 create_win_dump(s, errp);
Viktor Prutyanov2da91b52018-05-17 19:23:39 +03002023 } else if (s->has_format && s->format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
Janosch Frank86a518b2022-03-30 12:35:55 +00002024 create_kdump_vmcore(s, errp);
Peter Xuca1fc8c2016-02-18 13:16:50 +08002025 } else {
Janosch Frank86a518b2022-03-30 12:35:55 +00002026 create_vmcore(s, errp);
Peter Xuca1fc8c2016-02-18 13:16:50 +08002027 }
2028
Peter Xu39ba2ea2016-02-18 13:16:54 +08002029 /* make sure status is written after written_size updates */
2030 smp_wmb();
Stefan Hajnoczid73415a2020-09-23 11:56:46 +01002031 qatomic_set(&s->status,
Janosch Frank86a518b2022-03-30 12:35:55 +00002032 (*errp ? DUMP_STATUS_FAILED : DUMP_STATUS_COMPLETED));
Peter Xuca1fc8c2016-02-18 13:16:50 +08002033
Peter Xud42a0d12016-02-18 13:16:56 +08002034 /* send DUMP_COMPLETED message (unconditionally) */
2035 result = qmp_query_dump(NULL);
2036 /* should never fail */
2037 assert(result);
Markus Armbrusterd4f8bdc2022-11-04 17:06:55 +01002038 qapi_event_send_dump_completed(result,
2039 *errp ? error_get_pretty(*errp) : NULL);
Peter Xud42a0d12016-02-18 13:16:56 +08002040 qapi_free_DumpQueryResult(result);
2041
Peter Xuca1fc8c2016-02-18 13:16:50 +08002042 dump_cleanup(s);
2043}
2044
Peter Xu1fbeff72016-02-18 13:16:52 +08002045static void *dump_thread(void *data)
2046{
Peter Xu1fbeff72016-02-18 13:16:52 +08002047 DumpState *s = (DumpState *)data;
Alberto Garciac3a8fe32017-08-29 15:08:36 +03002048 dump_process(s, NULL);
Peter Xu1fbeff72016-02-18 13:16:52 +08002049 return NULL;
2050}
2051
Peter Xu39ba2ea2016-02-18 13:16:54 +08002052DumpQueryResult *qmp_query_dump(Error **errp)
2053{
2054 DumpQueryResult *result = g_new(DumpQueryResult, 1);
2055 DumpState *state = &dump_state_global;
Stefan Hajnoczid73415a2020-09-23 11:56:46 +01002056 result->status = qatomic_read(&state->status);
Peter Xu39ba2ea2016-02-18 13:16:54 +08002057 /* make sure we are reading status and written_size in order */
2058 smp_rmb();
2059 result->completed = state->written_size;
2060 result->total = state->total_size;
2061 return result;
2062}
2063
Markus Armbruster8beaeed2023-10-31 11:45:27 +01002064void qmp_dump_guest_memory(bool paging, const char *protocol,
Peter Xu228de9c2016-02-18 13:16:47 +08002065 bool has_detach, bool detach,
Markus Armbruster8beaeed2023-10-31 11:45:27 +01002066 bool has_begin, int64_t begin,
2067 bool has_length, int64_t length,
2068 bool has_format, DumpGuestMemoryFormat format,
2069 Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +08002070{
Janosch Frank86a518b2022-03-30 12:35:55 +00002071 ERRP_GUARD();
Wen Congyang783e9b42012-05-07 12:10:47 +08002072 const char *p;
Markus Armbruster28035be2023-10-31 11:45:30 +01002073 int fd;
Wen Congyang783e9b42012-05-07 12:10:47 +08002074 DumpState *s;
Peter Xu1fbeff72016-02-18 13:16:52 +08002075 bool detach_p = false;
Stephen Brennane6549192023-09-18 16:32:33 -07002076 bool kdump_raw = false;
Wen Congyang783e9b42012-05-07 12:10:47 +08002077
Peter Xu63e27f22016-02-18 13:16:51 +08002078 if (runstate_check(RUN_STATE_INMIGRATE)) {
2079 error_setg(errp, "Dump not allowed during incoming migration.");
2080 return;
2081 }
2082
Peter Xu65d64f32016-02-18 13:16:49 +08002083 /* if there is a dump in background, we should wait until the dump
2084 * finished */
Marc-André Lureau544803c2022-03-23 19:57:31 +04002085 if (qemu_system_dump_in_progress()) {
Peter Xu65d64f32016-02-18 13:16:49 +08002086 error_setg(errp, "There is a dump in process, please wait.");
2087 return;
2088 }
2089
qiaonuohanb53ccc32014-02-18 14:11:36 +08002090 /*
Stephen Brennane6549192023-09-18 16:32:33 -07002091 * externally, we represent kdump-raw-* as separate formats, but internally
2092 * they are handled the same, except for the "raw" flag
2093 */
2094 if (has_format) {
2095 switch (format) {
2096 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_RAW_ZLIB:
2097 format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB;
2098 kdump_raw = true;
2099 break;
2100 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_RAW_LZO:
2101 format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO;
2102 kdump_raw = true;
2103 break;
2104 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_RAW_SNAPPY:
2105 format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY;
2106 kdump_raw = true;
2107 break;
2108 default:
2109 break;
2110 }
2111 }
2112
2113 /*
qiaonuohanb53ccc32014-02-18 14:11:36 +08002114 * kdump-compressed format need the whole memory dumped, so paging or
2115 * filter is not supported here.
2116 */
2117 if ((has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) &&
2118 (paging || has_begin || has_length)) {
2119 error_setg(errp, "kdump-compressed format doesn't support paging or "
2120 "filter");
2121 return;
2122 }
Wen Congyang783e9b42012-05-07 12:10:47 +08002123 if (has_begin && !has_length) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002124 error_setg(errp, QERR_MISSING_PARAMETER, "length");
Wen Congyang783e9b42012-05-07 12:10:47 +08002125 return;
2126 }
2127 if (!has_begin && has_length) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002128 error_setg(errp, QERR_MISSING_PARAMETER, "begin");
Wen Congyang783e9b42012-05-07 12:10:47 +08002129 return;
2130 }
Peter Xu1fbeff72016-02-18 13:16:52 +08002131 if (has_detach) {
2132 detach_p = detach;
2133 }
Wen Congyang783e9b42012-05-07 12:10:47 +08002134
qiaonuohanb53ccc32014-02-18 14:11:36 +08002135 /* check whether lzo/snappy is supported */
2136#ifndef CONFIG_LZO
2137 if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO) {
2138 error_setg(errp, "kdump-lzo is not available now");
2139 return;
2140 }
2141#endif
2142
2143#ifndef CONFIG_SNAPPY
2144 if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY) {
2145 error_setg(errp, "kdump-snappy is not available now");
2146 return;
2147 }
2148#endif
2149
Philippe Mathieu-Daudéefc31462023-02-23 23:58:16 +01002150 if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_WIN_DMP
2151 && !win_dump_available(errp)) {
Viktor Prutyanov2da91b52018-05-17 19:23:39 +03002152 return;
2153 }
Viktor Prutyanov2da91b52018-05-17 19:23:39 +03002154
Markus Armbruster8beaeed2023-10-31 11:45:27 +01002155 if (strstart(protocol, "fd:", &p)) {
Kevin Wolf947e4742020-10-05 17:58:44 +02002156 fd = monitor_get_fd(monitor_cur(), p, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +08002157 if (fd == -1) {
Wen Congyang783e9b42012-05-07 12:10:47 +08002158 return;
2159 }
Markus Armbruster28035be2023-10-31 11:45:30 +01002160 } else if (strstart(protocol, "file:", &p)) {
2161 fd = qemu_create(p, O_WRONLY | O_TRUNC | O_BINARY, S_IRUSR, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +08002162 if (fd < 0) {
Wen Congyang783e9b42012-05-07 12:10:47 +08002163 return;
2164 }
Markus Armbruster28035be2023-10-31 11:45:30 +01002165 } else {
2166 error_setg(errp,
2167 "parameter 'protocol' must start with 'file:' or 'fd:'");
Wen Congyang783e9b42012-05-07 12:10:47 +08002168 return;
2169 }
Stephen Brennane6549192023-09-18 16:32:33 -07002170 if (kdump_raw && lseek(fd, 0, SEEK_CUR) == (off_t) -1) {
Zongmin Zhou95a40c42023-11-07 10:44:17 +08002171 close(fd);
Stephen Brennane6549192023-09-18 16:32:33 -07002172 error_setg(errp, "kdump-raw formats require a seekable file");
2173 return;
2174 }
Wen Congyang783e9b42012-05-07 12:10:47 +08002175
Peter Xub7bc6b12021-09-22 12:20:09 -04002176 if (!dump_migration_blocker) {
2177 error_setg(&dump_migration_blocker,
2178 "Live migration disabled: dump-guest-memory in progress");
2179 }
2180
2181 /*
2182 * Allows even for -only-migratable, but forbid migration during the
2183 * process of dump guest memory.
2184 */
Steve Sistarec8a7fc52023-10-18 06:03:36 -07002185 if (migrate_add_blocker_internal(&dump_migration_blocker, errp)) {
Peter Xub7bc6b12021-09-22 12:20:09 -04002186 /* Remember to release the fd before passing it over to dump state */
2187 close(fd);
2188 return;
2189 }
2190
Peter Xubaf28f52016-02-18 13:16:48 +08002191 s = &dump_state_global;
2192 dump_state_prepare(s);
Wen Congyang783e9b42012-05-07 12:10:47 +08002193
zhanghailiang4c7e2512014-10-09 14:13:11 +08002194 dump_init(s, fd, has_format, format, paging, has_begin,
Stephen Brennane6549192023-09-18 16:32:33 -07002195 begin, length, kdump_raw, errp);
Janosch Frank86a518b2022-03-30 12:35:55 +00002196 if (*errp) {
Stefan Hajnoczid73415a2020-09-23 11:56:46 +01002197 qatomic_set(&s->status, DUMP_STATUS_FAILED);
Wen Congyang783e9b42012-05-07 12:10:47 +08002198 return;
2199 }
2200
Peter Xu1fbeff72016-02-18 13:16:52 +08002201 if (detach_p) {
2202 /* detached dump */
Fam Zheng6796b402017-05-03 15:28:19 +08002203 s->detached = true;
Peter Xu1fbeff72016-02-18 13:16:52 +08002204 qemu_thread_create(&s->dump_thread, "dump_thread", dump_thread,
2205 s, QEMU_THREAD_DETACHED);
2206 } else {
2207 /* sync dump */
2208 dump_process(s, errp);
2209 }
Wen Congyang783e9b42012-05-07 12:10:47 +08002210}
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002211
2212DumpGuestMemoryCapability *qmp_query_dump_guest_memory_capability(Error **errp)
2213{
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002214 DumpGuestMemoryCapability *cap =
Markus Armbrusterb21e2382022-03-15 15:41:56 +01002215 g_new0(DumpGuestMemoryCapability, 1);
Eric Blake95b3a8c2021-01-13 16:10:13 -06002216 DumpGuestMemoryFormatList **tail = &cap->formats;
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002217
2218 /* elf is always available */
Eric Blake95b3a8c2021-01-13 16:10:13 -06002219 QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_ELF);
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002220
2221 /* kdump-zlib is always available */
Eric Blake95b3a8c2021-01-13 16:10:13 -06002222 QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB);
Stephen Brennane6549192023-09-18 16:32:33 -07002223 QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_KDUMP_RAW_ZLIB);
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002224
2225 /* add new item if kdump-lzo is available */
2226#ifdef CONFIG_LZO
Eric Blake95b3a8c2021-01-13 16:10:13 -06002227 QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO);
Stephen Brennane6549192023-09-18 16:32:33 -07002228 QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_KDUMP_RAW_LZO);
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002229#endif
2230
2231 /* add new item if kdump-snappy is available */
2232#ifdef CONFIG_SNAPPY
Eric Blake95b3a8c2021-01-13 16:10:13 -06002233 QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY);
Stephen Brennane6549192023-09-18 16:32:33 -07002234 QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_KDUMP_RAW_SNAPPY);
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002235#endif
2236
Philippe Mathieu-Daudéefc31462023-02-23 23:58:16 +01002237 if (win_dump_available(NULL)) {
2238 QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_WIN_DMP);
2239 }
Viktor Prutyanov2da91b52018-05-17 19:23:39 +03002240
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002241 return cap;
2242}