blob: b168a25321555e249364e8dae4ea598050d8a780 [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
Janosch Franke41ed292022-10-17 08:38:13 +0000383static void prepare_elf_section_hdr_zero(DumpState *s)
Wen Congyang783e9b42012-05-07 12:10:47 +0800384{
Janosch Franke41ed292022-10-17 08:38:13 +0000385 if (dump_is_64bit(s)) {
386 Elf64_Shdr *shdr64 = s->elf_section_hdrs;
387
388 shdr64->sh_info = cpu_to_dump32(s, s->phdr_num);
389 } else {
390 Elf32_Shdr *shdr32 = s->elf_section_hdrs;
391
392 shdr32->sh_info = cpu_to_dump32(s, s->phdr_num);
393 }
394}
395
396static void prepare_elf_section_hdrs(DumpState *s)
397{
398 size_t len, sizeof_shdr;
399
400 /*
401 * Section ordering:
402 * - HDR zero
403 */
404 sizeof_shdr = dump_is_64bit(s) ? sizeof(Elf64_Shdr) : sizeof(Elf32_Shdr);
405 len = sizeof_shdr * s->shdr_num;
406 s->elf_section_hdrs = g_malloc0(len);
407
408 /*
409 * The first section header is ALWAYS a special initial section
410 * header.
411 *
412 * The header should be 0 with one exception being that if
413 * phdr_num is PN_XNUM then the sh_info field contains the real
414 * number of segment entries.
415 *
416 * As we zero allocate the buffer we will only need to modify
417 * sh_info for the PN_XNUM case.
418 */
419 if (s->phdr_num >= PN_XNUM) {
420 prepare_elf_section_hdr_zero(s);
421 }
422}
423
424static void write_elf_section_headers(DumpState *s, Error **errp)
425{
426 size_t sizeof_shdr = dump_is_64bit(s) ? sizeof(Elf64_Shdr) : sizeof(Elf32_Shdr);
Wen Congyang783e9b42012-05-07 12:10:47 +0800427 int ret;
428
Janosch Franke41ed292022-10-17 08:38:13 +0000429 prepare_elf_section_hdrs(s);
430
431 ret = fd_write_vmcore(s->elf_section_hdrs, s->shdr_num * sizeof_shdr, s);
432 if (ret < 0) {
433 error_setg_errno(errp, -ret, "dump: failed to write section headers");
Wen Congyang783e9b42012-05-07 12:10:47 +0800434 }
435
Janosch Franke41ed292022-10-17 08:38:13 +0000436 g_free(s->elf_section_hdrs);
Wen Congyang783e9b42012-05-07 12:10:47 +0800437}
438
zhanghailiang4c7e2512014-10-09 14:13:11 +0800439static void write_data(DumpState *s, void *buf, int length, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800440{
441 int ret;
442
443 ret = fd_write_vmcore(buf, length, s);
444 if (ret < 0) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200445 error_setg_errno(errp, -ret, "dump: failed to save memory");
Peter Xu2264c2c2016-02-18 13:16:53 +0800446 } else {
447 s->written_size += length;
Wen Congyang783e9b42012-05-07 12:10:47 +0800448 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800449}
450
zhanghailiang4c7e2512014-10-09 14:13:11 +0800451/* write the memory to vmcore. 1 page per I/O. */
452static void write_memory(DumpState *s, GuestPhysBlock *block, ram_addr_t start,
453 int64_t size, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800454{
Janosch Frank86a518b2022-03-30 12:35:55 +0000455 ERRP_GUARD();
Wen Congyang783e9b42012-05-07 12:10:47 +0800456 int64_t i;
Wen Congyang783e9b42012-05-07 12:10:47 +0800457
Andrew Jones8161bef2016-01-11 20:56:20 +0100458 for (i = 0; i < size / s->dump_info.page_size; i++) {
459 write_data(s, block->host_addr + start + i * s->dump_info.page_size,
Janosch Frank86a518b2022-03-30 12:35:55 +0000460 s->dump_info.page_size, errp);
461 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800462 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800463 }
464 }
465
Andrew Jones8161bef2016-01-11 20:56:20 +0100466 if ((size % s->dump_info.page_size) != 0) {
467 write_data(s, block->host_addr + start + i * s->dump_info.page_size,
Janosch Frank86a518b2022-03-30 12:35:55 +0000468 size % s->dump_info.page_size, errp);
469 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800470 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800471 }
472 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800473}
474
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200475/* get the memory's offset and size in the vmcore */
476static void get_offset_range(hwaddr phys_addr,
477 ram_addr_t mapping_length,
478 DumpState *s,
479 hwaddr *p_offset,
480 hwaddr *p_filesz)
Wen Congyang783e9b42012-05-07 12:10:47 +0800481{
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200482 GuestPhysBlock *block;
Avi Kivitya8170e52012-10-23 12:30:10 +0200483 hwaddr offset = s->memory_offset;
Wen Congyang783e9b42012-05-07 12:10:47 +0800484 int64_t size_in_block, start;
485
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200486 /* When the memory is not stored into vmcore, offset will be -1 */
487 *p_offset = -1;
488 *p_filesz = 0;
489
Janosch Frankdddf7252022-08-11 12:10:58 +0000490 if (dump_has_filter(s)) {
491 if (phys_addr < s->filter_area_begin ||
492 phys_addr >= s->filter_area_begin + s->filter_area_length) {
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200493 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800494 }
495 }
496
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200497 QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
Janosch Frankdddf7252022-08-11 12:10:58 +0000498 if (dump_has_filter(s)) {
499 if (block->target_start >= s->filter_area_begin + s->filter_area_length ||
500 block->target_end <= s->filter_area_begin) {
Wen Congyang783e9b42012-05-07 12:10:47 +0800501 /* This block is out of the range */
502 continue;
503 }
504
Janosch Frankdddf7252022-08-11 12:10:58 +0000505 if (s->filter_area_begin <= block->target_start) {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200506 start = block->target_start;
Wen Congyang783e9b42012-05-07 12:10:47 +0800507 } else {
Janosch Frankdddf7252022-08-11 12:10:58 +0000508 start = s->filter_area_begin;
Wen Congyang783e9b42012-05-07 12:10:47 +0800509 }
510
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200511 size_in_block = block->target_end - start;
Janosch Frankdddf7252022-08-11 12:10:58 +0000512 if (s->filter_area_begin + s->filter_area_length < block->target_end) {
513 size_in_block -= block->target_end - (s->filter_area_begin + s->filter_area_length);
Wen Congyang783e9b42012-05-07 12:10:47 +0800514 }
515 } else {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200516 start = block->target_start;
517 size_in_block = block->target_end - block->target_start;
Wen Congyang783e9b42012-05-07 12:10:47 +0800518 }
519
520 if (phys_addr >= start && phys_addr < start + size_in_block) {
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200521 *p_offset = phys_addr - start + offset;
522
523 /* The offset range mapped from the vmcore file must not spill over
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200524 * the GuestPhysBlock, clamp it. The rest of the mapping will be
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200525 * zero-filled in memory at load time; see
526 * <http://refspecs.linuxbase.org/elf/gabi4+/ch5.pheader.html>.
527 */
528 *p_filesz = phys_addr + mapping_length <= start + size_in_block ?
529 mapping_length :
530 size_in_block - (phys_addr - start);
531 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800532 }
533
534 offset += size_in_block;
535 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800536}
537
Janosch Frankafae6052022-08-11 12:10:55 +0000538static void write_elf_phdr_loads(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800539{
Janosch Frank86a518b2022-03-30 12:35:55 +0000540 ERRP_GUARD();
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200541 hwaddr offset, filesz;
Wen Congyang783e9b42012-05-07 12:10:47 +0800542 MemoryMapping *memory_mapping;
543 uint32_t phdr_index = 1;
Wen Congyang783e9b42012-05-07 12:10:47 +0800544
545 QTAILQ_FOREACH(memory_mapping, &s->list.head, next) {
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200546 get_offset_range(memory_mapping->phys_addr,
547 memory_mapping->length,
548 s, &offset, &filesz);
Janosch Frank05bbaa502022-03-30 12:36:00 +0000549 if (dump_is_64bit(s)) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800550 write_elf64_load(s, memory_mapping, phdr_index++, offset,
Janosch Frank86a518b2022-03-30 12:35:55 +0000551 filesz, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800552 } else {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800553 write_elf32_load(s, memory_mapping, phdr_index++, offset,
Janosch Frank86a518b2022-03-30 12:35:55 +0000554 filesz, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800555 }
556
Janosch Frank86a518b2022-03-30 12:35:55 +0000557 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800558 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800559 }
560
Janosch Frank046bc412022-04-07 09:48:24 +0000561 if (phdr_index >= s->phdr_num) {
Wen Congyang783e9b42012-05-07 12:10:47 +0800562 break;
563 }
564 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800565}
566
Janosch Frankc6812472022-03-30 12:36:03 +0000567static void write_elf_notes(DumpState *s, Error **errp)
568{
569 if (dump_is_64bit(s)) {
570 write_elf64_notes(fd_write_vmcore, s, errp);
571 } else {
572 write_elf32_notes(fd_write_vmcore, s, errp);
573 }
574}
575
Wen Congyang783e9b42012-05-07 12:10:47 +0800576/* write elf header, PT_NOTE and elf note to vmcore. */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800577static void dump_begin(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800578{
Janosch Frank86a518b2022-03-30 12:35:55 +0000579 ERRP_GUARD();
Wen Congyang783e9b42012-05-07 12:10:47 +0800580
581 /*
582 * the vmcore's format is:
583 * --------------
584 * | elf header |
585 * --------------
Janosch Frankcb415fd2022-10-17 08:38:14 +0000586 * | sctn_hdr |
587 * --------------
Wen Congyang783e9b42012-05-07 12:10:47 +0800588 * | PT_NOTE |
589 * --------------
590 * | PT_LOAD |
591 * --------------
592 * | ...... |
593 * --------------
594 * | PT_LOAD |
595 * --------------
Wen Congyang783e9b42012-05-07 12:10:47 +0800596 * | elf note |
597 * --------------
598 * | memory |
599 * --------------
600 *
601 * we only know where the memory is saved after we write elf note into
602 * vmcore.
603 */
604
605 /* write elf header to vmcore */
Janosch Frank670e7692022-08-11 12:11:00 +0000606 write_elf_header(s, errp);
Janosch Frank86a518b2022-03-30 12:35:55 +0000607 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800608 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800609 }
610
Janosch Frankcb415fd2022-10-17 08:38:14 +0000611 /* write section headers to vmcore */
612 write_elf_section_headers(s, errp);
613 if (*errp) {
614 return;
615 }
616
Janosch Frankbc7d5582022-03-30 12:36:01 +0000617 /* write PT_NOTE to vmcore */
618 write_elf_phdr_note(s, errp);
619 if (*errp) {
620 return;
621 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800622
Janosch Frankafae6052022-08-11 12:10:55 +0000623 /* write all PT_LOADs to vmcore */
624 write_elf_phdr_loads(s, errp);
Janosch Frank5ff2e5a2022-03-30 12:36:02 +0000625 if (*errp) {
626 return;
627 }
628
Janosch Frankc6812472022-03-30 12:36:03 +0000629 /* write notes to vmcore */
630 write_elf_notes(s, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800631}
632
Janosch Frank1e811302022-08-11 12:10:56 +0000633static int64_t dump_filtered_memblock_size(GuestPhysBlock *block,
634 int64_t filter_area_start,
635 int64_t filter_area_length)
Wen Congyang783e9b42012-05-07 12:10:47 +0800636{
Janosch Frank1e811302022-08-11 12:10:56 +0000637 int64_t size, left, right;
Wen Congyang783e9b42012-05-07 12:10:47 +0800638
Janosch Frank1e811302022-08-11 12:10:56 +0000639 /* No filter, return full size */
640 if (!filter_area_length) {
641 return block->target_end - block->target_start;
Wen Congyang783e9b42012-05-07 12:10:47 +0800642 }
Janosch Frank1e811302022-08-11 12:10:56 +0000643
644 /* calculate the overlapped region. */
645 left = MAX(filter_area_start, block->target_start);
646 right = MIN(filter_area_start + filter_area_length, block->target_end);
647 size = right - left;
648 size = size > 0 ? size : 0;
649
650 return size;
651}
652
653static int64_t dump_filtered_memblock_start(GuestPhysBlock *block,
654 int64_t filter_area_start,
655 int64_t filter_area_length)
656{
657 if (filter_area_length) {
658 /* return -1 if the block is not within filter area */
659 if (block->target_start >= filter_area_start + filter_area_length ||
660 block->target_end <= filter_area_start) {
661 return -1;
662 }
663
664 if (filter_area_start > block->target_start) {
665 return filter_area_start - block->target_start;
666 }
667 }
668
669 return 0;
Wen Congyang783e9b42012-05-07 12:10:47 +0800670}
671
672/* write all memory to vmcore */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800673static void dump_iterate(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800674{
Janosch Frank86a518b2022-03-30 12:35:55 +0000675 ERRP_GUARD();
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200676 GuestPhysBlock *block;
Janosch Frank1e811302022-08-11 12:10:56 +0000677 int64_t memblock_size, memblock_start;
Wen Congyang783e9b42012-05-07 12:10:47 +0800678
Janosch Frank1e811302022-08-11 12:10:56 +0000679 QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
Janosch Frankdddf7252022-08-11 12:10:58 +0000680 memblock_start = dump_filtered_memblock_start(block, s->filter_area_begin, s->filter_area_length);
Janosch Frank1e811302022-08-11 12:10:56 +0000681 if (memblock_start == -1) {
682 continue;
Wen Congyang783e9b42012-05-07 12:10:47 +0800683 }
Janosch Frank1e811302022-08-11 12:10:56 +0000684
Janosch Frankdddf7252022-08-11 12:10:58 +0000685 memblock_size = dump_filtered_memblock_size(block, s->filter_area_begin, s->filter_area_length);
Janosch Frank1e811302022-08-11 12:10:56 +0000686
687 /* Write the memory to file */
688 write_memory(s, block, memblock_start, memblock_size, errp);
Janosch Frank86a518b2022-03-30 12:35:55 +0000689 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800690 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800691 }
Janosch Frank1e811302022-08-11 12:10:56 +0000692 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800693}
694
zhanghailiang4c7e2512014-10-09 14:13:11 +0800695static void create_vmcore(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800696{
Janosch Frank86a518b2022-03-30 12:35:55 +0000697 ERRP_GUARD();
Wen Congyang783e9b42012-05-07 12:10:47 +0800698
Janosch Frank86a518b2022-03-30 12:35:55 +0000699 dump_begin(s, errp);
700 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800701 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800702 }
703
zhanghailiang4c7e2512014-10-09 14:13:11 +0800704 dump_iterate(s, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800705}
706
qiaonuohanfda05382014-02-18 14:11:27 +0800707static int write_start_flat_header(int fd)
708{
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200709 MakedumpfileHeader *mh;
qiaonuohanfda05382014-02-18 14:11:27 +0800710 int ret = 0;
711
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200712 QEMU_BUILD_BUG_ON(sizeof *mh > MAX_SIZE_MDF_HEADER);
713 mh = g_malloc0(MAX_SIZE_MDF_HEADER);
qiaonuohanfda05382014-02-18 14:11:27 +0800714
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200715 memcpy(mh->signature, MAKEDUMPFILE_SIGNATURE,
716 MIN(sizeof mh->signature, sizeof MAKEDUMPFILE_SIGNATURE));
qiaonuohanfda05382014-02-18 14:11:27 +0800717
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200718 mh->type = cpu_to_be64(TYPE_FLAT_HEADER);
719 mh->version = cpu_to_be64(VERSION_FLAT_HEADER);
qiaonuohanfda05382014-02-18 14:11:27 +0800720
721 size_t written_size;
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200722 written_size = qemu_write_full(fd, mh, MAX_SIZE_MDF_HEADER);
qiaonuohanfda05382014-02-18 14:11:27 +0800723 if (written_size != MAX_SIZE_MDF_HEADER) {
724 ret = -1;
725 }
726
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200727 g_free(mh);
qiaonuohanfda05382014-02-18 14:11:27 +0800728 return ret;
729}
730
731static int write_end_flat_header(int fd)
732{
733 MakedumpfileDataHeader mdh;
734
735 mdh.offset = END_FLAG_FLAT_HEADER;
736 mdh.buf_size = END_FLAG_FLAT_HEADER;
737
738 size_t written_size;
739 written_size = qemu_write_full(fd, &mdh, sizeof(mdh));
740 if (written_size != sizeof(mdh)) {
741 return -1;
742 }
743
744 return 0;
745}
746
qiaonuohan5d31bab2014-02-18 14:11:28 +0800747static int write_buffer(int fd, off_t offset, const void *buf, size_t size)
748{
749 size_t written_size;
750 MakedumpfileDataHeader mdh;
751
752 mdh.offset = cpu_to_be64(offset);
753 mdh.buf_size = cpu_to_be64(size);
754
755 written_size = qemu_write_full(fd, &mdh, sizeof(mdh));
756 if (written_size != sizeof(mdh)) {
757 return -1;
758 }
759
760 written_size = qemu_write_full(fd, buf, size);
761 if (written_size != size) {
762 return -1;
763 }
764
765 return 0;
766}
767
qiaonuohan4835ef72014-02-18 14:11:29 +0800768static int buf_write_note(const void *buf, size_t size, void *opaque)
769{
770 DumpState *s = opaque;
771
772 /* note_buf is not enough */
773 if (s->note_buf_offset + size > s->note_size) {
774 return -1;
775 }
776
777 memcpy(s->note_buf + s->note_buf_offset, buf, size);
778
779 s->note_buf_offset += size;
780
781 return 0;
782}
783
Marc-André Lureau903ef732017-09-11 18:59:25 +0200784/*
785 * This function retrieves various sizes from an elf header.
786 *
787 * @note has to be a valid ELF note. The return sizes are unmodified
788 * (not padded or rounded up to be multiple of 4).
789 */
790static void get_note_sizes(DumpState *s, const void *note,
791 uint64_t *note_head_size,
792 uint64_t *name_size,
793 uint64_t *desc_size)
794{
795 uint64_t note_head_sz;
796 uint64_t name_sz;
797 uint64_t desc_sz;
798
Janosch Frank05bbaa502022-03-30 12:36:00 +0000799 if (dump_is_64bit(s)) {
Marc-André Lureau903ef732017-09-11 18:59:25 +0200800 const Elf64_Nhdr *hdr = note;
801 note_head_sz = sizeof(Elf64_Nhdr);
802 name_sz = tswap64(hdr->n_namesz);
803 desc_sz = tswap64(hdr->n_descsz);
804 } else {
805 const Elf32_Nhdr *hdr = note;
806 note_head_sz = sizeof(Elf32_Nhdr);
807 name_sz = tswap32(hdr->n_namesz);
808 desc_sz = tswap32(hdr->n_descsz);
809 }
810
811 if (note_head_size) {
812 *note_head_size = note_head_sz;
813 }
814 if (name_size) {
815 *name_size = name_sz;
816 }
817 if (desc_size) {
818 *desc_size = desc_sz;
819 }
820}
821
Marc-André Lureaud9feb512017-09-11 18:59:26 +0200822static bool note_name_equal(DumpState *s,
823 const uint8_t *note, const char *name)
824{
825 int len = strlen(name) + 1;
826 uint64_t head_size, name_size;
827
828 get_note_sizes(s, note, &head_size, &name_size, NULL);
829 head_size = ROUND_UP(head_size, 4);
830
Marc-André Lureauc983ca82017-12-12 15:53:59 +0100831 return name_size == len && memcmp(note + head_size, name, len) == 0;
Marc-André Lureaud9feb512017-09-11 18:59:26 +0200832}
833
qiaonuohan298f1162014-02-18 14:11:32 +0800834/* write common header, sub header and elf note to vmcore */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800835static void create_header32(DumpState *s, Error **errp)
qiaonuohan298f1162014-02-18 14:11:32 +0800836{
Janosch Frank86a518b2022-03-30 12:35:55 +0000837 ERRP_GUARD();
qiaonuohan298f1162014-02-18 14:11:32 +0800838 DiskDumpHeader32 *dh = NULL;
839 KdumpSubHeader32 *kh = NULL;
840 size_t size;
qiaonuohan298f1162014-02-18 14:11:32 +0800841 uint32_t block_size;
842 uint32_t sub_hdr_size;
843 uint32_t bitmap_blocks;
844 uint32_t status = 0;
845 uint64_t offset_note;
846
847 /* write common header, the version of kdump-compressed format is 6th */
848 size = sizeof(DiskDumpHeader32);
849 dh = g_malloc0(size);
850
Eric Blake84c868f2018-03-27 15:21:51 -0500851 memcpy(dh->signature, KDUMP_SIGNATURE, SIG_LEN);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200852 dh->header_version = cpu_to_dump32(s, 6);
Andrew Jones8161bef2016-01-11 20:56:20 +0100853 block_size = s->dump_info.page_size;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200854 dh->block_size = cpu_to_dump32(s, block_size);
qiaonuohan298f1162014-02-18 14:11:32 +0800855 sub_hdr_size = sizeof(struct KdumpSubHeader32) + s->note_size;
856 sub_hdr_size = DIV_ROUND_UP(sub_hdr_size, block_size);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200857 dh->sub_hdr_size = cpu_to_dump32(s, sub_hdr_size);
qiaonuohan298f1162014-02-18 14:11:32 +0800858 /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200859 dh->max_mapnr = cpu_to_dump32(s, MIN(s->max_mapnr, UINT_MAX));
860 dh->nr_cpus = cpu_to_dump32(s, s->nr_cpus);
qiaonuohan298f1162014-02-18 14:11:32 +0800861 bitmap_blocks = DIV_ROUND_UP(s->len_dump_bitmap, block_size) * 2;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200862 dh->bitmap_blocks = cpu_to_dump32(s, bitmap_blocks);
qiaonuohan4ab23a92014-02-18 14:11:37 +0800863 strncpy(dh->utsname.machine, ELF_MACHINE_UNAME, sizeof(dh->utsname.machine));
qiaonuohan298f1162014-02-18 14:11:32 +0800864
865 if (s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) {
866 status |= DUMP_DH_COMPRESSED_ZLIB;
867 }
868#ifdef CONFIG_LZO
869 if (s->flag_compress & DUMP_DH_COMPRESSED_LZO) {
870 status |= DUMP_DH_COMPRESSED_LZO;
871 }
872#endif
873#ifdef CONFIG_SNAPPY
874 if (s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) {
875 status |= DUMP_DH_COMPRESSED_SNAPPY;
876 }
877#endif
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200878 dh->status = cpu_to_dump32(s, status);
qiaonuohan298f1162014-02-18 14:11:32 +0800879
880 if (write_buffer(s->fd, 0, dh, size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800881 error_setg(errp, "dump: failed to write disk dump header");
qiaonuohan298f1162014-02-18 14:11:32 +0800882 goto out;
883 }
884
885 /* write sub header */
886 size = sizeof(KdumpSubHeader32);
887 kh = g_malloc0(size);
888
889 /* 64bit max_mapnr_64 */
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200890 kh->max_mapnr_64 = cpu_to_dump64(s, s->max_mapnr);
Andrew Jonesb6e05aa2016-01-11 20:56:21 +0100891 kh->phys_base = cpu_to_dump32(s, s->dump_info.phys_base);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200892 kh->dump_level = cpu_to_dump32(s, DUMP_LEVEL);
qiaonuohan298f1162014-02-18 14:11:32 +0800893
894 offset_note = DISKDUMP_HEADER_BLOCKS * block_size + size;
Marc-André Lureau9ada5752017-09-11 18:59:27 +0200895 if (s->guest_note &&
896 note_name_equal(s, s->guest_note, "VMCOREINFO")) {
897 uint64_t hsize, name_size, size_vmcoreinfo_desc, offset_vmcoreinfo;
898
899 get_note_sizes(s, s->guest_note,
900 &hsize, &name_size, &size_vmcoreinfo_desc);
901 offset_vmcoreinfo = offset_note + s->note_size - s->guest_note_size +
902 (DIV_ROUND_UP(hsize, 4) + DIV_ROUND_UP(name_size, 4)) * 4;
903 kh->offset_vmcoreinfo = cpu_to_dump64(s, offset_vmcoreinfo);
904 kh->size_vmcoreinfo = cpu_to_dump32(s, size_vmcoreinfo_desc);
905 }
906
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200907 kh->offset_note = cpu_to_dump64(s, offset_note);
908 kh->note_size = cpu_to_dump32(s, s->note_size);
qiaonuohan298f1162014-02-18 14:11:32 +0800909
910 if (write_buffer(s->fd, DISKDUMP_HEADER_BLOCKS *
911 block_size, kh, size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800912 error_setg(errp, "dump: failed to write kdump sub header");
qiaonuohan298f1162014-02-18 14:11:32 +0800913 goto out;
914 }
915
916 /* write note */
917 s->note_buf = g_malloc0(s->note_size);
918 s->note_buf_offset = 0;
919
920 /* use s->note_buf to store notes temporarily */
Janosch Frank86a518b2022-03-30 12:35:55 +0000921 write_elf32_notes(buf_write_note, s, errp);
922 if (*errp) {
qiaonuohan298f1162014-02-18 14:11:32 +0800923 goto out;
924 }
qiaonuohan298f1162014-02-18 14:11:32 +0800925 if (write_buffer(s->fd, offset_note, s->note_buf,
926 s->note_size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800927 error_setg(errp, "dump: failed to write notes");
qiaonuohan298f1162014-02-18 14:11:32 +0800928 goto out;
929 }
930
931 /* get offset of dump_bitmap */
932 s->offset_dump_bitmap = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size) *
933 block_size;
934
935 /* get offset of page */
936 s->offset_page = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size + bitmap_blocks) *
937 block_size;
938
939out:
940 g_free(dh);
941 g_free(kh);
942 g_free(s->note_buf);
qiaonuohan298f1162014-02-18 14:11:32 +0800943}
944
945/* write common header, sub header and elf note to vmcore */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800946static void create_header64(DumpState *s, Error **errp)
qiaonuohan298f1162014-02-18 14:11:32 +0800947{
Janosch Frank86a518b2022-03-30 12:35:55 +0000948 ERRP_GUARD();
qiaonuohan298f1162014-02-18 14:11:32 +0800949 DiskDumpHeader64 *dh = NULL;
950 KdumpSubHeader64 *kh = NULL;
951 size_t size;
qiaonuohan298f1162014-02-18 14:11:32 +0800952 uint32_t block_size;
953 uint32_t sub_hdr_size;
954 uint32_t bitmap_blocks;
955 uint32_t status = 0;
956 uint64_t offset_note;
957
958 /* write common header, the version of kdump-compressed format is 6th */
959 size = sizeof(DiskDumpHeader64);
960 dh = g_malloc0(size);
961
Eric Blake84c868f2018-03-27 15:21:51 -0500962 memcpy(dh->signature, KDUMP_SIGNATURE, SIG_LEN);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200963 dh->header_version = cpu_to_dump32(s, 6);
Andrew Jones8161bef2016-01-11 20:56:20 +0100964 block_size = s->dump_info.page_size;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200965 dh->block_size = cpu_to_dump32(s, block_size);
qiaonuohan298f1162014-02-18 14:11:32 +0800966 sub_hdr_size = sizeof(struct KdumpSubHeader64) + s->note_size;
967 sub_hdr_size = DIV_ROUND_UP(sub_hdr_size, block_size);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200968 dh->sub_hdr_size = cpu_to_dump32(s, sub_hdr_size);
qiaonuohan298f1162014-02-18 14:11:32 +0800969 /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200970 dh->max_mapnr = cpu_to_dump32(s, MIN(s->max_mapnr, UINT_MAX));
971 dh->nr_cpus = cpu_to_dump32(s, s->nr_cpus);
qiaonuohan298f1162014-02-18 14:11:32 +0800972 bitmap_blocks = DIV_ROUND_UP(s->len_dump_bitmap, block_size) * 2;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200973 dh->bitmap_blocks = cpu_to_dump32(s, bitmap_blocks);
qiaonuohan4ab23a92014-02-18 14:11:37 +0800974 strncpy(dh->utsname.machine, ELF_MACHINE_UNAME, sizeof(dh->utsname.machine));
qiaonuohan298f1162014-02-18 14:11:32 +0800975
976 if (s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) {
977 status |= DUMP_DH_COMPRESSED_ZLIB;
978 }
979#ifdef CONFIG_LZO
980 if (s->flag_compress & DUMP_DH_COMPRESSED_LZO) {
981 status |= DUMP_DH_COMPRESSED_LZO;
982 }
983#endif
984#ifdef CONFIG_SNAPPY
985 if (s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) {
986 status |= DUMP_DH_COMPRESSED_SNAPPY;
987 }
988#endif
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200989 dh->status = cpu_to_dump32(s, status);
qiaonuohan298f1162014-02-18 14:11:32 +0800990
991 if (write_buffer(s->fd, 0, dh, size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800992 error_setg(errp, "dump: failed to write disk dump header");
qiaonuohan298f1162014-02-18 14:11:32 +0800993 goto out;
994 }
995
996 /* write sub header */
997 size = sizeof(KdumpSubHeader64);
998 kh = g_malloc0(size);
999
1000 /* 64bit max_mapnr_64 */
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001001 kh->max_mapnr_64 = cpu_to_dump64(s, s->max_mapnr);
Andrew Jonesb6e05aa2016-01-11 20:56:21 +01001002 kh->phys_base = cpu_to_dump64(s, s->dump_info.phys_base);
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001003 kh->dump_level = cpu_to_dump32(s, DUMP_LEVEL);
qiaonuohan298f1162014-02-18 14:11:32 +08001004
1005 offset_note = DISKDUMP_HEADER_BLOCKS * block_size + size;
Marc-André Lureau9ada5752017-09-11 18:59:27 +02001006 if (s->guest_note &&
1007 note_name_equal(s, s->guest_note, "VMCOREINFO")) {
1008 uint64_t hsize, name_size, size_vmcoreinfo_desc, offset_vmcoreinfo;
1009
1010 get_note_sizes(s, s->guest_note,
1011 &hsize, &name_size, &size_vmcoreinfo_desc);
1012 offset_vmcoreinfo = offset_note + s->note_size - s->guest_note_size +
1013 (DIV_ROUND_UP(hsize, 4) + DIV_ROUND_UP(name_size, 4)) * 4;
1014 kh->offset_vmcoreinfo = cpu_to_dump64(s, offset_vmcoreinfo);
1015 kh->size_vmcoreinfo = cpu_to_dump64(s, size_vmcoreinfo_desc);
1016 }
1017
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001018 kh->offset_note = cpu_to_dump64(s, offset_note);
1019 kh->note_size = cpu_to_dump64(s, s->note_size);
qiaonuohan298f1162014-02-18 14:11:32 +08001020
1021 if (write_buffer(s->fd, DISKDUMP_HEADER_BLOCKS *
1022 block_size, kh, size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001023 error_setg(errp, "dump: failed to write kdump sub header");
qiaonuohan298f1162014-02-18 14:11:32 +08001024 goto out;
1025 }
1026
1027 /* write note */
1028 s->note_buf = g_malloc0(s->note_size);
1029 s->note_buf_offset = 0;
1030
1031 /* use s->note_buf to store notes temporarily */
Janosch Frank86a518b2022-03-30 12:35:55 +00001032 write_elf64_notes(buf_write_note, s, errp);
1033 if (*errp) {
qiaonuohan298f1162014-02-18 14:11:32 +08001034 goto out;
1035 }
1036
1037 if (write_buffer(s->fd, offset_note, s->note_buf,
1038 s->note_size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001039 error_setg(errp, "dump: failed to write notes");
qiaonuohan298f1162014-02-18 14:11:32 +08001040 goto out;
1041 }
1042
1043 /* get offset of dump_bitmap */
1044 s->offset_dump_bitmap = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size) *
1045 block_size;
1046
1047 /* get offset of page */
1048 s->offset_page = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size + bitmap_blocks) *
1049 block_size;
1050
1051out:
1052 g_free(dh);
1053 g_free(kh);
1054 g_free(s->note_buf);
qiaonuohan298f1162014-02-18 14:11:32 +08001055}
1056
zhanghailiang4c7e2512014-10-09 14:13:11 +08001057static void write_dump_header(DumpState *s, Error **errp)
qiaonuohan298f1162014-02-18 14:11:32 +08001058{
Janosch Frank05bbaa502022-03-30 12:36:00 +00001059 if (dump_is_64bit(s)) {
Markus Armbruster992861f2020-07-07 18:06:04 +02001060 create_header64(s, errp);
Janosch Frank05bbaa502022-03-30 12:36:00 +00001061 } else {
1062 create_header32(s, errp);
zhanghailiang4c7e2512014-10-09 14:13:11 +08001063 }
qiaonuohan298f1162014-02-18 14:11:32 +08001064}
1065
Andrew Jones8161bef2016-01-11 20:56:20 +01001066static size_t dump_bitmap_get_bufsize(DumpState *s)
1067{
1068 return s->dump_info.page_size;
1069}
1070
qiaonuohand0686c72014-02-18 14:11:33 +08001071/*
1072 * set dump_bitmap sequencely. the bit before last_pfn is not allowed to be
1073 * rewritten, so if need to set the first bit, set last_pfn and pfn to 0.
1074 * set_dump_bitmap will always leave the recently set bit un-sync. And setting
1075 * (last bit + sizeof(buf) * 8) to 0 will do flushing the content in buf into
1076 * vmcore, ie. synchronizing un-sync bit into vmcore.
1077 */
1078static int set_dump_bitmap(uint64_t last_pfn, uint64_t pfn, bool value,
1079 uint8_t *buf, DumpState *s)
1080{
1081 off_t old_offset, new_offset;
1082 off_t offset_bitmap1, offset_bitmap2;
1083 uint32_t byte, bit;
Andrew Jones8161bef2016-01-11 20:56:20 +01001084 size_t bitmap_bufsize = dump_bitmap_get_bufsize(s);
1085 size_t bits_per_buf = bitmap_bufsize * CHAR_BIT;
qiaonuohand0686c72014-02-18 14:11:33 +08001086
1087 /* should not set the previous place */
1088 assert(last_pfn <= pfn);
1089
1090 /*
1091 * if the bit needed to be set is not cached in buf, flush the data in buf
1092 * to vmcore firstly.
1093 * making new_offset be bigger than old_offset can also sync remained data
1094 * into vmcore.
1095 */
Andrew Jones8161bef2016-01-11 20:56:20 +01001096 old_offset = bitmap_bufsize * (last_pfn / bits_per_buf);
1097 new_offset = bitmap_bufsize * (pfn / bits_per_buf);
qiaonuohand0686c72014-02-18 14:11:33 +08001098
1099 while (old_offset < new_offset) {
1100 /* calculate the offset and write dump_bitmap */
1101 offset_bitmap1 = s->offset_dump_bitmap + old_offset;
1102 if (write_buffer(s->fd, offset_bitmap1, buf,
Andrew Jones8161bef2016-01-11 20:56:20 +01001103 bitmap_bufsize) < 0) {
qiaonuohand0686c72014-02-18 14:11:33 +08001104 return -1;
1105 }
1106
1107 /* dump level 1 is chosen, so 1st and 2nd bitmap are same */
1108 offset_bitmap2 = s->offset_dump_bitmap + s->len_dump_bitmap +
1109 old_offset;
1110 if (write_buffer(s->fd, offset_bitmap2, buf,
Andrew Jones8161bef2016-01-11 20:56:20 +01001111 bitmap_bufsize) < 0) {
qiaonuohand0686c72014-02-18 14:11:33 +08001112 return -1;
1113 }
1114
Andrew Jones8161bef2016-01-11 20:56:20 +01001115 memset(buf, 0, bitmap_bufsize);
1116 old_offset += bitmap_bufsize;
qiaonuohand0686c72014-02-18 14:11:33 +08001117 }
1118
1119 /* get the exact place of the bit in the buf, and set it */
Andrew Jones8161bef2016-01-11 20:56:20 +01001120 byte = (pfn % bits_per_buf) / CHAR_BIT;
1121 bit = (pfn % bits_per_buf) % CHAR_BIT;
qiaonuohand0686c72014-02-18 14:11:33 +08001122 if (value) {
1123 buf[byte] |= 1u << bit;
1124 } else {
1125 buf[byte] &= ~(1u << bit);
1126 }
1127
1128 return 0;
1129}
1130
Andrew Jones8161bef2016-01-11 20:56:20 +01001131static uint64_t dump_paddr_to_pfn(DumpState *s, uint64_t addr)
1132{
1133 int target_page_shift = ctz32(s->dump_info.page_size);
1134
1135 return (addr >> target_page_shift) - ARCH_PFN_OFFSET;
1136}
1137
1138static uint64_t dump_pfn_to_paddr(DumpState *s, uint64_t pfn)
1139{
1140 int target_page_shift = ctz32(s->dump_info.page_size);
1141
1142 return (pfn + ARCH_PFN_OFFSET) << target_page_shift;
1143}
1144
qiaonuohand0686c72014-02-18 14:11:33 +08001145/*
Marc-André Lureau94d78842022-09-05 16:06:21 +04001146 * Return the page frame number and the page content in *bufptr. bufptr can be
1147 * NULL. If not NULL, *bufptr must contains a target page size of pre-allocated
1148 * memory. This is not necessarily the memory returned.
qiaonuohand0686c72014-02-18 14:11:33 +08001149 */
1150static bool get_next_page(GuestPhysBlock **blockptr, uint64_t *pfnptr,
1151 uint8_t **bufptr, DumpState *s)
1152{
1153 GuestPhysBlock *block = *blockptr;
Marc-André Lureau94d78842022-09-05 16:06:21 +04001154 uint32_t page_size = s->dump_info.page_size;
1155 uint8_t *buf = NULL, *hbuf;
1156 hwaddr addr;
qiaonuohand0686c72014-02-18 14:11:33 +08001157
1158 /* block == NULL means the start of the iteration */
1159 if (!block) {
1160 block = QTAILQ_FIRST(&s->guest_phys_blocks.head);
1161 *blockptr = block;
Marc-André Lureau08df3432022-08-25 12:40:12 +04001162 addr = block->target_start;
Marc-André Lureau94d78842022-09-05 16:06:21 +04001163 *pfnptr = dump_paddr_to_pfn(s, addr);
Marc-André Lureau08df3432022-08-25 12:40:12 +04001164 } else {
Marc-André Lureau94d78842022-09-05 16:06:21 +04001165 *pfnptr += 1;
1166 addr = dump_pfn_to_paddr(s, *pfnptr);
qiaonuohand0686c72014-02-18 14:11:33 +08001167 }
Marc-André Lureau08df3432022-08-25 12:40:12 +04001168 assert(block != NULL);
qiaonuohand0686c72014-02-18 14:11:33 +08001169
Marc-André Lureau94d78842022-09-05 16:06:21 +04001170 while (1) {
1171 if (addr >= block->target_start && addr < block->target_end) {
1172 size_t n = MIN(block->target_end - addr, page_size - addr % page_size);
1173 hbuf = block->host_addr + (addr - block->target_start);
1174 if (!buf) {
1175 if (n == page_size) {
1176 /* this is a whole target page, go for it */
1177 assert(addr % page_size == 0);
1178 buf = hbuf;
1179 break;
1180 } else if (bufptr) {
1181 assert(*bufptr);
1182 buf = *bufptr;
1183 memset(buf, 0, page_size);
1184 } else {
1185 return true;
1186 }
1187 }
1188
1189 memcpy(buf + addr % page_size, hbuf, n);
1190 addr += n;
1191 if (addr % page_size == 0) {
1192 /* we filled up the page */
1193 break;
1194 }
1195 } else {
1196 /* the next page is in the next block */
1197 *blockptr = block = QTAILQ_NEXT(block, next);
1198 if (!block) {
1199 break;
1200 }
1201
1202 addr = block->target_start;
1203 /* are we still in the same page? */
1204 if (dump_paddr_to_pfn(s, addr) != *pfnptr) {
1205 if (buf) {
1206 /* no, but we already filled something earlier, return it */
1207 break;
1208 } else {
1209 /* else continue from there */
1210 *pfnptr = dump_paddr_to_pfn(s, addr);
1211 }
1212 }
qiaonuohand0686c72014-02-18 14:11:33 +08001213 }
qiaonuohand0686c72014-02-18 14:11:33 +08001214 }
1215
1216 if (bufptr) {
1217 *bufptr = buf;
1218 }
1219
Marc-André Lureau94d78842022-09-05 16:06:21 +04001220 return buf != NULL;
qiaonuohand0686c72014-02-18 14:11:33 +08001221}
1222
zhanghailiang4c7e2512014-10-09 14:13:11 +08001223static void write_dump_bitmap(DumpState *s, Error **errp)
qiaonuohand0686c72014-02-18 14:11:33 +08001224{
1225 int ret = 0;
1226 uint64_t last_pfn, pfn;
1227 void *dump_bitmap_buf;
1228 size_t num_dumpable;
1229 GuestPhysBlock *block_iter = NULL;
Andrew Jones8161bef2016-01-11 20:56:20 +01001230 size_t bitmap_bufsize = dump_bitmap_get_bufsize(s);
1231 size_t bits_per_buf = bitmap_bufsize * CHAR_BIT;
qiaonuohand0686c72014-02-18 14:11:33 +08001232
1233 /* dump_bitmap_buf is used to store dump_bitmap temporarily */
Andrew Jones8161bef2016-01-11 20:56:20 +01001234 dump_bitmap_buf = g_malloc0(bitmap_bufsize);
qiaonuohand0686c72014-02-18 14:11:33 +08001235
1236 num_dumpable = 0;
1237 last_pfn = 0;
1238
1239 /*
1240 * exam memory page by page, and set the bit in dump_bitmap corresponded
1241 * to the existing page.
1242 */
1243 while (get_next_page(&block_iter, &pfn, NULL, s)) {
1244 ret = set_dump_bitmap(last_pfn, pfn, true, dump_bitmap_buf, s);
1245 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001246 error_setg(errp, "dump: failed to set dump_bitmap");
qiaonuohand0686c72014-02-18 14:11:33 +08001247 goto out;
1248 }
1249
1250 last_pfn = pfn;
1251 num_dumpable++;
1252 }
1253
1254 /*
1255 * set_dump_bitmap will always leave the recently set bit un-sync. Here we
Andrew Jones8161bef2016-01-11 20:56:20 +01001256 * set the remaining bits from last_pfn to the end of the bitmap buffer to
1257 * 0. With those set, the un-sync bit will be synchronized into the vmcore.
qiaonuohand0686c72014-02-18 14:11:33 +08001258 */
1259 if (num_dumpable > 0) {
Andrew Jones8161bef2016-01-11 20:56:20 +01001260 ret = set_dump_bitmap(last_pfn, last_pfn + bits_per_buf, false,
qiaonuohand0686c72014-02-18 14:11:33 +08001261 dump_bitmap_buf, s);
1262 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001263 error_setg(errp, "dump: failed to sync dump_bitmap");
qiaonuohand0686c72014-02-18 14:11:33 +08001264 goto out;
1265 }
1266 }
1267
1268 /* number of dumpable pages that will be dumped later */
1269 s->num_dumpable = num_dumpable;
1270
1271out:
1272 g_free(dump_bitmap_buf);
qiaonuohand0686c72014-02-18 14:11:33 +08001273}
1274
qiaonuohan64cfba62014-02-18 14:11:34 +08001275static void prepare_data_cache(DataCache *data_cache, DumpState *s,
1276 off_t offset)
1277{
1278 data_cache->fd = s->fd;
1279 data_cache->data_size = 0;
Andrew Jones8161bef2016-01-11 20:56:20 +01001280 data_cache->buf_size = 4 * dump_bitmap_get_bufsize(s);
1281 data_cache->buf = g_malloc0(data_cache->buf_size);
qiaonuohan64cfba62014-02-18 14:11:34 +08001282 data_cache->offset = offset;
1283}
1284
1285static int write_cache(DataCache *dc, const void *buf, size_t size,
1286 bool flag_sync)
1287{
1288 /*
1289 * dc->buf_size should not be less than size, otherwise dc will never be
1290 * enough
1291 */
1292 assert(size <= dc->buf_size);
1293
1294 /*
1295 * if flag_sync is set, synchronize data in dc->buf into vmcore.
1296 * otherwise check if the space is enough for caching data in buf, if not,
1297 * write the data in dc->buf to dc->fd and reset dc->buf
1298 */
1299 if ((!flag_sync && dc->data_size + size > dc->buf_size) ||
1300 (flag_sync && dc->data_size > 0)) {
1301 if (write_buffer(dc->fd, dc->offset, dc->buf, dc->data_size) < 0) {
1302 return -1;
1303 }
1304
1305 dc->offset += dc->data_size;
1306 dc->data_size = 0;
1307 }
1308
1309 if (!flag_sync) {
1310 memcpy(dc->buf + dc->data_size, buf, size);
1311 dc->data_size += size;
1312 }
1313
1314 return 0;
1315}
1316
1317static void free_data_cache(DataCache *data_cache)
1318{
1319 g_free(data_cache->buf);
1320}
1321
qiaonuohand12f57e2014-02-18 14:11:35 +08001322static size_t get_len_buf_out(size_t page_size, uint32_t flag_compress)
1323{
Laszlo Ersekb87ef352014-05-20 13:42:53 +02001324 switch (flag_compress) {
1325 case DUMP_DH_COMPRESSED_ZLIB:
1326 return compressBound(page_size);
qiaonuohand12f57e2014-02-18 14:11:35 +08001327
Laszlo Ersekb87ef352014-05-20 13:42:53 +02001328 case DUMP_DH_COMPRESSED_LZO:
1329 /*
1330 * LZO will expand incompressible data by a little amount. Please check
1331 * the following URL to see the expansion calculation:
1332 * http://www.oberhumer.com/opensource/lzo/lzofaq.php
1333 */
1334 return page_size + page_size / 16 + 64 + 3;
qiaonuohand12f57e2014-02-18 14:11:35 +08001335
1336#ifdef CONFIG_SNAPPY
Laszlo Ersekb87ef352014-05-20 13:42:53 +02001337 case DUMP_DH_COMPRESSED_SNAPPY:
1338 return snappy_max_compressed_length(page_size);
qiaonuohand12f57e2014-02-18 14:11:35 +08001339#endif
Laszlo Ersekb87ef352014-05-20 13:42:53 +02001340 }
1341 return 0;
qiaonuohand12f57e2014-02-18 14:11:35 +08001342}
1343
zhanghailiang4c7e2512014-10-09 14:13:11 +08001344static void write_dump_pages(DumpState *s, Error **errp)
qiaonuohand12f57e2014-02-18 14:11:35 +08001345{
1346 int ret = 0;
1347 DataCache page_desc, page_data;
1348 size_t len_buf_out, size_out;
1349#ifdef CONFIG_LZO
1350 lzo_bytep wrkmem = NULL;
1351#endif
1352 uint8_t *buf_out = NULL;
1353 off_t offset_desc, offset_data;
1354 PageDescriptor pd, pd_zero;
1355 uint8_t *buf;
qiaonuohand12f57e2014-02-18 14:11:35 +08001356 GuestPhysBlock *block_iter = NULL;
1357 uint64_t pfn_iter;
Marc-André Lureau94d78842022-09-05 16:06:21 +04001358 g_autofree uint8_t *page = NULL;
qiaonuohand12f57e2014-02-18 14:11:35 +08001359
1360 /* get offset of page_desc and page_data in dump file */
1361 offset_desc = s->offset_page;
1362 offset_data = offset_desc + sizeof(PageDescriptor) * s->num_dumpable;
1363
1364 prepare_data_cache(&page_desc, s, offset_desc);
1365 prepare_data_cache(&page_data, s, offset_data);
1366
1367 /* prepare buffer to store compressed data */
Andrew Jones8161bef2016-01-11 20:56:20 +01001368 len_buf_out = get_len_buf_out(s->dump_info.page_size, s->flag_compress);
Laszlo Ersekb87ef352014-05-20 13:42:53 +02001369 assert(len_buf_out != 0);
qiaonuohand12f57e2014-02-18 14:11:35 +08001370
1371#ifdef CONFIG_LZO
1372 wrkmem = g_malloc(LZO1X_1_MEM_COMPRESS);
1373#endif
1374
1375 buf_out = g_malloc(len_buf_out);
1376
1377 /*
1378 * init zero page's page_desc and page_data, because every zero page
1379 * uses the same page_data
1380 */
Andrew Jones8161bef2016-01-11 20:56:20 +01001381 pd_zero.size = cpu_to_dump32(s, s->dump_info.page_size);
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001382 pd_zero.flags = cpu_to_dump32(s, 0);
1383 pd_zero.offset = cpu_to_dump64(s, offset_data);
1384 pd_zero.page_flags = cpu_to_dump64(s, 0);
Andrew Jones8161bef2016-01-11 20:56:20 +01001385 buf = g_malloc0(s->dump_info.page_size);
1386 ret = write_cache(&page_data, buf, s->dump_info.page_size, false);
qiaonuohand12f57e2014-02-18 14:11:35 +08001387 g_free(buf);
1388 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001389 error_setg(errp, "dump: failed to write page data (zero page)");
qiaonuohand12f57e2014-02-18 14:11:35 +08001390 goto out;
1391 }
1392
Andrew Jones8161bef2016-01-11 20:56:20 +01001393 offset_data += s->dump_info.page_size;
Marc-André Lureau94d78842022-09-05 16:06:21 +04001394 page = g_malloc(s->dump_info.page_size);
qiaonuohand12f57e2014-02-18 14:11:35 +08001395
1396 /*
1397 * dump memory to vmcore page by page. zero page will all be resided in the
1398 * first page of page section
1399 */
Marc-André Lureau94d78842022-09-05 16:06:21 +04001400 for (buf = page; get_next_page(&block_iter, &pfn_iter, &buf, s); buf = page) {
qiaonuohand12f57e2014-02-18 14:11:35 +08001401 /* check zero page */
Juan Quintelaf13f22b2021-11-18 15:57:44 +01001402 if (buffer_is_zero(buf, s->dump_info.page_size)) {
qiaonuohand12f57e2014-02-18 14:11:35 +08001403 ret = write_cache(&page_desc, &pd_zero, sizeof(PageDescriptor),
1404 false);
1405 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001406 error_setg(errp, "dump: failed to write page desc");
qiaonuohand12f57e2014-02-18 14:11:35 +08001407 goto out;
1408 }
1409 } else {
1410 /*
1411 * not zero page, then:
1412 * 1. compress the page
1413 * 2. write the compressed page into the cache of page_data
1414 * 3. get page desc of the compressed page and write it into the
1415 * cache of page_desc
1416 *
1417 * only one compression format will be used here, for
1418 * s->flag_compress is set. But when compression fails to work,
1419 * we fall back to save in plaintext.
1420 */
1421 size_out = len_buf_out;
1422 if ((s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) &&
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001423 (compress2(buf_out, (uLongf *)&size_out, buf,
Andrew Jones8161bef2016-01-11 20:56:20 +01001424 s->dump_info.page_size, Z_BEST_SPEED) == Z_OK) &&
1425 (size_out < s->dump_info.page_size)) {
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001426 pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_ZLIB);
1427 pd.size = cpu_to_dump32(s, size_out);
qiaonuohand12f57e2014-02-18 14:11:35 +08001428
1429 ret = write_cache(&page_data, buf_out, size_out, false);
1430 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001431 error_setg(errp, "dump: failed to write page data");
qiaonuohand12f57e2014-02-18 14:11:35 +08001432 goto out;
1433 }
1434#ifdef CONFIG_LZO
1435 } else if ((s->flag_compress & DUMP_DH_COMPRESSED_LZO) &&
Andrew Jones8161bef2016-01-11 20:56:20 +01001436 (lzo1x_1_compress(buf, s->dump_info.page_size, buf_out,
qiaonuohand12f57e2014-02-18 14:11:35 +08001437 (lzo_uint *)&size_out, wrkmem) == LZO_E_OK) &&
Andrew Jones8161bef2016-01-11 20:56:20 +01001438 (size_out < s->dump_info.page_size)) {
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001439 pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_LZO);
1440 pd.size = cpu_to_dump32(s, size_out);
qiaonuohand12f57e2014-02-18 14:11:35 +08001441
1442 ret = write_cache(&page_data, buf_out, size_out, false);
1443 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001444 error_setg(errp, "dump: failed to write page data");
qiaonuohand12f57e2014-02-18 14:11:35 +08001445 goto out;
1446 }
1447#endif
1448#ifdef CONFIG_SNAPPY
1449 } else if ((s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) &&
Andrew Jones8161bef2016-01-11 20:56:20 +01001450 (snappy_compress((char *)buf, s->dump_info.page_size,
qiaonuohand12f57e2014-02-18 14:11:35 +08001451 (char *)buf_out, &size_out) == SNAPPY_OK) &&
Andrew Jones8161bef2016-01-11 20:56:20 +01001452 (size_out < s->dump_info.page_size)) {
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001453 pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_SNAPPY);
1454 pd.size = cpu_to_dump32(s, size_out);
qiaonuohand12f57e2014-02-18 14:11:35 +08001455
1456 ret = write_cache(&page_data, buf_out, size_out, false);
1457 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001458 error_setg(errp, "dump: failed to write page data");
qiaonuohand12f57e2014-02-18 14:11:35 +08001459 goto out;
1460 }
1461#endif
1462 } else {
1463 /*
1464 * fall back to save in plaintext, size_out should be
Andrew Jones8161bef2016-01-11 20:56:20 +01001465 * assigned the target's page size
qiaonuohand12f57e2014-02-18 14:11:35 +08001466 */
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001467 pd.flags = cpu_to_dump32(s, 0);
Andrew Jones8161bef2016-01-11 20:56:20 +01001468 size_out = s->dump_info.page_size;
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001469 pd.size = cpu_to_dump32(s, size_out);
qiaonuohand12f57e2014-02-18 14:11:35 +08001470
Andrew Jones8161bef2016-01-11 20:56:20 +01001471 ret = write_cache(&page_data, buf,
1472 s->dump_info.page_size, false);
qiaonuohand12f57e2014-02-18 14:11:35 +08001473 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001474 error_setg(errp, "dump: failed to write page data");
qiaonuohand12f57e2014-02-18 14:11:35 +08001475 goto out;
1476 }
1477 }
1478
1479 /* get and write page desc here */
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001480 pd.page_flags = cpu_to_dump64(s, 0);
1481 pd.offset = cpu_to_dump64(s, offset_data);
qiaonuohand12f57e2014-02-18 14:11:35 +08001482 offset_data += size_out;
1483
1484 ret = write_cache(&page_desc, &pd, sizeof(PageDescriptor), false);
1485 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001486 error_setg(errp, "dump: failed to write page desc");
qiaonuohand12f57e2014-02-18 14:11:35 +08001487 goto out;
1488 }
1489 }
Peter Xu2264c2c2016-02-18 13:16:53 +08001490 s->written_size += s->dump_info.page_size;
qiaonuohand12f57e2014-02-18 14:11:35 +08001491 }
1492
1493 ret = write_cache(&page_desc, NULL, 0, true);
1494 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001495 error_setg(errp, "dump: failed to sync cache for page_desc");
qiaonuohand12f57e2014-02-18 14:11:35 +08001496 goto out;
1497 }
1498 ret = write_cache(&page_data, NULL, 0, true);
1499 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001500 error_setg(errp, "dump: failed to sync cache for page_data");
qiaonuohand12f57e2014-02-18 14:11:35 +08001501 goto out;
1502 }
1503
1504out:
1505 free_data_cache(&page_desc);
1506 free_data_cache(&page_data);
1507
1508#ifdef CONFIG_LZO
1509 g_free(wrkmem);
1510#endif
1511
1512 g_free(buf_out);
qiaonuohand12f57e2014-02-18 14:11:35 +08001513}
1514
zhanghailiang4c7e2512014-10-09 14:13:11 +08001515static void create_kdump_vmcore(DumpState *s, Error **errp)
qiaonuohanb53ccc32014-02-18 14:11:36 +08001516{
Janosch Frank86a518b2022-03-30 12:35:55 +00001517 ERRP_GUARD();
qiaonuohanb53ccc32014-02-18 14:11:36 +08001518 int ret;
1519
1520 /*
1521 * the kdump-compressed format is:
1522 * File offset
1523 * +------------------------------------------+ 0x0
1524 * | main header (struct disk_dump_header) |
1525 * |------------------------------------------+ block 1
1526 * | sub header (struct kdump_sub_header) |
1527 * |------------------------------------------+ block 2
1528 * | 1st-dump_bitmap |
1529 * |------------------------------------------+ block 2 + X blocks
1530 * | 2nd-dump_bitmap | (aligned by block)
1531 * |------------------------------------------+ block 2 + 2 * X blocks
1532 * | page desc for pfn 0 (struct page_desc) | (aligned by block)
1533 * | page desc for pfn 1 (struct page_desc) |
1534 * | : |
1535 * |------------------------------------------| (not aligned by block)
1536 * | page data (pfn 0) |
1537 * | page data (pfn 1) |
1538 * | : |
1539 * +------------------------------------------+
1540 */
1541
1542 ret = write_start_flat_header(s->fd);
1543 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001544 error_setg(errp, "dump: failed to write start flat header");
zhanghailiang4c7e2512014-10-09 14:13:11 +08001545 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001546 }
1547
Janosch Frank86a518b2022-03-30 12:35:55 +00001548 write_dump_header(s, errp);
1549 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +08001550 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001551 }
1552
Janosch Frank86a518b2022-03-30 12:35:55 +00001553 write_dump_bitmap(s, errp);
1554 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +08001555 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001556 }
1557
Janosch Frank86a518b2022-03-30 12:35:55 +00001558 write_dump_pages(s, errp);
1559 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +08001560 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001561 }
1562
1563 ret = write_end_flat_header(s->fd);
1564 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001565 error_setg(errp, "dump: failed to write end flat header");
zhanghailiang4c7e2512014-10-09 14:13:11 +08001566 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001567 }
qiaonuohanb53ccc32014-02-18 14:11:36 +08001568}
1569
Janosch Frank0c2994a2022-08-11 12:10:57 +00001570static int validate_start_block(DumpState *s)
Wen Congyang783e9b42012-05-07 12:10:47 +08001571{
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001572 GuestPhysBlock *block;
Wen Congyang783e9b42012-05-07 12:10:47 +08001573
Janosch Frankdddf7252022-08-11 12:10:58 +00001574 if (!dump_has_filter(s)) {
Wen Congyang783e9b42012-05-07 12:10:47 +08001575 return 0;
1576 }
1577
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001578 QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
Janosch Frank0c2994a2022-08-11 12:10:57 +00001579 /* This block is out of the range */
Janosch Frankdddf7252022-08-11 12:10:58 +00001580 if (block->target_start >= s->filter_area_begin + s->filter_area_length ||
1581 block->target_end <= s->filter_area_begin) {
Wen Congyang783e9b42012-05-07 12:10:47 +08001582 continue;
1583 }
Janosch Frank0c2994a2022-08-11 12:10:57 +00001584 return 0;
1585 }
Wen Congyang783e9b42012-05-07 12:10:47 +08001586
1587 return -1;
1588}
1589
qiaonuohan7aad2482014-02-18 14:11:31 +08001590static void get_max_mapnr(DumpState *s)
1591{
1592 GuestPhysBlock *last_block;
1593
Paolo Bonzinieae3eb32018-12-06 13:10:34 +01001594 last_block = QTAILQ_LAST(&s->guest_phys_blocks.head);
Andrew Jones8161bef2016-01-11 20:56:20 +01001595 s->max_mapnr = dump_paddr_to_pfn(s, last_block->target_end);
qiaonuohan7aad2482014-02-18 14:11:31 +08001596}
1597
Peter Xubaf28f52016-02-18 13:16:48 +08001598static DumpState dump_state_global = { .status = DUMP_STATUS_NONE };
1599
1600static void dump_state_prepare(DumpState *s)
1601{
1602 /* zero the struct, setting status to active */
1603 *s = (DumpState) { .status = DUMP_STATUS_ACTIVE };
1604}
1605
Marc-André Lureau544803c2022-03-23 19:57:31 +04001606bool qemu_system_dump_in_progress(void)
Peter Xu65d64f32016-02-18 13:16:49 +08001607{
1608 DumpState *state = &dump_state_global;
Stefan Hajnoczid73415a2020-09-23 11:56:46 +01001609 return (qatomic_read(&state->status) == DUMP_STATUS_ACTIVE);
Peter Xu65d64f32016-02-18 13:16:49 +08001610}
1611
Janosch Frankc370d532022-08-11 12:10:59 +00001612/*
1613 * calculate total size of memory to be dumped (taking filter into
1614 * account.)
1615 */
Peter Xu2264c2c2016-02-18 13:16:53 +08001616static int64_t dump_calculate_size(DumpState *s)
1617{
1618 GuestPhysBlock *block;
Janosch Frankc370d532022-08-11 12:10:59 +00001619 int64_t total = 0;
Peter Xu2264c2c2016-02-18 13:16:53 +08001620
1621 QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
Janosch Frankc370d532022-08-11 12:10:59 +00001622 total += dump_filtered_memblock_size(block,
1623 s->filter_area_begin,
1624 s->filter_area_length);
Peter Xu2264c2c2016-02-18 13:16:53 +08001625 }
1626
1627 return total;
1628}
1629
Marc-André Lureaud9feb512017-09-11 18:59:26 +02001630static void vmcoreinfo_update_phys_base(DumpState *s)
1631{
1632 uint64_t size, note_head_size, name_size, phys_base;
1633 char **lines;
1634 uint8_t *vmci;
1635 size_t i;
1636
1637 if (!note_name_equal(s, s->guest_note, "VMCOREINFO")) {
1638 return;
1639 }
1640
1641 get_note_sizes(s, s->guest_note, &note_head_size, &name_size, &size);
1642 note_head_size = ROUND_UP(note_head_size, 4);
1643
1644 vmci = s->guest_note + note_head_size + ROUND_UP(name_size, 4);
1645 *(vmci + size) = '\0';
1646
1647 lines = g_strsplit((char *)vmci, "\n", -1);
1648 for (i = 0; lines[i]; i++) {
Wei Huang68cbecf2018-03-09 12:03:23 -05001649 const char *prefix = NULL;
1650
1651 if (s->dump_info.d_machine == EM_X86_64) {
1652 prefix = "NUMBER(phys_base)=";
1653 } else if (s->dump_info.d_machine == EM_AARCH64) {
1654 prefix = "NUMBER(PHYS_OFFSET)=";
1655 }
1656
1657 if (prefix && g_str_has_prefix(lines[i], prefix)) {
1658 if (qemu_strtou64(lines[i] + strlen(prefix), NULL, 16,
Marc-André Lureaud9feb512017-09-11 18:59:26 +02001659 &phys_base) < 0) {
Wei Huang68cbecf2018-03-09 12:03:23 -05001660 warn_report("Failed to read %s", prefix);
Marc-André Lureaud9feb512017-09-11 18:59:26 +02001661 } else {
1662 s->dump_info.phys_base = phys_base;
1663 }
1664 break;
1665 }
1666 }
1667
1668 g_strfreev(lines);
1669}
1670
zhanghailiang4c7e2512014-10-09 14:13:11 +08001671static void dump_init(DumpState *s, int fd, bool has_format,
1672 DumpGuestMemoryFormat format, bool paging, bool has_filter,
1673 int64_t begin, int64_t length, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +08001674{
Janosch Frank86a518b2022-03-30 12:35:55 +00001675 ERRP_GUARD();
Marc-André Lureau903ef732017-09-11 18:59:25 +02001676 VMCoreInfoState *vmci = vmcoreinfo_find();
Andreas Färber182735e2013-05-29 22:29:20 +02001677 CPUState *cpu;
Wen Congyang783e9b42012-05-07 12:10:47 +08001678 int nr_cpus;
1679 int ret;
1680
Peter Xuca1fc8c2016-02-18 13:16:50 +08001681 s->has_format = has_format;
1682 s->format = format;
Peter Xu2264c2c2016-02-18 13:16:53 +08001683 s->written_size = 0;
Peter Xuca1fc8c2016-02-18 13:16:50 +08001684
qiaonuohanb53ccc32014-02-18 14:11:36 +08001685 /* kdump-compressed is conflict with paging and filter */
1686 if (has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
1687 assert(!paging && !has_filter);
1688 }
1689
Wen Congyang783e9b42012-05-07 12:10:47 +08001690 if (runstate_is_running()) {
1691 vm_stop(RUN_STATE_SAVE_VM);
1692 s->resume = true;
1693 } else {
1694 s->resume = false;
1695 }
1696
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001697 /* If we use KVM, we should synchronize the registers before we get dump
1698 * info or physmap info.
Wen Congyang783e9b42012-05-07 12:10:47 +08001699 */
Andreas Färber1b3509c2013-06-09 16:48:29 +02001700 cpu_synchronize_all_states();
Wen Congyang783e9b42012-05-07 12:10:47 +08001701 nr_cpus = 0;
Andreas Färberbdc44642013-06-24 23:50:24 +02001702 CPU_FOREACH(cpu) {
Wen Congyang783e9b42012-05-07 12:10:47 +08001703 nr_cpus++;
1704 }
1705
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001706 s->fd = fd;
Janosch Frankdddf7252022-08-11 12:10:58 +00001707 if (has_filter && !length) {
1708 error_setg(errp, QERR_INVALID_PARAMETER, "length");
1709 goto cleanup;
1710 }
1711 s->filter_area_begin = begin;
1712 s->filter_area_length = length;
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001713
Chen Gang29282072014-08-03 23:28:56 +08001714 memory_mapping_list_init(&s->list);
1715
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001716 guest_phys_blocks_init(&s->guest_phys_blocks);
Laszlo Ersekc5d7f602013-08-06 12:37:10 +02001717 guest_phys_blocks_append(&s->guest_phys_blocks);
Peter Xu2264c2c2016-02-18 13:16:53 +08001718 s->total_size = dump_calculate_size(s);
1719#ifdef DEBUG_DUMP_GUEST_MEMORY
1720 fprintf(stderr, "DUMP: total memory to dump: %lu\n", s->total_size);
1721#endif
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001722
Cornelia Huckd1e69942017-09-13 16:20:35 +02001723 /* it does not make sense to dump non-existent memory */
1724 if (!s->total_size) {
1725 error_setg(errp, "dump: no guest memory to dump");
1726 goto cleanup;
1727 }
1728
Janosch Frank0c2994a2022-08-11 12:10:57 +00001729 /* Is the filter filtering everything? */
1730 if (validate_start_block(s) == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001731 error_setg(errp, QERR_INVALID_PARAMETER, "begin");
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001732 goto cleanup;
1733 }
1734
1735 /* get dump info: endian, class and architecture.
1736 * If the target architecture is not supported, cpu_get_dump_info() will
1737 * return -1.
1738 */
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001739 ret = cpu_get_dump_info(&s->dump_info, &s->guest_phys_blocks);
Wen Congyang783e9b42012-05-07 12:10:47 +08001740 if (ret < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001741 error_setg(errp, QERR_UNSUPPORTED);
Wen Congyang783e9b42012-05-07 12:10:47 +08001742 goto cleanup;
1743 }
1744
Andrew Jones8161bef2016-01-11 20:56:20 +01001745 if (!s->dump_info.page_size) {
1746 s->dump_info.page_size = TARGET_PAGE_SIZE;
1747 }
1748
Paolo Bonzini4720bd02012-06-07 08:48:09 +02001749 s->note_size = cpu_get_note_size(s->dump_info.d_class,
1750 s->dump_info.d_machine, nr_cpus);
Aneesh Kumar K.Vbb6b6842013-10-01 21:49:32 +05301751 if (s->note_size < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001752 error_setg(errp, QERR_UNSUPPORTED);
Paolo Bonzini4720bd02012-06-07 08:48:09 +02001753 goto cleanup;
1754 }
1755
Marc-André Lureau903ef732017-09-11 18:59:25 +02001756 /*
Marc-André Lureaud9feb512017-09-11 18:59:26 +02001757 * The goal of this block is to (a) update the previously guessed
1758 * phys_base, (b) copy the guest note out of the guest.
1759 * Failure to do so is not fatal for dumping.
Marc-André Lureau903ef732017-09-11 18:59:25 +02001760 */
1761 if (vmci) {
1762 uint64_t addr, note_head_size, name_size, desc_size;
1763 uint32_t size;
1764 uint16_t format;
1765
Janosch Frank05bbaa502022-03-30 12:36:00 +00001766 note_head_size = dump_is_64bit(s) ?
1767 sizeof(Elf64_Nhdr) : sizeof(Elf32_Nhdr);
Marc-André Lureau903ef732017-09-11 18:59:25 +02001768
1769 format = le16_to_cpu(vmci->vmcoreinfo.guest_format);
1770 size = le32_to_cpu(vmci->vmcoreinfo.size);
1771 addr = le64_to_cpu(vmci->vmcoreinfo.paddr);
1772 if (!vmci->has_vmcoreinfo) {
1773 warn_report("guest note is not present");
1774 } else if (size < note_head_size || size > MAX_GUEST_NOTE_SIZE) {
1775 warn_report("guest note size is invalid: %" PRIu32, size);
Marc-André Lureau5be5df72018-08-17 17:59:10 +02001776 } else if (format != FW_CFG_VMCOREINFO_FORMAT_ELF) {
Marc-André Lureau903ef732017-09-11 18:59:25 +02001777 warn_report("guest note format is unsupported: %" PRIu16, format);
1778 } else {
1779 s->guest_note = g_malloc(size + 1); /* +1 for adding \0 */
1780 cpu_physical_memory_read(addr, s->guest_note, size);
1781
1782 get_note_sizes(s, s->guest_note, NULL, &name_size, &desc_size);
1783 s->guest_note_size = ELF_NOTE_SIZE(note_head_size, name_size,
1784 desc_size);
1785 if (name_size > MAX_GUEST_NOTE_SIZE ||
1786 desc_size > MAX_GUEST_NOTE_SIZE ||
1787 s->guest_note_size > size) {
1788 warn_report("Invalid guest note header");
1789 g_free(s->guest_note);
1790 s->guest_note = NULL;
1791 } else {
Marc-André Lureaud9feb512017-09-11 18:59:26 +02001792 vmcoreinfo_update_phys_base(s);
Marc-André Lureau903ef732017-09-11 18:59:25 +02001793 s->note_size += s->guest_note_size;
1794 }
1795 }
1796 }
1797
Wen Congyang783e9b42012-05-07 12:10:47 +08001798 /* get memory mapping */
Wen Congyang783e9b42012-05-07 12:10:47 +08001799 if (paging) {
Janosch Frank86a518b2022-03-30 12:35:55 +00001800 qemu_get_guest_memory_mapping(&s->list, &s->guest_phys_blocks, errp);
1801 if (*errp) {
Andreas Färber11ed09c2013-05-29 21:54:03 +02001802 goto cleanup;
1803 }
Wen Congyang783e9b42012-05-07 12:10:47 +08001804 } else {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001805 qemu_get_guest_simple_memory_mapping(&s->list, &s->guest_phys_blocks);
Wen Congyang783e9b42012-05-07 12:10:47 +08001806 }
1807
qiaonuohan7aad2482014-02-18 14:11:31 +08001808 s->nr_cpus = nr_cpus;
qiaonuohan7aad2482014-02-18 14:11:31 +08001809
1810 get_max_mapnr(s);
1811
1812 uint64_t tmp;
Andrew Jones8161bef2016-01-11 20:56:20 +01001813 tmp = DIV_ROUND_UP(DIV_ROUND_UP(s->max_mapnr, CHAR_BIT),
1814 s->dump_info.page_size);
1815 s->len_dump_bitmap = tmp * s->dump_info.page_size;
qiaonuohan7aad2482014-02-18 14:11:31 +08001816
qiaonuohanb53ccc32014-02-18 14:11:36 +08001817 /* init for kdump-compressed format */
1818 if (has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
1819 switch (format) {
1820 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB:
1821 s->flag_compress = DUMP_DH_COMPRESSED_ZLIB;
1822 break;
1823
1824 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO:
Laszlo Ersekc998acb2014-05-20 13:42:22 +02001825#ifdef CONFIG_LZO
1826 if (lzo_init() != LZO_E_OK) {
1827 error_setg(errp, "failed to initialize the LZO library");
1828 goto cleanup;
1829 }
1830#endif
qiaonuohanb53ccc32014-02-18 14:11:36 +08001831 s->flag_compress = DUMP_DH_COMPRESSED_LZO;
1832 break;
1833
1834 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY:
1835 s->flag_compress = DUMP_DH_COMPRESSED_SNAPPY;
1836 break;
1837
1838 default:
1839 s->flag_compress = 0;
1840 }
1841
zhanghailiang4c7e2512014-10-09 14:13:11 +08001842 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001843 }
1844
Janosch Frankdddf7252022-08-11 12:10:58 +00001845 if (dump_has_filter(s)) {
1846 memory_mapping_filter(&s->list, s->filter_area_begin, s->filter_area_length);
Wen Congyang783e9b42012-05-07 12:10:47 +08001847 }
1848
1849 /*
1850 * calculate phdr_num
1851 *
1852 * the type of ehdr->e_phnum is uint16_t, so we should avoid overflow
1853 */
1854 s->phdr_num = 1; /* PT_NOTE */
1855 if (s->list.num < UINT16_MAX - 2) {
Janosch Frank862a3952022-03-30 12:35:57 +00001856 s->shdr_num = 0;
Wen Congyang783e9b42012-05-07 12:10:47 +08001857 s->phdr_num += s->list.num;
Wen Congyang783e9b42012-05-07 12:10:47 +08001858 } else {
Janosch Frank046bc412022-04-07 09:48:24 +00001859 /* sh_info of section 0 holds the real number of phdrs */
Janosch Frank862a3952022-03-30 12:35:57 +00001860 s->shdr_num = 1;
Wen Congyang783e9b42012-05-07 12:10:47 +08001861
1862 /* the type of shdr->sh_info is uint32_t, so we should avoid overflow */
1863 if (s->list.num <= UINT32_MAX - 1) {
Janosch Frank046bc412022-04-07 09:48:24 +00001864 s->phdr_num += s->list.num;
Wen Congyang783e9b42012-05-07 12:10:47 +08001865 } else {
Janosch Frank046bc412022-04-07 09:48:24 +00001866 s->phdr_num = UINT32_MAX;
Wen Congyang783e9b42012-05-07 12:10:47 +08001867 }
1868 }
1869
Janosch Frank05bbaa502022-03-30 12:36:00 +00001870 if (dump_is_64bit(s)) {
Janosch Frankcb415fd2022-10-17 08:38:14 +00001871 s->shdr_offset = sizeof(Elf64_Ehdr);
1872 s->phdr_offset = s->shdr_offset + sizeof(Elf64_Shdr) * s->shdr_num;
1873 s->note_offset = s->phdr_offset + sizeof(Elf64_Phdr) * s->phdr_num;
Wen Congyang783e9b42012-05-07 12:10:47 +08001874 } else {
Janosch Frankcb415fd2022-10-17 08:38:14 +00001875 s->shdr_offset = sizeof(Elf32_Ehdr);
1876 s->phdr_offset = s->shdr_offset + sizeof(Elf32_Shdr) * s->shdr_num;
1877 s->note_offset = s->phdr_offset + sizeof(Elf32_Phdr) * s->phdr_num;
Wen Congyang783e9b42012-05-07 12:10:47 +08001878 }
1879
zhanghailiang4c7e2512014-10-09 14:13:11 +08001880 return;
Wen Congyang783e9b42012-05-07 12:10:47 +08001881
1882cleanup:
Chen Gang29282072014-08-03 23:28:56 +08001883 dump_cleanup(s);
Wen Congyang783e9b42012-05-07 12:10:47 +08001884}
1885
Peter Xuca1fc8c2016-02-18 13:16:50 +08001886/* this operation might be time consuming. */
1887static void dump_process(DumpState *s, Error **errp)
1888{
Janosch Frank86a518b2022-03-30 12:35:55 +00001889 ERRP_GUARD();
Peter Xud42a0d12016-02-18 13:16:56 +08001890 DumpQueryResult *result = NULL;
Peter Xuca1fc8c2016-02-18 13:16:50 +08001891
Viktor Prutyanov2da91b52018-05-17 19:23:39 +03001892 if (s->has_format && s->format == DUMP_GUEST_MEMORY_FORMAT_WIN_DMP) {
1893#ifdef TARGET_X86_64
Janosch Frank86a518b2022-03-30 12:35:55 +00001894 create_win_dump(s, errp);
Viktor Prutyanov2da91b52018-05-17 19:23:39 +03001895#endif
1896 } else if (s->has_format && s->format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
Janosch Frank86a518b2022-03-30 12:35:55 +00001897 create_kdump_vmcore(s, errp);
Peter Xuca1fc8c2016-02-18 13:16:50 +08001898 } else {
Janosch Frank86a518b2022-03-30 12:35:55 +00001899 create_vmcore(s, errp);
Peter Xuca1fc8c2016-02-18 13:16:50 +08001900 }
1901
Peter Xu39ba2ea2016-02-18 13:16:54 +08001902 /* make sure status is written after written_size updates */
1903 smp_wmb();
Stefan Hajnoczid73415a2020-09-23 11:56:46 +01001904 qatomic_set(&s->status,
Janosch Frank86a518b2022-03-30 12:35:55 +00001905 (*errp ? DUMP_STATUS_FAILED : DUMP_STATUS_COMPLETED));
Peter Xuca1fc8c2016-02-18 13:16:50 +08001906
Peter Xud42a0d12016-02-18 13:16:56 +08001907 /* send DUMP_COMPLETED message (unconditionally) */
1908 result = qmp_query_dump(NULL);
1909 /* should never fail */
1910 assert(result);
Janosch Frank86a518b2022-03-30 12:35:55 +00001911 qapi_event_send_dump_completed(result, !!*errp, (*errp ?
1912 error_get_pretty(*errp) : NULL));
Peter Xud42a0d12016-02-18 13:16:56 +08001913 qapi_free_DumpQueryResult(result);
1914
Peter Xuca1fc8c2016-02-18 13:16:50 +08001915 dump_cleanup(s);
1916}
1917
Peter Xu1fbeff72016-02-18 13:16:52 +08001918static void *dump_thread(void *data)
1919{
Peter Xu1fbeff72016-02-18 13:16:52 +08001920 DumpState *s = (DumpState *)data;
Alberto Garciac3a8fe32017-08-29 15:08:36 +03001921 dump_process(s, NULL);
Peter Xu1fbeff72016-02-18 13:16:52 +08001922 return NULL;
1923}
1924
Peter Xu39ba2ea2016-02-18 13:16:54 +08001925DumpQueryResult *qmp_query_dump(Error **errp)
1926{
1927 DumpQueryResult *result = g_new(DumpQueryResult, 1);
1928 DumpState *state = &dump_state_global;
Stefan Hajnoczid73415a2020-09-23 11:56:46 +01001929 result->status = qatomic_read(&state->status);
Peter Xu39ba2ea2016-02-18 13:16:54 +08001930 /* make sure we are reading status and written_size in order */
1931 smp_rmb();
1932 result->completed = state->written_size;
1933 result->total = state->total_size;
1934 return result;
1935}
1936
Peter Xu228de9c2016-02-18 13:16:47 +08001937void qmp_dump_guest_memory(bool paging, const char *file,
1938 bool has_detach, bool detach,
1939 bool has_begin, int64_t begin, bool has_length,
qiaonuohanb53ccc32014-02-18 14:11:36 +08001940 int64_t length, bool has_format,
1941 DumpGuestMemoryFormat format, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +08001942{
Janosch Frank86a518b2022-03-30 12:35:55 +00001943 ERRP_GUARD();
Wen Congyang783e9b42012-05-07 12:10:47 +08001944 const char *p;
1945 int fd = -1;
1946 DumpState *s;
Peter Xu1fbeff72016-02-18 13:16:52 +08001947 bool detach_p = false;
Wen Congyang783e9b42012-05-07 12:10:47 +08001948
Peter Xu63e27f22016-02-18 13:16:51 +08001949 if (runstate_check(RUN_STATE_INMIGRATE)) {
1950 error_setg(errp, "Dump not allowed during incoming migration.");
1951 return;
1952 }
1953
Peter Xu65d64f32016-02-18 13:16:49 +08001954 /* if there is a dump in background, we should wait until the dump
1955 * finished */
Marc-André Lureau544803c2022-03-23 19:57:31 +04001956 if (qemu_system_dump_in_progress()) {
Peter Xu65d64f32016-02-18 13:16:49 +08001957 error_setg(errp, "There is a dump in process, please wait.");
1958 return;
1959 }
1960
qiaonuohanb53ccc32014-02-18 14:11:36 +08001961 /*
1962 * kdump-compressed format need the whole memory dumped, so paging or
1963 * filter is not supported here.
1964 */
1965 if ((has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) &&
1966 (paging || has_begin || has_length)) {
1967 error_setg(errp, "kdump-compressed format doesn't support paging or "
1968 "filter");
1969 return;
1970 }
Wen Congyang783e9b42012-05-07 12:10:47 +08001971 if (has_begin && !has_length) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001972 error_setg(errp, QERR_MISSING_PARAMETER, "length");
Wen Congyang783e9b42012-05-07 12:10:47 +08001973 return;
1974 }
1975 if (!has_begin && has_length) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001976 error_setg(errp, QERR_MISSING_PARAMETER, "begin");
Wen Congyang783e9b42012-05-07 12:10:47 +08001977 return;
1978 }
Peter Xu1fbeff72016-02-18 13:16:52 +08001979 if (has_detach) {
1980 detach_p = detach;
1981 }
Wen Congyang783e9b42012-05-07 12:10:47 +08001982
qiaonuohanb53ccc32014-02-18 14:11:36 +08001983 /* check whether lzo/snappy is supported */
1984#ifndef CONFIG_LZO
1985 if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO) {
1986 error_setg(errp, "kdump-lzo is not available now");
1987 return;
1988 }
1989#endif
1990
1991#ifndef CONFIG_SNAPPY
1992 if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY) {
1993 error_setg(errp, "kdump-snappy is not available now");
1994 return;
1995 }
1996#endif
1997
Viktor Prutyanov2da91b52018-05-17 19:23:39 +03001998#ifndef TARGET_X86_64
1999 if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_WIN_DMP) {
2000 error_setg(errp, "Windows dump is only available for x86-64");
2001 return;
2002 }
2003#endif
2004
Wen Congyang783e9b42012-05-07 12:10:47 +08002005#if !defined(WIN32)
2006 if (strstart(file, "fd:", &p)) {
Kevin Wolf947e4742020-10-05 17:58:44 +02002007 fd = monitor_get_fd(monitor_cur(), p, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +08002008 if (fd == -1) {
Wen Congyang783e9b42012-05-07 12:10:47 +08002009 return;
2010 }
2011 }
2012#endif
2013
2014 if (strstart(file, "file:", &p)) {
Daniel P. Berrangé448058a2020-07-21 13:25:21 +01002015 fd = qemu_open_old(p, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, S_IRUSR);
Wen Congyang783e9b42012-05-07 12:10:47 +08002016 if (fd < 0) {
Luiz Capitulino75817662013-06-07 14:36:01 -04002017 error_setg_file_open(errp, errno, p);
Wen Congyang783e9b42012-05-07 12:10:47 +08002018 return;
2019 }
2020 }
2021
2022 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002023 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
Wen Congyang783e9b42012-05-07 12:10:47 +08002024 return;
2025 }
2026
Peter Xub7bc6b12021-09-22 12:20:09 -04002027 if (!dump_migration_blocker) {
2028 error_setg(&dump_migration_blocker,
2029 "Live migration disabled: dump-guest-memory in progress");
2030 }
2031
2032 /*
2033 * Allows even for -only-migratable, but forbid migration during the
2034 * process of dump guest memory.
2035 */
2036 if (migrate_add_blocker_internal(dump_migration_blocker, errp)) {
2037 /* Remember to release the fd before passing it over to dump state */
2038 close(fd);
2039 return;
2040 }
2041
Peter Xubaf28f52016-02-18 13:16:48 +08002042 s = &dump_state_global;
2043 dump_state_prepare(s);
Wen Congyang783e9b42012-05-07 12:10:47 +08002044
zhanghailiang4c7e2512014-10-09 14:13:11 +08002045 dump_init(s, fd, has_format, format, paging, has_begin,
Janosch Frank86a518b2022-03-30 12:35:55 +00002046 begin, length, errp);
2047 if (*errp) {
Stefan Hajnoczid73415a2020-09-23 11:56:46 +01002048 qatomic_set(&s->status, DUMP_STATUS_FAILED);
Wen Congyang783e9b42012-05-07 12:10:47 +08002049 return;
2050 }
2051
Peter Xu1fbeff72016-02-18 13:16:52 +08002052 if (detach_p) {
2053 /* detached dump */
Fam Zheng6796b402017-05-03 15:28:19 +08002054 s->detached = true;
Peter Xu1fbeff72016-02-18 13:16:52 +08002055 qemu_thread_create(&s->dump_thread, "dump_thread", dump_thread,
2056 s, QEMU_THREAD_DETACHED);
2057 } else {
2058 /* sync dump */
2059 dump_process(s, errp);
2060 }
Wen Congyang783e9b42012-05-07 12:10:47 +08002061}
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002062
2063DumpGuestMemoryCapability *qmp_query_dump_guest_memory_capability(Error **errp)
2064{
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002065 DumpGuestMemoryCapability *cap =
Markus Armbrusterb21e2382022-03-15 15:41:56 +01002066 g_new0(DumpGuestMemoryCapability, 1);
Eric Blake95b3a8c2021-01-13 16:10:13 -06002067 DumpGuestMemoryFormatList **tail = &cap->formats;
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002068
2069 /* elf is always available */
Eric Blake95b3a8c2021-01-13 16:10:13 -06002070 QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_ELF);
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002071
2072 /* kdump-zlib is always available */
Eric Blake95b3a8c2021-01-13 16:10:13 -06002073 QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB);
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002074
2075 /* add new item if kdump-lzo is available */
2076#ifdef CONFIG_LZO
Eric Blake95b3a8c2021-01-13 16:10:13 -06002077 QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO);
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002078#endif
2079
2080 /* add new item if kdump-snappy is available */
2081#ifdef CONFIG_SNAPPY
Eric Blake95b3a8c2021-01-13 16:10:13 -06002082 QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY);
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002083#endif
2084
Viktor Prutyanov2da91b52018-05-17 19:23:39 +03002085 /* Windows dump is available only if target is x86_64 */
2086#ifdef TARGET_X86_64
Eric Blake95b3a8c2021-01-13 16:10:13 -06002087 QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_WIN_DMP);
Viktor Prutyanov2da91b52018-05-17 19:23:39 +03002088#endif
2089
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002090 return cap;
2091}