blob: 5d71c47d06aa8a44a3f60e8e2f0ab796b6798f5c [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"
Markus Armbrustera8d25322019-05-23 16:35:08 +020015#include "qemu-common.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020016#include "qemu/cutils.h"
Wen Congyang783e9b42012-05-07 12:10:47 +080017#include "elf.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010018#include "exec/hwaddr.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010019#include "monitor/monitor.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010020#include "sysemu/kvm.h"
21#include "sysemu/dump.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010022#include "sysemu/memory_mapping.h"
Markus Armbruster54d31232019-08-12 07:23:59 +020023#include "sysemu/runstate.h"
Andreas Färber1b3509c2013-06-09 16:48:29 +020024#include "sysemu/cpus.h"
Markus Armbrustere688df62018-02-01 12:18:31 +010025#include "qapi/error.h"
Markus Armbrusterd06b7472019-06-19 22:10:47 +020026#include "qapi/qapi-commands-dump.h"
27#include "qapi/qapi-events-dump.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010028#include "qapi/qmp/qerror.h"
Marc-André Lureau903ef732017-09-11 18:59:25 +020029#include "qemu/error-report.h"
Markus Armbrusterdb725812019-08-12 07:23:50 +020030#include "qemu/main-loop.h"
Marc-André Lureau903ef732017-09-11 18:59:25 +020031#include "hw/misc/vmcoreinfo.h"
Peter Xub7bc6b12021-09-22 12:20:09 -040032#include "migration/blocker.h"
Wen Congyang783e9b42012-05-07 12:10:47 +080033
Viktor Prutyanov2da91b52018-05-17 19:23:39 +030034#ifdef TARGET_X86_64
35#include "win_dump.h"
36#endif
37
qiaonuohand12f57e2014-02-18 14:11:35 +080038#include <zlib.h>
39#ifdef CONFIG_LZO
40#include <lzo/lzo1x.h>
41#endif
42#ifdef CONFIG_SNAPPY
43#include <snappy-c.h>
44#endif
qiaonuohan4ab23a92014-02-18 14:11:37 +080045#ifndef ELF_MACHINE_UNAME
46#define ELF_MACHINE_UNAME "Unknown"
47#endif
qiaonuohand12f57e2014-02-18 14:11:35 +080048
Marc-André Lureau903ef732017-09-11 18:59:25 +020049#define MAX_GUEST_NOTE_SIZE (1 << 20) /* 1MB should be enough */
50
Peter Xub7bc6b12021-09-22 12:20:09 -040051static Error *dump_migration_blocker;
52
Marc-André Lureau903ef732017-09-11 18:59:25 +020053#define ELF_NOTE_SIZE(hdr_size, name_size, desc_size) \
54 ((DIV_ROUND_UP((hdr_size), 4) + \
55 DIV_ROUND_UP((name_size), 4) + \
56 DIV_ROUND_UP((desc_size), 4)) * 4)
57
Bharata B Raoacb0ef52014-05-19 19:57:44 +020058uint16_t cpu_to_dump16(DumpState *s, uint16_t val)
Wen Congyang783e9b42012-05-07 12:10:47 +080059{
Bharata B Raoacb0ef52014-05-19 19:57:44 +020060 if (s->dump_info.d_endian == ELFDATA2LSB) {
Wen Congyang783e9b42012-05-07 12:10:47 +080061 val = cpu_to_le16(val);
62 } else {
63 val = cpu_to_be16(val);
64 }
65
66 return val;
67}
68
Bharata B Raoacb0ef52014-05-19 19:57:44 +020069uint32_t cpu_to_dump32(DumpState *s, uint32_t val)
Wen Congyang783e9b42012-05-07 12:10:47 +080070{
Bharata B Raoacb0ef52014-05-19 19:57:44 +020071 if (s->dump_info.d_endian == ELFDATA2LSB) {
Wen Congyang783e9b42012-05-07 12:10:47 +080072 val = cpu_to_le32(val);
73 } else {
74 val = cpu_to_be32(val);
75 }
76
77 return val;
78}
79
Bharata B Raoacb0ef52014-05-19 19:57:44 +020080uint64_t cpu_to_dump64(DumpState *s, uint64_t val)
Wen Congyang783e9b42012-05-07 12:10:47 +080081{
Bharata B Raoacb0ef52014-05-19 19:57:44 +020082 if (s->dump_info.d_endian == ELFDATA2LSB) {
Wen Congyang783e9b42012-05-07 12:10:47 +080083 val = cpu_to_le64(val);
84 } else {
85 val = cpu_to_be64(val);
86 }
87
88 return val;
89}
90
Wen Congyang783e9b42012-05-07 12:10:47 +080091static int dump_cleanup(DumpState *s)
92{
Laszlo Ersek5ee163e2013-08-06 12:37:09 +020093 guest_phys_blocks_free(&s->guest_phys_blocks);
Wen Congyang783e9b42012-05-07 12:10:47 +080094 memory_mapping_list_free(&s->list);
Chen Gang29282072014-08-03 23:28:56 +080095 close(s->fd);
Marc-André Lureau903ef732017-09-11 18:59:25 +020096 g_free(s->guest_note);
97 s->guest_note = NULL;
Wen Congyang783e9b42012-05-07 12:10:47 +080098 if (s->resume) {
Fam Zheng6796b402017-05-03 15:28:19 +080099 if (s->detached) {
100 qemu_mutex_lock_iothread();
101 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800102 vm_start();
Fam Zheng6796b402017-05-03 15:28:19 +0800103 if (s->detached) {
104 qemu_mutex_unlock_iothread();
105 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800106 }
Peter Xub7bc6b12021-09-22 12:20:09 -0400107 migrate_del_blocker(dump_migration_blocker);
Wen Congyang783e9b42012-05-07 12:10:47 +0800108
Chen Gang29282072014-08-03 23:28:56 +0800109 return 0;
Wen Congyang783e9b42012-05-07 12:10:47 +0800110}
111
qiaonuohanb5ba1cc2014-02-18 14:11:25 +0800112static int fd_write_vmcore(const void *buf, size_t size, void *opaque)
Wen Congyang783e9b42012-05-07 12:10:47 +0800113{
114 DumpState *s = opaque;
Luiz Capitulino2f616522012-09-21 13:17:55 -0300115 size_t written_size;
Wen Congyang783e9b42012-05-07 12:10:47 +0800116
Luiz Capitulino2f616522012-09-21 13:17:55 -0300117 written_size = qemu_write_full(s->fd, buf, size);
118 if (written_size != size) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200119 return -errno;
Wen Congyang783e9b42012-05-07 12:10:47 +0800120 }
121
122 return 0;
123}
124
zhanghailiang4c7e2512014-10-09 14:13:11 +0800125static void write_elf64_header(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800126{
127 Elf64_Ehdr elf_header;
128 int ret;
Wen Congyang783e9b42012-05-07 12:10:47 +0800129
130 memset(&elf_header, 0, sizeof(Elf64_Ehdr));
131 memcpy(&elf_header, ELFMAG, SELFMAG);
132 elf_header.e_ident[EI_CLASS] = ELFCLASS64;
133 elf_header.e_ident[EI_DATA] = s->dump_info.d_endian;
134 elf_header.e_ident[EI_VERSION] = EV_CURRENT;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200135 elf_header.e_type = cpu_to_dump16(s, ET_CORE);
136 elf_header.e_machine = cpu_to_dump16(s, s->dump_info.d_machine);
137 elf_header.e_version = cpu_to_dump32(s, EV_CURRENT);
138 elf_header.e_ehsize = cpu_to_dump16(s, sizeof(elf_header));
139 elf_header.e_phoff = cpu_to_dump64(s, sizeof(Elf64_Ehdr));
140 elf_header.e_phentsize = cpu_to_dump16(s, sizeof(Elf64_Phdr));
141 elf_header.e_phnum = cpu_to_dump16(s, s->phdr_num);
Wen Congyang783e9b42012-05-07 12:10:47 +0800142 if (s->have_section) {
143 uint64_t shoff = sizeof(Elf64_Ehdr) + sizeof(Elf64_Phdr) * s->sh_info;
144
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200145 elf_header.e_shoff = cpu_to_dump64(s, shoff);
146 elf_header.e_shentsize = cpu_to_dump16(s, sizeof(Elf64_Shdr));
147 elf_header.e_shnum = cpu_to_dump16(s, 1);
Wen Congyang783e9b42012-05-07 12:10:47 +0800148 }
149
150 ret = fd_write_vmcore(&elf_header, sizeof(elf_header), s);
151 if (ret < 0) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200152 error_setg_errno(errp, -ret, "dump: failed to write elf header");
Wen Congyang783e9b42012-05-07 12:10:47 +0800153 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800154}
155
zhanghailiang4c7e2512014-10-09 14:13:11 +0800156static void write_elf32_header(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800157{
158 Elf32_Ehdr elf_header;
159 int ret;
Wen Congyang783e9b42012-05-07 12:10:47 +0800160
161 memset(&elf_header, 0, sizeof(Elf32_Ehdr));
162 memcpy(&elf_header, ELFMAG, SELFMAG);
163 elf_header.e_ident[EI_CLASS] = ELFCLASS32;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200164 elf_header.e_ident[EI_DATA] = s->dump_info.d_endian;
Wen Congyang783e9b42012-05-07 12:10:47 +0800165 elf_header.e_ident[EI_VERSION] = EV_CURRENT;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200166 elf_header.e_type = cpu_to_dump16(s, ET_CORE);
167 elf_header.e_machine = cpu_to_dump16(s, s->dump_info.d_machine);
168 elf_header.e_version = cpu_to_dump32(s, EV_CURRENT);
169 elf_header.e_ehsize = cpu_to_dump16(s, sizeof(elf_header));
170 elf_header.e_phoff = cpu_to_dump32(s, sizeof(Elf32_Ehdr));
171 elf_header.e_phentsize = cpu_to_dump16(s, sizeof(Elf32_Phdr));
172 elf_header.e_phnum = cpu_to_dump16(s, s->phdr_num);
Wen Congyang783e9b42012-05-07 12:10:47 +0800173 if (s->have_section) {
174 uint32_t shoff = sizeof(Elf32_Ehdr) + sizeof(Elf32_Phdr) * s->sh_info;
175
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200176 elf_header.e_shoff = cpu_to_dump32(s, shoff);
177 elf_header.e_shentsize = cpu_to_dump16(s, sizeof(Elf32_Shdr));
178 elf_header.e_shnum = cpu_to_dump16(s, 1);
Wen Congyang783e9b42012-05-07 12:10:47 +0800179 }
180
181 ret = fd_write_vmcore(&elf_header, sizeof(elf_header), s);
182 if (ret < 0) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200183 error_setg_errno(errp, -ret, "dump: failed to write elf header");
Wen Congyang783e9b42012-05-07 12:10:47 +0800184 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800185}
186
zhanghailiang4c7e2512014-10-09 14:13:11 +0800187static void write_elf64_load(DumpState *s, MemoryMapping *memory_mapping,
188 int phdr_index, hwaddr offset,
189 hwaddr filesz, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800190{
191 Elf64_Phdr phdr;
192 int ret;
Wen Congyang783e9b42012-05-07 12:10:47 +0800193
194 memset(&phdr, 0, sizeof(Elf64_Phdr));
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200195 phdr.p_type = cpu_to_dump32(s, PT_LOAD);
196 phdr.p_offset = cpu_to_dump64(s, offset);
197 phdr.p_paddr = cpu_to_dump64(s, memory_mapping->phys_addr);
198 phdr.p_filesz = cpu_to_dump64(s, filesz);
199 phdr.p_memsz = cpu_to_dump64(s, memory_mapping->length);
Jon Dorone17bebd2019-01-09 10:22:03 +0200200 phdr.p_vaddr = cpu_to_dump64(s, memory_mapping->virt_addr) ?: phdr.p_paddr;
Wen Congyang783e9b42012-05-07 12:10:47 +0800201
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200202 assert(memory_mapping->length >= filesz);
203
Wen Congyang783e9b42012-05-07 12:10:47 +0800204 ret = fd_write_vmcore(&phdr, sizeof(Elf64_Phdr), s);
205 if (ret < 0) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200206 error_setg_errno(errp, -ret,
207 "dump: failed to write program header table");
Wen Congyang783e9b42012-05-07 12:10:47 +0800208 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800209}
210
zhanghailiang4c7e2512014-10-09 14:13:11 +0800211static void write_elf32_load(DumpState *s, MemoryMapping *memory_mapping,
212 int phdr_index, hwaddr offset,
213 hwaddr filesz, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800214{
215 Elf32_Phdr phdr;
216 int ret;
Wen Congyang783e9b42012-05-07 12:10:47 +0800217
218 memset(&phdr, 0, sizeof(Elf32_Phdr));
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200219 phdr.p_type = cpu_to_dump32(s, PT_LOAD);
220 phdr.p_offset = cpu_to_dump32(s, offset);
221 phdr.p_paddr = cpu_to_dump32(s, memory_mapping->phys_addr);
222 phdr.p_filesz = cpu_to_dump32(s, filesz);
223 phdr.p_memsz = cpu_to_dump32(s, memory_mapping->length);
Jon Dorone17bebd2019-01-09 10:22:03 +0200224 phdr.p_vaddr =
225 cpu_to_dump32(s, memory_mapping->virt_addr) ?: phdr.p_paddr;
Wen Congyang783e9b42012-05-07 12:10:47 +0800226
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200227 assert(memory_mapping->length >= filesz);
228
Wen Congyang783e9b42012-05-07 12:10:47 +0800229 ret = fd_write_vmcore(&phdr, sizeof(Elf32_Phdr), s);
230 if (ret < 0) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200231 error_setg_errno(errp, -ret,
232 "dump: failed to write program header table");
Wen Congyang783e9b42012-05-07 12:10:47 +0800233 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800234}
235
zhanghailiang4c7e2512014-10-09 14:13:11 +0800236static void write_elf64_note(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800237{
238 Elf64_Phdr phdr;
Avi Kivitya8170e52012-10-23 12:30:10 +0200239 hwaddr begin = s->memory_offset - s->note_size;
Wen Congyang783e9b42012-05-07 12:10:47 +0800240 int ret;
241
242 memset(&phdr, 0, sizeof(Elf64_Phdr));
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200243 phdr.p_type = cpu_to_dump32(s, PT_NOTE);
244 phdr.p_offset = cpu_to_dump64(s, begin);
Wen Congyang783e9b42012-05-07 12:10:47 +0800245 phdr.p_paddr = 0;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200246 phdr.p_filesz = cpu_to_dump64(s, s->note_size);
247 phdr.p_memsz = cpu_to_dump64(s, s->note_size);
Wen Congyang783e9b42012-05-07 12:10:47 +0800248 phdr.p_vaddr = 0;
249
250 ret = fd_write_vmcore(&phdr, sizeof(Elf64_Phdr), s);
251 if (ret < 0) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200252 error_setg_errno(errp, -ret,
253 "dump: failed to write program header table");
Wen Congyang783e9b42012-05-07 12:10:47 +0800254 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800255}
256
Paolo Bonzini0bc3cd62013-04-08 17:29:59 +0200257static inline int cpu_index(CPUState *cpu)
258{
259 return cpu->cpu_index + 1;
260}
261
Marc-André Lureau903ef732017-09-11 18:59:25 +0200262static void write_guest_note(WriteCoreDumpFunction f, DumpState *s,
263 Error **errp)
264{
265 int ret;
266
267 if (s->guest_note) {
268 ret = f(s->guest_note, s->guest_note_size, s);
269 if (ret < 0) {
270 error_setg(errp, "dump: failed to write guest note");
271 }
272 }
273}
274
zhanghailiang4c7e2512014-10-09 14:13:11 +0800275static void write_elf64_notes(WriteCoreDumpFunction f, DumpState *s,
276 Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800277{
Andreas Färber0d342822012-12-17 07:12:13 +0100278 CPUState *cpu;
Wen Congyang783e9b42012-05-07 12:10:47 +0800279 int ret;
280 int id;
281
Andreas Färberbdc44642013-06-24 23:50:24 +0200282 CPU_FOREACH(cpu) {
Andreas Färber0d342822012-12-17 07:12:13 +0100283 id = cpu_index(cpu);
qiaonuohan6a519912014-02-18 14:11:26 +0800284 ret = cpu_write_elf64_note(f, cpu, id, s);
Wen Congyang783e9b42012-05-07 12:10:47 +0800285 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800286 error_setg(errp, "dump: failed to write elf notes");
zhanghailiang4c7e2512014-10-09 14:13:11 +0800287 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800288 }
289 }
290
Andreas Färberbdc44642013-06-24 23:50:24 +0200291 CPU_FOREACH(cpu) {
qiaonuohan6a519912014-02-18 14:11:26 +0800292 ret = cpu_write_elf64_qemunote(f, cpu, s);
Wen Congyang783e9b42012-05-07 12:10:47 +0800293 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800294 error_setg(errp, "dump: failed to write CPU status");
zhanghailiang4c7e2512014-10-09 14:13:11 +0800295 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800296 }
297 }
Marc-André Lureau903ef732017-09-11 18:59:25 +0200298
299 write_guest_note(f, s, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800300}
301
zhanghailiang4c7e2512014-10-09 14:13:11 +0800302static void write_elf32_note(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800303{
Avi Kivitya8170e52012-10-23 12:30:10 +0200304 hwaddr begin = s->memory_offset - s->note_size;
Wen Congyang783e9b42012-05-07 12:10:47 +0800305 Elf32_Phdr phdr;
Wen Congyang783e9b42012-05-07 12:10:47 +0800306 int ret;
307
308 memset(&phdr, 0, sizeof(Elf32_Phdr));
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200309 phdr.p_type = cpu_to_dump32(s, PT_NOTE);
310 phdr.p_offset = cpu_to_dump32(s, begin);
Wen Congyang783e9b42012-05-07 12:10:47 +0800311 phdr.p_paddr = 0;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200312 phdr.p_filesz = cpu_to_dump32(s, s->note_size);
313 phdr.p_memsz = cpu_to_dump32(s, s->note_size);
Wen Congyang783e9b42012-05-07 12:10:47 +0800314 phdr.p_vaddr = 0;
315
316 ret = fd_write_vmcore(&phdr, sizeof(Elf32_Phdr), s);
317 if (ret < 0) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200318 error_setg_errno(errp, -ret,
319 "dump: failed to write program header table");
Wen Congyang783e9b42012-05-07 12:10:47 +0800320 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800321}
322
zhanghailiang4c7e2512014-10-09 14:13:11 +0800323static void write_elf32_notes(WriteCoreDumpFunction f, DumpState *s,
324 Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800325{
Andreas Färber0d342822012-12-17 07:12:13 +0100326 CPUState *cpu;
Wen Congyang783e9b42012-05-07 12:10:47 +0800327 int ret;
328 int id;
329
Andreas Färberbdc44642013-06-24 23:50:24 +0200330 CPU_FOREACH(cpu) {
Andreas Färber0d342822012-12-17 07:12:13 +0100331 id = cpu_index(cpu);
qiaonuohan6a519912014-02-18 14:11:26 +0800332 ret = cpu_write_elf32_note(f, cpu, id, s);
Wen Congyang783e9b42012-05-07 12:10:47 +0800333 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800334 error_setg(errp, "dump: failed to write elf notes");
zhanghailiang4c7e2512014-10-09 14:13:11 +0800335 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800336 }
337 }
338
Andreas Färberbdc44642013-06-24 23:50:24 +0200339 CPU_FOREACH(cpu) {
qiaonuohan6a519912014-02-18 14:11:26 +0800340 ret = cpu_write_elf32_qemunote(f, cpu, s);
Wen Congyang783e9b42012-05-07 12:10:47 +0800341 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800342 error_setg(errp, "dump: failed to write CPU status");
zhanghailiang4c7e2512014-10-09 14:13:11 +0800343 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800344 }
345 }
Marc-André Lureau903ef732017-09-11 18:59:25 +0200346
347 write_guest_note(f, s, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800348}
349
zhanghailiang4c7e2512014-10-09 14:13:11 +0800350static void write_elf_section(DumpState *s, int type, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800351{
352 Elf32_Shdr shdr32;
353 Elf64_Shdr shdr64;
Wen Congyang783e9b42012-05-07 12:10:47 +0800354 int shdr_size;
355 void *shdr;
356 int ret;
357
358 if (type == 0) {
359 shdr_size = sizeof(Elf32_Shdr);
360 memset(&shdr32, 0, shdr_size);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200361 shdr32.sh_info = cpu_to_dump32(s, s->sh_info);
Wen Congyang783e9b42012-05-07 12:10:47 +0800362 shdr = &shdr32;
363 } else {
364 shdr_size = sizeof(Elf64_Shdr);
365 memset(&shdr64, 0, shdr_size);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200366 shdr64.sh_info = cpu_to_dump32(s, s->sh_info);
Wen Congyang783e9b42012-05-07 12:10:47 +0800367 shdr = &shdr64;
368 }
369
Peter Maydell174d2d62020-03-24 17:36:30 +0000370 ret = fd_write_vmcore(shdr, shdr_size, s);
Wen Congyang783e9b42012-05-07 12:10:47 +0800371 if (ret < 0) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200372 error_setg_errno(errp, -ret,
373 "dump: failed to write section header table");
Wen Congyang783e9b42012-05-07 12:10:47 +0800374 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800375}
376
zhanghailiang4c7e2512014-10-09 14:13:11 +0800377static void write_data(DumpState *s, void *buf, int length, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800378{
379 int ret;
380
381 ret = fd_write_vmcore(buf, length, s);
382 if (ret < 0) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200383 error_setg_errno(errp, -ret, "dump: failed to save memory");
Peter Xu2264c2c2016-02-18 13:16:53 +0800384 } else {
385 s->written_size += length;
Wen Congyang783e9b42012-05-07 12:10:47 +0800386 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800387}
388
zhanghailiang4c7e2512014-10-09 14:13:11 +0800389/* write the memory to vmcore. 1 page per I/O. */
390static void write_memory(DumpState *s, GuestPhysBlock *block, ram_addr_t start,
391 int64_t size, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800392{
393 int64_t i;
zhanghailiang4c7e2512014-10-09 14:13:11 +0800394 Error *local_err = NULL;
Wen Congyang783e9b42012-05-07 12:10:47 +0800395
Andrew Jones8161bef2016-01-11 20:56:20 +0100396 for (i = 0; i < size / s->dump_info.page_size; i++) {
397 write_data(s, block->host_addr + start + i * s->dump_info.page_size,
398 s->dump_info.page_size, &local_err);
zhanghailiang4c7e2512014-10-09 14:13:11 +0800399 if (local_err) {
400 error_propagate(errp, local_err);
401 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800402 }
403 }
404
Andrew Jones8161bef2016-01-11 20:56:20 +0100405 if ((size % s->dump_info.page_size) != 0) {
406 write_data(s, block->host_addr + start + i * s->dump_info.page_size,
407 size % s->dump_info.page_size, &local_err);
zhanghailiang4c7e2512014-10-09 14:13:11 +0800408 if (local_err) {
409 error_propagate(errp, local_err);
410 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800411 }
412 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800413}
414
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200415/* get the memory's offset and size in the vmcore */
416static void get_offset_range(hwaddr phys_addr,
417 ram_addr_t mapping_length,
418 DumpState *s,
419 hwaddr *p_offset,
420 hwaddr *p_filesz)
Wen Congyang783e9b42012-05-07 12:10:47 +0800421{
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200422 GuestPhysBlock *block;
Avi Kivitya8170e52012-10-23 12:30:10 +0200423 hwaddr offset = s->memory_offset;
Wen Congyang783e9b42012-05-07 12:10:47 +0800424 int64_t size_in_block, start;
425
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200426 /* When the memory is not stored into vmcore, offset will be -1 */
427 *p_offset = -1;
428 *p_filesz = 0;
429
Wen Congyang783e9b42012-05-07 12:10:47 +0800430 if (s->has_filter) {
431 if (phys_addr < s->begin || phys_addr >= s->begin + s->length) {
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200432 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800433 }
434 }
435
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200436 QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
Wen Congyang783e9b42012-05-07 12:10:47 +0800437 if (s->has_filter) {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200438 if (block->target_start >= s->begin + s->length ||
439 block->target_end <= s->begin) {
Wen Congyang783e9b42012-05-07 12:10:47 +0800440 /* This block is out of the range */
441 continue;
442 }
443
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200444 if (s->begin <= block->target_start) {
445 start = block->target_start;
Wen Congyang783e9b42012-05-07 12:10:47 +0800446 } else {
447 start = s->begin;
448 }
449
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200450 size_in_block = block->target_end - start;
451 if (s->begin + s->length < block->target_end) {
452 size_in_block -= block->target_end - (s->begin + s->length);
Wen Congyang783e9b42012-05-07 12:10:47 +0800453 }
454 } else {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200455 start = block->target_start;
456 size_in_block = block->target_end - block->target_start;
Wen Congyang783e9b42012-05-07 12:10:47 +0800457 }
458
459 if (phys_addr >= start && phys_addr < start + size_in_block) {
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200460 *p_offset = phys_addr - start + offset;
461
462 /* The offset range mapped from the vmcore file must not spill over
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200463 * the GuestPhysBlock, clamp it. The rest of the mapping will be
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200464 * zero-filled in memory at load time; see
465 * <http://refspecs.linuxbase.org/elf/gabi4+/ch5.pheader.html>.
466 */
467 *p_filesz = phys_addr + mapping_length <= start + size_in_block ?
468 mapping_length :
469 size_in_block - (phys_addr - start);
470 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800471 }
472
473 offset += size_in_block;
474 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800475}
476
zhanghailiang4c7e2512014-10-09 14:13:11 +0800477static void write_elf_loads(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800478{
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200479 hwaddr offset, filesz;
Wen Congyang783e9b42012-05-07 12:10:47 +0800480 MemoryMapping *memory_mapping;
481 uint32_t phdr_index = 1;
Wen Congyang783e9b42012-05-07 12:10:47 +0800482 uint32_t max_index;
zhanghailiang4c7e2512014-10-09 14:13:11 +0800483 Error *local_err = NULL;
Wen Congyang783e9b42012-05-07 12:10:47 +0800484
485 if (s->have_section) {
486 max_index = s->sh_info;
487 } else {
488 max_index = s->phdr_num;
489 }
490
491 QTAILQ_FOREACH(memory_mapping, &s->list.head, next) {
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200492 get_offset_range(memory_mapping->phys_addr,
493 memory_mapping->length,
494 s, &offset, &filesz);
Wen Congyang783e9b42012-05-07 12:10:47 +0800495 if (s->dump_info.d_class == ELFCLASS64) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800496 write_elf64_load(s, memory_mapping, phdr_index++, offset,
497 filesz, &local_err);
Wen Congyang783e9b42012-05-07 12:10:47 +0800498 } else {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800499 write_elf32_load(s, memory_mapping, phdr_index++, offset,
500 filesz, &local_err);
Wen Congyang783e9b42012-05-07 12:10:47 +0800501 }
502
zhanghailiang4c7e2512014-10-09 14:13:11 +0800503 if (local_err) {
504 error_propagate(errp, local_err);
505 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800506 }
507
508 if (phdr_index >= max_index) {
509 break;
510 }
511 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800512}
513
514/* write elf header, PT_NOTE and elf note to vmcore. */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800515static void dump_begin(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800516{
zhanghailiang4c7e2512014-10-09 14:13:11 +0800517 Error *local_err = NULL;
Wen Congyang783e9b42012-05-07 12:10:47 +0800518
519 /*
520 * the vmcore's format is:
521 * --------------
522 * | elf header |
523 * --------------
524 * | PT_NOTE |
525 * --------------
526 * | PT_LOAD |
527 * --------------
528 * | ...... |
529 * --------------
530 * | PT_LOAD |
531 * --------------
532 * | sec_hdr |
533 * --------------
534 * | elf note |
535 * --------------
536 * | memory |
537 * --------------
538 *
539 * we only know where the memory is saved after we write elf note into
540 * vmcore.
541 */
542
543 /* write elf header to vmcore */
544 if (s->dump_info.d_class == ELFCLASS64) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800545 write_elf64_header(s, &local_err);
Wen Congyang783e9b42012-05-07 12:10:47 +0800546 } else {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800547 write_elf32_header(s, &local_err);
Wen Congyang783e9b42012-05-07 12:10:47 +0800548 }
zhanghailiang4c7e2512014-10-09 14:13:11 +0800549 if (local_err) {
550 error_propagate(errp, local_err);
551 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800552 }
553
554 if (s->dump_info.d_class == ELFCLASS64) {
555 /* write PT_NOTE to vmcore */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800556 write_elf64_note(s, &local_err);
557 if (local_err) {
558 error_propagate(errp, local_err);
559 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800560 }
561
562 /* write all PT_LOAD to vmcore */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800563 write_elf_loads(s, &local_err);
564 if (local_err) {
565 error_propagate(errp, local_err);
566 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800567 }
568
569 /* write section to vmcore */
570 if (s->have_section) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800571 write_elf_section(s, 1, &local_err);
572 if (local_err) {
573 error_propagate(errp, local_err);
574 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800575 }
576 }
577
578 /* write notes to vmcore */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800579 write_elf64_notes(fd_write_vmcore, s, &local_err);
580 if (local_err) {
581 error_propagate(errp, local_err);
582 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800583 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800584 } else {
585 /* write PT_NOTE to vmcore */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800586 write_elf32_note(s, &local_err);
587 if (local_err) {
588 error_propagate(errp, local_err);
589 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800590 }
591
592 /* write all PT_LOAD to vmcore */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800593 write_elf_loads(s, &local_err);
594 if (local_err) {
595 error_propagate(errp, local_err);
596 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800597 }
598
599 /* write section to vmcore */
600 if (s->have_section) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800601 write_elf_section(s, 0, &local_err);
602 if (local_err) {
603 error_propagate(errp, local_err);
604 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800605 }
606 }
607
608 /* write notes to vmcore */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800609 write_elf32_notes(fd_write_vmcore, s, &local_err);
610 if (local_err) {
611 error_propagate(errp, local_err);
612 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800613 }
614 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800615}
616
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200617static int get_next_block(DumpState *s, GuestPhysBlock *block)
Wen Congyang783e9b42012-05-07 12:10:47 +0800618{
619 while (1) {
Paolo Bonzinia3161032012-11-14 15:54:48 +0100620 block = QTAILQ_NEXT(block, next);
Wen Congyang783e9b42012-05-07 12:10:47 +0800621 if (!block) {
622 /* no more block */
623 return 1;
624 }
625
626 s->start = 0;
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200627 s->next_block = block;
Wen Congyang783e9b42012-05-07 12:10:47 +0800628 if (s->has_filter) {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200629 if (block->target_start >= s->begin + s->length ||
630 block->target_end <= s->begin) {
Wen Congyang783e9b42012-05-07 12:10:47 +0800631 /* This block is out of the range */
632 continue;
633 }
634
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200635 if (s->begin > block->target_start) {
636 s->start = s->begin - block->target_start;
Wen Congyang783e9b42012-05-07 12:10:47 +0800637 }
638 }
639
640 return 0;
641 }
642}
643
644/* write all memory to vmcore */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800645static void dump_iterate(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800646{
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200647 GuestPhysBlock *block;
Wen Congyang783e9b42012-05-07 12:10:47 +0800648 int64_t size;
zhanghailiang4c7e2512014-10-09 14:13:11 +0800649 Error *local_err = NULL;
Wen Congyang783e9b42012-05-07 12:10:47 +0800650
Gonglei08a655b2014-10-30 14:01:17 +0800651 do {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200652 block = s->next_block;
Wen Congyang783e9b42012-05-07 12:10:47 +0800653
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200654 size = block->target_end - block->target_start;
Wen Congyang783e9b42012-05-07 12:10:47 +0800655 if (s->has_filter) {
656 size -= s->start;
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200657 if (s->begin + s->length < block->target_end) {
658 size -= block->target_end - (s->begin + s->length);
Wen Congyang783e9b42012-05-07 12:10:47 +0800659 }
660 }
zhanghailiang4c7e2512014-10-09 14:13:11 +0800661 write_memory(s, block, s->start, size, &local_err);
662 if (local_err) {
663 error_propagate(errp, local_err);
664 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800665 }
666
Gonglei08a655b2014-10-30 14:01:17 +0800667 } while (!get_next_block(s, block));
Wen Congyang783e9b42012-05-07 12:10:47 +0800668}
669
zhanghailiang4c7e2512014-10-09 14:13:11 +0800670static void create_vmcore(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800671{
zhanghailiang4c7e2512014-10-09 14:13:11 +0800672 Error *local_err = NULL;
Wen Congyang783e9b42012-05-07 12:10:47 +0800673
zhanghailiang4c7e2512014-10-09 14:13:11 +0800674 dump_begin(s, &local_err);
675 if (local_err) {
676 error_propagate(errp, local_err);
677 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800678 }
679
zhanghailiang4c7e2512014-10-09 14:13:11 +0800680 dump_iterate(s, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800681}
682
qiaonuohanfda05382014-02-18 14:11:27 +0800683static int write_start_flat_header(int fd)
684{
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200685 MakedumpfileHeader *mh;
qiaonuohanfda05382014-02-18 14:11:27 +0800686 int ret = 0;
687
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200688 QEMU_BUILD_BUG_ON(sizeof *mh > MAX_SIZE_MDF_HEADER);
689 mh = g_malloc0(MAX_SIZE_MDF_HEADER);
qiaonuohanfda05382014-02-18 14:11:27 +0800690
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200691 memcpy(mh->signature, MAKEDUMPFILE_SIGNATURE,
692 MIN(sizeof mh->signature, sizeof MAKEDUMPFILE_SIGNATURE));
qiaonuohanfda05382014-02-18 14:11:27 +0800693
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200694 mh->type = cpu_to_be64(TYPE_FLAT_HEADER);
695 mh->version = cpu_to_be64(VERSION_FLAT_HEADER);
qiaonuohanfda05382014-02-18 14:11:27 +0800696
697 size_t written_size;
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200698 written_size = qemu_write_full(fd, mh, MAX_SIZE_MDF_HEADER);
qiaonuohanfda05382014-02-18 14:11:27 +0800699 if (written_size != MAX_SIZE_MDF_HEADER) {
700 ret = -1;
701 }
702
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200703 g_free(mh);
qiaonuohanfda05382014-02-18 14:11:27 +0800704 return ret;
705}
706
707static int write_end_flat_header(int fd)
708{
709 MakedumpfileDataHeader mdh;
710
711 mdh.offset = END_FLAG_FLAT_HEADER;
712 mdh.buf_size = END_FLAG_FLAT_HEADER;
713
714 size_t written_size;
715 written_size = qemu_write_full(fd, &mdh, sizeof(mdh));
716 if (written_size != sizeof(mdh)) {
717 return -1;
718 }
719
720 return 0;
721}
722
qiaonuohan5d31bab2014-02-18 14:11:28 +0800723static int write_buffer(int fd, off_t offset, const void *buf, size_t size)
724{
725 size_t written_size;
726 MakedumpfileDataHeader mdh;
727
728 mdh.offset = cpu_to_be64(offset);
729 mdh.buf_size = cpu_to_be64(size);
730
731 written_size = qemu_write_full(fd, &mdh, sizeof(mdh));
732 if (written_size != sizeof(mdh)) {
733 return -1;
734 }
735
736 written_size = qemu_write_full(fd, buf, size);
737 if (written_size != size) {
738 return -1;
739 }
740
741 return 0;
742}
743
qiaonuohan4835ef72014-02-18 14:11:29 +0800744static int buf_write_note(const void *buf, size_t size, void *opaque)
745{
746 DumpState *s = opaque;
747
748 /* note_buf is not enough */
749 if (s->note_buf_offset + size > s->note_size) {
750 return -1;
751 }
752
753 memcpy(s->note_buf + s->note_buf_offset, buf, size);
754
755 s->note_buf_offset += size;
756
757 return 0;
758}
759
Marc-André Lureau903ef732017-09-11 18:59:25 +0200760/*
761 * This function retrieves various sizes from an elf header.
762 *
763 * @note has to be a valid ELF note. The return sizes are unmodified
764 * (not padded or rounded up to be multiple of 4).
765 */
766static void get_note_sizes(DumpState *s, const void *note,
767 uint64_t *note_head_size,
768 uint64_t *name_size,
769 uint64_t *desc_size)
770{
771 uint64_t note_head_sz;
772 uint64_t name_sz;
773 uint64_t desc_sz;
774
775 if (s->dump_info.d_class == ELFCLASS64) {
776 const Elf64_Nhdr *hdr = note;
777 note_head_sz = sizeof(Elf64_Nhdr);
778 name_sz = tswap64(hdr->n_namesz);
779 desc_sz = tswap64(hdr->n_descsz);
780 } else {
781 const Elf32_Nhdr *hdr = note;
782 note_head_sz = sizeof(Elf32_Nhdr);
783 name_sz = tswap32(hdr->n_namesz);
784 desc_sz = tswap32(hdr->n_descsz);
785 }
786
787 if (note_head_size) {
788 *note_head_size = note_head_sz;
789 }
790 if (name_size) {
791 *name_size = name_sz;
792 }
793 if (desc_size) {
794 *desc_size = desc_sz;
795 }
796}
797
Marc-André Lureaud9feb512017-09-11 18:59:26 +0200798static bool note_name_equal(DumpState *s,
799 const uint8_t *note, const char *name)
800{
801 int len = strlen(name) + 1;
802 uint64_t head_size, name_size;
803
804 get_note_sizes(s, note, &head_size, &name_size, NULL);
805 head_size = ROUND_UP(head_size, 4);
806
Marc-André Lureauc983ca82017-12-12 15:53:59 +0100807 return name_size == len && memcmp(note + head_size, name, len) == 0;
Marc-André Lureaud9feb512017-09-11 18:59:26 +0200808}
809
qiaonuohan298f1162014-02-18 14:11:32 +0800810/* write common header, sub header and elf note to vmcore */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800811static void create_header32(DumpState *s, Error **errp)
qiaonuohan298f1162014-02-18 14:11:32 +0800812{
qiaonuohan298f1162014-02-18 14:11:32 +0800813 DiskDumpHeader32 *dh = NULL;
814 KdumpSubHeader32 *kh = NULL;
815 size_t size;
qiaonuohan298f1162014-02-18 14:11:32 +0800816 uint32_t block_size;
817 uint32_t sub_hdr_size;
818 uint32_t bitmap_blocks;
819 uint32_t status = 0;
820 uint64_t offset_note;
zhanghailiang4c7e2512014-10-09 14:13:11 +0800821 Error *local_err = NULL;
qiaonuohan298f1162014-02-18 14:11:32 +0800822
823 /* write common header, the version of kdump-compressed format is 6th */
824 size = sizeof(DiskDumpHeader32);
825 dh = g_malloc0(size);
826
Eric Blake84c868f2018-03-27 15:21:51 -0500827 memcpy(dh->signature, KDUMP_SIGNATURE, SIG_LEN);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200828 dh->header_version = cpu_to_dump32(s, 6);
Andrew Jones8161bef2016-01-11 20:56:20 +0100829 block_size = s->dump_info.page_size;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200830 dh->block_size = cpu_to_dump32(s, block_size);
qiaonuohan298f1162014-02-18 14:11:32 +0800831 sub_hdr_size = sizeof(struct KdumpSubHeader32) + s->note_size;
832 sub_hdr_size = DIV_ROUND_UP(sub_hdr_size, block_size);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200833 dh->sub_hdr_size = cpu_to_dump32(s, sub_hdr_size);
qiaonuohan298f1162014-02-18 14:11:32 +0800834 /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200835 dh->max_mapnr = cpu_to_dump32(s, MIN(s->max_mapnr, UINT_MAX));
836 dh->nr_cpus = cpu_to_dump32(s, s->nr_cpus);
qiaonuohan298f1162014-02-18 14:11:32 +0800837 bitmap_blocks = DIV_ROUND_UP(s->len_dump_bitmap, block_size) * 2;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200838 dh->bitmap_blocks = cpu_to_dump32(s, bitmap_blocks);
qiaonuohan4ab23a92014-02-18 14:11:37 +0800839 strncpy(dh->utsname.machine, ELF_MACHINE_UNAME, sizeof(dh->utsname.machine));
qiaonuohan298f1162014-02-18 14:11:32 +0800840
841 if (s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) {
842 status |= DUMP_DH_COMPRESSED_ZLIB;
843 }
844#ifdef CONFIG_LZO
845 if (s->flag_compress & DUMP_DH_COMPRESSED_LZO) {
846 status |= DUMP_DH_COMPRESSED_LZO;
847 }
848#endif
849#ifdef CONFIG_SNAPPY
850 if (s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) {
851 status |= DUMP_DH_COMPRESSED_SNAPPY;
852 }
853#endif
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200854 dh->status = cpu_to_dump32(s, status);
qiaonuohan298f1162014-02-18 14:11:32 +0800855
856 if (write_buffer(s->fd, 0, dh, size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800857 error_setg(errp, "dump: failed to write disk dump header");
qiaonuohan298f1162014-02-18 14:11:32 +0800858 goto out;
859 }
860
861 /* write sub header */
862 size = sizeof(KdumpSubHeader32);
863 kh = g_malloc0(size);
864
865 /* 64bit max_mapnr_64 */
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200866 kh->max_mapnr_64 = cpu_to_dump64(s, s->max_mapnr);
Andrew Jonesb6e05aa2016-01-11 20:56:21 +0100867 kh->phys_base = cpu_to_dump32(s, s->dump_info.phys_base);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200868 kh->dump_level = cpu_to_dump32(s, DUMP_LEVEL);
qiaonuohan298f1162014-02-18 14:11:32 +0800869
870 offset_note = DISKDUMP_HEADER_BLOCKS * block_size + size;
Marc-André Lureau9ada5752017-09-11 18:59:27 +0200871 if (s->guest_note &&
872 note_name_equal(s, s->guest_note, "VMCOREINFO")) {
873 uint64_t hsize, name_size, size_vmcoreinfo_desc, offset_vmcoreinfo;
874
875 get_note_sizes(s, s->guest_note,
876 &hsize, &name_size, &size_vmcoreinfo_desc);
877 offset_vmcoreinfo = offset_note + s->note_size - s->guest_note_size +
878 (DIV_ROUND_UP(hsize, 4) + DIV_ROUND_UP(name_size, 4)) * 4;
879 kh->offset_vmcoreinfo = cpu_to_dump64(s, offset_vmcoreinfo);
880 kh->size_vmcoreinfo = cpu_to_dump32(s, size_vmcoreinfo_desc);
881 }
882
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200883 kh->offset_note = cpu_to_dump64(s, offset_note);
884 kh->note_size = cpu_to_dump32(s, s->note_size);
qiaonuohan298f1162014-02-18 14:11:32 +0800885
886 if (write_buffer(s->fd, DISKDUMP_HEADER_BLOCKS *
887 block_size, kh, size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800888 error_setg(errp, "dump: failed to write kdump sub header");
qiaonuohan298f1162014-02-18 14:11:32 +0800889 goto out;
890 }
891
892 /* write note */
893 s->note_buf = g_malloc0(s->note_size);
894 s->note_buf_offset = 0;
895
896 /* use s->note_buf to store notes temporarily */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800897 write_elf32_notes(buf_write_note, s, &local_err);
898 if (local_err) {
899 error_propagate(errp, local_err);
qiaonuohan298f1162014-02-18 14:11:32 +0800900 goto out;
901 }
qiaonuohan298f1162014-02-18 14:11:32 +0800902 if (write_buffer(s->fd, offset_note, s->note_buf,
903 s->note_size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800904 error_setg(errp, "dump: failed to write notes");
qiaonuohan298f1162014-02-18 14:11:32 +0800905 goto out;
906 }
907
908 /* get offset of dump_bitmap */
909 s->offset_dump_bitmap = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size) *
910 block_size;
911
912 /* get offset of page */
913 s->offset_page = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size + bitmap_blocks) *
914 block_size;
915
916out:
917 g_free(dh);
918 g_free(kh);
919 g_free(s->note_buf);
qiaonuohan298f1162014-02-18 14:11:32 +0800920}
921
922/* write common header, sub header and elf note to vmcore */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800923static void create_header64(DumpState *s, Error **errp)
qiaonuohan298f1162014-02-18 14:11:32 +0800924{
qiaonuohan298f1162014-02-18 14:11:32 +0800925 DiskDumpHeader64 *dh = NULL;
926 KdumpSubHeader64 *kh = NULL;
927 size_t size;
qiaonuohan298f1162014-02-18 14:11:32 +0800928 uint32_t block_size;
929 uint32_t sub_hdr_size;
930 uint32_t bitmap_blocks;
931 uint32_t status = 0;
932 uint64_t offset_note;
zhanghailiang4c7e2512014-10-09 14:13:11 +0800933 Error *local_err = NULL;
qiaonuohan298f1162014-02-18 14:11:32 +0800934
935 /* write common header, the version of kdump-compressed format is 6th */
936 size = sizeof(DiskDumpHeader64);
937 dh = g_malloc0(size);
938
Eric Blake84c868f2018-03-27 15:21:51 -0500939 memcpy(dh->signature, KDUMP_SIGNATURE, SIG_LEN);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200940 dh->header_version = cpu_to_dump32(s, 6);
Andrew Jones8161bef2016-01-11 20:56:20 +0100941 block_size = s->dump_info.page_size;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200942 dh->block_size = cpu_to_dump32(s, block_size);
qiaonuohan298f1162014-02-18 14:11:32 +0800943 sub_hdr_size = sizeof(struct KdumpSubHeader64) + s->note_size;
944 sub_hdr_size = DIV_ROUND_UP(sub_hdr_size, block_size);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200945 dh->sub_hdr_size = cpu_to_dump32(s, sub_hdr_size);
qiaonuohan298f1162014-02-18 14:11:32 +0800946 /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200947 dh->max_mapnr = cpu_to_dump32(s, MIN(s->max_mapnr, UINT_MAX));
948 dh->nr_cpus = cpu_to_dump32(s, s->nr_cpus);
qiaonuohan298f1162014-02-18 14:11:32 +0800949 bitmap_blocks = DIV_ROUND_UP(s->len_dump_bitmap, block_size) * 2;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200950 dh->bitmap_blocks = cpu_to_dump32(s, bitmap_blocks);
qiaonuohan4ab23a92014-02-18 14:11:37 +0800951 strncpy(dh->utsname.machine, ELF_MACHINE_UNAME, sizeof(dh->utsname.machine));
qiaonuohan298f1162014-02-18 14:11:32 +0800952
953 if (s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) {
954 status |= DUMP_DH_COMPRESSED_ZLIB;
955 }
956#ifdef CONFIG_LZO
957 if (s->flag_compress & DUMP_DH_COMPRESSED_LZO) {
958 status |= DUMP_DH_COMPRESSED_LZO;
959 }
960#endif
961#ifdef CONFIG_SNAPPY
962 if (s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) {
963 status |= DUMP_DH_COMPRESSED_SNAPPY;
964 }
965#endif
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200966 dh->status = cpu_to_dump32(s, status);
qiaonuohan298f1162014-02-18 14:11:32 +0800967
968 if (write_buffer(s->fd, 0, dh, size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800969 error_setg(errp, "dump: failed to write disk dump header");
qiaonuohan298f1162014-02-18 14:11:32 +0800970 goto out;
971 }
972
973 /* write sub header */
974 size = sizeof(KdumpSubHeader64);
975 kh = g_malloc0(size);
976
977 /* 64bit max_mapnr_64 */
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200978 kh->max_mapnr_64 = cpu_to_dump64(s, s->max_mapnr);
Andrew Jonesb6e05aa2016-01-11 20:56:21 +0100979 kh->phys_base = cpu_to_dump64(s, s->dump_info.phys_base);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200980 kh->dump_level = cpu_to_dump32(s, DUMP_LEVEL);
qiaonuohan298f1162014-02-18 14:11:32 +0800981
982 offset_note = DISKDUMP_HEADER_BLOCKS * block_size + size;
Marc-André Lureau9ada5752017-09-11 18:59:27 +0200983 if (s->guest_note &&
984 note_name_equal(s, s->guest_note, "VMCOREINFO")) {
985 uint64_t hsize, name_size, size_vmcoreinfo_desc, offset_vmcoreinfo;
986
987 get_note_sizes(s, s->guest_note,
988 &hsize, &name_size, &size_vmcoreinfo_desc);
989 offset_vmcoreinfo = offset_note + s->note_size - s->guest_note_size +
990 (DIV_ROUND_UP(hsize, 4) + DIV_ROUND_UP(name_size, 4)) * 4;
991 kh->offset_vmcoreinfo = cpu_to_dump64(s, offset_vmcoreinfo);
992 kh->size_vmcoreinfo = cpu_to_dump64(s, size_vmcoreinfo_desc);
993 }
994
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200995 kh->offset_note = cpu_to_dump64(s, offset_note);
996 kh->note_size = cpu_to_dump64(s, s->note_size);
qiaonuohan298f1162014-02-18 14:11:32 +0800997
998 if (write_buffer(s->fd, DISKDUMP_HEADER_BLOCKS *
999 block_size, kh, size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001000 error_setg(errp, "dump: failed to write kdump sub header");
qiaonuohan298f1162014-02-18 14:11:32 +08001001 goto out;
1002 }
1003
1004 /* write note */
1005 s->note_buf = g_malloc0(s->note_size);
1006 s->note_buf_offset = 0;
1007
1008 /* use s->note_buf to store notes temporarily */
zhanghailiang4c7e2512014-10-09 14:13:11 +08001009 write_elf64_notes(buf_write_note, s, &local_err);
1010 if (local_err) {
1011 error_propagate(errp, local_err);
qiaonuohan298f1162014-02-18 14:11:32 +08001012 goto out;
1013 }
1014
1015 if (write_buffer(s->fd, offset_note, s->note_buf,
1016 s->note_size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001017 error_setg(errp, "dump: failed to write notes");
qiaonuohan298f1162014-02-18 14:11:32 +08001018 goto out;
1019 }
1020
1021 /* get offset of dump_bitmap */
1022 s->offset_dump_bitmap = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size) *
1023 block_size;
1024
1025 /* get offset of page */
1026 s->offset_page = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size + bitmap_blocks) *
1027 block_size;
1028
1029out:
1030 g_free(dh);
1031 g_free(kh);
1032 g_free(s->note_buf);
qiaonuohan298f1162014-02-18 14:11:32 +08001033}
1034
zhanghailiang4c7e2512014-10-09 14:13:11 +08001035static void write_dump_header(DumpState *s, Error **errp)
qiaonuohan298f1162014-02-18 14:11:32 +08001036{
Laszlo Ersek24aeeac2014-05-20 13:39:45 +02001037 if (s->dump_info.d_class == ELFCLASS32) {
Markus Armbruster992861f2020-07-07 18:06:04 +02001038 create_header32(s, errp);
qiaonuohan298f1162014-02-18 14:11:32 +08001039 } else {
Markus Armbruster992861f2020-07-07 18:06:04 +02001040 create_header64(s, errp);
zhanghailiang4c7e2512014-10-09 14:13:11 +08001041 }
qiaonuohan298f1162014-02-18 14:11:32 +08001042}
1043
Andrew Jones8161bef2016-01-11 20:56:20 +01001044static size_t dump_bitmap_get_bufsize(DumpState *s)
1045{
1046 return s->dump_info.page_size;
1047}
1048
qiaonuohand0686c72014-02-18 14:11:33 +08001049/*
1050 * set dump_bitmap sequencely. the bit before last_pfn is not allowed to be
1051 * rewritten, so if need to set the first bit, set last_pfn and pfn to 0.
1052 * set_dump_bitmap will always leave the recently set bit un-sync. And setting
1053 * (last bit + sizeof(buf) * 8) to 0 will do flushing the content in buf into
1054 * vmcore, ie. synchronizing un-sync bit into vmcore.
1055 */
1056static int set_dump_bitmap(uint64_t last_pfn, uint64_t pfn, bool value,
1057 uint8_t *buf, DumpState *s)
1058{
1059 off_t old_offset, new_offset;
1060 off_t offset_bitmap1, offset_bitmap2;
1061 uint32_t byte, bit;
Andrew Jones8161bef2016-01-11 20:56:20 +01001062 size_t bitmap_bufsize = dump_bitmap_get_bufsize(s);
1063 size_t bits_per_buf = bitmap_bufsize * CHAR_BIT;
qiaonuohand0686c72014-02-18 14:11:33 +08001064
1065 /* should not set the previous place */
1066 assert(last_pfn <= pfn);
1067
1068 /*
1069 * if the bit needed to be set is not cached in buf, flush the data in buf
1070 * to vmcore firstly.
1071 * making new_offset be bigger than old_offset can also sync remained data
1072 * into vmcore.
1073 */
Andrew Jones8161bef2016-01-11 20:56:20 +01001074 old_offset = bitmap_bufsize * (last_pfn / bits_per_buf);
1075 new_offset = bitmap_bufsize * (pfn / bits_per_buf);
qiaonuohand0686c72014-02-18 14:11:33 +08001076
1077 while (old_offset < new_offset) {
1078 /* calculate the offset and write dump_bitmap */
1079 offset_bitmap1 = s->offset_dump_bitmap + old_offset;
1080 if (write_buffer(s->fd, offset_bitmap1, buf,
Andrew Jones8161bef2016-01-11 20:56:20 +01001081 bitmap_bufsize) < 0) {
qiaonuohand0686c72014-02-18 14:11:33 +08001082 return -1;
1083 }
1084
1085 /* dump level 1 is chosen, so 1st and 2nd bitmap are same */
1086 offset_bitmap2 = s->offset_dump_bitmap + s->len_dump_bitmap +
1087 old_offset;
1088 if (write_buffer(s->fd, offset_bitmap2, buf,
Andrew Jones8161bef2016-01-11 20:56:20 +01001089 bitmap_bufsize) < 0) {
qiaonuohand0686c72014-02-18 14:11:33 +08001090 return -1;
1091 }
1092
Andrew Jones8161bef2016-01-11 20:56:20 +01001093 memset(buf, 0, bitmap_bufsize);
1094 old_offset += bitmap_bufsize;
qiaonuohand0686c72014-02-18 14:11:33 +08001095 }
1096
1097 /* get the exact place of the bit in the buf, and set it */
Andrew Jones8161bef2016-01-11 20:56:20 +01001098 byte = (pfn % bits_per_buf) / CHAR_BIT;
1099 bit = (pfn % bits_per_buf) % CHAR_BIT;
qiaonuohand0686c72014-02-18 14:11:33 +08001100 if (value) {
1101 buf[byte] |= 1u << bit;
1102 } else {
1103 buf[byte] &= ~(1u << bit);
1104 }
1105
1106 return 0;
1107}
1108
Andrew Jones8161bef2016-01-11 20:56:20 +01001109static uint64_t dump_paddr_to_pfn(DumpState *s, uint64_t addr)
1110{
1111 int target_page_shift = ctz32(s->dump_info.page_size);
1112
1113 return (addr >> target_page_shift) - ARCH_PFN_OFFSET;
1114}
1115
1116static uint64_t dump_pfn_to_paddr(DumpState *s, uint64_t pfn)
1117{
1118 int target_page_shift = ctz32(s->dump_info.page_size);
1119
1120 return (pfn + ARCH_PFN_OFFSET) << target_page_shift;
1121}
1122
qiaonuohand0686c72014-02-18 14:11:33 +08001123/*
1124 * exam every page and return the page frame number and the address of the page.
1125 * bufptr can be NULL. note: the blocks here is supposed to reflect guest-phys
1126 * blocks, so block->target_start and block->target_end should be interal
1127 * multiples of the target page size.
1128 */
1129static bool get_next_page(GuestPhysBlock **blockptr, uint64_t *pfnptr,
1130 uint8_t **bufptr, DumpState *s)
1131{
1132 GuestPhysBlock *block = *blockptr;
Andrew Jones8161bef2016-01-11 20:56:20 +01001133 hwaddr addr, target_page_mask = ~((hwaddr)s->dump_info.page_size - 1);
qiaonuohand0686c72014-02-18 14:11:33 +08001134 uint8_t *buf;
1135
1136 /* block == NULL means the start of the iteration */
1137 if (!block) {
1138 block = QTAILQ_FIRST(&s->guest_phys_blocks.head);
1139 *blockptr = block;
Andrew Jones8161bef2016-01-11 20:56:20 +01001140 assert((block->target_start & ~target_page_mask) == 0);
1141 assert((block->target_end & ~target_page_mask) == 0);
1142 *pfnptr = dump_paddr_to_pfn(s, block->target_start);
qiaonuohand0686c72014-02-18 14:11:33 +08001143 if (bufptr) {
1144 *bufptr = block->host_addr;
1145 }
1146 return true;
1147 }
1148
1149 *pfnptr = *pfnptr + 1;
Andrew Jones8161bef2016-01-11 20:56:20 +01001150 addr = dump_pfn_to_paddr(s, *pfnptr);
qiaonuohand0686c72014-02-18 14:11:33 +08001151
1152 if ((addr >= block->target_start) &&
Andrew Jones8161bef2016-01-11 20:56:20 +01001153 (addr + s->dump_info.page_size <= block->target_end)) {
qiaonuohand0686c72014-02-18 14:11:33 +08001154 buf = block->host_addr + (addr - block->target_start);
1155 } else {
1156 /* the next page is in the next block */
1157 block = QTAILQ_NEXT(block, next);
1158 *blockptr = block;
1159 if (!block) {
1160 return false;
1161 }
Andrew Jones8161bef2016-01-11 20:56:20 +01001162 assert((block->target_start & ~target_page_mask) == 0);
1163 assert((block->target_end & ~target_page_mask) == 0);
1164 *pfnptr = dump_paddr_to_pfn(s, block->target_start);
qiaonuohand0686c72014-02-18 14:11:33 +08001165 buf = block->host_addr;
1166 }
1167
1168 if (bufptr) {
1169 *bufptr = buf;
1170 }
1171
1172 return true;
1173}
1174
zhanghailiang4c7e2512014-10-09 14:13:11 +08001175static void write_dump_bitmap(DumpState *s, Error **errp)
qiaonuohand0686c72014-02-18 14:11:33 +08001176{
1177 int ret = 0;
1178 uint64_t last_pfn, pfn;
1179 void *dump_bitmap_buf;
1180 size_t num_dumpable;
1181 GuestPhysBlock *block_iter = NULL;
Andrew Jones8161bef2016-01-11 20:56:20 +01001182 size_t bitmap_bufsize = dump_bitmap_get_bufsize(s);
1183 size_t bits_per_buf = bitmap_bufsize * CHAR_BIT;
qiaonuohand0686c72014-02-18 14:11:33 +08001184
1185 /* dump_bitmap_buf is used to store dump_bitmap temporarily */
Andrew Jones8161bef2016-01-11 20:56:20 +01001186 dump_bitmap_buf = g_malloc0(bitmap_bufsize);
qiaonuohand0686c72014-02-18 14:11:33 +08001187
1188 num_dumpable = 0;
1189 last_pfn = 0;
1190
1191 /*
1192 * exam memory page by page, and set the bit in dump_bitmap corresponded
1193 * to the existing page.
1194 */
1195 while (get_next_page(&block_iter, &pfn, NULL, s)) {
1196 ret = set_dump_bitmap(last_pfn, pfn, true, dump_bitmap_buf, s);
1197 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001198 error_setg(errp, "dump: failed to set dump_bitmap");
qiaonuohand0686c72014-02-18 14:11:33 +08001199 goto out;
1200 }
1201
1202 last_pfn = pfn;
1203 num_dumpable++;
1204 }
1205
1206 /*
1207 * set_dump_bitmap will always leave the recently set bit un-sync. Here we
Andrew Jones8161bef2016-01-11 20:56:20 +01001208 * set the remaining bits from last_pfn to the end of the bitmap buffer to
1209 * 0. With those set, the un-sync bit will be synchronized into the vmcore.
qiaonuohand0686c72014-02-18 14:11:33 +08001210 */
1211 if (num_dumpable > 0) {
Andrew Jones8161bef2016-01-11 20:56:20 +01001212 ret = set_dump_bitmap(last_pfn, last_pfn + bits_per_buf, false,
qiaonuohand0686c72014-02-18 14:11:33 +08001213 dump_bitmap_buf, s);
1214 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001215 error_setg(errp, "dump: failed to sync dump_bitmap");
qiaonuohand0686c72014-02-18 14:11:33 +08001216 goto out;
1217 }
1218 }
1219
1220 /* number of dumpable pages that will be dumped later */
1221 s->num_dumpable = num_dumpable;
1222
1223out:
1224 g_free(dump_bitmap_buf);
qiaonuohand0686c72014-02-18 14:11:33 +08001225}
1226
qiaonuohan64cfba62014-02-18 14:11:34 +08001227static void prepare_data_cache(DataCache *data_cache, DumpState *s,
1228 off_t offset)
1229{
1230 data_cache->fd = s->fd;
1231 data_cache->data_size = 0;
Andrew Jones8161bef2016-01-11 20:56:20 +01001232 data_cache->buf_size = 4 * dump_bitmap_get_bufsize(s);
1233 data_cache->buf = g_malloc0(data_cache->buf_size);
qiaonuohan64cfba62014-02-18 14:11:34 +08001234 data_cache->offset = offset;
1235}
1236
1237static int write_cache(DataCache *dc, const void *buf, size_t size,
1238 bool flag_sync)
1239{
1240 /*
1241 * dc->buf_size should not be less than size, otherwise dc will never be
1242 * enough
1243 */
1244 assert(size <= dc->buf_size);
1245
1246 /*
1247 * if flag_sync is set, synchronize data in dc->buf into vmcore.
1248 * otherwise check if the space is enough for caching data in buf, if not,
1249 * write the data in dc->buf to dc->fd and reset dc->buf
1250 */
1251 if ((!flag_sync && dc->data_size + size > dc->buf_size) ||
1252 (flag_sync && dc->data_size > 0)) {
1253 if (write_buffer(dc->fd, dc->offset, dc->buf, dc->data_size) < 0) {
1254 return -1;
1255 }
1256
1257 dc->offset += dc->data_size;
1258 dc->data_size = 0;
1259 }
1260
1261 if (!flag_sync) {
1262 memcpy(dc->buf + dc->data_size, buf, size);
1263 dc->data_size += size;
1264 }
1265
1266 return 0;
1267}
1268
1269static void free_data_cache(DataCache *data_cache)
1270{
1271 g_free(data_cache->buf);
1272}
1273
qiaonuohand12f57e2014-02-18 14:11:35 +08001274static size_t get_len_buf_out(size_t page_size, uint32_t flag_compress)
1275{
Laszlo Ersekb87ef352014-05-20 13:42:53 +02001276 switch (flag_compress) {
1277 case DUMP_DH_COMPRESSED_ZLIB:
1278 return compressBound(page_size);
qiaonuohand12f57e2014-02-18 14:11:35 +08001279
Laszlo Ersekb87ef352014-05-20 13:42:53 +02001280 case DUMP_DH_COMPRESSED_LZO:
1281 /*
1282 * LZO will expand incompressible data by a little amount. Please check
1283 * the following URL to see the expansion calculation:
1284 * http://www.oberhumer.com/opensource/lzo/lzofaq.php
1285 */
1286 return page_size + page_size / 16 + 64 + 3;
qiaonuohand12f57e2014-02-18 14:11:35 +08001287
1288#ifdef CONFIG_SNAPPY
Laszlo Ersekb87ef352014-05-20 13:42:53 +02001289 case DUMP_DH_COMPRESSED_SNAPPY:
1290 return snappy_max_compressed_length(page_size);
qiaonuohand12f57e2014-02-18 14:11:35 +08001291#endif
Laszlo Ersekb87ef352014-05-20 13:42:53 +02001292 }
1293 return 0;
qiaonuohand12f57e2014-02-18 14:11:35 +08001294}
1295
zhanghailiang4c7e2512014-10-09 14:13:11 +08001296static void write_dump_pages(DumpState *s, Error **errp)
qiaonuohand12f57e2014-02-18 14:11:35 +08001297{
1298 int ret = 0;
1299 DataCache page_desc, page_data;
1300 size_t len_buf_out, size_out;
1301#ifdef CONFIG_LZO
1302 lzo_bytep wrkmem = NULL;
1303#endif
1304 uint8_t *buf_out = NULL;
1305 off_t offset_desc, offset_data;
1306 PageDescriptor pd, pd_zero;
1307 uint8_t *buf;
qiaonuohand12f57e2014-02-18 14:11:35 +08001308 GuestPhysBlock *block_iter = NULL;
1309 uint64_t pfn_iter;
1310
1311 /* get offset of page_desc and page_data in dump file */
1312 offset_desc = s->offset_page;
1313 offset_data = offset_desc + sizeof(PageDescriptor) * s->num_dumpable;
1314
1315 prepare_data_cache(&page_desc, s, offset_desc);
1316 prepare_data_cache(&page_data, s, offset_data);
1317
1318 /* prepare buffer to store compressed data */
Andrew Jones8161bef2016-01-11 20:56:20 +01001319 len_buf_out = get_len_buf_out(s->dump_info.page_size, s->flag_compress);
Laszlo Ersekb87ef352014-05-20 13:42:53 +02001320 assert(len_buf_out != 0);
qiaonuohand12f57e2014-02-18 14:11:35 +08001321
1322#ifdef CONFIG_LZO
1323 wrkmem = g_malloc(LZO1X_1_MEM_COMPRESS);
1324#endif
1325
1326 buf_out = g_malloc(len_buf_out);
1327
1328 /*
1329 * init zero page's page_desc and page_data, because every zero page
1330 * uses the same page_data
1331 */
Andrew Jones8161bef2016-01-11 20:56:20 +01001332 pd_zero.size = cpu_to_dump32(s, s->dump_info.page_size);
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001333 pd_zero.flags = cpu_to_dump32(s, 0);
1334 pd_zero.offset = cpu_to_dump64(s, offset_data);
1335 pd_zero.page_flags = cpu_to_dump64(s, 0);
Andrew Jones8161bef2016-01-11 20:56:20 +01001336 buf = g_malloc0(s->dump_info.page_size);
1337 ret = write_cache(&page_data, buf, s->dump_info.page_size, false);
qiaonuohand12f57e2014-02-18 14:11:35 +08001338 g_free(buf);
1339 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001340 error_setg(errp, "dump: failed to write page data (zero page)");
qiaonuohand12f57e2014-02-18 14:11:35 +08001341 goto out;
1342 }
1343
Andrew Jones8161bef2016-01-11 20:56:20 +01001344 offset_data += s->dump_info.page_size;
qiaonuohand12f57e2014-02-18 14:11:35 +08001345
1346 /*
1347 * dump memory to vmcore page by page. zero page will all be resided in the
1348 * first page of page section
1349 */
1350 while (get_next_page(&block_iter, &pfn_iter, &buf, s)) {
1351 /* check zero page */
Juan Quintelaf13f22b2021-11-18 15:57:44 +01001352 if (buffer_is_zero(buf, s->dump_info.page_size)) {
qiaonuohand12f57e2014-02-18 14:11:35 +08001353 ret = write_cache(&page_desc, &pd_zero, sizeof(PageDescriptor),
1354 false);
1355 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001356 error_setg(errp, "dump: failed to write page desc");
qiaonuohand12f57e2014-02-18 14:11:35 +08001357 goto out;
1358 }
1359 } else {
1360 /*
1361 * not zero page, then:
1362 * 1. compress the page
1363 * 2. write the compressed page into the cache of page_data
1364 * 3. get page desc of the compressed page and write it into the
1365 * cache of page_desc
1366 *
1367 * only one compression format will be used here, for
1368 * s->flag_compress is set. But when compression fails to work,
1369 * we fall back to save in plaintext.
1370 */
1371 size_out = len_buf_out;
1372 if ((s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) &&
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001373 (compress2(buf_out, (uLongf *)&size_out, buf,
Andrew Jones8161bef2016-01-11 20:56:20 +01001374 s->dump_info.page_size, Z_BEST_SPEED) == Z_OK) &&
1375 (size_out < s->dump_info.page_size)) {
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001376 pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_ZLIB);
1377 pd.size = cpu_to_dump32(s, size_out);
qiaonuohand12f57e2014-02-18 14:11:35 +08001378
1379 ret = write_cache(&page_data, buf_out, size_out, false);
1380 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001381 error_setg(errp, "dump: failed to write page data");
qiaonuohand12f57e2014-02-18 14:11:35 +08001382 goto out;
1383 }
1384#ifdef CONFIG_LZO
1385 } else if ((s->flag_compress & DUMP_DH_COMPRESSED_LZO) &&
Andrew Jones8161bef2016-01-11 20:56:20 +01001386 (lzo1x_1_compress(buf, s->dump_info.page_size, buf_out,
qiaonuohand12f57e2014-02-18 14:11:35 +08001387 (lzo_uint *)&size_out, wrkmem) == LZO_E_OK) &&
Andrew Jones8161bef2016-01-11 20:56:20 +01001388 (size_out < s->dump_info.page_size)) {
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001389 pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_LZO);
1390 pd.size = cpu_to_dump32(s, size_out);
qiaonuohand12f57e2014-02-18 14:11:35 +08001391
1392 ret = write_cache(&page_data, buf_out, size_out, false);
1393 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001394 error_setg(errp, "dump: failed to write page data");
qiaonuohand12f57e2014-02-18 14:11:35 +08001395 goto out;
1396 }
1397#endif
1398#ifdef CONFIG_SNAPPY
1399 } else if ((s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) &&
Andrew Jones8161bef2016-01-11 20:56:20 +01001400 (snappy_compress((char *)buf, s->dump_info.page_size,
qiaonuohand12f57e2014-02-18 14:11:35 +08001401 (char *)buf_out, &size_out) == SNAPPY_OK) &&
Andrew Jones8161bef2016-01-11 20:56:20 +01001402 (size_out < s->dump_info.page_size)) {
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001403 pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_SNAPPY);
1404 pd.size = cpu_to_dump32(s, size_out);
qiaonuohand12f57e2014-02-18 14:11:35 +08001405
1406 ret = write_cache(&page_data, buf_out, size_out, false);
1407 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001408 error_setg(errp, "dump: failed to write page data");
qiaonuohand12f57e2014-02-18 14:11:35 +08001409 goto out;
1410 }
1411#endif
1412 } else {
1413 /*
1414 * fall back to save in plaintext, size_out should be
Andrew Jones8161bef2016-01-11 20:56:20 +01001415 * assigned the target's page size
qiaonuohand12f57e2014-02-18 14:11:35 +08001416 */
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001417 pd.flags = cpu_to_dump32(s, 0);
Andrew Jones8161bef2016-01-11 20:56:20 +01001418 size_out = s->dump_info.page_size;
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001419 pd.size = cpu_to_dump32(s, size_out);
qiaonuohand12f57e2014-02-18 14:11:35 +08001420
Andrew Jones8161bef2016-01-11 20:56:20 +01001421 ret = write_cache(&page_data, buf,
1422 s->dump_info.page_size, false);
qiaonuohand12f57e2014-02-18 14:11:35 +08001423 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001424 error_setg(errp, "dump: failed to write page data");
qiaonuohand12f57e2014-02-18 14:11:35 +08001425 goto out;
1426 }
1427 }
1428
1429 /* get and write page desc here */
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001430 pd.page_flags = cpu_to_dump64(s, 0);
1431 pd.offset = cpu_to_dump64(s, offset_data);
qiaonuohand12f57e2014-02-18 14:11:35 +08001432 offset_data += size_out;
1433
1434 ret = write_cache(&page_desc, &pd, sizeof(PageDescriptor), false);
1435 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001436 error_setg(errp, "dump: failed to write page desc");
qiaonuohand12f57e2014-02-18 14:11:35 +08001437 goto out;
1438 }
1439 }
Peter Xu2264c2c2016-02-18 13:16:53 +08001440 s->written_size += s->dump_info.page_size;
qiaonuohand12f57e2014-02-18 14:11:35 +08001441 }
1442
1443 ret = write_cache(&page_desc, NULL, 0, true);
1444 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001445 error_setg(errp, "dump: failed to sync cache for page_desc");
qiaonuohand12f57e2014-02-18 14:11:35 +08001446 goto out;
1447 }
1448 ret = write_cache(&page_data, NULL, 0, true);
1449 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001450 error_setg(errp, "dump: failed to sync cache for page_data");
qiaonuohand12f57e2014-02-18 14:11:35 +08001451 goto out;
1452 }
1453
1454out:
1455 free_data_cache(&page_desc);
1456 free_data_cache(&page_data);
1457
1458#ifdef CONFIG_LZO
1459 g_free(wrkmem);
1460#endif
1461
1462 g_free(buf_out);
qiaonuohand12f57e2014-02-18 14:11:35 +08001463}
1464
zhanghailiang4c7e2512014-10-09 14:13:11 +08001465static void create_kdump_vmcore(DumpState *s, Error **errp)
qiaonuohanb53ccc32014-02-18 14:11:36 +08001466{
1467 int ret;
zhanghailiang4c7e2512014-10-09 14:13:11 +08001468 Error *local_err = NULL;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001469
1470 /*
1471 * the kdump-compressed format is:
1472 * File offset
1473 * +------------------------------------------+ 0x0
1474 * | main header (struct disk_dump_header) |
1475 * |------------------------------------------+ block 1
1476 * | sub header (struct kdump_sub_header) |
1477 * |------------------------------------------+ block 2
1478 * | 1st-dump_bitmap |
1479 * |------------------------------------------+ block 2 + X blocks
1480 * | 2nd-dump_bitmap | (aligned by block)
1481 * |------------------------------------------+ block 2 + 2 * X blocks
1482 * | page desc for pfn 0 (struct page_desc) | (aligned by block)
1483 * | page desc for pfn 1 (struct page_desc) |
1484 * | : |
1485 * |------------------------------------------| (not aligned by block)
1486 * | page data (pfn 0) |
1487 * | page data (pfn 1) |
1488 * | : |
1489 * +------------------------------------------+
1490 */
1491
1492 ret = write_start_flat_header(s->fd);
1493 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001494 error_setg(errp, "dump: failed to write start flat header");
zhanghailiang4c7e2512014-10-09 14:13:11 +08001495 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001496 }
1497
zhanghailiang4c7e2512014-10-09 14:13:11 +08001498 write_dump_header(s, &local_err);
1499 if (local_err) {
1500 error_propagate(errp, local_err);
1501 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001502 }
1503
zhanghailiang4c7e2512014-10-09 14:13:11 +08001504 write_dump_bitmap(s, &local_err);
1505 if (local_err) {
1506 error_propagate(errp, local_err);
1507 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001508 }
1509
zhanghailiang4c7e2512014-10-09 14:13:11 +08001510 write_dump_pages(s, &local_err);
1511 if (local_err) {
1512 error_propagate(errp, local_err);
1513 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001514 }
1515
1516 ret = write_end_flat_header(s->fd);
1517 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001518 error_setg(errp, "dump: failed to write end flat header");
zhanghailiang4c7e2512014-10-09 14:13:11 +08001519 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001520 }
qiaonuohanb53ccc32014-02-18 14:11:36 +08001521}
1522
Wen Congyang783e9b42012-05-07 12:10:47 +08001523static ram_addr_t get_start_block(DumpState *s)
1524{
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001525 GuestPhysBlock *block;
Wen Congyang783e9b42012-05-07 12:10:47 +08001526
1527 if (!s->has_filter) {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001528 s->next_block = QTAILQ_FIRST(&s->guest_phys_blocks.head);
Wen Congyang783e9b42012-05-07 12:10:47 +08001529 return 0;
1530 }
1531
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001532 QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
1533 if (block->target_start >= s->begin + s->length ||
1534 block->target_end <= s->begin) {
Wen Congyang783e9b42012-05-07 12:10:47 +08001535 /* This block is out of the range */
1536 continue;
1537 }
1538
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001539 s->next_block = block;
1540 if (s->begin > block->target_start) {
1541 s->start = s->begin - block->target_start;
Wen Congyang783e9b42012-05-07 12:10:47 +08001542 } else {
1543 s->start = 0;
1544 }
1545 return s->start;
1546 }
1547
1548 return -1;
1549}
1550
qiaonuohan7aad2482014-02-18 14:11:31 +08001551static void get_max_mapnr(DumpState *s)
1552{
1553 GuestPhysBlock *last_block;
1554
Paolo Bonzinieae3eb32018-12-06 13:10:34 +01001555 last_block = QTAILQ_LAST(&s->guest_phys_blocks.head);
Andrew Jones8161bef2016-01-11 20:56:20 +01001556 s->max_mapnr = dump_paddr_to_pfn(s, last_block->target_end);
qiaonuohan7aad2482014-02-18 14:11:31 +08001557}
1558
Peter Xubaf28f52016-02-18 13:16:48 +08001559static DumpState dump_state_global = { .status = DUMP_STATUS_NONE };
1560
1561static void dump_state_prepare(DumpState *s)
1562{
1563 /* zero the struct, setting status to active */
1564 *s = (DumpState) { .status = DUMP_STATUS_ACTIVE };
1565}
1566
Marc-André Lureau544803c2022-03-23 19:57:31 +04001567bool qemu_system_dump_in_progress(void)
Peter Xu65d64f32016-02-18 13:16:49 +08001568{
1569 DumpState *state = &dump_state_global;
Stefan Hajnoczid73415a2020-09-23 11:56:46 +01001570 return (qatomic_read(&state->status) == DUMP_STATUS_ACTIVE);
Peter Xu65d64f32016-02-18 13:16:49 +08001571}
1572
Peter Xu2264c2c2016-02-18 13:16:53 +08001573/* calculate total size of memory to be dumped (taking filter into
1574 * acoount.) */
1575static int64_t dump_calculate_size(DumpState *s)
1576{
1577 GuestPhysBlock *block;
1578 int64_t size = 0, total = 0, left = 0, right = 0;
1579
1580 QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
1581 if (s->has_filter) {
1582 /* calculate the overlapped region. */
1583 left = MAX(s->begin, block->target_start);
1584 right = MIN(s->begin + s->length, block->target_end);
1585 size = right - left;
1586 size = size > 0 ? size : 0;
1587 } else {
1588 /* count the whole region in */
1589 size = (block->target_end - block->target_start);
1590 }
1591 total += size;
1592 }
1593
1594 return total;
1595}
1596
Marc-André Lureaud9feb512017-09-11 18:59:26 +02001597static void vmcoreinfo_update_phys_base(DumpState *s)
1598{
1599 uint64_t size, note_head_size, name_size, phys_base;
1600 char **lines;
1601 uint8_t *vmci;
1602 size_t i;
1603
1604 if (!note_name_equal(s, s->guest_note, "VMCOREINFO")) {
1605 return;
1606 }
1607
1608 get_note_sizes(s, s->guest_note, &note_head_size, &name_size, &size);
1609 note_head_size = ROUND_UP(note_head_size, 4);
1610
1611 vmci = s->guest_note + note_head_size + ROUND_UP(name_size, 4);
1612 *(vmci + size) = '\0';
1613
1614 lines = g_strsplit((char *)vmci, "\n", -1);
1615 for (i = 0; lines[i]; i++) {
Wei Huang68cbecf2018-03-09 12:03:23 -05001616 const char *prefix = NULL;
1617
1618 if (s->dump_info.d_machine == EM_X86_64) {
1619 prefix = "NUMBER(phys_base)=";
1620 } else if (s->dump_info.d_machine == EM_AARCH64) {
1621 prefix = "NUMBER(PHYS_OFFSET)=";
1622 }
1623
1624 if (prefix && g_str_has_prefix(lines[i], prefix)) {
1625 if (qemu_strtou64(lines[i] + strlen(prefix), NULL, 16,
Marc-André Lureaud9feb512017-09-11 18:59:26 +02001626 &phys_base) < 0) {
Wei Huang68cbecf2018-03-09 12:03:23 -05001627 warn_report("Failed to read %s", prefix);
Marc-André Lureaud9feb512017-09-11 18:59:26 +02001628 } else {
1629 s->dump_info.phys_base = phys_base;
1630 }
1631 break;
1632 }
1633 }
1634
1635 g_strfreev(lines);
1636}
1637
zhanghailiang4c7e2512014-10-09 14:13:11 +08001638static void dump_init(DumpState *s, int fd, bool has_format,
1639 DumpGuestMemoryFormat format, bool paging, bool has_filter,
1640 int64_t begin, int64_t length, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +08001641{
Marc-André Lureau903ef732017-09-11 18:59:25 +02001642 VMCoreInfoState *vmci = vmcoreinfo_find();
Andreas Färber182735e2013-05-29 22:29:20 +02001643 CPUState *cpu;
Wen Congyang783e9b42012-05-07 12:10:47 +08001644 int nr_cpus;
Andreas Färber11ed09c2013-05-29 21:54:03 +02001645 Error *err = NULL;
Wen Congyang783e9b42012-05-07 12:10:47 +08001646 int ret;
1647
Peter Xuca1fc8c2016-02-18 13:16:50 +08001648 s->has_format = has_format;
1649 s->format = format;
Peter Xu2264c2c2016-02-18 13:16:53 +08001650 s->written_size = 0;
Peter Xuca1fc8c2016-02-18 13:16:50 +08001651
qiaonuohanb53ccc32014-02-18 14:11:36 +08001652 /* kdump-compressed is conflict with paging and filter */
1653 if (has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
1654 assert(!paging && !has_filter);
1655 }
1656
Wen Congyang783e9b42012-05-07 12:10:47 +08001657 if (runstate_is_running()) {
1658 vm_stop(RUN_STATE_SAVE_VM);
1659 s->resume = true;
1660 } else {
1661 s->resume = false;
1662 }
1663
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001664 /* If we use KVM, we should synchronize the registers before we get dump
1665 * info or physmap info.
Wen Congyang783e9b42012-05-07 12:10:47 +08001666 */
Andreas Färber1b3509c2013-06-09 16:48:29 +02001667 cpu_synchronize_all_states();
Wen Congyang783e9b42012-05-07 12:10:47 +08001668 nr_cpus = 0;
Andreas Färberbdc44642013-06-24 23:50:24 +02001669 CPU_FOREACH(cpu) {
Wen Congyang783e9b42012-05-07 12:10:47 +08001670 nr_cpus++;
1671 }
1672
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001673 s->fd = fd;
1674 s->has_filter = has_filter;
1675 s->begin = begin;
1676 s->length = length;
1677
Chen Gang29282072014-08-03 23:28:56 +08001678 memory_mapping_list_init(&s->list);
1679
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001680 guest_phys_blocks_init(&s->guest_phys_blocks);
Laszlo Ersekc5d7f602013-08-06 12:37:10 +02001681 guest_phys_blocks_append(&s->guest_phys_blocks);
Peter Xu2264c2c2016-02-18 13:16:53 +08001682 s->total_size = dump_calculate_size(s);
1683#ifdef DEBUG_DUMP_GUEST_MEMORY
1684 fprintf(stderr, "DUMP: total memory to dump: %lu\n", s->total_size);
1685#endif
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001686
Cornelia Huckd1e69942017-09-13 16:20:35 +02001687 /* it does not make sense to dump non-existent memory */
1688 if (!s->total_size) {
1689 error_setg(errp, "dump: no guest memory to dump");
1690 goto cleanup;
1691 }
1692
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001693 s->start = get_start_block(s);
1694 if (s->start == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001695 error_setg(errp, QERR_INVALID_PARAMETER, "begin");
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001696 goto cleanup;
1697 }
1698
1699 /* get dump info: endian, class and architecture.
1700 * If the target architecture is not supported, cpu_get_dump_info() will
1701 * return -1.
1702 */
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001703 ret = cpu_get_dump_info(&s->dump_info, &s->guest_phys_blocks);
Wen Congyang783e9b42012-05-07 12:10:47 +08001704 if (ret < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001705 error_setg(errp, QERR_UNSUPPORTED);
Wen Congyang783e9b42012-05-07 12:10:47 +08001706 goto cleanup;
1707 }
1708
Andrew Jones8161bef2016-01-11 20:56:20 +01001709 if (!s->dump_info.page_size) {
1710 s->dump_info.page_size = TARGET_PAGE_SIZE;
1711 }
1712
Paolo Bonzini4720bd02012-06-07 08:48:09 +02001713 s->note_size = cpu_get_note_size(s->dump_info.d_class,
1714 s->dump_info.d_machine, nr_cpus);
Aneesh Kumar K.Vbb6b6842013-10-01 21:49:32 +05301715 if (s->note_size < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001716 error_setg(errp, QERR_UNSUPPORTED);
Paolo Bonzini4720bd02012-06-07 08:48:09 +02001717 goto cleanup;
1718 }
1719
Marc-André Lureau903ef732017-09-11 18:59:25 +02001720 /*
Marc-André Lureaud9feb512017-09-11 18:59:26 +02001721 * The goal of this block is to (a) update the previously guessed
1722 * phys_base, (b) copy the guest note out of the guest.
1723 * Failure to do so is not fatal for dumping.
Marc-André Lureau903ef732017-09-11 18:59:25 +02001724 */
1725 if (vmci) {
1726 uint64_t addr, note_head_size, name_size, desc_size;
1727 uint32_t size;
1728 uint16_t format;
1729
1730 note_head_size = s->dump_info.d_class == ELFCLASS32 ?
1731 sizeof(Elf32_Nhdr) : sizeof(Elf64_Nhdr);
1732
1733 format = le16_to_cpu(vmci->vmcoreinfo.guest_format);
1734 size = le32_to_cpu(vmci->vmcoreinfo.size);
1735 addr = le64_to_cpu(vmci->vmcoreinfo.paddr);
1736 if (!vmci->has_vmcoreinfo) {
1737 warn_report("guest note is not present");
1738 } else if (size < note_head_size || size > MAX_GUEST_NOTE_SIZE) {
1739 warn_report("guest note size is invalid: %" PRIu32, size);
Marc-André Lureau5be5df72018-08-17 17:59:10 +02001740 } else if (format != FW_CFG_VMCOREINFO_FORMAT_ELF) {
Marc-André Lureau903ef732017-09-11 18:59:25 +02001741 warn_report("guest note format is unsupported: %" PRIu16, format);
1742 } else {
1743 s->guest_note = g_malloc(size + 1); /* +1 for adding \0 */
1744 cpu_physical_memory_read(addr, s->guest_note, size);
1745
1746 get_note_sizes(s, s->guest_note, NULL, &name_size, &desc_size);
1747 s->guest_note_size = ELF_NOTE_SIZE(note_head_size, name_size,
1748 desc_size);
1749 if (name_size > MAX_GUEST_NOTE_SIZE ||
1750 desc_size > MAX_GUEST_NOTE_SIZE ||
1751 s->guest_note_size > size) {
1752 warn_report("Invalid guest note header");
1753 g_free(s->guest_note);
1754 s->guest_note = NULL;
1755 } else {
Marc-André Lureaud9feb512017-09-11 18:59:26 +02001756 vmcoreinfo_update_phys_base(s);
Marc-André Lureau903ef732017-09-11 18:59:25 +02001757 s->note_size += s->guest_note_size;
1758 }
1759 }
1760 }
1761
Wen Congyang783e9b42012-05-07 12:10:47 +08001762 /* get memory mapping */
Wen Congyang783e9b42012-05-07 12:10:47 +08001763 if (paging) {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001764 qemu_get_guest_memory_mapping(&s->list, &s->guest_phys_blocks, &err);
Andreas Färber11ed09c2013-05-29 21:54:03 +02001765 if (err != NULL) {
1766 error_propagate(errp, err);
1767 goto cleanup;
1768 }
Wen Congyang783e9b42012-05-07 12:10:47 +08001769 } else {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001770 qemu_get_guest_simple_memory_mapping(&s->list, &s->guest_phys_blocks);
Wen Congyang783e9b42012-05-07 12:10:47 +08001771 }
1772
qiaonuohan7aad2482014-02-18 14:11:31 +08001773 s->nr_cpus = nr_cpus;
qiaonuohan7aad2482014-02-18 14:11:31 +08001774
1775 get_max_mapnr(s);
1776
1777 uint64_t tmp;
Andrew Jones8161bef2016-01-11 20:56:20 +01001778 tmp = DIV_ROUND_UP(DIV_ROUND_UP(s->max_mapnr, CHAR_BIT),
1779 s->dump_info.page_size);
1780 s->len_dump_bitmap = tmp * s->dump_info.page_size;
qiaonuohan7aad2482014-02-18 14:11:31 +08001781
qiaonuohanb53ccc32014-02-18 14:11:36 +08001782 /* init for kdump-compressed format */
1783 if (has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
1784 switch (format) {
1785 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB:
1786 s->flag_compress = DUMP_DH_COMPRESSED_ZLIB;
1787 break;
1788
1789 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO:
Laszlo Ersekc998acb2014-05-20 13:42:22 +02001790#ifdef CONFIG_LZO
1791 if (lzo_init() != LZO_E_OK) {
1792 error_setg(errp, "failed to initialize the LZO library");
1793 goto cleanup;
1794 }
1795#endif
qiaonuohanb53ccc32014-02-18 14:11:36 +08001796 s->flag_compress = DUMP_DH_COMPRESSED_LZO;
1797 break;
1798
1799 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY:
1800 s->flag_compress = DUMP_DH_COMPRESSED_SNAPPY;
1801 break;
1802
1803 default:
1804 s->flag_compress = 0;
1805 }
1806
zhanghailiang4c7e2512014-10-09 14:13:11 +08001807 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001808 }
1809
Wen Congyang783e9b42012-05-07 12:10:47 +08001810 if (s->has_filter) {
1811 memory_mapping_filter(&s->list, s->begin, s->length);
1812 }
1813
1814 /*
1815 * calculate phdr_num
1816 *
1817 * the type of ehdr->e_phnum is uint16_t, so we should avoid overflow
1818 */
1819 s->phdr_num = 1; /* PT_NOTE */
1820 if (s->list.num < UINT16_MAX - 2) {
1821 s->phdr_num += s->list.num;
1822 s->have_section = false;
1823 } else {
1824 s->have_section = true;
1825 s->phdr_num = PN_XNUM;
1826 s->sh_info = 1; /* PT_NOTE */
1827
1828 /* the type of shdr->sh_info is uint32_t, so we should avoid overflow */
1829 if (s->list.num <= UINT32_MAX - 1) {
1830 s->sh_info += s->list.num;
1831 } else {
1832 s->sh_info = UINT32_MAX;
1833 }
1834 }
1835
Wen Congyang783e9b42012-05-07 12:10:47 +08001836 if (s->dump_info.d_class == ELFCLASS64) {
1837 if (s->have_section) {
1838 s->memory_offset = sizeof(Elf64_Ehdr) +
1839 sizeof(Elf64_Phdr) * s->sh_info +
1840 sizeof(Elf64_Shdr) + s->note_size;
1841 } else {
1842 s->memory_offset = sizeof(Elf64_Ehdr) +
1843 sizeof(Elf64_Phdr) * s->phdr_num + s->note_size;
1844 }
1845 } else {
1846 if (s->have_section) {
1847 s->memory_offset = sizeof(Elf32_Ehdr) +
1848 sizeof(Elf32_Phdr) * s->sh_info +
1849 sizeof(Elf32_Shdr) + s->note_size;
1850 } else {
1851 s->memory_offset = sizeof(Elf32_Ehdr) +
1852 sizeof(Elf32_Phdr) * s->phdr_num + s->note_size;
1853 }
1854 }
1855
zhanghailiang4c7e2512014-10-09 14:13:11 +08001856 return;
Wen Congyang783e9b42012-05-07 12:10:47 +08001857
1858cleanup:
Chen Gang29282072014-08-03 23:28:56 +08001859 dump_cleanup(s);
Wen Congyang783e9b42012-05-07 12:10:47 +08001860}
1861
Peter Xuca1fc8c2016-02-18 13:16:50 +08001862/* this operation might be time consuming. */
1863static void dump_process(DumpState *s, Error **errp)
1864{
1865 Error *local_err = NULL;
Peter Xud42a0d12016-02-18 13:16:56 +08001866 DumpQueryResult *result = NULL;
Peter Xuca1fc8c2016-02-18 13:16:50 +08001867
Viktor Prutyanov2da91b52018-05-17 19:23:39 +03001868 if (s->has_format && s->format == DUMP_GUEST_MEMORY_FORMAT_WIN_DMP) {
1869#ifdef TARGET_X86_64
1870 create_win_dump(s, &local_err);
1871#endif
1872 } else if (s->has_format && s->format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
Peter Xuca1fc8c2016-02-18 13:16:50 +08001873 create_kdump_vmcore(s, &local_err);
1874 } else {
1875 create_vmcore(s, &local_err);
1876 }
1877
Peter Xu39ba2ea2016-02-18 13:16:54 +08001878 /* make sure status is written after written_size updates */
1879 smp_wmb();
Stefan Hajnoczid73415a2020-09-23 11:56:46 +01001880 qatomic_set(&s->status,
Peter Xu39ba2ea2016-02-18 13:16:54 +08001881 (local_err ? DUMP_STATUS_FAILED : DUMP_STATUS_COMPLETED));
Peter Xuca1fc8c2016-02-18 13:16:50 +08001882
Peter Xud42a0d12016-02-18 13:16:56 +08001883 /* send DUMP_COMPLETED message (unconditionally) */
1884 result = qmp_query_dump(NULL);
1885 /* should never fail */
1886 assert(result);
Philippe Mathieu-Daudé78ee6bd2020-04-13 00:35:56 +02001887 qapi_event_send_dump_completed(result, !!local_err, (local_err ?
Peter Xu3ab72382018-08-15 21:37:37 +08001888 error_get_pretty(local_err) : NULL));
Peter Xud42a0d12016-02-18 13:16:56 +08001889 qapi_free_DumpQueryResult(result);
1890
Peter Xu39ba2ea2016-02-18 13:16:54 +08001891 error_propagate(errp, local_err);
Peter Xuca1fc8c2016-02-18 13:16:50 +08001892 dump_cleanup(s);
1893}
1894
Peter Xu1fbeff72016-02-18 13:16:52 +08001895static void *dump_thread(void *data)
1896{
Peter Xu1fbeff72016-02-18 13:16:52 +08001897 DumpState *s = (DumpState *)data;
Alberto Garciac3a8fe32017-08-29 15:08:36 +03001898 dump_process(s, NULL);
Peter Xu1fbeff72016-02-18 13:16:52 +08001899 return NULL;
1900}
1901
Peter Xu39ba2ea2016-02-18 13:16:54 +08001902DumpQueryResult *qmp_query_dump(Error **errp)
1903{
1904 DumpQueryResult *result = g_new(DumpQueryResult, 1);
1905 DumpState *state = &dump_state_global;
Stefan Hajnoczid73415a2020-09-23 11:56:46 +01001906 result->status = qatomic_read(&state->status);
Peter Xu39ba2ea2016-02-18 13:16:54 +08001907 /* make sure we are reading status and written_size in order */
1908 smp_rmb();
1909 result->completed = state->written_size;
1910 result->total = state->total_size;
1911 return result;
1912}
1913
Peter Xu228de9c2016-02-18 13:16:47 +08001914void qmp_dump_guest_memory(bool paging, const char *file,
1915 bool has_detach, bool detach,
1916 bool has_begin, int64_t begin, bool has_length,
qiaonuohanb53ccc32014-02-18 14:11:36 +08001917 int64_t length, bool has_format,
1918 DumpGuestMemoryFormat format, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +08001919{
1920 const char *p;
1921 int fd = -1;
1922 DumpState *s;
zhanghailiang4c7e2512014-10-09 14:13:11 +08001923 Error *local_err = NULL;
Peter Xu1fbeff72016-02-18 13:16:52 +08001924 bool detach_p = false;
Wen Congyang783e9b42012-05-07 12:10:47 +08001925
Peter Xu63e27f22016-02-18 13:16:51 +08001926 if (runstate_check(RUN_STATE_INMIGRATE)) {
1927 error_setg(errp, "Dump not allowed during incoming migration.");
1928 return;
1929 }
1930
Peter Xu65d64f32016-02-18 13:16:49 +08001931 /* if there is a dump in background, we should wait until the dump
1932 * finished */
Marc-André Lureau544803c2022-03-23 19:57:31 +04001933 if (qemu_system_dump_in_progress()) {
Peter Xu65d64f32016-02-18 13:16:49 +08001934 error_setg(errp, "There is a dump in process, please wait.");
1935 return;
1936 }
1937
qiaonuohanb53ccc32014-02-18 14:11:36 +08001938 /*
1939 * kdump-compressed format need the whole memory dumped, so paging or
1940 * filter is not supported here.
1941 */
1942 if ((has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) &&
1943 (paging || has_begin || has_length)) {
1944 error_setg(errp, "kdump-compressed format doesn't support paging or "
1945 "filter");
1946 return;
1947 }
Wen Congyang783e9b42012-05-07 12:10:47 +08001948 if (has_begin && !has_length) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001949 error_setg(errp, QERR_MISSING_PARAMETER, "length");
Wen Congyang783e9b42012-05-07 12:10:47 +08001950 return;
1951 }
1952 if (!has_begin && has_length) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001953 error_setg(errp, QERR_MISSING_PARAMETER, "begin");
Wen Congyang783e9b42012-05-07 12:10:47 +08001954 return;
1955 }
Peter Xu1fbeff72016-02-18 13:16:52 +08001956 if (has_detach) {
1957 detach_p = detach;
1958 }
Wen Congyang783e9b42012-05-07 12:10:47 +08001959
qiaonuohanb53ccc32014-02-18 14:11:36 +08001960 /* check whether lzo/snappy is supported */
1961#ifndef CONFIG_LZO
1962 if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO) {
1963 error_setg(errp, "kdump-lzo is not available now");
1964 return;
1965 }
1966#endif
1967
1968#ifndef CONFIG_SNAPPY
1969 if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY) {
1970 error_setg(errp, "kdump-snappy is not available now");
1971 return;
1972 }
1973#endif
1974
Viktor Prutyanov2da91b52018-05-17 19:23:39 +03001975#ifndef TARGET_X86_64
1976 if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_WIN_DMP) {
1977 error_setg(errp, "Windows dump is only available for x86-64");
1978 return;
1979 }
1980#endif
1981
Wen Congyang783e9b42012-05-07 12:10:47 +08001982#if !defined(WIN32)
1983 if (strstart(file, "fd:", &p)) {
Kevin Wolf947e4742020-10-05 17:58:44 +02001984 fd = monitor_get_fd(monitor_cur(), p, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +08001985 if (fd == -1) {
Wen Congyang783e9b42012-05-07 12:10:47 +08001986 return;
1987 }
1988 }
1989#endif
1990
1991 if (strstart(file, "file:", &p)) {
Daniel P. Berrangé448058a2020-07-21 13:25:21 +01001992 fd = qemu_open_old(p, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, S_IRUSR);
Wen Congyang783e9b42012-05-07 12:10:47 +08001993 if (fd < 0) {
Luiz Capitulino75817662013-06-07 14:36:01 -04001994 error_setg_file_open(errp, errno, p);
Wen Congyang783e9b42012-05-07 12:10:47 +08001995 return;
1996 }
1997 }
1998
1999 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002000 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
Wen Congyang783e9b42012-05-07 12:10:47 +08002001 return;
2002 }
2003
Peter Xub7bc6b12021-09-22 12:20:09 -04002004 if (!dump_migration_blocker) {
2005 error_setg(&dump_migration_blocker,
2006 "Live migration disabled: dump-guest-memory in progress");
2007 }
2008
2009 /*
2010 * Allows even for -only-migratable, but forbid migration during the
2011 * process of dump guest memory.
2012 */
2013 if (migrate_add_blocker_internal(dump_migration_blocker, errp)) {
2014 /* Remember to release the fd before passing it over to dump state */
2015 close(fd);
2016 return;
2017 }
2018
Peter Xubaf28f52016-02-18 13:16:48 +08002019 s = &dump_state_global;
2020 dump_state_prepare(s);
Wen Congyang783e9b42012-05-07 12:10:47 +08002021
zhanghailiang4c7e2512014-10-09 14:13:11 +08002022 dump_init(s, fd, has_format, format, paging, has_begin,
2023 begin, length, &local_err);
2024 if (local_err) {
zhanghailiang4c7e2512014-10-09 14:13:11 +08002025 error_propagate(errp, local_err);
Stefan Hajnoczid73415a2020-09-23 11:56:46 +01002026 qatomic_set(&s->status, DUMP_STATUS_FAILED);
Wen Congyang783e9b42012-05-07 12:10:47 +08002027 return;
2028 }
2029
Peter Xu1fbeff72016-02-18 13:16:52 +08002030 if (detach_p) {
2031 /* detached dump */
Fam Zheng6796b402017-05-03 15:28:19 +08002032 s->detached = true;
Peter Xu1fbeff72016-02-18 13:16:52 +08002033 qemu_thread_create(&s->dump_thread, "dump_thread", dump_thread,
2034 s, QEMU_THREAD_DETACHED);
2035 } else {
2036 /* sync dump */
2037 dump_process(s, errp);
2038 }
Wen Congyang783e9b42012-05-07 12:10:47 +08002039}
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002040
2041DumpGuestMemoryCapability *qmp_query_dump_guest_memory_capability(Error **errp)
2042{
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002043 DumpGuestMemoryCapability *cap =
Markus Armbrusterb21e2382022-03-15 15:41:56 +01002044 g_new0(DumpGuestMemoryCapability, 1);
Eric Blake95b3a8c2021-01-13 16:10:13 -06002045 DumpGuestMemoryFormatList **tail = &cap->formats;
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002046
2047 /* elf is always available */
Eric Blake95b3a8c2021-01-13 16:10:13 -06002048 QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_ELF);
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002049
2050 /* kdump-zlib is always available */
Eric Blake95b3a8c2021-01-13 16:10:13 -06002051 QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB);
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002052
2053 /* add new item if kdump-lzo is available */
2054#ifdef CONFIG_LZO
Eric Blake95b3a8c2021-01-13 16:10:13 -06002055 QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO);
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002056#endif
2057
2058 /* add new item if kdump-snappy is available */
2059#ifdef CONFIG_SNAPPY
Eric Blake95b3a8c2021-01-13 16:10:13 -06002060 QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY);
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002061#endif
2062
Viktor Prutyanov2da91b52018-05-17 19:23:39 +03002063 /* Windows dump is available only if target is x86_64 */
2064#ifdef TARGET_X86_64
Eric Blake95b3a8c2021-01-13 16:10:13 -06002065 QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_WIN_DMP);
Viktor Prutyanov2da91b52018-05-17 19:23:39 +03002066#endif
2067
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002068 return cap;
2069}