blob: 4d9658ffa24f6e9fc8d91e519f0207a8d7bad280 [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
Bharata B Raoacb0ef52014-05-19 19:57:44 +020062uint16_t cpu_to_dump16(DumpState *s, uint16_t val)
Wen Congyang783e9b42012-05-07 12:10:47 +080063{
Bharata B Raoacb0ef52014-05-19 19:57:44 +020064 if (s->dump_info.d_endian == ELFDATA2LSB) {
Wen Congyang783e9b42012-05-07 12:10:47 +080065 val = cpu_to_le16(val);
66 } else {
67 val = cpu_to_be16(val);
68 }
69
70 return val;
71}
72
Bharata B Raoacb0ef52014-05-19 19:57:44 +020073uint32_t cpu_to_dump32(DumpState *s, uint32_t val)
Wen Congyang783e9b42012-05-07 12:10:47 +080074{
Bharata B Raoacb0ef52014-05-19 19:57:44 +020075 if (s->dump_info.d_endian == ELFDATA2LSB) {
Wen Congyang783e9b42012-05-07 12:10:47 +080076 val = cpu_to_le32(val);
77 } else {
78 val = cpu_to_be32(val);
79 }
80
81 return val;
82}
83
Bharata B Raoacb0ef52014-05-19 19:57:44 +020084uint64_t cpu_to_dump64(DumpState *s, uint64_t val)
Wen Congyang783e9b42012-05-07 12:10:47 +080085{
Bharata B Raoacb0ef52014-05-19 19:57:44 +020086 if (s->dump_info.d_endian == ELFDATA2LSB) {
Wen Congyang783e9b42012-05-07 12:10:47 +080087 val = cpu_to_le64(val);
88 } else {
89 val = cpu_to_be64(val);
90 }
91
92 return val;
93}
94
Wen Congyang783e9b42012-05-07 12:10:47 +080095static int dump_cleanup(DumpState *s)
96{
Laszlo Ersek5ee163e2013-08-06 12:37:09 +020097 guest_phys_blocks_free(&s->guest_phys_blocks);
Wen Congyang783e9b42012-05-07 12:10:47 +080098 memory_mapping_list_free(&s->list);
Chen Gang29282072014-08-03 23:28:56 +080099 close(s->fd);
Marc-André Lureau903ef732017-09-11 18:59:25 +0200100 g_free(s->guest_note);
101 s->guest_note = NULL;
Wen Congyang783e9b42012-05-07 12:10:47 +0800102 if (s->resume) {
Fam Zheng6796b402017-05-03 15:28:19 +0800103 if (s->detached) {
104 qemu_mutex_lock_iothread();
105 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800106 vm_start();
Fam Zheng6796b402017-05-03 15:28:19 +0800107 if (s->detached) {
108 qemu_mutex_unlock_iothread();
109 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800110 }
Peter Xub7bc6b12021-09-22 12:20:09 -0400111 migrate_del_blocker(dump_migration_blocker);
Wen Congyang783e9b42012-05-07 12:10:47 +0800112
Chen Gang29282072014-08-03 23:28:56 +0800113 return 0;
Wen Congyang783e9b42012-05-07 12:10:47 +0800114}
115
qiaonuohanb5ba1cc2014-02-18 14:11:25 +0800116static int fd_write_vmcore(const void *buf, size_t size, void *opaque)
Wen Congyang783e9b42012-05-07 12:10:47 +0800117{
118 DumpState *s = opaque;
Luiz Capitulino2f616522012-09-21 13:17:55 -0300119 size_t written_size;
Wen Congyang783e9b42012-05-07 12:10:47 +0800120
Luiz Capitulino2f616522012-09-21 13:17:55 -0300121 written_size = qemu_write_full(s->fd, buf, size);
122 if (written_size != size) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200123 return -errno;
Wen Congyang783e9b42012-05-07 12:10:47 +0800124 }
125
126 return 0;
127}
128
zhanghailiang4c7e2512014-10-09 14:13:11 +0800129static void write_elf64_header(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800130{
Janosch Frank046bc412022-04-07 09:48:24 +0000131 /*
132 * phnum in the elf header is 16 bit, if we have more segments we
133 * set phnum to PN_XNUM and write the real number of segments to a
134 * special section.
135 */
136 uint16_t phnum = MIN(s->phdr_num, PN_XNUM);
Wen Congyang783e9b42012-05-07 12:10:47 +0800137 Elf64_Ehdr elf_header;
138 int ret;
Wen Congyang783e9b42012-05-07 12:10:47 +0800139
140 memset(&elf_header, 0, sizeof(Elf64_Ehdr));
141 memcpy(&elf_header, ELFMAG, SELFMAG);
142 elf_header.e_ident[EI_CLASS] = ELFCLASS64;
143 elf_header.e_ident[EI_DATA] = s->dump_info.d_endian;
144 elf_header.e_ident[EI_VERSION] = EV_CURRENT;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200145 elf_header.e_type = cpu_to_dump16(s, ET_CORE);
146 elf_header.e_machine = cpu_to_dump16(s, s->dump_info.d_machine);
147 elf_header.e_version = cpu_to_dump32(s, EV_CURRENT);
148 elf_header.e_ehsize = cpu_to_dump16(s, sizeof(elf_header));
Janosch Franke71d3532022-03-30 12:35:59 +0000149 elf_header.e_phoff = cpu_to_dump64(s, s->phdr_offset);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200150 elf_header.e_phentsize = cpu_to_dump16(s, sizeof(Elf64_Phdr));
Janosch Frank046bc412022-04-07 09:48:24 +0000151 elf_header.e_phnum = cpu_to_dump16(s, phnum);
Janosch Frank862a3952022-03-30 12:35:57 +0000152 if (s->shdr_num) {
Janosch Franke71d3532022-03-30 12:35:59 +0000153 elf_header.e_shoff = cpu_to_dump64(s, s->shdr_offset);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200154 elf_header.e_shentsize = cpu_to_dump16(s, sizeof(Elf64_Shdr));
Janosch Frank862a3952022-03-30 12:35:57 +0000155 elf_header.e_shnum = cpu_to_dump16(s, s->shdr_num);
Wen Congyang783e9b42012-05-07 12:10:47 +0800156 }
157
158 ret = fd_write_vmcore(&elf_header, sizeof(elf_header), s);
159 if (ret < 0) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200160 error_setg_errno(errp, -ret, "dump: failed to write elf header");
Wen Congyang783e9b42012-05-07 12:10:47 +0800161 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800162}
163
zhanghailiang4c7e2512014-10-09 14:13:11 +0800164static void write_elf32_header(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800165{
Janosch Frank046bc412022-04-07 09:48:24 +0000166 /*
167 * phnum in the elf header is 16 bit, if we have more segments we
168 * set phnum to PN_XNUM and write the real number of segments to a
169 * special section.
170 */
171 uint16_t phnum = MIN(s->phdr_num, PN_XNUM);
Wen Congyang783e9b42012-05-07 12:10:47 +0800172 Elf32_Ehdr elf_header;
173 int ret;
Wen Congyang783e9b42012-05-07 12:10:47 +0800174
175 memset(&elf_header, 0, sizeof(Elf32_Ehdr));
176 memcpy(&elf_header, ELFMAG, SELFMAG);
177 elf_header.e_ident[EI_CLASS] = ELFCLASS32;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200178 elf_header.e_ident[EI_DATA] = s->dump_info.d_endian;
Wen Congyang783e9b42012-05-07 12:10:47 +0800179 elf_header.e_ident[EI_VERSION] = EV_CURRENT;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200180 elf_header.e_type = cpu_to_dump16(s, ET_CORE);
181 elf_header.e_machine = cpu_to_dump16(s, s->dump_info.d_machine);
182 elf_header.e_version = cpu_to_dump32(s, EV_CURRENT);
183 elf_header.e_ehsize = cpu_to_dump16(s, sizeof(elf_header));
Janosch Franke71d3532022-03-30 12:35:59 +0000184 elf_header.e_phoff = cpu_to_dump32(s, s->phdr_offset);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200185 elf_header.e_phentsize = cpu_to_dump16(s, sizeof(Elf32_Phdr));
Janosch Frank046bc412022-04-07 09:48:24 +0000186 elf_header.e_phnum = cpu_to_dump16(s, phnum);
Janosch Frank862a3952022-03-30 12:35:57 +0000187 if (s->shdr_num) {
Janosch Franke71d3532022-03-30 12:35:59 +0000188 elf_header.e_shoff = cpu_to_dump32(s, s->shdr_offset);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200189 elf_header.e_shentsize = cpu_to_dump16(s, sizeof(Elf32_Shdr));
Janosch Frank862a3952022-03-30 12:35:57 +0000190 elf_header.e_shnum = cpu_to_dump16(s, s->shdr_num);
Wen Congyang783e9b42012-05-07 12:10:47 +0800191 }
192
193 ret = fd_write_vmcore(&elf_header, sizeof(elf_header), s);
194 if (ret < 0) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200195 error_setg_errno(errp, -ret, "dump: failed to write elf header");
Wen Congyang783e9b42012-05-07 12:10:47 +0800196 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800197}
198
zhanghailiang4c7e2512014-10-09 14:13:11 +0800199static void write_elf64_load(DumpState *s, MemoryMapping *memory_mapping,
200 int phdr_index, hwaddr offset,
201 hwaddr filesz, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800202{
203 Elf64_Phdr phdr;
204 int ret;
Wen Congyang783e9b42012-05-07 12:10:47 +0800205
206 memset(&phdr, 0, sizeof(Elf64_Phdr));
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200207 phdr.p_type = cpu_to_dump32(s, PT_LOAD);
208 phdr.p_offset = cpu_to_dump64(s, offset);
209 phdr.p_paddr = cpu_to_dump64(s, memory_mapping->phys_addr);
210 phdr.p_filesz = cpu_to_dump64(s, filesz);
211 phdr.p_memsz = cpu_to_dump64(s, memory_mapping->length);
Jon Dorone17bebd2019-01-09 10:22:03 +0200212 phdr.p_vaddr = cpu_to_dump64(s, memory_mapping->virt_addr) ?: phdr.p_paddr;
Wen Congyang783e9b42012-05-07 12:10:47 +0800213
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200214 assert(memory_mapping->length >= filesz);
215
Wen Congyang783e9b42012-05-07 12:10:47 +0800216 ret = fd_write_vmcore(&phdr, sizeof(Elf64_Phdr), s);
217 if (ret < 0) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200218 error_setg_errno(errp, -ret,
219 "dump: failed to write program header table");
Wen Congyang783e9b42012-05-07 12:10:47 +0800220 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800221}
222
zhanghailiang4c7e2512014-10-09 14:13:11 +0800223static void write_elf32_load(DumpState *s, MemoryMapping *memory_mapping,
224 int phdr_index, hwaddr offset,
225 hwaddr filesz, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800226{
227 Elf32_Phdr phdr;
228 int ret;
Wen Congyang783e9b42012-05-07 12:10:47 +0800229
230 memset(&phdr, 0, sizeof(Elf32_Phdr));
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200231 phdr.p_type = cpu_to_dump32(s, PT_LOAD);
232 phdr.p_offset = cpu_to_dump32(s, offset);
233 phdr.p_paddr = cpu_to_dump32(s, memory_mapping->phys_addr);
234 phdr.p_filesz = cpu_to_dump32(s, filesz);
235 phdr.p_memsz = cpu_to_dump32(s, memory_mapping->length);
Jon Dorone17bebd2019-01-09 10:22:03 +0200236 phdr.p_vaddr =
237 cpu_to_dump32(s, memory_mapping->virt_addr) ?: phdr.p_paddr;
Wen Congyang783e9b42012-05-07 12:10:47 +0800238
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200239 assert(memory_mapping->length >= filesz);
240
Wen Congyang783e9b42012-05-07 12:10:47 +0800241 ret = fd_write_vmcore(&phdr, sizeof(Elf32_Phdr), s);
242 if (ret < 0) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200243 error_setg_errno(errp, -ret,
244 "dump: failed to write program header table");
Wen Congyang783e9b42012-05-07 12:10:47 +0800245 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800246}
247
Janosch Frankbc7d5582022-03-30 12:36:01 +0000248static void write_elf64_phdr_note(DumpState *s, Elf64_Phdr *phdr)
Wen Congyang783e9b42012-05-07 12:10:47 +0800249{
Janosch Frankbc7d5582022-03-30 12:36:01 +0000250 memset(phdr, 0, sizeof(*phdr));
251 phdr->p_type = cpu_to_dump32(s, PT_NOTE);
252 phdr->p_offset = cpu_to_dump64(s, s->note_offset);
253 phdr->p_paddr = 0;
254 phdr->p_filesz = cpu_to_dump64(s, s->note_size);
255 phdr->p_memsz = cpu_to_dump64(s, s->note_size);
256 phdr->p_vaddr = 0;
Wen Congyang783e9b42012-05-07 12:10:47 +0800257}
258
Paolo Bonzini0bc3cd62013-04-08 17:29:59 +0200259static inline int cpu_index(CPUState *cpu)
260{
261 return cpu->cpu_index + 1;
262}
263
Marc-André Lureau903ef732017-09-11 18:59:25 +0200264static void write_guest_note(WriteCoreDumpFunction f, DumpState *s,
265 Error **errp)
266{
267 int ret;
268
269 if (s->guest_note) {
270 ret = f(s->guest_note, s->guest_note_size, s);
271 if (ret < 0) {
272 error_setg(errp, "dump: failed to write guest note");
273 }
274 }
275}
276
zhanghailiang4c7e2512014-10-09 14:13:11 +0800277static void write_elf64_notes(WriteCoreDumpFunction f, DumpState *s,
278 Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800279{
Andreas Färber0d342822012-12-17 07:12:13 +0100280 CPUState *cpu;
Wen Congyang783e9b42012-05-07 12:10:47 +0800281 int ret;
282 int id;
283
Andreas Färberbdc44642013-06-24 23:50:24 +0200284 CPU_FOREACH(cpu) {
Andreas Färber0d342822012-12-17 07:12:13 +0100285 id = cpu_index(cpu);
qiaonuohan6a519912014-02-18 14:11:26 +0800286 ret = cpu_write_elf64_note(f, cpu, id, s);
Wen Congyang783e9b42012-05-07 12:10:47 +0800287 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800288 error_setg(errp, "dump: failed to write elf notes");
zhanghailiang4c7e2512014-10-09 14:13:11 +0800289 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800290 }
291 }
292
Andreas Färberbdc44642013-06-24 23:50:24 +0200293 CPU_FOREACH(cpu) {
qiaonuohan6a519912014-02-18 14:11:26 +0800294 ret = cpu_write_elf64_qemunote(f, cpu, s);
Wen Congyang783e9b42012-05-07 12:10:47 +0800295 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800296 error_setg(errp, "dump: failed to write CPU status");
zhanghailiang4c7e2512014-10-09 14:13:11 +0800297 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800298 }
299 }
Marc-André Lureau903ef732017-09-11 18:59:25 +0200300
301 write_guest_note(f, s, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800302}
303
Janosch Frankbc7d5582022-03-30 12:36:01 +0000304static void write_elf32_phdr_note(DumpState *s, Elf32_Phdr *phdr)
Wen Congyang783e9b42012-05-07 12:10:47 +0800305{
Janosch Frankbc7d5582022-03-30 12:36:01 +0000306 memset(phdr, 0, sizeof(*phdr));
307 phdr->p_type = cpu_to_dump32(s, PT_NOTE);
308 phdr->p_offset = cpu_to_dump32(s, s->note_offset);
309 phdr->p_paddr = 0;
310 phdr->p_filesz = cpu_to_dump32(s, s->note_size);
311 phdr->p_memsz = cpu_to_dump32(s, s->note_size);
312 phdr->p_vaddr = 0;
Wen Congyang783e9b42012-05-07 12:10:47 +0800313}
314
zhanghailiang4c7e2512014-10-09 14:13:11 +0800315static void write_elf32_notes(WriteCoreDumpFunction f, DumpState *s,
316 Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800317{
Andreas Färber0d342822012-12-17 07:12:13 +0100318 CPUState *cpu;
Wen Congyang783e9b42012-05-07 12:10:47 +0800319 int ret;
320 int id;
321
Andreas Färberbdc44642013-06-24 23:50:24 +0200322 CPU_FOREACH(cpu) {
Andreas Färber0d342822012-12-17 07:12:13 +0100323 id = cpu_index(cpu);
qiaonuohan6a519912014-02-18 14:11:26 +0800324 ret = cpu_write_elf32_note(f, cpu, id, s);
Wen Congyang783e9b42012-05-07 12:10:47 +0800325 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800326 error_setg(errp, "dump: failed to write elf notes");
zhanghailiang4c7e2512014-10-09 14:13:11 +0800327 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800328 }
329 }
330
Andreas Färberbdc44642013-06-24 23:50:24 +0200331 CPU_FOREACH(cpu) {
qiaonuohan6a519912014-02-18 14:11:26 +0800332 ret = cpu_write_elf32_qemunote(f, cpu, s);
Wen Congyang783e9b42012-05-07 12:10:47 +0800333 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800334 error_setg(errp, "dump: failed to write CPU status");
zhanghailiang4c7e2512014-10-09 14:13:11 +0800335 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800336 }
337 }
Marc-André Lureau903ef732017-09-11 18:59:25 +0200338
339 write_guest_note(f, s, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800340}
341
Janosch Frankbc7d5582022-03-30 12:36:01 +0000342static void write_elf_phdr_note(DumpState *s, Error **errp)
343{
344 ERRP_GUARD();
345 Elf32_Phdr phdr32;
346 Elf64_Phdr phdr64;
347 void *phdr;
348 size_t size;
349 int ret;
350
351 if (dump_is_64bit(s)) {
352 write_elf64_phdr_note(s, &phdr64);
353 size = sizeof(phdr64);
354 phdr = &phdr64;
355 } else {
356 write_elf32_phdr_note(s, &phdr32);
357 size = sizeof(phdr32);
358 phdr = &phdr32;
359 }
360
361 ret = fd_write_vmcore(phdr, size, s);
362 if (ret < 0) {
363 error_setg_errno(errp, -ret,
364 "dump: failed to write program header table");
365 }
366}
367
zhanghailiang4c7e2512014-10-09 14:13:11 +0800368static void write_elf_section(DumpState *s, int type, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800369{
370 Elf32_Shdr shdr32;
371 Elf64_Shdr shdr64;
Wen Congyang783e9b42012-05-07 12:10:47 +0800372 int shdr_size;
373 void *shdr;
374 int ret;
375
376 if (type == 0) {
377 shdr_size = sizeof(Elf32_Shdr);
378 memset(&shdr32, 0, shdr_size);
Janosch Frank046bc412022-04-07 09:48:24 +0000379 shdr32.sh_info = cpu_to_dump32(s, s->phdr_num);
Wen Congyang783e9b42012-05-07 12:10:47 +0800380 shdr = &shdr32;
381 } else {
382 shdr_size = sizeof(Elf64_Shdr);
383 memset(&shdr64, 0, shdr_size);
Janosch Frank046bc412022-04-07 09:48:24 +0000384 shdr64.sh_info = cpu_to_dump32(s, s->phdr_num);
Wen Congyang783e9b42012-05-07 12:10:47 +0800385 shdr = &shdr64;
386 }
387
Peter Maydell174d2d62020-03-24 17:36:30 +0000388 ret = fd_write_vmcore(shdr, shdr_size, s);
Wen Congyang783e9b42012-05-07 12:10:47 +0800389 if (ret < 0) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200390 error_setg_errno(errp, -ret,
391 "dump: failed to write section header table");
Wen Congyang783e9b42012-05-07 12:10:47 +0800392 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800393}
394
zhanghailiang4c7e2512014-10-09 14:13:11 +0800395static void write_data(DumpState *s, void *buf, int length, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800396{
397 int ret;
398
399 ret = fd_write_vmcore(buf, length, s);
400 if (ret < 0) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200401 error_setg_errno(errp, -ret, "dump: failed to save memory");
Peter Xu2264c2c2016-02-18 13:16:53 +0800402 } else {
403 s->written_size += length;
Wen Congyang783e9b42012-05-07 12:10:47 +0800404 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800405}
406
zhanghailiang4c7e2512014-10-09 14:13:11 +0800407/* write the memory to vmcore. 1 page per I/O. */
408static void write_memory(DumpState *s, GuestPhysBlock *block, ram_addr_t start,
409 int64_t size, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800410{
Janosch Frank86a518b2022-03-30 12:35:55 +0000411 ERRP_GUARD();
Wen Congyang783e9b42012-05-07 12:10:47 +0800412 int64_t i;
Wen Congyang783e9b42012-05-07 12:10:47 +0800413
Andrew Jones8161bef2016-01-11 20:56:20 +0100414 for (i = 0; i < size / s->dump_info.page_size; i++) {
415 write_data(s, block->host_addr + start + i * s->dump_info.page_size,
Janosch Frank86a518b2022-03-30 12:35:55 +0000416 s->dump_info.page_size, errp);
417 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800418 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800419 }
420 }
421
Andrew Jones8161bef2016-01-11 20:56:20 +0100422 if ((size % s->dump_info.page_size) != 0) {
423 write_data(s, block->host_addr + start + i * s->dump_info.page_size,
Janosch Frank86a518b2022-03-30 12:35:55 +0000424 size % s->dump_info.page_size, errp);
425 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800426 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800427 }
428 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800429}
430
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200431/* get the memory's offset and size in the vmcore */
432static void get_offset_range(hwaddr phys_addr,
433 ram_addr_t mapping_length,
434 DumpState *s,
435 hwaddr *p_offset,
436 hwaddr *p_filesz)
Wen Congyang783e9b42012-05-07 12:10:47 +0800437{
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200438 GuestPhysBlock *block;
Avi Kivitya8170e52012-10-23 12:30:10 +0200439 hwaddr offset = s->memory_offset;
Wen Congyang783e9b42012-05-07 12:10:47 +0800440 int64_t size_in_block, start;
441
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200442 /* When the memory is not stored into vmcore, offset will be -1 */
443 *p_offset = -1;
444 *p_filesz = 0;
445
Wen Congyang783e9b42012-05-07 12:10:47 +0800446 if (s->has_filter) {
447 if (phys_addr < s->begin || phys_addr >= s->begin + s->length) {
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200448 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800449 }
450 }
451
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200452 QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
Wen Congyang783e9b42012-05-07 12:10:47 +0800453 if (s->has_filter) {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200454 if (block->target_start >= s->begin + s->length ||
455 block->target_end <= s->begin) {
Wen Congyang783e9b42012-05-07 12:10:47 +0800456 /* This block is out of the range */
457 continue;
458 }
459
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200460 if (s->begin <= block->target_start) {
461 start = block->target_start;
Wen Congyang783e9b42012-05-07 12:10:47 +0800462 } else {
463 start = s->begin;
464 }
465
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200466 size_in_block = block->target_end - start;
467 if (s->begin + s->length < block->target_end) {
468 size_in_block -= block->target_end - (s->begin + s->length);
Wen Congyang783e9b42012-05-07 12:10:47 +0800469 }
470 } else {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200471 start = block->target_start;
472 size_in_block = block->target_end - block->target_start;
Wen Congyang783e9b42012-05-07 12:10:47 +0800473 }
474
475 if (phys_addr >= start && phys_addr < start + size_in_block) {
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200476 *p_offset = phys_addr - start + offset;
477
478 /* The offset range mapped from the vmcore file must not spill over
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200479 * the GuestPhysBlock, clamp it. The rest of the mapping will be
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200480 * zero-filled in memory at load time; see
481 * <http://refspecs.linuxbase.org/elf/gabi4+/ch5.pheader.html>.
482 */
483 *p_filesz = phys_addr + mapping_length <= start + size_in_block ?
484 mapping_length :
485 size_in_block - (phys_addr - start);
486 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800487 }
488
489 offset += size_in_block;
490 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800491}
492
zhanghailiang4c7e2512014-10-09 14:13:11 +0800493static void write_elf_loads(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800494{
Janosch Frank86a518b2022-03-30 12:35:55 +0000495 ERRP_GUARD();
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200496 hwaddr offset, filesz;
Wen Congyang783e9b42012-05-07 12:10:47 +0800497 MemoryMapping *memory_mapping;
498 uint32_t phdr_index = 1;
Wen Congyang783e9b42012-05-07 12:10:47 +0800499
500 QTAILQ_FOREACH(memory_mapping, &s->list.head, next) {
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200501 get_offset_range(memory_mapping->phys_addr,
502 memory_mapping->length,
503 s, &offset, &filesz);
Janosch Frank05bbaa502022-03-30 12:36:00 +0000504 if (dump_is_64bit(s)) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800505 write_elf64_load(s, memory_mapping, phdr_index++, offset,
Janosch Frank86a518b2022-03-30 12:35:55 +0000506 filesz, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800507 } else {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800508 write_elf32_load(s, memory_mapping, phdr_index++, offset,
Janosch Frank86a518b2022-03-30 12:35:55 +0000509 filesz, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800510 }
511
Janosch Frank86a518b2022-03-30 12:35:55 +0000512 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800513 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800514 }
515
Janosch Frank046bc412022-04-07 09:48:24 +0000516 if (phdr_index >= s->phdr_num) {
Wen Congyang783e9b42012-05-07 12:10:47 +0800517 break;
518 }
519 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800520}
521
Janosch Frankc6812472022-03-30 12:36:03 +0000522static void write_elf_notes(DumpState *s, Error **errp)
523{
524 if (dump_is_64bit(s)) {
525 write_elf64_notes(fd_write_vmcore, s, errp);
526 } else {
527 write_elf32_notes(fd_write_vmcore, s, errp);
528 }
529}
530
Wen Congyang783e9b42012-05-07 12:10:47 +0800531/* write elf header, PT_NOTE and elf note to vmcore. */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800532static void dump_begin(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800533{
Janosch Frank86a518b2022-03-30 12:35:55 +0000534 ERRP_GUARD();
Wen Congyang783e9b42012-05-07 12:10:47 +0800535
536 /*
537 * the vmcore's format is:
538 * --------------
539 * | elf header |
540 * --------------
541 * | PT_NOTE |
542 * --------------
543 * | PT_LOAD |
544 * --------------
545 * | ...... |
546 * --------------
547 * | PT_LOAD |
548 * --------------
549 * | sec_hdr |
550 * --------------
551 * | elf note |
552 * --------------
553 * | memory |
554 * --------------
555 *
556 * we only know where the memory is saved after we write elf note into
557 * vmcore.
558 */
559
560 /* write elf header to vmcore */
Janosch Frank05bbaa502022-03-30 12:36:00 +0000561 if (dump_is_64bit(s)) {
Janosch Frank86a518b2022-03-30 12:35:55 +0000562 write_elf64_header(s, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800563 } else {
Janosch Frank86a518b2022-03-30 12:35:55 +0000564 write_elf32_header(s, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800565 }
Janosch Frank86a518b2022-03-30 12:35:55 +0000566 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800567 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800568 }
569
Janosch Frankbc7d5582022-03-30 12:36:01 +0000570 /* write PT_NOTE to vmcore */
571 write_elf_phdr_note(s, errp);
572 if (*errp) {
573 return;
574 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800575
Janosch Frank5ff2e5a2022-03-30 12:36:02 +0000576 /* write all PT_LOAD to vmcore */
577 write_elf_loads(s, errp);
578 if (*errp) {
579 return;
580 }
581
582 /* write section to vmcore */
583 if (s->shdr_num) {
584 write_elf_section(s, 1, errp);
Janosch Frank86a518b2022-03-30 12:35:55 +0000585 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800586 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800587 }
Janosch Frank5ff2e5a2022-03-30 12:36:02 +0000588 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800589
Janosch Frankc6812472022-03-30 12:36:03 +0000590 /* write notes to vmcore */
591 write_elf_notes(s, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800592}
593
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200594static int get_next_block(DumpState *s, GuestPhysBlock *block)
Wen Congyang783e9b42012-05-07 12:10:47 +0800595{
596 while (1) {
Paolo Bonzinia3161032012-11-14 15:54:48 +0100597 block = QTAILQ_NEXT(block, next);
Wen Congyang783e9b42012-05-07 12:10:47 +0800598 if (!block) {
599 /* no more block */
600 return 1;
601 }
602
603 s->start = 0;
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200604 s->next_block = block;
Wen Congyang783e9b42012-05-07 12:10:47 +0800605 if (s->has_filter) {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200606 if (block->target_start >= s->begin + s->length ||
607 block->target_end <= s->begin) {
Wen Congyang783e9b42012-05-07 12:10:47 +0800608 /* This block is out of the range */
609 continue;
610 }
611
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200612 if (s->begin > block->target_start) {
613 s->start = s->begin - block->target_start;
Wen Congyang783e9b42012-05-07 12:10:47 +0800614 }
615 }
616
617 return 0;
618 }
619}
620
621/* write all memory to vmcore */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800622static void dump_iterate(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800623{
Janosch Frank86a518b2022-03-30 12:35:55 +0000624 ERRP_GUARD();
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200625 GuestPhysBlock *block;
Wen Congyang783e9b42012-05-07 12:10:47 +0800626 int64_t size;
Wen Congyang783e9b42012-05-07 12:10:47 +0800627
Gonglei08a655b2014-10-30 14:01:17 +0800628 do {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200629 block = s->next_block;
Wen Congyang783e9b42012-05-07 12:10:47 +0800630
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200631 size = block->target_end - block->target_start;
Wen Congyang783e9b42012-05-07 12:10:47 +0800632 if (s->has_filter) {
633 size -= s->start;
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200634 if (s->begin + s->length < block->target_end) {
635 size -= block->target_end - (s->begin + s->length);
Wen Congyang783e9b42012-05-07 12:10:47 +0800636 }
637 }
Janosch Frank86a518b2022-03-30 12:35:55 +0000638 write_memory(s, block, s->start, size, errp);
639 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800640 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800641 }
642
Gonglei08a655b2014-10-30 14:01:17 +0800643 } while (!get_next_block(s, block));
Wen Congyang783e9b42012-05-07 12:10:47 +0800644}
645
zhanghailiang4c7e2512014-10-09 14:13:11 +0800646static void create_vmcore(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800647{
Janosch Frank86a518b2022-03-30 12:35:55 +0000648 ERRP_GUARD();
Wen Congyang783e9b42012-05-07 12:10:47 +0800649
Janosch Frank86a518b2022-03-30 12:35:55 +0000650 dump_begin(s, errp);
651 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800652 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800653 }
654
zhanghailiang4c7e2512014-10-09 14:13:11 +0800655 dump_iterate(s, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800656}
657
qiaonuohanfda05382014-02-18 14:11:27 +0800658static int write_start_flat_header(int fd)
659{
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200660 MakedumpfileHeader *mh;
qiaonuohanfda05382014-02-18 14:11:27 +0800661 int ret = 0;
662
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200663 QEMU_BUILD_BUG_ON(sizeof *mh > MAX_SIZE_MDF_HEADER);
664 mh = g_malloc0(MAX_SIZE_MDF_HEADER);
qiaonuohanfda05382014-02-18 14:11:27 +0800665
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200666 memcpy(mh->signature, MAKEDUMPFILE_SIGNATURE,
667 MIN(sizeof mh->signature, sizeof MAKEDUMPFILE_SIGNATURE));
qiaonuohanfda05382014-02-18 14:11:27 +0800668
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200669 mh->type = cpu_to_be64(TYPE_FLAT_HEADER);
670 mh->version = cpu_to_be64(VERSION_FLAT_HEADER);
qiaonuohanfda05382014-02-18 14:11:27 +0800671
672 size_t written_size;
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200673 written_size = qemu_write_full(fd, mh, MAX_SIZE_MDF_HEADER);
qiaonuohanfda05382014-02-18 14:11:27 +0800674 if (written_size != MAX_SIZE_MDF_HEADER) {
675 ret = -1;
676 }
677
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200678 g_free(mh);
qiaonuohanfda05382014-02-18 14:11:27 +0800679 return ret;
680}
681
682static int write_end_flat_header(int fd)
683{
684 MakedumpfileDataHeader mdh;
685
686 mdh.offset = END_FLAG_FLAT_HEADER;
687 mdh.buf_size = END_FLAG_FLAT_HEADER;
688
689 size_t written_size;
690 written_size = qemu_write_full(fd, &mdh, sizeof(mdh));
691 if (written_size != sizeof(mdh)) {
692 return -1;
693 }
694
695 return 0;
696}
697
qiaonuohan5d31bab2014-02-18 14:11:28 +0800698static int write_buffer(int fd, off_t offset, const void *buf, size_t size)
699{
700 size_t written_size;
701 MakedumpfileDataHeader mdh;
702
703 mdh.offset = cpu_to_be64(offset);
704 mdh.buf_size = cpu_to_be64(size);
705
706 written_size = qemu_write_full(fd, &mdh, sizeof(mdh));
707 if (written_size != sizeof(mdh)) {
708 return -1;
709 }
710
711 written_size = qemu_write_full(fd, buf, size);
712 if (written_size != size) {
713 return -1;
714 }
715
716 return 0;
717}
718
qiaonuohan4835ef72014-02-18 14:11:29 +0800719static int buf_write_note(const void *buf, size_t size, void *opaque)
720{
721 DumpState *s = opaque;
722
723 /* note_buf is not enough */
724 if (s->note_buf_offset + size > s->note_size) {
725 return -1;
726 }
727
728 memcpy(s->note_buf + s->note_buf_offset, buf, size);
729
730 s->note_buf_offset += size;
731
732 return 0;
733}
734
Marc-André Lureau903ef732017-09-11 18:59:25 +0200735/*
736 * This function retrieves various sizes from an elf header.
737 *
738 * @note has to be a valid ELF note. The return sizes are unmodified
739 * (not padded or rounded up to be multiple of 4).
740 */
741static void get_note_sizes(DumpState *s, const void *note,
742 uint64_t *note_head_size,
743 uint64_t *name_size,
744 uint64_t *desc_size)
745{
746 uint64_t note_head_sz;
747 uint64_t name_sz;
748 uint64_t desc_sz;
749
Janosch Frank05bbaa502022-03-30 12:36:00 +0000750 if (dump_is_64bit(s)) {
Marc-André Lureau903ef732017-09-11 18:59:25 +0200751 const Elf64_Nhdr *hdr = note;
752 note_head_sz = sizeof(Elf64_Nhdr);
753 name_sz = tswap64(hdr->n_namesz);
754 desc_sz = tswap64(hdr->n_descsz);
755 } else {
756 const Elf32_Nhdr *hdr = note;
757 note_head_sz = sizeof(Elf32_Nhdr);
758 name_sz = tswap32(hdr->n_namesz);
759 desc_sz = tswap32(hdr->n_descsz);
760 }
761
762 if (note_head_size) {
763 *note_head_size = note_head_sz;
764 }
765 if (name_size) {
766 *name_size = name_sz;
767 }
768 if (desc_size) {
769 *desc_size = desc_sz;
770 }
771}
772
Marc-André Lureaud9feb512017-09-11 18:59:26 +0200773static bool note_name_equal(DumpState *s,
774 const uint8_t *note, const char *name)
775{
776 int len = strlen(name) + 1;
777 uint64_t head_size, name_size;
778
779 get_note_sizes(s, note, &head_size, &name_size, NULL);
780 head_size = ROUND_UP(head_size, 4);
781
Marc-André Lureauc983ca82017-12-12 15:53:59 +0100782 return name_size == len && memcmp(note + head_size, name, len) == 0;
Marc-André Lureaud9feb512017-09-11 18:59:26 +0200783}
784
qiaonuohan298f1162014-02-18 14:11:32 +0800785/* write common header, sub header and elf note to vmcore */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800786static void create_header32(DumpState *s, Error **errp)
qiaonuohan298f1162014-02-18 14:11:32 +0800787{
Janosch Frank86a518b2022-03-30 12:35:55 +0000788 ERRP_GUARD();
qiaonuohan298f1162014-02-18 14:11:32 +0800789 DiskDumpHeader32 *dh = NULL;
790 KdumpSubHeader32 *kh = NULL;
791 size_t size;
qiaonuohan298f1162014-02-18 14:11:32 +0800792 uint32_t block_size;
793 uint32_t sub_hdr_size;
794 uint32_t bitmap_blocks;
795 uint32_t status = 0;
796 uint64_t offset_note;
797
798 /* write common header, the version of kdump-compressed format is 6th */
799 size = sizeof(DiskDumpHeader32);
800 dh = g_malloc0(size);
801
Eric Blake84c868f2018-03-27 15:21:51 -0500802 memcpy(dh->signature, KDUMP_SIGNATURE, SIG_LEN);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200803 dh->header_version = cpu_to_dump32(s, 6);
Andrew Jones8161bef2016-01-11 20:56:20 +0100804 block_size = s->dump_info.page_size;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200805 dh->block_size = cpu_to_dump32(s, block_size);
qiaonuohan298f1162014-02-18 14:11:32 +0800806 sub_hdr_size = sizeof(struct KdumpSubHeader32) + s->note_size;
807 sub_hdr_size = DIV_ROUND_UP(sub_hdr_size, block_size);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200808 dh->sub_hdr_size = cpu_to_dump32(s, sub_hdr_size);
qiaonuohan298f1162014-02-18 14:11:32 +0800809 /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200810 dh->max_mapnr = cpu_to_dump32(s, MIN(s->max_mapnr, UINT_MAX));
811 dh->nr_cpus = cpu_to_dump32(s, s->nr_cpus);
qiaonuohan298f1162014-02-18 14:11:32 +0800812 bitmap_blocks = DIV_ROUND_UP(s->len_dump_bitmap, block_size) * 2;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200813 dh->bitmap_blocks = cpu_to_dump32(s, bitmap_blocks);
qiaonuohan4ab23a92014-02-18 14:11:37 +0800814 strncpy(dh->utsname.machine, ELF_MACHINE_UNAME, sizeof(dh->utsname.machine));
qiaonuohan298f1162014-02-18 14:11:32 +0800815
816 if (s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) {
817 status |= DUMP_DH_COMPRESSED_ZLIB;
818 }
819#ifdef CONFIG_LZO
820 if (s->flag_compress & DUMP_DH_COMPRESSED_LZO) {
821 status |= DUMP_DH_COMPRESSED_LZO;
822 }
823#endif
824#ifdef CONFIG_SNAPPY
825 if (s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) {
826 status |= DUMP_DH_COMPRESSED_SNAPPY;
827 }
828#endif
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200829 dh->status = cpu_to_dump32(s, status);
qiaonuohan298f1162014-02-18 14:11:32 +0800830
831 if (write_buffer(s->fd, 0, dh, size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800832 error_setg(errp, "dump: failed to write disk dump header");
qiaonuohan298f1162014-02-18 14:11:32 +0800833 goto out;
834 }
835
836 /* write sub header */
837 size = sizeof(KdumpSubHeader32);
838 kh = g_malloc0(size);
839
840 /* 64bit max_mapnr_64 */
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200841 kh->max_mapnr_64 = cpu_to_dump64(s, s->max_mapnr);
Andrew Jonesb6e05aa2016-01-11 20:56:21 +0100842 kh->phys_base = cpu_to_dump32(s, s->dump_info.phys_base);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200843 kh->dump_level = cpu_to_dump32(s, DUMP_LEVEL);
qiaonuohan298f1162014-02-18 14:11:32 +0800844
845 offset_note = DISKDUMP_HEADER_BLOCKS * block_size + size;
Marc-André Lureau9ada5752017-09-11 18:59:27 +0200846 if (s->guest_note &&
847 note_name_equal(s, s->guest_note, "VMCOREINFO")) {
848 uint64_t hsize, name_size, size_vmcoreinfo_desc, offset_vmcoreinfo;
849
850 get_note_sizes(s, s->guest_note,
851 &hsize, &name_size, &size_vmcoreinfo_desc);
852 offset_vmcoreinfo = offset_note + s->note_size - s->guest_note_size +
853 (DIV_ROUND_UP(hsize, 4) + DIV_ROUND_UP(name_size, 4)) * 4;
854 kh->offset_vmcoreinfo = cpu_to_dump64(s, offset_vmcoreinfo);
855 kh->size_vmcoreinfo = cpu_to_dump32(s, size_vmcoreinfo_desc);
856 }
857
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200858 kh->offset_note = cpu_to_dump64(s, offset_note);
859 kh->note_size = cpu_to_dump32(s, s->note_size);
qiaonuohan298f1162014-02-18 14:11:32 +0800860
861 if (write_buffer(s->fd, DISKDUMP_HEADER_BLOCKS *
862 block_size, kh, size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800863 error_setg(errp, "dump: failed to write kdump sub header");
qiaonuohan298f1162014-02-18 14:11:32 +0800864 goto out;
865 }
866
867 /* write note */
868 s->note_buf = g_malloc0(s->note_size);
869 s->note_buf_offset = 0;
870
871 /* use s->note_buf to store notes temporarily */
Janosch Frank86a518b2022-03-30 12:35:55 +0000872 write_elf32_notes(buf_write_note, s, errp);
873 if (*errp) {
qiaonuohan298f1162014-02-18 14:11:32 +0800874 goto out;
875 }
qiaonuohan298f1162014-02-18 14:11:32 +0800876 if (write_buffer(s->fd, offset_note, s->note_buf,
877 s->note_size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800878 error_setg(errp, "dump: failed to write notes");
qiaonuohan298f1162014-02-18 14:11:32 +0800879 goto out;
880 }
881
882 /* get offset of dump_bitmap */
883 s->offset_dump_bitmap = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size) *
884 block_size;
885
886 /* get offset of page */
887 s->offset_page = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size + bitmap_blocks) *
888 block_size;
889
890out:
891 g_free(dh);
892 g_free(kh);
893 g_free(s->note_buf);
qiaonuohan298f1162014-02-18 14:11:32 +0800894}
895
896/* write common header, sub header and elf note to vmcore */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800897static void create_header64(DumpState *s, Error **errp)
qiaonuohan298f1162014-02-18 14:11:32 +0800898{
Janosch Frank86a518b2022-03-30 12:35:55 +0000899 ERRP_GUARD();
qiaonuohan298f1162014-02-18 14:11:32 +0800900 DiskDumpHeader64 *dh = NULL;
901 KdumpSubHeader64 *kh = NULL;
902 size_t size;
qiaonuohan298f1162014-02-18 14:11:32 +0800903 uint32_t block_size;
904 uint32_t sub_hdr_size;
905 uint32_t bitmap_blocks;
906 uint32_t status = 0;
907 uint64_t offset_note;
908
909 /* write common header, the version of kdump-compressed format is 6th */
910 size = sizeof(DiskDumpHeader64);
911 dh = g_malloc0(size);
912
Eric Blake84c868f2018-03-27 15:21:51 -0500913 memcpy(dh->signature, KDUMP_SIGNATURE, SIG_LEN);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200914 dh->header_version = cpu_to_dump32(s, 6);
Andrew Jones8161bef2016-01-11 20:56:20 +0100915 block_size = s->dump_info.page_size;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200916 dh->block_size = cpu_to_dump32(s, block_size);
qiaonuohan298f1162014-02-18 14:11:32 +0800917 sub_hdr_size = sizeof(struct KdumpSubHeader64) + s->note_size;
918 sub_hdr_size = DIV_ROUND_UP(sub_hdr_size, block_size);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200919 dh->sub_hdr_size = cpu_to_dump32(s, sub_hdr_size);
qiaonuohan298f1162014-02-18 14:11:32 +0800920 /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200921 dh->max_mapnr = cpu_to_dump32(s, MIN(s->max_mapnr, UINT_MAX));
922 dh->nr_cpus = cpu_to_dump32(s, s->nr_cpus);
qiaonuohan298f1162014-02-18 14:11:32 +0800923 bitmap_blocks = DIV_ROUND_UP(s->len_dump_bitmap, block_size) * 2;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200924 dh->bitmap_blocks = cpu_to_dump32(s, bitmap_blocks);
qiaonuohan4ab23a92014-02-18 14:11:37 +0800925 strncpy(dh->utsname.machine, ELF_MACHINE_UNAME, sizeof(dh->utsname.machine));
qiaonuohan298f1162014-02-18 14:11:32 +0800926
927 if (s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) {
928 status |= DUMP_DH_COMPRESSED_ZLIB;
929 }
930#ifdef CONFIG_LZO
931 if (s->flag_compress & DUMP_DH_COMPRESSED_LZO) {
932 status |= DUMP_DH_COMPRESSED_LZO;
933 }
934#endif
935#ifdef CONFIG_SNAPPY
936 if (s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) {
937 status |= DUMP_DH_COMPRESSED_SNAPPY;
938 }
939#endif
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200940 dh->status = cpu_to_dump32(s, status);
qiaonuohan298f1162014-02-18 14:11:32 +0800941
942 if (write_buffer(s->fd, 0, dh, size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800943 error_setg(errp, "dump: failed to write disk dump header");
qiaonuohan298f1162014-02-18 14:11:32 +0800944 goto out;
945 }
946
947 /* write sub header */
948 size = sizeof(KdumpSubHeader64);
949 kh = g_malloc0(size);
950
951 /* 64bit max_mapnr_64 */
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200952 kh->max_mapnr_64 = cpu_to_dump64(s, s->max_mapnr);
Andrew Jonesb6e05aa2016-01-11 20:56:21 +0100953 kh->phys_base = cpu_to_dump64(s, s->dump_info.phys_base);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200954 kh->dump_level = cpu_to_dump32(s, DUMP_LEVEL);
qiaonuohan298f1162014-02-18 14:11:32 +0800955
956 offset_note = DISKDUMP_HEADER_BLOCKS * block_size + size;
Marc-André Lureau9ada5752017-09-11 18:59:27 +0200957 if (s->guest_note &&
958 note_name_equal(s, s->guest_note, "VMCOREINFO")) {
959 uint64_t hsize, name_size, size_vmcoreinfo_desc, offset_vmcoreinfo;
960
961 get_note_sizes(s, s->guest_note,
962 &hsize, &name_size, &size_vmcoreinfo_desc);
963 offset_vmcoreinfo = offset_note + s->note_size - s->guest_note_size +
964 (DIV_ROUND_UP(hsize, 4) + DIV_ROUND_UP(name_size, 4)) * 4;
965 kh->offset_vmcoreinfo = cpu_to_dump64(s, offset_vmcoreinfo);
966 kh->size_vmcoreinfo = cpu_to_dump64(s, size_vmcoreinfo_desc);
967 }
968
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200969 kh->offset_note = cpu_to_dump64(s, offset_note);
970 kh->note_size = cpu_to_dump64(s, s->note_size);
qiaonuohan298f1162014-02-18 14:11:32 +0800971
972 if (write_buffer(s->fd, DISKDUMP_HEADER_BLOCKS *
973 block_size, kh, size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800974 error_setg(errp, "dump: failed to write kdump sub header");
qiaonuohan298f1162014-02-18 14:11:32 +0800975 goto out;
976 }
977
978 /* write note */
979 s->note_buf = g_malloc0(s->note_size);
980 s->note_buf_offset = 0;
981
982 /* use s->note_buf to store notes temporarily */
Janosch Frank86a518b2022-03-30 12:35:55 +0000983 write_elf64_notes(buf_write_note, s, errp);
984 if (*errp) {
qiaonuohan298f1162014-02-18 14:11:32 +0800985 goto out;
986 }
987
988 if (write_buffer(s->fd, offset_note, s->note_buf,
989 s->note_size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800990 error_setg(errp, "dump: failed to write notes");
qiaonuohan298f1162014-02-18 14:11:32 +0800991 goto out;
992 }
993
994 /* get offset of dump_bitmap */
995 s->offset_dump_bitmap = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size) *
996 block_size;
997
998 /* get offset of page */
999 s->offset_page = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size + bitmap_blocks) *
1000 block_size;
1001
1002out:
1003 g_free(dh);
1004 g_free(kh);
1005 g_free(s->note_buf);
qiaonuohan298f1162014-02-18 14:11:32 +08001006}
1007
zhanghailiang4c7e2512014-10-09 14:13:11 +08001008static void write_dump_header(DumpState *s, Error **errp)
qiaonuohan298f1162014-02-18 14:11:32 +08001009{
Janosch Frank05bbaa502022-03-30 12:36:00 +00001010 if (dump_is_64bit(s)) {
Markus Armbruster992861f2020-07-07 18:06:04 +02001011 create_header64(s, errp);
Janosch Frank05bbaa502022-03-30 12:36:00 +00001012 } else {
1013 create_header32(s, errp);
zhanghailiang4c7e2512014-10-09 14:13:11 +08001014 }
qiaonuohan298f1162014-02-18 14:11:32 +08001015}
1016
Andrew Jones8161bef2016-01-11 20:56:20 +01001017static size_t dump_bitmap_get_bufsize(DumpState *s)
1018{
1019 return s->dump_info.page_size;
1020}
1021
qiaonuohand0686c72014-02-18 14:11:33 +08001022/*
1023 * set dump_bitmap sequencely. the bit before last_pfn is not allowed to be
1024 * rewritten, so if need to set the first bit, set last_pfn and pfn to 0.
1025 * set_dump_bitmap will always leave the recently set bit un-sync. And setting
1026 * (last bit + sizeof(buf) * 8) to 0 will do flushing the content in buf into
1027 * vmcore, ie. synchronizing un-sync bit into vmcore.
1028 */
1029static int set_dump_bitmap(uint64_t last_pfn, uint64_t pfn, bool value,
1030 uint8_t *buf, DumpState *s)
1031{
1032 off_t old_offset, new_offset;
1033 off_t offset_bitmap1, offset_bitmap2;
1034 uint32_t byte, bit;
Andrew Jones8161bef2016-01-11 20:56:20 +01001035 size_t bitmap_bufsize = dump_bitmap_get_bufsize(s);
1036 size_t bits_per_buf = bitmap_bufsize * CHAR_BIT;
qiaonuohand0686c72014-02-18 14:11:33 +08001037
1038 /* should not set the previous place */
1039 assert(last_pfn <= pfn);
1040
1041 /*
1042 * if the bit needed to be set is not cached in buf, flush the data in buf
1043 * to vmcore firstly.
1044 * making new_offset be bigger than old_offset can also sync remained data
1045 * into vmcore.
1046 */
Andrew Jones8161bef2016-01-11 20:56:20 +01001047 old_offset = bitmap_bufsize * (last_pfn / bits_per_buf);
1048 new_offset = bitmap_bufsize * (pfn / bits_per_buf);
qiaonuohand0686c72014-02-18 14:11:33 +08001049
1050 while (old_offset < new_offset) {
1051 /* calculate the offset and write dump_bitmap */
1052 offset_bitmap1 = s->offset_dump_bitmap + old_offset;
1053 if (write_buffer(s->fd, offset_bitmap1, buf,
Andrew Jones8161bef2016-01-11 20:56:20 +01001054 bitmap_bufsize) < 0) {
qiaonuohand0686c72014-02-18 14:11:33 +08001055 return -1;
1056 }
1057
1058 /* dump level 1 is chosen, so 1st and 2nd bitmap are same */
1059 offset_bitmap2 = s->offset_dump_bitmap + s->len_dump_bitmap +
1060 old_offset;
1061 if (write_buffer(s->fd, offset_bitmap2, buf,
Andrew Jones8161bef2016-01-11 20:56:20 +01001062 bitmap_bufsize) < 0) {
qiaonuohand0686c72014-02-18 14:11:33 +08001063 return -1;
1064 }
1065
Andrew Jones8161bef2016-01-11 20:56:20 +01001066 memset(buf, 0, bitmap_bufsize);
1067 old_offset += bitmap_bufsize;
qiaonuohand0686c72014-02-18 14:11:33 +08001068 }
1069
1070 /* get the exact place of the bit in the buf, and set it */
Andrew Jones8161bef2016-01-11 20:56:20 +01001071 byte = (pfn % bits_per_buf) / CHAR_BIT;
1072 bit = (pfn % bits_per_buf) % CHAR_BIT;
qiaonuohand0686c72014-02-18 14:11:33 +08001073 if (value) {
1074 buf[byte] |= 1u << bit;
1075 } else {
1076 buf[byte] &= ~(1u << bit);
1077 }
1078
1079 return 0;
1080}
1081
Andrew Jones8161bef2016-01-11 20:56:20 +01001082static uint64_t dump_paddr_to_pfn(DumpState *s, uint64_t addr)
1083{
1084 int target_page_shift = ctz32(s->dump_info.page_size);
1085
1086 return (addr >> target_page_shift) - ARCH_PFN_OFFSET;
1087}
1088
1089static uint64_t dump_pfn_to_paddr(DumpState *s, uint64_t pfn)
1090{
1091 int target_page_shift = ctz32(s->dump_info.page_size);
1092
1093 return (pfn + ARCH_PFN_OFFSET) << target_page_shift;
1094}
1095
qiaonuohand0686c72014-02-18 14:11:33 +08001096/*
1097 * exam every page and return the page frame number and the address of the page.
1098 * bufptr can be NULL. note: the blocks here is supposed to reflect guest-phys
1099 * blocks, so block->target_start and block->target_end should be interal
1100 * multiples of the target page size.
1101 */
1102static bool get_next_page(GuestPhysBlock **blockptr, uint64_t *pfnptr,
1103 uint8_t **bufptr, DumpState *s)
1104{
1105 GuestPhysBlock *block = *blockptr;
Andrew Jones8161bef2016-01-11 20:56:20 +01001106 hwaddr addr, target_page_mask = ~((hwaddr)s->dump_info.page_size - 1);
qiaonuohand0686c72014-02-18 14:11:33 +08001107 uint8_t *buf;
1108
1109 /* block == NULL means the start of the iteration */
1110 if (!block) {
1111 block = QTAILQ_FIRST(&s->guest_phys_blocks.head);
1112 *blockptr = block;
Andrew Jones8161bef2016-01-11 20:56:20 +01001113 assert((block->target_start & ~target_page_mask) == 0);
1114 assert((block->target_end & ~target_page_mask) == 0);
1115 *pfnptr = dump_paddr_to_pfn(s, block->target_start);
qiaonuohand0686c72014-02-18 14:11:33 +08001116 if (bufptr) {
1117 *bufptr = block->host_addr;
1118 }
1119 return true;
1120 }
1121
1122 *pfnptr = *pfnptr + 1;
Andrew Jones8161bef2016-01-11 20:56:20 +01001123 addr = dump_pfn_to_paddr(s, *pfnptr);
qiaonuohand0686c72014-02-18 14:11:33 +08001124
1125 if ((addr >= block->target_start) &&
Andrew Jones8161bef2016-01-11 20:56:20 +01001126 (addr + s->dump_info.page_size <= block->target_end)) {
qiaonuohand0686c72014-02-18 14:11:33 +08001127 buf = block->host_addr + (addr - block->target_start);
1128 } else {
1129 /* the next page is in the next block */
1130 block = QTAILQ_NEXT(block, next);
1131 *blockptr = block;
1132 if (!block) {
1133 return false;
1134 }
Andrew Jones8161bef2016-01-11 20:56:20 +01001135 assert((block->target_start & ~target_page_mask) == 0);
1136 assert((block->target_end & ~target_page_mask) == 0);
1137 *pfnptr = dump_paddr_to_pfn(s, block->target_start);
qiaonuohand0686c72014-02-18 14:11:33 +08001138 buf = block->host_addr;
1139 }
1140
1141 if (bufptr) {
1142 *bufptr = buf;
1143 }
1144
1145 return true;
1146}
1147
zhanghailiang4c7e2512014-10-09 14:13:11 +08001148static void write_dump_bitmap(DumpState *s, Error **errp)
qiaonuohand0686c72014-02-18 14:11:33 +08001149{
1150 int ret = 0;
1151 uint64_t last_pfn, pfn;
1152 void *dump_bitmap_buf;
1153 size_t num_dumpable;
1154 GuestPhysBlock *block_iter = NULL;
Andrew Jones8161bef2016-01-11 20:56:20 +01001155 size_t bitmap_bufsize = dump_bitmap_get_bufsize(s);
1156 size_t bits_per_buf = bitmap_bufsize * CHAR_BIT;
qiaonuohand0686c72014-02-18 14:11:33 +08001157
1158 /* dump_bitmap_buf is used to store dump_bitmap temporarily */
Andrew Jones8161bef2016-01-11 20:56:20 +01001159 dump_bitmap_buf = g_malloc0(bitmap_bufsize);
qiaonuohand0686c72014-02-18 14:11:33 +08001160
1161 num_dumpable = 0;
1162 last_pfn = 0;
1163
1164 /*
1165 * exam memory page by page, and set the bit in dump_bitmap corresponded
1166 * to the existing page.
1167 */
1168 while (get_next_page(&block_iter, &pfn, NULL, s)) {
1169 ret = set_dump_bitmap(last_pfn, pfn, true, dump_bitmap_buf, s);
1170 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001171 error_setg(errp, "dump: failed to set dump_bitmap");
qiaonuohand0686c72014-02-18 14:11:33 +08001172 goto out;
1173 }
1174
1175 last_pfn = pfn;
1176 num_dumpable++;
1177 }
1178
1179 /*
1180 * set_dump_bitmap will always leave the recently set bit un-sync. Here we
Andrew Jones8161bef2016-01-11 20:56:20 +01001181 * set the remaining bits from last_pfn to the end of the bitmap buffer to
1182 * 0. With those set, the un-sync bit will be synchronized into the vmcore.
qiaonuohand0686c72014-02-18 14:11:33 +08001183 */
1184 if (num_dumpable > 0) {
Andrew Jones8161bef2016-01-11 20:56:20 +01001185 ret = set_dump_bitmap(last_pfn, last_pfn + bits_per_buf, false,
qiaonuohand0686c72014-02-18 14:11:33 +08001186 dump_bitmap_buf, s);
1187 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001188 error_setg(errp, "dump: failed to sync dump_bitmap");
qiaonuohand0686c72014-02-18 14:11:33 +08001189 goto out;
1190 }
1191 }
1192
1193 /* number of dumpable pages that will be dumped later */
1194 s->num_dumpable = num_dumpable;
1195
1196out:
1197 g_free(dump_bitmap_buf);
qiaonuohand0686c72014-02-18 14:11:33 +08001198}
1199
qiaonuohan64cfba62014-02-18 14:11:34 +08001200static void prepare_data_cache(DataCache *data_cache, DumpState *s,
1201 off_t offset)
1202{
1203 data_cache->fd = s->fd;
1204 data_cache->data_size = 0;
Andrew Jones8161bef2016-01-11 20:56:20 +01001205 data_cache->buf_size = 4 * dump_bitmap_get_bufsize(s);
1206 data_cache->buf = g_malloc0(data_cache->buf_size);
qiaonuohan64cfba62014-02-18 14:11:34 +08001207 data_cache->offset = offset;
1208}
1209
1210static int write_cache(DataCache *dc, const void *buf, size_t size,
1211 bool flag_sync)
1212{
1213 /*
1214 * dc->buf_size should not be less than size, otherwise dc will never be
1215 * enough
1216 */
1217 assert(size <= dc->buf_size);
1218
1219 /*
1220 * if flag_sync is set, synchronize data in dc->buf into vmcore.
1221 * otherwise check if the space is enough for caching data in buf, if not,
1222 * write the data in dc->buf to dc->fd and reset dc->buf
1223 */
1224 if ((!flag_sync && dc->data_size + size > dc->buf_size) ||
1225 (flag_sync && dc->data_size > 0)) {
1226 if (write_buffer(dc->fd, dc->offset, dc->buf, dc->data_size) < 0) {
1227 return -1;
1228 }
1229
1230 dc->offset += dc->data_size;
1231 dc->data_size = 0;
1232 }
1233
1234 if (!flag_sync) {
1235 memcpy(dc->buf + dc->data_size, buf, size);
1236 dc->data_size += size;
1237 }
1238
1239 return 0;
1240}
1241
1242static void free_data_cache(DataCache *data_cache)
1243{
1244 g_free(data_cache->buf);
1245}
1246
qiaonuohand12f57e2014-02-18 14:11:35 +08001247static size_t get_len_buf_out(size_t page_size, uint32_t flag_compress)
1248{
Laszlo Ersekb87ef352014-05-20 13:42:53 +02001249 switch (flag_compress) {
1250 case DUMP_DH_COMPRESSED_ZLIB:
1251 return compressBound(page_size);
qiaonuohand12f57e2014-02-18 14:11:35 +08001252
Laszlo Ersekb87ef352014-05-20 13:42:53 +02001253 case DUMP_DH_COMPRESSED_LZO:
1254 /*
1255 * LZO will expand incompressible data by a little amount. Please check
1256 * the following URL to see the expansion calculation:
1257 * http://www.oberhumer.com/opensource/lzo/lzofaq.php
1258 */
1259 return page_size + page_size / 16 + 64 + 3;
qiaonuohand12f57e2014-02-18 14:11:35 +08001260
1261#ifdef CONFIG_SNAPPY
Laszlo Ersekb87ef352014-05-20 13:42:53 +02001262 case DUMP_DH_COMPRESSED_SNAPPY:
1263 return snappy_max_compressed_length(page_size);
qiaonuohand12f57e2014-02-18 14:11:35 +08001264#endif
Laszlo Ersekb87ef352014-05-20 13:42:53 +02001265 }
1266 return 0;
qiaonuohand12f57e2014-02-18 14:11:35 +08001267}
1268
zhanghailiang4c7e2512014-10-09 14:13:11 +08001269static void write_dump_pages(DumpState *s, Error **errp)
qiaonuohand12f57e2014-02-18 14:11:35 +08001270{
1271 int ret = 0;
1272 DataCache page_desc, page_data;
1273 size_t len_buf_out, size_out;
1274#ifdef CONFIG_LZO
1275 lzo_bytep wrkmem = NULL;
1276#endif
1277 uint8_t *buf_out = NULL;
1278 off_t offset_desc, offset_data;
1279 PageDescriptor pd, pd_zero;
1280 uint8_t *buf;
qiaonuohand12f57e2014-02-18 14:11:35 +08001281 GuestPhysBlock *block_iter = NULL;
1282 uint64_t pfn_iter;
1283
1284 /* get offset of page_desc and page_data in dump file */
1285 offset_desc = s->offset_page;
1286 offset_data = offset_desc + sizeof(PageDescriptor) * s->num_dumpable;
1287
1288 prepare_data_cache(&page_desc, s, offset_desc);
1289 prepare_data_cache(&page_data, s, offset_data);
1290
1291 /* prepare buffer to store compressed data */
Andrew Jones8161bef2016-01-11 20:56:20 +01001292 len_buf_out = get_len_buf_out(s->dump_info.page_size, s->flag_compress);
Laszlo Ersekb87ef352014-05-20 13:42:53 +02001293 assert(len_buf_out != 0);
qiaonuohand12f57e2014-02-18 14:11:35 +08001294
1295#ifdef CONFIG_LZO
1296 wrkmem = g_malloc(LZO1X_1_MEM_COMPRESS);
1297#endif
1298
1299 buf_out = g_malloc(len_buf_out);
1300
1301 /*
1302 * init zero page's page_desc and page_data, because every zero page
1303 * uses the same page_data
1304 */
Andrew Jones8161bef2016-01-11 20:56:20 +01001305 pd_zero.size = cpu_to_dump32(s, s->dump_info.page_size);
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001306 pd_zero.flags = cpu_to_dump32(s, 0);
1307 pd_zero.offset = cpu_to_dump64(s, offset_data);
1308 pd_zero.page_flags = cpu_to_dump64(s, 0);
Andrew Jones8161bef2016-01-11 20:56:20 +01001309 buf = g_malloc0(s->dump_info.page_size);
1310 ret = write_cache(&page_data, buf, s->dump_info.page_size, false);
qiaonuohand12f57e2014-02-18 14:11:35 +08001311 g_free(buf);
1312 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001313 error_setg(errp, "dump: failed to write page data (zero page)");
qiaonuohand12f57e2014-02-18 14:11:35 +08001314 goto out;
1315 }
1316
Andrew Jones8161bef2016-01-11 20:56:20 +01001317 offset_data += s->dump_info.page_size;
qiaonuohand12f57e2014-02-18 14:11:35 +08001318
1319 /*
1320 * dump memory to vmcore page by page. zero page will all be resided in the
1321 * first page of page section
1322 */
1323 while (get_next_page(&block_iter, &pfn_iter, &buf, s)) {
1324 /* check zero page */
Juan Quintelaf13f22b2021-11-18 15:57:44 +01001325 if (buffer_is_zero(buf, s->dump_info.page_size)) {
qiaonuohand12f57e2014-02-18 14:11:35 +08001326 ret = write_cache(&page_desc, &pd_zero, sizeof(PageDescriptor),
1327 false);
1328 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001329 error_setg(errp, "dump: failed to write page desc");
qiaonuohand12f57e2014-02-18 14:11:35 +08001330 goto out;
1331 }
1332 } else {
1333 /*
1334 * not zero page, then:
1335 * 1. compress the page
1336 * 2. write the compressed page into the cache of page_data
1337 * 3. get page desc of the compressed page and write it into the
1338 * cache of page_desc
1339 *
1340 * only one compression format will be used here, for
1341 * s->flag_compress is set. But when compression fails to work,
1342 * we fall back to save in plaintext.
1343 */
1344 size_out = len_buf_out;
1345 if ((s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) &&
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001346 (compress2(buf_out, (uLongf *)&size_out, buf,
Andrew Jones8161bef2016-01-11 20:56:20 +01001347 s->dump_info.page_size, Z_BEST_SPEED) == Z_OK) &&
1348 (size_out < s->dump_info.page_size)) {
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001349 pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_ZLIB);
1350 pd.size = cpu_to_dump32(s, size_out);
qiaonuohand12f57e2014-02-18 14:11:35 +08001351
1352 ret = write_cache(&page_data, buf_out, size_out, false);
1353 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001354 error_setg(errp, "dump: failed to write page data");
qiaonuohand12f57e2014-02-18 14:11:35 +08001355 goto out;
1356 }
1357#ifdef CONFIG_LZO
1358 } else if ((s->flag_compress & DUMP_DH_COMPRESSED_LZO) &&
Andrew Jones8161bef2016-01-11 20:56:20 +01001359 (lzo1x_1_compress(buf, s->dump_info.page_size, buf_out,
qiaonuohand12f57e2014-02-18 14:11:35 +08001360 (lzo_uint *)&size_out, wrkmem) == LZO_E_OK) &&
Andrew Jones8161bef2016-01-11 20:56:20 +01001361 (size_out < s->dump_info.page_size)) {
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001362 pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_LZO);
1363 pd.size = cpu_to_dump32(s, size_out);
qiaonuohand12f57e2014-02-18 14:11:35 +08001364
1365 ret = write_cache(&page_data, buf_out, size_out, false);
1366 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001367 error_setg(errp, "dump: failed to write page data");
qiaonuohand12f57e2014-02-18 14:11:35 +08001368 goto out;
1369 }
1370#endif
1371#ifdef CONFIG_SNAPPY
1372 } else if ((s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) &&
Andrew Jones8161bef2016-01-11 20:56:20 +01001373 (snappy_compress((char *)buf, s->dump_info.page_size,
qiaonuohand12f57e2014-02-18 14:11:35 +08001374 (char *)buf_out, &size_out) == SNAPPY_OK) &&
Andrew Jones8161bef2016-01-11 20:56:20 +01001375 (size_out < s->dump_info.page_size)) {
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001376 pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_SNAPPY);
1377 pd.size = cpu_to_dump32(s, size_out);
qiaonuohand12f57e2014-02-18 14:11:35 +08001378
1379 ret = write_cache(&page_data, buf_out, size_out, false);
1380 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001381 error_setg(errp, "dump: failed to write page data");
qiaonuohand12f57e2014-02-18 14:11:35 +08001382 goto out;
1383 }
1384#endif
1385 } else {
1386 /*
1387 * fall back to save in plaintext, size_out should be
Andrew Jones8161bef2016-01-11 20:56:20 +01001388 * assigned the target's page size
qiaonuohand12f57e2014-02-18 14:11:35 +08001389 */
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001390 pd.flags = cpu_to_dump32(s, 0);
Andrew Jones8161bef2016-01-11 20:56:20 +01001391 size_out = s->dump_info.page_size;
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001392 pd.size = cpu_to_dump32(s, size_out);
qiaonuohand12f57e2014-02-18 14:11:35 +08001393
Andrew Jones8161bef2016-01-11 20:56:20 +01001394 ret = write_cache(&page_data, buf,
1395 s->dump_info.page_size, false);
qiaonuohand12f57e2014-02-18 14:11:35 +08001396 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001397 error_setg(errp, "dump: failed to write page data");
qiaonuohand12f57e2014-02-18 14:11:35 +08001398 goto out;
1399 }
1400 }
1401
1402 /* get and write page desc here */
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001403 pd.page_flags = cpu_to_dump64(s, 0);
1404 pd.offset = cpu_to_dump64(s, offset_data);
qiaonuohand12f57e2014-02-18 14:11:35 +08001405 offset_data += size_out;
1406
1407 ret = write_cache(&page_desc, &pd, sizeof(PageDescriptor), false);
1408 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001409 error_setg(errp, "dump: failed to write page desc");
qiaonuohand12f57e2014-02-18 14:11:35 +08001410 goto out;
1411 }
1412 }
Peter Xu2264c2c2016-02-18 13:16:53 +08001413 s->written_size += s->dump_info.page_size;
qiaonuohand12f57e2014-02-18 14:11:35 +08001414 }
1415
1416 ret = write_cache(&page_desc, NULL, 0, true);
1417 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001418 error_setg(errp, "dump: failed to sync cache for page_desc");
qiaonuohand12f57e2014-02-18 14:11:35 +08001419 goto out;
1420 }
1421 ret = write_cache(&page_data, NULL, 0, true);
1422 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001423 error_setg(errp, "dump: failed to sync cache for page_data");
qiaonuohand12f57e2014-02-18 14:11:35 +08001424 goto out;
1425 }
1426
1427out:
1428 free_data_cache(&page_desc);
1429 free_data_cache(&page_data);
1430
1431#ifdef CONFIG_LZO
1432 g_free(wrkmem);
1433#endif
1434
1435 g_free(buf_out);
qiaonuohand12f57e2014-02-18 14:11:35 +08001436}
1437
zhanghailiang4c7e2512014-10-09 14:13:11 +08001438static void create_kdump_vmcore(DumpState *s, Error **errp)
qiaonuohanb53ccc32014-02-18 14:11:36 +08001439{
Janosch Frank86a518b2022-03-30 12:35:55 +00001440 ERRP_GUARD();
qiaonuohanb53ccc32014-02-18 14:11:36 +08001441 int ret;
1442
1443 /*
1444 * the kdump-compressed format is:
1445 * File offset
1446 * +------------------------------------------+ 0x0
1447 * | main header (struct disk_dump_header) |
1448 * |------------------------------------------+ block 1
1449 * | sub header (struct kdump_sub_header) |
1450 * |------------------------------------------+ block 2
1451 * | 1st-dump_bitmap |
1452 * |------------------------------------------+ block 2 + X blocks
1453 * | 2nd-dump_bitmap | (aligned by block)
1454 * |------------------------------------------+ block 2 + 2 * X blocks
1455 * | page desc for pfn 0 (struct page_desc) | (aligned by block)
1456 * | page desc for pfn 1 (struct page_desc) |
1457 * | : |
1458 * |------------------------------------------| (not aligned by block)
1459 * | page data (pfn 0) |
1460 * | page data (pfn 1) |
1461 * | : |
1462 * +------------------------------------------+
1463 */
1464
1465 ret = write_start_flat_header(s->fd);
1466 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001467 error_setg(errp, "dump: failed to write start flat header");
zhanghailiang4c7e2512014-10-09 14:13:11 +08001468 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001469 }
1470
Janosch Frank86a518b2022-03-30 12:35:55 +00001471 write_dump_header(s, errp);
1472 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +08001473 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001474 }
1475
Janosch Frank86a518b2022-03-30 12:35:55 +00001476 write_dump_bitmap(s, errp);
1477 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +08001478 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001479 }
1480
Janosch Frank86a518b2022-03-30 12:35:55 +00001481 write_dump_pages(s, errp);
1482 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +08001483 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001484 }
1485
1486 ret = write_end_flat_header(s->fd);
1487 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001488 error_setg(errp, "dump: failed to write end flat header");
zhanghailiang4c7e2512014-10-09 14:13:11 +08001489 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001490 }
qiaonuohanb53ccc32014-02-18 14:11:36 +08001491}
1492
Wen Congyang783e9b42012-05-07 12:10:47 +08001493static ram_addr_t get_start_block(DumpState *s)
1494{
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001495 GuestPhysBlock *block;
Wen Congyang783e9b42012-05-07 12:10:47 +08001496
1497 if (!s->has_filter) {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001498 s->next_block = QTAILQ_FIRST(&s->guest_phys_blocks.head);
Wen Congyang783e9b42012-05-07 12:10:47 +08001499 return 0;
1500 }
1501
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001502 QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
1503 if (block->target_start >= s->begin + s->length ||
1504 block->target_end <= s->begin) {
Wen Congyang783e9b42012-05-07 12:10:47 +08001505 /* This block is out of the range */
1506 continue;
1507 }
1508
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001509 s->next_block = block;
1510 if (s->begin > block->target_start) {
1511 s->start = s->begin - block->target_start;
Wen Congyang783e9b42012-05-07 12:10:47 +08001512 } else {
1513 s->start = 0;
1514 }
1515 return s->start;
1516 }
1517
1518 return -1;
1519}
1520
qiaonuohan7aad2482014-02-18 14:11:31 +08001521static void get_max_mapnr(DumpState *s)
1522{
1523 GuestPhysBlock *last_block;
1524
Paolo Bonzinieae3eb32018-12-06 13:10:34 +01001525 last_block = QTAILQ_LAST(&s->guest_phys_blocks.head);
Andrew Jones8161bef2016-01-11 20:56:20 +01001526 s->max_mapnr = dump_paddr_to_pfn(s, last_block->target_end);
qiaonuohan7aad2482014-02-18 14:11:31 +08001527}
1528
Peter Xubaf28f52016-02-18 13:16:48 +08001529static DumpState dump_state_global = { .status = DUMP_STATUS_NONE };
1530
1531static void dump_state_prepare(DumpState *s)
1532{
1533 /* zero the struct, setting status to active */
1534 *s = (DumpState) { .status = DUMP_STATUS_ACTIVE };
1535}
1536
Marc-André Lureau544803c2022-03-23 19:57:31 +04001537bool qemu_system_dump_in_progress(void)
Peter Xu65d64f32016-02-18 13:16:49 +08001538{
1539 DumpState *state = &dump_state_global;
Stefan Hajnoczid73415a2020-09-23 11:56:46 +01001540 return (qatomic_read(&state->status) == DUMP_STATUS_ACTIVE);
Peter Xu65d64f32016-02-18 13:16:49 +08001541}
1542
Peter Xu2264c2c2016-02-18 13:16:53 +08001543/* calculate total size of memory to be dumped (taking filter into
1544 * acoount.) */
1545static int64_t dump_calculate_size(DumpState *s)
1546{
1547 GuestPhysBlock *block;
1548 int64_t size = 0, total = 0, left = 0, right = 0;
1549
1550 QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
1551 if (s->has_filter) {
1552 /* calculate the overlapped region. */
1553 left = MAX(s->begin, block->target_start);
1554 right = MIN(s->begin + s->length, block->target_end);
1555 size = right - left;
1556 size = size > 0 ? size : 0;
1557 } else {
1558 /* count the whole region in */
1559 size = (block->target_end - block->target_start);
1560 }
1561 total += size;
1562 }
1563
1564 return total;
1565}
1566
Marc-André Lureaud9feb512017-09-11 18:59:26 +02001567static void vmcoreinfo_update_phys_base(DumpState *s)
1568{
1569 uint64_t size, note_head_size, name_size, phys_base;
1570 char **lines;
1571 uint8_t *vmci;
1572 size_t i;
1573
1574 if (!note_name_equal(s, s->guest_note, "VMCOREINFO")) {
1575 return;
1576 }
1577
1578 get_note_sizes(s, s->guest_note, &note_head_size, &name_size, &size);
1579 note_head_size = ROUND_UP(note_head_size, 4);
1580
1581 vmci = s->guest_note + note_head_size + ROUND_UP(name_size, 4);
1582 *(vmci + size) = '\0';
1583
1584 lines = g_strsplit((char *)vmci, "\n", -1);
1585 for (i = 0; lines[i]; i++) {
Wei Huang68cbecf2018-03-09 12:03:23 -05001586 const char *prefix = NULL;
1587
1588 if (s->dump_info.d_machine == EM_X86_64) {
1589 prefix = "NUMBER(phys_base)=";
1590 } else if (s->dump_info.d_machine == EM_AARCH64) {
1591 prefix = "NUMBER(PHYS_OFFSET)=";
1592 }
1593
1594 if (prefix && g_str_has_prefix(lines[i], prefix)) {
1595 if (qemu_strtou64(lines[i] + strlen(prefix), NULL, 16,
Marc-André Lureaud9feb512017-09-11 18:59:26 +02001596 &phys_base) < 0) {
Wei Huang68cbecf2018-03-09 12:03:23 -05001597 warn_report("Failed to read %s", prefix);
Marc-André Lureaud9feb512017-09-11 18:59:26 +02001598 } else {
1599 s->dump_info.phys_base = phys_base;
1600 }
1601 break;
1602 }
1603 }
1604
1605 g_strfreev(lines);
1606}
1607
zhanghailiang4c7e2512014-10-09 14:13:11 +08001608static void dump_init(DumpState *s, int fd, bool has_format,
1609 DumpGuestMemoryFormat format, bool paging, bool has_filter,
1610 int64_t begin, int64_t length, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +08001611{
Janosch Frank86a518b2022-03-30 12:35:55 +00001612 ERRP_GUARD();
Marc-André Lureau903ef732017-09-11 18:59:25 +02001613 VMCoreInfoState *vmci = vmcoreinfo_find();
Andreas Färber182735e2013-05-29 22:29:20 +02001614 CPUState *cpu;
Wen Congyang783e9b42012-05-07 12:10:47 +08001615 int nr_cpus;
1616 int ret;
1617
Peter Xuca1fc8c2016-02-18 13:16:50 +08001618 s->has_format = has_format;
1619 s->format = format;
Peter Xu2264c2c2016-02-18 13:16:53 +08001620 s->written_size = 0;
Peter Xuca1fc8c2016-02-18 13:16:50 +08001621
qiaonuohanb53ccc32014-02-18 14:11:36 +08001622 /* kdump-compressed is conflict with paging and filter */
1623 if (has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
1624 assert(!paging && !has_filter);
1625 }
1626
Wen Congyang783e9b42012-05-07 12:10:47 +08001627 if (runstate_is_running()) {
1628 vm_stop(RUN_STATE_SAVE_VM);
1629 s->resume = true;
1630 } else {
1631 s->resume = false;
1632 }
1633
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001634 /* If we use KVM, we should synchronize the registers before we get dump
1635 * info or physmap info.
Wen Congyang783e9b42012-05-07 12:10:47 +08001636 */
Andreas Färber1b3509c2013-06-09 16:48:29 +02001637 cpu_synchronize_all_states();
Wen Congyang783e9b42012-05-07 12:10:47 +08001638 nr_cpus = 0;
Andreas Färberbdc44642013-06-24 23:50:24 +02001639 CPU_FOREACH(cpu) {
Wen Congyang783e9b42012-05-07 12:10:47 +08001640 nr_cpus++;
1641 }
1642
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001643 s->fd = fd;
1644 s->has_filter = has_filter;
1645 s->begin = begin;
1646 s->length = length;
1647
Chen Gang29282072014-08-03 23:28:56 +08001648 memory_mapping_list_init(&s->list);
1649
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001650 guest_phys_blocks_init(&s->guest_phys_blocks);
Laszlo Ersekc5d7f602013-08-06 12:37:10 +02001651 guest_phys_blocks_append(&s->guest_phys_blocks);
Peter Xu2264c2c2016-02-18 13:16:53 +08001652 s->total_size = dump_calculate_size(s);
1653#ifdef DEBUG_DUMP_GUEST_MEMORY
1654 fprintf(stderr, "DUMP: total memory to dump: %lu\n", s->total_size);
1655#endif
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001656
Cornelia Huckd1e69942017-09-13 16:20:35 +02001657 /* it does not make sense to dump non-existent memory */
1658 if (!s->total_size) {
1659 error_setg(errp, "dump: no guest memory to dump");
1660 goto cleanup;
1661 }
1662
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001663 s->start = get_start_block(s);
1664 if (s->start == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001665 error_setg(errp, QERR_INVALID_PARAMETER, "begin");
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001666 goto cleanup;
1667 }
1668
1669 /* get dump info: endian, class and architecture.
1670 * If the target architecture is not supported, cpu_get_dump_info() will
1671 * return -1.
1672 */
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001673 ret = cpu_get_dump_info(&s->dump_info, &s->guest_phys_blocks);
Wen Congyang783e9b42012-05-07 12:10:47 +08001674 if (ret < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001675 error_setg(errp, QERR_UNSUPPORTED);
Wen Congyang783e9b42012-05-07 12:10:47 +08001676 goto cleanup;
1677 }
1678
Andrew Jones8161bef2016-01-11 20:56:20 +01001679 if (!s->dump_info.page_size) {
1680 s->dump_info.page_size = TARGET_PAGE_SIZE;
1681 }
1682
Paolo Bonzini4720bd02012-06-07 08:48:09 +02001683 s->note_size = cpu_get_note_size(s->dump_info.d_class,
1684 s->dump_info.d_machine, nr_cpus);
Aneesh Kumar K.Vbb6b6842013-10-01 21:49:32 +05301685 if (s->note_size < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001686 error_setg(errp, QERR_UNSUPPORTED);
Paolo Bonzini4720bd02012-06-07 08:48:09 +02001687 goto cleanup;
1688 }
1689
Marc-André Lureau903ef732017-09-11 18:59:25 +02001690 /*
Marc-André Lureaud9feb512017-09-11 18:59:26 +02001691 * The goal of this block is to (a) update the previously guessed
1692 * phys_base, (b) copy the guest note out of the guest.
1693 * Failure to do so is not fatal for dumping.
Marc-André Lureau903ef732017-09-11 18:59:25 +02001694 */
1695 if (vmci) {
1696 uint64_t addr, note_head_size, name_size, desc_size;
1697 uint32_t size;
1698 uint16_t format;
1699
Janosch Frank05bbaa502022-03-30 12:36:00 +00001700 note_head_size = dump_is_64bit(s) ?
1701 sizeof(Elf64_Nhdr) : sizeof(Elf32_Nhdr);
Marc-André Lureau903ef732017-09-11 18:59:25 +02001702
1703 format = le16_to_cpu(vmci->vmcoreinfo.guest_format);
1704 size = le32_to_cpu(vmci->vmcoreinfo.size);
1705 addr = le64_to_cpu(vmci->vmcoreinfo.paddr);
1706 if (!vmci->has_vmcoreinfo) {
1707 warn_report("guest note is not present");
1708 } else if (size < note_head_size || size > MAX_GUEST_NOTE_SIZE) {
1709 warn_report("guest note size is invalid: %" PRIu32, size);
Marc-André Lureau5be5df72018-08-17 17:59:10 +02001710 } else if (format != FW_CFG_VMCOREINFO_FORMAT_ELF) {
Marc-André Lureau903ef732017-09-11 18:59:25 +02001711 warn_report("guest note format is unsupported: %" PRIu16, format);
1712 } else {
1713 s->guest_note = g_malloc(size + 1); /* +1 for adding \0 */
1714 cpu_physical_memory_read(addr, s->guest_note, size);
1715
1716 get_note_sizes(s, s->guest_note, NULL, &name_size, &desc_size);
1717 s->guest_note_size = ELF_NOTE_SIZE(note_head_size, name_size,
1718 desc_size);
1719 if (name_size > MAX_GUEST_NOTE_SIZE ||
1720 desc_size > MAX_GUEST_NOTE_SIZE ||
1721 s->guest_note_size > size) {
1722 warn_report("Invalid guest note header");
1723 g_free(s->guest_note);
1724 s->guest_note = NULL;
1725 } else {
Marc-André Lureaud9feb512017-09-11 18:59:26 +02001726 vmcoreinfo_update_phys_base(s);
Marc-André Lureau903ef732017-09-11 18:59:25 +02001727 s->note_size += s->guest_note_size;
1728 }
1729 }
1730 }
1731
Wen Congyang783e9b42012-05-07 12:10:47 +08001732 /* get memory mapping */
Wen Congyang783e9b42012-05-07 12:10:47 +08001733 if (paging) {
Janosch Frank86a518b2022-03-30 12:35:55 +00001734 qemu_get_guest_memory_mapping(&s->list, &s->guest_phys_blocks, errp);
1735 if (*errp) {
Andreas Färber11ed09c2013-05-29 21:54:03 +02001736 goto cleanup;
1737 }
Wen Congyang783e9b42012-05-07 12:10:47 +08001738 } else {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001739 qemu_get_guest_simple_memory_mapping(&s->list, &s->guest_phys_blocks);
Wen Congyang783e9b42012-05-07 12:10:47 +08001740 }
1741
qiaonuohan7aad2482014-02-18 14:11:31 +08001742 s->nr_cpus = nr_cpus;
qiaonuohan7aad2482014-02-18 14:11:31 +08001743
1744 get_max_mapnr(s);
1745
1746 uint64_t tmp;
Andrew Jones8161bef2016-01-11 20:56:20 +01001747 tmp = DIV_ROUND_UP(DIV_ROUND_UP(s->max_mapnr, CHAR_BIT),
1748 s->dump_info.page_size);
1749 s->len_dump_bitmap = tmp * s->dump_info.page_size;
qiaonuohan7aad2482014-02-18 14:11:31 +08001750
qiaonuohanb53ccc32014-02-18 14:11:36 +08001751 /* init for kdump-compressed format */
1752 if (has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
1753 switch (format) {
1754 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB:
1755 s->flag_compress = DUMP_DH_COMPRESSED_ZLIB;
1756 break;
1757
1758 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO:
Laszlo Ersekc998acb2014-05-20 13:42:22 +02001759#ifdef CONFIG_LZO
1760 if (lzo_init() != LZO_E_OK) {
1761 error_setg(errp, "failed to initialize the LZO library");
1762 goto cleanup;
1763 }
1764#endif
qiaonuohanb53ccc32014-02-18 14:11:36 +08001765 s->flag_compress = DUMP_DH_COMPRESSED_LZO;
1766 break;
1767
1768 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY:
1769 s->flag_compress = DUMP_DH_COMPRESSED_SNAPPY;
1770 break;
1771
1772 default:
1773 s->flag_compress = 0;
1774 }
1775
zhanghailiang4c7e2512014-10-09 14:13:11 +08001776 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001777 }
1778
Wen Congyang783e9b42012-05-07 12:10:47 +08001779 if (s->has_filter) {
1780 memory_mapping_filter(&s->list, s->begin, s->length);
1781 }
1782
1783 /*
1784 * calculate phdr_num
1785 *
1786 * the type of ehdr->e_phnum is uint16_t, so we should avoid overflow
1787 */
1788 s->phdr_num = 1; /* PT_NOTE */
1789 if (s->list.num < UINT16_MAX - 2) {
Janosch Frank862a3952022-03-30 12:35:57 +00001790 s->shdr_num = 0;
Wen Congyang783e9b42012-05-07 12:10:47 +08001791 s->phdr_num += s->list.num;
Wen Congyang783e9b42012-05-07 12:10:47 +08001792 } else {
Janosch Frank046bc412022-04-07 09:48:24 +00001793 /* sh_info of section 0 holds the real number of phdrs */
Janosch Frank862a3952022-03-30 12:35:57 +00001794 s->shdr_num = 1;
Wen Congyang783e9b42012-05-07 12:10:47 +08001795
1796 /* the type of shdr->sh_info is uint32_t, so we should avoid overflow */
1797 if (s->list.num <= UINT32_MAX - 1) {
Janosch Frank046bc412022-04-07 09:48:24 +00001798 s->phdr_num += s->list.num;
Wen Congyang783e9b42012-05-07 12:10:47 +08001799 } else {
Janosch Frank046bc412022-04-07 09:48:24 +00001800 s->phdr_num = UINT32_MAX;
Wen Congyang783e9b42012-05-07 12:10:47 +08001801 }
1802 }
1803
Janosch Frank05bbaa502022-03-30 12:36:00 +00001804 if (dump_is_64bit(s)) {
Janosch Franke71d3532022-03-30 12:35:59 +00001805 s->phdr_offset = sizeof(Elf64_Ehdr);
1806 s->shdr_offset = s->phdr_offset + sizeof(Elf64_Phdr) * s->phdr_num;
1807 s->note_offset = s->shdr_offset + sizeof(Elf64_Shdr) * s->shdr_num;
1808 s->memory_offset = s->note_offset + s->note_size;
Wen Congyang783e9b42012-05-07 12:10:47 +08001809 } else {
Janosch Franke71d3532022-03-30 12:35:59 +00001810
1811 s->phdr_offset = sizeof(Elf32_Ehdr);
1812 s->shdr_offset = s->phdr_offset + sizeof(Elf32_Phdr) * s->phdr_num;
1813 s->note_offset = s->shdr_offset + sizeof(Elf32_Shdr) * s->shdr_num;
1814 s->memory_offset = s->note_offset + s->note_size;
Wen Congyang783e9b42012-05-07 12:10:47 +08001815 }
1816
zhanghailiang4c7e2512014-10-09 14:13:11 +08001817 return;
Wen Congyang783e9b42012-05-07 12:10:47 +08001818
1819cleanup:
Chen Gang29282072014-08-03 23:28:56 +08001820 dump_cleanup(s);
Wen Congyang783e9b42012-05-07 12:10:47 +08001821}
1822
Peter Xuca1fc8c2016-02-18 13:16:50 +08001823/* this operation might be time consuming. */
1824static void dump_process(DumpState *s, Error **errp)
1825{
Janosch Frank86a518b2022-03-30 12:35:55 +00001826 ERRP_GUARD();
Peter Xud42a0d12016-02-18 13:16:56 +08001827 DumpQueryResult *result = NULL;
Peter Xuca1fc8c2016-02-18 13:16:50 +08001828
Viktor Prutyanov2da91b52018-05-17 19:23:39 +03001829 if (s->has_format && s->format == DUMP_GUEST_MEMORY_FORMAT_WIN_DMP) {
1830#ifdef TARGET_X86_64
Janosch Frank86a518b2022-03-30 12:35:55 +00001831 create_win_dump(s, errp);
Viktor Prutyanov2da91b52018-05-17 19:23:39 +03001832#endif
1833 } else if (s->has_format && s->format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
Janosch Frank86a518b2022-03-30 12:35:55 +00001834 create_kdump_vmcore(s, errp);
Peter Xuca1fc8c2016-02-18 13:16:50 +08001835 } else {
Janosch Frank86a518b2022-03-30 12:35:55 +00001836 create_vmcore(s, errp);
Peter Xuca1fc8c2016-02-18 13:16:50 +08001837 }
1838
Peter Xu39ba2ea2016-02-18 13:16:54 +08001839 /* make sure status is written after written_size updates */
1840 smp_wmb();
Stefan Hajnoczid73415a2020-09-23 11:56:46 +01001841 qatomic_set(&s->status,
Janosch Frank86a518b2022-03-30 12:35:55 +00001842 (*errp ? DUMP_STATUS_FAILED : DUMP_STATUS_COMPLETED));
Peter Xuca1fc8c2016-02-18 13:16:50 +08001843
Peter Xud42a0d12016-02-18 13:16:56 +08001844 /* send DUMP_COMPLETED message (unconditionally) */
1845 result = qmp_query_dump(NULL);
1846 /* should never fail */
1847 assert(result);
Janosch Frank86a518b2022-03-30 12:35:55 +00001848 qapi_event_send_dump_completed(result, !!*errp, (*errp ?
1849 error_get_pretty(*errp) : NULL));
Peter Xud42a0d12016-02-18 13:16:56 +08001850 qapi_free_DumpQueryResult(result);
1851
Peter Xuca1fc8c2016-02-18 13:16:50 +08001852 dump_cleanup(s);
1853}
1854
Peter Xu1fbeff72016-02-18 13:16:52 +08001855static void *dump_thread(void *data)
1856{
Peter Xu1fbeff72016-02-18 13:16:52 +08001857 DumpState *s = (DumpState *)data;
Alberto Garciac3a8fe32017-08-29 15:08:36 +03001858 dump_process(s, NULL);
Peter Xu1fbeff72016-02-18 13:16:52 +08001859 return NULL;
1860}
1861
Peter Xu39ba2ea2016-02-18 13:16:54 +08001862DumpQueryResult *qmp_query_dump(Error **errp)
1863{
1864 DumpQueryResult *result = g_new(DumpQueryResult, 1);
1865 DumpState *state = &dump_state_global;
Stefan Hajnoczid73415a2020-09-23 11:56:46 +01001866 result->status = qatomic_read(&state->status);
Peter Xu39ba2ea2016-02-18 13:16:54 +08001867 /* make sure we are reading status and written_size in order */
1868 smp_rmb();
1869 result->completed = state->written_size;
1870 result->total = state->total_size;
1871 return result;
1872}
1873
Peter Xu228de9c2016-02-18 13:16:47 +08001874void qmp_dump_guest_memory(bool paging, const char *file,
1875 bool has_detach, bool detach,
1876 bool has_begin, int64_t begin, bool has_length,
qiaonuohanb53ccc32014-02-18 14:11:36 +08001877 int64_t length, bool has_format,
1878 DumpGuestMemoryFormat format, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +08001879{
Janosch Frank86a518b2022-03-30 12:35:55 +00001880 ERRP_GUARD();
Wen Congyang783e9b42012-05-07 12:10:47 +08001881 const char *p;
1882 int fd = -1;
1883 DumpState *s;
Peter Xu1fbeff72016-02-18 13:16:52 +08001884 bool detach_p = false;
Wen Congyang783e9b42012-05-07 12:10:47 +08001885
Peter Xu63e27f22016-02-18 13:16:51 +08001886 if (runstate_check(RUN_STATE_INMIGRATE)) {
1887 error_setg(errp, "Dump not allowed during incoming migration.");
1888 return;
1889 }
1890
Peter Xu65d64f32016-02-18 13:16:49 +08001891 /* if there is a dump in background, we should wait until the dump
1892 * finished */
Marc-André Lureau544803c2022-03-23 19:57:31 +04001893 if (qemu_system_dump_in_progress()) {
Peter Xu65d64f32016-02-18 13:16:49 +08001894 error_setg(errp, "There is a dump in process, please wait.");
1895 return;
1896 }
1897
qiaonuohanb53ccc32014-02-18 14:11:36 +08001898 /*
1899 * kdump-compressed format need the whole memory dumped, so paging or
1900 * filter is not supported here.
1901 */
1902 if ((has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) &&
1903 (paging || has_begin || has_length)) {
1904 error_setg(errp, "kdump-compressed format doesn't support paging or "
1905 "filter");
1906 return;
1907 }
Wen Congyang783e9b42012-05-07 12:10:47 +08001908 if (has_begin && !has_length) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001909 error_setg(errp, QERR_MISSING_PARAMETER, "length");
Wen Congyang783e9b42012-05-07 12:10:47 +08001910 return;
1911 }
1912 if (!has_begin && has_length) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001913 error_setg(errp, QERR_MISSING_PARAMETER, "begin");
Wen Congyang783e9b42012-05-07 12:10:47 +08001914 return;
1915 }
Peter Xu1fbeff72016-02-18 13:16:52 +08001916 if (has_detach) {
1917 detach_p = detach;
1918 }
Wen Congyang783e9b42012-05-07 12:10:47 +08001919
qiaonuohanb53ccc32014-02-18 14:11:36 +08001920 /* check whether lzo/snappy is supported */
1921#ifndef CONFIG_LZO
1922 if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO) {
1923 error_setg(errp, "kdump-lzo is not available now");
1924 return;
1925 }
1926#endif
1927
1928#ifndef CONFIG_SNAPPY
1929 if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY) {
1930 error_setg(errp, "kdump-snappy is not available now");
1931 return;
1932 }
1933#endif
1934
Viktor Prutyanov2da91b52018-05-17 19:23:39 +03001935#ifndef TARGET_X86_64
1936 if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_WIN_DMP) {
1937 error_setg(errp, "Windows dump is only available for x86-64");
1938 return;
1939 }
1940#endif
1941
Wen Congyang783e9b42012-05-07 12:10:47 +08001942#if !defined(WIN32)
1943 if (strstart(file, "fd:", &p)) {
Kevin Wolf947e4742020-10-05 17:58:44 +02001944 fd = monitor_get_fd(monitor_cur(), p, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +08001945 if (fd == -1) {
Wen Congyang783e9b42012-05-07 12:10:47 +08001946 return;
1947 }
1948 }
1949#endif
1950
1951 if (strstart(file, "file:", &p)) {
Daniel P. Berrangé448058a2020-07-21 13:25:21 +01001952 fd = qemu_open_old(p, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, S_IRUSR);
Wen Congyang783e9b42012-05-07 12:10:47 +08001953 if (fd < 0) {
Luiz Capitulino75817662013-06-07 14:36:01 -04001954 error_setg_file_open(errp, errno, p);
Wen Congyang783e9b42012-05-07 12:10:47 +08001955 return;
1956 }
1957 }
1958
1959 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001960 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
Wen Congyang783e9b42012-05-07 12:10:47 +08001961 return;
1962 }
1963
Peter Xub7bc6b12021-09-22 12:20:09 -04001964 if (!dump_migration_blocker) {
1965 error_setg(&dump_migration_blocker,
1966 "Live migration disabled: dump-guest-memory in progress");
1967 }
1968
1969 /*
1970 * Allows even for -only-migratable, but forbid migration during the
1971 * process of dump guest memory.
1972 */
1973 if (migrate_add_blocker_internal(dump_migration_blocker, errp)) {
1974 /* Remember to release the fd before passing it over to dump state */
1975 close(fd);
1976 return;
1977 }
1978
Peter Xubaf28f52016-02-18 13:16:48 +08001979 s = &dump_state_global;
1980 dump_state_prepare(s);
Wen Congyang783e9b42012-05-07 12:10:47 +08001981
zhanghailiang4c7e2512014-10-09 14:13:11 +08001982 dump_init(s, fd, has_format, format, paging, has_begin,
Janosch Frank86a518b2022-03-30 12:35:55 +00001983 begin, length, errp);
1984 if (*errp) {
Stefan Hajnoczid73415a2020-09-23 11:56:46 +01001985 qatomic_set(&s->status, DUMP_STATUS_FAILED);
Wen Congyang783e9b42012-05-07 12:10:47 +08001986 return;
1987 }
1988
Peter Xu1fbeff72016-02-18 13:16:52 +08001989 if (detach_p) {
1990 /* detached dump */
Fam Zheng6796b402017-05-03 15:28:19 +08001991 s->detached = true;
Peter Xu1fbeff72016-02-18 13:16:52 +08001992 qemu_thread_create(&s->dump_thread, "dump_thread", dump_thread,
1993 s, QEMU_THREAD_DETACHED);
1994 } else {
1995 /* sync dump */
1996 dump_process(s, errp);
1997 }
Wen Congyang783e9b42012-05-07 12:10:47 +08001998}
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08001999
2000DumpGuestMemoryCapability *qmp_query_dump_guest_memory_capability(Error **errp)
2001{
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002002 DumpGuestMemoryCapability *cap =
Markus Armbrusterb21e2382022-03-15 15:41:56 +01002003 g_new0(DumpGuestMemoryCapability, 1);
Eric Blake95b3a8c2021-01-13 16:10:13 -06002004 DumpGuestMemoryFormatList **tail = &cap->formats;
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002005
2006 /* elf is always available */
Eric Blake95b3a8c2021-01-13 16:10:13 -06002007 QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_ELF);
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002008
2009 /* kdump-zlib is always available */
Eric Blake95b3a8c2021-01-13 16:10:13 -06002010 QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB);
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002011
2012 /* add new item if kdump-lzo is available */
2013#ifdef CONFIG_LZO
Eric Blake95b3a8c2021-01-13 16:10:13 -06002014 QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO);
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002015#endif
2016
2017 /* add new item if kdump-snappy is available */
2018#ifdef CONFIG_SNAPPY
Eric Blake95b3a8c2021-01-13 16:10:13 -06002019 QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY);
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002020#endif
2021
Viktor Prutyanov2da91b52018-05-17 19:23:39 +03002022 /* Windows dump is available only if target is x86_64 */
2023#ifdef TARGET_X86_64
Eric Blake95b3a8c2021-01-13 16:10:13 -06002024 QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_WIN_DMP);
Viktor Prutyanov2da91b52018-05-17 19:23:39 +03002025#endif
2026
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002027 return cap;
2028}