blob: b91e9d8c123e170b80e1fdf6f2c235447b49480e [file] [log] [blame]
Wen Congyang783e9b42012-05-07 12:10:47 +08001/*
2 * QEMU dump
3 *
4 * Copyright Fujitsu, Corp. 2011, 2012
5 *
6 * Authors:
7 * Wen Congyang <wency@cn.fujitsu.com>
8 *
Stefan Weil352666e2012-06-10 19:34:04 +00009 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
Wen Congyang783e9b42012-05-07 12:10:47 +080011 *
12 */
13
Peter Maydelld38ea872016-01-29 17:50:05 +000014#include "qemu/osdep.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020015#include "qemu/cutils.h"
Wen Congyang783e9b42012-05-07 12:10:47 +080016#include "elf.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010017#include "exec/hwaddr.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010018#include "monitor/monitor.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010019#include "sysemu/kvm.h"
20#include "sysemu/dump.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010021#include "sysemu/memory_mapping.h"
Markus Armbruster54d31232019-08-12 07:23:59 +020022#include "sysemu/runstate.h"
Andreas Färber1b3509c2013-06-09 16:48:29 +020023#include "sysemu/cpus.h"
Markus Armbrustere688df62018-02-01 12:18:31 +010024#include "qapi/error.h"
Markus Armbrusterd06b7472019-06-19 22:10:47 +020025#include "qapi/qapi-commands-dump.h"
26#include "qapi/qapi-events-dump.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010027#include "qapi/qmp/qerror.h"
Marc-André Lureau903ef732017-09-11 18:59:25 +020028#include "qemu/error-report.h"
Markus Armbrusterdb725812019-08-12 07:23:50 +020029#include "qemu/main-loop.h"
Marc-André Lureau903ef732017-09-11 18:59:25 +020030#include "hw/misc/vmcoreinfo.h"
Peter Xub7bc6b12021-09-22 12:20:09 -040031#include "migration/blocker.h"
Wen Congyang783e9b42012-05-07 12:10:47 +080032
Viktor Prutyanov2da91b52018-05-17 19:23:39 +030033#ifdef TARGET_X86_64
34#include "win_dump.h"
35#endif
36
qiaonuohand12f57e2014-02-18 14:11:35 +080037#include <zlib.h>
38#ifdef CONFIG_LZO
39#include <lzo/lzo1x.h>
40#endif
41#ifdef CONFIG_SNAPPY
42#include <snappy-c.h>
43#endif
qiaonuohan4ab23a92014-02-18 14:11:37 +080044#ifndef ELF_MACHINE_UNAME
45#define ELF_MACHINE_UNAME "Unknown"
46#endif
qiaonuohand12f57e2014-02-18 14:11:35 +080047
Marc-André Lureau903ef732017-09-11 18:59:25 +020048#define MAX_GUEST_NOTE_SIZE (1 << 20) /* 1MB should be enough */
49
Peter Xub7bc6b12021-09-22 12:20:09 -040050static Error *dump_migration_blocker;
51
Marc-André Lureau903ef732017-09-11 18:59:25 +020052#define ELF_NOTE_SIZE(hdr_size, name_size, desc_size) \
53 ((DIV_ROUND_UP((hdr_size), 4) + \
54 DIV_ROUND_UP((name_size), 4) + \
55 DIV_ROUND_UP((desc_size), 4)) * 4)
56
Bharata B Raoacb0ef52014-05-19 19:57:44 +020057uint16_t cpu_to_dump16(DumpState *s, uint16_t val)
Wen Congyang783e9b42012-05-07 12:10:47 +080058{
Bharata B Raoacb0ef52014-05-19 19:57:44 +020059 if (s->dump_info.d_endian == ELFDATA2LSB) {
Wen Congyang783e9b42012-05-07 12:10:47 +080060 val = cpu_to_le16(val);
61 } else {
62 val = cpu_to_be16(val);
63 }
64
65 return val;
66}
67
Bharata B Raoacb0ef52014-05-19 19:57:44 +020068uint32_t cpu_to_dump32(DumpState *s, uint32_t val)
Wen Congyang783e9b42012-05-07 12:10:47 +080069{
Bharata B Raoacb0ef52014-05-19 19:57:44 +020070 if (s->dump_info.d_endian == ELFDATA2LSB) {
Wen Congyang783e9b42012-05-07 12:10:47 +080071 val = cpu_to_le32(val);
72 } else {
73 val = cpu_to_be32(val);
74 }
75
76 return val;
77}
78
Bharata B Raoacb0ef52014-05-19 19:57:44 +020079uint64_t cpu_to_dump64(DumpState *s, uint64_t val)
Wen Congyang783e9b42012-05-07 12:10:47 +080080{
Bharata B Raoacb0ef52014-05-19 19:57:44 +020081 if (s->dump_info.d_endian == ELFDATA2LSB) {
Wen Congyang783e9b42012-05-07 12:10:47 +080082 val = cpu_to_le64(val);
83 } else {
84 val = cpu_to_be64(val);
85 }
86
87 return val;
88}
89
Wen Congyang783e9b42012-05-07 12:10:47 +080090static int dump_cleanup(DumpState *s)
91{
Laszlo Ersek5ee163e2013-08-06 12:37:09 +020092 guest_phys_blocks_free(&s->guest_phys_blocks);
Wen Congyang783e9b42012-05-07 12:10:47 +080093 memory_mapping_list_free(&s->list);
Chen Gang29282072014-08-03 23:28:56 +080094 close(s->fd);
Marc-André Lureau903ef732017-09-11 18:59:25 +020095 g_free(s->guest_note);
96 s->guest_note = NULL;
Wen Congyang783e9b42012-05-07 12:10:47 +080097 if (s->resume) {
Fam Zheng6796b402017-05-03 15:28:19 +080098 if (s->detached) {
99 qemu_mutex_lock_iothread();
100 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800101 vm_start();
Fam Zheng6796b402017-05-03 15:28:19 +0800102 if (s->detached) {
103 qemu_mutex_unlock_iothread();
104 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800105 }
Peter Xub7bc6b12021-09-22 12:20:09 -0400106 migrate_del_blocker(dump_migration_blocker);
Wen Congyang783e9b42012-05-07 12:10:47 +0800107
Chen Gang29282072014-08-03 23:28:56 +0800108 return 0;
Wen Congyang783e9b42012-05-07 12:10:47 +0800109}
110
qiaonuohanb5ba1cc2014-02-18 14:11:25 +0800111static int fd_write_vmcore(const void *buf, size_t size, void *opaque)
Wen Congyang783e9b42012-05-07 12:10:47 +0800112{
113 DumpState *s = opaque;
Luiz Capitulino2f616522012-09-21 13:17:55 -0300114 size_t written_size;
Wen Congyang783e9b42012-05-07 12:10:47 +0800115
Luiz Capitulino2f616522012-09-21 13:17:55 -0300116 written_size = qemu_write_full(s->fd, buf, size);
117 if (written_size != size) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200118 return -errno;
Wen Congyang783e9b42012-05-07 12:10:47 +0800119 }
120
121 return 0;
122}
123
zhanghailiang4c7e2512014-10-09 14:13:11 +0800124static void write_elf64_header(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800125{
126 Elf64_Ehdr elf_header;
127 int ret;
Wen Congyang783e9b42012-05-07 12:10:47 +0800128
129 memset(&elf_header, 0, sizeof(Elf64_Ehdr));
130 memcpy(&elf_header, ELFMAG, SELFMAG);
131 elf_header.e_ident[EI_CLASS] = ELFCLASS64;
132 elf_header.e_ident[EI_DATA] = s->dump_info.d_endian;
133 elf_header.e_ident[EI_VERSION] = EV_CURRENT;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200134 elf_header.e_type = cpu_to_dump16(s, ET_CORE);
135 elf_header.e_machine = cpu_to_dump16(s, s->dump_info.d_machine);
136 elf_header.e_version = cpu_to_dump32(s, EV_CURRENT);
137 elf_header.e_ehsize = cpu_to_dump16(s, sizeof(elf_header));
138 elf_header.e_phoff = cpu_to_dump64(s, sizeof(Elf64_Ehdr));
139 elf_header.e_phentsize = cpu_to_dump16(s, sizeof(Elf64_Phdr));
140 elf_header.e_phnum = cpu_to_dump16(s, s->phdr_num);
Wen Congyang783e9b42012-05-07 12:10:47 +0800141 if (s->have_section) {
142 uint64_t shoff = sizeof(Elf64_Ehdr) + sizeof(Elf64_Phdr) * s->sh_info;
143
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200144 elf_header.e_shoff = cpu_to_dump64(s, shoff);
145 elf_header.e_shentsize = cpu_to_dump16(s, sizeof(Elf64_Shdr));
146 elf_header.e_shnum = cpu_to_dump16(s, 1);
Wen Congyang783e9b42012-05-07 12:10:47 +0800147 }
148
149 ret = fd_write_vmcore(&elf_header, sizeof(elf_header), s);
150 if (ret < 0) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200151 error_setg_errno(errp, -ret, "dump: failed to write elf header");
Wen Congyang783e9b42012-05-07 12:10:47 +0800152 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800153}
154
zhanghailiang4c7e2512014-10-09 14:13:11 +0800155static void write_elf32_header(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800156{
157 Elf32_Ehdr elf_header;
158 int ret;
Wen Congyang783e9b42012-05-07 12:10:47 +0800159
160 memset(&elf_header, 0, sizeof(Elf32_Ehdr));
161 memcpy(&elf_header, ELFMAG, SELFMAG);
162 elf_header.e_ident[EI_CLASS] = ELFCLASS32;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200163 elf_header.e_ident[EI_DATA] = s->dump_info.d_endian;
Wen Congyang783e9b42012-05-07 12:10:47 +0800164 elf_header.e_ident[EI_VERSION] = EV_CURRENT;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200165 elf_header.e_type = cpu_to_dump16(s, ET_CORE);
166 elf_header.e_machine = cpu_to_dump16(s, s->dump_info.d_machine);
167 elf_header.e_version = cpu_to_dump32(s, EV_CURRENT);
168 elf_header.e_ehsize = cpu_to_dump16(s, sizeof(elf_header));
169 elf_header.e_phoff = cpu_to_dump32(s, sizeof(Elf32_Ehdr));
170 elf_header.e_phentsize = cpu_to_dump16(s, sizeof(Elf32_Phdr));
171 elf_header.e_phnum = cpu_to_dump16(s, s->phdr_num);
Wen Congyang783e9b42012-05-07 12:10:47 +0800172 if (s->have_section) {
173 uint32_t shoff = sizeof(Elf32_Ehdr) + sizeof(Elf32_Phdr) * s->sh_info;
174
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200175 elf_header.e_shoff = cpu_to_dump32(s, shoff);
176 elf_header.e_shentsize = cpu_to_dump16(s, sizeof(Elf32_Shdr));
177 elf_header.e_shnum = cpu_to_dump16(s, 1);
Wen Congyang783e9b42012-05-07 12:10:47 +0800178 }
179
180 ret = fd_write_vmcore(&elf_header, sizeof(elf_header), s);
181 if (ret < 0) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200182 error_setg_errno(errp, -ret, "dump: failed to write elf header");
Wen Congyang783e9b42012-05-07 12:10:47 +0800183 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800184}
185
zhanghailiang4c7e2512014-10-09 14:13:11 +0800186static void write_elf64_load(DumpState *s, MemoryMapping *memory_mapping,
187 int phdr_index, hwaddr offset,
188 hwaddr filesz, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800189{
190 Elf64_Phdr phdr;
191 int ret;
Wen Congyang783e9b42012-05-07 12:10:47 +0800192
193 memset(&phdr, 0, sizeof(Elf64_Phdr));
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200194 phdr.p_type = cpu_to_dump32(s, PT_LOAD);
195 phdr.p_offset = cpu_to_dump64(s, offset);
196 phdr.p_paddr = cpu_to_dump64(s, memory_mapping->phys_addr);
197 phdr.p_filesz = cpu_to_dump64(s, filesz);
198 phdr.p_memsz = cpu_to_dump64(s, memory_mapping->length);
Jon Dorone17bebd2019-01-09 10:22:03 +0200199 phdr.p_vaddr = cpu_to_dump64(s, memory_mapping->virt_addr) ?: phdr.p_paddr;
Wen Congyang783e9b42012-05-07 12:10:47 +0800200
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200201 assert(memory_mapping->length >= filesz);
202
Wen Congyang783e9b42012-05-07 12:10:47 +0800203 ret = fd_write_vmcore(&phdr, sizeof(Elf64_Phdr), s);
204 if (ret < 0) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200205 error_setg_errno(errp, -ret,
206 "dump: failed to write program header table");
Wen Congyang783e9b42012-05-07 12:10:47 +0800207 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800208}
209
zhanghailiang4c7e2512014-10-09 14:13:11 +0800210static void write_elf32_load(DumpState *s, MemoryMapping *memory_mapping,
211 int phdr_index, hwaddr offset,
212 hwaddr filesz, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800213{
214 Elf32_Phdr phdr;
215 int ret;
Wen Congyang783e9b42012-05-07 12:10:47 +0800216
217 memset(&phdr, 0, sizeof(Elf32_Phdr));
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200218 phdr.p_type = cpu_to_dump32(s, PT_LOAD);
219 phdr.p_offset = cpu_to_dump32(s, offset);
220 phdr.p_paddr = cpu_to_dump32(s, memory_mapping->phys_addr);
221 phdr.p_filesz = cpu_to_dump32(s, filesz);
222 phdr.p_memsz = cpu_to_dump32(s, memory_mapping->length);
Jon Dorone17bebd2019-01-09 10:22:03 +0200223 phdr.p_vaddr =
224 cpu_to_dump32(s, memory_mapping->virt_addr) ?: phdr.p_paddr;
Wen Congyang783e9b42012-05-07 12:10:47 +0800225
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200226 assert(memory_mapping->length >= filesz);
227
Wen Congyang783e9b42012-05-07 12:10:47 +0800228 ret = fd_write_vmcore(&phdr, sizeof(Elf32_Phdr), s);
229 if (ret < 0) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200230 error_setg_errno(errp, -ret,
231 "dump: failed to write program header table");
Wen Congyang783e9b42012-05-07 12:10:47 +0800232 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800233}
234
zhanghailiang4c7e2512014-10-09 14:13:11 +0800235static void write_elf64_note(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800236{
237 Elf64_Phdr phdr;
Avi Kivitya8170e52012-10-23 12:30:10 +0200238 hwaddr begin = s->memory_offset - s->note_size;
Wen Congyang783e9b42012-05-07 12:10:47 +0800239 int ret;
240
241 memset(&phdr, 0, sizeof(Elf64_Phdr));
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200242 phdr.p_type = cpu_to_dump32(s, PT_NOTE);
243 phdr.p_offset = cpu_to_dump64(s, begin);
Wen Congyang783e9b42012-05-07 12:10:47 +0800244 phdr.p_paddr = 0;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200245 phdr.p_filesz = cpu_to_dump64(s, s->note_size);
246 phdr.p_memsz = cpu_to_dump64(s, s->note_size);
Wen Congyang783e9b42012-05-07 12:10:47 +0800247 phdr.p_vaddr = 0;
248
249 ret = fd_write_vmcore(&phdr, sizeof(Elf64_Phdr), s);
250 if (ret < 0) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200251 error_setg_errno(errp, -ret,
252 "dump: failed to write program header table");
Wen Congyang783e9b42012-05-07 12:10:47 +0800253 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800254}
255
Paolo Bonzini0bc3cd62013-04-08 17:29:59 +0200256static inline int cpu_index(CPUState *cpu)
257{
258 return cpu->cpu_index + 1;
259}
260
Marc-André Lureau903ef732017-09-11 18:59:25 +0200261static void write_guest_note(WriteCoreDumpFunction f, DumpState *s,
262 Error **errp)
263{
264 int ret;
265
266 if (s->guest_note) {
267 ret = f(s->guest_note, s->guest_note_size, s);
268 if (ret < 0) {
269 error_setg(errp, "dump: failed to write guest note");
270 }
271 }
272}
273
zhanghailiang4c7e2512014-10-09 14:13:11 +0800274static void write_elf64_notes(WriteCoreDumpFunction f, DumpState *s,
275 Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800276{
Andreas Färber0d342822012-12-17 07:12:13 +0100277 CPUState *cpu;
Wen Congyang783e9b42012-05-07 12:10:47 +0800278 int ret;
279 int id;
280
Andreas Färberbdc44642013-06-24 23:50:24 +0200281 CPU_FOREACH(cpu) {
Andreas Färber0d342822012-12-17 07:12:13 +0100282 id = cpu_index(cpu);
qiaonuohan6a519912014-02-18 14:11:26 +0800283 ret = cpu_write_elf64_note(f, cpu, id, s);
Wen Congyang783e9b42012-05-07 12:10:47 +0800284 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800285 error_setg(errp, "dump: failed to write elf notes");
zhanghailiang4c7e2512014-10-09 14:13:11 +0800286 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800287 }
288 }
289
Andreas Färberbdc44642013-06-24 23:50:24 +0200290 CPU_FOREACH(cpu) {
qiaonuohan6a519912014-02-18 14:11:26 +0800291 ret = cpu_write_elf64_qemunote(f, cpu, s);
Wen Congyang783e9b42012-05-07 12:10:47 +0800292 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800293 error_setg(errp, "dump: failed to write CPU status");
zhanghailiang4c7e2512014-10-09 14:13:11 +0800294 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800295 }
296 }
Marc-André Lureau903ef732017-09-11 18:59:25 +0200297
298 write_guest_note(f, s, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800299}
300
zhanghailiang4c7e2512014-10-09 14:13:11 +0800301static void write_elf32_note(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800302{
Avi Kivitya8170e52012-10-23 12:30:10 +0200303 hwaddr begin = s->memory_offset - s->note_size;
Wen Congyang783e9b42012-05-07 12:10:47 +0800304 Elf32_Phdr phdr;
Wen Congyang783e9b42012-05-07 12:10:47 +0800305 int ret;
306
307 memset(&phdr, 0, sizeof(Elf32_Phdr));
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200308 phdr.p_type = cpu_to_dump32(s, PT_NOTE);
309 phdr.p_offset = cpu_to_dump32(s, begin);
Wen Congyang783e9b42012-05-07 12:10:47 +0800310 phdr.p_paddr = 0;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200311 phdr.p_filesz = cpu_to_dump32(s, s->note_size);
312 phdr.p_memsz = cpu_to_dump32(s, s->note_size);
Wen Congyang783e9b42012-05-07 12:10:47 +0800313 phdr.p_vaddr = 0;
314
315 ret = fd_write_vmcore(&phdr, sizeof(Elf32_Phdr), s);
316 if (ret < 0) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200317 error_setg_errno(errp, -ret,
318 "dump: failed to write program header table");
Wen Congyang783e9b42012-05-07 12:10:47 +0800319 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800320}
321
zhanghailiang4c7e2512014-10-09 14:13:11 +0800322static void write_elf32_notes(WriteCoreDumpFunction f, DumpState *s,
323 Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800324{
Andreas Färber0d342822012-12-17 07:12:13 +0100325 CPUState *cpu;
Wen Congyang783e9b42012-05-07 12:10:47 +0800326 int ret;
327 int id;
328
Andreas Färberbdc44642013-06-24 23:50:24 +0200329 CPU_FOREACH(cpu) {
Andreas Färber0d342822012-12-17 07:12:13 +0100330 id = cpu_index(cpu);
qiaonuohan6a519912014-02-18 14:11:26 +0800331 ret = cpu_write_elf32_note(f, cpu, id, s);
Wen Congyang783e9b42012-05-07 12:10:47 +0800332 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800333 error_setg(errp, "dump: failed to write elf notes");
zhanghailiang4c7e2512014-10-09 14:13:11 +0800334 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800335 }
336 }
337
Andreas Färberbdc44642013-06-24 23:50:24 +0200338 CPU_FOREACH(cpu) {
qiaonuohan6a519912014-02-18 14:11:26 +0800339 ret = cpu_write_elf32_qemunote(f, cpu, s);
Wen Congyang783e9b42012-05-07 12:10:47 +0800340 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800341 error_setg(errp, "dump: failed to write CPU status");
zhanghailiang4c7e2512014-10-09 14:13:11 +0800342 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800343 }
344 }
Marc-André Lureau903ef732017-09-11 18:59:25 +0200345
346 write_guest_note(f, s, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800347}
348
zhanghailiang4c7e2512014-10-09 14:13:11 +0800349static void write_elf_section(DumpState *s, int type, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800350{
351 Elf32_Shdr shdr32;
352 Elf64_Shdr shdr64;
Wen Congyang783e9b42012-05-07 12:10:47 +0800353 int shdr_size;
354 void *shdr;
355 int ret;
356
357 if (type == 0) {
358 shdr_size = sizeof(Elf32_Shdr);
359 memset(&shdr32, 0, shdr_size);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200360 shdr32.sh_info = cpu_to_dump32(s, s->sh_info);
Wen Congyang783e9b42012-05-07 12:10:47 +0800361 shdr = &shdr32;
362 } else {
363 shdr_size = sizeof(Elf64_Shdr);
364 memset(&shdr64, 0, shdr_size);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200365 shdr64.sh_info = cpu_to_dump32(s, s->sh_info);
Wen Congyang783e9b42012-05-07 12:10:47 +0800366 shdr = &shdr64;
367 }
368
Peter Maydell174d2d62020-03-24 17:36:30 +0000369 ret = fd_write_vmcore(shdr, shdr_size, s);
Wen Congyang783e9b42012-05-07 12:10:47 +0800370 if (ret < 0) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200371 error_setg_errno(errp, -ret,
372 "dump: failed to write section header table");
Wen Congyang783e9b42012-05-07 12:10:47 +0800373 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800374}
375
zhanghailiang4c7e2512014-10-09 14:13:11 +0800376static void write_data(DumpState *s, void *buf, int length, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800377{
378 int ret;
379
380 ret = fd_write_vmcore(buf, length, s);
381 if (ret < 0) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200382 error_setg_errno(errp, -ret, "dump: failed to save memory");
Peter Xu2264c2c2016-02-18 13:16:53 +0800383 } else {
384 s->written_size += length;
Wen Congyang783e9b42012-05-07 12:10:47 +0800385 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800386}
387
zhanghailiang4c7e2512014-10-09 14:13:11 +0800388/* write the memory to vmcore. 1 page per I/O. */
389static void write_memory(DumpState *s, GuestPhysBlock *block, ram_addr_t start,
390 int64_t size, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800391{
Janosch Frank86a518b2022-03-30 12:35:55 +0000392 ERRP_GUARD();
Wen Congyang783e9b42012-05-07 12:10:47 +0800393 int64_t i;
Wen Congyang783e9b42012-05-07 12:10:47 +0800394
Andrew Jones8161bef2016-01-11 20:56:20 +0100395 for (i = 0; i < size / s->dump_info.page_size; i++) {
396 write_data(s, block->host_addr + start + i * s->dump_info.page_size,
Janosch Frank86a518b2022-03-30 12:35:55 +0000397 s->dump_info.page_size, errp);
398 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800399 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800400 }
401 }
402
Andrew Jones8161bef2016-01-11 20:56:20 +0100403 if ((size % s->dump_info.page_size) != 0) {
404 write_data(s, block->host_addr + start + i * s->dump_info.page_size,
Janosch Frank86a518b2022-03-30 12:35:55 +0000405 size % s->dump_info.page_size, errp);
406 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800407 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800408 }
409 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800410}
411
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200412/* get the memory's offset and size in the vmcore */
413static void get_offset_range(hwaddr phys_addr,
414 ram_addr_t mapping_length,
415 DumpState *s,
416 hwaddr *p_offset,
417 hwaddr *p_filesz)
Wen Congyang783e9b42012-05-07 12:10:47 +0800418{
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200419 GuestPhysBlock *block;
Avi Kivitya8170e52012-10-23 12:30:10 +0200420 hwaddr offset = s->memory_offset;
Wen Congyang783e9b42012-05-07 12:10:47 +0800421 int64_t size_in_block, start;
422
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200423 /* When the memory is not stored into vmcore, offset will be -1 */
424 *p_offset = -1;
425 *p_filesz = 0;
426
Wen Congyang783e9b42012-05-07 12:10:47 +0800427 if (s->has_filter) {
428 if (phys_addr < s->begin || phys_addr >= s->begin + s->length) {
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200429 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800430 }
431 }
432
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200433 QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
Wen Congyang783e9b42012-05-07 12:10:47 +0800434 if (s->has_filter) {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200435 if (block->target_start >= s->begin + s->length ||
436 block->target_end <= s->begin) {
Wen Congyang783e9b42012-05-07 12:10:47 +0800437 /* This block is out of the range */
438 continue;
439 }
440
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200441 if (s->begin <= block->target_start) {
442 start = block->target_start;
Wen Congyang783e9b42012-05-07 12:10:47 +0800443 } else {
444 start = s->begin;
445 }
446
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200447 size_in_block = block->target_end - start;
448 if (s->begin + s->length < block->target_end) {
449 size_in_block -= block->target_end - (s->begin + s->length);
Wen Congyang783e9b42012-05-07 12:10:47 +0800450 }
451 } else {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200452 start = block->target_start;
453 size_in_block = block->target_end - block->target_start;
Wen Congyang783e9b42012-05-07 12:10:47 +0800454 }
455
456 if (phys_addr >= start && phys_addr < start + size_in_block) {
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200457 *p_offset = phys_addr - start + offset;
458
459 /* The offset range mapped from the vmcore file must not spill over
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200460 * the GuestPhysBlock, clamp it. The rest of the mapping will be
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200461 * zero-filled in memory at load time; see
462 * <http://refspecs.linuxbase.org/elf/gabi4+/ch5.pheader.html>.
463 */
464 *p_filesz = phys_addr + mapping_length <= start + size_in_block ?
465 mapping_length :
466 size_in_block - (phys_addr - start);
467 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800468 }
469
470 offset += size_in_block;
471 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800472}
473
zhanghailiang4c7e2512014-10-09 14:13:11 +0800474static void write_elf_loads(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800475{
Janosch Frank86a518b2022-03-30 12:35:55 +0000476 ERRP_GUARD();
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200477 hwaddr offset, filesz;
Wen Congyang783e9b42012-05-07 12:10:47 +0800478 MemoryMapping *memory_mapping;
479 uint32_t phdr_index = 1;
Wen Congyang783e9b42012-05-07 12:10:47 +0800480 uint32_t max_index;
481
482 if (s->have_section) {
483 max_index = s->sh_info;
484 } else {
485 max_index = s->phdr_num;
486 }
487
488 QTAILQ_FOREACH(memory_mapping, &s->list.head, next) {
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200489 get_offset_range(memory_mapping->phys_addr,
490 memory_mapping->length,
491 s, &offset, &filesz);
Wen Congyang783e9b42012-05-07 12:10:47 +0800492 if (s->dump_info.d_class == ELFCLASS64) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800493 write_elf64_load(s, memory_mapping, phdr_index++, offset,
Janosch Frank86a518b2022-03-30 12:35:55 +0000494 filesz, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800495 } else {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800496 write_elf32_load(s, memory_mapping, phdr_index++, offset,
Janosch Frank86a518b2022-03-30 12:35:55 +0000497 filesz, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800498 }
499
Janosch Frank86a518b2022-03-30 12:35:55 +0000500 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800501 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800502 }
503
504 if (phdr_index >= max_index) {
505 break;
506 }
507 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800508}
509
510/* write elf header, PT_NOTE and elf note to vmcore. */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800511static void dump_begin(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800512{
Janosch Frank86a518b2022-03-30 12:35:55 +0000513 ERRP_GUARD();
Wen Congyang783e9b42012-05-07 12:10:47 +0800514
515 /*
516 * the vmcore's format is:
517 * --------------
518 * | elf header |
519 * --------------
520 * | PT_NOTE |
521 * --------------
522 * | PT_LOAD |
523 * --------------
524 * | ...... |
525 * --------------
526 * | PT_LOAD |
527 * --------------
528 * | sec_hdr |
529 * --------------
530 * | elf note |
531 * --------------
532 * | memory |
533 * --------------
534 *
535 * we only know where the memory is saved after we write elf note into
536 * vmcore.
537 */
538
539 /* write elf header to vmcore */
540 if (s->dump_info.d_class == ELFCLASS64) {
Janosch Frank86a518b2022-03-30 12:35:55 +0000541 write_elf64_header(s, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800542 } else {
Janosch Frank86a518b2022-03-30 12:35:55 +0000543 write_elf32_header(s, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800544 }
Janosch Frank86a518b2022-03-30 12:35:55 +0000545 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800546 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800547 }
548
549 if (s->dump_info.d_class == ELFCLASS64) {
550 /* write PT_NOTE to vmcore */
Janosch Frank86a518b2022-03-30 12:35:55 +0000551 write_elf64_note(s, errp);
552 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800553 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800554 }
555
556 /* write all PT_LOAD to vmcore */
Janosch Frank86a518b2022-03-30 12:35:55 +0000557 write_elf_loads(s, errp);
558 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800559 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800560 }
561
562 /* write section to vmcore */
563 if (s->have_section) {
Janosch Frank86a518b2022-03-30 12:35:55 +0000564 write_elf_section(s, 1, errp);
565 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800566 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800567 }
568 }
569
570 /* write notes to vmcore */
Janosch Frank86a518b2022-03-30 12:35:55 +0000571 write_elf64_notes(fd_write_vmcore, s, errp);
572 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800573 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800574 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800575 } else {
576 /* write PT_NOTE to vmcore */
Janosch Frank86a518b2022-03-30 12:35:55 +0000577 write_elf32_note(s, errp);
578 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800579 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800580 }
581
582 /* write all PT_LOAD to vmcore */
Janosch Frank86a518b2022-03-30 12:35:55 +0000583 write_elf_loads(s, errp);
584 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800585 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800586 }
587
588 /* write section to vmcore */
589 if (s->have_section) {
Janosch Frank86a518b2022-03-30 12:35:55 +0000590 write_elf_section(s, 0, errp);
591 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800592 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800593 }
594 }
595
596 /* write notes to vmcore */
Janosch Frank86a518b2022-03-30 12:35:55 +0000597 write_elf32_notes(fd_write_vmcore, s, errp);
598 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800599 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800600 }
601 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800602}
603
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200604static int get_next_block(DumpState *s, GuestPhysBlock *block)
Wen Congyang783e9b42012-05-07 12:10:47 +0800605{
606 while (1) {
Paolo Bonzinia3161032012-11-14 15:54:48 +0100607 block = QTAILQ_NEXT(block, next);
Wen Congyang783e9b42012-05-07 12:10:47 +0800608 if (!block) {
609 /* no more block */
610 return 1;
611 }
612
613 s->start = 0;
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200614 s->next_block = block;
Wen Congyang783e9b42012-05-07 12:10:47 +0800615 if (s->has_filter) {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200616 if (block->target_start >= s->begin + s->length ||
617 block->target_end <= s->begin) {
Wen Congyang783e9b42012-05-07 12:10:47 +0800618 /* This block is out of the range */
619 continue;
620 }
621
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200622 if (s->begin > block->target_start) {
623 s->start = s->begin - block->target_start;
Wen Congyang783e9b42012-05-07 12:10:47 +0800624 }
625 }
626
627 return 0;
628 }
629}
630
631/* write all memory to vmcore */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800632static void dump_iterate(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800633{
Janosch Frank86a518b2022-03-30 12:35:55 +0000634 ERRP_GUARD();
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200635 GuestPhysBlock *block;
Wen Congyang783e9b42012-05-07 12:10:47 +0800636 int64_t size;
Wen Congyang783e9b42012-05-07 12:10:47 +0800637
Gonglei08a655b2014-10-30 14:01:17 +0800638 do {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200639 block = s->next_block;
Wen Congyang783e9b42012-05-07 12:10:47 +0800640
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200641 size = block->target_end - block->target_start;
Wen Congyang783e9b42012-05-07 12:10:47 +0800642 if (s->has_filter) {
643 size -= s->start;
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200644 if (s->begin + s->length < block->target_end) {
645 size -= block->target_end - (s->begin + s->length);
Wen Congyang783e9b42012-05-07 12:10:47 +0800646 }
647 }
Janosch Frank86a518b2022-03-30 12:35:55 +0000648 write_memory(s, block, s->start, size, errp);
649 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800650 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800651 }
652
Gonglei08a655b2014-10-30 14:01:17 +0800653 } while (!get_next_block(s, block));
Wen Congyang783e9b42012-05-07 12:10:47 +0800654}
655
zhanghailiang4c7e2512014-10-09 14:13:11 +0800656static void create_vmcore(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800657{
Janosch Frank86a518b2022-03-30 12:35:55 +0000658 ERRP_GUARD();
Wen Congyang783e9b42012-05-07 12:10:47 +0800659
Janosch Frank86a518b2022-03-30 12:35:55 +0000660 dump_begin(s, errp);
661 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800662 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800663 }
664
zhanghailiang4c7e2512014-10-09 14:13:11 +0800665 dump_iterate(s, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800666}
667
qiaonuohanfda05382014-02-18 14:11:27 +0800668static int write_start_flat_header(int fd)
669{
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200670 MakedumpfileHeader *mh;
qiaonuohanfda05382014-02-18 14:11:27 +0800671 int ret = 0;
672
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200673 QEMU_BUILD_BUG_ON(sizeof *mh > MAX_SIZE_MDF_HEADER);
674 mh = g_malloc0(MAX_SIZE_MDF_HEADER);
qiaonuohanfda05382014-02-18 14:11:27 +0800675
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200676 memcpy(mh->signature, MAKEDUMPFILE_SIGNATURE,
677 MIN(sizeof mh->signature, sizeof MAKEDUMPFILE_SIGNATURE));
qiaonuohanfda05382014-02-18 14:11:27 +0800678
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200679 mh->type = cpu_to_be64(TYPE_FLAT_HEADER);
680 mh->version = cpu_to_be64(VERSION_FLAT_HEADER);
qiaonuohanfda05382014-02-18 14:11:27 +0800681
682 size_t written_size;
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200683 written_size = qemu_write_full(fd, mh, MAX_SIZE_MDF_HEADER);
qiaonuohanfda05382014-02-18 14:11:27 +0800684 if (written_size != MAX_SIZE_MDF_HEADER) {
685 ret = -1;
686 }
687
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200688 g_free(mh);
qiaonuohanfda05382014-02-18 14:11:27 +0800689 return ret;
690}
691
692static int write_end_flat_header(int fd)
693{
694 MakedumpfileDataHeader mdh;
695
696 mdh.offset = END_FLAG_FLAT_HEADER;
697 mdh.buf_size = END_FLAG_FLAT_HEADER;
698
699 size_t written_size;
700 written_size = qemu_write_full(fd, &mdh, sizeof(mdh));
701 if (written_size != sizeof(mdh)) {
702 return -1;
703 }
704
705 return 0;
706}
707
qiaonuohan5d31bab2014-02-18 14:11:28 +0800708static int write_buffer(int fd, off_t offset, const void *buf, size_t size)
709{
710 size_t written_size;
711 MakedumpfileDataHeader mdh;
712
713 mdh.offset = cpu_to_be64(offset);
714 mdh.buf_size = cpu_to_be64(size);
715
716 written_size = qemu_write_full(fd, &mdh, sizeof(mdh));
717 if (written_size != sizeof(mdh)) {
718 return -1;
719 }
720
721 written_size = qemu_write_full(fd, buf, size);
722 if (written_size != size) {
723 return -1;
724 }
725
726 return 0;
727}
728
qiaonuohan4835ef72014-02-18 14:11:29 +0800729static int buf_write_note(const void *buf, size_t size, void *opaque)
730{
731 DumpState *s = opaque;
732
733 /* note_buf is not enough */
734 if (s->note_buf_offset + size > s->note_size) {
735 return -1;
736 }
737
738 memcpy(s->note_buf + s->note_buf_offset, buf, size);
739
740 s->note_buf_offset += size;
741
742 return 0;
743}
744
Marc-André Lureau903ef732017-09-11 18:59:25 +0200745/*
746 * This function retrieves various sizes from an elf header.
747 *
748 * @note has to be a valid ELF note. The return sizes are unmodified
749 * (not padded or rounded up to be multiple of 4).
750 */
751static void get_note_sizes(DumpState *s, const void *note,
752 uint64_t *note_head_size,
753 uint64_t *name_size,
754 uint64_t *desc_size)
755{
756 uint64_t note_head_sz;
757 uint64_t name_sz;
758 uint64_t desc_sz;
759
760 if (s->dump_info.d_class == ELFCLASS64) {
761 const Elf64_Nhdr *hdr = note;
762 note_head_sz = sizeof(Elf64_Nhdr);
763 name_sz = tswap64(hdr->n_namesz);
764 desc_sz = tswap64(hdr->n_descsz);
765 } else {
766 const Elf32_Nhdr *hdr = note;
767 note_head_sz = sizeof(Elf32_Nhdr);
768 name_sz = tswap32(hdr->n_namesz);
769 desc_sz = tswap32(hdr->n_descsz);
770 }
771
772 if (note_head_size) {
773 *note_head_size = note_head_sz;
774 }
775 if (name_size) {
776 *name_size = name_sz;
777 }
778 if (desc_size) {
779 *desc_size = desc_sz;
780 }
781}
782
Marc-André Lureaud9feb512017-09-11 18:59:26 +0200783static bool note_name_equal(DumpState *s,
784 const uint8_t *note, const char *name)
785{
786 int len = strlen(name) + 1;
787 uint64_t head_size, name_size;
788
789 get_note_sizes(s, note, &head_size, &name_size, NULL);
790 head_size = ROUND_UP(head_size, 4);
791
Marc-André Lureauc983ca82017-12-12 15:53:59 +0100792 return name_size == len && memcmp(note + head_size, name, len) == 0;
Marc-André Lureaud9feb512017-09-11 18:59:26 +0200793}
794
qiaonuohan298f1162014-02-18 14:11:32 +0800795/* write common header, sub header and elf note to vmcore */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800796static void create_header32(DumpState *s, Error **errp)
qiaonuohan298f1162014-02-18 14:11:32 +0800797{
Janosch Frank86a518b2022-03-30 12:35:55 +0000798 ERRP_GUARD();
qiaonuohan298f1162014-02-18 14:11:32 +0800799 DiskDumpHeader32 *dh = NULL;
800 KdumpSubHeader32 *kh = NULL;
801 size_t size;
qiaonuohan298f1162014-02-18 14:11:32 +0800802 uint32_t block_size;
803 uint32_t sub_hdr_size;
804 uint32_t bitmap_blocks;
805 uint32_t status = 0;
806 uint64_t offset_note;
807
808 /* write common header, the version of kdump-compressed format is 6th */
809 size = sizeof(DiskDumpHeader32);
810 dh = g_malloc0(size);
811
Eric Blake84c868f2018-03-27 15:21:51 -0500812 memcpy(dh->signature, KDUMP_SIGNATURE, SIG_LEN);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200813 dh->header_version = cpu_to_dump32(s, 6);
Andrew Jones8161bef2016-01-11 20:56:20 +0100814 block_size = s->dump_info.page_size;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200815 dh->block_size = cpu_to_dump32(s, block_size);
qiaonuohan298f1162014-02-18 14:11:32 +0800816 sub_hdr_size = sizeof(struct KdumpSubHeader32) + s->note_size;
817 sub_hdr_size = DIV_ROUND_UP(sub_hdr_size, block_size);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200818 dh->sub_hdr_size = cpu_to_dump32(s, sub_hdr_size);
qiaonuohan298f1162014-02-18 14:11:32 +0800819 /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200820 dh->max_mapnr = cpu_to_dump32(s, MIN(s->max_mapnr, UINT_MAX));
821 dh->nr_cpus = cpu_to_dump32(s, s->nr_cpus);
qiaonuohan298f1162014-02-18 14:11:32 +0800822 bitmap_blocks = DIV_ROUND_UP(s->len_dump_bitmap, block_size) * 2;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200823 dh->bitmap_blocks = cpu_to_dump32(s, bitmap_blocks);
qiaonuohan4ab23a92014-02-18 14:11:37 +0800824 strncpy(dh->utsname.machine, ELF_MACHINE_UNAME, sizeof(dh->utsname.machine));
qiaonuohan298f1162014-02-18 14:11:32 +0800825
826 if (s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) {
827 status |= DUMP_DH_COMPRESSED_ZLIB;
828 }
829#ifdef CONFIG_LZO
830 if (s->flag_compress & DUMP_DH_COMPRESSED_LZO) {
831 status |= DUMP_DH_COMPRESSED_LZO;
832 }
833#endif
834#ifdef CONFIG_SNAPPY
835 if (s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) {
836 status |= DUMP_DH_COMPRESSED_SNAPPY;
837 }
838#endif
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200839 dh->status = cpu_to_dump32(s, status);
qiaonuohan298f1162014-02-18 14:11:32 +0800840
841 if (write_buffer(s->fd, 0, dh, size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800842 error_setg(errp, "dump: failed to write disk dump header");
qiaonuohan298f1162014-02-18 14:11:32 +0800843 goto out;
844 }
845
846 /* write sub header */
847 size = sizeof(KdumpSubHeader32);
848 kh = g_malloc0(size);
849
850 /* 64bit max_mapnr_64 */
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200851 kh->max_mapnr_64 = cpu_to_dump64(s, s->max_mapnr);
Andrew Jonesb6e05aa2016-01-11 20:56:21 +0100852 kh->phys_base = cpu_to_dump32(s, s->dump_info.phys_base);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200853 kh->dump_level = cpu_to_dump32(s, DUMP_LEVEL);
qiaonuohan298f1162014-02-18 14:11:32 +0800854
855 offset_note = DISKDUMP_HEADER_BLOCKS * block_size + size;
Marc-André Lureau9ada5752017-09-11 18:59:27 +0200856 if (s->guest_note &&
857 note_name_equal(s, s->guest_note, "VMCOREINFO")) {
858 uint64_t hsize, name_size, size_vmcoreinfo_desc, offset_vmcoreinfo;
859
860 get_note_sizes(s, s->guest_note,
861 &hsize, &name_size, &size_vmcoreinfo_desc);
862 offset_vmcoreinfo = offset_note + s->note_size - s->guest_note_size +
863 (DIV_ROUND_UP(hsize, 4) + DIV_ROUND_UP(name_size, 4)) * 4;
864 kh->offset_vmcoreinfo = cpu_to_dump64(s, offset_vmcoreinfo);
865 kh->size_vmcoreinfo = cpu_to_dump32(s, size_vmcoreinfo_desc);
866 }
867
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200868 kh->offset_note = cpu_to_dump64(s, offset_note);
869 kh->note_size = cpu_to_dump32(s, s->note_size);
qiaonuohan298f1162014-02-18 14:11:32 +0800870
871 if (write_buffer(s->fd, DISKDUMP_HEADER_BLOCKS *
872 block_size, kh, size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800873 error_setg(errp, "dump: failed to write kdump sub header");
qiaonuohan298f1162014-02-18 14:11:32 +0800874 goto out;
875 }
876
877 /* write note */
878 s->note_buf = g_malloc0(s->note_size);
879 s->note_buf_offset = 0;
880
881 /* use s->note_buf to store notes temporarily */
Janosch Frank86a518b2022-03-30 12:35:55 +0000882 write_elf32_notes(buf_write_note, s, errp);
883 if (*errp) {
qiaonuohan298f1162014-02-18 14:11:32 +0800884 goto out;
885 }
qiaonuohan298f1162014-02-18 14:11:32 +0800886 if (write_buffer(s->fd, offset_note, s->note_buf,
887 s->note_size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800888 error_setg(errp, "dump: failed to write notes");
qiaonuohan298f1162014-02-18 14:11:32 +0800889 goto out;
890 }
891
892 /* get offset of dump_bitmap */
893 s->offset_dump_bitmap = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size) *
894 block_size;
895
896 /* get offset of page */
897 s->offset_page = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size + bitmap_blocks) *
898 block_size;
899
900out:
901 g_free(dh);
902 g_free(kh);
903 g_free(s->note_buf);
qiaonuohan298f1162014-02-18 14:11:32 +0800904}
905
906/* write common header, sub header and elf note to vmcore */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800907static void create_header64(DumpState *s, Error **errp)
qiaonuohan298f1162014-02-18 14:11:32 +0800908{
Janosch Frank86a518b2022-03-30 12:35:55 +0000909 ERRP_GUARD();
qiaonuohan298f1162014-02-18 14:11:32 +0800910 DiskDumpHeader64 *dh = NULL;
911 KdumpSubHeader64 *kh = NULL;
912 size_t size;
qiaonuohan298f1162014-02-18 14:11:32 +0800913 uint32_t block_size;
914 uint32_t sub_hdr_size;
915 uint32_t bitmap_blocks;
916 uint32_t status = 0;
917 uint64_t offset_note;
918
919 /* write common header, the version of kdump-compressed format is 6th */
920 size = sizeof(DiskDumpHeader64);
921 dh = g_malloc0(size);
922
Eric Blake84c868f2018-03-27 15:21:51 -0500923 memcpy(dh->signature, KDUMP_SIGNATURE, SIG_LEN);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200924 dh->header_version = cpu_to_dump32(s, 6);
Andrew Jones8161bef2016-01-11 20:56:20 +0100925 block_size = s->dump_info.page_size;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200926 dh->block_size = cpu_to_dump32(s, block_size);
qiaonuohan298f1162014-02-18 14:11:32 +0800927 sub_hdr_size = sizeof(struct KdumpSubHeader64) + s->note_size;
928 sub_hdr_size = DIV_ROUND_UP(sub_hdr_size, block_size);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200929 dh->sub_hdr_size = cpu_to_dump32(s, sub_hdr_size);
qiaonuohan298f1162014-02-18 14:11:32 +0800930 /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200931 dh->max_mapnr = cpu_to_dump32(s, MIN(s->max_mapnr, UINT_MAX));
932 dh->nr_cpus = cpu_to_dump32(s, s->nr_cpus);
qiaonuohan298f1162014-02-18 14:11:32 +0800933 bitmap_blocks = DIV_ROUND_UP(s->len_dump_bitmap, block_size) * 2;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200934 dh->bitmap_blocks = cpu_to_dump32(s, bitmap_blocks);
qiaonuohan4ab23a92014-02-18 14:11:37 +0800935 strncpy(dh->utsname.machine, ELF_MACHINE_UNAME, sizeof(dh->utsname.machine));
qiaonuohan298f1162014-02-18 14:11:32 +0800936
937 if (s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) {
938 status |= DUMP_DH_COMPRESSED_ZLIB;
939 }
940#ifdef CONFIG_LZO
941 if (s->flag_compress & DUMP_DH_COMPRESSED_LZO) {
942 status |= DUMP_DH_COMPRESSED_LZO;
943 }
944#endif
945#ifdef CONFIG_SNAPPY
946 if (s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) {
947 status |= DUMP_DH_COMPRESSED_SNAPPY;
948 }
949#endif
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200950 dh->status = cpu_to_dump32(s, status);
qiaonuohan298f1162014-02-18 14:11:32 +0800951
952 if (write_buffer(s->fd, 0, dh, size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800953 error_setg(errp, "dump: failed to write disk dump header");
qiaonuohan298f1162014-02-18 14:11:32 +0800954 goto out;
955 }
956
957 /* write sub header */
958 size = sizeof(KdumpSubHeader64);
959 kh = g_malloc0(size);
960
961 /* 64bit max_mapnr_64 */
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200962 kh->max_mapnr_64 = cpu_to_dump64(s, s->max_mapnr);
Andrew Jonesb6e05aa2016-01-11 20:56:21 +0100963 kh->phys_base = cpu_to_dump64(s, s->dump_info.phys_base);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200964 kh->dump_level = cpu_to_dump32(s, DUMP_LEVEL);
qiaonuohan298f1162014-02-18 14:11:32 +0800965
966 offset_note = DISKDUMP_HEADER_BLOCKS * block_size + size;
Marc-André Lureau9ada5752017-09-11 18:59:27 +0200967 if (s->guest_note &&
968 note_name_equal(s, s->guest_note, "VMCOREINFO")) {
969 uint64_t hsize, name_size, size_vmcoreinfo_desc, offset_vmcoreinfo;
970
971 get_note_sizes(s, s->guest_note,
972 &hsize, &name_size, &size_vmcoreinfo_desc);
973 offset_vmcoreinfo = offset_note + s->note_size - s->guest_note_size +
974 (DIV_ROUND_UP(hsize, 4) + DIV_ROUND_UP(name_size, 4)) * 4;
975 kh->offset_vmcoreinfo = cpu_to_dump64(s, offset_vmcoreinfo);
976 kh->size_vmcoreinfo = cpu_to_dump64(s, size_vmcoreinfo_desc);
977 }
978
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200979 kh->offset_note = cpu_to_dump64(s, offset_note);
980 kh->note_size = cpu_to_dump64(s, s->note_size);
qiaonuohan298f1162014-02-18 14:11:32 +0800981
982 if (write_buffer(s->fd, DISKDUMP_HEADER_BLOCKS *
983 block_size, kh, size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800984 error_setg(errp, "dump: failed to write kdump sub header");
qiaonuohan298f1162014-02-18 14:11:32 +0800985 goto out;
986 }
987
988 /* write note */
989 s->note_buf = g_malloc0(s->note_size);
990 s->note_buf_offset = 0;
991
992 /* use s->note_buf to store notes temporarily */
Janosch Frank86a518b2022-03-30 12:35:55 +0000993 write_elf64_notes(buf_write_note, s, errp);
994 if (*errp) {
qiaonuohan298f1162014-02-18 14:11:32 +0800995 goto out;
996 }
997
998 if (write_buffer(s->fd, offset_note, s->note_buf,
999 s->note_size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001000 error_setg(errp, "dump: failed to write notes");
qiaonuohan298f1162014-02-18 14:11:32 +08001001 goto out;
1002 }
1003
1004 /* get offset of dump_bitmap */
1005 s->offset_dump_bitmap = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size) *
1006 block_size;
1007
1008 /* get offset of page */
1009 s->offset_page = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size + bitmap_blocks) *
1010 block_size;
1011
1012out:
1013 g_free(dh);
1014 g_free(kh);
1015 g_free(s->note_buf);
qiaonuohan298f1162014-02-18 14:11:32 +08001016}
1017
zhanghailiang4c7e2512014-10-09 14:13:11 +08001018static void write_dump_header(DumpState *s, Error **errp)
qiaonuohan298f1162014-02-18 14:11:32 +08001019{
Laszlo Ersek24aeeac2014-05-20 13:39:45 +02001020 if (s->dump_info.d_class == ELFCLASS32) {
Markus Armbruster992861f2020-07-07 18:06:04 +02001021 create_header32(s, errp);
qiaonuohan298f1162014-02-18 14:11:32 +08001022 } else {
Markus Armbruster992861f2020-07-07 18:06:04 +02001023 create_header64(s, errp);
zhanghailiang4c7e2512014-10-09 14:13:11 +08001024 }
qiaonuohan298f1162014-02-18 14:11:32 +08001025}
1026
Andrew Jones8161bef2016-01-11 20:56:20 +01001027static size_t dump_bitmap_get_bufsize(DumpState *s)
1028{
1029 return s->dump_info.page_size;
1030}
1031
qiaonuohand0686c72014-02-18 14:11:33 +08001032/*
1033 * set dump_bitmap sequencely. the bit before last_pfn is not allowed to be
1034 * rewritten, so if need to set the first bit, set last_pfn and pfn to 0.
1035 * set_dump_bitmap will always leave the recently set bit un-sync. And setting
1036 * (last bit + sizeof(buf) * 8) to 0 will do flushing the content in buf into
1037 * vmcore, ie. synchronizing un-sync bit into vmcore.
1038 */
1039static int set_dump_bitmap(uint64_t last_pfn, uint64_t pfn, bool value,
1040 uint8_t *buf, DumpState *s)
1041{
1042 off_t old_offset, new_offset;
1043 off_t offset_bitmap1, offset_bitmap2;
1044 uint32_t byte, bit;
Andrew Jones8161bef2016-01-11 20:56:20 +01001045 size_t bitmap_bufsize = dump_bitmap_get_bufsize(s);
1046 size_t bits_per_buf = bitmap_bufsize * CHAR_BIT;
qiaonuohand0686c72014-02-18 14:11:33 +08001047
1048 /* should not set the previous place */
1049 assert(last_pfn <= pfn);
1050
1051 /*
1052 * if the bit needed to be set is not cached in buf, flush the data in buf
1053 * to vmcore firstly.
1054 * making new_offset be bigger than old_offset can also sync remained data
1055 * into vmcore.
1056 */
Andrew Jones8161bef2016-01-11 20:56:20 +01001057 old_offset = bitmap_bufsize * (last_pfn / bits_per_buf);
1058 new_offset = bitmap_bufsize * (pfn / bits_per_buf);
qiaonuohand0686c72014-02-18 14:11:33 +08001059
1060 while (old_offset < new_offset) {
1061 /* calculate the offset and write dump_bitmap */
1062 offset_bitmap1 = s->offset_dump_bitmap + old_offset;
1063 if (write_buffer(s->fd, offset_bitmap1, buf,
Andrew Jones8161bef2016-01-11 20:56:20 +01001064 bitmap_bufsize) < 0) {
qiaonuohand0686c72014-02-18 14:11:33 +08001065 return -1;
1066 }
1067
1068 /* dump level 1 is chosen, so 1st and 2nd bitmap are same */
1069 offset_bitmap2 = s->offset_dump_bitmap + s->len_dump_bitmap +
1070 old_offset;
1071 if (write_buffer(s->fd, offset_bitmap2, buf,
Andrew Jones8161bef2016-01-11 20:56:20 +01001072 bitmap_bufsize) < 0) {
qiaonuohand0686c72014-02-18 14:11:33 +08001073 return -1;
1074 }
1075
Andrew Jones8161bef2016-01-11 20:56:20 +01001076 memset(buf, 0, bitmap_bufsize);
1077 old_offset += bitmap_bufsize;
qiaonuohand0686c72014-02-18 14:11:33 +08001078 }
1079
1080 /* get the exact place of the bit in the buf, and set it */
Andrew Jones8161bef2016-01-11 20:56:20 +01001081 byte = (pfn % bits_per_buf) / CHAR_BIT;
1082 bit = (pfn % bits_per_buf) % CHAR_BIT;
qiaonuohand0686c72014-02-18 14:11:33 +08001083 if (value) {
1084 buf[byte] |= 1u << bit;
1085 } else {
1086 buf[byte] &= ~(1u << bit);
1087 }
1088
1089 return 0;
1090}
1091
Andrew Jones8161bef2016-01-11 20:56:20 +01001092static uint64_t dump_paddr_to_pfn(DumpState *s, uint64_t addr)
1093{
1094 int target_page_shift = ctz32(s->dump_info.page_size);
1095
1096 return (addr >> target_page_shift) - ARCH_PFN_OFFSET;
1097}
1098
1099static uint64_t dump_pfn_to_paddr(DumpState *s, uint64_t pfn)
1100{
1101 int target_page_shift = ctz32(s->dump_info.page_size);
1102
1103 return (pfn + ARCH_PFN_OFFSET) << target_page_shift;
1104}
1105
qiaonuohand0686c72014-02-18 14:11:33 +08001106/*
1107 * exam every page and return the page frame number and the address of the page.
1108 * bufptr can be NULL. note: the blocks here is supposed to reflect guest-phys
1109 * blocks, so block->target_start and block->target_end should be interal
1110 * multiples of the target page size.
1111 */
1112static bool get_next_page(GuestPhysBlock **blockptr, uint64_t *pfnptr,
1113 uint8_t **bufptr, DumpState *s)
1114{
1115 GuestPhysBlock *block = *blockptr;
Andrew Jones8161bef2016-01-11 20:56:20 +01001116 hwaddr addr, target_page_mask = ~((hwaddr)s->dump_info.page_size - 1);
qiaonuohand0686c72014-02-18 14:11:33 +08001117 uint8_t *buf;
1118
1119 /* block == NULL means the start of the iteration */
1120 if (!block) {
1121 block = QTAILQ_FIRST(&s->guest_phys_blocks.head);
1122 *blockptr = block;
Andrew Jones8161bef2016-01-11 20:56:20 +01001123 assert((block->target_start & ~target_page_mask) == 0);
1124 assert((block->target_end & ~target_page_mask) == 0);
1125 *pfnptr = dump_paddr_to_pfn(s, block->target_start);
qiaonuohand0686c72014-02-18 14:11:33 +08001126 if (bufptr) {
1127 *bufptr = block->host_addr;
1128 }
1129 return true;
1130 }
1131
1132 *pfnptr = *pfnptr + 1;
Andrew Jones8161bef2016-01-11 20:56:20 +01001133 addr = dump_pfn_to_paddr(s, *pfnptr);
qiaonuohand0686c72014-02-18 14:11:33 +08001134
1135 if ((addr >= block->target_start) &&
Andrew Jones8161bef2016-01-11 20:56:20 +01001136 (addr + s->dump_info.page_size <= block->target_end)) {
qiaonuohand0686c72014-02-18 14:11:33 +08001137 buf = block->host_addr + (addr - block->target_start);
1138 } else {
1139 /* the next page is in the next block */
1140 block = QTAILQ_NEXT(block, next);
1141 *blockptr = block;
1142 if (!block) {
1143 return false;
1144 }
Andrew Jones8161bef2016-01-11 20:56:20 +01001145 assert((block->target_start & ~target_page_mask) == 0);
1146 assert((block->target_end & ~target_page_mask) == 0);
1147 *pfnptr = dump_paddr_to_pfn(s, block->target_start);
qiaonuohand0686c72014-02-18 14:11:33 +08001148 buf = block->host_addr;
1149 }
1150
1151 if (bufptr) {
1152 *bufptr = buf;
1153 }
1154
1155 return true;
1156}
1157
zhanghailiang4c7e2512014-10-09 14:13:11 +08001158static void write_dump_bitmap(DumpState *s, Error **errp)
qiaonuohand0686c72014-02-18 14:11:33 +08001159{
1160 int ret = 0;
1161 uint64_t last_pfn, pfn;
1162 void *dump_bitmap_buf;
1163 size_t num_dumpable;
1164 GuestPhysBlock *block_iter = NULL;
Andrew Jones8161bef2016-01-11 20:56:20 +01001165 size_t bitmap_bufsize = dump_bitmap_get_bufsize(s);
1166 size_t bits_per_buf = bitmap_bufsize * CHAR_BIT;
qiaonuohand0686c72014-02-18 14:11:33 +08001167
1168 /* dump_bitmap_buf is used to store dump_bitmap temporarily */
Andrew Jones8161bef2016-01-11 20:56:20 +01001169 dump_bitmap_buf = g_malloc0(bitmap_bufsize);
qiaonuohand0686c72014-02-18 14:11:33 +08001170
1171 num_dumpable = 0;
1172 last_pfn = 0;
1173
1174 /*
1175 * exam memory page by page, and set the bit in dump_bitmap corresponded
1176 * to the existing page.
1177 */
1178 while (get_next_page(&block_iter, &pfn, NULL, s)) {
1179 ret = set_dump_bitmap(last_pfn, pfn, true, dump_bitmap_buf, s);
1180 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001181 error_setg(errp, "dump: failed to set dump_bitmap");
qiaonuohand0686c72014-02-18 14:11:33 +08001182 goto out;
1183 }
1184
1185 last_pfn = pfn;
1186 num_dumpable++;
1187 }
1188
1189 /*
1190 * set_dump_bitmap will always leave the recently set bit un-sync. Here we
Andrew Jones8161bef2016-01-11 20:56:20 +01001191 * set the remaining bits from last_pfn to the end of the bitmap buffer to
1192 * 0. With those set, the un-sync bit will be synchronized into the vmcore.
qiaonuohand0686c72014-02-18 14:11:33 +08001193 */
1194 if (num_dumpable > 0) {
Andrew Jones8161bef2016-01-11 20:56:20 +01001195 ret = set_dump_bitmap(last_pfn, last_pfn + bits_per_buf, false,
qiaonuohand0686c72014-02-18 14:11:33 +08001196 dump_bitmap_buf, s);
1197 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001198 error_setg(errp, "dump: failed to sync dump_bitmap");
qiaonuohand0686c72014-02-18 14:11:33 +08001199 goto out;
1200 }
1201 }
1202
1203 /* number of dumpable pages that will be dumped later */
1204 s->num_dumpable = num_dumpable;
1205
1206out:
1207 g_free(dump_bitmap_buf);
qiaonuohand0686c72014-02-18 14:11:33 +08001208}
1209
qiaonuohan64cfba62014-02-18 14:11:34 +08001210static void prepare_data_cache(DataCache *data_cache, DumpState *s,
1211 off_t offset)
1212{
1213 data_cache->fd = s->fd;
1214 data_cache->data_size = 0;
Andrew Jones8161bef2016-01-11 20:56:20 +01001215 data_cache->buf_size = 4 * dump_bitmap_get_bufsize(s);
1216 data_cache->buf = g_malloc0(data_cache->buf_size);
qiaonuohan64cfba62014-02-18 14:11:34 +08001217 data_cache->offset = offset;
1218}
1219
1220static int write_cache(DataCache *dc, const void *buf, size_t size,
1221 bool flag_sync)
1222{
1223 /*
1224 * dc->buf_size should not be less than size, otherwise dc will never be
1225 * enough
1226 */
1227 assert(size <= dc->buf_size);
1228
1229 /*
1230 * if flag_sync is set, synchronize data in dc->buf into vmcore.
1231 * otherwise check if the space is enough for caching data in buf, if not,
1232 * write the data in dc->buf to dc->fd and reset dc->buf
1233 */
1234 if ((!flag_sync && dc->data_size + size > dc->buf_size) ||
1235 (flag_sync && dc->data_size > 0)) {
1236 if (write_buffer(dc->fd, dc->offset, dc->buf, dc->data_size) < 0) {
1237 return -1;
1238 }
1239
1240 dc->offset += dc->data_size;
1241 dc->data_size = 0;
1242 }
1243
1244 if (!flag_sync) {
1245 memcpy(dc->buf + dc->data_size, buf, size);
1246 dc->data_size += size;
1247 }
1248
1249 return 0;
1250}
1251
1252static void free_data_cache(DataCache *data_cache)
1253{
1254 g_free(data_cache->buf);
1255}
1256
qiaonuohand12f57e2014-02-18 14:11:35 +08001257static size_t get_len_buf_out(size_t page_size, uint32_t flag_compress)
1258{
Laszlo Ersekb87ef352014-05-20 13:42:53 +02001259 switch (flag_compress) {
1260 case DUMP_DH_COMPRESSED_ZLIB:
1261 return compressBound(page_size);
qiaonuohand12f57e2014-02-18 14:11:35 +08001262
Laszlo Ersekb87ef352014-05-20 13:42:53 +02001263 case DUMP_DH_COMPRESSED_LZO:
1264 /*
1265 * LZO will expand incompressible data by a little amount. Please check
1266 * the following URL to see the expansion calculation:
1267 * http://www.oberhumer.com/opensource/lzo/lzofaq.php
1268 */
1269 return page_size + page_size / 16 + 64 + 3;
qiaonuohand12f57e2014-02-18 14:11:35 +08001270
1271#ifdef CONFIG_SNAPPY
Laszlo Ersekb87ef352014-05-20 13:42:53 +02001272 case DUMP_DH_COMPRESSED_SNAPPY:
1273 return snappy_max_compressed_length(page_size);
qiaonuohand12f57e2014-02-18 14:11:35 +08001274#endif
Laszlo Ersekb87ef352014-05-20 13:42:53 +02001275 }
1276 return 0;
qiaonuohand12f57e2014-02-18 14:11:35 +08001277}
1278
zhanghailiang4c7e2512014-10-09 14:13:11 +08001279static void write_dump_pages(DumpState *s, Error **errp)
qiaonuohand12f57e2014-02-18 14:11:35 +08001280{
1281 int ret = 0;
1282 DataCache page_desc, page_data;
1283 size_t len_buf_out, size_out;
1284#ifdef CONFIG_LZO
1285 lzo_bytep wrkmem = NULL;
1286#endif
1287 uint8_t *buf_out = NULL;
1288 off_t offset_desc, offset_data;
1289 PageDescriptor pd, pd_zero;
1290 uint8_t *buf;
qiaonuohand12f57e2014-02-18 14:11:35 +08001291 GuestPhysBlock *block_iter = NULL;
1292 uint64_t pfn_iter;
1293
1294 /* get offset of page_desc and page_data in dump file */
1295 offset_desc = s->offset_page;
1296 offset_data = offset_desc + sizeof(PageDescriptor) * s->num_dumpable;
1297
1298 prepare_data_cache(&page_desc, s, offset_desc);
1299 prepare_data_cache(&page_data, s, offset_data);
1300
1301 /* prepare buffer to store compressed data */
Andrew Jones8161bef2016-01-11 20:56:20 +01001302 len_buf_out = get_len_buf_out(s->dump_info.page_size, s->flag_compress);
Laszlo Ersekb87ef352014-05-20 13:42:53 +02001303 assert(len_buf_out != 0);
qiaonuohand12f57e2014-02-18 14:11:35 +08001304
1305#ifdef CONFIG_LZO
1306 wrkmem = g_malloc(LZO1X_1_MEM_COMPRESS);
1307#endif
1308
1309 buf_out = g_malloc(len_buf_out);
1310
1311 /*
1312 * init zero page's page_desc and page_data, because every zero page
1313 * uses the same page_data
1314 */
Andrew Jones8161bef2016-01-11 20:56:20 +01001315 pd_zero.size = cpu_to_dump32(s, s->dump_info.page_size);
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001316 pd_zero.flags = cpu_to_dump32(s, 0);
1317 pd_zero.offset = cpu_to_dump64(s, offset_data);
1318 pd_zero.page_flags = cpu_to_dump64(s, 0);
Andrew Jones8161bef2016-01-11 20:56:20 +01001319 buf = g_malloc0(s->dump_info.page_size);
1320 ret = write_cache(&page_data, buf, s->dump_info.page_size, false);
qiaonuohand12f57e2014-02-18 14:11:35 +08001321 g_free(buf);
1322 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001323 error_setg(errp, "dump: failed to write page data (zero page)");
qiaonuohand12f57e2014-02-18 14:11:35 +08001324 goto out;
1325 }
1326
Andrew Jones8161bef2016-01-11 20:56:20 +01001327 offset_data += s->dump_info.page_size;
qiaonuohand12f57e2014-02-18 14:11:35 +08001328
1329 /*
1330 * dump memory to vmcore page by page. zero page will all be resided in the
1331 * first page of page section
1332 */
1333 while (get_next_page(&block_iter, &pfn_iter, &buf, s)) {
1334 /* check zero page */
Juan Quintelaf13f22b2021-11-18 15:57:44 +01001335 if (buffer_is_zero(buf, s->dump_info.page_size)) {
qiaonuohand12f57e2014-02-18 14:11:35 +08001336 ret = write_cache(&page_desc, &pd_zero, sizeof(PageDescriptor),
1337 false);
1338 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001339 error_setg(errp, "dump: failed to write page desc");
qiaonuohand12f57e2014-02-18 14:11:35 +08001340 goto out;
1341 }
1342 } else {
1343 /*
1344 * not zero page, then:
1345 * 1. compress the page
1346 * 2. write the compressed page into the cache of page_data
1347 * 3. get page desc of the compressed page and write it into the
1348 * cache of page_desc
1349 *
1350 * only one compression format will be used here, for
1351 * s->flag_compress is set. But when compression fails to work,
1352 * we fall back to save in plaintext.
1353 */
1354 size_out = len_buf_out;
1355 if ((s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) &&
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001356 (compress2(buf_out, (uLongf *)&size_out, buf,
Andrew Jones8161bef2016-01-11 20:56:20 +01001357 s->dump_info.page_size, Z_BEST_SPEED) == Z_OK) &&
1358 (size_out < s->dump_info.page_size)) {
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001359 pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_ZLIB);
1360 pd.size = cpu_to_dump32(s, size_out);
qiaonuohand12f57e2014-02-18 14:11:35 +08001361
1362 ret = write_cache(&page_data, buf_out, size_out, false);
1363 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001364 error_setg(errp, "dump: failed to write page data");
qiaonuohand12f57e2014-02-18 14:11:35 +08001365 goto out;
1366 }
1367#ifdef CONFIG_LZO
1368 } else if ((s->flag_compress & DUMP_DH_COMPRESSED_LZO) &&
Andrew Jones8161bef2016-01-11 20:56:20 +01001369 (lzo1x_1_compress(buf, s->dump_info.page_size, buf_out,
qiaonuohand12f57e2014-02-18 14:11:35 +08001370 (lzo_uint *)&size_out, wrkmem) == LZO_E_OK) &&
Andrew Jones8161bef2016-01-11 20:56:20 +01001371 (size_out < s->dump_info.page_size)) {
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001372 pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_LZO);
1373 pd.size = cpu_to_dump32(s, size_out);
qiaonuohand12f57e2014-02-18 14:11:35 +08001374
1375 ret = write_cache(&page_data, buf_out, size_out, false);
1376 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001377 error_setg(errp, "dump: failed to write page data");
qiaonuohand12f57e2014-02-18 14:11:35 +08001378 goto out;
1379 }
1380#endif
1381#ifdef CONFIG_SNAPPY
1382 } else if ((s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) &&
Andrew Jones8161bef2016-01-11 20:56:20 +01001383 (snappy_compress((char *)buf, s->dump_info.page_size,
qiaonuohand12f57e2014-02-18 14:11:35 +08001384 (char *)buf_out, &size_out) == SNAPPY_OK) &&
Andrew Jones8161bef2016-01-11 20:56:20 +01001385 (size_out < s->dump_info.page_size)) {
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001386 pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_SNAPPY);
1387 pd.size = cpu_to_dump32(s, size_out);
qiaonuohand12f57e2014-02-18 14:11:35 +08001388
1389 ret = write_cache(&page_data, buf_out, size_out, false);
1390 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001391 error_setg(errp, "dump: failed to write page data");
qiaonuohand12f57e2014-02-18 14:11:35 +08001392 goto out;
1393 }
1394#endif
1395 } else {
1396 /*
1397 * fall back to save in plaintext, size_out should be
Andrew Jones8161bef2016-01-11 20:56:20 +01001398 * assigned the target's page size
qiaonuohand12f57e2014-02-18 14:11:35 +08001399 */
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001400 pd.flags = cpu_to_dump32(s, 0);
Andrew Jones8161bef2016-01-11 20:56:20 +01001401 size_out = s->dump_info.page_size;
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001402 pd.size = cpu_to_dump32(s, size_out);
qiaonuohand12f57e2014-02-18 14:11:35 +08001403
Andrew Jones8161bef2016-01-11 20:56:20 +01001404 ret = write_cache(&page_data, buf,
1405 s->dump_info.page_size, false);
qiaonuohand12f57e2014-02-18 14:11:35 +08001406 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001407 error_setg(errp, "dump: failed to write page data");
qiaonuohand12f57e2014-02-18 14:11:35 +08001408 goto out;
1409 }
1410 }
1411
1412 /* get and write page desc here */
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001413 pd.page_flags = cpu_to_dump64(s, 0);
1414 pd.offset = cpu_to_dump64(s, offset_data);
qiaonuohand12f57e2014-02-18 14:11:35 +08001415 offset_data += size_out;
1416
1417 ret = write_cache(&page_desc, &pd, sizeof(PageDescriptor), false);
1418 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001419 error_setg(errp, "dump: failed to write page desc");
qiaonuohand12f57e2014-02-18 14:11:35 +08001420 goto out;
1421 }
1422 }
Peter Xu2264c2c2016-02-18 13:16:53 +08001423 s->written_size += s->dump_info.page_size;
qiaonuohand12f57e2014-02-18 14:11:35 +08001424 }
1425
1426 ret = write_cache(&page_desc, NULL, 0, true);
1427 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001428 error_setg(errp, "dump: failed to sync cache for page_desc");
qiaonuohand12f57e2014-02-18 14:11:35 +08001429 goto out;
1430 }
1431 ret = write_cache(&page_data, NULL, 0, true);
1432 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001433 error_setg(errp, "dump: failed to sync cache for page_data");
qiaonuohand12f57e2014-02-18 14:11:35 +08001434 goto out;
1435 }
1436
1437out:
1438 free_data_cache(&page_desc);
1439 free_data_cache(&page_data);
1440
1441#ifdef CONFIG_LZO
1442 g_free(wrkmem);
1443#endif
1444
1445 g_free(buf_out);
qiaonuohand12f57e2014-02-18 14:11:35 +08001446}
1447
zhanghailiang4c7e2512014-10-09 14:13:11 +08001448static void create_kdump_vmcore(DumpState *s, Error **errp)
qiaonuohanb53ccc32014-02-18 14:11:36 +08001449{
Janosch Frank86a518b2022-03-30 12:35:55 +00001450 ERRP_GUARD();
qiaonuohanb53ccc32014-02-18 14:11:36 +08001451 int ret;
1452
1453 /*
1454 * the kdump-compressed format is:
1455 * File offset
1456 * +------------------------------------------+ 0x0
1457 * | main header (struct disk_dump_header) |
1458 * |------------------------------------------+ block 1
1459 * | sub header (struct kdump_sub_header) |
1460 * |------------------------------------------+ block 2
1461 * | 1st-dump_bitmap |
1462 * |------------------------------------------+ block 2 + X blocks
1463 * | 2nd-dump_bitmap | (aligned by block)
1464 * |------------------------------------------+ block 2 + 2 * X blocks
1465 * | page desc for pfn 0 (struct page_desc) | (aligned by block)
1466 * | page desc for pfn 1 (struct page_desc) |
1467 * | : |
1468 * |------------------------------------------| (not aligned by block)
1469 * | page data (pfn 0) |
1470 * | page data (pfn 1) |
1471 * | : |
1472 * +------------------------------------------+
1473 */
1474
1475 ret = write_start_flat_header(s->fd);
1476 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001477 error_setg(errp, "dump: failed to write start flat header");
zhanghailiang4c7e2512014-10-09 14:13:11 +08001478 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001479 }
1480
Janosch Frank86a518b2022-03-30 12:35:55 +00001481 write_dump_header(s, errp);
1482 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +08001483 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001484 }
1485
Janosch Frank86a518b2022-03-30 12:35:55 +00001486 write_dump_bitmap(s, errp);
1487 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +08001488 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001489 }
1490
Janosch Frank86a518b2022-03-30 12:35:55 +00001491 write_dump_pages(s, errp);
1492 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +08001493 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001494 }
1495
1496 ret = write_end_flat_header(s->fd);
1497 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001498 error_setg(errp, "dump: failed to write end flat header");
zhanghailiang4c7e2512014-10-09 14:13:11 +08001499 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001500 }
qiaonuohanb53ccc32014-02-18 14:11:36 +08001501}
1502
Wen Congyang783e9b42012-05-07 12:10:47 +08001503static ram_addr_t get_start_block(DumpState *s)
1504{
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001505 GuestPhysBlock *block;
Wen Congyang783e9b42012-05-07 12:10:47 +08001506
1507 if (!s->has_filter) {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001508 s->next_block = QTAILQ_FIRST(&s->guest_phys_blocks.head);
Wen Congyang783e9b42012-05-07 12:10:47 +08001509 return 0;
1510 }
1511
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001512 QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
1513 if (block->target_start >= s->begin + s->length ||
1514 block->target_end <= s->begin) {
Wen Congyang783e9b42012-05-07 12:10:47 +08001515 /* This block is out of the range */
1516 continue;
1517 }
1518
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001519 s->next_block = block;
1520 if (s->begin > block->target_start) {
1521 s->start = s->begin - block->target_start;
Wen Congyang783e9b42012-05-07 12:10:47 +08001522 } else {
1523 s->start = 0;
1524 }
1525 return s->start;
1526 }
1527
1528 return -1;
1529}
1530
qiaonuohan7aad2482014-02-18 14:11:31 +08001531static void get_max_mapnr(DumpState *s)
1532{
1533 GuestPhysBlock *last_block;
1534
Paolo Bonzinieae3eb32018-12-06 13:10:34 +01001535 last_block = QTAILQ_LAST(&s->guest_phys_blocks.head);
Andrew Jones8161bef2016-01-11 20:56:20 +01001536 s->max_mapnr = dump_paddr_to_pfn(s, last_block->target_end);
qiaonuohan7aad2482014-02-18 14:11:31 +08001537}
1538
Peter Xubaf28f52016-02-18 13:16:48 +08001539static DumpState dump_state_global = { .status = DUMP_STATUS_NONE };
1540
1541static void dump_state_prepare(DumpState *s)
1542{
1543 /* zero the struct, setting status to active */
1544 *s = (DumpState) { .status = DUMP_STATUS_ACTIVE };
1545}
1546
Marc-André Lureau544803c2022-03-23 19:57:31 +04001547bool qemu_system_dump_in_progress(void)
Peter Xu65d64f32016-02-18 13:16:49 +08001548{
1549 DumpState *state = &dump_state_global;
Stefan Hajnoczid73415a2020-09-23 11:56:46 +01001550 return (qatomic_read(&state->status) == DUMP_STATUS_ACTIVE);
Peter Xu65d64f32016-02-18 13:16:49 +08001551}
1552
Peter Xu2264c2c2016-02-18 13:16:53 +08001553/* calculate total size of memory to be dumped (taking filter into
1554 * acoount.) */
1555static int64_t dump_calculate_size(DumpState *s)
1556{
1557 GuestPhysBlock *block;
1558 int64_t size = 0, total = 0, left = 0, right = 0;
1559
1560 QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
1561 if (s->has_filter) {
1562 /* calculate the overlapped region. */
1563 left = MAX(s->begin, block->target_start);
1564 right = MIN(s->begin + s->length, block->target_end);
1565 size = right - left;
1566 size = size > 0 ? size : 0;
1567 } else {
1568 /* count the whole region in */
1569 size = (block->target_end - block->target_start);
1570 }
1571 total += size;
1572 }
1573
1574 return total;
1575}
1576
Marc-André Lureaud9feb512017-09-11 18:59:26 +02001577static void vmcoreinfo_update_phys_base(DumpState *s)
1578{
1579 uint64_t size, note_head_size, name_size, phys_base;
1580 char **lines;
1581 uint8_t *vmci;
1582 size_t i;
1583
1584 if (!note_name_equal(s, s->guest_note, "VMCOREINFO")) {
1585 return;
1586 }
1587
1588 get_note_sizes(s, s->guest_note, &note_head_size, &name_size, &size);
1589 note_head_size = ROUND_UP(note_head_size, 4);
1590
1591 vmci = s->guest_note + note_head_size + ROUND_UP(name_size, 4);
1592 *(vmci + size) = '\0';
1593
1594 lines = g_strsplit((char *)vmci, "\n", -1);
1595 for (i = 0; lines[i]; i++) {
Wei Huang68cbecf2018-03-09 12:03:23 -05001596 const char *prefix = NULL;
1597
1598 if (s->dump_info.d_machine == EM_X86_64) {
1599 prefix = "NUMBER(phys_base)=";
1600 } else if (s->dump_info.d_machine == EM_AARCH64) {
1601 prefix = "NUMBER(PHYS_OFFSET)=";
1602 }
1603
1604 if (prefix && g_str_has_prefix(lines[i], prefix)) {
1605 if (qemu_strtou64(lines[i] + strlen(prefix), NULL, 16,
Marc-André Lureaud9feb512017-09-11 18:59:26 +02001606 &phys_base) < 0) {
Wei Huang68cbecf2018-03-09 12:03:23 -05001607 warn_report("Failed to read %s", prefix);
Marc-André Lureaud9feb512017-09-11 18:59:26 +02001608 } else {
1609 s->dump_info.phys_base = phys_base;
1610 }
1611 break;
1612 }
1613 }
1614
1615 g_strfreev(lines);
1616}
1617
zhanghailiang4c7e2512014-10-09 14:13:11 +08001618static void dump_init(DumpState *s, int fd, bool has_format,
1619 DumpGuestMemoryFormat format, bool paging, bool has_filter,
1620 int64_t begin, int64_t length, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +08001621{
Janosch Frank86a518b2022-03-30 12:35:55 +00001622 ERRP_GUARD();
Marc-André Lureau903ef732017-09-11 18:59:25 +02001623 VMCoreInfoState *vmci = vmcoreinfo_find();
Andreas Färber182735e2013-05-29 22:29:20 +02001624 CPUState *cpu;
Wen Congyang783e9b42012-05-07 12:10:47 +08001625 int nr_cpus;
1626 int ret;
1627
Peter Xuca1fc8c2016-02-18 13:16:50 +08001628 s->has_format = has_format;
1629 s->format = format;
Peter Xu2264c2c2016-02-18 13:16:53 +08001630 s->written_size = 0;
Peter Xuca1fc8c2016-02-18 13:16:50 +08001631
qiaonuohanb53ccc32014-02-18 14:11:36 +08001632 /* kdump-compressed is conflict with paging and filter */
1633 if (has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
1634 assert(!paging && !has_filter);
1635 }
1636
Wen Congyang783e9b42012-05-07 12:10:47 +08001637 if (runstate_is_running()) {
1638 vm_stop(RUN_STATE_SAVE_VM);
1639 s->resume = true;
1640 } else {
1641 s->resume = false;
1642 }
1643
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001644 /* If we use KVM, we should synchronize the registers before we get dump
1645 * info or physmap info.
Wen Congyang783e9b42012-05-07 12:10:47 +08001646 */
Andreas Färber1b3509c2013-06-09 16:48:29 +02001647 cpu_synchronize_all_states();
Wen Congyang783e9b42012-05-07 12:10:47 +08001648 nr_cpus = 0;
Andreas Färberbdc44642013-06-24 23:50:24 +02001649 CPU_FOREACH(cpu) {
Wen Congyang783e9b42012-05-07 12:10:47 +08001650 nr_cpus++;
1651 }
1652
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001653 s->fd = fd;
1654 s->has_filter = has_filter;
1655 s->begin = begin;
1656 s->length = length;
1657
Chen Gang29282072014-08-03 23:28:56 +08001658 memory_mapping_list_init(&s->list);
1659
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001660 guest_phys_blocks_init(&s->guest_phys_blocks);
Laszlo Ersekc5d7f602013-08-06 12:37:10 +02001661 guest_phys_blocks_append(&s->guest_phys_blocks);
Peter Xu2264c2c2016-02-18 13:16:53 +08001662 s->total_size = dump_calculate_size(s);
1663#ifdef DEBUG_DUMP_GUEST_MEMORY
1664 fprintf(stderr, "DUMP: total memory to dump: %lu\n", s->total_size);
1665#endif
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001666
Cornelia Huckd1e69942017-09-13 16:20:35 +02001667 /* it does not make sense to dump non-existent memory */
1668 if (!s->total_size) {
1669 error_setg(errp, "dump: no guest memory to dump");
1670 goto cleanup;
1671 }
1672
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001673 s->start = get_start_block(s);
1674 if (s->start == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001675 error_setg(errp, QERR_INVALID_PARAMETER, "begin");
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001676 goto cleanup;
1677 }
1678
1679 /* get dump info: endian, class and architecture.
1680 * If the target architecture is not supported, cpu_get_dump_info() will
1681 * return -1.
1682 */
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001683 ret = cpu_get_dump_info(&s->dump_info, &s->guest_phys_blocks);
Wen Congyang783e9b42012-05-07 12:10:47 +08001684 if (ret < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001685 error_setg(errp, QERR_UNSUPPORTED);
Wen Congyang783e9b42012-05-07 12:10:47 +08001686 goto cleanup;
1687 }
1688
Andrew Jones8161bef2016-01-11 20:56:20 +01001689 if (!s->dump_info.page_size) {
1690 s->dump_info.page_size = TARGET_PAGE_SIZE;
1691 }
1692
Paolo Bonzini4720bd02012-06-07 08:48:09 +02001693 s->note_size = cpu_get_note_size(s->dump_info.d_class,
1694 s->dump_info.d_machine, nr_cpus);
Aneesh Kumar K.Vbb6b6842013-10-01 21:49:32 +05301695 if (s->note_size < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001696 error_setg(errp, QERR_UNSUPPORTED);
Paolo Bonzini4720bd02012-06-07 08:48:09 +02001697 goto cleanup;
1698 }
1699
Marc-André Lureau903ef732017-09-11 18:59:25 +02001700 /*
Marc-André Lureaud9feb512017-09-11 18:59:26 +02001701 * The goal of this block is to (a) update the previously guessed
1702 * phys_base, (b) copy the guest note out of the guest.
1703 * Failure to do so is not fatal for dumping.
Marc-André Lureau903ef732017-09-11 18:59:25 +02001704 */
1705 if (vmci) {
1706 uint64_t addr, note_head_size, name_size, desc_size;
1707 uint32_t size;
1708 uint16_t format;
1709
1710 note_head_size = s->dump_info.d_class == ELFCLASS32 ?
1711 sizeof(Elf32_Nhdr) : sizeof(Elf64_Nhdr);
1712
1713 format = le16_to_cpu(vmci->vmcoreinfo.guest_format);
1714 size = le32_to_cpu(vmci->vmcoreinfo.size);
1715 addr = le64_to_cpu(vmci->vmcoreinfo.paddr);
1716 if (!vmci->has_vmcoreinfo) {
1717 warn_report("guest note is not present");
1718 } else if (size < note_head_size || size > MAX_GUEST_NOTE_SIZE) {
1719 warn_report("guest note size is invalid: %" PRIu32, size);
Marc-André Lureau5be5df72018-08-17 17:59:10 +02001720 } else if (format != FW_CFG_VMCOREINFO_FORMAT_ELF) {
Marc-André Lureau903ef732017-09-11 18:59:25 +02001721 warn_report("guest note format is unsupported: %" PRIu16, format);
1722 } else {
1723 s->guest_note = g_malloc(size + 1); /* +1 for adding \0 */
1724 cpu_physical_memory_read(addr, s->guest_note, size);
1725
1726 get_note_sizes(s, s->guest_note, NULL, &name_size, &desc_size);
1727 s->guest_note_size = ELF_NOTE_SIZE(note_head_size, name_size,
1728 desc_size);
1729 if (name_size > MAX_GUEST_NOTE_SIZE ||
1730 desc_size > MAX_GUEST_NOTE_SIZE ||
1731 s->guest_note_size > size) {
1732 warn_report("Invalid guest note header");
1733 g_free(s->guest_note);
1734 s->guest_note = NULL;
1735 } else {
Marc-André Lureaud9feb512017-09-11 18:59:26 +02001736 vmcoreinfo_update_phys_base(s);
Marc-André Lureau903ef732017-09-11 18:59:25 +02001737 s->note_size += s->guest_note_size;
1738 }
1739 }
1740 }
1741
Wen Congyang783e9b42012-05-07 12:10:47 +08001742 /* get memory mapping */
Wen Congyang783e9b42012-05-07 12:10:47 +08001743 if (paging) {
Janosch Frank86a518b2022-03-30 12:35:55 +00001744 qemu_get_guest_memory_mapping(&s->list, &s->guest_phys_blocks, errp);
1745 if (*errp) {
Andreas Färber11ed09c2013-05-29 21:54:03 +02001746 goto cleanup;
1747 }
Wen Congyang783e9b42012-05-07 12:10:47 +08001748 } else {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001749 qemu_get_guest_simple_memory_mapping(&s->list, &s->guest_phys_blocks);
Wen Congyang783e9b42012-05-07 12:10:47 +08001750 }
1751
qiaonuohan7aad2482014-02-18 14:11:31 +08001752 s->nr_cpus = nr_cpus;
qiaonuohan7aad2482014-02-18 14:11:31 +08001753
1754 get_max_mapnr(s);
1755
1756 uint64_t tmp;
Andrew Jones8161bef2016-01-11 20:56:20 +01001757 tmp = DIV_ROUND_UP(DIV_ROUND_UP(s->max_mapnr, CHAR_BIT),
1758 s->dump_info.page_size);
1759 s->len_dump_bitmap = tmp * s->dump_info.page_size;
qiaonuohan7aad2482014-02-18 14:11:31 +08001760
qiaonuohanb53ccc32014-02-18 14:11:36 +08001761 /* init for kdump-compressed format */
1762 if (has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
1763 switch (format) {
1764 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB:
1765 s->flag_compress = DUMP_DH_COMPRESSED_ZLIB;
1766 break;
1767
1768 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO:
Laszlo Ersekc998acb2014-05-20 13:42:22 +02001769#ifdef CONFIG_LZO
1770 if (lzo_init() != LZO_E_OK) {
1771 error_setg(errp, "failed to initialize the LZO library");
1772 goto cleanup;
1773 }
1774#endif
qiaonuohanb53ccc32014-02-18 14:11:36 +08001775 s->flag_compress = DUMP_DH_COMPRESSED_LZO;
1776 break;
1777
1778 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY:
1779 s->flag_compress = DUMP_DH_COMPRESSED_SNAPPY;
1780 break;
1781
1782 default:
1783 s->flag_compress = 0;
1784 }
1785
zhanghailiang4c7e2512014-10-09 14:13:11 +08001786 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001787 }
1788
Wen Congyang783e9b42012-05-07 12:10:47 +08001789 if (s->has_filter) {
1790 memory_mapping_filter(&s->list, s->begin, s->length);
1791 }
1792
1793 /*
1794 * calculate phdr_num
1795 *
1796 * the type of ehdr->e_phnum is uint16_t, so we should avoid overflow
1797 */
1798 s->phdr_num = 1; /* PT_NOTE */
1799 if (s->list.num < UINT16_MAX - 2) {
1800 s->phdr_num += s->list.num;
1801 s->have_section = false;
1802 } else {
1803 s->have_section = true;
1804 s->phdr_num = PN_XNUM;
1805 s->sh_info = 1; /* PT_NOTE */
1806
1807 /* the type of shdr->sh_info is uint32_t, so we should avoid overflow */
1808 if (s->list.num <= UINT32_MAX - 1) {
1809 s->sh_info += s->list.num;
1810 } else {
1811 s->sh_info = UINT32_MAX;
1812 }
1813 }
1814
Wen Congyang783e9b42012-05-07 12:10:47 +08001815 if (s->dump_info.d_class == ELFCLASS64) {
1816 if (s->have_section) {
1817 s->memory_offset = sizeof(Elf64_Ehdr) +
1818 sizeof(Elf64_Phdr) * s->sh_info +
1819 sizeof(Elf64_Shdr) + s->note_size;
1820 } else {
1821 s->memory_offset = sizeof(Elf64_Ehdr) +
1822 sizeof(Elf64_Phdr) * s->phdr_num + s->note_size;
1823 }
1824 } else {
1825 if (s->have_section) {
1826 s->memory_offset = sizeof(Elf32_Ehdr) +
1827 sizeof(Elf32_Phdr) * s->sh_info +
1828 sizeof(Elf32_Shdr) + s->note_size;
1829 } else {
1830 s->memory_offset = sizeof(Elf32_Ehdr) +
1831 sizeof(Elf32_Phdr) * s->phdr_num + s->note_size;
1832 }
1833 }
1834
zhanghailiang4c7e2512014-10-09 14:13:11 +08001835 return;
Wen Congyang783e9b42012-05-07 12:10:47 +08001836
1837cleanup:
Chen Gang29282072014-08-03 23:28:56 +08001838 dump_cleanup(s);
Wen Congyang783e9b42012-05-07 12:10:47 +08001839}
1840
Peter Xuca1fc8c2016-02-18 13:16:50 +08001841/* this operation might be time consuming. */
1842static void dump_process(DumpState *s, Error **errp)
1843{
Janosch Frank86a518b2022-03-30 12:35:55 +00001844 ERRP_GUARD();
Peter Xud42a0d12016-02-18 13:16:56 +08001845 DumpQueryResult *result = NULL;
Peter Xuca1fc8c2016-02-18 13:16:50 +08001846
Viktor Prutyanov2da91b52018-05-17 19:23:39 +03001847 if (s->has_format && s->format == DUMP_GUEST_MEMORY_FORMAT_WIN_DMP) {
1848#ifdef TARGET_X86_64
Janosch Frank86a518b2022-03-30 12:35:55 +00001849 create_win_dump(s, errp);
Viktor Prutyanov2da91b52018-05-17 19:23:39 +03001850#endif
1851 } else if (s->has_format && s->format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
Janosch Frank86a518b2022-03-30 12:35:55 +00001852 create_kdump_vmcore(s, errp);
Peter Xuca1fc8c2016-02-18 13:16:50 +08001853 } else {
Janosch Frank86a518b2022-03-30 12:35:55 +00001854 create_vmcore(s, errp);
Peter Xuca1fc8c2016-02-18 13:16:50 +08001855 }
1856
Peter Xu39ba2ea2016-02-18 13:16:54 +08001857 /* make sure status is written after written_size updates */
1858 smp_wmb();
Stefan Hajnoczid73415a2020-09-23 11:56:46 +01001859 qatomic_set(&s->status,
Janosch Frank86a518b2022-03-30 12:35:55 +00001860 (*errp ? DUMP_STATUS_FAILED : DUMP_STATUS_COMPLETED));
Peter Xuca1fc8c2016-02-18 13:16:50 +08001861
Peter Xud42a0d12016-02-18 13:16:56 +08001862 /* send DUMP_COMPLETED message (unconditionally) */
1863 result = qmp_query_dump(NULL);
1864 /* should never fail */
1865 assert(result);
Janosch Frank86a518b2022-03-30 12:35:55 +00001866 qapi_event_send_dump_completed(result, !!*errp, (*errp ?
1867 error_get_pretty(*errp) : NULL));
Peter Xud42a0d12016-02-18 13:16:56 +08001868 qapi_free_DumpQueryResult(result);
1869
Peter Xuca1fc8c2016-02-18 13:16:50 +08001870 dump_cleanup(s);
1871}
1872
Peter Xu1fbeff72016-02-18 13:16:52 +08001873static void *dump_thread(void *data)
1874{
Peter Xu1fbeff72016-02-18 13:16:52 +08001875 DumpState *s = (DumpState *)data;
Alberto Garciac3a8fe32017-08-29 15:08:36 +03001876 dump_process(s, NULL);
Peter Xu1fbeff72016-02-18 13:16:52 +08001877 return NULL;
1878}
1879
Peter Xu39ba2ea2016-02-18 13:16:54 +08001880DumpQueryResult *qmp_query_dump(Error **errp)
1881{
1882 DumpQueryResult *result = g_new(DumpQueryResult, 1);
1883 DumpState *state = &dump_state_global;
Stefan Hajnoczid73415a2020-09-23 11:56:46 +01001884 result->status = qatomic_read(&state->status);
Peter Xu39ba2ea2016-02-18 13:16:54 +08001885 /* make sure we are reading status and written_size in order */
1886 smp_rmb();
1887 result->completed = state->written_size;
1888 result->total = state->total_size;
1889 return result;
1890}
1891
Peter Xu228de9c2016-02-18 13:16:47 +08001892void qmp_dump_guest_memory(bool paging, const char *file,
1893 bool has_detach, bool detach,
1894 bool has_begin, int64_t begin, bool has_length,
qiaonuohanb53ccc32014-02-18 14:11:36 +08001895 int64_t length, bool has_format,
1896 DumpGuestMemoryFormat format, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +08001897{
Janosch Frank86a518b2022-03-30 12:35:55 +00001898 ERRP_GUARD();
Wen Congyang783e9b42012-05-07 12:10:47 +08001899 const char *p;
1900 int fd = -1;
1901 DumpState *s;
Peter Xu1fbeff72016-02-18 13:16:52 +08001902 bool detach_p = false;
Wen Congyang783e9b42012-05-07 12:10:47 +08001903
Peter Xu63e27f22016-02-18 13:16:51 +08001904 if (runstate_check(RUN_STATE_INMIGRATE)) {
1905 error_setg(errp, "Dump not allowed during incoming migration.");
1906 return;
1907 }
1908
Peter Xu65d64f32016-02-18 13:16:49 +08001909 /* if there is a dump in background, we should wait until the dump
1910 * finished */
Marc-André Lureau544803c2022-03-23 19:57:31 +04001911 if (qemu_system_dump_in_progress()) {
Peter Xu65d64f32016-02-18 13:16:49 +08001912 error_setg(errp, "There is a dump in process, please wait.");
1913 return;
1914 }
1915
qiaonuohanb53ccc32014-02-18 14:11:36 +08001916 /*
1917 * kdump-compressed format need the whole memory dumped, so paging or
1918 * filter is not supported here.
1919 */
1920 if ((has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) &&
1921 (paging || has_begin || has_length)) {
1922 error_setg(errp, "kdump-compressed format doesn't support paging or "
1923 "filter");
1924 return;
1925 }
Wen Congyang783e9b42012-05-07 12:10:47 +08001926 if (has_begin && !has_length) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001927 error_setg(errp, QERR_MISSING_PARAMETER, "length");
Wen Congyang783e9b42012-05-07 12:10:47 +08001928 return;
1929 }
1930 if (!has_begin && has_length) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001931 error_setg(errp, QERR_MISSING_PARAMETER, "begin");
Wen Congyang783e9b42012-05-07 12:10:47 +08001932 return;
1933 }
Peter Xu1fbeff72016-02-18 13:16:52 +08001934 if (has_detach) {
1935 detach_p = detach;
1936 }
Wen Congyang783e9b42012-05-07 12:10:47 +08001937
qiaonuohanb53ccc32014-02-18 14:11:36 +08001938 /* check whether lzo/snappy is supported */
1939#ifndef CONFIG_LZO
1940 if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO) {
1941 error_setg(errp, "kdump-lzo is not available now");
1942 return;
1943 }
1944#endif
1945
1946#ifndef CONFIG_SNAPPY
1947 if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY) {
1948 error_setg(errp, "kdump-snappy is not available now");
1949 return;
1950 }
1951#endif
1952
Viktor Prutyanov2da91b52018-05-17 19:23:39 +03001953#ifndef TARGET_X86_64
1954 if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_WIN_DMP) {
1955 error_setg(errp, "Windows dump is only available for x86-64");
1956 return;
1957 }
1958#endif
1959
Wen Congyang783e9b42012-05-07 12:10:47 +08001960#if !defined(WIN32)
1961 if (strstart(file, "fd:", &p)) {
Kevin Wolf947e4742020-10-05 17:58:44 +02001962 fd = monitor_get_fd(monitor_cur(), p, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +08001963 if (fd == -1) {
Wen Congyang783e9b42012-05-07 12:10:47 +08001964 return;
1965 }
1966 }
1967#endif
1968
1969 if (strstart(file, "file:", &p)) {
Daniel P. Berrangé448058a2020-07-21 13:25:21 +01001970 fd = qemu_open_old(p, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, S_IRUSR);
Wen Congyang783e9b42012-05-07 12:10:47 +08001971 if (fd < 0) {
Luiz Capitulino75817662013-06-07 14:36:01 -04001972 error_setg_file_open(errp, errno, p);
Wen Congyang783e9b42012-05-07 12:10:47 +08001973 return;
1974 }
1975 }
1976
1977 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001978 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
Wen Congyang783e9b42012-05-07 12:10:47 +08001979 return;
1980 }
1981
Peter Xub7bc6b12021-09-22 12:20:09 -04001982 if (!dump_migration_blocker) {
1983 error_setg(&dump_migration_blocker,
1984 "Live migration disabled: dump-guest-memory in progress");
1985 }
1986
1987 /*
1988 * Allows even for -only-migratable, but forbid migration during the
1989 * process of dump guest memory.
1990 */
1991 if (migrate_add_blocker_internal(dump_migration_blocker, errp)) {
1992 /* Remember to release the fd before passing it over to dump state */
1993 close(fd);
1994 return;
1995 }
1996
Peter Xubaf28f52016-02-18 13:16:48 +08001997 s = &dump_state_global;
1998 dump_state_prepare(s);
Wen Congyang783e9b42012-05-07 12:10:47 +08001999
zhanghailiang4c7e2512014-10-09 14:13:11 +08002000 dump_init(s, fd, has_format, format, paging, has_begin,
Janosch Frank86a518b2022-03-30 12:35:55 +00002001 begin, length, errp);
2002 if (*errp) {
Stefan Hajnoczid73415a2020-09-23 11:56:46 +01002003 qatomic_set(&s->status, DUMP_STATUS_FAILED);
Wen Congyang783e9b42012-05-07 12:10:47 +08002004 return;
2005 }
2006
Peter Xu1fbeff72016-02-18 13:16:52 +08002007 if (detach_p) {
2008 /* detached dump */
Fam Zheng6796b402017-05-03 15:28:19 +08002009 s->detached = true;
Peter Xu1fbeff72016-02-18 13:16:52 +08002010 qemu_thread_create(&s->dump_thread, "dump_thread", dump_thread,
2011 s, QEMU_THREAD_DETACHED);
2012 } else {
2013 /* sync dump */
2014 dump_process(s, errp);
2015 }
Wen Congyang783e9b42012-05-07 12:10:47 +08002016}
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002017
2018DumpGuestMemoryCapability *qmp_query_dump_guest_memory_capability(Error **errp)
2019{
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002020 DumpGuestMemoryCapability *cap =
Markus Armbrusterb21e2382022-03-15 15:41:56 +01002021 g_new0(DumpGuestMemoryCapability, 1);
Eric Blake95b3a8c2021-01-13 16:10:13 -06002022 DumpGuestMemoryFormatList **tail = &cap->formats;
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002023
2024 /* elf is always available */
Eric Blake95b3a8c2021-01-13 16:10:13 -06002025 QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_ELF);
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002026
2027 /* kdump-zlib is always available */
Eric Blake95b3a8c2021-01-13 16:10:13 -06002028 QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB);
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002029
2030 /* add new item if kdump-lzo is available */
2031#ifdef CONFIG_LZO
Eric Blake95b3a8c2021-01-13 16:10:13 -06002032 QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO);
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002033#endif
2034
2035 /* add new item if kdump-snappy is available */
2036#ifdef CONFIG_SNAPPY
Eric Blake95b3a8c2021-01-13 16:10:13 -06002037 QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY);
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002038#endif
2039
Viktor Prutyanov2da91b52018-05-17 19:23:39 +03002040 /* Windows dump is available only if target is x86_64 */
2041#ifdef TARGET_X86_64
Eric Blake95b3a8c2021-01-13 16:10:13 -06002042 QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_WIN_DMP);
Viktor Prutyanov2da91b52018-05-17 19:23:39 +03002043#endif
2044
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002045 return cap;
2046}