blob: 107a67165a645dcd96dfcf64dfd6d398915014b1 [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"
Wen Congyang783e9b42012-05-07 12:10:47 +080017#include "cpu.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010018#include "exec/hwaddr.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010019#include "monitor/monitor.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010020#include "sysemu/kvm.h"
21#include "sysemu/dump.h"
22#include "sysemu/sysemu.h"
23#include "sysemu/memory_mapping.h"
Andreas Färber1b3509c2013-06-09 16:48:29 +020024#include "sysemu/cpus.h"
Markus Armbrustere688df62018-02-01 12:18:31 +010025#include "qapi/error.h"
Markus Armbruster112ed242018-02-26 17:13:27 -060026#include "qapi/qapi-commands-misc.h"
27#include "qapi/qapi-events-misc.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010028#include "qapi/qmp/qerror.h"
Marc-André Lureau903ef732017-09-11 18:59:25 +020029#include "qemu/error-report.h"
30#include "hw/misc/vmcoreinfo.h"
Wen Congyang783e9b42012-05-07 12:10:47 +080031
Viktor Prutyanov2da91b52018-05-17 19:23:39 +030032#ifdef TARGET_X86_64
33#include "win_dump.h"
34#endif
35
qiaonuohand12f57e2014-02-18 14:11:35 +080036#include <zlib.h>
37#ifdef CONFIG_LZO
38#include <lzo/lzo1x.h>
39#endif
40#ifdef CONFIG_SNAPPY
41#include <snappy-c.h>
42#endif
qiaonuohan4ab23a92014-02-18 14:11:37 +080043#ifndef ELF_MACHINE_UNAME
44#define ELF_MACHINE_UNAME "Unknown"
45#endif
qiaonuohand12f57e2014-02-18 14:11:35 +080046
Marc-André Lureau903ef732017-09-11 18:59:25 +020047#define MAX_GUEST_NOTE_SIZE (1 << 20) /* 1MB should be enough */
48
49#define ELF_NOTE_SIZE(hdr_size, name_size, desc_size) \
50 ((DIV_ROUND_UP((hdr_size), 4) + \
51 DIV_ROUND_UP((name_size), 4) + \
52 DIV_ROUND_UP((desc_size), 4)) * 4)
53
Bharata B Raoacb0ef52014-05-19 19:57:44 +020054uint16_t cpu_to_dump16(DumpState *s, uint16_t val)
Wen Congyang783e9b42012-05-07 12:10:47 +080055{
Bharata B Raoacb0ef52014-05-19 19:57:44 +020056 if (s->dump_info.d_endian == ELFDATA2LSB) {
Wen Congyang783e9b42012-05-07 12:10:47 +080057 val = cpu_to_le16(val);
58 } else {
59 val = cpu_to_be16(val);
60 }
61
62 return val;
63}
64
Bharata B Raoacb0ef52014-05-19 19:57:44 +020065uint32_t cpu_to_dump32(DumpState *s, uint32_t val)
Wen Congyang783e9b42012-05-07 12:10:47 +080066{
Bharata B Raoacb0ef52014-05-19 19:57:44 +020067 if (s->dump_info.d_endian == ELFDATA2LSB) {
Wen Congyang783e9b42012-05-07 12:10:47 +080068 val = cpu_to_le32(val);
69 } else {
70 val = cpu_to_be32(val);
71 }
72
73 return val;
74}
75
Bharata B Raoacb0ef52014-05-19 19:57:44 +020076uint64_t cpu_to_dump64(DumpState *s, uint64_t val)
Wen Congyang783e9b42012-05-07 12:10:47 +080077{
Bharata B Raoacb0ef52014-05-19 19:57:44 +020078 if (s->dump_info.d_endian == ELFDATA2LSB) {
Wen Congyang783e9b42012-05-07 12:10:47 +080079 val = cpu_to_le64(val);
80 } else {
81 val = cpu_to_be64(val);
82 }
83
84 return val;
85}
86
Wen Congyang783e9b42012-05-07 12:10:47 +080087static int dump_cleanup(DumpState *s)
88{
Laszlo Ersek5ee163e2013-08-06 12:37:09 +020089 guest_phys_blocks_free(&s->guest_phys_blocks);
Wen Congyang783e9b42012-05-07 12:10:47 +080090 memory_mapping_list_free(&s->list);
Chen Gang29282072014-08-03 23:28:56 +080091 close(s->fd);
Marc-André Lureau903ef732017-09-11 18:59:25 +020092 g_free(s->guest_note);
93 s->guest_note = NULL;
Wen Congyang783e9b42012-05-07 12:10:47 +080094 if (s->resume) {
Fam Zheng6796b402017-05-03 15:28:19 +080095 if (s->detached) {
96 qemu_mutex_lock_iothread();
97 }
Wen Congyang783e9b42012-05-07 12:10:47 +080098 vm_start();
Fam Zheng6796b402017-05-03 15:28:19 +080099 if (s->detached) {
100 qemu_mutex_unlock_iothread();
101 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800102 }
103
Chen Gang29282072014-08-03 23:28:56 +0800104 return 0;
Wen Congyang783e9b42012-05-07 12:10:47 +0800105}
106
qiaonuohanb5ba1cc2014-02-18 14:11:25 +0800107static int fd_write_vmcore(const void *buf, size_t size, void *opaque)
Wen Congyang783e9b42012-05-07 12:10:47 +0800108{
109 DumpState *s = opaque;
Luiz Capitulino2f616522012-09-21 13:17:55 -0300110 size_t written_size;
Wen Congyang783e9b42012-05-07 12:10:47 +0800111
Luiz Capitulino2f616522012-09-21 13:17:55 -0300112 written_size = qemu_write_full(s->fd, buf, size);
113 if (written_size != size) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200114 return -errno;
Wen Congyang783e9b42012-05-07 12:10:47 +0800115 }
116
117 return 0;
118}
119
zhanghailiang4c7e2512014-10-09 14:13:11 +0800120static void write_elf64_header(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800121{
122 Elf64_Ehdr elf_header;
123 int ret;
Wen Congyang783e9b42012-05-07 12:10:47 +0800124
125 memset(&elf_header, 0, sizeof(Elf64_Ehdr));
126 memcpy(&elf_header, ELFMAG, SELFMAG);
127 elf_header.e_ident[EI_CLASS] = ELFCLASS64;
128 elf_header.e_ident[EI_DATA] = s->dump_info.d_endian;
129 elf_header.e_ident[EI_VERSION] = EV_CURRENT;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200130 elf_header.e_type = cpu_to_dump16(s, ET_CORE);
131 elf_header.e_machine = cpu_to_dump16(s, s->dump_info.d_machine);
132 elf_header.e_version = cpu_to_dump32(s, EV_CURRENT);
133 elf_header.e_ehsize = cpu_to_dump16(s, sizeof(elf_header));
134 elf_header.e_phoff = cpu_to_dump64(s, sizeof(Elf64_Ehdr));
135 elf_header.e_phentsize = cpu_to_dump16(s, sizeof(Elf64_Phdr));
136 elf_header.e_phnum = cpu_to_dump16(s, s->phdr_num);
Wen Congyang783e9b42012-05-07 12:10:47 +0800137 if (s->have_section) {
138 uint64_t shoff = sizeof(Elf64_Ehdr) + sizeof(Elf64_Phdr) * s->sh_info;
139
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200140 elf_header.e_shoff = cpu_to_dump64(s, shoff);
141 elf_header.e_shentsize = cpu_to_dump16(s, sizeof(Elf64_Shdr));
142 elf_header.e_shnum = cpu_to_dump16(s, 1);
Wen Congyang783e9b42012-05-07 12:10:47 +0800143 }
144
145 ret = fd_write_vmcore(&elf_header, sizeof(elf_header), s);
146 if (ret < 0) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200147 error_setg_errno(errp, -ret, "dump: failed to write elf header");
Wen Congyang783e9b42012-05-07 12:10:47 +0800148 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800149}
150
zhanghailiang4c7e2512014-10-09 14:13:11 +0800151static void write_elf32_header(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800152{
153 Elf32_Ehdr elf_header;
154 int ret;
Wen Congyang783e9b42012-05-07 12:10:47 +0800155
156 memset(&elf_header, 0, sizeof(Elf32_Ehdr));
157 memcpy(&elf_header, ELFMAG, SELFMAG);
158 elf_header.e_ident[EI_CLASS] = ELFCLASS32;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200159 elf_header.e_ident[EI_DATA] = s->dump_info.d_endian;
Wen Congyang783e9b42012-05-07 12:10:47 +0800160 elf_header.e_ident[EI_VERSION] = EV_CURRENT;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200161 elf_header.e_type = cpu_to_dump16(s, ET_CORE);
162 elf_header.e_machine = cpu_to_dump16(s, s->dump_info.d_machine);
163 elf_header.e_version = cpu_to_dump32(s, EV_CURRENT);
164 elf_header.e_ehsize = cpu_to_dump16(s, sizeof(elf_header));
165 elf_header.e_phoff = cpu_to_dump32(s, sizeof(Elf32_Ehdr));
166 elf_header.e_phentsize = cpu_to_dump16(s, sizeof(Elf32_Phdr));
167 elf_header.e_phnum = cpu_to_dump16(s, s->phdr_num);
Wen Congyang783e9b42012-05-07 12:10:47 +0800168 if (s->have_section) {
169 uint32_t shoff = sizeof(Elf32_Ehdr) + sizeof(Elf32_Phdr) * s->sh_info;
170
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200171 elf_header.e_shoff = cpu_to_dump32(s, shoff);
172 elf_header.e_shentsize = cpu_to_dump16(s, sizeof(Elf32_Shdr));
173 elf_header.e_shnum = cpu_to_dump16(s, 1);
Wen Congyang783e9b42012-05-07 12:10:47 +0800174 }
175
176 ret = fd_write_vmcore(&elf_header, sizeof(elf_header), s);
177 if (ret < 0) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200178 error_setg_errno(errp, -ret, "dump: failed to write elf header");
Wen Congyang783e9b42012-05-07 12:10:47 +0800179 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800180}
181
zhanghailiang4c7e2512014-10-09 14:13:11 +0800182static void write_elf64_load(DumpState *s, MemoryMapping *memory_mapping,
183 int phdr_index, hwaddr offset,
184 hwaddr filesz, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800185{
186 Elf64_Phdr phdr;
187 int ret;
Wen Congyang783e9b42012-05-07 12:10:47 +0800188
189 memset(&phdr, 0, sizeof(Elf64_Phdr));
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200190 phdr.p_type = cpu_to_dump32(s, PT_LOAD);
191 phdr.p_offset = cpu_to_dump64(s, offset);
192 phdr.p_paddr = cpu_to_dump64(s, memory_mapping->phys_addr);
193 phdr.p_filesz = cpu_to_dump64(s, filesz);
194 phdr.p_memsz = cpu_to_dump64(s, memory_mapping->length);
Jon Dorone17bebd2019-01-09 10:22:03 +0200195 phdr.p_vaddr = cpu_to_dump64(s, memory_mapping->virt_addr) ?: phdr.p_paddr;
Wen Congyang783e9b42012-05-07 12:10:47 +0800196
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200197 assert(memory_mapping->length >= filesz);
198
Wen Congyang783e9b42012-05-07 12:10:47 +0800199 ret = fd_write_vmcore(&phdr, sizeof(Elf64_Phdr), s);
200 if (ret < 0) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200201 error_setg_errno(errp, -ret,
202 "dump: failed to write program header table");
Wen Congyang783e9b42012-05-07 12:10:47 +0800203 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800204}
205
zhanghailiang4c7e2512014-10-09 14:13:11 +0800206static void write_elf32_load(DumpState *s, MemoryMapping *memory_mapping,
207 int phdr_index, hwaddr offset,
208 hwaddr filesz, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800209{
210 Elf32_Phdr phdr;
211 int ret;
Wen Congyang783e9b42012-05-07 12:10:47 +0800212
213 memset(&phdr, 0, sizeof(Elf32_Phdr));
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200214 phdr.p_type = cpu_to_dump32(s, PT_LOAD);
215 phdr.p_offset = cpu_to_dump32(s, offset);
216 phdr.p_paddr = cpu_to_dump32(s, memory_mapping->phys_addr);
217 phdr.p_filesz = cpu_to_dump32(s, filesz);
218 phdr.p_memsz = cpu_to_dump32(s, memory_mapping->length);
Jon Dorone17bebd2019-01-09 10:22:03 +0200219 phdr.p_vaddr =
220 cpu_to_dump32(s, memory_mapping->virt_addr) ?: phdr.p_paddr;
Wen Congyang783e9b42012-05-07 12:10:47 +0800221
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200222 assert(memory_mapping->length >= filesz);
223
Wen Congyang783e9b42012-05-07 12:10:47 +0800224 ret = fd_write_vmcore(&phdr, sizeof(Elf32_Phdr), s);
225 if (ret < 0) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200226 error_setg_errno(errp, -ret,
227 "dump: failed to write program header table");
Wen Congyang783e9b42012-05-07 12:10:47 +0800228 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800229}
230
zhanghailiang4c7e2512014-10-09 14:13:11 +0800231static void write_elf64_note(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800232{
233 Elf64_Phdr phdr;
Avi Kivitya8170e52012-10-23 12:30:10 +0200234 hwaddr begin = s->memory_offset - s->note_size;
Wen Congyang783e9b42012-05-07 12:10:47 +0800235 int ret;
236
237 memset(&phdr, 0, sizeof(Elf64_Phdr));
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200238 phdr.p_type = cpu_to_dump32(s, PT_NOTE);
239 phdr.p_offset = cpu_to_dump64(s, begin);
Wen Congyang783e9b42012-05-07 12:10:47 +0800240 phdr.p_paddr = 0;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200241 phdr.p_filesz = cpu_to_dump64(s, s->note_size);
242 phdr.p_memsz = cpu_to_dump64(s, s->note_size);
Wen Congyang783e9b42012-05-07 12:10:47 +0800243 phdr.p_vaddr = 0;
244
245 ret = fd_write_vmcore(&phdr, sizeof(Elf64_Phdr), s);
246 if (ret < 0) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200247 error_setg_errno(errp, -ret,
248 "dump: failed to write program header table");
Wen Congyang783e9b42012-05-07 12:10:47 +0800249 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800250}
251
Paolo Bonzini0bc3cd62013-04-08 17:29:59 +0200252static inline int cpu_index(CPUState *cpu)
253{
254 return cpu->cpu_index + 1;
255}
256
Marc-André Lureau903ef732017-09-11 18:59:25 +0200257static void write_guest_note(WriteCoreDumpFunction f, DumpState *s,
258 Error **errp)
259{
260 int ret;
261
262 if (s->guest_note) {
263 ret = f(s->guest_note, s->guest_note_size, s);
264 if (ret < 0) {
265 error_setg(errp, "dump: failed to write guest note");
266 }
267 }
268}
269
zhanghailiang4c7e2512014-10-09 14:13:11 +0800270static void write_elf64_notes(WriteCoreDumpFunction f, DumpState *s,
271 Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800272{
Andreas Färber0d342822012-12-17 07:12:13 +0100273 CPUState *cpu;
Wen Congyang783e9b42012-05-07 12:10:47 +0800274 int ret;
275 int id;
276
Andreas Färberbdc44642013-06-24 23:50:24 +0200277 CPU_FOREACH(cpu) {
Andreas Färber0d342822012-12-17 07:12:13 +0100278 id = cpu_index(cpu);
qiaonuohan6a519912014-02-18 14:11:26 +0800279 ret = cpu_write_elf64_note(f, cpu, id, s);
Wen Congyang783e9b42012-05-07 12:10:47 +0800280 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800281 error_setg(errp, "dump: failed to write elf notes");
zhanghailiang4c7e2512014-10-09 14:13:11 +0800282 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800283 }
284 }
285
Andreas Färberbdc44642013-06-24 23:50:24 +0200286 CPU_FOREACH(cpu) {
qiaonuohan6a519912014-02-18 14:11:26 +0800287 ret = cpu_write_elf64_qemunote(f, cpu, s);
Wen Congyang783e9b42012-05-07 12:10:47 +0800288 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800289 error_setg(errp, "dump: failed to write CPU status");
zhanghailiang4c7e2512014-10-09 14:13:11 +0800290 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800291 }
292 }
Marc-André Lureau903ef732017-09-11 18:59:25 +0200293
294 write_guest_note(f, s, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800295}
296
zhanghailiang4c7e2512014-10-09 14:13:11 +0800297static void write_elf32_note(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800298{
Avi Kivitya8170e52012-10-23 12:30:10 +0200299 hwaddr begin = s->memory_offset - s->note_size;
Wen Congyang783e9b42012-05-07 12:10:47 +0800300 Elf32_Phdr phdr;
Wen Congyang783e9b42012-05-07 12:10:47 +0800301 int ret;
302
303 memset(&phdr, 0, sizeof(Elf32_Phdr));
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200304 phdr.p_type = cpu_to_dump32(s, PT_NOTE);
305 phdr.p_offset = cpu_to_dump32(s, begin);
Wen Congyang783e9b42012-05-07 12:10:47 +0800306 phdr.p_paddr = 0;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200307 phdr.p_filesz = cpu_to_dump32(s, s->note_size);
308 phdr.p_memsz = cpu_to_dump32(s, s->note_size);
Wen Congyang783e9b42012-05-07 12:10:47 +0800309 phdr.p_vaddr = 0;
310
311 ret = fd_write_vmcore(&phdr, sizeof(Elf32_Phdr), s);
312 if (ret < 0) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200313 error_setg_errno(errp, -ret,
314 "dump: failed to write program header table");
Wen Congyang783e9b42012-05-07 12:10:47 +0800315 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800316}
317
zhanghailiang4c7e2512014-10-09 14:13:11 +0800318static void write_elf32_notes(WriteCoreDumpFunction f, DumpState *s,
319 Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800320{
Andreas Färber0d342822012-12-17 07:12:13 +0100321 CPUState *cpu;
Wen Congyang783e9b42012-05-07 12:10:47 +0800322 int ret;
323 int id;
324
Andreas Färberbdc44642013-06-24 23:50:24 +0200325 CPU_FOREACH(cpu) {
Andreas Färber0d342822012-12-17 07:12:13 +0100326 id = cpu_index(cpu);
qiaonuohan6a519912014-02-18 14:11:26 +0800327 ret = cpu_write_elf32_note(f, cpu, id, s);
Wen Congyang783e9b42012-05-07 12:10:47 +0800328 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800329 error_setg(errp, "dump: failed to write elf notes");
zhanghailiang4c7e2512014-10-09 14:13:11 +0800330 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800331 }
332 }
333
Andreas Färberbdc44642013-06-24 23:50:24 +0200334 CPU_FOREACH(cpu) {
qiaonuohan6a519912014-02-18 14:11:26 +0800335 ret = cpu_write_elf32_qemunote(f, cpu, s);
Wen Congyang783e9b42012-05-07 12:10:47 +0800336 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800337 error_setg(errp, "dump: failed to write CPU status");
zhanghailiang4c7e2512014-10-09 14:13:11 +0800338 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800339 }
340 }
Marc-André Lureau903ef732017-09-11 18:59:25 +0200341
342 write_guest_note(f, s, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800343}
344
zhanghailiang4c7e2512014-10-09 14:13:11 +0800345static void write_elf_section(DumpState *s, int type, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800346{
347 Elf32_Shdr shdr32;
348 Elf64_Shdr shdr64;
Wen Congyang783e9b42012-05-07 12:10:47 +0800349 int shdr_size;
350 void *shdr;
351 int ret;
352
353 if (type == 0) {
354 shdr_size = sizeof(Elf32_Shdr);
355 memset(&shdr32, 0, shdr_size);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200356 shdr32.sh_info = cpu_to_dump32(s, s->sh_info);
Wen Congyang783e9b42012-05-07 12:10:47 +0800357 shdr = &shdr32;
358 } else {
359 shdr_size = sizeof(Elf64_Shdr);
360 memset(&shdr64, 0, shdr_size);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200361 shdr64.sh_info = cpu_to_dump32(s, s->sh_info);
Wen Congyang783e9b42012-05-07 12:10:47 +0800362 shdr = &shdr64;
363 }
364
365 ret = fd_write_vmcore(&shdr, shdr_size, s);
366 if (ret < 0) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200367 error_setg_errno(errp, -ret,
368 "dump: failed to write section header table");
Wen Congyang783e9b42012-05-07 12:10:47 +0800369 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800370}
371
zhanghailiang4c7e2512014-10-09 14:13:11 +0800372static void write_data(DumpState *s, void *buf, int length, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800373{
374 int ret;
375
376 ret = fd_write_vmcore(buf, length, s);
377 if (ret < 0) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200378 error_setg_errno(errp, -ret, "dump: failed to save memory");
Peter Xu2264c2c2016-02-18 13:16:53 +0800379 } else {
380 s->written_size += length;
Wen Congyang783e9b42012-05-07 12:10:47 +0800381 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800382}
383
zhanghailiang4c7e2512014-10-09 14:13:11 +0800384/* write the memory to vmcore. 1 page per I/O. */
385static void write_memory(DumpState *s, GuestPhysBlock *block, ram_addr_t start,
386 int64_t size, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800387{
388 int64_t i;
zhanghailiang4c7e2512014-10-09 14:13:11 +0800389 Error *local_err = NULL;
Wen Congyang783e9b42012-05-07 12:10:47 +0800390
Andrew Jones8161bef2016-01-11 20:56:20 +0100391 for (i = 0; i < size / s->dump_info.page_size; i++) {
392 write_data(s, block->host_addr + start + i * s->dump_info.page_size,
393 s->dump_info.page_size, &local_err);
zhanghailiang4c7e2512014-10-09 14:13:11 +0800394 if (local_err) {
395 error_propagate(errp, local_err);
396 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800397 }
398 }
399
Andrew Jones8161bef2016-01-11 20:56:20 +0100400 if ((size % s->dump_info.page_size) != 0) {
401 write_data(s, block->host_addr + start + i * s->dump_info.page_size,
402 size % s->dump_info.page_size, &local_err);
zhanghailiang4c7e2512014-10-09 14:13:11 +0800403 if (local_err) {
404 error_propagate(errp, local_err);
405 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800406 }
407 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800408}
409
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200410/* get the memory's offset and size in the vmcore */
411static void get_offset_range(hwaddr phys_addr,
412 ram_addr_t mapping_length,
413 DumpState *s,
414 hwaddr *p_offset,
415 hwaddr *p_filesz)
Wen Congyang783e9b42012-05-07 12:10:47 +0800416{
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200417 GuestPhysBlock *block;
Avi Kivitya8170e52012-10-23 12:30:10 +0200418 hwaddr offset = s->memory_offset;
Wen Congyang783e9b42012-05-07 12:10:47 +0800419 int64_t size_in_block, start;
420
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200421 /* When the memory is not stored into vmcore, offset will be -1 */
422 *p_offset = -1;
423 *p_filesz = 0;
424
Wen Congyang783e9b42012-05-07 12:10:47 +0800425 if (s->has_filter) {
426 if (phys_addr < s->begin || phys_addr >= s->begin + s->length) {
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200427 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800428 }
429 }
430
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200431 QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
Wen Congyang783e9b42012-05-07 12:10:47 +0800432 if (s->has_filter) {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200433 if (block->target_start >= s->begin + s->length ||
434 block->target_end <= s->begin) {
Wen Congyang783e9b42012-05-07 12:10:47 +0800435 /* This block is out of the range */
436 continue;
437 }
438
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200439 if (s->begin <= block->target_start) {
440 start = block->target_start;
Wen Congyang783e9b42012-05-07 12:10:47 +0800441 } else {
442 start = s->begin;
443 }
444
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200445 size_in_block = block->target_end - start;
446 if (s->begin + s->length < block->target_end) {
447 size_in_block -= block->target_end - (s->begin + s->length);
Wen Congyang783e9b42012-05-07 12:10:47 +0800448 }
449 } else {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200450 start = block->target_start;
451 size_in_block = block->target_end - block->target_start;
Wen Congyang783e9b42012-05-07 12:10:47 +0800452 }
453
454 if (phys_addr >= start && phys_addr < start + size_in_block) {
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200455 *p_offset = phys_addr - start + offset;
456
457 /* The offset range mapped from the vmcore file must not spill over
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200458 * the GuestPhysBlock, clamp it. The rest of the mapping will be
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200459 * zero-filled in memory at load time; see
460 * <http://refspecs.linuxbase.org/elf/gabi4+/ch5.pheader.html>.
461 */
462 *p_filesz = phys_addr + mapping_length <= start + size_in_block ?
463 mapping_length :
464 size_in_block - (phys_addr - start);
465 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800466 }
467
468 offset += size_in_block;
469 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800470}
471
zhanghailiang4c7e2512014-10-09 14:13:11 +0800472static void write_elf_loads(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800473{
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200474 hwaddr offset, filesz;
Wen Congyang783e9b42012-05-07 12:10:47 +0800475 MemoryMapping *memory_mapping;
476 uint32_t phdr_index = 1;
Wen Congyang783e9b42012-05-07 12:10:47 +0800477 uint32_t max_index;
zhanghailiang4c7e2512014-10-09 14:13:11 +0800478 Error *local_err = NULL;
Wen Congyang783e9b42012-05-07 12:10:47 +0800479
480 if (s->have_section) {
481 max_index = s->sh_info;
482 } else {
483 max_index = s->phdr_num;
484 }
485
486 QTAILQ_FOREACH(memory_mapping, &s->list.head, next) {
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200487 get_offset_range(memory_mapping->phys_addr,
488 memory_mapping->length,
489 s, &offset, &filesz);
Wen Congyang783e9b42012-05-07 12:10:47 +0800490 if (s->dump_info.d_class == ELFCLASS64) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800491 write_elf64_load(s, memory_mapping, phdr_index++, offset,
492 filesz, &local_err);
Wen Congyang783e9b42012-05-07 12:10:47 +0800493 } else {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800494 write_elf32_load(s, memory_mapping, phdr_index++, offset,
495 filesz, &local_err);
Wen Congyang783e9b42012-05-07 12:10:47 +0800496 }
497
zhanghailiang4c7e2512014-10-09 14:13:11 +0800498 if (local_err) {
499 error_propagate(errp, local_err);
500 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800501 }
502
503 if (phdr_index >= max_index) {
504 break;
505 }
506 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800507}
508
509/* write elf header, PT_NOTE and elf note to vmcore. */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800510static void dump_begin(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800511{
zhanghailiang4c7e2512014-10-09 14:13:11 +0800512 Error *local_err = NULL;
Wen Congyang783e9b42012-05-07 12:10:47 +0800513
514 /*
515 * the vmcore's format is:
516 * --------------
517 * | elf header |
518 * --------------
519 * | PT_NOTE |
520 * --------------
521 * | PT_LOAD |
522 * --------------
523 * | ...... |
524 * --------------
525 * | PT_LOAD |
526 * --------------
527 * | sec_hdr |
528 * --------------
529 * | elf note |
530 * --------------
531 * | memory |
532 * --------------
533 *
534 * we only know where the memory is saved after we write elf note into
535 * vmcore.
536 */
537
538 /* write elf header to vmcore */
539 if (s->dump_info.d_class == ELFCLASS64) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800540 write_elf64_header(s, &local_err);
Wen Congyang783e9b42012-05-07 12:10:47 +0800541 } else {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800542 write_elf32_header(s, &local_err);
Wen Congyang783e9b42012-05-07 12:10:47 +0800543 }
zhanghailiang4c7e2512014-10-09 14:13:11 +0800544 if (local_err) {
545 error_propagate(errp, local_err);
546 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800547 }
548
549 if (s->dump_info.d_class == ELFCLASS64) {
550 /* write PT_NOTE to vmcore */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800551 write_elf64_note(s, &local_err);
552 if (local_err) {
553 error_propagate(errp, local_err);
554 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800555 }
556
557 /* write all PT_LOAD to vmcore */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800558 write_elf_loads(s, &local_err);
559 if (local_err) {
560 error_propagate(errp, local_err);
561 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800562 }
563
564 /* write section to vmcore */
565 if (s->have_section) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800566 write_elf_section(s, 1, &local_err);
567 if (local_err) {
568 error_propagate(errp, local_err);
569 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800570 }
571 }
572
573 /* write notes to vmcore */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800574 write_elf64_notes(fd_write_vmcore, s, &local_err);
575 if (local_err) {
576 error_propagate(errp, local_err);
577 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800578 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800579 } else {
580 /* write PT_NOTE to vmcore */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800581 write_elf32_note(s, &local_err);
582 if (local_err) {
583 error_propagate(errp, local_err);
584 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800585 }
586
587 /* write all PT_LOAD to vmcore */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800588 write_elf_loads(s, &local_err);
589 if (local_err) {
590 error_propagate(errp, local_err);
591 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800592 }
593
594 /* write section to vmcore */
595 if (s->have_section) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800596 write_elf_section(s, 0, &local_err);
597 if (local_err) {
598 error_propagate(errp, local_err);
599 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800600 }
601 }
602
603 /* write notes to vmcore */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800604 write_elf32_notes(fd_write_vmcore, s, &local_err);
605 if (local_err) {
606 error_propagate(errp, local_err);
607 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800608 }
609 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800610}
611
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200612static int get_next_block(DumpState *s, GuestPhysBlock *block)
Wen Congyang783e9b42012-05-07 12:10:47 +0800613{
614 while (1) {
Paolo Bonzinia3161032012-11-14 15:54:48 +0100615 block = QTAILQ_NEXT(block, next);
Wen Congyang783e9b42012-05-07 12:10:47 +0800616 if (!block) {
617 /* no more block */
618 return 1;
619 }
620
621 s->start = 0;
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200622 s->next_block = block;
Wen Congyang783e9b42012-05-07 12:10:47 +0800623 if (s->has_filter) {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200624 if (block->target_start >= s->begin + s->length ||
625 block->target_end <= s->begin) {
Wen Congyang783e9b42012-05-07 12:10:47 +0800626 /* This block is out of the range */
627 continue;
628 }
629
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200630 if (s->begin > block->target_start) {
631 s->start = s->begin - block->target_start;
Wen Congyang783e9b42012-05-07 12:10:47 +0800632 }
633 }
634
635 return 0;
636 }
637}
638
639/* write all memory to vmcore */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800640static void dump_iterate(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800641{
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200642 GuestPhysBlock *block;
Wen Congyang783e9b42012-05-07 12:10:47 +0800643 int64_t size;
zhanghailiang4c7e2512014-10-09 14:13:11 +0800644 Error *local_err = NULL;
Wen Congyang783e9b42012-05-07 12:10:47 +0800645
Gonglei08a655b2014-10-30 14:01:17 +0800646 do {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200647 block = s->next_block;
Wen Congyang783e9b42012-05-07 12:10:47 +0800648
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200649 size = block->target_end - block->target_start;
Wen Congyang783e9b42012-05-07 12:10:47 +0800650 if (s->has_filter) {
651 size -= s->start;
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200652 if (s->begin + s->length < block->target_end) {
653 size -= block->target_end - (s->begin + s->length);
Wen Congyang783e9b42012-05-07 12:10:47 +0800654 }
655 }
zhanghailiang4c7e2512014-10-09 14:13:11 +0800656 write_memory(s, block, s->start, size, &local_err);
657 if (local_err) {
658 error_propagate(errp, local_err);
659 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800660 }
661
Gonglei08a655b2014-10-30 14:01:17 +0800662 } while (!get_next_block(s, block));
Wen Congyang783e9b42012-05-07 12:10:47 +0800663}
664
zhanghailiang4c7e2512014-10-09 14:13:11 +0800665static void create_vmcore(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800666{
zhanghailiang4c7e2512014-10-09 14:13:11 +0800667 Error *local_err = NULL;
Wen Congyang783e9b42012-05-07 12:10:47 +0800668
zhanghailiang4c7e2512014-10-09 14:13:11 +0800669 dump_begin(s, &local_err);
670 if (local_err) {
671 error_propagate(errp, local_err);
672 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800673 }
674
zhanghailiang4c7e2512014-10-09 14:13:11 +0800675 dump_iterate(s, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800676}
677
qiaonuohanfda05382014-02-18 14:11:27 +0800678static int write_start_flat_header(int fd)
679{
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200680 MakedumpfileHeader *mh;
qiaonuohanfda05382014-02-18 14:11:27 +0800681 int ret = 0;
682
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200683 QEMU_BUILD_BUG_ON(sizeof *mh > MAX_SIZE_MDF_HEADER);
684 mh = g_malloc0(MAX_SIZE_MDF_HEADER);
qiaonuohanfda05382014-02-18 14:11:27 +0800685
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200686 memcpy(mh->signature, MAKEDUMPFILE_SIGNATURE,
687 MIN(sizeof mh->signature, sizeof MAKEDUMPFILE_SIGNATURE));
qiaonuohanfda05382014-02-18 14:11:27 +0800688
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200689 mh->type = cpu_to_be64(TYPE_FLAT_HEADER);
690 mh->version = cpu_to_be64(VERSION_FLAT_HEADER);
qiaonuohanfda05382014-02-18 14:11:27 +0800691
692 size_t written_size;
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200693 written_size = qemu_write_full(fd, mh, MAX_SIZE_MDF_HEADER);
qiaonuohanfda05382014-02-18 14:11:27 +0800694 if (written_size != MAX_SIZE_MDF_HEADER) {
695 ret = -1;
696 }
697
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200698 g_free(mh);
qiaonuohanfda05382014-02-18 14:11:27 +0800699 return ret;
700}
701
702static int write_end_flat_header(int fd)
703{
704 MakedumpfileDataHeader mdh;
705
706 mdh.offset = END_FLAG_FLAT_HEADER;
707 mdh.buf_size = END_FLAG_FLAT_HEADER;
708
709 size_t written_size;
710 written_size = qemu_write_full(fd, &mdh, sizeof(mdh));
711 if (written_size != sizeof(mdh)) {
712 return -1;
713 }
714
715 return 0;
716}
717
qiaonuohan5d31bab2014-02-18 14:11:28 +0800718static int write_buffer(int fd, off_t offset, const void *buf, size_t size)
719{
720 size_t written_size;
721 MakedumpfileDataHeader mdh;
722
723 mdh.offset = cpu_to_be64(offset);
724 mdh.buf_size = cpu_to_be64(size);
725
726 written_size = qemu_write_full(fd, &mdh, sizeof(mdh));
727 if (written_size != sizeof(mdh)) {
728 return -1;
729 }
730
731 written_size = qemu_write_full(fd, buf, size);
732 if (written_size != size) {
733 return -1;
734 }
735
736 return 0;
737}
738
qiaonuohan4835ef72014-02-18 14:11:29 +0800739static int buf_write_note(const void *buf, size_t size, void *opaque)
740{
741 DumpState *s = opaque;
742
743 /* note_buf is not enough */
744 if (s->note_buf_offset + size > s->note_size) {
745 return -1;
746 }
747
748 memcpy(s->note_buf + s->note_buf_offset, buf, size);
749
750 s->note_buf_offset += size;
751
752 return 0;
753}
754
Marc-André Lureau903ef732017-09-11 18:59:25 +0200755/*
756 * This function retrieves various sizes from an elf header.
757 *
758 * @note has to be a valid ELF note. The return sizes are unmodified
759 * (not padded or rounded up to be multiple of 4).
760 */
761static void get_note_sizes(DumpState *s, const void *note,
762 uint64_t *note_head_size,
763 uint64_t *name_size,
764 uint64_t *desc_size)
765{
766 uint64_t note_head_sz;
767 uint64_t name_sz;
768 uint64_t desc_sz;
769
770 if (s->dump_info.d_class == ELFCLASS64) {
771 const Elf64_Nhdr *hdr = note;
772 note_head_sz = sizeof(Elf64_Nhdr);
773 name_sz = tswap64(hdr->n_namesz);
774 desc_sz = tswap64(hdr->n_descsz);
775 } else {
776 const Elf32_Nhdr *hdr = note;
777 note_head_sz = sizeof(Elf32_Nhdr);
778 name_sz = tswap32(hdr->n_namesz);
779 desc_sz = tswap32(hdr->n_descsz);
780 }
781
782 if (note_head_size) {
783 *note_head_size = note_head_sz;
784 }
785 if (name_size) {
786 *name_size = name_sz;
787 }
788 if (desc_size) {
789 *desc_size = desc_sz;
790 }
791}
792
Marc-André Lureaud9feb512017-09-11 18:59:26 +0200793static bool note_name_equal(DumpState *s,
794 const uint8_t *note, const char *name)
795{
796 int len = strlen(name) + 1;
797 uint64_t head_size, name_size;
798
799 get_note_sizes(s, note, &head_size, &name_size, NULL);
800 head_size = ROUND_UP(head_size, 4);
801
Marc-André Lureauc983ca82017-12-12 15:53:59 +0100802 return name_size == len && memcmp(note + head_size, name, len) == 0;
Marc-André Lureaud9feb512017-09-11 18:59:26 +0200803}
804
qiaonuohan298f1162014-02-18 14:11:32 +0800805/* write common header, sub header and elf note to vmcore */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800806static void create_header32(DumpState *s, Error **errp)
qiaonuohan298f1162014-02-18 14:11:32 +0800807{
qiaonuohan298f1162014-02-18 14:11:32 +0800808 DiskDumpHeader32 *dh = NULL;
809 KdumpSubHeader32 *kh = NULL;
810 size_t size;
qiaonuohan298f1162014-02-18 14:11:32 +0800811 uint32_t block_size;
812 uint32_t sub_hdr_size;
813 uint32_t bitmap_blocks;
814 uint32_t status = 0;
815 uint64_t offset_note;
zhanghailiang4c7e2512014-10-09 14:13:11 +0800816 Error *local_err = NULL;
qiaonuohan298f1162014-02-18 14:11:32 +0800817
818 /* write common header, the version of kdump-compressed format is 6th */
819 size = sizeof(DiskDumpHeader32);
820 dh = g_malloc0(size);
821
Eric Blake84c868f2018-03-27 15:21:51 -0500822 memcpy(dh->signature, KDUMP_SIGNATURE, SIG_LEN);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200823 dh->header_version = cpu_to_dump32(s, 6);
Andrew Jones8161bef2016-01-11 20:56:20 +0100824 block_size = s->dump_info.page_size;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200825 dh->block_size = cpu_to_dump32(s, block_size);
qiaonuohan298f1162014-02-18 14:11:32 +0800826 sub_hdr_size = sizeof(struct KdumpSubHeader32) + s->note_size;
827 sub_hdr_size = DIV_ROUND_UP(sub_hdr_size, block_size);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200828 dh->sub_hdr_size = cpu_to_dump32(s, sub_hdr_size);
qiaonuohan298f1162014-02-18 14:11:32 +0800829 /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200830 dh->max_mapnr = cpu_to_dump32(s, MIN(s->max_mapnr, UINT_MAX));
831 dh->nr_cpus = cpu_to_dump32(s, s->nr_cpus);
qiaonuohan298f1162014-02-18 14:11:32 +0800832 bitmap_blocks = DIV_ROUND_UP(s->len_dump_bitmap, block_size) * 2;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200833 dh->bitmap_blocks = cpu_to_dump32(s, bitmap_blocks);
qiaonuohan4ab23a92014-02-18 14:11:37 +0800834 strncpy(dh->utsname.machine, ELF_MACHINE_UNAME, sizeof(dh->utsname.machine));
qiaonuohan298f1162014-02-18 14:11:32 +0800835
836 if (s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) {
837 status |= DUMP_DH_COMPRESSED_ZLIB;
838 }
839#ifdef CONFIG_LZO
840 if (s->flag_compress & DUMP_DH_COMPRESSED_LZO) {
841 status |= DUMP_DH_COMPRESSED_LZO;
842 }
843#endif
844#ifdef CONFIG_SNAPPY
845 if (s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) {
846 status |= DUMP_DH_COMPRESSED_SNAPPY;
847 }
848#endif
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200849 dh->status = cpu_to_dump32(s, status);
qiaonuohan298f1162014-02-18 14:11:32 +0800850
851 if (write_buffer(s->fd, 0, dh, size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800852 error_setg(errp, "dump: failed to write disk dump header");
qiaonuohan298f1162014-02-18 14:11:32 +0800853 goto out;
854 }
855
856 /* write sub header */
857 size = sizeof(KdumpSubHeader32);
858 kh = g_malloc0(size);
859
860 /* 64bit max_mapnr_64 */
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200861 kh->max_mapnr_64 = cpu_to_dump64(s, s->max_mapnr);
Andrew Jonesb6e05aa2016-01-11 20:56:21 +0100862 kh->phys_base = cpu_to_dump32(s, s->dump_info.phys_base);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200863 kh->dump_level = cpu_to_dump32(s, DUMP_LEVEL);
qiaonuohan298f1162014-02-18 14:11:32 +0800864
865 offset_note = DISKDUMP_HEADER_BLOCKS * block_size + size;
Marc-André Lureau9ada5752017-09-11 18:59:27 +0200866 if (s->guest_note &&
867 note_name_equal(s, s->guest_note, "VMCOREINFO")) {
868 uint64_t hsize, name_size, size_vmcoreinfo_desc, offset_vmcoreinfo;
869
870 get_note_sizes(s, s->guest_note,
871 &hsize, &name_size, &size_vmcoreinfo_desc);
872 offset_vmcoreinfo = offset_note + s->note_size - s->guest_note_size +
873 (DIV_ROUND_UP(hsize, 4) + DIV_ROUND_UP(name_size, 4)) * 4;
874 kh->offset_vmcoreinfo = cpu_to_dump64(s, offset_vmcoreinfo);
875 kh->size_vmcoreinfo = cpu_to_dump32(s, size_vmcoreinfo_desc);
876 }
877
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200878 kh->offset_note = cpu_to_dump64(s, offset_note);
879 kh->note_size = cpu_to_dump32(s, s->note_size);
qiaonuohan298f1162014-02-18 14:11:32 +0800880
881 if (write_buffer(s->fd, DISKDUMP_HEADER_BLOCKS *
882 block_size, kh, size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800883 error_setg(errp, "dump: failed to write kdump sub header");
qiaonuohan298f1162014-02-18 14:11:32 +0800884 goto out;
885 }
886
887 /* write note */
888 s->note_buf = g_malloc0(s->note_size);
889 s->note_buf_offset = 0;
890
891 /* use s->note_buf to store notes temporarily */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800892 write_elf32_notes(buf_write_note, s, &local_err);
893 if (local_err) {
894 error_propagate(errp, local_err);
qiaonuohan298f1162014-02-18 14:11:32 +0800895 goto out;
896 }
qiaonuohan298f1162014-02-18 14:11:32 +0800897 if (write_buffer(s->fd, offset_note, s->note_buf,
898 s->note_size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800899 error_setg(errp, "dump: failed to write notes");
qiaonuohan298f1162014-02-18 14:11:32 +0800900 goto out;
901 }
902
903 /* get offset of dump_bitmap */
904 s->offset_dump_bitmap = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size) *
905 block_size;
906
907 /* get offset of page */
908 s->offset_page = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size + bitmap_blocks) *
909 block_size;
910
911out:
912 g_free(dh);
913 g_free(kh);
914 g_free(s->note_buf);
qiaonuohan298f1162014-02-18 14:11:32 +0800915}
916
917/* write common header, sub header and elf note to vmcore */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800918static void create_header64(DumpState *s, Error **errp)
qiaonuohan298f1162014-02-18 14:11:32 +0800919{
qiaonuohan298f1162014-02-18 14:11:32 +0800920 DiskDumpHeader64 *dh = NULL;
921 KdumpSubHeader64 *kh = NULL;
922 size_t size;
qiaonuohan298f1162014-02-18 14:11:32 +0800923 uint32_t block_size;
924 uint32_t sub_hdr_size;
925 uint32_t bitmap_blocks;
926 uint32_t status = 0;
927 uint64_t offset_note;
zhanghailiang4c7e2512014-10-09 14:13:11 +0800928 Error *local_err = NULL;
qiaonuohan298f1162014-02-18 14:11:32 +0800929
930 /* write common header, the version of kdump-compressed format is 6th */
931 size = sizeof(DiskDumpHeader64);
932 dh = g_malloc0(size);
933
Eric Blake84c868f2018-03-27 15:21:51 -0500934 memcpy(dh->signature, KDUMP_SIGNATURE, SIG_LEN);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200935 dh->header_version = cpu_to_dump32(s, 6);
Andrew Jones8161bef2016-01-11 20:56:20 +0100936 block_size = s->dump_info.page_size;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200937 dh->block_size = cpu_to_dump32(s, block_size);
qiaonuohan298f1162014-02-18 14:11:32 +0800938 sub_hdr_size = sizeof(struct KdumpSubHeader64) + s->note_size;
939 sub_hdr_size = DIV_ROUND_UP(sub_hdr_size, block_size);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200940 dh->sub_hdr_size = cpu_to_dump32(s, sub_hdr_size);
qiaonuohan298f1162014-02-18 14:11:32 +0800941 /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200942 dh->max_mapnr = cpu_to_dump32(s, MIN(s->max_mapnr, UINT_MAX));
943 dh->nr_cpus = cpu_to_dump32(s, s->nr_cpus);
qiaonuohan298f1162014-02-18 14:11:32 +0800944 bitmap_blocks = DIV_ROUND_UP(s->len_dump_bitmap, block_size) * 2;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200945 dh->bitmap_blocks = cpu_to_dump32(s, bitmap_blocks);
qiaonuohan4ab23a92014-02-18 14:11:37 +0800946 strncpy(dh->utsname.machine, ELF_MACHINE_UNAME, sizeof(dh->utsname.machine));
qiaonuohan298f1162014-02-18 14:11:32 +0800947
948 if (s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) {
949 status |= DUMP_DH_COMPRESSED_ZLIB;
950 }
951#ifdef CONFIG_LZO
952 if (s->flag_compress & DUMP_DH_COMPRESSED_LZO) {
953 status |= DUMP_DH_COMPRESSED_LZO;
954 }
955#endif
956#ifdef CONFIG_SNAPPY
957 if (s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) {
958 status |= DUMP_DH_COMPRESSED_SNAPPY;
959 }
960#endif
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200961 dh->status = cpu_to_dump32(s, status);
qiaonuohan298f1162014-02-18 14:11:32 +0800962
963 if (write_buffer(s->fd, 0, dh, size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800964 error_setg(errp, "dump: failed to write disk dump header");
qiaonuohan298f1162014-02-18 14:11:32 +0800965 goto out;
966 }
967
968 /* write sub header */
969 size = sizeof(KdumpSubHeader64);
970 kh = g_malloc0(size);
971
972 /* 64bit max_mapnr_64 */
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200973 kh->max_mapnr_64 = cpu_to_dump64(s, s->max_mapnr);
Andrew Jonesb6e05aa2016-01-11 20:56:21 +0100974 kh->phys_base = cpu_to_dump64(s, s->dump_info.phys_base);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200975 kh->dump_level = cpu_to_dump32(s, DUMP_LEVEL);
qiaonuohan298f1162014-02-18 14:11:32 +0800976
977 offset_note = DISKDUMP_HEADER_BLOCKS * block_size + size;
Marc-André Lureau9ada5752017-09-11 18:59:27 +0200978 if (s->guest_note &&
979 note_name_equal(s, s->guest_note, "VMCOREINFO")) {
980 uint64_t hsize, name_size, size_vmcoreinfo_desc, offset_vmcoreinfo;
981
982 get_note_sizes(s, s->guest_note,
983 &hsize, &name_size, &size_vmcoreinfo_desc);
984 offset_vmcoreinfo = offset_note + s->note_size - s->guest_note_size +
985 (DIV_ROUND_UP(hsize, 4) + DIV_ROUND_UP(name_size, 4)) * 4;
986 kh->offset_vmcoreinfo = cpu_to_dump64(s, offset_vmcoreinfo);
987 kh->size_vmcoreinfo = cpu_to_dump64(s, size_vmcoreinfo_desc);
988 }
989
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200990 kh->offset_note = cpu_to_dump64(s, offset_note);
991 kh->note_size = cpu_to_dump64(s, s->note_size);
qiaonuohan298f1162014-02-18 14:11:32 +0800992
993 if (write_buffer(s->fd, DISKDUMP_HEADER_BLOCKS *
994 block_size, kh, size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800995 error_setg(errp, "dump: failed to write kdump sub header");
qiaonuohan298f1162014-02-18 14:11:32 +0800996 goto out;
997 }
998
999 /* write note */
1000 s->note_buf = g_malloc0(s->note_size);
1001 s->note_buf_offset = 0;
1002
1003 /* use s->note_buf to store notes temporarily */
zhanghailiang4c7e2512014-10-09 14:13:11 +08001004 write_elf64_notes(buf_write_note, s, &local_err);
1005 if (local_err) {
1006 error_propagate(errp, local_err);
qiaonuohan298f1162014-02-18 14:11:32 +08001007 goto out;
1008 }
1009
1010 if (write_buffer(s->fd, offset_note, s->note_buf,
1011 s->note_size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001012 error_setg(errp, "dump: failed to write notes");
qiaonuohan298f1162014-02-18 14:11:32 +08001013 goto out;
1014 }
1015
1016 /* get offset of dump_bitmap */
1017 s->offset_dump_bitmap = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size) *
1018 block_size;
1019
1020 /* get offset of page */
1021 s->offset_page = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size + bitmap_blocks) *
1022 block_size;
1023
1024out:
1025 g_free(dh);
1026 g_free(kh);
1027 g_free(s->note_buf);
qiaonuohan298f1162014-02-18 14:11:32 +08001028}
1029
zhanghailiang4c7e2512014-10-09 14:13:11 +08001030static void write_dump_header(DumpState *s, Error **errp)
qiaonuohan298f1162014-02-18 14:11:32 +08001031{
zhanghailiang4c7e2512014-10-09 14:13:11 +08001032 Error *local_err = NULL;
1033
Laszlo Ersek24aeeac2014-05-20 13:39:45 +02001034 if (s->dump_info.d_class == ELFCLASS32) {
zhanghailiang4c7e2512014-10-09 14:13:11 +08001035 create_header32(s, &local_err);
qiaonuohan298f1162014-02-18 14:11:32 +08001036 } else {
zhanghailiang4c7e2512014-10-09 14:13:11 +08001037 create_header64(s, &local_err);
1038 }
Eduardo Habkost621ff942016-06-13 18:57:56 -03001039 error_propagate(errp, local_err);
qiaonuohan298f1162014-02-18 14:11:32 +08001040}
1041
Andrew Jones8161bef2016-01-11 20:56:20 +01001042static size_t dump_bitmap_get_bufsize(DumpState *s)
1043{
1044 return s->dump_info.page_size;
1045}
1046
qiaonuohand0686c72014-02-18 14:11:33 +08001047/*
1048 * set dump_bitmap sequencely. the bit before last_pfn is not allowed to be
1049 * rewritten, so if need to set the first bit, set last_pfn and pfn to 0.
1050 * set_dump_bitmap will always leave the recently set bit un-sync. And setting
1051 * (last bit + sizeof(buf) * 8) to 0 will do flushing the content in buf into
1052 * vmcore, ie. synchronizing un-sync bit into vmcore.
1053 */
1054static int set_dump_bitmap(uint64_t last_pfn, uint64_t pfn, bool value,
1055 uint8_t *buf, DumpState *s)
1056{
1057 off_t old_offset, new_offset;
1058 off_t offset_bitmap1, offset_bitmap2;
1059 uint32_t byte, bit;
Andrew Jones8161bef2016-01-11 20:56:20 +01001060 size_t bitmap_bufsize = dump_bitmap_get_bufsize(s);
1061 size_t bits_per_buf = bitmap_bufsize * CHAR_BIT;
qiaonuohand0686c72014-02-18 14:11:33 +08001062
1063 /* should not set the previous place */
1064 assert(last_pfn <= pfn);
1065
1066 /*
1067 * if the bit needed to be set is not cached in buf, flush the data in buf
1068 * to vmcore firstly.
1069 * making new_offset be bigger than old_offset can also sync remained data
1070 * into vmcore.
1071 */
Andrew Jones8161bef2016-01-11 20:56:20 +01001072 old_offset = bitmap_bufsize * (last_pfn / bits_per_buf);
1073 new_offset = bitmap_bufsize * (pfn / bits_per_buf);
qiaonuohand0686c72014-02-18 14:11:33 +08001074
1075 while (old_offset < new_offset) {
1076 /* calculate the offset and write dump_bitmap */
1077 offset_bitmap1 = s->offset_dump_bitmap + old_offset;
1078 if (write_buffer(s->fd, offset_bitmap1, buf,
Andrew Jones8161bef2016-01-11 20:56:20 +01001079 bitmap_bufsize) < 0) {
qiaonuohand0686c72014-02-18 14:11:33 +08001080 return -1;
1081 }
1082
1083 /* dump level 1 is chosen, so 1st and 2nd bitmap are same */
1084 offset_bitmap2 = s->offset_dump_bitmap + s->len_dump_bitmap +
1085 old_offset;
1086 if (write_buffer(s->fd, offset_bitmap2, buf,
Andrew Jones8161bef2016-01-11 20:56:20 +01001087 bitmap_bufsize) < 0) {
qiaonuohand0686c72014-02-18 14:11:33 +08001088 return -1;
1089 }
1090
Andrew Jones8161bef2016-01-11 20:56:20 +01001091 memset(buf, 0, bitmap_bufsize);
1092 old_offset += bitmap_bufsize;
qiaonuohand0686c72014-02-18 14:11:33 +08001093 }
1094
1095 /* get the exact place of the bit in the buf, and set it */
Andrew Jones8161bef2016-01-11 20:56:20 +01001096 byte = (pfn % bits_per_buf) / CHAR_BIT;
1097 bit = (pfn % bits_per_buf) % CHAR_BIT;
qiaonuohand0686c72014-02-18 14:11:33 +08001098 if (value) {
1099 buf[byte] |= 1u << bit;
1100 } else {
1101 buf[byte] &= ~(1u << bit);
1102 }
1103
1104 return 0;
1105}
1106
Andrew Jones8161bef2016-01-11 20:56:20 +01001107static uint64_t dump_paddr_to_pfn(DumpState *s, uint64_t addr)
1108{
1109 int target_page_shift = ctz32(s->dump_info.page_size);
1110
1111 return (addr >> target_page_shift) - ARCH_PFN_OFFSET;
1112}
1113
1114static uint64_t dump_pfn_to_paddr(DumpState *s, uint64_t pfn)
1115{
1116 int target_page_shift = ctz32(s->dump_info.page_size);
1117
1118 return (pfn + ARCH_PFN_OFFSET) << target_page_shift;
1119}
1120
qiaonuohand0686c72014-02-18 14:11:33 +08001121/*
1122 * exam every page and return the page frame number and the address of the page.
1123 * bufptr can be NULL. note: the blocks here is supposed to reflect guest-phys
1124 * blocks, so block->target_start and block->target_end should be interal
1125 * multiples of the target page size.
1126 */
1127static bool get_next_page(GuestPhysBlock **blockptr, uint64_t *pfnptr,
1128 uint8_t **bufptr, DumpState *s)
1129{
1130 GuestPhysBlock *block = *blockptr;
Andrew Jones8161bef2016-01-11 20:56:20 +01001131 hwaddr addr, target_page_mask = ~((hwaddr)s->dump_info.page_size - 1);
qiaonuohand0686c72014-02-18 14:11:33 +08001132 uint8_t *buf;
1133
1134 /* block == NULL means the start of the iteration */
1135 if (!block) {
1136 block = QTAILQ_FIRST(&s->guest_phys_blocks.head);
1137 *blockptr = block;
Andrew Jones8161bef2016-01-11 20:56:20 +01001138 assert((block->target_start & ~target_page_mask) == 0);
1139 assert((block->target_end & ~target_page_mask) == 0);
1140 *pfnptr = dump_paddr_to_pfn(s, block->target_start);
qiaonuohand0686c72014-02-18 14:11:33 +08001141 if (bufptr) {
1142 *bufptr = block->host_addr;
1143 }
1144 return true;
1145 }
1146
1147 *pfnptr = *pfnptr + 1;
Andrew Jones8161bef2016-01-11 20:56:20 +01001148 addr = dump_pfn_to_paddr(s, *pfnptr);
qiaonuohand0686c72014-02-18 14:11:33 +08001149
1150 if ((addr >= block->target_start) &&
Andrew Jones8161bef2016-01-11 20:56:20 +01001151 (addr + s->dump_info.page_size <= block->target_end)) {
qiaonuohand0686c72014-02-18 14:11:33 +08001152 buf = block->host_addr + (addr - block->target_start);
1153 } else {
1154 /* the next page is in the next block */
1155 block = QTAILQ_NEXT(block, next);
1156 *blockptr = block;
1157 if (!block) {
1158 return false;
1159 }
Andrew Jones8161bef2016-01-11 20:56:20 +01001160 assert((block->target_start & ~target_page_mask) == 0);
1161 assert((block->target_end & ~target_page_mask) == 0);
1162 *pfnptr = dump_paddr_to_pfn(s, block->target_start);
qiaonuohand0686c72014-02-18 14:11:33 +08001163 buf = block->host_addr;
1164 }
1165
1166 if (bufptr) {
1167 *bufptr = buf;
1168 }
1169
1170 return true;
1171}
1172
zhanghailiang4c7e2512014-10-09 14:13:11 +08001173static void write_dump_bitmap(DumpState *s, Error **errp)
qiaonuohand0686c72014-02-18 14:11:33 +08001174{
1175 int ret = 0;
1176 uint64_t last_pfn, pfn;
1177 void *dump_bitmap_buf;
1178 size_t num_dumpable;
1179 GuestPhysBlock *block_iter = NULL;
Andrew Jones8161bef2016-01-11 20:56:20 +01001180 size_t bitmap_bufsize = dump_bitmap_get_bufsize(s);
1181 size_t bits_per_buf = bitmap_bufsize * CHAR_BIT;
qiaonuohand0686c72014-02-18 14:11:33 +08001182
1183 /* dump_bitmap_buf is used to store dump_bitmap temporarily */
Andrew Jones8161bef2016-01-11 20:56:20 +01001184 dump_bitmap_buf = g_malloc0(bitmap_bufsize);
qiaonuohand0686c72014-02-18 14:11:33 +08001185
1186 num_dumpable = 0;
1187 last_pfn = 0;
1188
1189 /*
1190 * exam memory page by page, and set the bit in dump_bitmap corresponded
1191 * to the existing page.
1192 */
1193 while (get_next_page(&block_iter, &pfn, NULL, s)) {
1194 ret = set_dump_bitmap(last_pfn, pfn, true, dump_bitmap_buf, s);
1195 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001196 error_setg(errp, "dump: failed to set dump_bitmap");
qiaonuohand0686c72014-02-18 14:11:33 +08001197 goto out;
1198 }
1199
1200 last_pfn = pfn;
1201 num_dumpable++;
1202 }
1203
1204 /*
1205 * set_dump_bitmap will always leave the recently set bit un-sync. Here we
Andrew Jones8161bef2016-01-11 20:56:20 +01001206 * set the remaining bits from last_pfn to the end of the bitmap buffer to
1207 * 0. With those set, the un-sync bit will be synchronized into the vmcore.
qiaonuohand0686c72014-02-18 14:11:33 +08001208 */
1209 if (num_dumpable > 0) {
Andrew Jones8161bef2016-01-11 20:56:20 +01001210 ret = set_dump_bitmap(last_pfn, last_pfn + bits_per_buf, false,
qiaonuohand0686c72014-02-18 14:11:33 +08001211 dump_bitmap_buf, s);
1212 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001213 error_setg(errp, "dump: failed to sync dump_bitmap");
qiaonuohand0686c72014-02-18 14:11:33 +08001214 goto out;
1215 }
1216 }
1217
1218 /* number of dumpable pages that will be dumped later */
1219 s->num_dumpable = num_dumpable;
1220
1221out:
1222 g_free(dump_bitmap_buf);
qiaonuohand0686c72014-02-18 14:11:33 +08001223}
1224
qiaonuohan64cfba62014-02-18 14:11:34 +08001225static void prepare_data_cache(DataCache *data_cache, DumpState *s,
1226 off_t offset)
1227{
1228 data_cache->fd = s->fd;
1229 data_cache->data_size = 0;
Andrew Jones8161bef2016-01-11 20:56:20 +01001230 data_cache->buf_size = 4 * dump_bitmap_get_bufsize(s);
1231 data_cache->buf = g_malloc0(data_cache->buf_size);
qiaonuohan64cfba62014-02-18 14:11:34 +08001232 data_cache->offset = offset;
1233}
1234
1235static int write_cache(DataCache *dc, const void *buf, size_t size,
1236 bool flag_sync)
1237{
1238 /*
1239 * dc->buf_size should not be less than size, otherwise dc will never be
1240 * enough
1241 */
1242 assert(size <= dc->buf_size);
1243
1244 /*
1245 * if flag_sync is set, synchronize data in dc->buf into vmcore.
1246 * otherwise check if the space is enough for caching data in buf, if not,
1247 * write the data in dc->buf to dc->fd and reset dc->buf
1248 */
1249 if ((!flag_sync && dc->data_size + size > dc->buf_size) ||
1250 (flag_sync && dc->data_size > 0)) {
1251 if (write_buffer(dc->fd, dc->offset, dc->buf, dc->data_size) < 0) {
1252 return -1;
1253 }
1254
1255 dc->offset += dc->data_size;
1256 dc->data_size = 0;
1257 }
1258
1259 if (!flag_sync) {
1260 memcpy(dc->buf + dc->data_size, buf, size);
1261 dc->data_size += size;
1262 }
1263
1264 return 0;
1265}
1266
1267static void free_data_cache(DataCache *data_cache)
1268{
1269 g_free(data_cache->buf);
1270}
1271
qiaonuohand12f57e2014-02-18 14:11:35 +08001272static size_t get_len_buf_out(size_t page_size, uint32_t flag_compress)
1273{
Laszlo Ersekb87ef352014-05-20 13:42:53 +02001274 switch (flag_compress) {
1275 case DUMP_DH_COMPRESSED_ZLIB:
1276 return compressBound(page_size);
qiaonuohand12f57e2014-02-18 14:11:35 +08001277
Laszlo Ersekb87ef352014-05-20 13:42:53 +02001278 case DUMP_DH_COMPRESSED_LZO:
1279 /*
1280 * LZO will expand incompressible data by a little amount. Please check
1281 * the following URL to see the expansion calculation:
1282 * http://www.oberhumer.com/opensource/lzo/lzofaq.php
1283 */
1284 return page_size + page_size / 16 + 64 + 3;
qiaonuohand12f57e2014-02-18 14:11:35 +08001285
1286#ifdef CONFIG_SNAPPY
Laszlo Ersekb87ef352014-05-20 13:42:53 +02001287 case DUMP_DH_COMPRESSED_SNAPPY:
1288 return snappy_max_compressed_length(page_size);
qiaonuohand12f57e2014-02-18 14:11:35 +08001289#endif
Laszlo Ersekb87ef352014-05-20 13:42:53 +02001290 }
1291 return 0;
qiaonuohand12f57e2014-02-18 14:11:35 +08001292}
1293
1294/*
1295 * check if the page is all 0
1296 */
1297static inline bool is_zero_page(const uint8_t *buf, size_t page_size)
1298{
1299 return buffer_is_zero(buf, page_size);
1300}
1301
zhanghailiang4c7e2512014-10-09 14:13:11 +08001302static void write_dump_pages(DumpState *s, Error **errp)
qiaonuohand12f57e2014-02-18 14:11:35 +08001303{
1304 int ret = 0;
1305 DataCache page_desc, page_data;
1306 size_t len_buf_out, size_out;
1307#ifdef CONFIG_LZO
1308 lzo_bytep wrkmem = NULL;
1309#endif
1310 uint8_t *buf_out = NULL;
1311 off_t offset_desc, offset_data;
1312 PageDescriptor pd, pd_zero;
1313 uint8_t *buf;
qiaonuohand12f57e2014-02-18 14:11:35 +08001314 GuestPhysBlock *block_iter = NULL;
1315 uint64_t pfn_iter;
1316
1317 /* get offset of page_desc and page_data in dump file */
1318 offset_desc = s->offset_page;
1319 offset_data = offset_desc + sizeof(PageDescriptor) * s->num_dumpable;
1320
1321 prepare_data_cache(&page_desc, s, offset_desc);
1322 prepare_data_cache(&page_data, s, offset_data);
1323
1324 /* prepare buffer to store compressed data */
Andrew Jones8161bef2016-01-11 20:56:20 +01001325 len_buf_out = get_len_buf_out(s->dump_info.page_size, s->flag_compress);
Laszlo Ersekb87ef352014-05-20 13:42:53 +02001326 assert(len_buf_out != 0);
qiaonuohand12f57e2014-02-18 14:11:35 +08001327
1328#ifdef CONFIG_LZO
1329 wrkmem = g_malloc(LZO1X_1_MEM_COMPRESS);
1330#endif
1331
1332 buf_out = g_malloc(len_buf_out);
1333
1334 /*
1335 * init zero page's page_desc and page_data, because every zero page
1336 * uses the same page_data
1337 */
Andrew Jones8161bef2016-01-11 20:56:20 +01001338 pd_zero.size = cpu_to_dump32(s, s->dump_info.page_size);
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001339 pd_zero.flags = cpu_to_dump32(s, 0);
1340 pd_zero.offset = cpu_to_dump64(s, offset_data);
1341 pd_zero.page_flags = cpu_to_dump64(s, 0);
Andrew Jones8161bef2016-01-11 20:56:20 +01001342 buf = g_malloc0(s->dump_info.page_size);
1343 ret = write_cache(&page_data, buf, s->dump_info.page_size, false);
qiaonuohand12f57e2014-02-18 14:11:35 +08001344 g_free(buf);
1345 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001346 error_setg(errp, "dump: failed to write page data (zero page)");
qiaonuohand12f57e2014-02-18 14:11:35 +08001347 goto out;
1348 }
1349
Andrew Jones8161bef2016-01-11 20:56:20 +01001350 offset_data += s->dump_info.page_size;
qiaonuohand12f57e2014-02-18 14:11:35 +08001351
1352 /*
1353 * dump memory to vmcore page by page. zero page will all be resided in the
1354 * first page of page section
1355 */
1356 while (get_next_page(&block_iter, &pfn_iter, &buf, s)) {
1357 /* check zero page */
Andrew Jones8161bef2016-01-11 20:56:20 +01001358 if (is_zero_page(buf, s->dump_info.page_size)) {
qiaonuohand12f57e2014-02-18 14:11:35 +08001359 ret = write_cache(&page_desc, &pd_zero, sizeof(PageDescriptor),
1360 false);
1361 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001362 error_setg(errp, "dump: failed to write page desc");
qiaonuohand12f57e2014-02-18 14:11:35 +08001363 goto out;
1364 }
1365 } else {
1366 /*
1367 * not zero page, then:
1368 * 1. compress the page
1369 * 2. write the compressed page into the cache of page_data
1370 * 3. get page desc of the compressed page and write it into the
1371 * cache of page_desc
1372 *
1373 * only one compression format will be used here, for
1374 * s->flag_compress is set. But when compression fails to work,
1375 * we fall back to save in plaintext.
1376 */
1377 size_out = len_buf_out;
1378 if ((s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) &&
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001379 (compress2(buf_out, (uLongf *)&size_out, buf,
Andrew Jones8161bef2016-01-11 20:56:20 +01001380 s->dump_info.page_size, Z_BEST_SPEED) == Z_OK) &&
1381 (size_out < s->dump_info.page_size)) {
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001382 pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_ZLIB);
1383 pd.size = cpu_to_dump32(s, size_out);
qiaonuohand12f57e2014-02-18 14:11:35 +08001384
1385 ret = write_cache(&page_data, buf_out, size_out, false);
1386 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001387 error_setg(errp, "dump: failed to write page data");
qiaonuohand12f57e2014-02-18 14:11:35 +08001388 goto out;
1389 }
1390#ifdef CONFIG_LZO
1391 } else if ((s->flag_compress & DUMP_DH_COMPRESSED_LZO) &&
Andrew Jones8161bef2016-01-11 20:56:20 +01001392 (lzo1x_1_compress(buf, s->dump_info.page_size, buf_out,
qiaonuohand12f57e2014-02-18 14:11:35 +08001393 (lzo_uint *)&size_out, wrkmem) == LZO_E_OK) &&
Andrew Jones8161bef2016-01-11 20:56:20 +01001394 (size_out < s->dump_info.page_size)) {
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001395 pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_LZO);
1396 pd.size = cpu_to_dump32(s, size_out);
qiaonuohand12f57e2014-02-18 14:11:35 +08001397
1398 ret = write_cache(&page_data, buf_out, size_out, false);
1399 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001400 error_setg(errp, "dump: failed to write page data");
qiaonuohand12f57e2014-02-18 14:11:35 +08001401 goto out;
1402 }
1403#endif
1404#ifdef CONFIG_SNAPPY
1405 } else if ((s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) &&
Andrew Jones8161bef2016-01-11 20:56:20 +01001406 (snappy_compress((char *)buf, s->dump_info.page_size,
qiaonuohand12f57e2014-02-18 14:11:35 +08001407 (char *)buf_out, &size_out) == SNAPPY_OK) &&
Andrew Jones8161bef2016-01-11 20:56:20 +01001408 (size_out < s->dump_info.page_size)) {
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001409 pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_SNAPPY);
1410 pd.size = cpu_to_dump32(s, size_out);
qiaonuohand12f57e2014-02-18 14:11:35 +08001411
1412 ret = write_cache(&page_data, buf_out, size_out, false);
1413 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001414 error_setg(errp, "dump: failed to write page data");
qiaonuohand12f57e2014-02-18 14:11:35 +08001415 goto out;
1416 }
1417#endif
1418 } else {
1419 /*
1420 * fall back to save in plaintext, size_out should be
Andrew Jones8161bef2016-01-11 20:56:20 +01001421 * assigned the target's page size
qiaonuohand12f57e2014-02-18 14:11:35 +08001422 */
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001423 pd.flags = cpu_to_dump32(s, 0);
Andrew Jones8161bef2016-01-11 20:56:20 +01001424 size_out = s->dump_info.page_size;
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001425 pd.size = cpu_to_dump32(s, size_out);
qiaonuohand12f57e2014-02-18 14:11:35 +08001426
Andrew Jones8161bef2016-01-11 20:56:20 +01001427 ret = write_cache(&page_data, buf,
1428 s->dump_info.page_size, false);
qiaonuohand12f57e2014-02-18 14:11:35 +08001429 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001430 error_setg(errp, "dump: failed to write page data");
qiaonuohand12f57e2014-02-18 14:11:35 +08001431 goto out;
1432 }
1433 }
1434
1435 /* get and write page desc here */
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001436 pd.page_flags = cpu_to_dump64(s, 0);
1437 pd.offset = cpu_to_dump64(s, offset_data);
qiaonuohand12f57e2014-02-18 14:11:35 +08001438 offset_data += size_out;
1439
1440 ret = write_cache(&page_desc, &pd, sizeof(PageDescriptor), false);
1441 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001442 error_setg(errp, "dump: failed to write page desc");
qiaonuohand12f57e2014-02-18 14:11:35 +08001443 goto out;
1444 }
1445 }
Peter Xu2264c2c2016-02-18 13:16:53 +08001446 s->written_size += s->dump_info.page_size;
qiaonuohand12f57e2014-02-18 14:11:35 +08001447 }
1448
1449 ret = write_cache(&page_desc, NULL, 0, true);
1450 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001451 error_setg(errp, "dump: failed to sync cache for page_desc");
qiaonuohand12f57e2014-02-18 14:11:35 +08001452 goto out;
1453 }
1454 ret = write_cache(&page_data, NULL, 0, true);
1455 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001456 error_setg(errp, "dump: failed to sync cache for page_data");
qiaonuohand12f57e2014-02-18 14:11:35 +08001457 goto out;
1458 }
1459
1460out:
1461 free_data_cache(&page_desc);
1462 free_data_cache(&page_data);
1463
1464#ifdef CONFIG_LZO
1465 g_free(wrkmem);
1466#endif
1467
1468 g_free(buf_out);
qiaonuohand12f57e2014-02-18 14:11:35 +08001469}
1470
zhanghailiang4c7e2512014-10-09 14:13:11 +08001471static void create_kdump_vmcore(DumpState *s, Error **errp)
qiaonuohanb53ccc32014-02-18 14:11:36 +08001472{
1473 int ret;
zhanghailiang4c7e2512014-10-09 14:13:11 +08001474 Error *local_err = NULL;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001475
1476 /*
1477 * the kdump-compressed format is:
1478 * File offset
1479 * +------------------------------------------+ 0x0
1480 * | main header (struct disk_dump_header) |
1481 * |------------------------------------------+ block 1
1482 * | sub header (struct kdump_sub_header) |
1483 * |------------------------------------------+ block 2
1484 * | 1st-dump_bitmap |
1485 * |------------------------------------------+ block 2 + X blocks
1486 * | 2nd-dump_bitmap | (aligned by block)
1487 * |------------------------------------------+ block 2 + 2 * X blocks
1488 * | page desc for pfn 0 (struct page_desc) | (aligned by block)
1489 * | page desc for pfn 1 (struct page_desc) |
1490 * | : |
1491 * |------------------------------------------| (not aligned by block)
1492 * | page data (pfn 0) |
1493 * | page data (pfn 1) |
1494 * | : |
1495 * +------------------------------------------+
1496 */
1497
1498 ret = write_start_flat_header(s->fd);
1499 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001500 error_setg(errp, "dump: failed to write start flat header");
zhanghailiang4c7e2512014-10-09 14:13:11 +08001501 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001502 }
1503
zhanghailiang4c7e2512014-10-09 14:13:11 +08001504 write_dump_header(s, &local_err);
1505 if (local_err) {
1506 error_propagate(errp, local_err);
1507 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001508 }
1509
zhanghailiang4c7e2512014-10-09 14:13:11 +08001510 write_dump_bitmap(s, &local_err);
1511 if (local_err) {
1512 error_propagate(errp, local_err);
1513 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001514 }
1515
zhanghailiang4c7e2512014-10-09 14:13:11 +08001516 write_dump_pages(s, &local_err);
1517 if (local_err) {
1518 error_propagate(errp, local_err);
1519 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001520 }
1521
1522 ret = write_end_flat_header(s->fd);
1523 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001524 error_setg(errp, "dump: failed to write end flat header");
zhanghailiang4c7e2512014-10-09 14:13:11 +08001525 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001526 }
qiaonuohanb53ccc32014-02-18 14:11:36 +08001527}
1528
Wen Congyang783e9b42012-05-07 12:10:47 +08001529static ram_addr_t get_start_block(DumpState *s)
1530{
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001531 GuestPhysBlock *block;
Wen Congyang783e9b42012-05-07 12:10:47 +08001532
1533 if (!s->has_filter) {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001534 s->next_block = QTAILQ_FIRST(&s->guest_phys_blocks.head);
Wen Congyang783e9b42012-05-07 12:10:47 +08001535 return 0;
1536 }
1537
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001538 QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
1539 if (block->target_start >= s->begin + s->length ||
1540 block->target_end <= s->begin) {
Wen Congyang783e9b42012-05-07 12:10:47 +08001541 /* This block is out of the range */
1542 continue;
1543 }
1544
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001545 s->next_block = block;
1546 if (s->begin > block->target_start) {
1547 s->start = s->begin - block->target_start;
Wen Congyang783e9b42012-05-07 12:10:47 +08001548 } else {
1549 s->start = 0;
1550 }
1551 return s->start;
1552 }
1553
1554 return -1;
1555}
1556
qiaonuohan7aad2482014-02-18 14:11:31 +08001557static void get_max_mapnr(DumpState *s)
1558{
1559 GuestPhysBlock *last_block;
1560
Paolo Bonzinieae3eb32018-12-06 13:10:34 +01001561 last_block = QTAILQ_LAST(&s->guest_phys_blocks.head);
Andrew Jones8161bef2016-01-11 20:56:20 +01001562 s->max_mapnr = dump_paddr_to_pfn(s, last_block->target_end);
qiaonuohan7aad2482014-02-18 14:11:31 +08001563}
1564
Peter Xubaf28f52016-02-18 13:16:48 +08001565static DumpState dump_state_global = { .status = DUMP_STATUS_NONE };
1566
1567static void dump_state_prepare(DumpState *s)
1568{
1569 /* zero the struct, setting status to active */
1570 *s = (DumpState) { .status = DUMP_STATUS_ACTIVE };
1571}
1572
Peter Xu65d64f32016-02-18 13:16:49 +08001573bool dump_in_progress(void)
1574{
1575 DumpState *state = &dump_state_global;
Peter Xu39ba2ea2016-02-18 13:16:54 +08001576 return (atomic_read(&state->status) == DUMP_STATUS_ACTIVE);
Peter Xu65d64f32016-02-18 13:16:49 +08001577}
1578
Peter Xu2264c2c2016-02-18 13:16:53 +08001579/* calculate total size of memory to be dumped (taking filter into
1580 * acoount.) */
1581static int64_t dump_calculate_size(DumpState *s)
1582{
1583 GuestPhysBlock *block;
1584 int64_t size = 0, total = 0, left = 0, right = 0;
1585
1586 QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
1587 if (s->has_filter) {
1588 /* calculate the overlapped region. */
1589 left = MAX(s->begin, block->target_start);
1590 right = MIN(s->begin + s->length, block->target_end);
1591 size = right - left;
1592 size = size > 0 ? size : 0;
1593 } else {
1594 /* count the whole region in */
1595 size = (block->target_end - block->target_start);
1596 }
1597 total += size;
1598 }
1599
1600 return total;
1601}
1602
Marc-André Lureaud9feb512017-09-11 18:59:26 +02001603static void vmcoreinfo_update_phys_base(DumpState *s)
1604{
1605 uint64_t size, note_head_size, name_size, phys_base;
1606 char **lines;
1607 uint8_t *vmci;
1608 size_t i;
1609
1610 if (!note_name_equal(s, s->guest_note, "VMCOREINFO")) {
1611 return;
1612 }
1613
1614 get_note_sizes(s, s->guest_note, &note_head_size, &name_size, &size);
1615 note_head_size = ROUND_UP(note_head_size, 4);
1616
1617 vmci = s->guest_note + note_head_size + ROUND_UP(name_size, 4);
1618 *(vmci + size) = '\0';
1619
1620 lines = g_strsplit((char *)vmci, "\n", -1);
1621 for (i = 0; lines[i]; i++) {
Wei Huang68cbecf2018-03-09 12:03:23 -05001622 const char *prefix = NULL;
1623
1624 if (s->dump_info.d_machine == EM_X86_64) {
1625 prefix = "NUMBER(phys_base)=";
1626 } else if (s->dump_info.d_machine == EM_AARCH64) {
1627 prefix = "NUMBER(PHYS_OFFSET)=";
1628 }
1629
1630 if (prefix && g_str_has_prefix(lines[i], prefix)) {
1631 if (qemu_strtou64(lines[i] + strlen(prefix), NULL, 16,
Marc-André Lureaud9feb512017-09-11 18:59:26 +02001632 &phys_base) < 0) {
Wei Huang68cbecf2018-03-09 12:03:23 -05001633 warn_report("Failed to read %s", prefix);
Marc-André Lureaud9feb512017-09-11 18:59:26 +02001634 } else {
1635 s->dump_info.phys_base = phys_base;
1636 }
1637 break;
1638 }
1639 }
1640
1641 g_strfreev(lines);
1642}
1643
zhanghailiang4c7e2512014-10-09 14:13:11 +08001644static void dump_init(DumpState *s, int fd, bool has_format,
1645 DumpGuestMemoryFormat format, bool paging, bool has_filter,
1646 int64_t begin, int64_t length, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +08001647{
Marc-André Lureau903ef732017-09-11 18:59:25 +02001648 VMCoreInfoState *vmci = vmcoreinfo_find();
Andreas Färber182735e2013-05-29 22:29:20 +02001649 CPUState *cpu;
Wen Congyang783e9b42012-05-07 12:10:47 +08001650 int nr_cpus;
Andreas Färber11ed09c2013-05-29 21:54:03 +02001651 Error *err = NULL;
Wen Congyang783e9b42012-05-07 12:10:47 +08001652 int ret;
1653
Peter Xuca1fc8c2016-02-18 13:16:50 +08001654 s->has_format = has_format;
1655 s->format = format;
Peter Xu2264c2c2016-02-18 13:16:53 +08001656 s->written_size = 0;
Peter Xuca1fc8c2016-02-18 13:16:50 +08001657
qiaonuohanb53ccc32014-02-18 14:11:36 +08001658 /* kdump-compressed is conflict with paging and filter */
1659 if (has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
1660 assert(!paging && !has_filter);
1661 }
1662
Wen Congyang783e9b42012-05-07 12:10:47 +08001663 if (runstate_is_running()) {
1664 vm_stop(RUN_STATE_SAVE_VM);
1665 s->resume = true;
1666 } else {
1667 s->resume = false;
1668 }
1669
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001670 /* If we use KVM, we should synchronize the registers before we get dump
1671 * info or physmap info.
Wen Congyang783e9b42012-05-07 12:10:47 +08001672 */
Andreas Färber1b3509c2013-06-09 16:48:29 +02001673 cpu_synchronize_all_states();
Wen Congyang783e9b42012-05-07 12:10:47 +08001674 nr_cpus = 0;
Andreas Färberbdc44642013-06-24 23:50:24 +02001675 CPU_FOREACH(cpu) {
Wen Congyang783e9b42012-05-07 12:10:47 +08001676 nr_cpus++;
1677 }
1678
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001679 s->fd = fd;
1680 s->has_filter = has_filter;
1681 s->begin = begin;
1682 s->length = length;
1683
Chen Gang29282072014-08-03 23:28:56 +08001684 memory_mapping_list_init(&s->list);
1685
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001686 guest_phys_blocks_init(&s->guest_phys_blocks);
Laszlo Ersekc5d7f602013-08-06 12:37:10 +02001687 guest_phys_blocks_append(&s->guest_phys_blocks);
Peter Xu2264c2c2016-02-18 13:16:53 +08001688 s->total_size = dump_calculate_size(s);
1689#ifdef DEBUG_DUMP_GUEST_MEMORY
1690 fprintf(stderr, "DUMP: total memory to dump: %lu\n", s->total_size);
1691#endif
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001692
Cornelia Huckd1e69942017-09-13 16:20:35 +02001693 /* it does not make sense to dump non-existent memory */
1694 if (!s->total_size) {
1695 error_setg(errp, "dump: no guest memory to dump");
1696 goto cleanup;
1697 }
1698
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001699 s->start = get_start_block(s);
1700 if (s->start == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001701 error_setg(errp, QERR_INVALID_PARAMETER, "begin");
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001702 goto cleanup;
1703 }
1704
1705 /* get dump info: endian, class and architecture.
1706 * If the target architecture is not supported, cpu_get_dump_info() will
1707 * return -1.
1708 */
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001709 ret = cpu_get_dump_info(&s->dump_info, &s->guest_phys_blocks);
Wen Congyang783e9b42012-05-07 12:10:47 +08001710 if (ret < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001711 error_setg(errp, QERR_UNSUPPORTED);
Wen Congyang783e9b42012-05-07 12:10:47 +08001712 goto cleanup;
1713 }
1714
Andrew Jones8161bef2016-01-11 20:56:20 +01001715 if (!s->dump_info.page_size) {
1716 s->dump_info.page_size = TARGET_PAGE_SIZE;
1717 }
1718
Paolo Bonzini4720bd02012-06-07 08:48:09 +02001719 s->note_size = cpu_get_note_size(s->dump_info.d_class,
1720 s->dump_info.d_machine, nr_cpus);
Aneesh Kumar K.Vbb6b6842013-10-01 21:49:32 +05301721 if (s->note_size < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001722 error_setg(errp, QERR_UNSUPPORTED);
Paolo Bonzini4720bd02012-06-07 08:48:09 +02001723 goto cleanup;
1724 }
1725
Marc-André Lureau903ef732017-09-11 18:59:25 +02001726 /*
Marc-André Lureaud9feb512017-09-11 18:59:26 +02001727 * The goal of this block is to (a) update the previously guessed
1728 * phys_base, (b) copy the guest note out of the guest.
1729 * Failure to do so is not fatal for dumping.
Marc-André Lureau903ef732017-09-11 18:59:25 +02001730 */
1731 if (vmci) {
1732 uint64_t addr, note_head_size, name_size, desc_size;
1733 uint32_t size;
1734 uint16_t format;
1735
1736 note_head_size = s->dump_info.d_class == ELFCLASS32 ?
1737 sizeof(Elf32_Nhdr) : sizeof(Elf64_Nhdr);
1738
1739 format = le16_to_cpu(vmci->vmcoreinfo.guest_format);
1740 size = le32_to_cpu(vmci->vmcoreinfo.size);
1741 addr = le64_to_cpu(vmci->vmcoreinfo.paddr);
1742 if (!vmci->has_vmcoreinfo) {
1743 warn_report("guest note is not present");
1744 } else if (size < note_head_size || size > MAX_GUEST_NOTE_SIZE) {
1745 warn_report("guest note size is invalid: %" PRIu32, size);
Marc-André Lureau5be5df72018-08-17 17:59:10 +02001746 } else if (format != FW_CFG_VMCOREINFO_FORMAT_ELF) {
Marc-André Lureau903ef732017-09-11 18:59:25 +02001747 warn_report("guest note format is unsupported: %" PRIu16, format);
1748 } else {
1749 s->guest_note = g_malloc(size + 1); /* +1 for adding \0 */
1750 cpu_physical_memory_read(addr, s->guest_note, size);
1751
1752 get_note_sizes(s, s->guest_note, NULL, &name_size, &desc_size);
1753 s->guest_note_size = ELF_NOTE_SIZE(note_head_size, name_size,
1754 desc_size);
1755 if (name_size > MAX_GUEST_NOTE_SIZE ||
1756 desc_size > MAX_GUEST_NOTE_SIZE ||
1757 s->guest_note_size > size) {
1758 warn_report("Invalid guest note header");
1759 g_free(s->guest_note);
1760 s->guest_note = NULL;
1761 } else {
Marc-André Lureaud9feb512017-09-11 18:59:26 +02001762 vmcoreinfo_update_phys_base(s);
Marc-André Lureau903ef732017-09-11 18:59:25 +02001763 s->note_size += s->guest_note_size;
1764 }
1765 }
1766 }
1767
Wen Congyang783e9b42012-05-07 12:10:47 +08001768 /* get memory mapping */
Wen Congyang783e9b42012-05-07 12:10:47 +08001769 if (paging) {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001770 qemu_get_guest_memory_mapping(&s->list, &s->guest_phys_blocks, &err);
Andreas Färber11ed09c2013-05-29 21:54:03 +02001771 if (err != NULL) {
1772 error_propagate(errp, err);
1773 goto cleanup;
1774 }
Wen Congyang783e9b42012-05-07 12:10:47 +08001775 } else {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001776 qemu_get_guest_simple_memory_mapping(&s->list, &s->guest_phys_blocks);
Wen Congyang783e9b42012-05-07 12:10:47 +08001777 }
1778
qiaonuohan7aad2482014-02-18 14:11:31 +08001779 s->nr_cpus = nr_cpus;
qiaonuohan7aad2482014-02-18 14:11:31 +08001780
1781 get_max_mapnr(s);
1782
1783 uint64_t tmp;
Andrew Jones8161bef2016-01-11 20:56:20 +01001784 tmp = DIV_ROUND_UP(DIV_ROUND_UP(s->max_mapnr, CHAR_BIT),
1785 s->dump_info.page_size);
1786 s->len_dump_bitmap = tmp * s->dump_info.page_size;
qiaonuohan7aad2482014-02-18 14:11:31 +08001787
qiaonuohanb53ccc32014-02-18 14:11:36 +08001788 /* init for kdump-compressed format */
1789 if (has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
1790 switch (format) {
1791 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB:
1792 s->flag_compress = DUMP_DH_COMPRESSED_ZLIB;
1793 break;
1794
1795 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO:
Laszlo Ersekc998acb2014-05-20 13:42:22 +02001796#ifdef CONFIG_LZO
1797 if (lzo_init() != LZO_E_OK) {
1798 error_setg(errp, "failed to initialize the LZO library");
1799 goto cleanup;
1800 }
1801#endif
qiaonuohanb53ccc32014-02-18 14:11:36 +08001802 s->flag_compress = DUMP_DH_COMPRESSED_LZO;
1803 break;
1804
1805 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY:
1806 s->flag_compress = DUMP_DH_COMPRESSED_SNAPPY;
1807 break;
1808
1809 default:
1810 s->flag_compress = 0;
1811 }
1812
zhanghailiang4c7e2512014-10-09 14:13:11 +08001813 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001814 }
1815
Wen Congyang783e9b42012-05-07 12:10:47 +08001816 if (s->has_filter) {
1817 memory_mapping_filter(&s->list, s->begin, s->length);
1818 }
1819
1820 /*
1821 * calculate phdr_num
1822 *
1823 * the type of ehdr->e_phnum is uint16_t, so we should avoid overflow
1824 */
1825 s->phdr_num = 1; /* PT_NOTE */
1826 if (s->list.num < UINT16_MAX - 2) {
1827 s->phdr_num += s->list.num;
1828 s->have_section = false;
1829 } else {
1830 s->have_section = true;
1831 s->phdr_num = PN_XNUM;
1832 s->sh_info = 1; /* PT_NOTE */
1833
1834 /* the type of shdr->sh_info is uint32_t, so we should avoid overflow */
1835 if (s->list.num <= UINT32_MAX - 1) {
1836 s->sh_info += s->list.num;
1837 } else {
1838 s->sh_info = UINT32_MAX;
1839 }
1840 }
1841
Wen Congyang783e9b42012-05-07 12:10:47 +08001842 if (s->dump_info.d_class == ELFCLASS64) {
1843 if (s->have_section) {
1844 s->memory_offset = sizeof(Elf64_Ehdr) +
1845 sizeof(Elf64_Phdr) * s->sh_info +
1846 sizeof(Elf64_Shdr) + s->note_size;
1847 } else {
1848 s->memory_offset = sizeof(Elf64_Ehdr) +
1849 sizeof(Elf64_Phdr) * s->phdr_num + s->note_size;
1850 }
1851 } else {
1852 if (s->have_section) {
1853 s->memory_offset = sizeof(Elf32_Ehdr) +
1854 sizeof(Elf32_Phdr) * s->sh_info +
1855 sizeof(Elf32_Shdr) + s->note_size;
1856 } else {
1857 s->memory_offset = sizeof(Elf32_Ehdr) +
1858 sizeof(Elf32_Phdr) * s->phdr_num + s->note_size;
1859 }
1860 }
1861
zhanghailiang4c7e2512014-10-09 14:13:11 +08001862 return;
Wen Congyang783e9b42012-05-07 12:10:47 +08001863
1864cleanup:
Chen Gang29282072014-08-03 23:28:56 +08001865 dump_cleanup(s);
Wen Congyang783e9b42012-05-07 12:10:47 +08001866}
1867
Peter Xuca1fc8c2016-02-18 13:16:50 +08001868/* this operation might be time consuming. */
1869static void dump_process(DumpState *s, Error **errp)
1870{
1871 Error *local_err = NULL;
Peter Xud42a0d12016-02-18 13:16:56 +08001872 DumpQueryResult *result = NULL;
Peter Xuca1fc8c2016-02-18 13:16:50 +08001873
Viktor Prutyanov2da91b52018-05-17 19:23:39 +03001874 if (s->has_format && s->format == DUMP_GUEST_MEMORY_FORMAT_WIN_DMP) {
1875#ifdef TARGET_X86_64
1876 create_win_dump(s, &local_err);
1877#endif
1878 } else if (s->has_format && s->format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
Peter Xuca1fc8c2016-02-18 13:16:50 +08001879 create_kdump_vmcore(s, &local_err);
1880 } else {
1881 create_vmcore(s, &local_err);
1882 }
1883
Peter Xu39ba2ea2016-02-18 13:16:54 +08001884 /* make sure status is written after written_size updates */
1885 smp_wmb();
1886 atomic_set(&s->status,
1887 (local_err ? DUMP_STATUS_FAILED : DUMP_STATUS_COMPLETED));
Peter Xuca1fc8c2016-02-18 13:16:50 +08001888
Peter Xud42a0d12016-02-18 13:16:56 +08001889 /* send DUMP_COMPLETED message (unconditionally) */
1890 result = qmp_query_dump(NULL);
1891 /* should never fail */
1892 assert(result);
1893 qapi_event_send_dump_completed(result, !!local_err, (local_err ? \
Peter Xu3ab72382018-08-15 21:37:37 +08001894 error_get_pretty(local_err) : NULL));
Peter Xud42a0d12016-02-18 13:16:56 +08001895 qapi_free_DumpQueryResult(result);
1896
Peter Xu39ba2ea2016-02-18 13:16:54 +08001897 error_propagate(errp, local_err);
Peter Xuca1fc8c2016-02-18 13:16:50 +08001898 dump_cleanup(s);
1899}
1900
Peter Xu1fbeff72016-02-18 13:16:52 +08001901static void *dump_thread(void *data)
1902{
Peter Xu1fbeff72016-02-18 13:16:52 +08001903 DumpState *s = (DumpState *)data;
Alberto Garciac3a8fe32017-08-29 15:08:36 +03001904 dump_process(s, NULL);
Peter Xu1fbeff72016-02-18 13:16:52 +08001905 return NULL;
1906}
1907
Peter Xu39ba2ea2016-02-18 13:16:54 +08001908DumpQueryResult *qmp_query_dump(Error **errp)
1909{
1910 DumpQueryResult *result = g_new(DumpQueryResult, 1);
1911 DumpState *state = &dump_state_global;
1912 result->status = atomic_read(&state->status);
1913 /* make sure we are reading status and written_size in order */
1914 smp_rmb();
1915 result->completed = state->written_size;
1916 result->total = state->total_size;
1917 return result;
1918}
1919
Peter Xu228de9c2016-02-18 13:16:47 +08001920void qmp_dump_guest_memory(bool paging, const char *file,
1921 bool has_detach, bool detach,
1922 bool has_begin, int64_t begin, bool has_length,
qiaonuohanb53ccc32014-02-18 14:11:36 +08001923 int64_t length, bool has_format,
1924 DumpGuestMemoryFormat format, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +08001925{
1926 const char *p;
1927 int fd = -1;
1928 DumpState *s;
zhanghailiang4c7e2512014-10-09 14:13:11 +08001929 Error *local_err = NULL;
Peter Xu1fbeff72016-02-18 13:16:52 +08001930 bool detach_p = false;
Wen Congyang783e9b42012-05-07 12:10:47 +08001931
Peter Xu63e27f22016-02-18 13:16:51 +08001932 if (runstate_check(RUN_STATE_INMIGRATE)) {
1933 error_setg(errp, "Dump not allowed during incoming migration.");
1934 return;
1935 }
1936
Peter Xu65d64f32016-02-18 13:16:49 +08001937 /* if there is a dump in background, we should wait until the dump
1938 * finished */
1939 if (dump_in_progress()) {
1940 error_setg(errp, "There is a dump in process, please wait.");
1941 return;
1942 }
1943
qiaonuohanb53ccc32014-02-18 14:11:36 +08001944 /*
1945 * kdump-compressed format need the whole memory dumped, so paging or
1946 * filter is not supported here.
1947 */
1948 if ((has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) &&
1949 (paging || has_begin || has_length)) {
1950 error_setg(errp, "kdump-compressed format doesn't support paging or "
1951 "filter");
1952 return;
1953 }
Wen Congyang783e9b42012-05-07 12:10:47 +08001954 if (has_begin && !has_length) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001955 error_setg(errp, QERR_MISSING_PARAMETER, "length");
Wen Congyang783e9b42012-05-07 12:10:47 +08001956 return;
1957 }
1958 if (!has_begin && has_length) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001959 error_setg(errp, QERR_MISSING_PARAMETER, "begin");
Wen Congyang783e9b42012-05-07 12:10:47 +08001960 return;
1961 }
Peter Xu1fbeff72016-02-18 13:16:52 +08001962 if (has_detach) {
1963 detach_p = detach;
1964 }
Wen Congyang783e9b42012-05-07 12:10:47 +08001965
qiaonuohanb53ccc32014-02-18 14:11:36 +08001966 /* check whether lzo/snappy is supported */
1967#ifndef CONFIG_LZO
1968 if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO) {
1969 error_setg(errp, "kdump-lzo is not available now");
1970 return;
1971 }
1972#endif
1973
1974#ifndef CONFIG_SNAPPY
1975 if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY) {
1976 error_setg(errp, "kdump-snappy is not available now");
1977 return;
1978 }
1979#endif
1980
Viktor Prutyanov2da91b52018-05-17 19:23:39 +03001981#ifndef TARGET_X86_64
1982 if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_WIN_DMP) {
1983 error_setg(errp, "Windows dump is only available for x86-64");
1984 return;
1985 }
1986#endif
1987
Wen Congyang783e9b42012-05-07 12:10:47 +08001988#if !defined(WIN32)
1989 if (strstart(file, "fd:", &p)) {
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02001990 fd = monitor_get_fd(cur_mon, p, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +08001991 if (fd == -1) {
Wen Congyang783e9b42012-05-07 12:10:47 +08001992 return;
1993 }
1994 }
1995#endif
1996
1997 if (strstart(file, "file:", &p)) {
1998 fd = qemu_open(p, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, S_IRUSR);
1999 if (fd < 0) {
Luiz Capitulino75817662013-06-07 14:36:01 -04002000 error_setg_file_open(errp, errno, p);
Wen Congyang783e9b42012-05-07 12:10:47 +08002001 return;
2002 }
2003 }
2004
2005 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002006 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
Wen Congyang783e9b42012-05-07 12:10:47 +08002007 return;
2008 }
2009
Peter Xubaf28f52016-02-18 13:16:48 +08002010 s = &dump_state_global;
2011 dump_state_prepare(s);
Wen Congyang783e9b42012-05-07 12:10:47 +08002012
zhanghailiang4c7e2512014-10-09 14:13:11 +08002013 dump_init(s, fd, has_format, format, paging, has_begin,
2014 begin, length, &local_err);
2015 if (local_err) {
zhanghailiang4c7e2512014-10-09 14:13:11 +08002016 error_propagate(errp, local_err);
Peter Xu39ba2ea2016-02-18 13:16:54 +08002017 atomic_set(&s->status, DUMP_STATUS_FAILED);
Wen Congyang783e9b42012-05-07 12:10:47 +08002018 return;
2019 }
2020
Peter Xu1fbeff72016-02-18 13:16:52 +08002021 if (detach_p) {
2022 /* detached dump */
Fam Zheng6796b402017-05-03 15:28:19 +08002023 s->detached = true;
Peter Xu1fbeff72016-02-18 13:16:52 +08002024 qemu_thread_create(&s->dump_thread, "dump_thread", dump_thread,
2025 s, QEMU_THREAD_DETACHED);
2026 } else {
2027 /* sync dump */
2028 dump_process(s, errp);
2029 }
Wen Congyang783e9b42012-05-07 12:10:47 +08002030}
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002031
2032DumpGuestMemoryCapability *qmp_query_dump_guest_memory_capability(Error **errp)
2033{
2034 DumpGuestMemoryFormatList *item;
2035 DumpGuestMemoryCapability *cap =
2036 g_malloc0(sizeof(DumpGuestMemoryCapability));
2037
2038 /* elf is always available */
2039 item = g_malloc0(sizeof(DumpGuestMemoryFormatList));
2040 cap->formats = item;
2041 item->value = DUMP_GUEST_MEMORY_FORMAT_ELF;
2042
2043 /* kdump-zlib is always available */
2044 item->next = g_malloc0(sizeof(DumpGuestMemoryFormatList));
2045 item = item->next;
2046 item->value = DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB;
2047
2048 /* add new item if kdump-lzo is available */
2049#ifdef CONFIG_LZO
2050 item->next = g_malloc0(sizeof(DumpGuestMemoryFormatList));
2051 item = item->next;
2052 item->value = DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO;
2053#endif
2054
2055 /* add new item if kdump-snappy is available */
2056#ifdef CONFIG_SNAPPY
2057 item->next = g_malloc0(sizeof(DumpGuestMemoryFormatList));
2058 item = item->next;
2059 item->value = DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY;
2060#endif
2061
Viktor Prutyanov2da91b52018-05-17 19:23:39 +03002062 /* Windows dump is available only if target is x86_64 */
2063#ifdef TARGET_X86_64
2064 item->next = g_malloc0(sizeof(DumpGuestMemoryFormatList));
2065 item = item->next;
2066 item->value = DUMP_GUEST_MEMORY_FORMAT_WIN_DMP;
2067#endif
2068
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002069 return cap;
2070}