blob: b043337bc73217730f7b38d46484e6bb030322fb [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
Janosch Frank05bbaa502022-03-30 12:36:00 +000057static inline bool dump_is_64bit(DumpState *s)
58{
59 return s->dump_info.d_class == ELFCLASS64;
60}
61
Janosch Frankdddf7252022-08-11 12:10:58 +000062static inline bool dump_has_filter(DumpState *s)
63{
64 return s->filter_area_length > 0;
65}
66
Bharata B Raoacb0ef52014-05-19 19:57:44 +020067uint16_t cpu_to_dump16(DumpState *s, uint16_t val)
Wen Congyang783e9b42012-05-07 12:10:47 +080068{
Bharata B Raoacb0ef52014-05-19 19:57:44 +020069 if (s->dump_info.d_endian == ELFDATA2LSB) {
Wen Congyang783e9b42012-05-07 12:10:47 +080070 val = cpu_to_le16(val);
71 } else {
72 val = cpu_to_be16(val);
73 }
74
75 return val;
76}
77
Bharata B Raoacb0ef52014-05-19 19:57:44 +020078uint32_t cpu_to_dump32(DumpState *s, uint32_t val)
Wen Congyang783e9b42012-05-07 12:10:47 +080079{
Bharata B Raoacb0ef52014-05-19 19:57:44 +020080 if (s->dump_info.d_endian == ELFDATA2LSB) {
Wen Congyang783e9b42012-05-07 12:10:47 +080081 val = cpu_to_le32(val);
82 } else {
83 val = cpu_to_be32(val);
84 }
85
86 return val;
87}
88
Bharata B Raoacb0ef52014-05-19 19:57:44 +020089uint64_t cpu_to_dump64(DumpState *s, uint64_t val)
Wen Congyang783e9b42012-05-07 12:10:47 +080090{
Bharata B Raoacb0ef52014-05-19 19:57:44 +020091 if (s->dump_info.d_endian == ELFDATA2LSB) {
Wen Congyang783e9b42012-05-07 12:10:47 +080092 val = cpu_to_le64(val);
93 } else {
94 val = cpu_to_be64(val);
95 }
96
97 return val;
98}
99
Wen Congyang783e9b42012-05-07 12:10:47 +0800100static int dump_cleanup(DumpState *s)
101{
Laszlo Ersek5ee163e2013-08-06 12:37:09 +0200102 guest_phys_blocks_free(&s->guest_phys_blocks);
Wen Congyang783e9b42012-05-07 12:10:47 +0800103 memory_mapping_list_free(&s->list);
Chen Gang29282072014-08-03 23:28:56 +0800104 close(s->fd);
Marc-André Lureau903ef732017-09-11 18:59:25 +0200105 g_free(s->guest_note);
106 s->guest_note = NULL;
Wen Congyang783e9b42012-05-07 12:10:47 +0800107 if (s->resume) {
Fam Zheng6796b402017-05-03 15:28:19 +0800108 if (s->detached) {
109 qemu_mutex_lock_iothread();
110 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800111 vm_start();
Fam Zheng6796b402017-05-03 15:28:19 +0800112 if (s->detached) {
113 qemu_mutex_unlock_iothread();
114 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800115 }
Peter Xub7bc6b12021-09-22 12:20:09 -0400116 migrate_del_blocker(dump_migration_blocker);
Wen Congyang783e9b42012-05-07 12:10:47 +0800117
Chen Gang29282072014-08-03 23:28:56 +0800118 return 0;
Wen Congyang783e9b42012-05-07 12:10:47 +0800119}
120
qiaonuohanb5ba1cc2014-02-18 14:11:25 +0800121static int fd_write_vmcore(const void *buf, size_t size, void *opaque)
Wen Congyang783e9b42012-05-07 12:10:47 +0800122{
123 DumpState *s = opaque;
Luiz Capitulino2f616522012-09-21 13:17:55 -0300124 size_t written_size;
Wen Congyang783e9b42012-05-07 12:10:47 +0800125
Luiz Capitulino2f616522012-09-21 13:17:55 -0300126 written_size = qemu_write_full(s->fd, buf, size);
127 if (written_size != size) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200128 return -errno;
Wen Congyang783e9b42012-05-07 12:10:47 +0800129 }
130
131 return 0;
132}
133
zhanghailiang4c7e2512014-10-09 14:13:11 +0800134static void write_elf64_header(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800135{
Janosch Frank046bc412022-04-07 09:48:24 +0000136 /*
137 * phnum in the elf header is 16 bit, if we have more segments we
138 * set phnum to PN_XNUM and write the real number of segments to a
139 * special section.
140 */
141 uint16_t phnum = MIN(s->phdr_num, PN_XNUM);
Wen Congyang783e9b42012-05-07 12:10:47 +0800142 Elf64_Ehdr elf_header;
143 int ret;
Wen Congyang783e9b42012-05-07 12:10:47 +0800144
145 memset(&elf_header, 0, sizeof(Elf64_Ehdr));
146 memcpy(&elf_header, ELFMAG, SELFMAG);
147 elf_header.e_ident[EI_CLASS] = ELFCLASS64;
148 elf_header.e_ident[EI_DATA] = s->dump_info.d_endian;
149 elf_header.e_ident[EI_VERSION] = EV_CURRENT;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200150 elf_header.e_type = cpu_to_dump16(s, ET_CORE);
151 elf_header.e_machine = cpu_to_dump16(s, s->dump_info.d_machine);
152 elf_header.e_version = cpu_to_dump32(s, EV_CURRENT);
153 elf_header.e_ehsize = cpu_to_dump16(s, sizeof(elf_header));
Janosch Franke71d3532022-03-30 12:35:59 +0000154 elf_header.e_phoff = cpu_to_dump64(s, s->phdr_offset);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200155 elf_header.e_phentsize = cpu_to_dump16(s, sizeof(Elf64_Phdr));
Janosch Frank046bc412022-04-07 09:48:24 +0000156 elf_header.e_phnum = cpu_to_dump16(s, phnum);
Janosch Frank862a3952022-03-30 12:35:57 +0000157 if (s->shdr_num) {
Janosch Franke71d3532022-03-30 12:35:59 +0000158 elf_header.e_shoff = cpu_to_dump64(s, s->shdr_offset);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200159 elf_header.e_shentsize = cpu_to_dump16(s, sizeof(Elf64_Shdr));
Janosch Frank862a3952022-03-30 12:35:57 +0000160 elf_header.e_shnum = cpu_to_dump16(s, s->shdr_num);
Wen Congyang783e9b42012-05-07 12:10:47 +0800161 }
162
163 ret = fd_write_vmcore(&elf_header, sizeof(elf_header), s);
164 if (ret < 0) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200165 error_setg_errno(errp, -ret, "dump: failed to write elf header");
Wen Congyang783e9b42012-05-07 12:10:47 +0800166 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800167}
168
zhanghailiang4c7e2512014-10-09 14:13:11 +0800169static void write_elf32_header(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800170{
Janosch Frank046bc412022-04-07 09:48:24 +0000171 /*
172 * phnum in the elf header is 16 bit, if we have more segments we
173 * set phnum to PN_XNUM and write the real number of segments to a
174 * special section.
175 */
176 uint16_t phnum = MIN(s->phdr_num, PN_XNUM);
Wen Congyang783e9b42012-05-07 12:10:47 +0800177 Elf32_Ehdr elf_header;
178 int ret;
Wen Congyang783e9b42012-05-07 12:10:47 +0800179
180 memset(&elf_header, 0, sizeof(Elf32_Ehdr));
181 memcpy(&elf_header, ELFMAG, SELFMAG);
182 elf_header.e_ident[EI_CLASS] = ELFCLASS32;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200183 elf_header.e_ident[EI_DATA] = s->dump_info.d_endian;
Wen Congyang783e9b42012-05-07 12:10:47 +0800184 elf_header.e_ident[EI_VERSION] = EV_CURRENT;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200185 elf_header.e_type = cpu_to_dump16(s, ET_CORE);
186 elf_header.e_machine = cpu_to_dump16(s, s->dump_info.d_machine);
187 elf_header.e_version = cpu_to_dump32(s, EV_CURRENT);
188 elf_header.e_ehsize = cpu_to_dump16(s, sizeof(elf_header));
Janosch Franke71d3532022-03-30 12:35:59 +0000189 elf_header.e_phoff = cpu_to_dump32(s, s->phdr_offset);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200190 elf_header.e_phentsize = cpu_to_dump16(s, sizeof(Elf32_Phdr));
Janosch Frank046bc412022-04-07 09:48:24 +0000191 elf_header.e_phnum = cpu_to_dump16(s, phnum);
Janosch Frank862a3952022-03-30 12:35:57 +0000192 if (s->shdr_num) {
Janosch Franke71d3532022-03-30 12:35:59 +0000193 elf_header.e_shoff = cpu_to_dump32(s, s->shdr_offset);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200194 elf_header.e_shentsize = cpu_to_dump16(s, sizeof(Elf32_Shdr));
Janosch Frank862a3952022-03-30 12:35:57 +0000195 elf_header.e_shnum = cpu_to_dump16(s, s->shdr_num);
Wen Congyang783e9b42012-05-07 12:10:47 +0800196 }
197
198 ret = fd_write_vmcore(&elf_header, sizeof(elf_header), s);
199 if (ret < 0) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200200 error_setg_errno(errp, -ret, "dump: failed to write elf header");
Wen Congyang783e9b42012-05-07 12:10:47 +0800201 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800202}
203
zhanghailiang4c7e2512014-10-09 14:13:11 +0800204static void write_elf64_load(DumpState *s, MemoryMapping *memory_mapping,
205 int phdr_index, hwaddr offset,
206 hwaddr filesz, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800207{
208 Elf64_Phdr phdr;
209 int ret;
Wen Congyang783e9b42012-05-07 12:10:47 +0800210
211 memset(&phdr, 0, sizeof(Elf64_Phdr));
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200212 phdr.p_type = cpu_to_dump32(s, PT_LOAD);
213 phdr.p_offset = cpu_to_dump64(s, offset);
214 phdr.p_paddr = cpu_to_dump64(s, memory_mapping->phys_addr);
215 phdr.p_filesz = cpu_to_dump64(s, filesz);
216 phdr.p_memsz = cpu_to_dump64(s, memory_mapping->length);
Jon Dorone17bebd2019-01-09 10:22:03 +0200217 phdr.p_vaddr = cpu_to_dump64(s, memory_mapping->virt_addr) ?: phdr.p_paddr;
Wen Congyang783e9b42012-05-07 12:10:47 +0800218
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200219 assert(memory_mapping->length >= filesz);
220
Wen Congyang783e9b42012-05-07 12:10:47 +0800221 ret = fd_write_vmcore(&phdr, sizeof(Elf64_Phdr), s);
222 if (ret < 0) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200223 error_setg_errno(errp, -ret,
224 "dump: failed to write program header table");
Wen Congyang783e9b42012-05-07 12:10:47 +0800225 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800226}
227
zhanghailiang4c7e2512014-10-09 14:13:11 +0800228static void write_elf32_load(DumpState *s, MemoryMapping *memory_mapping,
229 int phdr_index, hwaddr offset,
230 hwaddr filesz, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800231{
232 Elf32_Phdr phdr;
233 int ret;
Wen Congyang783e9b42012-05-07 12:10:47 +0800234
235 memset(&phdr, 0, sizeof(Elf32_Phdr));
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200236 phdr.p_type = cpu_to_dump32(s, PT_LOAD);
237 phdr.p_offset = cpu_to_dump32(s, offset);
238 phdr.p_paddr = cpu_to_dump32(s, memory_mapping->phys_addr);
239 phdr.p_filesz = cpu_to_dump32(s, filesz);
240 phdr.p_memsz = cpu_to_dump32(s, memory_mapping->length);
Jon Dorone17bebd2019-01-09 10:22:03 +0200241 phdr.p_vaddr =
242 cpu_to_dump32(s, memory_mapping->virt_addr) ?: phdr.p_paddr;
Wen Congyang783e9b42012-05-07 12:10:47 +0800243
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200244 assert(memory_mapping->length >= filesz);
245
Wen Congyang783e9b42012-05-07 12:10:47 +0800246 ret = fd_write_vmcore(&phdr, sizeof(Elf32_Phdr), s);
247 if (ret < 0) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200248 error_setg_errno(errp, -ret,
249 "dump: failed to write program header table");
Wen Congyang783e9b42012-05-07 12:10:47 +0800250 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800251}
252
Janosch Frankbc7d5582022-03-30 12:36:01 +0000253static void write_elf64_phdr_note(DumpState *s, Elf64_Phdr *phdr)
Wen Congyang783e9b42012-05-07 12:10:47 +0800254{
Janosch Frankbc7d5582022-03-30 12:36:01 +0000255 memset(phdr, 0, sizeof(*phdr));
256 phdr->p_type = cpu_to_dump32(s, PT_NOTE);
257 phdr->p_offset = cpu_to_dump64(s, s->note_offset);
258 phdr->p_paddr = 0;
259 phdr->p_filesz = cpu_to_dump64(s, s->note_size);
260 phdr->p_memsz = cpu_to_dump64(s, s->note_size);
261 phdr->p_vaddr = 0;
Wen Congyang783e9b42012-05-07 12:10:47 +0800262}
263
Paolo Bonzini0bc3cd62013-04-08 17:29:59 +0200264static inline int cpu_index(CPUState *cpu)
265{
266 return cpu->cpu_index + 1;
267}
268
Marc-André Lureau903ef732017-09-11 18:59:25 +0200269static void write_guest_note(WriteCoreDumpFunction f, DumpState *s,
270 Error **errp)
271{
272 int ret;
273
274 if (s->guest_note) {
275 ret = f(s->guest_note, s->guest_note_size, s);
276 if (ret < 0) {
277 error_setg(errp, "dump: failed to write guest note");
278 }
279 }
280}
281
zhanghailiang4c7e2512014-10-09 14:13:11 +0800282static void write_elf64_notes(WriteCoreDumpFunction f, DumpState *s,
283 Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800284{
Andreas Färber0d342822012-12-17 07:12:13 +0100285 CPUState *cpu;
Wen Congyang783e9b42012-05-07 12:10:47 +0800286 int ret;
287 int id;
288
Andreas Färberbdc44642013-06-24 23:50:24 +0200289 CPU_FOREACH(cpu) {
Andreas Färber0d342822012-12-17 07:12:13 +0100290 id = cpu_index(cpu);
qiaonuohan6a519912014-02-18 14:11:26 +0800291 ret = cpu_write_elf64_note(f, cpu, id, 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 elf notes");
zhanghailiang4c7e2512014-10-09 14:13:11 +0800294 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800295 }
296 }
297
Andreas Färberbdc44642013-06-24 23:50:24 +0200298 CPU_FOREACH(cpu) {
qiaonuohan6a519912014-02-18 14:11:26 +0800299 ret = cpu_write_elf64_qemunote(f, cpu, s);
Wen Congyang783e9b42012-05-07 12:10:47 +0800300 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800301 error_setg(errp, "dump: failed to write CPU status");
zhanghailiang4c7e2512014-10-09 14:13:11 +0800302 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800303 }
304 }
Marc-André Lureau903ef732017-09-11 18:59:25 +0200305
306 write_guest_note(f, s, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800307}
308
Janosch Frankbc7d5582022-03-30 12:36:01 +0000309static void write_elf32_phdr_note(DumpState *s, Elf32_Phdr *phdr)
Wen Congyang783e9b42012-05-07 12:10:47 +0800310{
Janosch Frankbc7d5582022-03-30 12:36:01 +0000311 memset(phdr, 0, sizeof(*phdr));
312 phdr->p_type = cpu_to_dump32(s, PT_NOTE);
313 phdr->p_offset = cpu_to_dump32(s, s->note_offset);
314 phdr->p_paddr = 0;
315 phdr->p_filesz = cpu_to_dump32(s, s->note_size);
316 phdr->p_memsz = cpu_to_dump32(s, s->note_size);
317 phdr->p_vaddr = 0;
Wen Congyang783e9b42012-05-07 12:10:47 +0800318}
319
zhanghailiang4c7e2512014-10-09 14:13:11 +0800320static void write_elf32_notes(WriteCoreDumpFunction f, DumpState *s,
321 Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800322{
Andreas Färber0d342822012-12-17 07:12:13 +0100323 CPUState *cpu;
Wen Congyang783e9b42012-05-07 12:10:47 +0800324 int ret;
325 int id;
326
Andreas Färberbdc44642013-06-24 23:50:24 +0200327 CPU_FOREACH(cpu) {
Andreas Färber0d342822012-12-17 07:12:13 +0100328 id = cpu_index(cpu);
qiaonuohan6a519912014-02-18 14:11:26 +0800329 ret = cpu_write_elf32_note(f, cpu, id, s);
Wen Congyang783e9b42012-05-07 12:10:47 +0800330 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800331 error_setg(errp, "dump: failed to write elf notes");
zhanghailiang4c7e2512014-10-09 14:13:11 +0800332 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800333 }
334 }
335
Andreas Färberbdc44642013-06-24 23:50:24 +0200336 CPU_FOREACH(cpu) {
qiaonuohan6a519912014-02-18 14:11:26 +0800337 ret = cpu_write_elf32_qemunote(f, cpu, s);
Wen Congyang783e9b42012-05-07 12:10:47 +0800338 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800339 error_setg(errp, "dump: failed to write CPU status");
zhanghailiang4c7e2512014-10-09 14:13:11 +0800340 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800341 }
342 }
Marc-André Lureau903ef732017-09-11 18:59:25 +0200343
344 write_guest_note(f, s, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800345}
346
Janosch Frankbc7d5582022-03-30 12:36:01 +0000347static void write_elf_phdr_note(DumpState *s, Error **errp)
348{
349 ERRP_GUARD();
350 Elf32_Phdr phdr32;
351 Elf64_Phdr phdr64;
352 void *phdr;
353 size_t size;
354 int ret;
355
356 if (dump_is_64bit(s)) {
357 write_elf64_phdr_note(s, &phdr64);
358 size = sizeof(phdr64);
359 phdr = &phdr64;
360 } else {
361 write_elf32_phdr_note(s, &phdr32);
362 size = sizeof(phdr32);
363 phdr = &phdr32;
364 }
365
366 ret = fd_write_vmcore(phdr, size, s);
367 if (ret < 0) {
368 error_setg_errno(errp, -ret,
369 "dump: failed to write program header table");
370 }
371}
372
zhanghailiang4c7e2512014-10-09 14:13:11 +0800373static void write_elf_section(DumpState *s, int type, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800374{
375 Elf32_Shdr shdr32;
376 Elf64_Shdr shdr64;
Wen Congyang783e9b42012-05-07 12:10:47 +0800377 int shdr_size;
378 void *shdr;
379 int ret;
380
381 if (type == 0) {
382 shdr_size = sizeof(Elf32_Shdr);
383 memset(&shdr32, 0, shdr_size);
Janosch Frank046bc412022-04-07 09:48:24 +0000384 shdr32.sh_info = cpu_to_dump32(s, s->phdr_num);
Wen Congyang783e9b42012-05-07 12:10:47 +0800385 shdr = &shdr32;
386 } else {
387 shdr_size = sizeof(Elf64_Shdr);
388 memset(&shdr64, 0, shdr_size);
Janosch Frank046bc412022-04-07 09:48:24 +0000389 shdr64.sh_info = cpu_to_dump32(s, s->phdr_num);
Wen Congyang783e9b42012-05-07 12:10:47 +0800390 shdr = &shdr64;
391 }
392
Peter Maydell174d2d62020-03-24 17:36:30 +0000393 ret = fd_write_vmcore(shdr, shdr_size, s);
Wen Congyang783e9b42012-05-07 12:10:47 +0800394 if (ret < 0) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200395 error_setg_errno(errp, -ret,
396 "dump: failed to write section header table");
Wen Congyang783e9b42012-05-07 12:10:47 +0800397 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800398}
399
zhanghailiang4c7e2512014-10-09 14:13:11 +0800400static void write_data(DumpState *s, void *buf, int length, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800401{
402 int ret;
403
404 ret = fd_write_vmcore(buf, length, s);
405 if (ret < 0) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200406 error_setg_errno(errp, -ret, "dump: failed to save memory");
Peter Xu2264c2c2016-02-18 13:16:53 +0800407 } else {
408 s->written_size += length;
Wen Congyang783e9b42012-05-07 12:10:47 +0800409 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800410}
411
zhanghailiang4c7e2512014-10-09 14:13:11 +0800412/* write the memory to vmcore. 1 page per I/O. */
413static void write_memory(DumpState *s, GuestPhysBlock *block, ram_addr_t start,
414 int64_t size, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800415{
Janosch Frank86a518b2022-03-30 12:35:55 +0000416 ERRP_GUARD();
Wen Congyang783e9b42012-05-07 12:10:47 +0800417 int64_t i;
Wen Congyang783e9b42012-05-07 12:10:47 +0800418
Andrew Jones8161bef2016-01-11 20:56:20 +0100419 for (i = 0; i < size / s->dump_info.page_size; i++) {
420 write_data(s, block->host_addr + start + i * s->dump_info.page_size,
Janosch Frank86a518b2022-03-30 12:35:55 +0000421 s->dump_info.page_size, errp);
422 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800423 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800424 }
425 }
426
Andrew Jones8161bef2016-01-11 20:56:20 +0100427 if ((size % s->dump_info.page_size) != 0) {
428 write_data(s, block->host_addr + start + i * s->dump_info.page_size,
Janosch Frank86a518b2022-03-30 12:35:55 +0000429 size % s->dump_info.page_size, errp);
430 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800431 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800432 }
433 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800434}
435
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200436/* get the memory's offset and size in the vmcore */
437static void get_offset_range(hwaddr phys_addr,
438 ram_addr_t mapping_length,
439 DumpState *s,
440 hwaddr *p_offset,
441 hwaddr *p_filesz)
Wen Congyang783e9b42012-05-07 12:10:47 +0800442{
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200443 GuestPhysBlock *block;
Avi Kivitya8170e52012-10-23 12:30:10 +0200444 hwaddr offset = s->memory_offset;
Wen Congyang783e9b42012-05-07 12:10:47 +0800445 int64_t size_in_block, start;
446
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200447 /* When the memory is not stored into vmcore, offset will be -1 */
448 *p_offset = -1;
449 *p_filesz = 0;
450
Janosch Frankdddf7252022-08-11 12:10:58 +0000451 if (dump_has_filter(s)) {
452 if (phys_addr < s->filter_area_begin ||
453 phys_addr >= s->filter_area_begin + s->filter_area_length) {
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200454 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800455 }
456 }
457
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200458 QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
Janosch Frankdddf7252022-08-11 12:10:58 +0000459 if (dump_has_filter(s)) {
460 if (block->target_start >= s->filter_area_begin + s->filter_area_length ||
461 block->target_end <= s->filter_area_begin) {
Wen Congyang783e9b42012-05-07 12:10:47 +0800462 /* This block is out of the range */
463 continue;
464 }
465
Janosch Frankdddf7252022-08-11 12:10:58 +0000466 if (s->filter_area_begin <= block->target_start) {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200467 start = block->target_start;
Wen Congyang783e9b42012-05-07 12:10:47 +0800468 } else {
Janosch Frankdddf7252022-08-11 12:10:58 +0000469 start = s->filter_area_begin;
Wen Congyang783e9b42012-05-07 12:10:47 +0800470 }
471
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200472 size_in_block = block->target_end - start;
Janosch Frankdddf7252022-08-11 12:10:58 +0000473 if (s->filter_area_begin + s->filter_area_length < block->target_end) {
474 size_in_block -= block->target_end - (s->filter_area_begin + s->filter_area_length);
Wen Congyang783e9b42012-05-07 12:10:47 +0800475 }
476 } else {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200477 start = block->target_start;
478 size_in_block = block->target_end - block->target_start;
Wen Congyang783e9b42012-05-07 12:10:47 +0800479 }
480
481 if (phys_addr >= start && phys_addr < start + size_in_block) {
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200482 *p_offset = phys_addr - start + offset;
483
484 /* The offset range mapped from the vmcore file must not spill over
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200485 * the GuestPhysBlock, clamp it. The rest of the mapping will be
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200486 * zero-filled in memory at load time; see
487 * <http://refspecs.linuxbase.org/elf/gabi4+/ch5.pheader.html>.
488 */
489 *p_filesz = phys_addr + mapping_length <= start + size_in_block ?
490 mapping_length :
491 size_in_block - (phys_addr - start);
492 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800493 }
494
495 offset += size_in_block;
496 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800497}
498
Janosch Frankafae6052022-08-11 12:10:55 +0000499static void write_elf_phdr_loads(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800500{
Janosch Frank86a518b2022-03-30 12:35:55 +0000501 ERRP_GUARD();
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200502 hwaddr offset, filesz;
Wen Congyang783e9b42012-05-07 12:10:47 +0800503 MemoryMapping *memory_mapping;
504 uint32_t phdr_index = 1;
Wen Congyang783e9b42012-05-07 12:10:47 +0800505
506 QTAILQ_FOREACH(memory_mapping, &s->list.head, next) {
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200507 get_offset_range(memory_mapping->phys_addr,
508 memory_mapping->length,
509 s, &offset, &filesz);
Janosch Frank05bbaa502022-03-30 12:36:00 +0000510 if (dump_is_64bit(s)) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800511 write_elf64_load(s, memory_mapping, phdr_index++, offset,
Janosch Frank86a518b2022-03-30 12:35:55 +0000512 filesz, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800513 } else {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800514 write_elf32_load(s, memory_mapping, phdr_index++, offset,
Janosch Frank86a518b2022-03-30 12:35:55 +0000515 filesz, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800516 }
517
Janosch Frank86a518b2022-03-30 12:35:55 +0000518 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800519 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800520 }
521
Janosch Frank046bc412022-04-07 09:48:24 +0000522 if (phdr_index >= s->phdr_num) {
Wen Congyang783e9b42012-05-07 12:10:47 +0800523 break;
524 }
525 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800526}
527
Janosch Frankc6812472022-03-30 12:36:03 +0000528static void write_elf_notes(DumpState *s, Error **errp)
529{
530 if (dump_is_64bit(s)) {
531 write_elf64_notes(fd_write_vmcore, s, errp);
532 } else {
533 write_elf32_notes(fd_write_vmcore, s, errp);
534 }
535}
536
Wen Congyang783e9b42012-05-07 12:10:47 +0800537/* write elf header, PT_NOTE and elf note to vmcore. */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800538static void dump_begin(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800539{
Janosch Frank86a518b2022-03-30 12:35:55 +0000540 ERRP_GUARD();
Wen Congyang783e9b42012-05-07 12:10:47 +0800541
542 /*
543 * the vmcore's format is:
544 * --------------
545 * | elf header |
546 * --------------
547 * | PT_NOTE |
548 * --------------
549 * | PT_LOAD |
550 * --------------
551 * | ...... |
552 * --------------
553 * | PT_LOAD |
554 * --------------
555 * | sec_hdr |
556 * --------------
557 * | elf note |
558 * --------------
559 * | memory |
560 * --------------
561 *
562 * we only know where the memory is saved after we write elf note into
563 * vmcore.
564 */
565
566 /* write elf header to vmcore */
Janosch Frank05bbaa502022-03-30 12:36:00 +0000567 if (dump_is_64bit(s)) {
Janosch Frank86a518b2022-03-30 12:35:55 +0000568 write_elf64_header(s, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800569 } else {
Janosch Frank86a518b2022-03-30 12:35:55 +0000570 write_elf32_header(s, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800571 }
Janosch Frank86a518b2022-03-30 12:35:55 +0000572 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800573 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800574 }
575
Janosch Frankbc7d5582022-03-30 12:36:01 +0000576 /* write PT_NOTE to vmcore */
577 write_elf_phdr_note(s, errp);
578 if (*errp) {
579 return;
580 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800581
Janosch Frankafae6052022-08-11 12:10:55 +0000582 /* write all PT_LOADs to vmcore */
583 write_elf_phdr_loads(s, errp);
Janosch Frank5ff2e5a2022-03-30 12:36:02 +0000584 if (*errp) {
585 return;
586 }
587
588 /* write section to vmcore */
589 if (s->shdr_num) {
590 write_elf_section(s, 1, errp);
Janosch Frank86a518b2022-03-30 12:35:55 +0000591 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800592 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800593 }
Janosch Frank5ff2e5a2022-03-30 12:36:02 +0000594 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800595
Janosch Frankc6812472022-03-30 12:36:03 +0000596 /* write notes to vmcore */
597 write_elf_notes(s, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800598}
599
Janosch Frank1e811302022-08-11 12:10:56 +0000600static int64_t dump_filtered_memblock_size(GuestPhysBlock *block,
601 int64_t filter_area_start,
602 int64_t filter_area_length)
Wen Congyang783e9b42012-05-07 12:10:47 +0800603{
Janosch Frank1e811302022-08-11 12:10:56 +0000604 int64_t size, left, right;
Wen Congyang783e9b42012-05-07 12:10:47 +0800605
Janosch Frank1e811302022-08-11 12:10:56 +0000606 /* No filter, return full size */
607 if (!filter_area_length) {
608 return block->target_end - block->target_start;
Wen Congyang783e9b42012-05-07 12:10:47 +0800609 }
Janosch Frank1e811302022-08-11 12:10:56 +0000610
611 /* calculate the overlapped region. */
612 left = MAX(filter_area_start, block->target_start);
613 right = MIN(filter_area_start + filter_area_length, block->target_end);
614 size = right - left;
615 size = size > 0 ? size : 0;
616
617 return size;
618}
619
620static int64_t dump_filtered_memblock_start(GuestPhysBlock *block,
621 int64_t filter_area_start,
622 int64_t filter_area_length)
623{
624 if (filter_area_length) {
625 /* return -1 if the block is not within filter area */
626 if (block->target_start >= filter_area_start + filter_area_length ||
627 block->target_end <= filter_area_start) {
628 return -1;
629 }
630
631 if (filter_area_start > block->target_start) {
632 return filter_area_start - block->target_start;
633 }
634 }
635
636 return 0;
Wen Congyang783e9b42012-05-07 12:10:47 +0800637}
638
639/* write all memory to vmcore */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800640static void dump_iterate(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800641{
Janosch Frank86a518b2022-03-30 12:35:55 +0000642 ERRP_GUARD();
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200643 GuestPhysBlock *block;
Janosch Frank1e811302022-08-11 12:10:56 +0000644 int64_t memblock_size, memblock_start;
Wen Congyang783e9b42012-05-07 12:10:47 +0800645
Janosch Frank1e811302022-08-11 12:10:56 +0000646 QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
Janosch Frankdddf7252022-08-11 12:10:58 +0000647 memblock_start = dump_filtered_memblock_start(block, s->filter_area_begin, s->filter_area_length);
Janosch Frank1e811302022-08-11 12:10:56 +0000648 if (memblock_start == -1) {
649 continue;
Wen Congyang783e9b42012-05-07 12:10:47 +0800650 }
Janosch Frank1e811302022-08-11 12:10:56 +0000651
Janosch Frankdddf7252022-08-11 12:10:58 +0000652 memblock_size = dump_filtered_memblock_size(block, s->filter_area_begin, s->filter_area_length);
Janosch Frank1e811302022-08-11 12:10:56 +0000653
654 /* Write the memory to file */
655 write_memory(s, block, memblock_start, memblock_size, errp);
Janosch Frank86a518b2022-03-30 12:35:55 +0000656 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800657 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800658 }
Janosch Frank1e811302022-08-11 12:10:56 +0000659 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800660}
661
zhanghailiang4c7e2512014-10-09 14:13:11 +0800662static void create_vmcore(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800663{
Janosch Frank86a518b2022-03-30 12:35:55 +0000664 ERRP_GUARD();
Wen Congyang783e9b42012-05-07 12:10:47 +0800665
Janosch Frank86a518b2022-03-30 12:35:55 +0000666 dump_begin(s, errp);
667 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800668 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800669 }
670
zhanghailiang4c7e2512014-10-09 14:13:11 +0800671 dump_iterate(s, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800672}
673
qiaonuohanfda05382014-02-18 14:11:27 +0800674static int write_start_flat_header(int fd)
675{
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200676 MakedumpfileHeader *mh;
qiaonuohanfda05382014-02-18 14:11:27 +0800677 int ret = 0;
678
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200679 QEMU_BUILD_BUG_ON(sizeof *mh > MAX_SIZE_MDF_HEADER);
680 mh = g_malloc0(MAX_SIZE_MDF_HEADER);
qiaonuohanfda05382014-02-18 14:11:27 +0800681
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200682 memcpy(mh->signature, MAKEDUMPFILE_SIGNATURE,
683 MIN(sizeof mh->signature, sizeof MAKEDUMPFILE_SIGNATURE));
qiaonuohanfda05382014-02-18 14:11:27 +0800684
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200685 mh->type = cpu_to_be64(TYPE_FLAT_HEADER);
686 mh->version = cpu_to_be64(VERSION_FLAT_HEADER);
qiaonuohanfda05382014-02-18 14:11:27 +0800687
688 size_t written_size;
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200689 written_size = qemu_write_full(fd, mh, MAX_SIZE_MDF_HEADER);
qiaonuohanfda05382014-02-18 14:11:27 +0800690 if (written_size != MAX_SIZE_MDF_HEADER) {
691 ret = -1;
692 }
693
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200694 g_free(mh);
qiaonuohanfda05382014-02-18 14:11:27 +0800695 return ret;
696}
697
698static int write_end_flat_header(int fd)
699{
700 MakedumpfileDataHeader mdh;
701
702 mdh.offset = END_FLAG_FLAT_HEADER;
703 mdh.buf_size = END_FLAG_FLAT_HEADER;
704
705 size_t written_size;
706 written_size = qemu_write_full(fd, &mdh, sizeof(mdh));
707 if (written_size != sizeof(mdh)) {
708 return -1;
709 }
710
711 return 0;
712}
713
qiaonuohan5d31bab2014-02-18 14:11:28 +0800714static int write_buffer(int fd, off_t offset, const void *buf, size_t size)
715{
716 size_t written_size;
717 MakedumpfileDataHeader mdh;
718
719 mdh.offset = cpu_to_be64(offset);
720 mdh.buf_size = cpu_to_be64(size);
721
722 written_size = qemu_write_full(fd, &mdh, sizeof(mdh));
723 if (written_size != sizeof(mdh)) {
724 return -1;
725 }
726
727 written_size = qemu_write_full(fd, buf, size);
728 if (written_size != size) {
729 return -1;
730 }
731
732 return 0;
733}
734
qiaonuohan4835ef72014-02-18 14:11:29 +0800735static int buf_write_note(const void *buf, size_t size, void *opaque)
736{
737 DumpState *s = opaque;
738
739 /* note_buf is not enough */
740 if (s->note_buf_offset + size > s->note_size) {
741 return -1;
742 }
743
744 memcpy(s->note_buf + s->note_buf_offset, buf, size);
745
746 s->note_buf_offset += size;
747
748 return 0;
749}
750
Marc-André Lureau903ef732017-09-11 18:59:25 +0200751/*
752 * This function retrieves various sizes from an elf header.
753 *
754 * @note has to be a valid ELF note. The return sizes are unmodified
755 * (not padded or rounded up to be multiple of 4).
756 */
757static void get_note_sizes(DumpState *s, const void *note,
758 uint64_t *note_head_size,
759 uint64_t *name_size,
760 uint64_t *desc_size)
761{
762 uint64_t note_head_sz;
763 uint64_t name_sz;
764 uint64_t desc_sz;
765
Janosch Frank05bbaa502022-03-30 12:36:00 +0000766 if (dump_is_64bit(s)) {
Marc-André Lureau903ef732017-09-11 18:59:25 +0200767 const Elf64_Nhdr *hdr = note;
768 note_head_sz = sizeof(Elf64_Nhdr);
769 name_sz = tswap64(hdr->n_namesz);
770 desc_sz = tswap64(hdr->n_descsz);
771 } else {
772 const Elf32_Nhdr *hdr = note;
773 note_head_sz = sizeof(Elf32_Nhdr);
774 name_sz = tswap32(hdr->n_namesz);
775 desc_sz = tswap32(hdr->n_descsz);
776 }
777
778 if (note_head_size) {
779 *note_head_size = note_head_sz;
780 }
781 if (name_size) {
782 *name_size = name_sz;
783 }
784 if (desc_size) {
785 *desc_size = desc_sz;
786 }
787}
788
Marc-André Lureaud9feb512017-09-11 18:59:26 +0200789static bool note_name_equal(DumpState *s,
790 const uint8_t *note, const char *name)
791{
792 int len = strlen(name) + 1;
793 uint64_t head_size, name_size;
794
795 get_note_sizes(s, note, &head_size, &name_size, NULL);
796 head_size = ROUND_UP(head_size, 4);
797
Marc-André Lureauc983ca82017-12-12 15:53:59 +0100798 return name_size == len && memcmp(note + head_size, name, len) == 0;
Marc-André Lureaud9feb512017-09-11 18:59:26 +0200799}
800
qiaonuohan298f1162014-02-18 14:11:32 +0800801/* write common header, sub header and elf note to vmcore */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800802static void create_header32(DumpState *s, Error **errp)
qiaonuohan298f1162014-02-18 14:11:32 +0800803{
Janosch Frank86a518b2022-03-30 12:35:55 +0000804 ERRP_GUARD();
qiaonuohan298f1162014-02-18 14:11:32 +0800805 DiskDumpHeader32 *dh = NULL;
806 KdumpSubHeader32 *kh = NULL;
807 size_t size;
qiaonuohan298f1162014-02-18 14:11:32 +0800808 uint32_t block_size;
809 uint32_t sub_hdr_size;
810 uint32_t bitmap_blocks;
811 uint32_t status = 0;
812 uint64_t offset_note;
813
814 /* write common header, the version of kdump-compressed format is 6th */
815 size = sizeof(DiskDumpHeader32);
816 dh = g_malloc0(size);
817
Eric Blake84c868f2018-03-27 15:21:51 -0500818 memcpy(dh->signature, KDUMP_SIGNATURE, SIG_LEN);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200819 dh->header_version = cpu_to_dump32(s, 6);
Andrew Jones8161bef2016-01-11 20:56:20 +0100820 block_size = s->dump_info.page_size;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200821 dh->block_size = cpu_to_dump32(s, block_size);
qiaonuohan298f1162014-02-18 14:11:32 +0800822 sub_hdr_size = sizeof(struct KdumpSubHeader32) + s->note_size;
823 sub_hdr_size = DIV_ROUND_UP(sub_hdr_size, block_size);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200824 dh->sub_hdr_size = cpu_to_dump32(s, sub_hdr_size);
qiaonuohan298f1162014-02-18 14:11:32 +0800825 /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200826 dh->max_mapnr = cpu_to_dump32(s, MIN(s->max_mapnr, UINT_MAX));
827 dh->nr_cpus = cpu_to_dump32(s, s->nr_cpus);
qiaonuohan298f1162014-02-18 14:11:32 +0800828 bitmap_blocks = DIV_ROUND_UP(s->len_dump_bitmap, block_size) * 2;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200829 dh->bitmap_blocks = cpu_to_dump32(s, bitmap_blocks);
qiaonuohan4ab23a92014-02-18 14:11:37 +0800830 strncpy(dh->utsname.machine, ELF_MACHINE_UNAME, sizeof(dh->utsname.machine));
qiaonuohan298f1162014-02-18 14:11:32 +0800831
832 if (s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) {
833 status |= DUMP_DH_COMPRESSED_ZLIB;
834 }
835#ifdef CONFIG_LZO
836 if (s->flag_compress & DUMP_DH_COMPRESSED_LZO) {
837 status |= DUMP_DH_COMPRESSED_LZO;
838 }
839#endif
840#ifdef CONFIG_SNAPPY
841 if (s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) {
842 status |= DUMP_DH_COMPRESSED_SNAPPY;
843 }
844#endif
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200845 dh->status = cpu_to_dump32(s, status);
qiaonuohan298f1162014-02-18 14:11:32 +0800846
847 if (write_buffer(s->fd, 0, dh, size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800848 error_setg(errp, "dump: failed to write disk dump header");
qiaonuohan298f1162014-02-18 14:11:32 +0800849 goto out;
850 }
851
852 /* write sub header */
853 size = sizeof(KdumpSubHeader32);
854 kh = g_malloc0(size);
855
856 /* 64bit max_mapnr_64 */
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200857 kh->max_mapnr_64 = cpu_to_dump64(s, s->max_mapnr);
Andrew Jonesb6e05aa2016-01-11 20:56:21 +0100858 kh->phys_base = cpu_to_dump32(s, s->dump_info.phys_base);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200859 kh->dump_level = cpu_to_dump32(s, DUMP_LEVEL);
qiaonuohan298f1162014-02-18 14:11:32 +0800860
861 offset_note = DISKDUMP_HEADER_BLOCKS * block_size + size;
Marc-André Lureau9ada5752017-09-11 18:59:27 +0200862 if (s->guest_note &&
863 note_name_equal(s, s->guest_note, "VMCOREINFO")) {
864 uint64_t hsize, name_size, size_vmcoreinfo_desc, offset_vmcoreinfo;
865
866 get_note_sizes(s, s->guest_note,
867 &hsize, &name_size, &size_vmcoreinfo_desc);
868 offset_vmcoreinfo = offset_note + s->note_size - s->guest_note_size +
869 (DIV_ROUND_UP(hsize, 4) + DIV_ROUND_UP(name_size, 4)) * 4;
870 kh->offset_vmcoreinfo = cpu_to_dump64(s, offset_vmcoreinfo);
871 kh->size_vmcoreinfo = cpu_to_dump32(s, size_vmcoreinfo_desc);
872 }
873
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200874 kh->offset_note = cpu_to_dump64(s, offset_note);
875 kh->note_size = cpu_to_dump32(s, s->note_size);
qiaonuohan298f1162014-02-18 14:11:32 +0800876
877 if (write_buffer(s->fd, DISKDUMP_HEADER_BLOCKS *
878 block_size, kh, size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800879 error_setg(errp, "dump: failed to write kdump sub header");
qiaonuohan298f1162014-02-18 14:11:32 +0800880 goto out;
881 }
882
883 /* write note */
884 s->note_buf = g_malloc0(s->note_size);
885 s->note_buf_offset = 0;
886
887 /* use s->note_buf to store notes temporarily */
Janosch Frank86a518b2022-03-30 12:35:55 +0000888 write_elf32_notes(buf_write_note, s, errp);
889 if (*errp) {
qiaonuohan298f1162014-02-18 14:11:32 +0800890 goto out;
891 }
qiaonuohan298f1162014-02-18 14:11:32 +0800892 if (write_buffer(s->fd, offset_note, s->note_buf,
893 s->note_size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800894 error_setg(errp, "dump: failed to write notes");
qiaonuohan298f1162014-02-18 14:11:32 +0800895 goto out;
896 }
897
898 /* get offset of dump_bitmap */
899 s->offset_dump_bitmap = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size) *
900 block_size;
901
902 /* get offset of page */
903 s->offset_page = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size + bitmap_blocks) *
904 block_size;
905
906out:
907 g_free(dh);
908 g_free(kh);
909 g_free(s->note_buf);
qiaonuohan298f1162014-02-18 14:11:32 +0800910}
911
912/* write common header, sub header and elf note to vmcore */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800913static void create_header64(DumpState *s, Error **errp)
qiaonuohan298f1162014-02-18 14:11:32 +0800914{
Janosch Frank86a518b2022-03-30 12:35:55 +0000915 ERRP_GUARD();
qiaonuohan298f1162014-02-18 14:11:32 +0800916 DiskDumpHeader64 *dh = NULL;
917 KdumpSubHeader64 *kh = NULL;
918 size_t size;
qiaonuohan298f1162014-02-18 14:11:32 +0800919 uint32_t block_size;
920 uint32_t sub_hdr_size;
921 uint32_t bitmap_blocks;
922 uint32_t status = 0;
923 uint64_t offset_note;
924
925 /* write common header, the version of kdump-compressed format is 6th */
926 size = sizeof(DiskDumpHeader64);
927 dh = g_malloc0(size);
928
Eric Blake84c868f2018-03-27 15:21:51 -0500929 memcpy(dh->signature, KDUMP_SIGNATURE, SIG_LEN);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200930 dh->header_version = cpu_to_dump32(s, 6);
Andrew Jones8161bef2016-01-11 20:56:20 +0100931 block_size = s->dump_info.page_size;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200932 dh->block_size = cpu_to_dump32(s, block_size);
qiaonuohan298f1162014-02-18 14:11:32 +0800933 sub_hdr_size = sizeof(struct KdumpSubHeader64) + s->note_size;
934 sub_hdr_size = DIV_ROUND_UP(sub_hdr_size, block_size);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200935 dh->sub_hdr_size = cpu_to_dump32(s, sub_hdr_size);
qiaonuohan298f1162014-02-18 14:11:32 +0800936 /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200937 dh->max_mapnr = cpu_to_dump32(s, MIN(s->max_mapnr, UINT_MAX));
938 dh->nr_cpus = cpu_to_dump32(s, s->nr_cpus);
qiaonuohan298f1162014-02-18 14:11:32 +0800939 bitmap_blocks = DIV_ROUND_UP(s->len_dump_bitmap, block_size) * 2;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200940 dh->bitmap_blocks = cpu_to_dump32(s, bitmap_blocks);
qiaonuohan4ab23a92014-02-18 14:11:37 +0800941 strncpy(dh->utsname.machine, ELF_MACHINE_UNAME, sizeof(dh->utsname.machine));
qiaonuohan298f1162014-02-18 14:11:32 +0800942
943 if (s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) {
944 status |= DUMP_DH_COMPRESSED_ZLIB;
945 }
946#ifdef CONFIG_LZO
947 if (s->flag_compress & DUMP_DH_COMPRESSED_LZO) {
948 status |= DUMP_DH_COMPRESSED_LZO;
949 }
950#endif
951#ifdef CONFIG_SNAPPY
952 if (s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) {
953 status |= DUMP_DH_COMPRESSED_SNAPPY;
954 }
955#endif
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200956 dh->status = cpu_to_dump32(s, status);
qiaonuohan298f1162014-02-18 14:11:32 +0800957
958 if (write_buffer(s->fd, 0, dh, size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800959 error_setg(errp, "dump: failed to write disk dump header");
qiaonuohan298f1162014-02-18 14:11:32 +0800960 goto out;
961 }
962
963 /* write sub header */
964 size = sizeof(KdumpSubHeader64);
965 kh = g_malloc0(size);
966
967 /* 64bit max_mapnr_64 */
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200968 kh->max_mapnr_64 = cpu_to_dump64(s, s->max_mapnr);
Andrew Jonesb6e05aa2016-01-11 20:56:21 +0100969 kh->phys_base = cpu_to_dump64(s, s->dump_info.phys_base);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200970 kh->dump_level = cpu_to_dump32(s, DUMP_LEVEL);
qiaonuohan298f1162014-02-18 14:11:32 +0800971
972 offset_note = DISKDUMP_HEADER_BLOCKS * block_size + size;
Marc-André Lureau9ada5752017-09-11 18:59:27 +0200973 if (s->guest_note &&
974 note_name_equal(s, s->guest_note, "VMCOREINFO")) {
975 uint64_t hsize, name_size, size_vmcoreinfo_desc, offset_vmcoreinfo;
976
977 get_note_sizes(s, s->guest_note,
978 &hsize, &name_size, &size_vmcoreinfo_desc);
979 offset_vmcoreinfo = offset_note + s->note_size - s->guest_note_size +
980 (DIV_ROUND_UP(hsize, 4) + DIV_ROUND_UP(name_size, 4)) * 4;
981 kh->offset_vmcoreinfo = cpu_to_dump64(s, offset_vmcoreinfo);
982 kh->size_vmcoreinfo = cpu_to_dump64(s, size_vmcoreinfo_desc);
983 }
984
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200985 kh->offset_note = cpu_to_dump64(s, offset_note);
986 kh->note_size = cpu_to_dump64(s, s->note_size);
qiaonuohan298f1162014-02-18 14:11:32 +0800987
988 if (write_buffer(s->fd, DISKDUMP_HEADER_BLOCKS *
989 block_size, kh, size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800990 error_setg(errp, "dump: failed to write kdump sub header");
qiaonuohan298f1162014-02-18 14:11:32 +0800991 goto out;
992 }
993
994 /* write note */
995 s->note_buf = g_malloc0(s->note_size);
996 s->note_buf_offset = 0;
997
998 /* use s->note_buf to store notes temporarily */
Janosch Frank86a518b2022-03-30 12:35:55 +0000999 write_elf64_notes(buf_write_note, s, errp);
1000 if (*errp) {
qiaonuohan298f1162014-02-18 14:11:32 +08001001 goto out;
1002 }
1003
1004 if (write_buffer(s->fd, offset_note, s->note_buf,
1005 s->note_size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001006 error_setg(errp, "dump: failed to write notes");
qiaonuohan298f1162014-02-18 14:11:32 +08001007 goto out;
1008 }
1009
1010 /* get offset of dump_bitmap */
1011 s->offset_dump_bitmap = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size) *
1012 block_size;
1013
1014 /* get offset of page */
1015 s->offset_page = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size + bitmap_blocks) *
1016 block_size;
1017
1018out:
1019 g_free(dh);
1020 g_free(kh);
1021 g_free(s->note_buf);
qiaonuohan298f1162014-02-18 14:11:32 +08001022}
1023
zhanghailiang4c7e2512014-10-09 14:13:11 +08001024static void write_dump_header(DumpState *s, Error **errp)
qiaonuohan298f1162014-02-18 14:11:32 +08001025{
Janosch Frank05bbaa502022-03-30 12:36:00 +00001026 if (dump_is_64bit(s)) {
Markus Armbruster992861f2020-07-07 18:06:04 +02001027 create_header64(s, errp);
Janosch Frank05bbaa502022-03-30 12:36:00 +00001028 } else {
1029 create_header32(s, errp);
zhanghailiang4c7e2512014-10-09 14:13:11 +08001030 }
qiaonuohan298f1162014-02-18 14:11:32 +08001031}
1032
Andrew Jones8161bef2016-01-11 20:56:20 +01001033static size_t dump_bitmap_get_bufsize(DumpState *s)
1034{
1035 return s->dump_info.page_size;
1036}
1037
qiaonuohand0686c72014-02-18 14:11:33 +08001038/*
1039 * set dump_bitmap sequencely. the bit before last_pfn is not allowed to be
1040 * rewritten, so if need to set the first bit, set last_pfn and pfn to 0.
1041 * set_dump_bitmap will always leave the recently set bit un-sync. And setting
1042 * (last bit + sizeof(buf) * 8) to 0 will do flushing the content in buf into
1043 * vmcore, ie. synchronizing un-sync bit into vmcore.
1044 */
1045static int set_dump_bitmap(uint64_t last_pfn, uint64_t pfn, bool value,
1046 uint8_t *buf, DumpState *s)
1047{
1048 off_t old_offset, new_offset;
1049 off_t offset_bitmap1, offset_bitmap2;
1050 uint32_t byte, bit;
Andrew Jones8161bef2016-01-11 20:56:20 +01001051 size_t bitmap_bufsize = dump_bitmap_get_bufsize(s);
1052 size_t bits_per_buf = bitmap_bufsize * CHAR_BIT;
qiaonuohand0686c72014-02-18 14:11:33 +08001053
1054 /* should not set the previous place */
1055 assert(last_pfn <= pfn);
1056
1057 /*
1058 * if the bit needed to be set is not cached in buf, flush the data in buf
1059 * to vmcore firstly.
1060 * making new_offset be bigger than old_offset can also sync remained data
1061 * into vmcore.
1062 */
Andrew Jones8161bef2016-01-11 20:56:20 +01001063 old_offset = bitmap_bufsize * (last_pfn / bits_per_buf);
1064 new_offset = bitmap_bufsize * (pfn / bits_per_buf);
qiaonuohand0686c72014-02-18 14:11:33 +08001065
1066 while (old_offset < new_offset) {
1067 /* calculate the offset and write dump_bitmap */
1068 offset_bitmap1 = s->offset_dump_bitmap + old_offset;
1069 if (write_buffer(s->fd, offset_bitmap1, buf,
Andrew Jones8161bef2016-01-11 20:56:20 +01001070 bitmap_bufsize) < 0) {
qiaonuohand0686c72014-02-18 14:11:33 +08001071 return -1;
1072 }
1073
1074 /* dump level 1 is chosen, so 1st and 2nd bitmap are same */
1075 offset_bitmap2 = s->offset_dump_bitmap + s->len_dump_bitmap +
1076 old_offset;
1077 if (write_buffer(s->fd, offset_bitmap2, buf,
Andrew Jones8161bef2016-01-11 20:56:20 +01001078 bitmap_bufsize) < 0) {
qiaonuohand0686c72014-02-18 14:11:33 +08001079 return -1;
1080 }
1081
Andrew Jones8161bef2016-01-11 20:56:20 +01001082 memset(buf, 0, bitmap_bufsize);
1083 old_offset += bitmap_bufsize;
qiaonuohand0686c72014-02-18 14:11:33 +08001084 }
1085
1086 /* get the exact place of the bit in the buf, and set it */
Andrew Jones8161bef2016-01-11 20:56:20 +01001087 byte = (pfn % bits_per_buf) / CHAR_BIT;
1088 bit = (pfn % bits_per_buf) % CHAR_BIT;
qiaonuohand0686c72014-02-18 14:11:33 +08001089 if (value) {
1090 buf[byte] |= 1u << bit;
1091 } else {
1092 buf[byte] &= ~(1u << bit);
1093 }
1094
1095 return 0;
1096}
1097
Andrew Jones8161bef2016-01-11 20:56:20 +01001098static uint64_t dump_paddr_to_pfn(DumpState *s, uint64_t addr)
1099{
1100 int target_page_shift = ctz32(s->dump_info.page_size);
1101
1102 return (addr >> target_page_shift) - ARCH_PFN_OFFSET;
1103}
1104
1105static uint64_t dump_pfn_to_paddr(DumpState *s, uint64_t pfn)
1106{
1107 int target_page_shift = ctz32(s->dump_info.page_size);
1108
1109 return (pfn + ARCH_PFN_OFFSET) << target_page_shift;
1110}
1111
qiaonuohand0686c72014-02-18 14:11:33 +08001112/*
1113 * exam every page and return the page frame number and the address of the page.
1114 * bufptr can be NULL. note: the blocks here is supposed to reflect guest-phys
1115 * blocks, so block->target_start and block->target_end should be interal
1116 * multiples of the target page size.
1117 */
1118static bool get_next_page(GuestPhysBlock **blockptr, uint64_t *pfnptr,
1119 uint8_t **bufptr, DumpState *s)
1120{
1121 GuestPhysBlock *block = *blockptr;
Andrew Jones8161bef2016-01-11 20:56:20 +01001122 hwaddr addr, target_page_mask = ~((hwaddr)s->dump_info.page_size - 1);
qiaonuohand0686c72014-02-18 14:11:33 +08001123 uint8_t *buf;
1124
1125 /* block == NULL means the start of the iteration */
1126 if (!block) {
1127 block = QTAILQ_FIRST(&s->guest_phys_blocks.head);
1128 *blockptr = block;
Andrew Jones8161bef2016-01-11 20:56:20 +01001129 assert((block->target_start & ~target_page_mask) == 0);
1130 assert((block->target_end & ~target_page_mask) == 0);
1131 *pfnptr = dump_paddr_to_pfn(s, block->target_start);
qiaonuohand0686c72014-02-18 14:11:33 +08001132 if (bufptr) {
1133 *bufptr = block->host_addr;
1134 }
1135 return true;
1136 }
1137
1138 *pfnptr = *pfnptr + 1;
Andrew Jones8161bef2016-01-11 20:56:20 +01001139 addr = dump_pfn_to_paddr(s, *pfnptr);
qiaonuohand0686c72014-02-18 14:11:33 +08001140
1141 if ((addr >= block->target_start) &&
Andrew Jones8161bef2016-01-11 20:56:20 +01001142 (addr + s->dump_info.page_size <= block->target_end)) {
qiaonuohand0686c72014-02-18 14:11:33 +08001143 buf = block->host_addr + (addr - block->target_start);
1144 } else {
1145 /* the next page is in the next block */
1146 block = QTAILQ_NEXT(block, next);
1147 *blockptr = block;
1148 if (!block) {
1149 return false;
1150 }
Andrew Jones8161bef2016-01-11 20:56:20 +01001151 assert((block->target_start & ~target_page_mask) == 0);
1152 assert((block->target_end & ~target_page_mask) == 0);
1153 *pfnptr = dump_paddr_to_pfn(s, block->target_start);
qiaonuohand0686c72014-02-18 14:11:33 +08001154 buf = block->host_addr;
1155 }
1156
1157 if (bufptr) {
1158 *bufptr = buf;
1159 }
1160
1161 return true;
1162}
1163
zhanghailiang4c7e2512014-10-09 14:13:11 +08001164static void write_dump_bitmap(DumpState *s, Error **errp)
qiaonuohand0686c72014-02-18 14:11:33 +08001165{
1166 int ret = 0;
1167 uint64_t last_pfn, pfn;
1168 void *dump_bitmap_buf;
1169 size_t num_dumpable;
1170 GuestPhysBlock *block_iter = NULL;
Andrew Jones8161bef2016-01-11 20:56:20 +01001171 size_t bitmap_bufsize = dump_bitmap_get_bufsize(s);
1172 size_t bits_per_buf = bitmap_bufsize * CHAR_BIT;
qiaonuohand0686c72014-02-18 14:11:33 +08001173
1174 /* dump_bitmap_buf is used to store dump_bitmap temporarily */
Andrew Jones8161bef2016-01-11 20:56:20 +01001175 dump_bitmap_buf = g_malloc0(bitmap_bufsize);
qiaonuohand0686c72014-02-18 14:11:33 +08001176
1177 num_dumpable = 0;
1178 last_pfn = 0;
1179
1180 /*
1181 * exam memory page by page, and set the bit in dump_bitmap corresponded
1182 * to the existing page.
1183 */
1184 while (get_next_page(&block_iter, &pfn, NULL, s)) {
1185 ret = set_dump_bitmap(last_pfn, pfn, true, dump_bitmap_buf, s);
1186 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001187 error_setg(errp, "dump: failed to set dump_bitmap");
qiaonuohand0686c72014-02-18 14:11:33 +08001188 goto out;
1189 }
1190
1191 last_pfn = pfn;
1192 num_dumpable++;
1193 }
1194
1195 /*
1196 * set_dump_bitmap will always leave the recently set bit un-sync. Here we
Andrew Jones8161bef2016-01-11 20:56:20 +01001197 * set the remaining bits from last_pfn to the end of the bitmap buffer to
1198 * 0. With those set, the un-sync bit will be synchronized into the vmcore.
qiaonuohand0686c72014-02-18 14:11:33 +08001199 */
1200 if (num_dumpable > 0) {
Andrew Jones8161bef2016-01-11 20:56:20 +01001201 ret = set_dump_bitmap(last_pfn, last_pfn + bits_per_buf, false,
qiaonuohand0686c72014-02-18 14:11:33 +08001202 dump_bitmap_buf, s);
1203 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001204 error_setg(errp, "dump: failed to sync dump_bitmap");
qiaonuohand0686c72014-02-18 14:11:33 +08001205 goto out;
1206 }
1207 }
1208
1209 /* number of dumpable pages that will be dumped later */
1210 s->num_dumpable = num_dumpable;
1211
1212out:
1213 g_free(dump_bitmap_buf);
qiaonuohand0686c72014-02-18 14:11:33 +08001214}
1215
qiaonuohan64cfba62014-02-18 14:11:34 +08001216static void prepare_data_cache(DataCache *data_cache, DumpState *s,
1217 off_t offset)
1218{
1219 data_cache->fd = s->fd;
1220 data_cache->data_size = 0;
Andrew Jones8161bef2016-01-11 20:56:20 +01001221 data_cache->buf_size = 4 * dump_bitmap_get_bufsize(s);
1222 data_cache->buf = g_malloc0(data_cache->buf_size);
qiaonuohan64cfba62014-02-18 14:11:34 +08001223 data_cache->offset = offset;
1224}
1225
1226static int write_cache(DataCache *dc, const void *buf, size_t size,
1227 bool flag_sync)
1228{
1229 /*
1230 * dc->buf_size should not be less than size, otherwise dc will never be
1231 * enough
1232 */
1233 assert(size <= dc->buf_size);
1234
1235 /*
1236 * if flag_sync is set, synchronize data in dc->buf into vmcore.
1237 * otherwise check if the space is enough for caching data in buf, if not,
1238 * write the data in dc->buf to dc->fd and reset dc->buf
1239 */
1240 if ((!flag_sync && dc->data_size + size > dc->buf_size) ||
1241 (flag_sync && dc->data_size > 0)) {
1242 if (write_buffer(dc->fd, dc->offset, dc->buf, dc->data_size) < 0) {
1243 return -1;
1244 }
1245
1246 dc->offset += dc->data_size;
1247 dc->data_size = 0;
1248 }
1249
1250 if (!flag_sync) {
1251 memcpy(dc->buf + dc->data_size, buf, size);
1252 dc->data_size += size;
1253 }
1254
1255 return 0;
1256}
1257
1258static void free_data_cache(DataCache *data_cache)
1259{
1260 g_free(data_cache->buf);
1261}
1262
qiaonuohand12f57e2014-02-18 14:11:35 +08001263static size_t get_len_buf_out(size_t page_size, uint32_t flag_compress)
1264{
Laszlo Ersekb87ef352014-05-20 13:42:53 +02001265 switch (flag_compress) {
1266 case DUMP_DH_COMPRESSED_ZLIB:
1267 return compressBound(page_size);
qiaonuohand12f57e2014-02-18 14:11:35 +08001268
Laszlo Ersekb87ef352014-05-20 13:42:53 +02001269 case DUMP_DH_COMPRESSED_LZO:
1270 /*
1271 * LZO will expand incompressible data by a little amount. Please check
1272 * the following URL to see the expansion calculation:
1273 * http://www.oberhumer.com/opensource/lzo/lzofaq.php
1274 */
1275 return page_size + page_size / 16 + 64 + 3;
qiaonuohand12f57e2014-02-18 14:11:35 +08001276
1277#ifdef CONFIG_SNAPPY
Laszlo Ersekb87ef352014-05-20 13:42:53 +02001278 case DUMP_DH_COMPRESSED_SNAPPY:
1279 return snappy_max_compressed_length(page_size);
qiaonuohand12f57e2014-02-18 14:11:35 +08001280#endif
Laszlo Ersekb87ef352014-05-20 13:42:53 +02001281 }
1282 return 0;
qiaonuohand12f57e2014-02-18 14:11:35 +08001283}
1284
zhanghailiang4c7e2512014-10-09 14:13:11 +08001285static void write_dump_pages(DumpState *s, Error **errp)
qiaonuohand12f57e2014-02-18 14:11:35 +08001286{
1287 int ret = 0;
1288 DataCache page_desc, page_data;
1289 size_t len_buf_out, size_out;
1290#ifdef CONFIG_LZO
1291 lzo_bytep wrkmem = NULL;
1292#endif
1293 uint8_t *buf_out = NULL;
1294 off_t offset_desc, offset_data;
1295 PageDescriptor pd, pd_zero;
1296 uint8_t *buf;
qiaonuohand12f57e2014-02-18 14:11:35 +08001297 GuestPhysBlock *block_iter = NULL;
1298 uint64_t pfn_iter;
1299
1300 /* get offset of page_desc and page_data in dump file */
1301 offset_desc = s->offset_page;
1302 offset_data = offset_desc + sizeof(PageDescriptor) * s->num_dumpable;
1303
1304 prepare_data_cache(&page_desc, s, offset_desc);
1305 prepare_data_cache(&page_data, s, offset_data);
1306
1307 /* prepare buffer to store compressed data */
Andrew Jones8161bef2016-01-11 20:56:20 +01001308 len_buf_out = get_len_buf_out(s->dump_info.page_size, s->flag_compress);
Laszlo Ersekb87ef352014-05-20 13:42:53 +02001309 assert(len_buf_out != 0);
qiaonuohand12f57e2014-02-18 14:11:35 +08001310
1311#ifdef CONFIG_LZO
1312 wrkmem = g_malloc(LZO1X_1_MEM_COMPRESS);
1313#endif
1314
1315 buf_out = g_malloc(len_buf_out);
1316
1317 /*
1318 * init zero page's page_desc and page_data, because every zero page
1319 * uses the same page_data
1320 */
Andrew Jones8161bef2016-01-11 20:56:20 +01001321 pd_zero.size = cpu_to_dump32(s, s->dump_info.page_size);
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001322 pd_zero.flags = cpu_to_dump32(s, 0);
1323 pd_zero.offset = cpu_to_dump64(s, offset_data);
1324 pd_zero.page_flags = cpu_to_dump64(s, 0);
Andrew Jones8161bef2016-01-11 20:56:20 +01001325 buf = g_malloc0(s->dump_info.page_size);
1326 ret = write_cache(&page_data, buf, s->dump_info.page_size, false);
qiaonuohand12f57e2014-02-18 14:11:35 +08001327 g_free(buf);
1328 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001329 error_setg(errp, "dump: failed to write page data (zero page)");
qiaonuohand12f57e2014-02-18 14:11:35 +08001330 goto out;
1331 }
1332
Andrew Jones8161bef2016-01-11 20:56:20 +01001333 offset_data += s->dump_info.page_size;
qiaonuohand12f57e2014-02-18 14:11:35 +08001334
1335 /*
1336 * dump memory to vmcore page by page. zero page will all be resided in the
1337 * first page of page section
1338 */
1339 while (get_next_page(&block_iter, &pfn_iter, &buf, s)) {
1340 /* check zero page */
Juan Quintelaf13f22b2021-11-18 15:57:44 +01001341 if (buffer_is_zero(buf, s->dump_info.page_size)) {
qiaonuohand12f57e2014-02-18 14:11:35 +08001342 ret = write_cache(&page_desc, &pd_zero, sizeof(PageDescriptor),
1343 false);
1344 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001345 error_setg(errp, "dump: failed to write page desc");
qiaonuohand12f57e2014-02-18 14:11:35 +08001346 goto out;
1347 }
1348 } else {
1349 /*
1350 * not zero page, then:
1351 * 1. compress the page
1352 * 2. write the compressed page into the cache of page_data
1353 * 3. get page desc of the compressed page and write it into the
1354 * cache of page_desc
1355 *
1356 * only one compression format will be used here, for
1357 * s->flag_compress is set. But when compression fails to work,
1358 * we fall back to save in plaintext.
1359 */
1360 size_out = len_buf_out;
1361 if ((s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) &&
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001362 (compress2(buf_out, (uLongf *)&size_out, buf,
Andrew Jones8161bef2016-01-11 20:56:20 +01001363 s->dump_info.page_size, Z_BEST_SPEED) == Z_OK) &&
1364 (size_out < s->dump_info.page_size)) {
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001365 pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_ZLIB);
1366 pd.size = cpu_to_dump32(s, size_out);
qiaonuohand12f57e2014-02-18 14:11:35 +08001367
1368 ret = write_cache(&page_data, buf_out, size_out, false);
1369 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001370 error_setg(errp, "dump: failed to write page data");
qiaonuohand12f57e2014-02-18 14:11:35 +08001371 goto out;
1372 }
1373#ifdef CONFIG_LZO
1374 } else if ((s->flag_compress & DUMP_DH_COMPRESSED_LZO) &&
Andrew Jones8161bef2016-01-11 20:56:20 +01001375 (lzo1x_1_compress(buf, s->dump_info.page_size, buf_out,
qiaonuohand12f57e2014-02-18 14:11:35 +08001376 (lzo_uint *)&size_out, wrkmem) == LZO_E_OK) &&
Andrew Jones8161bef2016-01-11 20:56:20 +01001377 (size_out < s->dump_info.page_size)) {
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001378 pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_LZO);
1379 pd.size = cpu_to_dump32(s, size_out);
qiaonuohand12f57e2014-02-18 14:11:35 +08001380
1381 ret = write_cache(&page_data, buf_out, size_out, false);
1382 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001383 error_setg(errp, "dump: failed to write page data");
qiaonuohand12f57e2014-02-18 14:11:35 +08001384 goto out;
1385 }
1386#endif
1387#ifdef CONFIG_SNAPPY
1388 } else if ((s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) &&
Andrew Jones8161bef2016-01-11 20:56:20 +01001389 (snappy_compress((char *)buf, s->dump_info.page_size,
qiaonuohand12f57e2014-02-18 14:11:35 +08001390 (char *)buf_out, &size_out) == SNAPPY_OK) &&
Andrew Jones8161bef2016-01-11 20:56:20 +01001391 (size_out < s->dump_info.page_size)) {
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001392 pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_SNAPPY);
1393 pd.size = cpu_to_dump32(s, size_out);
qiaonuohand12f57e2014-02-18 14:11:35 +08001394
1395 ret = write_cache(&page_data, buf_out, size_out, false);
1396 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001397 error_setg(errp, "dump: failed to write page data");
qiaonuohand12f57e2014-02-18 14:11:35 +08001398 goto out;
1399 }
1400#endif
1401 } else {
1402 /*
1403 * fall back to save in plaintext, size_out should be
Andrew Jones8161bef2016-01-11 20:56:20 +01001404 * assigned the target's page size
qiaonuohand12f57e2014-02-18 14:11:35 +08001405 */
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001406 pd.flags = cpu_to_dump32(s, 0);
Andrew Jones8161bef2016-01-11 20:56:20 +01001407 size_out = s->dump_info.page_size;
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001408 pd.size = cpu_to_dump32(s, size_out);
qiaonuohand12f57e2014-02-18 14:11:35 +08001409
Andrew Jones8161bef2016-01-11 20:56:20 +01001410 ret = write_cache(&page_data, buf,
1411 s->dump_info.page_size, false);
qiaonuohand12f57e2014-02-18 14:11:35 +08001412 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001413 error_setg(errp, "dump: failed to write page data");
qiaonuohand12f57e2014-02-18 14:11:35 +08001414 goto out;
1415 }
1416 }
1417
1418 /* get and write page desc here */
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001419 pd.page_flags = cpu_to_dump64(s, 0);
1420 pd.offset = cpu_to_dump64(s, offset_data);
qiaonuohand12f57e2014-02-18 14:11:35 +08001421 offset_data += size_out;
1422
1423 ret = write_cache(&page_desc, &pd, sizeof(PageDescriptor), false);
1424 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001425 error_setg(errp, "dump: failed to write page desc");
qiaonuohand12f57e2014-02-18 14:11:35 +08001426 goto out;
1427 }
1428 }
Peter Xu2264c2c2016-02-18 13:16:53 +08001429 s->written_size += s->dump_info.page_size;
qiaonuohand12f57e2014-02-18 14:11:35 +08001430 }
1431
1432 ret = write_cache(&page_desc, NULL, 0, true);
1433 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001434 error_setg(errp, "dump: failed to sync cache for page_desc");
qiaonuohand12f57e2014-02-18 14:11:35 +08001435 goto out;
1436 }
1437 ret = write_cache(&page_data, NULL, 0, true);
1438 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001439 error_setg(errp, "dump: failed to sync cache for page_data");
qiaonuohand12f57e2014-02-18 14:11:35 +08001440 goto out;
1441 }
1442
1443out:
1444 free_data_cache(&page_desc);
1445 free_data_cache(&page_data);
1446
1447#ifdef CONFIG_LZO
1448 g_free(wrkmem);
1449#endif
1450
1451 g_free(buf_out);
qiaonuohand12f57e2014-02-18 14:11:35 +08001452}
1453
zhanghailiang4c7e2512014-10-09 14:13:11 +08001454static void create_kdump_vmcore(DumpState *s, Error **errp)
qiaonuohanb53ccc32014-02-18 14:11:36 +08001455{
Janosch Frank86a518b2022-03-30 12:35:55 +00001456 ERRP_GUARD();
qiaonuohanb53ccc32014-02-18 14:11:36 +08001457 int ret;
1458
1459 /*
1460 * the kdump-compressed format is:
1461 * File offset
1462 * +------------------------------------------+ 0x0
1463 * | main header (struct disk_dump_header) |
1464 * |------------------------------------------+ block 1
1465 * | sub header (struct kdump_sub_header) |
1466 * |------------------------------------------+ block 2
1467 * | 1st-dump_bitmap |
1468 * |------------------------------------------+ block 2 + X blocks
1469 * | 2nd-dump_bitmap | (aligned by block)
1470 * |------------------------------------------+ block 2 + 2 * X blocks
1471 * | page desc for pfn 0 (struct page_desc) | (aligned by block)
1472 * | page desc for pfn 1 (struct page_desc) |
1473 * | : |
1474 * |------------------------------------------| (not aligned by block)
1475 * | page data (pfn 0) |
1476 * | page data (pfn 1) |
1477 * | : |
1478 * +------------------------------------------+
1479 */
1480
1481 ret = write_start_flat_header(s->fd);
1482 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001483 error_setg(errp, "dump: failed to write start flat header");
zhanghailiang4c7e2512014-10-09 14:13:11 +08001484 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001485 }
1486
Janosch Frank86a518b2022-03-30 12:35:55 +00001487 write_dump_header(s, errp);
1488 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +08001489 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001490 }
1491
Janosch Frank86a518b2022-03-30 12:35:55 +00001492 write_dump_bitmap(s, errp);
1493 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +08001494 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001495 }
1496
Janosch Frank86a518b2022-03-30 12:35:55 +00001497 write_dump_pages(s, errp);
1498 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +08001499 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001500 }
1501
1502 ret = write_end_flat_header(s->fd);
1503 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001504 error_setg(errp, "dump: failed to write end flat header");
zhanghailiang4c7e2512014-10-09 14:13:11 +08001505 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001506 }
qiaonuohanb53ccc32014-02-18 14:11:36 +08001507}
1508
Janosch Frank0c2994a2022-08-11 12:10:57 +00001509static int validate_start_block(DumpState *s)
Wen Congyang783e9b42012-05-07 12:10:47 +08001510{
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001511 GuestPhysBlock *block;
Wen Congyang783e9b42012-05-07 12:10:47 +08001512
Janosch Frankdddf7252022-08-11 12:10:58 +00001513 if (!dump_has_filter(s)) {
Wen Congyang783e9b42012-05-07 12:10:47 +08001514 return 0;
1515 }
1516
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001517 QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
Janosch Frank0c2994a2022-08-11 12:10:57 +00001518 /* This block is out of the range */
Janosch Frankdddf7252022-08-11 12:10:58 +00001519 if (block->target_start >= s->filter_area_begin + s->filter_area_length ||
1520 block->target_end <= s->filter_area_begin) {
Wen Congyang783e9b42012-05-07 12:10:47 +08001521 continue;
1522 }
Janosch Frank0c2994a2022-08-11 12:10:57 +00001523 return 0;
1524 }
Wen Congyang783e9b42012-05-07 12:10:47 +08001525
1526 return -1;
1527}
1528
qiaonuohan7aad2482014-02-18 14:11:31 +08001529static void get_max_mapnr(DumpState *s)
1530{
1531 GuestPhysBlock *last_block;
1532
Paolo Bonzinieae3eb32018-12-06 13:10:34 +01001533 last_block = QTAILQ_LAST(&s->guest_phys_blocks.head);
Andrew Jones8161bef2016-01-11 20:56:20 +01001534 s->max_mapnr = dump_paddr_to_pfn(s, last_block->target_end);
qiaonuohan7aad2482014-02-18 14:11:31 +08001535}
1536
Peter Xubaf28f52016-02-18 13:16:48 +08001537static DumpState dump_state_global = { .status = DUMP_STATUS_NONE };
1538
1539static void dump_state_prepare(DumpState *s)
1540{
1541 /* zero the struct, setting status to active */
1542 *s = (DumpState) { .status = DUMP_STATUS_ACTIVE };
1543}
1544
Marc-André Lureau544803c2022-03-23 19:57:31 +04001545bool qemu_system_dump_in_progress(void)
Peter Xu65d64f32016-02-18 13:16:49 +08001546{
1547 DumpState *state = &dump_state_global;
Stefan Hajnoczid73415a2020-09-23 11:56:46 +01001548 return (qatomic_read(&state->status) == DUMP_STATUS_ACTIVE);
Peter Xu65d64f32016-02-18 13:16:49 +08001549}
1550
Peter Xu2264c2c2016-02-18 13:16:53 +08001551/* calculate total size of memory to be dumped (taking filter into
1552 * acoount.) */
1553static int64_t dump_calculate_size(DumpState *s)
1554{
1555 GuestPhysBlock *block;
1556 int64_t size = 0, total = 0, left = 0, right = 0;
1557
1558 QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
Janosch Frankdddf7252022-08-11 12:10:58 +00001559 if (dump_has_filter(s)) {
Peter Xu2264c2c2016-02-18 13:16:53 +08001560 /* calculate the overlapped region. */
Janosch Frankdddf7252022-08-11 12:10:58 +00001561 left = MAX(s->filter_area_begin, block->target_start);
1562 right = MIN(s->filter_area_begin + s->filter_area_length, block->target_end);
Peter Xu2264c2c2016-02-18 13:16:53 +08001563 size = right - left;
1564 size = size > 0 ? size : 0;
1565 } else {
1566 /* count the whole region in */
1567 size = (block->target_end - block->target_start);
1568 }
1569 total += size;
1570 }
1571
1572 return total;
1573}
1574
Marc-André Lureaud9feb512017-09-11 18:59:26 +02001575static void vmcoreinfo_update_phys_base(DumpState *s)
1576{
1577 uint64_t size, note_head_size, name_size, phys_base;
1578 char **lines;
1579 uint8_t *vmci;
1580 size_t i;
1581
1582 if (!note_name_equal(s, s->guest_note, "VMCOREINFO")) {
1583 return;
1584 }
1585
1586 get_note_sizes(s, s->guest_note, &note_head_size, &name_size, &size);
1587 note_head_size = ROUND_UP(note_head_size, 4);
1588
1589 vmci = s->guest_note + note_head_size + ROUND_UP(name_size, 4);
1590 *(vmci + size) = '\0';
1591
1592 lines = g_strsplit((char *)vmci, "\n", -1);
1593 for (i = 0; lines[i]; i++) {
Wei Huang68cbecf2018-03-09 12:03:23 -05001594 const char *prefix = NULL;
1595
1596 if (s->dump_info.d_machine == EM_X86_64) {
1597 prefix = "NUMBER(phys_base)=";
1598 } else if (s->dump_info.d_machine == EM_AARCH64) {
1599 prefix = "NUMBER(PHYS_OFFSET)=";
1600 }
1601
1602 if (prefix && g_str_has_prefix(lines[i], prefix)) {
1603 if (qemu_strtou64(lines[i] + strlen(prefix), NULL, 16,
Marc-André Lureaud9feb512017-09-11 18:59:26 +02001604 &phys_base) < 0) {
Wei Huang68cbecf2018-03-09 12:03:23 -05001605 warn_report("Failed to read %s", prefix);
Marc-André Lureaud9feb512017-09-11 18:59:26 +02001606 } else {
1607 s->dump_info.phys_base = phys_base;
1608 }
1609 break;
1610 }
1611 }
1612
1613 g_strfreev(lines);
1614}
1615
zhanghailiang4c7e2512014-10-09 14:13:11 +08001616static void dump_init(DumpState *s, int fd, bool has_format,
1617 DumpGuestMemoryFormat format, bool paging, bool has_filter,
1618 int64_t begin, int64_t length, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +08001619{
Janosch Frank86a518b2022-03-30 12:35:55 +00001620 ERRP_GUARD();
Marc-André Lureau903ef732017-09-11 18:59:25 +02001621 VMCoreInfoState *vmci = vmcoreinfo_find();
Andreas Färber182735e2013-05-29 22:29:20 +02001622 CPUState *cpu;
Wen Congyang783e9b42012-05-07 12:10:47 +08001623 int nr_cpus;
1624 int ret;
1625
Peter Xuca1fc8c2016-02-18 13:16:50 +08001626 s->has_format = has_format;
1627 s->format = format;
Peter Xu2264c2c2016-02-18 13:16:53 +08001628 s->written_size = 0;
Peter Xuca1fc8c2016-02-18 13:16:50 +08001629
qiaonuohanb53ccc32014-02-18 14:11:36 +08001630 /* kdump-compressed is conflict with paging and filter */
1631 if (has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
1632 assert(!paging && !has_filter);
1633 }
1634
Wen Congyang783e9b42012-05-07 12:10:47 +08001635 if (runstate_is_running()) {
1636 vm_stop(RUN_STATE_SAVE_VM);
1637 s->resume = true;
1638 } else {
1639 s->resume = false;
1640 }
1641
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001642 /* If we use KVM, we should synchronize the registers before we get dump
1643 * info or physmap info.
Wen Congyang783e9b42012-05-07 12:10:47 +08001644 */
Andreas Färber1b3509c2013-06-09 16:48:29 +02001645 cpu_synchronize_all_states();
Wen Congyang783e9b42012-05-07 12:10:47 +08001646 nr_cpus = 0;
Andreas Färberbdc44642013-06-24 23:50:24 +02001647 CPU_FOREACH(cpu) {
Wen Congyang783e9b42012-05-07 12:10:47 +08001648 nr_cpus++;
1649 }
1650
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001651 s->fd = fd;
Janosch Frankdddf7252022-08-11 12:10:58 +00001652 if (has_filter && !length) {
1653 error_setg(errp, QERR_INVALID_PARAMETER, "length");
1654 goto cleanup;
1655 }
1656 s->filter_area_begin = begin;
1657 s->filter_area_length = length;
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001658
Chen Gang29282072014-08-03 23:28:56 +08001659 memory_mapping_list_init(&s->list);
1660
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001661 guest_phys_blocks_init(&s->guest_phys_blocks);
Laszlo Ersekc5d7f602013-08-06 12:37:10 +02001662 guest_phys_blocks_append(&s->guest_phys_blocks);
Peter Xu2264c2c2016-02-18 13:16:53 +08001663 s->total_size = dump_calculate_size(s);
1664#ifdef DEBUG_DUMP_GUEST_MEMORY
1665 fprintf(stderr, "DUMP: total memory to dump: %lu\n", s->total_size);
1666#endif
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001667
Cornelia Huckd1e69942017-09-13 16:20:35 +02001668 /* it does not make sense to dump non-existent memory */
1669 if (!s->total_size) {
1670 error_setg(errp, "dump: no guest memory to dump");
1671 goto cleanup;
1672 }
1673
Janosch Frank0c2994a2022-08-11 12:10:57 +00001674 /* Is the filter filtering everything? */
1675 if (validate_start_block(s) == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001676 error_setg(errp, QERR_INVALID_PARAMETER, "begin");
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001677 goto cleanup;
1678 }
1679
1680 /* get dump info: endian, class and architecture.
1681 * If the target architecture is not supported, cpu_get_dump_info() will
1682 * return -1.
1683 */
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001684 ret = cpu_get_dump_info(&s->dump_info, &s->guest_phys_blocks);
Wen Congyang783e9b42012-05-07 12:10:47 +08001685 if (ret < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001686 error_setg(errp, QERR_UNSUPPORTED);
Wen Congyang783e9b42012-05-07 12:10:47 +08001687 goto cleanup;
1688 }
1689
Andrew Jones8161bef2016-01-11 20:56:20 +01001690 if (!s->dump_info.page_size) {
1691 s->dump_info.page_size = TARGET_PAGE_SIZE;
1692 }
1693
Paolo Bonzini4720bd02012-06-07 08:48:09 +02001694 s->note_size = cpu_get_note_size(s->dump_info.d_class,
1695 s->dump_info.d_machine, nr_cpus);
Aneesh Kumar K.Vbb6b6842013-10-01 21:49:32 +05301696 if (s->note_size < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001697 error_setg(errp, QERR_UNSUPPORTED);
Paolo Bonzini4720bd02012-06-07 08:48:09 +02001698 goto cleanup;
1699 }
1700
Marc-André Lureau903ef732017-09-11 18:59:25 +02001701 /*
Marc-André Lureaud9feb512017-09-11 18:59:26 +02001702 * The goal of this block is to (a) update the previously guessed
1703 * phys_base, (b) copy the guest note out of the guest.
1704 * Failure to do so is not fatal for dumping.
Marc-André Lureau903ef732017-09-11 18:59:25 +02001705 */
1706 if (vmci) {
1707 uint64_t addr, note_head_size, name_size, desc_size;
1708 uint32_t size;
1709 uint16_t format;
1710
Janosch Frank05bbaa502022-03-30 12:36:00 +00001711 note_head_size = dump_is_64bit(s) ?
1712 sizeof(Elf64_Nhdr) : sizeof(Elf32_Nhdr);
Marc-André Lureau903ef732017-09-11 18:59:25 +02001713
1714 format = le16_to_cpu(vmci->vmcoreinfo.guest_format);
1715 size = le32_to_cpu(vmci->vmcoreinfo.size);
1716 addr = le64_to_cpu(vmci->vmcoreinfo.paddr);
1717 if (!vmci->has_vmcoreinfo) {
1718 warn_report("guest note is not present");
1719 } else if (size < note_head_size || size > MAX_GUEST_NOTE_SIZE) {
1720 warn_report("guest note size is invalid: %" PRIu32, size);
Marc-André Lureau5be5df72018-08-17 17:59:10 +02001721 } else if (format != FW_CFG_VMCOREINFO_FORMAT_ELF) {
Marc-André Lureau903ef732017-09-11 18:59:25 +02001722 warn_report("guest note format is unsupported: %" PRIu16, format);
1723 } else {
1724 s->guest_note = g_malloc(size + 1); /* +1 for adding \0 */
1725 cpu_physical_memory_read(addr, s->guest_note, size);
1726
1727 get_note_sizes(s, s->guest_note, NULL, &name_size, &desc_size);
1728 s->guest_note_size = ELF_NOTE_SIZE(note_head_size, name_size,
1729 desc_size);
1730 if (name_size > MAX_GUEST_NOTE_SIZE ||
1731 desc_size > MAX_GUEST_NOTE_SIZE ||
1732 s->guest_note_size > size) {
1733 warn_report("Invalid guest note header");
1734 g_free(s->guest_note);
1735 s->guest_note = NULL;
1736 } else {
Marc-André Lureaud9feb512017-09-11 18:59:26 +02001737 vmcoreinfo_update_phys_base(s);
Marc-André Lureau903ef732017-09-11 18:59:25 +02001738 s->note_size += s->guest_note_size;
1739 }
1740 }
1741 }
1742
Wen Congyang783e9b42012-05-07 12:10:47 +08001743 /* get memory mapping */
Wen Congyang783e9b42012-05-07 12:10:47 +08001744 if (paging) {
Janosch Frank86a518b2022-03-30 12:35:55 +00001745 qemu_get_guest_memory_mapping(&s->list, &s->guest_phys_blocks, errp);
1746 if (*errp) {
Andreas Färber11ed09c2013-05-29 21:54:03 +02001747 goto cleanup;
1748 }
Wen Congyang783e9b42012-05-07 12:10:47 +08001749 } else {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001750 qemu_get_guest_simple_memory_mapping(&s->list, &s->guest_phys_blocks);
Wen Congyang783e9b42012-05-07 12:10:47 +08001751 }
1752
qiaonuohan7aad2482014-02-18 14:11:31 +08001753 s->nr_cpus = nr_cpus;
qiaonuohan7aad2482014-02-18 14:11:31 +08001754
1755 get_max_mapnr(s);
1756
1757 uint64_t tmp;
Andrew Jones8161bef2016-01-11 20:56:20 +01001758 tmp = DIV_ROUND_UP(DIV_ROUND_UP(s->max_mapnr, CHAR_BIT),
1759 s->dump_info.page_size);
1760 s->len_dump_bitmap = tmp * s->dump_info.page_size;
qiaonuohan7aad2482014-02-18 14:11:31 +08001761
qiaonuohanb53ccc32014-02-18 14:11:36 +08001762 /* init for kdump-compressed format */
1763 if (has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
1764 switch (format) {
1765 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB:
1766 s->flag_compress = DUMP_DH_COMPRESSED_ZLIB;
1767 break;
1768
1769 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO:
Laszlo Ersekc998acb2014-05-20 13:42:22 +02001770#ifdef CONFIG_LZO
1771 if (lzo_init() != LZO_E_OK) {
1772 error_setg(errp, "failed to initialize the LZO library");
1773 goto cleanup;
1774 }
1775#endif
qiaonuohanb53ccc32014-02-18 14:11:36 +08001776 s->flag_compress = DUMP_DH_COMPRESSED_LZO;
1777 break;
1778
1779 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY:
1780 s->flag_compress = DUMP_DH_COMPRESSED_SNAPPY;
1781 break;
1782
1783 default:
1784 s->flag_compress = 0;
1785 }
1786
zhanghailiang4c7e2512014-10-09 14:13:11 +08001787 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001788 }
1789
Janosch Frankdddf7252022-08-11 12:10:58 +00001790 if (dump_has_filter(s)) {
1791 memory_mapping_filter(&s->list, s->filter_area_begin, s->filter_area_length);
Wen Congyang783e9b42012-05-07 12:10:47 +08001792 }
1793
1794 /*
1795 * calculate phdr_num
1796 *
1797 * the type of ehdr->e_phnum is uint16_t, so we should avoid overflow
1798 */
1799 s->phdr_num = 1; /* PT_NOTE */
1800 if (s->list.num < UINT16_MAX - 2) {
Janosch Frank862a3952022-03-30 12:35:57 +00001801 s->shdr_num = 0;
Wen Congyang783e9b42012-05-07 12:10:47 +08001802 s->phdr_num += s->list.num;
Wen Congyang783e9b42012-05-07 12:10:47 +08001803 } else {
Janosch Frank046bc412022-04-07 09:48:24 +00001804 /* sh_info of section 0 holds the real number of phdrs */
Janosch Frank862a3952022-03-30 12:35:57 +00001805 s->shdr_num = 1;
Wen Congyang783e9b42012-05-07 12:10:47 +08001806
1807 /* the type of shdr->sh_info is uint32_t, so we should avoid overflow */
1808 if (s->list.num <= UINT32_MAX - 1) {
Janosch Frank046bc412022-04-07 09:48:24 +00001809 s->phdr_num += s->list.num;
Wen Congyang783e9b42012-05-07 12:10:47 +08001810 } else {
Janosch Frank046bc412022-04-07 09:48:24 +00001811 s->phdr_num = UINT32_MAX;
Wen Congyang783e9b42012-05-07 12:10:47 +08001812 }
1813 }
1814
Janosch Frank05bbaa502022-03-30 12:36:00 +00001815 if (dump_is_64bit(s)) {
Janosch Franke71d3532022-03-30 12:35:59 +00001816 s->phdr_offset = sizeof(Elf64_Ehdr);
1817 s->shdr_offset = s->phdr_offset + sizeof(Elf64_Phdr) * s->phdr_num;
1818 s->note_offset = s->shdr_offset + sizeof(Elf64_Shdr) * s->shdr_num;
1819 s->memory_offset = s->note_offset + s->note_size;
Wen Congyang783e9b42012-05-07 12:10:47 +08001820 } else {
Janosch Franke71d3532022-03-30 12:35:59 +00001821
1822 s->phdr_offset = sizeof(Elf32_Ehdr);
1823 s->shdr_offset = s->phdr_offset + sizeof(Elf32_Phdr) * s->phdr_num;
1824 s->note_offset = s->shdr_offset + sizeof(Elf32_Shdr) * s->shdr_num;
1825 s->memory_offset = s->note_offset + s->note_size;
Wen Congyang783e9b42012-05-07 12:10:47 +08001826 }
1827
zhanghailiang4c7e2512014-10-09 14:13:11 +08001828 return;
Wen Congyang783e9b42012-05-07 12:10:47 +08001829
1830cleanup:
Chen Gang29282072014-08-03 23:28:56 +08001831 dump_cleanup(s);
Wen Congyang783e9b42012-05-07 12:10:47 +08001832}
1833
Peter Xuca1fc8c2016-02-18 13:16:50 +08001834/* this operation might be time consuming. */
1835static void dump_process(DumpState *s, Error **errp)
1836{
Janosch Frank86a518b2022-03-30 12:35:55 +00001837 ERRP_GUARD();
Peter Xud42a0d12016-02-18 13:16:56 +08001838 DumpQueryResult *result = NULL;
Peter Xuca1fc8c2016-02-18 13:16:50 +08001839
Viktor Prutyanov2da91b52018-05-17 19:23:39 +03001840 if (s->has_format && s->format == DUMP_GUEST_MEMORY_FORMAT_WIN_DMP) {
1841#ifdef TARGET_X86_64
Janosch Frank86a518b2022-03-30 12:35:55 +00001842 create_win_dump(s, errp);
Viktor Prutyanov2da91b52018-05-17 19:23:39 +03001843#endif
1844 } else if (s->has_format && s->format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
Janosch Frank86a518b2022-03-30 12:35:55 +00001845 create_kdump_vmcore(s, errp);
Peter Xuca1fc8c2016-02-18 13:16:50 +08001846 } else {
Janosch Frank86a518b2022-03-30 12:35:55 +00001847 create_vmcore(s, errp);
Peter Xuca1fc8c2016-02-18 13:16:50 +08001848 }
1849
Peter Xu39ba2ea2016-02-18 13:16:54 +08001850 /* make sure status is written after written_size updates */
1851 smp_wmb();
Stefan Hajnoczid73415a2020-09-23 11:56:46 +01001852 qatomic_set(&s->status,
Janosch Frank86a518b2022-03-30 12:35:55 +00001853 (*errp ? DUMP_STATUS_FAILED : DUMP_STATUS_COMPLETED));
Peter Xuca1fc8c2016-02-18 13:16:50 +08001854
Peter Xud42a0d12016-02-18 13:16:56 +08001855 /* send DUMP_COMPLETED message (unconditionally) */
1856 result = qmp_query_dump(NULL);
1857 /* should never fail */
1858 assert(result);
Janosch Frank86a518b2022-03-30 12:35:55 +00001859 qapi_event_send_dump_completed(result, !!*errp, (*errp ?
1860 error_get_pretty(*errp) : NULL));
Peter Xud42a0d12016-02-18 13:16:56 +08001861 qapi_free_DumpQueryResult(result);
1862
Peter Xuca1fc8c2016-02-18 13:16:50 +08001863 dump_cleanup(s);
1864}
1865
Peter Xu1fbeff72016-02-18 13:16:52 +08001866static void *dump_thread(void *data)
1867{
Peter Xu1fbeff72016-02-18 13:16:52 +08001868 DumpState *s = (DumpState *)data;
Alberto Garciac3a8fe32017-08-29 15:08:36 +03001869 dump_process(s, NULL);
Peter Xu1fbeff72016-02-18 13:16:52 +08001870 return NULL;
1871}
1872
Peter Xu39ba2ea2016-02-18 13:16:54 +08001873DumpQueryResult *qmp_query_dump(Error **errp)
1874{
1875 DumpQueryResult *result = g_new(DumpQueryResult, 1);
1876 DumpState *state = &dump_state_global;
Stefan Hajnoczid73415a2020-09-23 11:56:46 +01001877 result->status = qatomic_read(&state->status);
Peter Xu39ba2ea2016-02-18 13:16:54 +08001878 /* make sure we are reading status and written_size in order */
1879 smp_rmb();
1880 result->completed = state->written_size;
1881 result->total = state->total_size;
1882 return result;
1883}
1884
Peter Xu228de9c2016-02-18 13:16:47 +08001885void qmp_dump_guest_memory(bool paging, const char *file,
1886 bool has_detach, bool detach,
1887 bool has_begin, int64_t begin, bool has_length,
qiaonuohanb53ccc32014-02-18 14:11:36 +08001888 int64_t length, bool has_format,
1889 DumpGuestMemoryFormat format, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +08001890{
Janosch Frank86a518b2022-03-30 12:35:55 +00001891 ERRP_GUARD();
Wen Congyang783e9b42012-05-07 12:10:47 +08001892 const char *p;
1893 int fd = -1;
1894 DumpState *s;
Peter Xu1fbeff72016-02-18 13:16:52 +08001895 bool detach_p = false;
Wen Congyang783e9b42012-05-07 12:10:47 +08001896
Peter Xu63e27f22016-02-18 13:16:51 +08001897 if (runstate_check(RUN_STATE_INMIGRATE)) {
1898 error_setg(errp, "Dump not allowed during incoming migration.");
1899 return;
1900 }
1901
Peter Xu65d64f32016-02-18 13:16:49 +08001902 /* if there is a dump in background, we should wait until the dump
1903 * finished */
Marc-André Lureau544803c2022-03-23 19:57:31 +04001904 if (qemu_system_dump_in_progress()) {
Peter Xu65d64f32016-02-18 13:16:49 +08001905 error_setg(errp, "There is a dump in process, please wait.");
1906 return;
1907 }
1908
qiaonuohanb53ccc32014-02-18 14:11:36 +08001909 /*
1910 * kdump-compressed format need the whole memory dumped, so paging or
1911 * filter is not supported here.
1912 */
1913 if ((has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) &&
1914 (paging || has_begin || has_length)) {
1915 error_setg(errp, "kdump-compressed format doesn't support paging or "
1916 "filter");
1917 return;
1918 }
Wen Congyang783e9b42012-05-07 12:10:47 +08001919 if (has_begin && !has_length) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001920 error_setg(errp, QERR_MISSING_PARAMETER, "length");
Wen Congyang783e9b42012-05-07 12:10:47 +08001921 return;
1922 }
1923 if (!has_begin && has_length) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001924 error_setg(errp, QERR_MISSING_PARAMETER, "begin");
Wen Congyang783e9b42012-05-07 12:10:47 +08001925 return;
1926 }
Peter Xu1fbeff72016-02-18 13:16:52 +08001927 if (has_detach) {
1928 detach_p = detach;
1929 }
Wen Congyang783e9b42012-05-07 12:10:47 +08001930
qiaonuohanb53ccc32014-02-18 14:11:36 +08001931 /* check whether lzo/snappy is supported */
1932#ifndef CONFIG_LZO
1933 if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO) {
1934 error_setg(errp, "kdump-lzo is not available now");
1935 return;
1936 }
1937#endif
1938
1939#ifndef CONFIG_SNAPPY
1940 if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY) {
1941 error_setg(errp, "kdump-snappy is not available now");
1942 return;
1943 }
1944#endif
1945
Viktor Prutyanov2da91b52018-05-17 19:23:39 +03001946#ifndef TARGET_X86_64
1947 if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_WIN_DMP) {
1948 error_setg(errp, "Windows dump is only available for x86-64");
1949 return;
1950 }
1951#endif
1952
Wen Congyang783e9b42012-05-07 12:10:47 +08001953#if !defined(WIN32)
1954 if (strstart(file, "fd:", &p)) {
Kevin Wolf947e4742020-10-05 17:58:44 +02001955 fd = monitor_get_fd(monitor_cur(), p, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +08001956 if (fd == -1) {
Wen Congyang783e9b42012-05-07 12:10:47 +08001957 return;
1958 }
1959 }
1960#endif
1961
1962 if (strstart(file, "file:", &p)) {
Daniel P. Berrangé448058a2020-07-21 13:25:21 +01001963 fd = qemu_open_old(p, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, S_IRUSR);
Wen Congyang783e9b42012-05-07 12:10:47 +08001964 if (fd < 0) {
Luiz Capitulino75817662013-06-07 14:36:01 -04001965 error_setg_file_open(errp, errno, p);
Wen Congyang783e9b42012-05-07 12:10:47 +08001966 return;
1967 }
1968 }
1969
1970 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001971 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
Wen Congyang783e9b42012-05-07 12:10:47 +08001972 return;
1973 }
1974
Peter Xub7bc6b12021-09-22 12:20:09 -04001975 if (!dump_migration_blocker) {
1976 error_setg(&dump_migration_blocker,
1977 "Live migration disabled: dump-guest-memory in progress");
1978 }
1979
1980 /*
1981 * Allows even for -only-migratable, but forbid migration during the
1982 * process of dump guest memory.
1983 */
1984 if (migrate_add_blocker_internal(dump_migration_blocker, errp)) {
1985 /* Remember to release the fd before passing it over to dump state */
1986 close(fd);
1987 return;
1988 }
1989
Peter Xubaf28f52016-02-18 13:16:48 +08001990 s = &dump_state_global;
1991 dump_state_prepare(s);
Wen Congyang783e9b42012-05-07 12:10:47 +08001992
zhanghailiang4c7e2512014-10-09 14:13:11 +08001993 dump_init(s, fd, has_format, format, paging, has_begin,
Janosch Frank86a518b2022-03-30 12:35:55 +00001994 begin, length, errp);
1995 if (*errp) {
Stefan Hajnoczid73415a2020-09-23 11:56:46 +01001996 qatomic_set(&s->status, DUMP_STATUS_FAILED);
Wen Congyang783e9b42012-05-07 12:10:47 +08001997 return;
1998 }
1999
Peter Xu1fbeff72016-02-18 13:16:52 +08002000 if (detach_p) {
2001 /* detached dump */
Fam Zheng6796b402017-05-03 15:28:19 +08002002 s->detached = true;
Peter Xu1fbeff72016-02-18 13:16:52 +08002003 qemu_thread_create(&s->dump_thread, "dump_thread", dump_thread,
2004 s, QEMU_THREAD_DETACHED);
2005 } else {
2006 /* sync dump */
2007 dump_process(s, errp);
2008 }
Wen Congyang783e9b42012-05-07 12:10:47 +08002009}
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002010
2011DumpGuestMemoryCapability *qmp_query_dump_guest_memory_capability(Error **errp)
2012{
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002013 DumpGuestMemoryCapability *cap =
Markus Armbrusterb21e2382022-03-15 15:41:56 +01002014 g_new0(DumpGuestMemoryCapability, 1);
Eric Blake95b3a8c2021-01-13 16:10:13 -06002015 DumpGuestMemoryFormatList **tail = &cap->formats;
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002016
2017 /* elf is always available */
Eric Blake95b3a8c2021-01-13 16:10:13 -06002018 QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_ELF);
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002019
2020 /* kdump-zlib is always available */
Eric Blake95b3a8c2021-01-13 16:10:13 -06002021 QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB);
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002022
2023 /* add new item if kdump-lzo is available */
2024#ifdef CONFIG_LZO
Eric Blake95b3a8c2021-01-13 16:10:13 -06002025 QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO);
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002026#endif
2027
2028 /* add new item if kdump-snappy is available */
2029#ifdef CONFIG_SNAPPY
Eric Blake95b3a8c2021-01-13 16:10:13 -06002030 QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY);
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002031#endif
2032
Viktor Prutyanov2da91b52018-05-17 19:23:39 +03002033 /* Windows dump is available only if target is x86_64 */
2034#ifdef TARGET_X86_64
Eric Blake95b3a8c2021-01-13 16:10:13 -06002035 QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_WIN_DMP);
Viktor Prutyanov2da91b52018-05-17 19:23:39 +03002036#endif
2037
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002038 return cap;
2039}