blob: b55a497c425c8f270175e5cc239adbf9d7d2c5cd [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
Janosch Frank670e7692022-08-11 12:11:00 +0000134static void prepare_elf64_header(DumpState *s, Elf64_Ehdr *elf_header)
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
Janosch Frank670e7692022-08-11 12:11:00 +0000143 memset(elf_header, 0, sizeof(Elf64_Ehdr));
144 memcpy(elf_header, ELFMAG, SELFMAG);
145 elf_header->e_ident[EI_CLASS] = ELFCLASS64;
146 elf_header->e_ident[EI_DATA] = s->dump_info.d_endian;
147 elf_header->e_ident[EI_VERSION] = EV_CURRENT;
148 elf_header->e_type = cpu_to_dump16(s, ET_CORE);
149 elf_header->e_machine = cpu_to_dump16(s, s->dump_info.d_machine);
150 elf_header->e_version = cpu_to_dump32(s, EV_CURRENT);
151 elf_header->e_ehsize = cpu_to_dump16(s, sizeof(elf_header));
152 elf_header->e_phoff = cpu_to_dump64(s, s->phdr_offset);
153 elf_header->e_phentsize = cpu_to_dump16(s, sizeof(Elf64_Phdr));
154 elf_header->e_phnum = cpu_to_dump16(s, phnum);
Janosch Frank862a3952022-03-30 12:35:57 +0000155 if (s->shdr_num) {
Janosch Frank670e7692022-08-11 12:11:00 +0000156 elf_header->e_shoff = cpu_to_dump64(s, s->shdr_offset);
157 elf_header->e_shentsize = cpu_to_dump16(s, sizeof(Elf64_Shdr));
158 elf_header->e_shnum = cpu_to_dump16(s, s->shdr_num);
Wen Congyang783e9b42012-05-07 12:10:47 +0800159 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800160}
161
Janosch Frank670e7692022-08-11 12:11:00 +0000162static void prepare_elf32_header(DumpState *s, Elf32_Ehdr *elf_header)
Wen Congyang783e9b42012-05-07 12:10:47 +0800163{
Janosch Frank046bc412022-04-07 09:48:24 +0000164 /*
165 * phnum in the elf header is 16 bit, if we have more segments we
166 * set phnum to PN_XNUM and write the real number of segments to a
167 * special section.
168 */
169 uint16_t phnum = MIN(s->phdr_num, PN_XNUM);
Janosch Frank670e7692022-08-11 12:11:00 +0000170
171 memset(elf_header, 0, sizeof(Elf32_Ehdr));
172 memcpy(elf_header, ELFMAG, SELFMAG);
173 elf_header->e_ident[EI_CLASS] = ELFCLASS32;
174 elf_header->e_ident[EI_DATA] = s->dump_info.d_endian;
175 elf_header->e_ident[EI_VERSION] = EV_CURRENT;
176 elf_header->e_type = cpu_to_dump16(s, ET_CORE);
177 elf_header->e_machine = cpu_to_dump16(s, s->dump_info.d_machine);
178 elf_header->e_version = cpu_to_dump32(s, EV_CURRENT);
179 elf_header->e_ehsize = cpu_to_dump16(s, sizeof(elf_header));
180 elf_header->e_phoff = cpu_to_dump32(s, s->phdr_offset);
181 elf_header->e_phentsize = cpu_to_dump16(s, sizeof(Elf32_Phdr));
182 elf_header->e_phnum = cpu_to_dump16(s, phnum);
183 if (s->shdr_num) {
184 elf_header->e_shoff = cpu_to_dump32(s, s->shdr_offset);
185 elf_header->e_shentsize = cpu_to_dump16(s, sizeof(Elf32_Shdr));
186 elf_header->e_shnum = cpu_to_dump16(s, s->shdr_num);
187 }
188}
189
190static void write_elf_header(DumpState *s, Error **errp)
191{
192 Elf32_Ehdr elf32_header;
193 Elf64_Ehdr elf64_header;
194 size_t header_size;
195 void *header_ptr;
Wen Congyang783e9b42012-05-07 12:10:47 +0800196 int ret;
Wen Congyang783e9b42012-05-07 12:10:47 +0800197
Janosch Frank670e7692022-08-11 12:11:00 +0000198 if (dump_is_64bit(s)) {
199 prepare_elf64_header(s, &elf64_header);
200 header_size = sizeof(elf64_header);
201 header_ptr = &elf64_header;
202 } else {
203 prepare_elf32_header(s, &elf32_header);
204 header_size = sizeof(elf32_header);
205 header_ptr = &elf32_header;
Wen Congyang783e9b42012-05-07 12:10:47 +0800206 }
207
Janosch Frank670e7692022-08-11 12:11:00 +0000208 ret = fd_write_vmcore(header_ptr, header_size, s);
Wen Congyang783e9b42012-05-07 12:10:47 +0800209 if (ret < 0) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200210 error_setg_errno(errp, -ret, "dump: failed to write elf header");
Wen Congyang783e9b42012-05-07 12:10:47 +0800211 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800212}
213
zhanghailiang4c7e2512014-10-09 14:13:11 +0800214static void write_elf64_load(DumpState *s, MemoryMapping *memory_mapping,
215 int phdr_index, hwaddr offset,
216 hwaddr filesz, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800217{
218 Elf64_Phdr phdr;
219 int ret;
Wen Congyang783e9b42012-05-07 12:10:47 +0800220
221 memset(&phdr, 0, sizeof(Elf64_Phdr));
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200222 phdr.p_type = cpu_to_dump32(s, PT_LOAD);
223 phdr.p_offset = cpu_to_dump64(s, offset);
224 phdr.p_paddr = cpu_to_dump64(s, memory_mapping->phys_addr);
225 phdr.p_filesz = cpu_to_dump64(s, filesz);
226 phdr.p_memsz = cpu_to_dump64(s, memory_mapping->length);
Jon Dorone17bebd2019-01-09 10:22:03 +0200227 phdr.p_vaddr = cpu_to_dump64(s, memory_mapping->virt_addr) ?: phdr.p_paddr;
Wen Congyang783e9b42012-05-07 12:10:47 +0800228
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200229 assert(memory_mapping->length >= filesz);
230
Wen Congyang783e9b42012-05-07 12:10:47 +0800231 ret = fd_write_vmcore(&phdr, sizeof(Elf64_Phdr), s);
232 if (ret < 0) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200233 error_setg_errno(errp, -ret,
234 "dump: failed to write program header table");
Wen Congyang783e9b42012-05-07 12:10:47 +0800235 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800236}
237
zhanghailiang4c7e2512014-10-09 14:13:11 +0800238static void write_elf32_load(DumpState *s, MemoryMapping *memory_mapping,
239 int phdr_index, hwaddr offset,
240 hwaddr filesz, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800241{
242 Elf32_Phdr phdr;
243 int ret;
Wen Congyang783e9b42012-05-07 12:10:47 +0800244
245 memset(&phdr, 0, sizeof(Elf32_Phdr));
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200246 phdr.p_type = cpu_to_dump32(s, PT_LOAD);
247 phdr.p_offset = cpu_to_dump32(s, offset);
248 phdr.p_paddr = cpu_to_dump32(s, memory_mapping->phys_addr);
249 phdr.p_filesz = cpu_to_dump32(s, filesz);
250 phdr.p_memsz = cpu_to_dump32(s, memory_mapping->length);
Jon Dorone17bebd2019-01-09 10:22:03 +0200251 phdr.p_vaddr =
252 cpu_to_dump32(s, memory_mapping->virt_addr) ?: phdr.p_paddr;
Wen Congyang783e9b42012-05-07 12:10:47 +0800253
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200254 assert(memory_mapping->length >= filesz);
255
Wen Congyang783e9b42012-05-07 12:10:47 +0800256 ret = fd_write_vmcore(&phdr, sizeof(Elf32_Phdr), s);
257 if (ret < 0) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200258 error_setg_errno(errp, -ret,
259 "dump: failed to write program header table");
Wen Congyang783e9b42012-05-07 12:10:47 +0800260 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800261}
262
Janosch Frank2341a942022-08-11 12:11:01 +0000263static void prepare_elf64_phdr_note(DumpState *s, Elf64_Phdr *phdr)
Wen Congyang783e9b42012-05-07 12:10:47 +0800264{
Janosch Frankbc7d5582022-03-30 12:36:01 +0000265 memset(phdr, 0, sizeof(*phdr));
266 phdr->p_type = cpu_to_dump32(s, PT_NOTE);
267 phdr->p_offset = cpu_to_dump64(s, s->note_offset);
268 phdr->p_paddr = 0;
269 phdr->p_filesz = cpu_to_dump64(s, s->note_size);
270 phdr->p_memsz = cpu_to_dump64(s, s->note_size);
271 phdr->p_vaddr = 0;
Wen Congyang783e9b42012-05-07 12:10:47 +0800272}
273
Paolo Bonzini0bc3cd62013-04-08 17:29:59 +0200274static inline int cpu_index(CPUState *cpu)
275{
276 return cpu->cpu_index + 1;
277}
278
Marc-André Lureau903ef732017-09-11 18:59:25 +0200279static void write_guest_note(WriteCoreDumpFunction f, DumpState *s,
280 Error **errp)
281{
282 int ret;
283
284 if (s->guest_note) {
285 ret = f(s->guest_note, s->guest_note_size, s);
286 if (ret < 0) {
287 error_setg(errp, "dump: failed to write guest note");
288 }
289 }
290}
291
zhanghailiang4c7e2512014-10-09 14:13:11 +0800292static void write_elf64_notes(WriteCoreDumpFunction f, DumpState *s,
293 Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800294{
Andreas Färber0d342822012-12-17 07:12:13 +0100295 CPUState *cpu;
Wen Congyang783e9b42012-05-07 12:10:47 +0800296 int ret;
297 int id;
298
Andreas Färberbdc44642013-06-24 23:50:24 +0200299 CPU_FOREACH(cpu) {
Andreas Färber0d342822012-12-17 07:12:13 +0100300 id = cpu_index(cpu);
qiaonuohan6a519912014-02-18 14:11:26 +0800301 ret = cpu_write_elf64_note(f, cpu, id, s);
Wen Congyang783e9b42012-05-07 12:10:47 +0800302 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800303 error_setg(errp, "dump: failed to write elf notes");
zhanghailiang4c7e2512014-10-09 14:13:11 +0800304 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800305 }
306 }
307
Andreas Färberbdc44642013-06-24 23:50:24 +0200308 CPU_FOREACH(cpu) {
qiaonuohan6a519912014-02-18 14:11:26 +0800309 ret = cpu_write_elf64_qemunote(f, cpu, s);
Wen Congyang783e9b42012-05-07 12:10:47 +0800310 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800311 error_setg(errp, "dump: failed to write CPU status");
zhanghailiang4c7e2512014-10-09 14:13:11 +0800312 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800313 }
314 }
Marc-André Lureau903ef732017-09-11 18:59:25 +0200315
316 write_guest_note(f, s, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800317}
318
Janosch Frank2341a942022-08-11 12:11:01 +0000319static void prepare_elf32_phdr_note(DumpState *s, Elf32_Phdr *phdr)
Wen Congyang783e9b42012-05-07 12:10:47 +0800320{
Janosch Frankbc7d5582022-03-30 12:36:01 +0000321 memset(phdr, 0, sizeof(*phdr));
322 phdr->p_type = cpu_to_dump32(s, PT_NOTE);
323 phdr->p_offset = cpu_to_dump32(s, s->note_offset);
324 phdr->p_paddr = 0;
325 phdr->p_filesz = cpu_to_dump32(s, s->note_size);
326 phdr->p_memsz = cpu_to_dump32(s, s->note_size);
327 phdr->p_vaddr = 0;
Wen Congyang783e9b42012-05-07 12:10:47 +0800328}
329
zhanghailiang4c7e2512014-10-09 14:13:11 +0800330static void write_elf32_notes(WriteCoreDumpFunction f, DumpState *s,
331 Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800332{
Andreas Färber0d342822012-12-17 07:12:13 +0100333 CPUState *cpu;
Wen Congyang783e9b42012-05-07 12:10:47 +0800334 int ret;
335 int id;
336
Andreas Färberbdc44642013-06-24 23:50:24 +0200337 CPU_FOREACH(cpu) {
Andreas Färber0d342822012-12-17 07:12:13 +0100338 id = cpu_index(cpu);
qiaonuohan6a519912014-02-18 14:11:26 +0800339 ret = cpu_write_elf32_note(f, cpu, id, s);
Wen Congyang783e9b42012-05-07 12:10:47 +0800340 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800341 error_setg(errp, "dump: failed to write elf notes");
zhanghailiang4c7e2512014-10-09 14:13:11 +0800342 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800343 }
344 }
345
Andreas Färberbdc44642013-06-24 23:50:24 +0200346 CPU_FOREACH(cpu) {
qiaonuohan6a519912014-02-18 14:11:26 +0800347 ret = cpu_write_elf32_qemunote(f, cpu, s);
Wen Congyang783e9b42012-05-07 12:10:47 +0800348 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800349 error_setg(errp, "dump: failed to write CPU status");
zhanghailiang4c7e2512014-10-09 14:13:11 +0800350 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800351 }
352 }
Marc-André Lureau903ef732017-09-11 18:59:25 +0200353
354 write_guest_note(f, s, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800355}
356
Janosch Frankbc7d5582022-03-30 12:36:01 +0000357static void write_elf_phdr_note(DumpState *s, Error **errp)
358{
359 ERRP_GUARD();
360 Elf32_Phdr phdr32;
361 Elf64_Phdr phdr64;
362 void *phdr;
363 size_t size;
364 int ret;
365
366 if (dump_is_64bit(s)) {
Janosch Frank2341a942022-08-11 12:11:01 +0000367 prepare_elf64_phdr_note(s, &phdr64);
Janosch Frankbc7d5582022-03-30 12:36:01 +0000368 size = sizeof(phdr64);
369 phdr = &phdr64;
370 } else {
Janosch Frank2341a942022-08-11 12:11:01 +0000371 prepare_elf32_phdr_note(s, &phdr32);
Janosch Frankbc7d5582022-03-30 12:36:01 +0000372 size = sizeof(phdr32);
373 phdr = &phdr32;
374 }
375
376 ret = fd_write_vmcore(phdr, size, s);
377 if (ret < 0) {
378 error_setg_errno(errp, -ret,
379 "dump: failed to write program header table");
380 }
381}
382
zhanghailiang4c7e2512014-10-09 14:13:11 +0800383static void write_elf_section(DumpState *s, int type, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800384{
385 Elf32_Shdr shdr32;
386 Elf64_Shdr shdr64;
Wen Congyang783e9b42012-05-07 12:10:47 +0800387 int shdr_size;
388 void *shdr;
389 int ret;
390
391 if (type == 0) {
392 shdr_size = sizeof(Elf32_Shdr);
393 memset(&shdr32, 0, shdr_size);
Janosch Frank046bc412022-04-07 09:48:24 +0000394 shdr32.sh_info = cpu_to_dump32(s, s->phdr_num);
Wen Congyang783e9b42012-05-07 12:10:47 +0800395 shdr = &shdr32;
396 } else {
397 shdr_size = sizeof(Elf64_Shdr);
398 memset(&shdr64, 0, shdr_size);
Janosch Frank046bc412022-04-07 09:48:24 +0000399 shdr64.sh_info = cpu_to_dump32(s, s->phdr_num);
Wen Congyang783e9b42012-05-07 12:10:47 +0800400 shdr = &shdr64;
401 }
402
Peter Maydell174d2d62020-03-24 17:36:30 +0000403 ret = fd_write_vmcore(shdr, shdr_size, s);
Wen Congyang783e9b42012-05-07 12:10:47 +0800404 if (ret < 0) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200405 error_setg_errno(errp, -ret,
406 "dump: failed to write section header table");
Wen Congyang783e9b42012-05-07 12:10:47 +0800407 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800408}
409
zhanghailiang4c7e2512014-10-09 14:13:11 +0800410static void write_data(DumpState *s, void *buf, int length, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800411{
412 int ret;
413
414 ret = fd_write_vmcore(buf, length, s);
415 if (ret < 0) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200416 error_setg_errno(errp, -ret, "dump: failed to save memory");
Peter Xu2264c2c2016-02-18 13:16:53 +0800417 } else {
418 s->written_size += length;
Wen Congyang783e9b42012-05-07 12:10:47 +0800419 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800420}
421
zhanghailiang4c7e2512014-10-09 14:13:11 +0800422/* write the memory to vmcore. 1 page per I/O. */
423static void write_memory(DumpState *s, GuestPhysBlock *block, ram_addr_t start,
424 int64_t size, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800425{
Janosch Frank86a518b2022-03-30 12:35:55 +0000426 ERRP_GUARD();
Wen Congyang783e9b42012-05-07 12:10:47 +0800427 int64_t i;
Wen Congyang783e9b42012-05-07 12:10:47 +0800428
Andrew Jones8161bef2016-01-11 20:56:20 +0100429 for (i = 0; i < size / s->dump_info.page_size; i++) {
430 write_data(s, block->host_addr + start + i * s->dump_info.page_size,
Janosch Frank86a518b2022-03-30 12:35:55 +0000431 s->dump_info.page_size, errp);
432 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800433 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800434 }
435 }
436
Andrew Jones8161bef2016-01-11 20:56:20 +0100437 if ((size % s->dump_info.page_size) != 0) {
438 write_data(s, block->host_addr + start + i * s->dump_info.page_size,
Janosch Frank86a518b2022-03-30 12:35:55 +0000439 size % s->dump_info.page_size, errp);
440 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800441 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800442 }
443 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800444}
445
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200446/* get the memory's offset and size in the vmcore */
447static void get_offset_range(hwaddr phys_addr,
448 ram_addr_t mapping_length,
449 DumpState *s,
450 hwaddr *p_offset,
451 hwaddr *p_filesz)
Wen Congyang783e9b42012-05-07 12:10:47 +0800452{
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200453 GuestPhysBlock *block;
Avi Kivitya8170e52012-10-23 12:30:10 +0200454 hwaddr offset = s->memory_offset;
Wen Congyang783e9b42012-05-07 12:10:47 +0800455 int64_t size_in_block, start;
456
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200457 /* When the memory is not stored into vmcore, offset will be -1 */
458 *p_offset = -1;
459 *p_filesz = 0;
460
Janosch Frankdddf7252022-08-11 12:10:58 +0000461 if (dump_has_filter(s)) {
462 if (phys_addr < s->filter_area_begin ||
463 phys_addr >= s->filter_area_begin + s->filter_area_length) {
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200464 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800465 }
466 }
467
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200468 QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
Janosch Frankdddf7252022-08-11 12:10:58 +0000469 if (dump_has_filter(s)) {
470 if (block->target_start >= s->filter_area_begin + s->filter_area_length ||
471 block->target_end <= s->filter_area_begin) {
Wen Congyang783e9b42012-05-07 12:10:47 +0800472 /* This block is out of the range */
473 continue;
474 }
475
Janosch Frankdddf7252022-08-11 12:10:58 +0000476 if (s->filter_area_begin <= block->target_start) {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200477 start = block->target_start;
Wen Congyang783e9b42012-05-07 12:10:47 +0800478 } else {
Janosch Frankdddf7252022-08-11 12:10:58 +0000479 start = s->filter_area_begin;
Wen Congyang783e9b42012-05-07 12:10:47 +0800480 }
481
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200482 size_in_block = block->target_end - start;
Janosch Frankdddf7252022-08-11 12:10:58 +0000483 if (s->filter_area_begin + s->filter_area_length < block->target_end) {
484 size_in_block -= block->target_end - (s->filter_area_begin + s->filter_area_length);
Wen Congyang783e9b42012-05-07 12:10:47 +0800485 }
486 } else {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200487 start = block->target_start;
488 size_in_block = block->target_end - block->target_start;
Wen Congyang783e9b42012-05-07 12:10:47 +0800489 }
490
491 if (phys_addr >= start && phys_addr < start + size_in_block) {
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200492 *p_offset = phys_addr - start + offset;
493
494 /* The offset range mapped from the vmcore file must not spill over
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200495 * the GuestPhysBlock, clamp it. The rest of the mapping will be
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200496 * zero-filled in memory at load time; see
497 * <http://refspecs.linuxbase.org/elf/gabi4+/ch5.pheader.html>.
498 */
499 *p_filesz = phys_addr + mapping_length <= start + size_in_block ?
500 mapping_length :
501 size_in_block - (phys_addr - start);
502 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800503 }
504
505 offset += size_in_block;
506 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800507}
508
Janosch Frankafae6052022-08-11 12:10:55 +0000509static void write_elf_phdr_loads(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800510{
Janosch Frank86a518b2022-03-30 12:35:55 +0000511 ERRP_GUARD();
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200512 hwaddr offset, filesz;
Wen Congyang783e9b42012-05-07 12:10:47 +0800513 MemoryMapping *memory_mapping;
514 uint32_t phdr_index = 1;
Wen Congyang783e9b42012-05-07 12:10:47 +0800515
516 QTAILQ_FOREACH(memory_mapping, &s->list.head, next) {
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200517 get_offset_range(memory_mapping->phys_addr,
518 memory_mapping->length,
519 s, &offset, &filesz);
Janosch Frank05bbaa502022-03-30 12:36:00 +0000520 if (dump_is_64bit(s)) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800521 write_elf64_load(s, memory_mapping, phdr_index++, offset,
Janosch Frank86a518b2022-03-30 12:35:55 +0000522 filesz, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800523 } else {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800524 write_elf32_load(s, memory_mapping, phdr_index++, offset,
Janosch Frank86a518b2022-03-30 12:35:55 +0000525 filesz, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800526 }
527
Janosch Frank86a518b2022-03-30 12:35:55 +0000528 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800529 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800530 }
531
Janosch Frank046bc412022-04-07 09:48:24 +0000532 if (phdr_index >= s->phdr_num) {
Wen Congyang783e9b42012-05-07 12:10:47 +0800533 break;
534 }
535 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800536}
537
Janosch Frankc6812472022-03-30 12:36:03 +0000538static void write_elf_notes(DumpState *s, Error **errp)
539{
540 if (dump_is_64bit(s)) {
541 write_elf64_notes(fd_write_vmcore, s, errp);
542 } else {
543 write_elf32_notes(fd_write_vmcore, s, errp);
544 }
545}
546
Wen Congyang783e9b42012-05-07 12:10:47 +0800547/* write elf header, PT_NOTE and elf note to vmcore. */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800548static void dump_begin(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800549{
Janosch Frank86a518b2022-03-30 12:35:55 +0000550 ERRP_GUARD();
Wen Congyang783e9b42012-05-07 12:10:47 +0800551
552 /*
553 * the vmcore's format is:
554 * --------------
555 * | elf header |
556 * --------------
557 * | PT_NOTE |
558 * --------------
559 * | PT_LOAD |
560 * --------------
561 * | ...... |
562 * --------------
563 * | PT_LOAD |
564 * --------------
565 * | sec_hdr |
566 * --------------
567 * | elf note |
568 * --------------
569 * | memory |
570 * --------------
571 *
572 * we only know where the memory is saved after we write elf note into
573 * vmcore.
574 */
575
576 /* write elf header to vmcore */
Janosch Frank670e7692022-08-11 12:11:00 +0000577 write_elf_header(s, errp);
Janosch Frank86a518b2022-03-30 12:35:55 +0000578 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800579 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800580 }
581
Janosch Frankbc7d5582022-03-30 12:36:01 +0000582 /* write PT_NOTE to vmcore */
583 write_elf_phdr_note(s, errp);
584 if (*errp) {
585 return;
586 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800587
Janosch Frankafae6052022-08-11 12:10:55 +0000588 /* write all PT_LOADs to vmcore */
589 write_elf_phdr_loads(s, errp);
Janosch Frank5ff2e5a2022-03-30 12:36:02 +0000590 if (*errp) {
591 return;
592 }
593
594 /* write section to vmcore */
595 if (s->shdr_num) {
596 write_elf_section(s, 1, errp);
Janosch Frank86a518b2022-03-30 12:35:55 +0000597 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800598 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800599 }
Janosch Frank5ff2e5a2022-03-30 12:36:02 +0000600 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800601
Janosch Frankc6812472022-03-30 12:36:03 +0000602 /* write notes to vmcore */
603 write_elf_notes(s, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800604}
605
Janosch Frank1e811302022-08-11 12:10:56 +0000606static int64_t dump_filtered_memblock_size(GuestPhysBlock *block,
607 int64_t filter_area_start,
608 int64_t filter_area_length)
Wen Congyang783e9b42012-05-07 12:10:47 +0800609{
Janosch Frank1e811302022-08-11 12:10:56 +0000610 int64_t size, left, right;
Wen Congyang783e9b42012-05-07 12:10:47 +0800611
Janosch Frank1e811302022-08-11 12:10:56 +0000612 /* No filter, return full size */
613 if (!filter_area_length) {
614 return block->target_end - block->target_start;
Wen Congyang783e9b42012-05-07 12:10:47 +0800615 }
Janosch Frank1e811302022-08-11 12:10:56 +0000616
617 /* calculate the overlapped region. */
618 left = MAX(filter_area_start, block->target_start);
619 right = MIN(filter_area_start + filter_area_length, block->target_end);
620 size = right - left;
621 size = size > 0 ? size : 0;
622
623 return size;
624}
625
626static int64_t dump_filtered_memblock_start(GuestPhysBlock *block,
627 int64_t filter_area_start,
628 int64_t filter_area_length)
629{
630 if (filter_area_length) {
631 /* return -1 if the block is not within filter area */
632 if (block->target_start >= filter_area_start + filter_area_length ||
633 block->target_end <= filter_area_start) {
634 return -1;
635 }
636
637 if (filter_area_start > block->target_start) {
638 return filter_area_start - block->target_start;
639 }
640 }
641
642 return 0;
Wen Congyang783e9b42012-05-07 12:10:47 +0800643}
644
645/* write all memory to vmcore */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800646static void dump_iterate(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800647{
Janosch Frank86a518b2022-03-30 12:35:55 +0000648 ERRP_GUARD();
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200649 GuestPhysBlock *block;
Janosch Frank1e811302022-08-11 12:10:56 +0000650 int64_t memblock_size, memblock_start;
Wen Congyang783e9b42012-05-07 12:10:47 +0800651
Janosch Frank1e811302022-08-11 12:10:56 +0000652 QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
Janosch Frankdddf7252022-08-11 12:10:58 +0000653 memblock_start = dump_filtered_memblock_start(block, s->filter_area_begin, s->filter_area_length);
Janosch Frank1e811302022-08-11 12:10:56 +0000654 if (memblock_start == -1) {
655 continue;
Wen Congyang783e9b42012-05-07 12:10:47 +0800656 }
Janosch Frank1e811302022-08-11 12:10:56 +0000657
Janosch Frankdddf7252022-08-11 12:10:58 +0000658 memblock_size = dump_filtered_memblock_size(block, s->filter_area_begin, s->filter_area_length);
Janosch Frank1e811302022-08-11 12:10:56 +0000659
660 /* Write the memory to file */
661 write_memory(s, block, memblock_start, memblock_size, errp);
Janosch Frank86a518b2022-03-30 12:35:55 +0000662 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800663 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800664 }
Janosch Frank1e811302022-08-11 12:10:56 +0000665 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800666}
667
zhanghailiang4c7e2512014-10-09 14:13:11 +0800668static void create_vmcore(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800669{
Janosch Frank86a518b2022-03-30 12:35:55 +0000670 ERRP_GUARD();
Wen Congyang783e9b42012-05-07 12:10:47 +0800671
Janosch Frank86a518b2022-03-30 12:35:55 +0000672 dump_begin(s, errp);
673 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800674 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800675 }
676
zhanghailiang4c7e2512014-10-09 14:13:11 +0800677 dump_iterate(s, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800678}
679
qiaonuohanfda05382014-02-18 14:11:27 +0800680static int write_start_flat_header(int fd)
681{
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200682 MakedumpfileHeader *mh;
qiaonuohanfda05382014-02-18 14:11:27 +0800683 int ret = 0;
684
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200685 QEMU_BUILD_BUG_ON(sizeof *mh > MAX_SIZE_MDF_HEADER);
686 mh = g_malloc0(MAX_SIZE_MDF_HEADER);
qiaonuohanfda05382014-02-18 14:11:27 +0800687
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200688 memcpy(mh->signature, MAKEDUMPFILE_SIGNATURE,
689 MIN(sizeof mh->signature, sizeof MAKEDUMPFILE_SIGNATURE));
qiaonuohanfda05382014-02-18 14:11:27 +0800690
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200691 mh->type = cpu_to_be64(TYPE_FLAT_HEADER);
692 mh->version = cpu_to_be64(VERSION_FLAT_HEADER);
qiaonuohanfda05382014-02-18 14:11:27 +0800693
694 size_t written_size;
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200695 written_size = qemu_write_full(fd, mh, MAX_SIZE_MDF_HEADER);
qiaonuohanfda05382014-02-18 14:11:27 +0800696 if (written_size != MAX_SIZE_MDF_HEADER) {
697 ret = -1;
698 }
699
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200700 g_free(mh);
qiaonuohanfda05382014-02-18 14:11:27 +0800701 return ret;
702}
703
704static int write_end_flat_header(int fd)
705{
706 MakedumpfileDataHeader mdh;
707
708 mdh.offset = END_FLAG_FLAT_HEADER;
709 mdh.buf_size = END_FLAG_FLAT_HEADER;
710
711 size_t written_size;
712 written_size = qemu_write_full(fd, &mdh, sizeof(mdh));
713 if (written_size != sizeof(mdh)) {
714 return -1;
715 }
716
717 return 0;
718}
719
qiaonuohan5d31bab2014-02-18 14:11:28 +0800720static int write_buffer(int fd, off_t offset, const void *buf, size_t size)
721{
722 size_t written_size;
723 MakedumpfileDataHeader mdh;
724
725 mdh.offset = cpu_to_be64(offset);
726 mdh.buf_size = cpu_to_be64(size);
727
728 written_size = qemu_write_full(fd, &mdh, sizeof(mdh));
729 if (written_size != sizeof(mdh)) {
730 return -1;
731 }
732
733 written_size = qemu_write_full(fd, buf, size);
734 if (written_size != size) {
735 return -1;
736 }
737
738 return 0;
739}
740
qiaonuohan4835ef72014-02-18 14:11:29 +0800741static int buf_write_note(const void *buf, size_t size, void *opaque)
742{
743 DumpState *s = opaque;
744
745 /* note_buf is not enough */
746 if (s->note_buf_offset + size > s->note_size) {
747 return -1;
748 }
749
750 memcpy(s->note_buf + s->note_buf_offset, buf, size);
751
752 s->note_buf_offset += size;
753
754 return 0;
755}
756
Marc-André Lureau903ef732017-09-11 18:59:25 +0200757/*
758 * This function retrieves various sizes from an elf header.
759 *
760 * @note has to be a valid ELF note. The return sizes are unmodified
761 * (not padded or rounded up to be multiple of 4).
762 */
763static void get_note_sizes(DumpState *s, const void *note,
764 uint64_t *note_head_size,
765 uint64_t *name_size,
766 uint64_t *desc_size)
767{
768 uint64_t note_head_sz;
769 uint64_t name_sz;
770 uint64_t desc_sz;
771
Janosch Frank05bbaa502022-03-30 12:36:00 +0000772 if (dump_is_64bit(s)) {
Marc-André Lureau903ef732017-09-11 18:59:25 +0200773 const Elf64_Nhdr *hdr = note;
774 note_head_sz = sizeof(Elf64_Nhdr);
775 name_sz = tswap64(hdr->n_namesz);
776 desc_sz = tswap64(hdr->n_descsz);
777 } else {
778 const Elf32_Nhdr *hdr = note;
779 note_head_sz = sizeof(Elf32_Nhdr);
780 name_sz = tswap32(hdr->n_namesz);
781 desc_sz = tswap32(hdr->n_descsz);
782 }
783
784 if (note_head_size) {
785 *note_head_size = note_head_sz;
786 }
787 if (name_size) {
788 *name_size = name_sz;
789 }
790 if (desc_size) {
791 *desc_size = desc_sz;
792 }
793}
794
Marc-André Lureaud9feb512017-09-11 18:59:26 +0200795static bool note_name_equal(DumpState *s,
796 const uint8_t *note, const char *name)
797{
798 int len = strlen(name) + 1;
799 uint64_t head_size, name_size;
800
801 get_note_sizes(s, note, &head_size, &name_size, NULL);
802 head_size = ROUND_UP(head_size, 4);
803
Marc-André Lureauc983ca82017-12-12 15:53:59 +0100804 return name_size == len && memcmp(note + head_size, name, len) == 0;
Marc-André Lureaud9feb512017-09-11 18:59:26 +0200805}
806
qiaonuohan298f1162014-02-18 14:11:32 +0800807/* write common header, sub header and elf note to vmcore */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800808static void create_header32(DumpState *s, Error **errp)
qiaonuohan298f1162014-02-18 14:11:32 +0800809{
Janosch Frank86a518b2022-03-30 12:35:55 +0000810 ERRP_GUARD();
qiaonuohan298f1162014-02-18 14:11:32 +0800811 DiskDumpHeader32 *dh = NULL;
812 KdumpSubHeader32 *kh = NULL;
813 size_t size;
qiaonuohan298f1162014-02-18 14:11:32 +0800814 uint32_t block_size;
815 uint32_t sub_hdr_size;
816 uint32_t bitmap_blocks;
817 uint32_t status = 0;
818 uint64_t offset_note;
819
820 /* write common header, the version of kdump-compressed format is 6th */
821 size = sizeof(DiskDumpHeader32);
822 dh = g_malloc0(size);
823
Eric Blake84c868f2018-03-27 15:21:51 -0500824 memcpy(dh->signature, KDUMP_SIGNATURE, SIG_LEN);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200825 dh->header_version = cpu_to_dump32(s, 6);
Andrew Jones8161bef2016-01-11 20:56:20 +0100826 block_size = s->dump_info.page_size;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200827 dh->block_size = cpu_to_dump32(s, block_size);
qiaonuohan298f1162014-02-18 14:11:32 +0800828 sub_hdr_size = sizeof(struct KdumpSubHeader32) + s->note_size;
829 sub_hdr_size = DIV_ROUND_UP(sub_hdr_size, block_size);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200830 dh->sub_hdr_size = cpu_to_dump32(s, sub_hdr_size);
qiaonuohan298f1162014-02-18 14:11:32 +0800831 /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200832 dh->max_mapnr = cpu_to_dump32(s, MIN(s->max_mapnr, UINT_MAX));
833 dh->nr_cpus = cpu_to_dump32(s, s->nr_cpus);
qiaonuohan298f1162014-02-18 14:11:32 +0800834 bitmap_blocks = DIV_ROUND_UP(s->len_dump_bitmap, block_size) * 2;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200835 dh->bitmap_blocks = cpu_to_dump32(s, bitmap_blocks);
qiaonuohan4ab23a92014-02-18 14:11:37 +0800836 strncpy(dh->utsname.machine, ELF_MACHINE_UNAME, sizeof(dh->utsname.machine));
qiaonuohan298f1162014-02-18 14:11:32 +0800837
838 if (s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) {
839 status |= DUMP_DH_COMPRESSED_ZLIB;
840 }
841#ifdef CONFIG_LZO
842 if (s->flag_compress & DUMP_DH_COMPRESSED_LZO) {
843 status |= DUMP_DH_COMPRESSED_LZO;
844 }
845#endif
846#ifdef CONFIG_SNAPPY
847 if (s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) {
848 status |= DUMP_DH_COMPRESSED_SNAPPY;
849 }
850#endif
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200851 dh->status = cpu_to_dump32(s, status);
qiaonuohan298f1162014-02-18 14:11:32 +0800852
853 if (write_buffer(s->fd, 0, dh, size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800854 error_setg(errp, "dump: failed to write disk dump header");
qiaonuohan298f1162014-02-18 14:11:32 +0800855 goto out;
856 }
857
858 /* write sub header */
859 size = sizeof(KdumpSubHeader32);
860 kh = g_malloc0(size);
861
862 /* 64bit max_mapnr_64 */
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200863 kh->max_mapnr_64 = cpu_to_dump64(s, s->max_mapnr);
Andrew Jonesb6e05aa2016-01-11 20:56:21 +0100864 kh->phys_base = cpu_to_dump32(s, s->dump_info.phys_base);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200865 kh->dump_level = cpu_to_dump32(s, DUMP_LEVEL);
qiaonuohan298f1162014-02-18 14:11:32 +0800866
867 offset_note = DISKDUMP_HEADER_BLOCKS * block_size + size;
Marc-André Lureau9ada5752017-09-11 18:59:27 +0200868 if (s->guest_note &&
869 note_name_equal(s, s->guest_note, "VMCOREINFO")) {
870 uint64_t hsize, name_size, size_vmcoreinfo_desc, offset_vmcoreinfo;
871
872 get_note_sizes(s, s->guest_note,
873 &hsize, &name_size, &size_vmcoreinfo_desc);
874 offset_vmcoreinfo = offset_note + s->note_size - s->guest_note_size +
875 (DIV_ROUND_UP(hsize, 4) + DIV_ROUND_UP(name_size, 4)) * 4;
876 kh->offset_vmcoreinfo = cpu_to_dump64(s, offset_vmcoreinfo);
877 kh->size_vmcoreinfo = cpu_to_dump32(s, size_vmcoreinfo_desc);
878 }
879
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200880 kh->offset_note = cpu_to_dump64(s, offset_note);
881 kh->note_size = cpu_to_dump32(s, s->note_size);
qiaonuohan298f1162014-02-18 14:11:32 +0800882
883 if (write_buffer(s->fd, DISKDUMP_HEADER_BLOCKS *
884 block_size, kh, size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800885 error_setg(errp, "dump: failed to write kdump sub header");
qiaonuohan298f1162014-02-18 14:11:32 +0800886 goto out;
887 }
888
889 /* write note */
890 s->note_buf = g_malloc0(s->note_size);
891 s->note_buf_offset = 0;
892
893 /* use s->note_buf to store notes temporarily */
Janosch Frank86a518b2022-03-30 12:35:55 +0000894 write_elf32_notes(buf_write_note, s, errp);
895 if (*errp) {
qiaonuohan298f1162014-02-18 14:11:32 +0800896 goto out;
897 }
qiaonuohan298f1162014-02-18 14:11:32 +0800898 if (write_buffer(s->fd, offset_note, s->note_buf,
899 s->note_size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800900 error_setg(errp, "dump: failed to write notes");
qiaonuohan298f1162014-02-18 14:11:32 +0800901 goto out;
902 }
903
904 /* get offset of dump_bitmap */
905 s->offset_dump_bitmap = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size) *
906 block_size;
907
908 /* get offset of page */
909 s->offset_page = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size + bitmap_blocks) *
910 block_size;
911
912out:
913 g_free(dh);
914 g_free(kh);
915 g_free(s->note_buf);
qiaonuohan298f1162014-02-18 14:11:32 +0800916}
917
918/* write common header, sub header and elf note to vmcore */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800919static void create_header64(DumpState *s, Error **errp)
qiaonuohan298f1162014-02-18 14:11:32 +0800920{
Janosch Frank86a518b2022-03-30 12:35:55 +0000921 ERRP_GUARD();
qiaonuohan298f1162014-02-18 14:11:32 +0800922 DiskDumpHeader64 *dh = NULL;
923 KdumpSubHeader64 *kh = NULL;
924 size_t size;
qiaonuohan298f1162014-02-18 14:11:32 +0800925 uint32_t block_size;
926 uint32_t sub_hdr_size;
927 uint32_t bitmap_blocks;
928 uint32_t status = 0;
929 uint64_t offset_note;
930
931 /* write common header, the version of kdump-compressed format is 6th */
932 size = sizeof(DiskDumpHeader64);
933 dh = g_malloc0(size);
934
Eric Blake84c868f2018-03-27 15:21:51 -0500935 memcpy(dh->signature, KDUMP_SIGNATURE, SIG_LEN);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200936 dh->header_version = cpu_to_dump32(s, 6);
Andrew Jones8161bef2016-01-11 20:56:20 +0100937 block_size = s->dump_info.page_size;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200938 dh->block_size = cpu_to_dump32(s, block_size);
qiaonuohan298f1162014-02-18 14:11:32 +0800939 sub_hdr_size = sizeof(struct KdumpSubHeader64) + s->note_size;
940 sub_hdr_size = DIV_ROUND_UP(sub_hdr_size, block_size);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200941 dh->sub_hdr_size = cpu_to_dump32(s, sub_hdr_size);
qiaonuohan298f1162014-02-18 14:11:32 +0800942 /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200943 dh->max_mapnr = cpu_to_dump32(s, MIN(s->max_mapnr, UINT_MAX));
944 dh->nr_cpus = cpu_to_dump32(s, s->nr_cpus);
qiaonuohan298f1162014-02-18 14:11:32 +0800945 bitmap_blocks = DIV_ROUND_UP(s->len_dump_bitmap, block_size) * 2;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200946 dh->bitmap_blocks = cpu_to_dump32(s, bitmap_blocks);
qiaonuohan4ab23a92014-02-18 14:11:37 +0800947 strncpy(dh->utsname.machine, ELF_MACHINE_UNAME, sizeof(dh->utsname.machine));
qiaonuohan298f1162014-02-18 14:11:32 +0800948
949 if (s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) {
950 status |= DUMP_DH_COMPRESSED_ZLIB;
951 }
952#ifdef CONFIG_LZO
953 if (s->flag_compress & DUMP_DH_COMPRESSED_LZO) {
954 status |= DUMP_DH_COMPRESSED_LZO;
955 }
956#endif
957#ifdef CONFIG_SNAPPY
958 if (s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) {
959 status |= DUMP_DH_COMPRESSED_SNAPPY;
960 }
961#endif
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200962 dh->status = cpu_to_dump32(s, status);
qiaonuohan298f1162014-02-18 14:11:32 +0800963
964 if (write_buffer(s->fd, 0, dh, size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800965 error_setg(errp, "dump: failed to write disk dump header");
qiaonuohan298f1162014-02-18 14:11:32 +0800966 goto out;
967 }
968
969 /* write sub header */
970 size = sizeof(KdumpSubHeader64);
971 kh = g_malloc0(size);
972
973 /* 64bit max_mapnr_64 */
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200974 kh->max_mapnr_64 = cpu_to_dump64(s, s->max_mapnr);
Andrew Jonesb6e05aa2016-01-11 20:56:21 +0100975 kh->phys_base = cpu_to_dump64(s, s->dump_info.phys_base);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200976 kh->dump_level = cpu_to_dump32(s, DUMP_LEVEL);
qiaonuohan298f1162014-02-18 14:11:32 +0800977
978 offset_note = DISKDUMP_HEADER_BLOCKS * block_size + size;
Marc-André Lureau9ada5752017-09-11 18:59:27 +0200979 if (s->guest_note &&
980 note_name_equal(s, s->guest_note, "VMCOREINFO")) {
981 uint64_t hsize, name_size, size_vmcoreinfo_desc, offset_vmcoreinfo;
982
983 get_note_sizes(s, s->guest_note,
984 &hsize, &name_size, &size_vmcoreinfo_desc);
985 offset_vmcoreinfo = offset_note + s->note_size - s->guest_note_size +
986 (DIV_ROUND_UP(hsize, 4) + DIV_ROUND_UP(name_size, 4)) * 4;
987 kh->offset_vmcoreinfo = cpu_to_dump64(s, offset_vmcoreinfo);
988 kh->size_vmcoreinfo = cpu_to_dump64(s, size_vmcoreinfo_desc);
989 }
990
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200991 kh->offset_note = cpu_to_dump64(s, offset_note);
992 kh->note_size = cpu_to_dump64(s, s->note_size);
qiaonuohan298f1162014-02-18 14:11:32 +0800993
994 if (write_buffer(s->fd, DISKDUMP_HEADER_BLOCKS *
995 block_size, kh, size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800996 error_setg(errp, "dump: failed to write kdump sub header");
qiaonuohan298f1162014-02-18 14:11:32 +0800997 goto out;
998 }
999
1000 /* write note */
1001 s->note_buf = g_malloc0(s->note_size);
1002 s->note_buf_offset = 0;
1003
1004 /* use s->note_buf to store notes temporarily */
Janosch Frank86a518b2022-03-30 12:35:55 +00001005 write_elf64_notes(buf_write_note, s, errp);
1006 if (*errp) {
qiaonuohan298f1162014-02-18 14:11:32 +08001007 goto out;
1008 }
1009
1010 if (write_buffer(s->fd, offset_note, s->note_buf,
1011 s->note_size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001012 error_setg(errp, "dump: failed to write notes");
qiaonuohan298f1162014-02-18 14:11:32 +08001013 goto out;
1014 }
1015
1016 /* get offset of dump_bitmap */
1017 s->offset_dump_bitmap = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size) *
1018 block_size;
1019
1020 /* get offset of page */
1021 s->offset_page = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size + bitmap_blocks) *
1022 block_size;
1023
1024out:
1025 g_free(dh);
1026 g_free(kh);
1027 g_free(s->note_buf);
qiaonuohan298f1162014-02-18 14:11:32 +08001028}
1029
zhanghailiang4c7e2512014-10-09 14:13:11 +08001030static void write_dump_header(DumpState *s, Error **errp)
qiaonuohan298f1162014-02-18 14:11:32 +08001031{
Janosch Frank05bbaa502022-03-30 12:36:00 +00001032 if (dump_is_64bit(s)) {
Markus Armbruster992861f2020-07-07 18:06:04 +02001033 create_header64(s, errp);
Janosch Frank05bbaa502022-03-30 12:36:00 +00001034 } else {
1035 create_header32(s, errp);
zhanghailiang4c7e2512014-10-09 14:13:11 +08001036 }
qiaonuohan298f1162014-02-18 14:11:32 +08001037}
1038
Andrew Jones8161bef2016-01-11 20:56:20 +01001039static size_t dump_bitmap_get_bufsize(DumpState *s)
1040{
1041 return s->dump_info.page_size;
1042}
1043
qiaonuohand0686c72014-02-18 14:11:33 +08001044/*
1045 * set dump_bitmap sequencely. the bit before last_pfn is not allowed to be
1046 * rewritten, so if need to set the first bit, set last_pfn and pfn to 0.
1047 * set_dump_bitmap will always leave the recently set bit un-sync. And setting
1048 * (last bit + sizeof(buf) * 8) to 0 will do flushing the content in buf into
1049 * vmcore, ie. synchronizing un-sync bit into vmcore.
1050 */
1051static int set_dump_bitmap(uint64_t last_pfn, uint64_t pfn, bool value,
1052 uint8_t *buf, DumpState *s)
1053{
1054 off_t old_offset, new_offset;
1055 off_t offset_bitmap1, offset_bitmap2;
1056 uint32_t byte, bit;
Andrew Jones8161bef2016-01-11 20:56:20 +01001057 size_t bitmap_bufsize = dump_bitmap_get_bufsize(s);
1058 size_t bits_per_buf = bitmap_bufsize * CHAR_BIT;
qiaonuohand0686c72014-02-18 14:11:33 +08001059
1060 /* should not set the previous place */
1061 assert(last_pfn <= pfn);
1062
1063 /*
1064 * if the bit needed to be set is not cached in buf, flush the data in buf
1065 * to vmcore firstly.
1066 * making new_offset be bigger than old_offset can also sync remained data
1067 * into vmcore.
1068 */
Andrew Jones8161bef2016-01-11 20:56:20 +01001069 old_offset = bitmap_bufsize * (last_pfn / bits_per_buf);
1070 new_offset = bitmap_bufsize * (pfn / bits_per_buf);
qiaonuohand0686c72014-02-18 14:11:33 +08001071
1072 while (old_offset < new_offset) {
1073 /* calculate the offset and write dump_bitmap */
1074 offset_bitmap1 = s->offset_dump_bitmap + old_offset;
1075 if (write_buffer(s->fd, offset_bitmap1, buf,
Andrew Jones8161bef2016-01-11 20:56:20 +01001076 bitmap_bufsize) < 0) {
qiaonuohand0686c72014-02-18 14:11:33 +08001077 return -1;
1078 }
1079
1080 /* dump level 1 is chosen, so 1st and 2nd bitmap are same */
1081 offset_bitmap2 = s->offset_dump_bitmap + s->len_dump_bitmap +
1082 old_offset;
1083 if (write_buffer(s->fd, offset_bitmap2, buf,
Andrew Jones8161bef2016-01-11 20:56:20 +01001084 bitmap_bufsize) < 0) {
qiaonuohand0686c72014-02-18 14:11:33 +08001085 return -1;
1086 }
1087
Andrew Jones8161bef2016-01-11 20:56:20 +01001088 memset(buf, 0, bitmap_bufsize);
1089 old_offset += bitmap_bufsize;
qiaonuohand0686c72014-02-18 14:11:33 +08001090 }
1091
1092 /* get the exact place of the bit in the buf, and set it */
Andrew Jones8161bef2016-01-11 20:56:20 +01001093 byte = (pfn % bits_per_buf) / CHAR_BIT;
1094 bit = (pfn % bits_per_buf) % CHAR_BIT;
qiaonuohand0686c72014-02-18 14:11:33 +08001095 if (value) {
1096 buf[byte] |= 1u << bit;
1097 } else {
1098 buf[byte] &= ~(1u << bit);
1099 }
1100
1101 return 0;
1102}
1103
Andrew Jones8161bef2016-01-11 20:56:20 +01001104static uint64_t dump_paddr_to_pfn(DumpState *s, uint64_t addr)
1105{
1106 int target_page_shift = ctz32(s->dump_info.page_size);
1107
1108 return (addr >> target_page_shift) - ARCH_PFN_OFFSET;
1109}
1110
1111static uint64_t dump_pfn_to_paddr(DumpState *s, uint64_t pfn)
1112{
1113 int target_page_shift = ctz32(s->dump_info.page_size);
1114
1115 return (pfn + ARCH_PFN_OFFSET) << target_page_shift;
1116}
1117
qiaonuohand0686c72014-02-18 14:11:33 +08001118/*
1119 * exam every page and return the page frame number and the address of the page.
1120 * bufptr can be NULL. note: the blocks here is supposed to reflect guest-phys
1121 * blocks, so block->target_start and block->target_end should be interal
1122 * multiples of the target page size.
1123 */
1124static bool get_next_page(GuestPhysBlock **blockptr, uint64_t *pfnptr,
1125 uint8_t **bufptr, DumpState *s)
1126{
1127 GuestPhysBlock *block = *blockptr;
Andrew Jones8161bef2016-01-11 20:56:20 +01001128 hwaddr addr, target_page_mask = ~((hwaddr)s->dump_info.page_size - 1);
qiaonuohand0686c72014-02-18 14:11:33 +08001129 uint8_t *buf;
1130
1131 /* block == NULL means the start of the iteration */
1132 if (!block) {
1133 block = QTAILQ_FIRST(&s->guest_phys_blocks.head);
1134 *blockptr = block;
Marc-André Lureau08df3432022-08-25 12:40:12 +04001135 addr = block->target_start;
1136 } else {
1137 addr = dump_pfn_to_paddr(s, *pfnptr + 1);
qiaonuohand0686c72014-02-18 14:11:33 +08001138 }
Marc-André Lureau08df3432022-08-25 12:40:12 +04001139 assert(block != NULL);
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 }
Marc-André Lureau08df3432022-08-25 12:40:12 +04001151 addr = block->target_start;
qiaonuohand0686c72014-02-18 14:11:33 +08001152 buf = block->host_addr;
1153 }
1154
Marc-André Lureau08df3432022-08-25 12:40:12 +04001155 assert((block->target_start & ~target_page_mask) == 0);
1156 assert((block->target_end & ~target_page_mask) == 0);
1157 *pfnptr = dump_paddr_to_pfn(s, addr);
qiaonuohand0686c72014-02-18 14:11:33 +08001158 if (bufptr) {
1159 *bufptr = buf;
1160 }
1161
1162 return true;
1163}
1164
zhanghailiang4c7e2512014-10-09 14:13:11 +08001165static void write_dump_bitmap(DumpState *s, Error **errp)
qiaonuohand0686c72014-02-18 14:11:33 +08001166{
1167 int ret = 0;
1168 uint64_t last_pfn, pfn;
1169 void *dump_bitmap_buf;
1170 size_t num_dumpable;
1171 GuestPhysBlock *block_iter = NULL;
Andrew Jones8161bef2016-01-11 20:56:20 +01001172 size_t bitmap_bufsize = dump_bitmap_get_bufsize(s);
1173 size_t bits_per_buf = bitmap_bufsize * CHAR_BIT;
qiaonuohand0686c72014-02-18 14:11:33 +08001174
1175 /* dump_bitmap_buf is used to store dump_bitmap temporarily */
Andrew Jones8161bef2016-01-11 20:56:20 +01001176 dump_bitmap_buf = g_malloc0(bitmap_bufsize);
qiaonuohand0686c72014-02-18 14:11:33 +08001177
1178 num_dumpable = 0;
1179 last_pfn = 0;
1180
1181 /*
1182 * exam memory page by page, and set the bit in dump_bitmap corresponded
1183 * to the existing page.
1184 */
1185 while (get_next_page(&block_iter, &pfn, NULL, s)) {
1186 ret = set_dump_bitmap(last_pfn, pfn, true, dump_bitmap_buf, s);
1187 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001188 error_setg(errp, "dump: failed to set dump_bitmap");
qiaonuohand0686c72014-02-18 14:11:33 +08001189 goto out;
1190 }
1191
1192 last_pfn = pfn;
1193 num_dumpable++;
1194 }
1195
1196 /*
1197 * set_dump_bitmap will always leave the recently set bit un-sync. Here we
Andrew Jones8161bef2016-01-11 20:56:20 +01001198 * set the remaining bits from last_pfn to the end of the bitmap buffer to
1199 * 0. With those set, the un-sync bit will be synchronized into the vmcore.
qiaonuohand0686c72014-02-18 14:11:33 +08001200 */
1201 if (num_dumpable > 0) {
Andrew Jones8161bef2016-01-11 20:56:20 +01001202 ret = set_dump_bitmap(last_pfn, last_pfn + bits_per_buf, false,
qiaonuohand0686c72014-02-18 14:11:33 +08001203 dump_bitmap_buf, s);
1204 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001205 error_setg(errp, "dump: failed to sync dump_bitmap");
qiaonuohand0686c72014-02-18 14:11:33 +08001206 goto out;
1207 }
1208 }
1209
1210 /* number of dumpable pages that will be dumped later */
1211 s->num_dumpable = num_dumpable;
1212
1213out:
1214 g_free(dump_bitmap_buf);
qiaonuohand0686c72014-02-18 14:11:33 +08001215}
1216
qiaonuohan64cfba62014-02-18 14:11:34 +08001217static void prepare_data_cache(DataCache *data_cache, DumpState *s,
1218 off_t offset)
1219{
1220 data_cache->fd = s->fd;
1221 data_cache->data_size = 0;
Andrew Jones8161bef2016-01-11 20:56:20 +01001222 data_cache->buf_size = 4 * dump_bitmap_get_bufsize(s);
1223 data_cache->buf = g_malloc0(data_cache->buf_size);
qiaonuohan64cfba62014-02-18 14:11:34 +08001224 data_cache->offset = offset;
1225}
1226
1227static int write_cache(DataCache *dc, const void *buf, size_t size,
1228 bool flag_sync)
1229{
1230 /*
1231 * dc->buf_size should not be less than size, otherwise dc will never be
1232 * enough
1233 */
1234 assert(size <= dc->buf_size);
1235
1236 /*
1237 * if flag_sync is set, synchronize data in dc->buf into vmcore.
1238 * otherwise check if the space is enough for caching data in buf, if not,
1239 * write the data in dc->buf to dc->fd and reset dc->buf
1240 */
1241 if ((!flag_sync && dc->data_size + size > dc->buf_size) ||
1242 (flag_sync && dc->data_size > 0)) {
1243 if (write_buffer(dc->fd, dc->offset, dc->buf, dc->data_size) < 0) {
1244 return -1;
1245 }
1246
1247 dc->offset += dc->data_size;
1248 dc->data_size = 0;
1249 }
1250
1251 if (!flag_sync) {
1252 memcpy(dc->buf + dc->data_size, buf, size);
1253 dc->data_size += size;
1254 }
1255
1256 return 0;
1257}
1258
1259static void free_data_cache(DataCache *data_cache)
1260{
1261 g_free(data_cache->buf);
1262}
1263
qiaonuohand12f57e2014-02-18 14:11:35 +08001264static size_t get_len_buf_out(size_t page_size, uint32_t flag_compress)
1265{
Laszlo Ersekb87ef352014-05-20 13:42:53 +02001266 switch (flag_compress) {
1267 case DUMP_DH_COMPRESSED_ZLIB:
1268 return compressBound(page_size);
qiaonuohand12f57e2014-02-18 14:11:35 +08001269
Laszlo Ersekb87ef352014-05-20 13:42:53 +02001270 case DUMP_DH_COMPRESSED_LZO:
1271 /*
1272 * LZO will expand incompressible data by a little amount. Please check
1273 * the following URL to see the expansion calculation:
1274 * http://www.oberhumer.com/opensource/lzo/lzofaq.php
1275 */
1276 return page_size + page_size / 16 + 64 + 3;
qiaonuohand12f57e2014-02-18 14:11:35 +08001277
1278#ifdef CONFIG_SNAPPY
Laszlo Ersekb87ef352014-05-20 13:42:53 +02001279 case DUMP_DH_COMPRESSED_SNAPPY:
1280 return snappy_max_compressed_length(page_size);
qiaonuohand12f57e2014-02-18 14:11:35 +08001281#endif
Laszlo Ersekb87ef352014-05-20 13:42:53 +02001282 }
1283 return 0;
qiaonuohand12f57e2014-02-18 14:11:35 +08001284}
1285
zhanghailiang4c7e2512014-10-09 14:13:11 +08001286static void write_dump_pages(DumpState *s, Error **errp)
qiaonuohand12f57e2014-02-18 14:11:35 +08001287{
1288 int ret = 0;
1289 DataCache page_desc, page_data;
1290 size_t len_buf_out, size_out;
1291#ifdef CONFIG_LZO
1292 lzo_bytep wrkmem = NULL;
1293#endif
1294 uint8_t *buf_out = NULL;
1295 off_t offset_desc, offset_data;
1296 PageDescriptor pd, pd_zero;
1297 uint8_t *buf;
qiaonuohand12f57e2014-02-18 14:11:35 +08001298 GuestPhysBlock *block_iter = NULL;
1299 uint64_t pfn_iter;
1300
1301 /* get offset of page_desc and page_data in dump file */
1302 offset_desc = s->offset_page;
1303 offset_data = offset_desc + sizeof(PageDescriptor) * s->num_dumpable;
1304
1305 prepare_data_cache(&page_desc, s, offset_desc);
1306 prepare_data_cache(&page_data, s, offset_data);
1307
1308 /* prepare buffer to store compressed data */
Andrew Jones8161bef2016-01-11 20:56:20 +01001309 len_buf_out = get_len_buf_out(s->dump_info.page_size, s->flag_compress);
Laszlo Ersekb87ef352014-05-20 13:42:53 +02001310 assert(len_buf_out != 0);
qiaonuohand12f57e2014-02-18 14:11:35 +08001311
1312#ifdef CONFIG_LZO
1313 wrkmem = g_malloc(LZO1X_1_MEM_COMPRESS);
1314#endif
1315
1316 buf_out = g_malloc(len_buf_out);
1317
1318 /*
1319 * init zero page's page_desc and page_data, because every zero page
1320 * uses the same page_data
1321 */
Andrew Jones8161bef2016-01-11 20:56:20 +01001322 pd_zero.size = cpu_to_dump32(s, s->dump_info.page_size);
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001323 pd_zero.flags = cpu_to_dump32(s, 0);
1324 pd_zero.offset = cpu_to_dump64(s, offset_data);
1325 pd_zero.page_flags = cpu_to_dump64(s, 0);
Andrew Jones8161bef2016-01-11 20:56:20 +01001326 buf = g_malloc0(s->dump_info.page_size);
1327 ret = write_cache(&page_data, buf, s->dump_info.page_size, false);
qiaonuohand12f57e2014-02-18 14:11:35 +08001328 g_free(buf);
1329 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001330 error_setg(errp, "dump: failed to write page data (zero page)");
qiaonuohand12f57e2014-02-18 14:11:35 +08001331 goto out;
1332 }
1333
Andrew Jones8161bef2016-01-11 20:56:20 +01001334 offset_data += s->dump_info.page_size;
qiaonuohand12f57e2014-02-18 14:11:35 +08001335
1336 /*
1337 * dump memory to vmcore page by page. zero page will all be resided in the
1338 * first page of page section
1339 */
1340 while (get_next_page(&block_iter, &pfn_iter, &buf, s)) {
1341 /* check zero page */
Juan Quintelaf13f22b2021-11-18 15:57:44 +01001342 if (buffer_is_zero(buf, s->dump_info.page_size)) {
qiaonuohand12f57e2014-02-18 14:11:35 +08001343 ret = write_cache(&page_desc, &pd_zero, sizeof(PageDescriptor),
1344 false);
1345 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001346 error_setg(errp, "dump: failed to write page desc");
qiaonuohand12f57e2014-02-18 14:11:35 +08001347 goto out;
1348 }
1349 } else {
1350 /*
1351 * not zero page, then:
1352 * 1. compress the page
1353 * 2. write the compressed page into the cache of page_data
1354 * 3. get page desc of the compressed page and write it into the
1355 * cache of page_desc
1356 *
1357 * only one compression format will be used here, for
1358 * s->flag_compress is set. But when compression fails to work,
1359 * we fall back to save in plaintext.
1360 */
1361 size_out = len_buf_out;
1362 if ((s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) &&
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001363 (compress2(buf_out, (uLongf *)&size_out, buf,
Andrew Jones8161bef2016-01-11 20:56:20 +01001364 s->dump_info.page_size, Z_BEST_SPEED) == Z_OK) &&
1365 (size_out < s->dump_info.page_size)) {
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001366 pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_ZLIB);
1367 pd.size = cpu_to_dump32(s, size_out);
qiaonuohand12f57e2014-02-18 14:11:35 +08001368
1369 ret = write_cache(&page_data, buf_out, size_out, false);
1370 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001371 error_setg(errp, "dump: failed to write page data");
qiaonuohand12f57e2014-02-18 14:11:35 +08001372 goto out;
1373 }
1374#ifdef CONFIG_LZO
1375 } else if ((s->flag_compress & DUMP_DH_COMPRESSED_LZO) &&
Andrew Jones8161bef2016-01-11 20:56:20 +01001376 (lzo1x_1_compress(buf, s->dump_info.page_size, buf_out,
qiaonuohand12f57e2014-02-18 14:11:35 +08001377 (lzo_uint *)&size_out, wrkmem) == LZO_E_OK) &&
Andrew Jones8161bef2016-01-11 20:56:20 +01001378 (size_out < s->dump_info.page_size)) {
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001379 pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_LZO);
1380 pd.size = cpu_to_dump32(s, size_out);
qiaonuohand12f57e2014-02-18 14:11:35 +08001381
1382 ret = write_cache(&page_data, buf_out, size_out, false);
1383 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001384 error_setg(errp, "dump: failed to write page data");
qiaonuohand12f57e2014-02-18 14:11:35 +08001385 goto out;
1386 }
1387#endif
1388#ifdef CONFIG_SNAPPY
1389 } else if ((s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) &&
Andrew Jones8161bef2016-01-11 20:56:20 +01001390 (snappy_compress((char *)buf, s->dump_info.page_size,
qiaonuohand12f57e2014-02-18 14:11:35 +08001391 (char *)buf_out, &size_out) == SNAPPY_OK) &&
Andrew Jones8161bef2016-01-11 20:56:20 +01001392 (size_out < s->dump_info.page_size)) {
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001393 pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_SNAPPY);
1394 pd.size = cpu_to_dump32(s, size_out);
qiaonuohand12f57e2014-02-18 14:11:35 +08001395
1396 ret = write_cache(&page_data, buf_out, size_out, false);
1397 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001398 error_setg(errp, "dump: failed to write page data");
qiaonuohand12f57e2014-02-18 14:11:35 +08001399 goto out;
1400 }
1401#endif
1402 } else {
1403 /*
1404 * fall back to save in plaintext, size_out should be
Andrew Jones8161bef2016-01-11 20:56:20 +01001405 * assigned the target's page size
qiaonuohand12f57e2014-02-18 14:11:35 +08001406 */
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001407 pd.flags = cpu_to_dump32(s, 0);
Andrew Jones8161bef2016-01-11 20:56:20 +01001408 size_out = s->dump_info.page_size;
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001409 pd.size = cpu_to_dump32(s, size_out);
qiaonuohand12f57e2014-02-18 14:11:35 +08001410
Andrew Jones8161bef2016-01-11 20:56:20 +01001411 ret = write_cache(&page_data, buf,
1412 s->dump_info.page_size, false);
qiaonuohand12f57e2014-02-18 14:11:35 +08001413 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001414 error_setg(errp, "dump: failed to write page data");
qiaonuohand12f57e2014-02-18 14:11:35 +08001415 goto out;
1416 }
1417 }
1418
1419 /* get and write page desc here */
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001420 pd.page_flags = cpu_to_dump64(s, 0);
1421 pd.offset = cpu_to_dump64(s, offset_data);
qiaonuohand12f57e2014-02-18 14:11:35 +08001422 offset_data += size_out;
1423
1424 ret = write_cache(&page_desc, &pd, sizeof(PageDescriptor), false);
1425 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001426 error_setg(errp, "dump: failed to write page desc");
qiaonuohand12f57e2014-02-18 14:11:35 +08001427 goto out;
1428 }
1429 }
Peter Xu2264c2c2016-02-18 13:16:53 +08001430 s->written_size += s->dump_info.page_size;
qiaonuohand12f57e2014-02-18 14:11:35 +08001431 }
1432
1433 ret = write_cache(&page_desc, NULL, 0, true);
1434 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001435 error_setg(errp, "dump: failed to sync cache for page_desc");
qiaonuohand12f57e2014-02-18 14:11:35 +08001436 goto out;
1437 }
1438 ret = write_cache(&page_data, NULL, 0, true);
1439 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001440 error_setg(errp, "dump: failed to sync cache for page_data");
qiaonuohand12f57e2014-02-18 14:11:35 +08001441 goto out;
1442 }
1443
1444out:
1445 free_data_cache(&page_desc);
1446 free_data_cache(&page_data);
1447
1448#ifdef CONFIG_LZO
1449 g_free(wrkmem);
1450#endif
1451
1452 g_free(buf_out);
qiaonuohand12f57e2014-02-18 14:11:35 +08001453}
1454
zhanghailiang4c7e2512014-10-09 14:13:11 +08001455static void create_kdump_vmcore(DumpState *s, Error **errp)
qiaonuohanb53ccc32014-02-18 14:11:36 +08001456{
Janosch Frank86a518b2022-03-30 12:35:55 +00001457 ERRP_GUARD();
qiaonuohanb53ccc32014-02-18 14:11:36 +08001458 int ret;
1459
1460 /*
1461 * the kdump-compressed format is:
1462 * File offset
1463 * +------------------------------------------+ 0x0
1464 * | main header (struct disk_dump_header) |
1465 * |------------------------------------------+ block 1
1466 * | sub header (struct kdump_sub_header) |
1467 * |------------------------------------------+ block 2
1468 * | 1st-dump_bitmap |
1469 * |------------------------------------------+ block 2 + X blocks
1470 * | 2nd-dump_bitmap | (aligned by block)
1471 * |------------------------------------------+ block 2 + 2 * X blocks
1472 * | page desc for pfn 0 (struct page_desc) | (aligned by block)
1473 * | page desc for pfn 1 (struct page_desc) |
1474 * | : |
1475 * |------------------------------------------| (not aligned by block)
1476 * | page data (pfn 0) |
1477 * | page data (pfn 1) |
1478 * | : |
1479 * +------------------------------------------+
1480 */
1481
1482 ret = write_start_flat_header(s->fd);
1483 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001484 error_setg(errp, "dump: failed to write start flat header");
zhanghailiang4c7e2512014-10-09 14:13:11 +08001485 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001486 }
1487
Janosch Frank86a518b2022-03-30 12:35:55 +00001488 write_dump_header(s, errp);
1489 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +08001490 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001491 }
1492
Janosch Frank86a518b2022-03-30 12:35:55 +00001493 write_dump_bitmap(s, errp);
1494 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +08001495 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001496 }
1497
Janosch Frank86a518b2022-03-30 12:35:55 +00001498 write_dump_pages(s, errp);
1499 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +08001500 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001501 }
1502
1503 ret = write_end_flat_header(s->fd);
1504 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001505 error_setg(errp, "dump: failed to write end flat header");
zhanghailiang4c7e2512014-10-09 14:13:11 +08001506 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001507 }
qiaonuohanb53ccc32014-02-18 14:11:36 +08001508}
1509
Janosch Frank0c2994a2022-08-11 12:10:57 +00001510static int validate_start_block(DumpState *s)
Wen Congyang783e9b42012-05-07 12:10:47 +08001511{
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001512 GuestPhysBlock *block;
Wen Congyang783e9b42012-05-07 12:10:47 +08001513
Janosch Frankdddf7252022-08-11 12:10:58 +00001514 if (!dump_has_filter(s)) {
Wen Congyang783e9b42012-05-07 12:10:47 +08001515 return 0;
1516 }
1517
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001518 QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
Janosch Frank0c2994a2022-08-11 12:10:57 +00001519 /* This block is out of the range */
Janosch Frankdddf7252022-08-11 12:10:58 +00001520 if (block->target_start >= s->filter_area_begin + s->filter_area_length ||
1521 block->target_end <= s->filter_area_begin) {
Wen Congyang783e9b42012-05-07 12:10:47 +08001522 continue;
1523 }
Janosch Frank0c2994a2022-08-11 12:10:57 +00001524 return 0;
1525 }
Wen Congyang783e9b42012-05-07 12:10:47 +08001526
1527 return -1;
1528}
1529
qiaonuohan7aad2482014-02-18 14:11:31 +08001530static void get_max_mapnr(DumpState *s)
1531{
1532 GuestPhysBlock *last_block;
1533
Paolo Bonzinieae3eb32018-12-06 13:10:34 +01001534 last_block = QTAILQ_LAST(&s->guest_phys_blocks.head);
Andrew Jones8161bef2016-01-11 20:56:20 +01001535 s->max_mapnr = dump_paddr_to_pfn(s, last_block->target_end);
qiaonuohan7aad2482014-02-18 14:11:31 +08001536}
1537
Peter Xubaf28f52016-02-18 13:16:48 +08001538static DumpState dump_state_global = { .status = DUMP_STATUS_NONE };
1539
1540static void dump_state_prepare(DumpState *s)
1541{
1542 /* zero the struct, setting status to active */
1543 *s = (DumpState) { .status = DUMP_STATUS_ACTIVE };
1544}
1545
Marc-André Lureau544803c2022-03-23 19:57:31 +04001546bool qemu_system_dump_in_progress(void)
Peter Xu65d64f32016-02-18 13:16:49 +08001547{
1548 DumpState *state = &dump_state_global;
Stefan Hajnoczid73415a2020-09-23 11:56:46 +01001549 return (qatomic_read(&state->status) == DUMP_STATUS_ACTIVE);
Peter Xu65d64f32016-02-18 13:16:49 +08001550}
1551
Janosch Frankc370d532022-08-11 12:10:59 +00001552/*
1553 * calculate total size of memory to be dumped (taking filter into
1554 * account.)
1555 */
Peter Xu2264c2c2016-02-18 13:16:53 +08001556static int64_t dump_calculate_size(DumpState *s)
1557{
1558 GuestPhysBlock *block;
Janosch Frankc370d532022-08-11 12:10:59 +00001559 int64_t total = 0;
Peter Xu2264c2c2016-02-18 13:16:53 +08001560
1561 QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
Janosch Frankc370d532022-08-11 12:10:59 +00001562 total += dump_filtered_memblock_size(block,
1563 s->filter_area_begin,
1564 s->filter_area_length);
Peter Xu2264c2c2016-02-18 13:16:53 +08001565 }
1566
1567 return total;
1568}
1569
Marc-André Lureaud9feb512017-09-11 18:59:26 +02001570static void vmcoreinfo_update_phys_base(DumpState *s)
1571{
1572 uint64_t size, note_head_size, name_size, phys_base;
1573 char **lines;
1574 uint8_t *vmci;
1575 size_t i;
1576
1577 if (!note_name_equal(s, s->guest_note, "VMCOREINFO")) {
1578 return;
1579 }
1580
1581 get_note_sizes(s, s->guest_note, &note_head_size, &name_size, &size);
1582 note_head_size = ROUND_UP(note_head_size, 4);
1583
1584 vmci = s->guest_note + note_head_size + ROUND_UP(name_size, 4);
1585 *(vmci + size) = '\0';
1586
1587 lines = g_strsplit((char *)vmci, "\n", -1);
1588 for (i = 0; lines[i]; i++) {
Wei Huang68cbecf2018-03-09 12:03:23 -05001589 const char *prefix = NULL;
1590
1591 if (s->dump_info.d_machine == EM_X86_64) {
1592 prefix = "NUMBER(phys_base)=";
1593 } else if (s->dump_info.d_machine == EM_AARCH64) {
1594 prefix = "NUMBER(PHYS_OFFSET)=";
1595 }
1596
1597 if (prefix && g_str_has_prefix(lines[i], prefix)) {
1598 if (qemu_strtou64(lines[i] + strlen(prefix), NULL, 16,
Marc-André Lureaud9feb512017-09-11 18:59:26 +02001599 &phys_base) < 0) {
Wei Huang68cbecf2018-03-09 12:03:23 -05001600 warn_report("Failed to read %s", prefix);
Marc-André Lureaud9feb512017-09-11 18:59:26 +02001601 } else {
1602 s->dump_info.phys_base = phys_base;
1603 }
1604 break;
1605 }
1606 }
1607
1608 g_strfreev(lines);
1609}
1610
zhanghailiang4c7e2512014-10-09 14:13:11 +08001611static void dump_init(DumpState *s, int fd, bool has_format,
1612 DumpGuestMemoryFormat format, bool paging, bool has_filter,
1613 int64_t begin, int64_t length, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +08001614{
Janosch Frank86a518b2022-03-30 12:35:55 +00001615 ERRP_GUARD();
Marc-André Lureau903ef732017-09-11 18:59:25 +02001616 VMCoreInfoState *vmci = vmcoreinfo_find();
Andreas Färber182735e2013-05-29 22:29:20 +02001617 CPUState *cpu;
Wen Congyang783e9b42012-05-07 12:10:47 +08001618 int nr_cpus;
1619 int ret;
1620
Peter Xuca1fc8c2016-02-18 13:16:50 +08001621 s->has_format = has_format;
1622 s->format = format;
Peter Xu2264c2c2016-02-18 13:16:53 +08001623 s->written_size = 0;
Peter Xuca1fc8c2016-02-18 13:16:50 +08001624
qiaonuohanb53ccc32014-02-18 14:11:36 +08001625 /* kdump-compressed is conflict with paging and filter */
1626 if (has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
1627 assert(!paging && !has_filter);
1628 }
1629
Wen Congyang783e9b42012-05-07 12:10:47 +08001630 if (runstate_is_running()) {
1631 vm_stop(RUN_STATE_SAVE_VM);
1632 s->resume = true;
1633 } else {
1634 s->resume = false;
1635 }
1636
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001637 /* If we use KVM, we should synchronize the registers before we get dump
1638 * info or physmap info.
Wen Congyang783e9b42012-05-07 12:10:47 +08001639 */
Andreas Färber1b3509c2013-06-09 16:48:29 +02001640 cpu_synchronize_all_states();
Wen Congyang783e9b42012-05-07 12:10:47 +08001641 nr_cpus = 0;
Andreas Färberbdc44642013-06-24 23:50:24 +02001642 CPU_FOREACH(cpu) {
Wen Congyang783e9b42012-05-07 12:10:47 +08001643 nr_cpus++;
1644 }
1645
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001646 s->fd = fd;
Janosch Frankdddf7252022-08-11 12:10:58 +00001647 if (has_filter && !length) {
1648 error_setg(errp, QERR_INVALID_PARAMETER, "length");
1649 goto cleanup;
1650 }
1651 s->filter_area_begin = begin;
1652 s->filter_area_length = length;
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001653
Chen Gang29282072014-08-03 23:28:56 +08001654 memory_mapping_list_init(&s->list);
1655
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001656 guest_phys_blocks_init(&s->guest_phys_blocks);
Laszlo Ersekc5d7f602013-08-06 12:37:10 +02001657 guest_phys_blocks_append(&s->guest_phys_blocks);
Peter Xu2264c2c2016-02-18 13:16:53 +08001658 s->total_size = dump_calculate_size(s);
1659#ifdef DEBUG_DUMP_GUEST_MEMORY
1660 fprintf(stderr, "DUMP: total memory to dump: %lu\n", s->total_size);
1661#endif
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001662
Cornelia Huckd1e69942017-09-13 16:20:35 +02001663 /* it does not make sense to dump non-existent memory */
1664 if (!s->total_size) {
1665 error_setg(errp, "dump: no guest memory to dump");
1666 goto cleanup;
1667 }
1668
Janosch Frank0c2994a2022-08-11 12:10:57 +00001669 /* Is the filter filtering everything? */
1670 if (validate_start_block(s) == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001671 error_setg(errp, QERR_INVALID_PARAMETER, "begin");
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001672 goto cleanup;
1673 }
1674
1675 /* get dump info: endian, class and architecture.
1676 * If the target architecture is not supported, cpu_get_dump_info() will
1677 * return -1.
1678 */
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001679 ret = cpu_get_dump_info(&s->dump_info, &s->guest_phys_blocks);
Wen Congyang783e9b42012-05-07 12:10:47 +08001680 if (ret < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001681 error_setg(errp, QERR_UNSUPPORTED);
Wen Congyang783e9b42012-05-07 12:10:47 +08001682 goto cleanup;
1683 }
1684
Andrew Jones8161bef2016-01-11 20:56:20 +01001685 if (!s->dump_info.page_size) {
1686 s->dump_info.page_size = TARGET_PAGE_SIZE;
1687 }
1688
Paolo Bonzini4720bd02012-06-07 08:48:09 +02001689 s->note_size = cpu_get_note_size(s->dump_info.d_class,
1690 s->dump_info.d_machine, nr_cpus);
Aneesh Kumar K.Vbb6b6842013-10-01 21:49:32 +05301691 if (s->note_size < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001692 error_setg(errp, QERR_UNSUPPORTED);
Paolo Bonzini4720bd02012-06-07 08:48:09 +02001693 goto cleanup;
1694 }
1695
Marc-André Lureau903ef732017-09-11 18:59:25 +02001696 /*
Marc-André Lureaud9feb512017-09-11 18:59:26 +02001697 * The goal of this block is to (a) update the previously guessed
1698 * phys_base, (b) copy the guest note out of the guest.
1699 * Failure to do so is not fatal for dumping.
Marc-André Lureau903ef732017-09-11 18:59:25 +02001700 */
1701 if (vmci) {
1702 uint64_t addr, note_head_size, name_size, desc_size;
1703 uint32_t size;
1704 uint16_t format;
1705
Janosch Frank05bbaa502022-03-30 12:36:00 +00001706 note_head_size = dump_is_64bit(s) ?
1707 sizeof(Elf64_Nhdr) : sizeof(Elf32_Nhdr);
Marc-André Lureau903ef732017-09-11 18:59:25 +02001708
1709 format = le16_to_cpu(vmci->vmcoreinfo.guest_format);
1710 size = le32_to_cpu(vmci->vmcoreinfo.size);
1711 addr = le64_to_cpu(vmci->vmcoreinfo.paddr);
1712 if (!vmci->has_vmcoreinfo) {
1713 warn_report("guest note is not present");
1714 } else if (size < note_head_size || size > MAX_GUEST_NOTE_SIZE) {
1715 warn_report("guest note size is invalid: %" PRIu32, size);
Marc-André Lureau5be5df72018-08-17 17:59:10 +02001716 } else if (format != FW_CFG_VMCOREINFO_FORMAT_ELF) {
Marc-André Lureau903ef732017-09-11 18:59:25 +02001717 warn_report("guest note format is unsupported: %" PRIu16, format);
1718 } else {
1719 s->guest_note = g_malloc(size + 1); /* +1 for adding \0 */
1720 cpu_physical_memory_read(addr, s->guest_note, size);
1721
1722 get_note_sizes(s, s->guest_note, NULL, &name_size, &desc_size);
1723 s->guest_note_size = ELF_NOTE_SIZE(note_head_size, name_size,
1724 desc_size);
1725 if (name_size > MAX_GUEST_NOTE_SIZE ||
1726 desc_size > MAX_GUEST_NOTE_SIZE ||
1727 s->guest_note_size > size) {
1728 warn_report("Invalid guest note header");
1729 g_free(s->guest_note);
1730 s->guest_note = NULL;
1731 } else {
Marc-André Lureaud9feb512017-09-11 18:59:26 +02001732 vmcoreinfo_update_phys_base(s);
Marc-André Lureau903ef732017-09-11 18:59:25 +02001733 s->note_size += s->guest_note_size;
1734 }
1735 }
1736 }
1737
Wen Congyang783e9b42012-05-07 12:10:47 +08001738 /* get memory mapping */
Wen Congyang783e9b42012-05-07 12:10:47 +08001739 if (paging) {
Janosch Frank86a518b2022-03-30 12:35:55 +00001740 qemu_get_guest_memory_mapping(&s->list, &s->guest_phys_blocks, errp);
1741 if (*errp) {
Andreas Färber11ed09c2013-05-29 21:54:03 +02001742 goto cleanup;
1743 }
Wen Congyang783e9b42012-05-07 12:10:47 +08001744 } else {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001745 qemu_get_guest_simple_memory_mapping(&s->list, &s->guest_phys_blocks);
Wen Congyang783e9b42012-05-07 12:10:47 +08001746 }
1747
qiaonuohan7aad2482014-02-18 14:11:31 +08001748 s->nr_cpus = nr_cpus;
qiaonuohan7aad2482014-02-18 14:11:31 +08001749
1750 get_max_mapnr(s);
1751
1752 uint64_t tmp;
Andrew Jones8161bef2016-01-11 20:56:20 +01001753 tmp = DIV_ROUND_UP(DIV_ROUND_UP(s->max_mapnr, CHAR_BIT),
1754 s->dump_info.page_size);
1755 s->len_dump_bitmap = tmp * s->dump_info.page_size;
qiaonuohan7aad2482014-02-18 14:11:31 +08001756
qiaonuohanb53ccc32014-02-18 14:11:36 +08001757 /* init for kdump-compressed format */
1758 if (has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
1759 switch (format) {
1760 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB:
1761 s->flag_compress = DUMP_DH_COMPRESSED_ZLIB;
1762 break;
1763
1764 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO:
Laszlo Ersekc998acb2014-05-20 13:42:22 +02001765#ifdef CONFIG_LZO
1766 if (lzo_init() != LZO_E_OK) {
1767 error_setg(errp, "failed to initialize the LZO library");
1768 goto cleanup;
1769 }
1770#endif
qiaonuohanb53ccc32014-02-18 14:11:36 +08001771 s->flag_compress = DUMP_DH_COMPRESSED_LZO;
1772 break;
1773
1774 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY:
1775 s->flag_compress = DUMP_DH_COMPRESSED_SNAPPY;
1776 break;
1777
1778 default:
1779 s->flag_compress = 0;
1780 }
1781
zhanghailiang4c7e2512014-10-09 14:13:11 +08001782 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001783 }
1784
Janosch Frankdddf7252022-08-11 12:10:58 +00001785 if (dump_has_filter(s)) {
1786 memory_mapping_filter(&s->list, s->filter_area_begin, s->filter_area_length);
Wen Congyang783e9b42012-05-07 12:10:47 +08001787 }
1788
1789 /*
1790 * calculate phdr_num
1791 *
1792 * the type of ehdr->e_phnum is uint16_t, so we should avoid overflow
1793 */
1794 s->phdr_num = 1; /* PT_NOTE */
1795 if (s->list.num < UINT16_MAX - 2) {
Janosch Frank862a3952022-03-30 12:35:57 +00001796 s->shdr_num = 0;
Wen Congyang783e9b42012-05-07 12:10:47 +08001797 s->phdr_num += s->list.num;
Wen Congyang783e9b42012-05-07 12:10:47 +08001798 } else {
Janosch Frank046bc412022-04-07 09:48:24 +00001799 /* sh_info of section 0 holds the real number of phdrs */
Janosch Frank862a3952022-03-30 12:35:57 +00001800 s->shdr_num = 1;
Wen Congyang783e9b42012-05-07 12:10:47 +08001801
1802 /* the type of shdr->sh_info is uint32_t, so we should avoid overflow */
1803 if (s->list.num <= UINT32_MAX - 1) {
Janosch Frank046bc412022-04-07 09:48:24 +00001804 s->phdr_num += s->list.num;
Wen Congyang783e9b42012-05-07 12:10:47 +08001805 } else {
Janosch Frank046bc412022-04-07 09:48:24 +00001806 s->phdr_num = UINT32_MAX;
Wen Congyang783e9b42012-05-07 12:10:47 +08001807 }
1808 }
1809
Janosch Frank05bbaa502022-03-30 12:36:00 +00001810 if (dump_is_64bit(s)) {
Janosch Franke71d3532022-03-30 12:35:59 +00001811 s->phdr_offset = sizeof(Elf64_Ehdr);
1812 s->shdr_offset = s->phdr_offset + sizeof(Elf64_Phdr) * s->phdr_num;
1813 s->note_offset = s->shdr_offset + sizeof(Elf64_Shdr) * s->shdr_num;
1814 s->memory_offset = s->note_offset + s->note_size;
Wen Congyang783e9b42012-05-07 12:10:47 +08001815 } else {
Janosch Franke71d3532022-03-30 12:35:59 +00001816
1817 s->phdr_offset = sizeof(Elf32_Ehdr);
1818 s->shdr_offset = s->phdr_offset + sizeof(Elf32_Phdr) * s->phdr_num;
1819 s->note_offset = s->shdr_offset + sizeof(Elf32_Shdr) * s->shdr_num;
1820 s->memory_offset = s->note_offset + s->note_size;
Wen Congyang783e9b42012-05-07 12:10:47 +08001821 }
1822
zhanghailiang4c7e2512014-10-09 14:13:11 +08001823 return;
Wen Congyang783e9b42012-05-07 12:10:47 +08001824
1825cleanup:
Chen Gang29282072014-08-03 23:28:56 +08001826 dump_cleanup(s);
Wen Congyang783e9b42012-05-07 12:10:47 +08001827}
1828
Peter Xuca1fc8c2016-02-18 13:16:50 +08001829/* this operation might be time consuming. */
1830static void dump_process(DumpState *s, Error **errp)
1831{
Janosch Frank86a518b2022-03-30 12:35:55 +00001832 ERRP_GUARD();
Peter Xud42a0d12016-02-18 13:16:56 +08001833 DumpQueryResult *result = NULL;
Peter Xuca1fc8c2016-02-18 13:16:50 +08001834
Viktor Prutyanov2da91b52018-05-17 19:23:39 +03001835 if (s->has_format && s->format == DUMP_GUEST_MEMORY_FORMAT_WIN_DMP) {
1836#ifdef TARGET_X86_64
Janosch Frank86a518b2022-03-30 12:35:55 +00001837 create_win_dump(s, errp);
Viktor Prutyanov2da91b52018-05-17 19:23:39 +03001838#endif
1839 } else if (s->has_format && s->format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
Janosch Frank86a518b2022-03-30 12:35:55 +00001840 create_kdump_vmcore(s, errp);
Peter Xuca1fc8c2016-02-18 13:16:50 +08001841 } else {
Janosch Frank86a518b2022-03-30 12:35:55 +00001842 create_vmcore(s, errp);
Peter Xuca1fc8c2016-02-18 13:16:50 +08001843 }
1844
Peter Xu39ba2ea2016-02-18 13:16:54 +08001845 /* make sure status is written after written_size updates */
1846 smp_wmb();
Stefan Hajnoczid73415a2020-09-23 11:56:46 +01001847 qatomic_set(&s->status,
Janosch Frank86a518b2022-03-30 12:35:55 +00001848 (*errp ? DUMP_STATUS_FAILED : DUMP_STATUS_COMPLETED));
Peter Xuca1fc8c2016-02-18 13:16:50 +08001849
Peter Xud42a0d12016-02-18 13:16:56 +08001850 /* send DUMP_COMPLETED message (unconditionally) */
1851 result = qmp_query_dump(NULL);
1852 /* should never fail */
1853 assert(result);
Janosch Frank86a518b2022-03-30 12:35:55 +00001854 qapi_event_send_dump_completed(result, !!*errp, (*errp ?
1855 error_get_pretty(*errp) : NULL));
Peter Xud42a0d12016-02-18 13:16:56 +08001856 qapi_free_DumpQueryResult(result);
1857
Peter Xuca1fc8c2016-02-18 13:16:50 +08001858 dump_cleanup(s);
1859}
1860
Peter Xu1fbeff72016-02-18 13:16:52 +08001861static void *dump_thread(void *data)
1862{
Peter Xu1fbeff72016-02-18 13:16:52 +08001863 DumpState *s = (DumpState *)data;
Alberto Garciac3a8fe32017-08-29 15:08:36 +03001864 dump_process(s, NULL);
Peter Xu1fbeff72016-02-18 13:16:52 +08001865 return NULL;
1866}
1867
Peter Xu39ba2ea2016-02-18 13:16:54 +08001868DumpQueryResult *qmp_query_dump(Error **errp)
1869{
1870 DumpQueryResult *result = g_new(DumpQueryResult, 1);
1871 DumpState *state = &dump_state_global;
Stefan Hajnoczid73415a2020-09-23 11:56:46 +01001872 result->status = qatomic_read(&state->status);
Peter Xu39ba2ea2016-02-18 13:16:54 +08001873 /* make sure we are reading status and written_size in order */
1874 smp_rmb();
1875 result->completed = state->written_size;
1876 result->total = state->total_size;
1877 return result;
1878}
1879
Peter Xu228de9c2016-02-18 13:16:47 +08001880void qmp_dump_guest_memory(bool paging, const char *file,
1881 bool has_detach, bool detach,
1882 bool has_begin, int64_t begin, bool has_length,
qiaonuohanb53ccc32014-02-18 14:11:36 +08001883 int64_t length, bool has_format,
1884 DumpGuestMemoryFormat format, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +08001885{
Janosch Frank86a518b2022-03-30 12:35:55 +00001886 ERRP_GUARD();
Wen Congyang783e9b42012-05-07 12:10:47 +08001887 const char *p;
1888 int fd = -1;
1889 DumpState *s;
Peter Xu1fbeff72016-02-18 13:16:52 +08001890 bool detach_p = false;
Wen Congyang783e9b42012-05-07 12:10:47 +08001891
Peter Xu63e27f22016-02-18 13:16:51 +08001892 if (runstate_check(RUN_STATE_INMIGRATE)) {
1893 error_setg(errp, "Dump not allowed during incoming migration.");
1894 return;
1895 }
1896
Peter Xu65d64f32016-02-18 13:16:49 +08001897 /* if there is a dump in background, we should wait until the dump
1898 * finished */
Marc-André Lureau544803c2022-03-23 19:57:31 +04001899 if (qemu_system_dump_in_progress()) {
Peter Xu65d64f32016-02-18 13:16:49 +08001900 error_setg(errp, "There is a dump in process, please wait.");
1901 return;
1902 }
1903
qiaonuohanb53ccc32014-02-18 14:11:36 +08001904 /*
1905 * kdump-compressed format need the whole memory dumped, so paging or
1906 * filter is not supported here.
1907 */
1908 if ((has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) &&
1909 (paging || has_begin || has_length)) {
1910 error_setg(errp, "kdump-compressed format doesn't support paging or "
1911 "filter");
1912 return;
1913 }
Wen Congyang783e9b42012-05-07 12:10:47 +08001914 if (has_begin && !has_length) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001915 error_setg(errp, QERR_MISSING_PARAMETER, "length");
Wen Congyang783e9b42012-05-07 12:10:47 +08001916 return;
1917 }
1918 if (!has_begin && has_length) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001919 error_setg(errp, QERR_MISSING_PARAMETER, "begin");
Wen Congyang783e9b42012-05-07 12:10:47 +08001920 return;
1921 }
Peter Xu1fbeff72016-02-18 13:16:52 +08001922 if (has_detach) {
1923 detach_p = detach;
1924 }
Wen Congyang783e9b42012-05-07 12:10:47 +08001925
qiaonuohanb53ccc32014-02-18 14:11:36 +08001926 /* check whether lzo/snappy is supported */
1927#ifndef CONFIG_LZO
1928 if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO) {
1929 error_setg(errp, "kdump-lzo is not available now");
1930 return;
1931 }
1932#endif
1933
1934#ifndef CONFIG_SNAPPY
1935 if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY) {
1936 error_setg(errp, "kdump-snappy is not available now");
1937 return;
1938 }
1939#endif
1940
Viktor Prutyanov2da91b52018-05-17 19:23:39 +03001941#ifndef TARGET_X86_64
1942 if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_WIN_DMP) {
1943 error_setg(errp, "Windows dump is only available for x86-64");
1944 return;
1945 }
1946#endif
1947
Wen Congyang783e9b42012-05-07 12:10:47 +08001948#if !defined(WIN32)
1949 if (strstart(file, "fd:", &p)) {
Kevin Wolf947e4742020-10-05 17:58:44 +02001950 fd = monitor_get_fd(monitor_cur(), p, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +08001951 if (fd == -1) {
Wen Congyang783e9b42012-05-07 12:10:47 +08001952 return;
1953 }
1954 }
1955#endif
1956
1957 if (strstart(file, "file:", &p)) {
Daniel P. Berrangé448058a2020-07-21 13:25:21 +01001958 fd = qemu_open_old(p, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, S_IRUSR);
Wen Congyang783e9b42012-05-07 12:10:47 +08001959 if (fd < 0) {
Luiz Capitulino75817662013-06-07 14:36:01 -04001960 error_setg_file_open(errp, errno, p);
Wen Congyang783e9b42012-05-07 12:10:47 +08001961 return;
1962 }
1963 }
1964
1965 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001966 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
Wen Congyang783e9b42012-05-07 12:10:47 +08001967 return;
1968 }
1969
Peter Xub7bc6b12021-09-22 12:20:09 -04001970 if (!dump_migration_blocker) {
1971 error_setg(&dump_migration_blocker,
1972 "Live migration disabled: dump-guest-memory in progress");
1973 }
1974
1975 /*
1976 * Allows even for -only-migratable, but forbid migration during the
1977 * process of dump guest memory.
1978 */
1979 if (migrate_add_blocker_internal(dump_migration_blocker, errp)) {
1980 /* Remember to release the fd before passing it over to dump state */
1981 close(fd);
1982 return;
1983 }
1984
Peter Xubaf28f52016-02-18 13:16:48 +08001985 s = &dump_state_global;
1986 dump_state_prepare(s);
Wen Congyang783e9b42012-05-07 12:10:47 +08001987
zhanghailiang4c7e2512014-10-09 14:13:11 +08001988 dump_init(s, fd, has_format, format, paging, has_begin,
Janosch Frank86a518b2022-03-30 12:35:55 +00001989 begin, length, errp);
1990 if (*errp) {
Stefan Hajnoczid73415a2020-09-23 11:56:46 +01001991 qatomic_set(&s->status, DUMP_STATUS_FAILED);
Wen Congyang783e9b42012-05-07 12:10:47 +08001992 return;
1993 }
1994
Peter Xu1fbeff72016-02-18 13:16:52 +08001995 if (detach_p) {
1996 /* detached dump */
Fam Zheng6796b402017-05-03 15:28:19 +08001997 s->detached = true;
Peter Xu1fbeff72016-02-18 13:16:52 +08001998 qemu_thread_create(&s->dump_thread, "dump_thread", dump_thread,
1999 s, QEMU_THREAD_DETACHED);
2000 } else {
2001 /* sync dump */
2002 dump_process(s, errp);
2003 }
Wen Congyang783e9b42012-05-07 12:10:47 +08002004}
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002005
2006DumpGuestMemoryCapability *qmp_query_dump_guest_memory_capability(Error **errp)
2007{
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002008 DumpGuestMemoryCapability *cap =
Markus Armbrusterb21e2382022-03-15 15:41:56 +01002009 g_new0(DumpGuestMemoryCapability, 1);
Eric Blake95b3a8c2021-01-13 16:10:13 -06002010 DumpGuestMemoryFormatList **tail = &cap->formats;
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002011
2012 /* elf is always available */
Eric Blake95b3a8c2021-01-13 16:10:13 -06002013 QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_ELF);
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002014
2015 /* kdump-zlib is always available */
Eric Blake95b3a8c2021-01-13 16:10:13 -06002016 QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB);
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002017
2018 /* add new item if kdump-lzo is available */
2019#ifdef CONFIG_LZO
Eric Blake95b3a8c2021-01-13 16:10:13 -06002020 QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO);
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002021#endif
2022
2023 /* add new item if kdump-snappy is available */
2024#ifdef CONFIG_SNAPPY
Eric Blake95b3a8c2021-01-13 16:10:13 -06002025 QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY);
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002026#endif
2027
Viktor Prutyanov2da91b52018-05-17 19:23:39 +03002028 /* Windows dump is available only if target is x86_64 */
2029#ifdef TARGET_X86_64
Eric Blake95b3a8c2021-01-13 16:10:13 -06002030 QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_WIN_DMP);
Viktor Prutyanov2da91b52018-05-17 19:23:39 +03002031#endif
2032
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002033 return cap;
2034}