blob: b54cd42b2178f29caff467eefb947b4d1e476a53 [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
qiaonuohand12f57e2014-02-18 14:11:35 +080032#include <zlib.h>
33#ifdef CONFIG_LZO
34#include <lzo/lzo1x.h>
35#endif
36#ifdef CONFIG_SNAPPY
37#include <snappy-c.h>
38#endif
qiaonuohan4ab23a92014-02-18 14:11:37 +080039#ifndef ELF_MACHINE_UNAME
40#define ELF_MACHINE_UNAME "Unknown"
41#endif
qiaonuohand12f57e2014-02-18 14:11:35 +080042
Marc-André Lureau903ef732017-09-11 18:59:25 +020043#define MAX_GUEST_NOTE_SIZE (1 << 20) /* 1MB should be enough */
44
45#define ELF_NOTE_SIZE(hdr_size, name_size, desc_size) \
46 ((DIV_ROUND_UP((hdr_size), 4) + \
47 DIV_ROUND_UP((name_size), 4) + \
48 DIV_ROUND_UP((desc_size), 4)) * 4)
49
Bharata B Raoacb0ef52014-05-19 19:57:44 +020050uint16_t cpu_to_dump16(DumpState *s, uint16_t val)
Wen Congyang783e9b42012-05-07 12:10:47 +080051{
Bharata B Raoacb0ef52014-05-19 19:57:44 +020052 if (s->dump_info.d_endian == ELFDATA2LSB) {
Wen Congyang783e9b42012-05-07 12:10:47 +080053 val = cpu_to_le16(val);
54 } else {
55 val = cpu_to_be16(val);
56 }
57
58 return val;
59}
60
Bharata B Raoacb0ef52014-05-19 19:57:44 +020061uint32_t cpu_to_dump32(DumpState *s, uint32_t val)
Wen Congyang783e9b42012-05-07 12:10:47 +080062{
Bharata B Raoacb0ef52014-05-19 19:57:44 +020063 if (s->dump_info.d_endian == ELFDATA2LSB) {
Wen Congyang783e9b42012-05-07 12:10:47 +080064 val = cpu_to_le32(val);
65 } else {
66 val = cpu_to_be32(val);
67 }
68
69 return val;
70}
71
Bharata B Raoacb0ef52014-05-19 19:57:44 +020072uint64_t cpu_to_dump64(DumpState *s, uint64_t val)
Wen Congyang783e9b42012-05-07 12:10:47 +080073{
Bharata B Raoacb0ef52014-05-19 19:57:44 +020074 if (s->dump_info.d_endian == ELFDATA2LSB) {
Wen Congyang783e9b42012-05-07 12:10:47 +080075 val = cpu_to_le64(val);
76 } else {
77 val = cpu_to_be64(val);
78 }
79
80 return val;
81}
82
Wen Congyang783e9b42012-05-07 12:10:47 +080083static int dump_cleanup(DumpState *s)
84{
Laszlo Ersek5ee163e2013-08-06 12:37:09 +020085 guest_phys_blocks_free(&s->guest_phys_blocks);
Wen Congyang783e9b42012-05-07 12:10:47 +080086 memory_mapping_list_free(&s->list);
Chen Gang29282072014-08-03 23:28:56 +080087 close(s->fd);
Marc-André Lureau903ef732017-09-11 18:59:25 +020088 g_free(s->guest_note);
89 s->guest_note = NULL;
Wen Congyang783e9b42012-05-07 12:10:47 +080090 if (s->resume) {
Fam Zheng6796b402017-05-03 15:28:19 +080091 if (s->detached) {
92 qemu_mutex_lock_iothread();
93 }
Wen Congyang783e9b42012-05-07 12:10:47 +080094 vm_start();
Fam Zheng6796b402017-05-03 15:28:19 +080095 if (s->detached) {
96 qemu_mutex_unlock_iothread();
97 }
Wen Congyang783e9b42012-05-07 12:10:47 +080098 }
99
Chen Gang29282072014-08-03 23:28:56 +0800100 return 0;
Wen Congyang783e9b42012-05-07 12:10:47 +0800101}
102
qiaonuohanb5ba1cc2014-02-18 14:11:25 +0800103static int fd_write_vmcore(const void *buf, size_t size, void *opaque)
Wen Congyang783e9b42012-05-07 12:10:47 +0800104{
105 DumpState *s = opaque;
Luiz Capitulino2f616522012-09-21 13:17:55 -0300106 size_t written_size;
Wen Congyang783e9b42012-05-07 12:10:47 +0800107
Luiz Capitulino2f616522012-09-21 13:17:55 -0300108 written_size = qemu_write_full(s->fd, buf, size);
109 if (written_size != size) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200110 return -errno;
Wen Congyang783e9b42012-05-07 12:10:47 +0800111 }
112
113 return 0;
114}
115
zhanghailiang4c7e2512014-10-09 14:13:11 +0800116static void write_elf64_header(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800117{
118 Elf64_Ehdr elf_header;
119 int ret;
Wen Congyang783e9b42012-05-07 12:10:47 +0800120
121 memset(&elf_header, 0, sizeof(Elf64_Ehdr));
122 memcpy(&elf_header, ELFMAG, SELFMAG);
123 elf_header.e_ident[EI_CLASS] = ELFCLASS64;
124 elf_header.e_ident[EI_DATA] = s->dump_info.d_endian;
125 elf_header.e_ident[EI_VERSION] = EV_CURRENT;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200126 elf_header.e_type = cpu_to_dump16(s, ET_CORE);
127 elf_header.e_machine = cpu_to_dump16(s, s->dump_info.d_machine);
128 elf_header.e_version = cpu_to_dump32(s, EV_CURRENT);
129 elf_header.e_ehsize = cpu_to_dump16(s, sizeof(elf_header));
130 elf_header.e_phoff = cpu_to_dump64(s, sizeof(Elf64_Ehdr));
131 elf_header.e_phentsize = cpu_to_dump16(s, sizeof(Elf64_Phdr));
132 elf_header.e_phnum = cpu_to_dump16(s, s->phdr_num);
Wen Congyang783e9b42012-05-07 12:10:47 +0800133 if (s->have_section) {
134 uint64_t shoff = sizeof(Elf64_Ehdr) + sizeof(Elf64_Phdr) * s->sh_info;
135
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200136 elf_header.e_shoff = cpu_to_dump64(s, shoff);
137 elf_header.e_shentsize = cpu_to_dump16(s, sizeof(Elf64_Shdr));
138 elf_header.e_shnum = cpu_to_dump16(s, 1);
Wen Congyang783e9b42012-05-07 12:10:47 +0800139 }
140
141 ret = fd_write_vmcore(&elf_header, sizeof(elf_header), s);
142 if (ret < 0) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200143 error_setg_errno(errp, -ret, "dump: failed to write elf header");
Wen Congyang783e9b42012-05-07 12:10:47 +0800144 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800145}
146
zhanghailiang4c7e2512014-10-09 14:13:11 +0800147static void write_elf32_header(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800148{
149 Elf32_Ehdr elf_header;
150 int ret;
Wen Congyang783e9b42012-05-07 12:10:47 +0800151
152 memset(&elf_header, 0, sizeof(Elf32_Ehdr));
153 memcpy(&elf_header, ELFMAG, SELFMAG);
154 elf_header.e_ident[EI_CLASS] = ELFCLASS32;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200155 elf_header.e_ident[EI_DATA] = s->dump_info.d_endian;
Wen Congyang783e9b42012-05-07 12:10:47 +0800156 elf_header.e_ident[EI_VERSION] = EV_CURRENT;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200157 elf_header.e_type = cpu_to_dump16(s, ET_CORE);
158 elf_header.e_machine = cpu_to_dump16(s, s->dump_info.d_machine);
159 elf_header.e_version = cpu_to_dump32(s, EV_CURRENT);
160 elf_header.e_ehsize = cpu_to_dump16(s, sizeof(elf_header));
161 elf_header.e_phoff = cpu_to_dump32(s, sizeof(Elf32_Ehdr));
162 elf_header.e_phentsize = cpu_to_dump16(s, sizeof(Elf32_Phdr));
163 elf_header.e_phnum = cpu_to_dump16(s, s->phdr_num);
Wen Congyang783e9b42012-05-07 12:10:47 +0800164 if (s->have_section) {
165 uint32_t shoff = sizeof(Elf32_Ehdr) + sizeof(Elf32_Phdr) * s->sh_info;
166
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200167 elf_header.e_shoff = cpu_to_dump32(s, shoff);
168 elf_header.e_shentsize = cpu_to_dump16(s, sizeof(Elf32_Shdr));
169 elf_header.e_shnum = cpu_to_dump16(s, 1);
Wen Congyang783e9b42012-05-07 12:10:47 +0800170 }
171
172 ret = fd_write_vmcore(&elf_header, sizeof(elf_header), s);
173 if (ret < 0) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200174 error_setg_errno(errp, -ret, "dump: failed to write elf header");
Wen Congyang783e9b42012-05-07 12:10:47 +0800175 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800176}
177
zhanghailiang4c7e2512014-10-09 14:13:11 +0800178static void write_elf64_load(DumpState *s, MemoryMapping *memory_mapping,
179 int phdr_index, hwaddr offset,
180 hwaddr filesz, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800181{
182 Elf64_Phdr phdr;
183 int ret;
Wen Congyang783e9b42012-05-07 12:10:47 +0800184
185 memset(&phdr, 0, sizeof(Elf64_Phdr));
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200186 phdr.p_type = cpu_to_dump32(s, PT_LOAD);
187 phdr.p_offset = cpu_to_dump64(s, offset);
188 phdr.p_paddr = cpu_to_dump64(s, memory_mapping->phys_addr);
189 phdr.p_filesz = cpu_to_dump64(s, filesz);
190 phdr.p_memsz = cpu_to_dump64(s, memory_mapping->length);
191 phdr.p_vaddr = cpu_to_dump64(s, memory_mapping->virt_addr);
Wen Congyang783e9b42012-05-07 12:10:47 +0800192
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200193 assert(memory_mapping->length >= filesz);
194
Wen Congyang783e9b42012-05-07 12:10:47 +0800195 ret = fd_write_vmcore(&phdr, sizeof(Elf64_Phdr), s);
196 if (ret < 0) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200197 error_setg_errno(errp, -ret,
198 "dump: failed to write program header table");
Wen Congyang783e9b42012-05-07 12:10:47 +0800199 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800200}
201
zhanghailiang4c7e2512014-10-09 14:13:11 +0800202static void write_elf32_load(DumpState *s, MemoryMapping *memory_mapping,
203 int phdr_index, hwaddr offset,
204 hwaddr filesz, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800205{
206 Elf32_Phdr phdr;
207 int ret;
Wen Congyang783e9b42012-05-07 12:10:47 +0800208
209 memset(&phdr, 0, sizeof(Elf32_Phdr));
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200210 phdr.p_type = cpu_to_dump32(s, PT_LOAD);
211 phdr.p_offset = cpu_to_dump32(s, offset);
212 phdr.p_paddr = cpu_to_dump32(s, memory_mapping->phys_addr);
213 phdr.p_filesz = cpu_to_dump32(s, filesz);
214 phdr.p_memsz = cpu_to_dump32(s, memory_mapping->length);
215 phdr.p_vaddr = cpu_to_dump32(s, memory_mapping->virt_addr);
Wen Congyang783e9b42012-05-07 12:10:47 +0800216
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200217 assert(memory_mapping->length >= filesz);
218
Wen Congyang783e9b42012-05-07 12:10:47 +0800219 ret = fd_write_vmcore(&phdr, sizeof(Elf32_Phdr), s);
220 if (ret < 0) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200221 error_setg_errno(errp, -ret,
222 "dump: failed to write program header table");
Wen Congyang783e9b42012-05-07 12:10:47 +0800223 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800224}
225
zhanghailiang4c7e2512014-10-09 14:13:11 +0800226static void write_elf64_note(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800227{
228 Elf64_Phdr phdr;
Avi Kivitya8170e52012-10-23 12:30:10 +0200229 hwaddr begin = s->memory_offset - s->note_size;
Wen Congyang783e9b42012-05-07 12:10:47 +0800230 int ret;
231
232 memset(&phdr, 0, sizeof(Elf64_Phdr));
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200233 phdr.p_type = cpu_to_dump32(s, PT_NOTE);
234 phdr.p_offset = cpu_to_dump64(s, begin);
Wen Congyang783e9b42012-05-07 12:10:47 +0800235 phdr.p_paddr = 0;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200236 phdr.p_filesz = cpu_to_dump64(s, s->note_size);
237 phdr.p_memsz = cpu_to_dump64(s, s->note_size);
Wen Congyang783e9b42012-05-07 12:10:47 +0800238 phdr.p_vaddr = 0;
239
240 ret = fd_write_vmcore(&phdr, sizeof(Elf64_Phdr), s);
241 if (ret < 0) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200242 error_setg_errno(errp, -ret,
243 "dump: failed to write program header table");
Wen Congyang783e9b42012-05-07 12:10:47 +0800244 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800245}
246
Paolo Bonzini0bc3cd62013-04-08 17:29:59 +0200247static inline int cpu_index(CPUState *cpu)
248{
249 return cpu->cpu_index + 1;
250}
251
Marc-André Lureau903ef732017-09-11 18:59:25 +0200252static void write_guest_note(WriteCoreDumpFunction f, DumpState *s,
253 Error **errp)
254{
255 int ret;
256
257 if (s->guest_note) {
258 ret = f(s->guest_note, s->guest_note_size, s);
259 if (ret < 0) {
260 error_setg(errp, "dump: failed to write guest note");
261 }
262 }
263}
264
zhanghailiang4c7e2512014-10-09 14:13:11 +0800265static void write_elf64_notes(WriteCoreDumpFunction f, DumpState *s,
266 Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800267{
Andreas Färber0d342822012-12-17 07:12:13 +0100268 CPUState *cpu;
Wen Congyang783e9b42012-05-07 12:10:47 +0800269 int ret;
270 int id;
271
Andreas Färberbdc44642013-06-24 23:50:24 +0200272 CPU_FOREACH(cpu) {
Andreas Färber0d342822012-12-17 07:12:13 +0100273 id = cpu_index(cpu);
qiaonuohan6a519912014-02-18 14:11:26 +0800274 ret = cpu_write_elf64_note(f, cpu, id, s);
Wen Congyang783e9b42012-05-07 12:10:47 +0800275 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800276 error_setg(errp, "dump: failed to write elf notes");
zhanghailiang4c7e2512014-10-09 14:13:11 +0800277 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800278 }
279 }
280
Andreas Färberbdc44642013-06-24 23:50:24 +0200281 CPU_FOREACH(cpu) {
qiaonuohan6a519912014-02-18 14:11:26 +0800282 ret = cpu_write_elf64_qemunote(f, cpu, s);
Wen Congyang783e9b42012-05-07 12:10:47 +0800283 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800284 error_setg(errp, "dump: failed to write CPU status");
zhanghailiang4c7e2512014-10-09 14:13:11 +0800285 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800286 }
287 }
Marc-André Lureau903ef732017-09-11 18:59:25 +0200288
289 write_guest_note(f, s, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800290}
291
zhanghailiang4c7e2512014-10-09 14:13:11 +0800292static void write_elf32_note(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800293{
Avi Kivitya8170e52012-10-23 12:30:10 +0200294 hwaddr begin = s->memory_offset - s->note_size;
Wen Congyang783e9b42012-05-07 12:10:47 +0800295 Elf32_Phdr phdr;
Wen Congyang783e9b42012-05-07 12:10:47 +0800296 int ret;
297
298 memset(&phdr, 0, sizeof(Elf32_Phdr));
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200299 phdr.p_type = cpu_to_dump32(s, PT_NOTE);
300 phdr.p_offset = cpu_to_dump32(s, begin);
Wen Congyang783e9b42012-05-07 12:10:47 +0800301 phdr.p_paddr = 0;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200302 phdr.p_filesz = cpu_to_dump32(s, s->note_size);
303 phdr.p_memsz = cpu_to_dump32(s, s->note_size);
Wen Congyang783e9b42012-05-07 12:10:47 +0800304 phdr.p_vaddr = 0;
305
306 ret = fd_write_vmcore(&phdr, sizeof(Elf32_Phdr), s);
307 if (ret < 0) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200308 error_setg_errno(errp, -ret,
309 "dump: failed to write program header table");
Wen Congyang783e9b42012-05-07 12:10:47 +0800310 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800311}
312
zhanghailiang4c7e2512014-10-09 14:13:11 +0800313static void write_elf32_notes(WriteCoreDumpFunction f, DumpState *s,
314 Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800315{
Andreas Färber0d342822012-12-17 07:12:13 +0100316 CPUState *cpu;
Wen Congyang783e9b42012-05-07 12:10:47 +0800317 int ret;
318 int id;
319
Andreas Färberbdc44642013-06-24 23:50:24 +0200320 CPU_FOREACH(cpu) {
Andreas Färber0d342822012-12-17 07:12:13 +0100321 id = cpu_index(cpu);
qiaonuohan6a519912014-02-18 14:11:26 +0800322 ret = cpu_write_elf32_note(f, cpu, id, s);
Wen Congyang783e9b42012-05-07 12:10:47 +0800323 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800324 error_setg(errp, "dump: failed to write elf notes");
zhanghailiang4c7e2512014-10-09 14:13:11 +0800325 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800326 }
327 }
328
Andreas Färberbdc44642013-06-24 23:50:24 +0200329 CPU_FOREACH(cpu) {
qiaonuohan6a519912014-02-18 14:11:26 +0800330 ret = cpu_write_elf32_qemunote(f, cpu, s);
Wen Congyang783e9b42012-05-07 12:10:47 +0800331 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800332 error_setg(errp, "dump: failed to write CPU status");
zhanghailiang4c7e2512014-10-09 14:13:11 +0800333 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800334 }
335 }
Marc-André Lureau903ef732017-09-11 18:59:25 +0200336
337 write_guest_note(f, s, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800338}
339
zhanghailiang4c7e2512014-10-09 14:13:11 +0800340static void write_elf_section(DumpState *s, int type, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800341{
342 Elf32_Shdr shdr32;
343 Elf64_Shdr shdr64;
Wen Congyang783e9b42012-05-07 12:10:47 +0800344 int shdr_size;
345 void *shdr;
346 int ret;
347
348 if (type == 0) {
349 shdr_size = sizeof(Elf32_Shdr);
350 memset(&shdr32, 0, shdr_size);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200351 shdr32.sh_info = cpu_to_dump32(s, s->sh_info);
Wen Congyang783e9b42012-05-07 12:10:47 +0800352 shdr = &shdr32;
353 } else {
354 shdr_size = sizeof(Elf64_Shdr);
355 memset(&shdr64, 0, shdr_size);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200356 shdr64.sh_info = cpu_to_dump32(s, s->sh_info);
Wen Congyang783e9b42012-05-07 12:10:47 +0800357 shdr = &shdr64;
358 }
359
360 ret = fd_write_vmcore(&shdr, shdr_size, s);
361 if (ret < 0) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200362 error_setg_errno(errp, -ret,
363 "dump: failed to write section header table");
Wen Congyang783e9b42012-05-07 12:10:47 +0800364 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800365}
366
zhanghailiang4c7e2512014-10-09 14:13:11 +0800367static void write_data(DumpState *s, void *buf, int length, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800368{
369 int ret;
370
371 ret = fd_write_vmcore(buf, length, s);
372 if (ret < 0) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200373 error_setg_errno(errp, -ret, "dump: failed to save memory");
Peter Xu2264c2c2016-02-18 13:16:53 +0800374 } else {
375 s->written_size += length;
Wen Congyang783e9b42012-05-07 12:10:47 +0800376 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800377}
378
zhanghailiang4c7e2512014-10-09 14:13:11 +0800379/* write the memory to vmcore. 1 page per I/O. */
380static void write_memory(DumpState *s, GuestPhysBlock *block, ram_addr_t start,
381 int64_t size, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800382{
383 int64_t i;
zhanghailiang4c7e2512014-10-09 14:13:11 +0800384 Error *local_err = NULL;
Wen Congyang783e9b42012-05-07 12:10:47 +0800385
Andrew Jones8161bef2016-01-11 20:56:20 +0100386 for (i = 0; i < size / s->dump_info.page_size; i++) {
387 write_data(s, block->host_addr + start + i * s->dump_info.page_size,
388 s->dump_info.page_size, &local_err);
zhanghailiang4c7e2512014-10-09 14:13:11 +0800389 if (local_err) {
390 error_propagate(errp, local_err);
391 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800392 }
393 }
394
Andrew Jones8161bef2016-01-11 20:56:20 +0100395 if ((size % s->dump_info.page_size) != 0) {
396 write_data(s, block->host_addr + start + i * s->dump_info.page_size,
397 size % s->dump_info.page_size, &local_err);
zhanghailiang4c7e2512014-10-09 14:13:11 +0800398 if (local_err) {
399 error_propagate(errp, local_err);
400 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800401 }
402 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800403}
404
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200405/* get the memory's offset and size in the vmcore */
406static void get_offset_range(hwaddr phys_addr,
407 ram_addr_t mapping_length,
408 DumpState *s,
409 hwaddr *p_offset,
410 hwaddr *p_filesz)
Wen Congyang783e9b42012-05-07 12:10:47 +0800411{
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200412 GuestPhysBlock *block;
Avi Kivitya8170e52012-10-23 12:30:10 +0200413 hwaddr offset = s->memory_offset;
Wen Congyang783e9b42012-05-07 12:10:47 +0800414 int64_t size_in_block, start;
415
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200416 /* When the memory is not stored into vmcore, offset will be -1 */
417 *p_offset = -1;
418 *p_filesz = 0;
419
Wen Congyang783e9b42012-05-07 12:10:47 +0800420 if (s->has_filter) {
421 if (phys_addr < s->begin || phys_addr >= s->begin + s->length) {
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200422 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800423 }
424 }
425
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200426 QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
Wen Congyang783e9b42012-05-07 12:10:47 +0800427 if (s->has_filter) {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200428 if (block->target_start >= s->begin + s->length ||
429 block->target_end <= s->begin) {
Wen Congyang783e9b42012-05-07 12:10:47 +0800430 /* This block is out of the range */
431 continue;
432 }
433
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200434 if (s->begin <= block->target_start) {
435 start = block->target_start;
Wen Congyang783e9b42012-05-07 12:10:47 +0800436 } else {
437 start = s->begin;
438 }
439
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200440 size_in_block = block->target_end - start;
441 if (s->begin + s->length < block->target_end) {
442 size_in_block -= block->target_end - (s->begin + s->length);
Wen Congyang783e9b42012-05-07 12:10:47 +0800443 }
444 } else {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200445 start = block->target_start;
446 size_in_block = block->target_end - block->target_start;
Wen Congyang783e9b42012-05-07 12:10:47 +0800447 }
448
449 if (phys_addr >= start && phys_addr < start + size_in_block) {
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200450 *p_offset = phys_addr - start + offset;
451
452 /* The offset range mapped from the vmcore file must not spill over
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200453 * the GuestPhysBlock, clamp it. The rest of the mapping will be
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200454 * zero-filled in memory at load time; see
455 * <http://refspecs.linuxbase.org/elf/gabi4+/ch5.pheader.html>.
456 */
457 *p_filesz = phys_addr + mapping_length <= start + size_in_block ?
458 mapping_length :
459 size_in_block - (phys_addr - start);
460 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800461 }
462
463 offset += size_in_block;
464 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800465}
466
zhanghailiang4c7e2512014-10-09 14:13:11 +0800467static void write_elf_loads(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800468{
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200469 hwaddr offset, filesz;
Wen Congyang783e9b42012-05-07 12:10:47 +0800470 MemoryMapping *memory_mapping;
471 uint32_t phdr_index = 1;
Wen Congyang783e9b42012-05-07 12:10:47 +0800472 uint32_t max_index;
zhanghailiang4c7e2512014-10-09 14:13:11 +0800473 Error *local_err = NULL;
Wen Congyang783e9b42012-05-07 12:10:47 +0800474
475 if (s->have_section) {
476 max_index = s->sh_info;
477 } else {
478 max_index = s->phdr_num;
479 }
480
481 QTAILQ_FOREACH(memory_mapping, &s->list.head, next) {
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200482 get_offset_range(memory_mapping->phys_addr,
483 memory_mapping->length,
484 s, &offset, &filesz);
Wen Congyang783e9b42012-05-07 12:10:47 +0800485 if (s->dump_info.d_class == ELFCLASS64) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800486 write_elf64_load(s, memory_mapping, phdr_index++, offset,
487 filesz, &local_err);
Wen Congyang783e9b42012-05-07 12:10:47 +0800488 } else {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800489 write_elf32_load(s, memory_mapping, phdr_index++, offset,
490 filesz, &local_err);
Wen Congyang783e9b42012-05-07 12:10:47 +0800491 }
492
zhanghailiang4c7e2512014-10-09 14:13:11 +0800493 if (local_err) {
494 error_propagate(errp, local_err);
495 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800496 }
497
498 if (phdr_index >= max_index) {
499 break;
500 }
501 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800502}
503
504/* write elf header, PT_NOTE and elf note to vmcore. */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800505static void dump_begin(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800506{
zhanghailiang4c7e2512014-10-09 14:13:11 +0800507 Error *local_err = NULL;
Wen Congyang783e9b42012-05-07 12:10:47 +0800508
509 /*
510 * the vmcore's format is:
511 * --------------
512 * | elf header |
513 * --------------
514 * | PT_NOTE |
515 * --------------
516 * | PT_LOAD |
517 * --------------
518 * | ...... |
519 * --------------
520 * | PT_LOAD |
521 * --------------
522 * | sec_hdr |
523 * --------------
524 * | elf note |
525 * --------------
526 * | memory |
527 * --------------
528 *
529 * we only know where the memory is saved after we write elf note into
530 * vmcore.
531 */
532
533 /* write elf header to vmcore */
534 if (s->dump_info.d_class == ELFCLASS64) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800535 write_elf64_header(s, &local_err);
Wen Congyang783e9b42012-05-07 12:10:47 +0800536 } else {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800537 write_elf32_header(s, &local_err);
Wen Congyang783e9b42012-05-07 12:10:47 +0800538 }
zhanghailiang4c7e2512014-10-09 14:13:11 +0800539 if (local_err) {
540 error_propagate(errp, local_err);
541 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800542 }
543
544 if (s->dump_info.d_class == ELFCLASS64) {
545 /* write PT_NOTE to vmcore */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800546 write_elf64_note(s, &local_err);
547 if (local_err) {
548 error_propagate(errp, local_err);
549 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800550 }
551
552 /* write all PT_LOAD to vmcore */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800553 write_elf_loads(s, &local_err);
554 if (local_err) {
555 error_propagate(errp, local_err);
556 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800557 }
558
559 /* write section to vmcore */
560 if (s->have_section) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800561 write_elf_section(s, 1, &local_err);
562 if (local_err) {
563 error_propagate(errp, local_err);
564 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800565 }
566 }
567
568 /* write notes to vmcore */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800569 write_elf64_notes(fd_write_vmcore, s, &local_err);
570 if (local_err) {
571 error_propagate(errp, local_err);
572 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800573 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800574 } else {
575 /* write PT_NOTE to vmcore */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800576 write_elf32_note(s, &local_err);
577 if (local_err) {
578 error_propagate(errp, local_err);
579 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800580 }
581
582 /* write all PT_LOAD to vmcore */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800583 write_elf_loads(s, &local_err);
584 if (local_err) {
585 error_propagate(errp, local_err);
586 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800587 }
588
589 /* write section to vmcore */
590 if (s->have_section) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800591 write_elf_section(s, 0, &local_err);
592 if (local_err) {
593 error_propagate(errp, local_err);
594 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800595 }
596 }
597
598 /* write notes to vmcore */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800599 write_elf32_notes(fd_write_vmcore, s, &local_err);
600 if (local_err) {
601 error_propagate(errp, local_err);
602 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800603 }
604 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800605}
606
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200607static int get_next_block(DumpState *s, GuestPhysBlock *block)
Wen Congyang783e9b42012-05-07 12:10:47 +0800608{
609 while (1) {
Paolo Bonzinia3161032012-11-14 15:54:48 +0100610 block = QTAILQ_NEXT(block, next);
Wen Congyang783e9b42012-05-07 12:10:47 +0800611 if (!block) {
612 /* no more block */
613 return 1;
614 }
615
616 s->start = 0;
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200617 s->next_block = block;
Wen Congyang783e9b42012-05-07 12:10:47 +0800618 if (s->has_filter) {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200619 if (block->target_start >= s->begin + s->length ||
620 block->target_end <= s->begin) {
Wen Congyang783e9b42012-05-07 12:10:47 +0800621 /* This block is out of the range */
622 continue;
623 }
624
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200625 if (s->begin > block->target_start) {
626 s->start = s->begin - block->target_start;
Wen Congyang783e9b42012-05-07 12:10:47 +0800627 }
628 }
629
630 return 0;
631 }
632}
633
634/* write all memory to vmcore */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800635static void dump_iterate(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800636{
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200637 GuestPhysBlock *block;
Wen Congyang783e9b42012-05-07 12:10:47 +0800638 int64_t size;
zhanghailiang4c7e2512014-10-09 14:13:11 +0800639 Error *local_err = NULL;
Wen Congyang783e9b42012-05-07 12:10:47 +0800640
Gonglei08a655b2014-10-30 14:01:17 +0800641 do {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200642 block = s->next_block;
Wen Congyang783e9b42012-05-07 12:10:47 +0800643
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200644 size = block->target_end - block->target_start;
Wen Congyang783e9b42012-05-07 12:10:47 +0800645 if (s->has_filter) {
646 size -= s->start;
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200647 if (s->begin + s->length < block->target_end) {
648 size -= block->target_end - (s->begin + s->length);
Wen Congyang783e9b42012-05-07 12:10:47 +0800649 }
650 }
zhanghailiang4c7e2512014-10-09 14:13:11 +0800651 write_memory(s, block, s->start, size, &local_err);
652 if (local_err) {
653 error_propagate(errp, local_err);
654 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800655 }
656
Gonglei08a655b2014-10-30 14:01:17 +0800657 } while (!get_next_block(s, block));
Wen Congyang783e9b42012-05-07 12:10:47 +0800658}
659
zhanghailiang4c7e2512014-10-09 14:13:11 +0800660static void create_vmcore(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800661{
zhanghailiang4c7e2512014-10-09 14:13:11 +0800662 Error *local_err = NULL;
Wen Congyang783e9b42012-05-07 12:10:47 +0800663
zhanghailiang4c7e2512014-10-09 14:13:11 +0800664 dump_begin(s, &local_err);
665 if (local_err) {
666 error_propagate(errp, local_err);
667 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800668 }
669
zhanghailiang4c7e2512014-10-09 14:13:11 +0800670 dump_iterate(s, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800671}
672
qiaonuohanfda05382014-02-18 14:11:27 +0800673static int write_start_flat_header(int fd)
674{
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200675 MakedumpfileHeader *mh;
qiaonuohanfda05382014-02-18 14:11:27 +0800676 int ret = 0;
677
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200678 QEMU_BUILD_BUG_ON(sizeof *mh > MAX_SIZE_MDF_HEADER);
679 mh = g_malloc0(MAX_SIZE_MDF_HEADER);
qiaonuohanfda05382014-02-18 14:11:27 +0800680
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200681 memcpy(mh->signature, MAKEDUMPFILE_SIGNATURE,
682 MIN(sizeof mh->signature, sizeof MAKEDUMPFILE_SIGNATURE));
qiaonuohanfda05382014-02-18 14:11:27 +0800683
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200684 mh->type = cpu_to_be64(TYPE_FLAT_HEADER);
685 mh->version = cpu_to_be64(VERSION_FLAT_HEADER);
qiaonuohanfda05382014-02-18 14:11:27 +0800686
687 size_t written_size;
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200688 written_size = qemu_write_full(fd, mh, MAX_SIZE_MDF_HEADER);
qiaonuohanfda05382014-02-18 14:11:27 +0800689 if (written_size != MAX_SIZE_MDF_HEADER) {
690 ret = -1;
691 }
692
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200693 g_free(mh);
qiaonuohanfda05382014-02-18 14:11:27 +0800694 return ret;
695}
696
697static int write_end_flat_header(int fd)
698{
699 MakedumpfileDataHeader mdh;
700
701 mdh.offset = END_FLAG_FLAT_HEADER;
702 mdh.buf_size = END_FLAG_FLAT_HEADER;
703
704 size_t written_size;
705 written_size = qemu_write_full(fd, &mdh, sizeof(mdh));
706 if (written_size != sizeof(mdh)) {
707 return -1;
708 }
709
710 return 0;
711}
712
qiaonuohan5d31bab2014-02-18 14:11:28 +0800713static int write_buffer(int fd, off_t offset, const void *buf, size_t size)
714{
715 size_t written_size;
716 MakedumpfileDataHeader mdh;
717
718 mdh.offset = cpu_to_be64(offset);
719 mdh.buf_size = cpu_to_be64(size);
720
721 written_size = qemu_write_full(fd, &mdh, sizeof(mdh));
722 if (written_size != sizeof(mdh)) {
723 return -1;
724 }
725
726 written_size = qemu_write_full(fd, buf, size);
727 if (written_size != size) {
728 return -1;
729 }
730
731 return 0;
732}
733
qiaonuohan4835ef72014-02-18 14:11:29 +0800734static int buf_write_note(const void *buf, size_t size, void *opaque)
735{
736 DumpState *s = opaque;
737
738 /* note_buf is not enough */
739 if (s->note_buf_offset + size > s->note_size) {
740 return -1;
741 }
742
743 memcpy(s->note_buf + s->note_buf_offset, buf, size);
744
745 s->note_buf_offset += size;
746
747 return 0;
748}
749
Marc-André Lureau903ef732017-09-11 18:59:25 +0200750/*
751 * This function retrieves various sizes from an elf header.
752 *
753 * @note has to be a valid ELF note. The return sizes are unmodified
754 * (not padded or rounded up to be multiple of 4).
755 */
756static void get_note_sizes(DumpState *s, const void *note,
757 uint64_t *note_head_size,
758 uint64_t *name_size,
759 uint64_t *desc_size)
760{
761 uint64_t note_head_sz;
762 uint64_t name_sz;
763 uint64_t desc_sz;
764
765 if (s->dump_info.d_class == ELFCLASS64) {
766 const Elf64_Nhdr *hdr = note;
767 note_head_sz = sizeof(Elf64_Nhdr);
768 name_sz = tswap64(hdr->n_namesz);
769 desc_sz = tswap64(hdr->n_descsz);
770 } else {
771 const Elf32_Nhdr *hdr = note;
772 note_head_sz = sizeof(Elf32_Nhdr);
773 name_sz = tswap32(hdr->n_namesz);
774 desc_sz = tswap32(hdr->n_descsz);
775 }
776
777 if (note_head_size) {
778 *note_head_size = note_head_sz;
779 }
780 if (name_size) {
781 *name_size = name_sz;
782 }
783 if (desc_size) {
784 *desc_size = desc_sz;
785 }
786}
787
Marc-André Lureaud9feb512017-09-11 18:59:26 +0200788static bool note_name_equal(DumpState *s,
789 const uint8_t *note, const char *name)
790{
791 int len = strlen(name) + 1;
792 uint64_t head_size, name_size;
793
794 get_note_sizes(s, note, &head_size, &name_size, NULL);
795 head_size = ROUND_UP(head_size, 4);
796
Marc-André Lureauc983ca82017-12-12 15:53:59 +0100797 return name_size == len && memcmp(note + head_size, name, len) == 0;
Marc-André Lureaud9feb512017-09-11 18:59:26 +0200798}
799
qiaonuohan298f1162014-02-18 14:11:32 +0800800/* write common header, sub header and elf note to vmcore */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800801static void create_header32(DumpState *s, Error **errp)
qiaonuohan298f1162014-02-18 14:11:32 +0800802{
qiaonuohan298f1162014-02-18 14:11:32 +0800803 DiskDumpHeader32 *dh = NULL;
804 KdumpSubHeader32 *kh = NULL;
805 size_t size;
qiaonuohan298f1162014-02-18 14:11:32 +0800806 uint32_t block_size;
807 uint32_t sub_hdr_size;
808 uint32_t bitmap_blocks;
809 uint32_t status = 0;
810 uint64_t offset_note;
zhanghailiang4c7e2512014-10-09 14:13:11 +0800811 Error *local_err = NULL;
qiaonuohan298f1162014-02-18 14:11:32 +0800812
813 /* write common header, the version of kdump-compressed format is 6th */
814 size = sizeof(DiskDumpHeader32);
815 dh = g_malloc0(size);
816
Eric Blake84c868f2018-03-27 15:21:51 -0500817 memcpy(dh->signature, KDUMP_SIGNATURE, SIG_LEN);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200818 dh->header_version = cpu_to_dump32(s, 6);
Andrew Jones8161bef2016-01-11 20:56:20 +0100819 block_size = s->dump_info.page_size;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200820 dh->block_size = cpu_to_dump32(s, block_size);
qiaonuohan298f1162014-02-18 14:11:32 +0800821 sub_hdr_size = sizeof(struct KdumpSubHeader32) + s->note_size;
822 sub_hdr_size = DIV_ROUND_UP(sub_hdr_size, block_size);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200823 dh->sub_hdr_size = cpu_to_dump32(s, sub_hdr_size);
qiaonuohan298f1162014-02-18 14:11:32 +0800824 /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200825 dh->max_mapnr = cpu_to_dump32(s, MIN(s->max_mapnr, UINT_MAX));
826 dh->nr_cpus = cpu_to_dump32(s, s->nr_cpus);
qiaonuohan298f1162014-02-18 14:11:32 +0800827 bitmap_blocks = DIV_ROUND_UP(s->len_dump_bitmap, block_size) * 2;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200828 dh->bitmap_blocks = cpu_to_dump32(s, bitmap_blocks);
qiaonuohan4ab23a92014-02-18 14:11:37 +0800829 strncpy(dh->utsname.machine, ELF_MACHINE_UNAME, sizeof(dh->utsname.machine));
qiaonuohan298f1162014-02-18 14:11:32 +0800830
831 if (s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) {
832 status |= DUMP_DH_COMPRESSED_ZLIB;
833 }
834#ifdef CONFIG_LZO
835 if (s->flag_compress & DUMP_DH_COMPRESSED_LZO) {
836 status |= DUMP_DH_COMPRESSED_LZO;
837 }
838#endif
839#ifdef CONFIG_SNAPPY
840 if (s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) {
841 status |= DUMP_DH_COMPRESSED_SNAPPY;
842 }
843#endif
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200844 dh->status = cpu_to_dump32(s, status);
qiaonuohan298f1162014-02-18 14:11:32 +0800845
846 if (write_buffer(s->fd, 0, dh, size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800847 error_setg(errp, "dump: failed to write disk dump header");
qiaonuohan298f1162014-02-18 14:11:32 +0800848 goto out;
849 }
850
851 /* write sub header */
852 size = sizeof(KdumpSubHeader32);
853 kh = g_malloc0(size);
854
855 /* 64bit max_mapnr_64 */
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200856 kh->max_mapnr_64 = cpu_to_dump64(s, s->max_mapnr);
Andrew Jonesb6e05aa2016-01-11 20:56:21 +0100857 kh->phys_base = cpu_to_dump32(s, s->dump_info.phys_base);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200858 kh->dump_level = cpu_to_dump32(s, DUMP_LEVEL);
qiaonuohan298f1162014-02-18 14:11:32 +0800859
860 offset_note = DISKDUMP_HEADER_BLOCKS * block_size + size;
Marc-André Lureau9ada5752017-09-11 18:59:27 +0200861 if (s->guest_note &&
862 note_name_equal(s, s->guest_note, "VMCOREINFO")) {
863 uint64_t hsize, name_size, size_vmcoreinfo_desc, offset_vmcoreinfo;
864
865 get_note_sizes(s, s->guest_note,
866 &hsize, &name_size, &size_vmcoreinfo_desc);
867 offset_vmcoreinfo = offset_note + s->note_size - s->guest_note_size +
868 (DIV_ROUND_UP(hsize, 4) + DIV_ROUND_UP(name_size, 4)) * 4;
869 kh->offset_vmcoreinfo = cpu_to_dump64(s, offset_vmcoreinfo);
870 kh->size_vmcoreinfo = cpu_to_dump32(s, size_vmcoreinfo_desc);
871 }
872
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200873 kh->offset_note = cpu_to_dump64(s, offset_note);
874 kh->note_size = cpu_to_dump32(s, s->note_size);
qiaonuohan298f1162014-02-18 14:11:32 +0800875
876 if (write_buffer(s->fd, DISKDUMP_HEADER_BLOCKS *
877 block_size, kh, size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800878 error_setg(errp, "dump: failed to write kdump sub header");
qiaonuohan298f1162014-02-18 14:11:32 +0800879 goto out;
880 }
881
882 /* write note */
883 s->note_buf = g_malloc0(s->note_size);
884 s->note_buf_offset = 0;
885
886 /* use s->note_buf to store notes temporarily */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800887 write_elf32_notes(buf_write_note, s, &local_err);
888 if (local_err) {
889 error_propagate(errp, local_err);
qiaonuohan298f1162014-02-18 14:11:32 +0800890 goto out;
891 }
qiaonuohan298f1162014-02-18 14:11:32 +0800892 if (write_buffer(s->fd, offset_note, s->note_buf,
893 s->note_size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800894 error_setg(errp, "dump: failed to write notes");
qiaonuohan298f1162014-02-18 14:11:32 +0800895 goto out;
896 }
897
898 /* get offset of dump_bitmap */
899 s->offset_dump_bitmap = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size) *
900 block_size;
901
902 /* get offset of page */
903 s->offset_page = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size + bitmap_blocks) *
904 block_size;
905
906out:
907 g_free(dh);
908 g_free(kh);
909 g_free(s->note_buf);
qiaonuohan298f1162014-02-18 14:11:32 +0800910}
911
912/* write common header, sub header and elf note to vmcore */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800913static void create_header64(DumpState *s, Error **errp)
qiaonuohan298f1162014-02-18 14:11:32 +0800914{
qiaonuohan298f1162014-02-18 14:11:32 +0800915 DiskDumpHeader64 *dh = NULL;
916 KdumpSubHeader64 *kh = NULL;
917 size_t size;
qiaonuohan298f1162014-02-18 14:11:32 +0800918 uint32_t block_size;
919 uint32_t sub_hdr_size;
920 uint32_t bitmap_blocks;
921 uint32_t status = 0;
922 uint64_t offset_note;
zhanghailiang4c7e2512014-10-09 14:13:11 +0800923 Error *local_err = NULL;
qiaonuohan298f1162014-02-18 14:11:32 +0800924
925 /* write common header, the version of kdump-compressed format is 6th */
926 size = sizeof(DiskDumpHeader64);
927 dh = g_malloc0(size);
928
Eric Blake84c868f2018-03-27 15:21:51 -0500929 memcpy(dh->signature, KDUMP_SIGNATURE, SIG_LEN);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200930 dh->header_version = cpu_to_dump32(s, 6);
Andrew Jones8161bef2016-01-11 20:56:20 +0100931 block_size = s->dump_info.page_size;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200932 dh->block_size = cpu_to_dump32(s, block_size);
qiaonuohan298f1162014-02-18 14:11:32 +0800933 sub_hdr_size = sizeof(struct KdumpSubHeader64) + s->note_size;
934 sub_hdr_size = DIV_ROUND_UP(sub_hdr_size, block_size);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200935 dh->sub_hdr_size = cpu_to_dump32(s, sub_hdr_size);
qiaonuohan298f1162014-02-18 14:11:32 +0800936 /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200937 dh->max_mapnr = cpu_to_dump32(s, MIN(s->max_mapnr, UINT_MAX));
938 dh->nr_cpus = cpu_to_dump32(s, s->nr_cpus);
qiaonuohan298f1162014-02-18 14:11:32 +0800939 bitmap_blocks = DIV_ROUND_UP(s->len_dump_bitmap, block_size) * 2;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200940 dh->bitmap_blocks = cpu_to_dump32(s, bitmap_blocks);
qiaonuohan4ab23a92014-02-18 14:11:37 +0800941 strncpy(dh->utsname.machine, ELF_MACHINE_UNAME, sizeof(dh->utsname.machine));
qiaonuohan298f1162014-02-18 14:11:32 +0800942
943 if (s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) {
944 status |= DUMP_DH_COMPRESSED_ZLIB;
945 }
946#ifdef CONFIG_LZO
947 if (s->flag_compress & DUMP_DH_COMPRESSED_LZO) {
948 status |= DUMP_DH_COMPRESSED_LZO;
949 }
950#endif
951#ifdef CONFIG_SNAPPY
952 if (s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) {
953 status |= DUMP_DH_COMPRESSED_SNAPPY;
954 }
955#endif
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200956 dh->status = cpu_to_dump32(s, status);
qiaonuohan298f1162014-02-18 14:11:32 +0800957
958 if (write_buffer(s->fd, 0, dh, size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800959 error_setg(errp, "dump: failed to write disk dump header");
qiaonuohan298f1162014-02-18 14:11:32 +0800960 goto out;
961 }
962
963 /* write sub header */
964 size = sizeof(KdumpSubHeader64);
965 kh = g_malloc0(size);
966
967 /* 64bit max_mapnr_64 */
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200968 kh->max_mapnr_64 = cpu_to_dump64(s, s->max_mapnr);
Andrew Jonesb6e05aa2016-01-11 20:56:21 +0100969 kh->phys_base = cpu_to_dump64(s, s->dump_info.phys_base);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200970 kh->dump_level = cpu_to_dump32(s, DUMP_LEVEL);
qiaonuohan298f1162014-02-18 14:11:32 +0800971
972 offset_note = DISKDUMP_HEADER_BLOCKS * block_size + size;
Marc-André Lureau9ada5752017-09-11 18:59:27 +0200973 if (s->guest_note &&
974 note_name_equal(s, s->guest_note, "VMCOREINFO")) {
975 uint64_t hsize, name_size, size_vmcoreinfo_desc, offset_vmcoreinfo;
976
977 get_note_sizes(s, s->guest_note,
978 &hsize, &name_size, &size_vmcoreinfo_desc);
979 offset_vmcoreinfo = offset_note + s->note_size - s->guest_note_size +
980 (DIV_ROUND_UP(hsize, 4) + DIV_ROUND_UP(name_size, 4)) * 4;
981 kh->offset_vmcoreinfo = cpu_to_dump64(s, offset_vmcoreinfo);
982 kh->size_vmcoreinfo = cpu_to_dump64(s, size_vmcoreinfo_desc);
983 }
984
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200985 kh->offset_note = cpu_to_dump64(s, offset_note);
986 kh->note_size = cpu_to_dump64(s, s->note_size);
qiaonuohan298f1162014-02-18 14:11:32 +0800987
988 if (write_buffer(s->fd, DISKDUMP_HEADER_BLOCKS *
989 block_size, kh, size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800990 error_setg(errp, "dump: failed to write kdump sub header");
qiaonuohan298f1162014-02-18 14:11:32 +0800991 goto out;
992 }
993
994 /* write note */
995 s->note_buf = g_malloc0(s->note_size);
996 s->note_buf_offset = 0;
997
998 /* use s->note_buf to store notes temporarily */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800999 write_elf64_notes(buf_write_note, s, &local_err);
1000 if (local_err) {
1001 error_propagate(errp, local_err);
qiaonuohan298f1162014-02-18 14:11:32 +08001002 goto out;
1003 }
1004
1005 if (write_buffer(s->fd, offset_note, s->note_buf,
1006 s->note_size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001007 error_setg(errp, "dump: failed to write notes");
qiaonuohan298f1162014-02-18 14:11:32 +08001008 goto out;
1009 }
1010
1011 /* get offset of dump_bitmap */
1012 s->offset_dump_bitmap = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size) *
1013 block_size;
1014
1015 /* get offset of page */
1016 s->offset_page = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size + bitmap_blocks) *
1017 block_size;
1018
1019out:
1020 g_free(dh);
1021 g_free(kh);
1022 g_free(s->note_buf);
qiaonuohan298f1162014-02-18 14:11:32 +08001023}
1024
zhanghailiang4c7e2512014-10-09 14:13:11 +08001025static void write_dump_header(DumpState *s, Error **errp)
qiaonuohan298f1162014-02-18 14:11:32 +08001026{
zhanghailiang4c7e2512014-10-09 14:13:11 +08001027 Error *local_err = NULL;
1028
Laszlo Ersek24aeeac2014-05-20 13:39:45 +02001029 if (s->dump_info.d_class == ELFCLASS32) {
zhanghailiang4c7e2512014-10-09 14:13:11 +08001030 create_header32(s, &local_err);
qiaonuohan298f1162014-02-18 14:11:32 +08001031 } else {
zhanghailiang4c7e2512014-10-09 14:13:11 +08001032 create_header64(s, &local_err);
1033 }
Eduardo Habkost621ff942016-06-13 18:57:56 -03001034 error_propagate(errp, local_err);
qiaonuohan298f1162014-02-18 14:11:32 +08001035}
1036
Andrew Jones8161bef2016-01-11 20:56:20 +01001037static size_t dump_bitmap_get_bufsize(DumpState *s)
1038{
1039 return s->dump_info.page_size;
1040}
1041
qiaonuohand0686c72014-02-18 14:11:33 +08001042/*
1043 * set dump_bitmap sequencely. the bit before last_pfn is not allowed to be
1044 * rewritten, so if need to set the first bit, set last_pfn and pfn to 0.
1045 * set_dump_bitmap will always leave the recently set bit un-sync. And setting
1046 * (last bit + sizeof(buf) * 8) to 0 will do flushing the content in buf into
1047 * vmcore, ie. synchronizing un-sync bit into vmcore.
1048 */
1049static int set_dump_bitmap(uint64_t last_pfn, uint64_t pfn, bool value,
1050 uint8_t *buf, DumpState *s)
1051{
1052 off_t old_offset, new_offset;
1053 off_t offset_bitmap1, offset_bitmap2;
1054 uint32_t byte, bit;
Andrew Jones8161bef2016-01-11 20:56:20 +01001055 size_t bitmap_bufsize = dump_bitmap_get_bufsize(s);
1056 size_t bits_per_buf = bitmap_bufsize * CHAR_BIT;
qiaonuohand0686c72014-02-18 14:11:33 +08001057
1058 /* should not set the previous place */
1059 assert(last_pfn <= pfn);
1060
1061 /*
1062 * if the bit needed to be set is not cached in buf, flush the data in buf
1063 * to vmcore firstly.
1064 * making new_offset be bigger than old_offset can also sync remained data
1065 * into vmcore.
1066 */
Andrew Jones8161bef2016-01-11 20:56:20 +01001067 old_offset = bitmap_bufsize * (last_pfn / bits_per_buf);
1068 new_offset = bitmap_bufsize * (pfn / bits_per_buf);
qiaonuohand0686c72014-02-18 14:11:33 +08001069
1070 while (old_offset < new_offset) {
1071 /* calculate the offset and write dump_bitmap */
1072 offset_bitmap1 = s->offset_dump_bitmap + old_offset;
1073 if (write_buffer(s->fd, offset_bitmap1, buf,
Andrew Jones8161bef2016-01-11 20:56:20 +01001074 bitmap_bufsize) < 0) {
qiaonuohand0686c72014-02-18 14:11:33 +08001075 return -1;
1076 }
1077
1078 /* dump level 1 is chosen, so 1st and 2nd bitmap are same */
1079 offset_bitmap2 = s->offset_dump_bitmap + s->len_dump_bitmap +
1080 old_offset;
1081 if (write_buffer(s->fd, offset_bitmap2, buf,
Andrew Jones8161bef2016-01-11 20:56:20 +01001082 bitmap_bufsize) < 0) {
qiaonuohand0686c72014-02-18 14:11:33 +08001083 return -1;
1084 }
1085
Andrew Jones8161bef2016-01-11 20:56:20 +01001086 memset(buf, 0, bitmap_bufsize);
1087 old_offset += bitmap_bufsize;
qiaonuohand0686c72014-02-18 14:11:33 +08001088 }
1089
1090 /* get the exact place of the bit in the buf, and set it */
Andrew Jones8161bef2016-01-11 20:56:20 +01001091 byte = (pfn % bits_per_buf) / CHAR_BIT;
1092 bit = (pfn % bits_per_buf) % CHAR_BIT;
qiaonuohand0686c72014-02-18 14:11:33 +08001093 if (value) {
1094 buf[byte] |= 1u << bit;
1095 } else {
1096 buf[byte] &= ~(1u << bit);
1097 }
1098
1099 return 0;
1100}
1101
Andrew Jones8161bef2016-01-11 20:56:20 +01001102static uint64_t dump_paddr_to_pfn(DumpState *s, uint64_t addr)
1103{
1104 int target_page_shift = ctz32(s->dump_info.page_size);
1105
1106 return (addr >> target_page_shift) - ARCH_PFN_OFFSET;
1107}
1108
1109static uint64_t dump_pfn_to_paddr(DumpState *s, uint64_t pfn)
1110{
1111 int target_page_shift = ctz32(s->dump_info.page_size);
1112
1113 return (pfn + ARCH_PFN_OFFSET) << target_page_shift;
1114}
1115
qiaonuohand0686c72014-02-18 14:11:33 +08001116/*
1117 * exam every page and return the page frame number and the address of the page.
1118 * bufptr can be NULL. note: the blocks here is supposed to reflect guest-phys
1119 * blocks, so block->target_start and block->target_end should be interal
1120 * multiples of the target page size.
1121 */
1122static bool get_next_page(GuestPhysBlock **blockptr, uint64_t *pfnptr,
1123 uint8_t **bufptr, DumpState *s)
1124{
1125 GuestPhysBlock *block = *blockptr;
Andrew Jones8161bef2016-01-11 20:56:20 +01001126 hwaddr addr, target_page_mask = ~((hwaddr)s->dump_info.page_size - 1);
qiaonuohand0686c72014-02-18 14:11:33 +08001127 uint8_t *buf;
1128
1129 /* block == NULL means the start of the iteration */
1130 if (!block) {
1131 block = QTAILQ_FIRST(&s->guest_phys_blocks.head);
1132 *blockptr = block;
Andrew Jones8161bef2016-01-11 20:56:20 +01001133 assert((block->target_start & ~target_page_mask) == 0);
1134 assert((block->target_end & ~target_page_mask) == 0);
1135 *pfnptr = dump_paddr_to_pfn(s, block->target_start);
qiaonuohand0686c72014-02-18 14:11:33 +08001136 if (bufptr) {
1137 *bufptr = block->host_addr;
1138 }
1139 return true;
1140 }
1141
1142 *pfnptr = *pfnptr + 1;
Andrew Jones8161bef2016-01-11 20:56:20 +01001143 addr = dump_pfn_to_paddr(s, *pfnptr);
qiaonuohand0686c72014-02-18 14:11:33 +08001144
1145 if ((addr >= block->target_start) &&
Andrew Jones8161bef2016-01-11 20:56:20 +01001146 (addr + s->dump_info.page_size <= block->target_end)) {
qiaonuohand0686c72014-02-18 14:11:33 +08001147 buf = block->host_addr + (addr - block->target_start);
1148 } else {
1149 /* the next page is in the next block */
1150 block = QTAILQ_NEXT(block, next);
1151 *blockptr = block;
1152 if (!block) {
1153 return false;
1154 }
Andrew Jones8161bef2016-01-11 20:56:20 +01001155 assert((block->target_start & ~target_page_mask) == 0);
1156 assert((block->target_end & ~target_page_mask) == 0);
1157 *pfnptr = dump_paddr_to_pfn(s, block->target_start);
qiaonuohand0686c72014-02-18 14:11:33 +08001158 buf = block->host_addr;
1159 }
1160
1161 if (bufptr) {
1162 *bufptr = buf;
1163 }
1164
1165 return true;
1166}
1167
zhanghailiang4c7e2512014-10-09 14:13:11 +08001168static void write_dump_bitmap(DumpState *s, Error **errp)
qiaonuohand0686c72014-02-18 14:11:33 +08001169{
1170 int ret = 0;
1171 uint64_t last_pfn, pfn;
1172 void *dump_bitmap_buf;
1173 size_t num_dumpable;
1174 GuestPhysBlock *block_iter = NULL;
Andrew Jones8161bef2016-01-11 20:56:20 +01001175 size_t bitmap_bufsize = dump_bitmap_get_bufsize(s);
1176 size_t bits_per_buf = bitmap_bufsize * CHAR_BIT;
qiaonuohand0686c72014-02-18 14:11:33 +08001177
1178 /* dump_bitmap_buf is used to store dump_bitmap temporarily */
Andrew Jones8161bef2016-01-11 20:56:20 +01001179 dump_bitmap_buf = g_malloc0(bitmap_bufsize);
qiaonuohand0686c72014-02-18 14:11:33 +08001180
1181 num_dumpable = 0;
1182 last_pfn = 0;
1183
1184 /*
1185 * exam memory page by page, and set the bit in dump_bitmap corresponded
1186 * to the existing page.
1187 */
1188 while (get_next_page(&block_iter, &pfn, NULL, s)) {
1189 ret = set_dump_bitmap(last_pfn, pfn, true, dump_bitmap_buf, s);
1190 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001191 error_setg(errp, "dump: failed to set dump_bitmap");
qiaonuohand0686c72014-02-18 14:11:33 +08001192 goto out;
1193 }
1194
1195 last_pfn = pfn;
1196 num_dumpable++;
1197 }
1198
1199 /*
1200 * set_dump_bitmap will always leave the recently set bit un-sync. Here we
Andrew Jones8161bef2016-01-11 20:56:20 +01001201 * set the remaining bits from last_pfn to the end of the bitmap buffer to
1202 * 0. With those set, the un-sync bit will be synchronized into the vmcore.
qiaonuohand0686c72014-02-18 14:11:33 +08001203 */
1204 if (num_dumpable > 0) {
Andrew Jones8161bef2016-01-11 20:56:20 +01001205 ret = set_dump_bitmap(last_pfn, last_pfn + bits_per_buf, false,
qiaonuohand0686c72014-02-18 14:11:33 +08001206 dump_bitmap_buf, s);
1207 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001208 error_setg(errp, "dump: failed to sync dump_bitmap");
qiaonuohand0686c72014-02-18 14:11:33 +08001209 goto out;
1210 }
1211 }
1212
1213 /* number of dumpable pages that will be dumped later */
1214 s->num_dumpable = num_dumpable;
1215
1216out:
1217 g_free(dump_bitmap_buf);
qiaonuohand0686c72014-02-18 14:11:33 +08001218}
1219
qiaonuohan64cfba62014-02-18 14:11:34 +08001220static void prepare_data_cache(DataCache *data_cache, DumpState *s,
1221 off_t offset)
1222{
1223 data_cache->fd = s->fd;
1224 data_cache->data_size = 0;
Andrew Jones8161bef2016-01-11 20:56:20 +01001225 data_cache->buf_size = 4 * dump_bitmap_get_bufsize(s);
1226 data_cache->buf = g_malloc0(data_cache->buf_size);
qiaonuohan64cfba62014-02-18 14:11:34 +08001227 data_cache->offset = offset;
1228}
1229
1230static int write_cache(DataCache *dc, const void *buf, size_t size,
1231 bool flag_sync)
1232{
1233 /*
1234 * dc->buf_size should not be less than size, otherwise dc will never be
1235 * enough
1236 */
1237 assert(size <= dc->buf_size);
1238
1239 /*
1240 * if flag_sync is set, synchronize data in dc->buf into vmcore.
1241 * otherwise check if the space is enough for caching data in buf, if not,
1242 * write the data in dc->buf to dc->fd and reset dc->buf
1243 */
1244 if ((!flag_sync && dc->data_size + size > dc->buf_size) ||
1245 (flag_sync && dc->data_size > 0)) {
1246 if (write_buffer(dc->fd, dc->offset, dc->buf, dc->data_size) < 0) {
1247 return -1;
1248 }
1249
1250 dc->offset += dc->data_size;
1251 dc->data_size = 0;
1252 }
1253
1254 if (!flag_sync) {
1255 memcpy(dc->buf + dc->data_size, buf, size);
1256 dc->data_size += size;
1257 }
1258
1259 return 0;
1260}
1261
1262static void free_data_cache(DataCache *data_cache)
1263{
1264 g_free(data_cache->buf);
1265}
1266
qiaonuohand12f57e2014-02-18 14:11:35 +08001267static size_t get_len_buf_out(size_t page_size, uint32_t flag_compress)
1268{
Laszlo Ersekb87ef352014-05-20 13:42:53 +02001269 switch (flag_compress) {
1270 case DUMP_DH_COMPRESSED_ZLIB:
1271 return compressBound(page_size);
qiaonuohand12f57e2014-02-18 14:11:35 +08001272
Laszlo Ersekb87ef352014-05-20 13:42:53 +02001273 case DUMP_DH_COMPRESSED_LZO:
1274 /*
1275 * LZO will expand incompressible data by a little amount. Please check
1276 * the following URL to see the expansion calculation:
1277 * http://www.oberhumer.com/opensource/lzo/lzofaq.php
1278 */
1279 return page_size + page_size / 16 + 64 + 3;
qiaonuohand12f57e2014-02-18 14:11:35 +08001280
1281#ifdef CONFIG_SNAPPY
Laszlo Ersekb87ef352014-05-20 13:42:53 +02001282 case DUMP_DH_COMPRESSED_SNAPPY:
1283 return snappy_max_compressed_length(page_size);
qiaonuohand12f57e2014-02-18 14:11:35 +08001284#endif
Laszlo Ersekb87ef352014-05-20 13:42:53 +02001285 }
1286 return 0;
qiaonuohand12f57e2014-02-18 14:11:35 +08001287}
1288
1289/*
1290 * check if the page is all 0
1291 */
1292static inline bool is_zero_page(const uint8_t *buf, size_t page_size)
1293{
1294 return buffer_is_zero(buf, page_size);
1295}
1296
zhanghailiang4c7e2512014-10-09 14:13:11 +08001297static void write_dump_pages(DumpState *s, Error **errp)
qiaonuohand12f57e2014-02-18 14:11:35 +08001298{
1299 int ret = 0;
1300 DataCache page_desc, page_data;
1301 size_t len_buf_out, size_out;
1302#ifdef CONFIG_LZO
1303 lzo_bytep wrkmem = NULL;
1304#endif
1305 uint8_t *buf_out = NULL;
1306 off_t offset_desc, offset_data;
1307 PageDescriptor pd, pd_zero;
1308 uint8_t *buf;
qiaonuohand12f57e2014-02-18 14:11:35 +08001309 GuestPhysBlock *block_iter = NULL;
1310 uint64_t pfn_iter;
1311
1312 /* get offset of page_desc and page_data in dump file */
1313 offset_desc = s->offset_page;
1314 offset_data = offset_desc + sizeof(PageDescriptor) * s->num_dumpable;
1315
1316 prepare_data_cache(&page_desc, s, offset_desc);
1317 prepare_data_cache(&page_data, s, offset_data);
1318
1319 /* prepare buffer to store compressed data */
Andrew Jones8161bef2016-01-11 20:56:20 +01001320 len_buf_out = get_len_buf_out(s->dump_info.page_size, s->flag_compress);
Laszlo Ersekb87ef352014-05-20 13:42:53 +02001321 assert(len_buf_out != 0);
qiaonuohand12f57e2014-02-18 14:11:35 +08001322
1323#ifdef CONFIG_LZO
1324 wrkmem = g_malloc(LZO1X_1_MEM_COMPRESS);
1325#endif
1326
1327 buf_out = g_malloc(len_buf_out);
1328
1329 /*
1330 * init zero page's page_desc and page_data, because every zero page
1331 * uses the same page_data
1332 */
Andrew Jones8161bef2016-01-11 20:56:20 +01001333 pd_zero.size = cpu_to_dump32(s, s->dump_info.page_size);
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001334 pd_zero.flags = cpu_to_dump32(s, 0);
1335 pd_zero.offset = cpu_to_dump64(s, offset_data);
1336 pd_zero.page_flags = cpu_to_dump64(s, 0);
Andrew Jones8161bef2016-01-11 20:56:20 +01001337 buf = g_malloc0(s->dump_info.page_size);
1338 ret = write_cache(&page_data, buf, s->dump_info.page_size, false);
qiaonuohand12f57e2014-02-18 14:11:35 +08001339 g_free(buf);
1340 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001341 error_setg(errp, "dump: failed to write page data (zero page)");
qiaonuohand12f57e2014-02-18 14:11:35 +08001342 goto out;
1343 }
1344
Andrew Jones8161bef2016-01-11 20:56:20 +01001345 offset_data += s->dump_info.page_size;
qiaonuohand12f57e2014-02-18 14:11:35 +08001346
1347 /*
1348 * dump memory to vmcore page by page. zero page will all be resided in the
1349 * first page of page section
1350 */
1351 while (get_next_page(&block_iter, &pfn_iter, &buf, s)) {
1352 /* check zero page */
Andrew Jones8161bef2016-01-11 20:56:20 +01001353 if (is_zero_page(buf, s->dump_info.page_size)) {
qiaonuohand12f57e2014-02-18 14:11:35 +08001354 ret = write_cache(&page_desc, &pd_zero, sizeof(PageDescriptor),
1355 false);
1356 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001357 error_setg(errp, "dump: failed to write page desc");
qiaonuohand12f57e2014-02-18 14:11:35 +08001358 goto out;
1359 }
1360 } else {
1361 /*
1362 * not zero page, then:
1363 * 1. compress the page
1364 * 2. write the compressed page into the cache of page_data
1365 * 3. get page desc of the compressed page and write it into the
1366 * cache of page_desc
1367 *
1368 * only one compression format will be used here, for
1369 * s->flag_compress is set. But when compression fails to work,
1370 * we fall back to save in plaintext.
1371 */
1372 size_out = len_buf_out;
1373 if ((s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) &&
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001374 (compress2(buf_out, (uLongf *)&size_out, buf,
Andrew Jones8161bef2016-01-11 20:56:20 +01001375 s->dump_info.page_size, Z_BEST_SPEED) == Z_OK) &&
1376 (size_out < s->dump_info.page_size)) {
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001377 pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_ZLIB);
1378 pd.size = cpu_to_dump32(s, size_out);
qiaonuohand12f57e2014-02-18 14:11:35 +08001379
1380 ret = write_cache(&page_data, buf_out, size_out, false);
1381 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001382 error_setg(errp, "dump: failed to write page data");
qiaonuohand12f57e2014-02-18 14:11:35 +08001383 goto out;
1384 }
1385#ifdef CONFIG_LZO
1386 } else if ((s->flag_compress & DUMP_DH_COMPRESSED_LZO) &&
Andrew Jones8161bef2016-01-11 20:56:20 +01001387 (lzo1x_1_compress(buf, s->dump_info.page_size, buf_out,
qiaonuohand12f57e2014-02-18 14:11:35 +08001388 (lzo_uint *)&size_out, wrkmem) == LZO_E_OK) &&
Andrew Jones8161bef2016-01-11 20:56:20 +01001389 (size_out < s->dump_info.page_size)) {
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001390 pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_LZO);
1391 pd.size = cpu_to_dump32(s, size_out);
qiaonuohand12f57e2014-02-18 14:11:35 +08001392
1393 ret = write_cache(&page_data, buf_out, size_out, false);
1394 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001395 error_setg(errp, "dump: failed to write page data");
qiaonuohand12f57e2014-02-18 14:11:35 +08001396 goto out;
1397 }
1398#endif
1399#ifdef CONFIG_SNAPPY
1400 } else if ((s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) &&
Andrew Jones8161bef2016-01-11 20:56:20 +01001401 (snappy_compress((char *)buf, s->dump_info.page_size,
qiaonuohand12f57e2014-02-18 14:11:35 +08001402 (char *)buf_out, &size_out) == SNAPPY_OK) &&
Andrew Jones8161bef2016-01-11 20:56:20 +01001403 (size_out < s->dump_info.page_size)) {
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001404 pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_SNAPPY);
1405 pd.size = cpu_to_dump32(s, size_out);
qiaonuohand12f57e2014-02-18 14:11:35 +08001406
1407 ret = write_cache(&page_data, buf_out, size_out, false);
1408 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001409 error_setg(errp, "dump: failed to write page data");
qiaonuohand12f57e2014-02-18 14:11:35 +08001410 goto out;
1411 }
1412#endif
1413 } else {
1414 /*
1415 * fall back to save in plaintext, size_out should be
Andrew Jones8161bef2016-01-11 20:56:20 +01001416 * assigned the target's page size
qiaonuohand12f57e2014-02-18 14:11:35 +08001417 */
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001418 pd.flags = cpu_to_dump32(s, 0);
Andrew Jones8161bef2016-01-11 20:56:20 +01001419 size_out = s->dump_info.page_size;
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001420 pd.size = cpu_to_dump32(s, size_out);
qiaonuohand12f57e2014-02-18 14:11:35 +08001421
Andrew Jones8161bef2016-01-11 20:56:20 +01001422 ret = write_cache(&page_data, buf,
1423 s->dump_info.page_size, false);
qiaonuohand12f57e2014-02-18 14:11:35 +08001424 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001425 error_setg(errp, "dump: failed to write page data");
qiaonuohand12f57e2014-02-18 14:11:35 +08001426 goto out;
1427 }
1428 }
1429
1430 /* get and write page desc here */
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001431 pd.page_flags = cpu_to_dump64(s, 0);
1432 pd.offset = cpu_to_dump64(s, offset_data);
qiaonuohand12f57e2014-02-18 14:11:35 +08001433 offset_data += size_out;
1434
1435 ret = write_cache(&page_desc, &pd, sizeof(PageDescriptor), false);
1436 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001437 error_setg(errp, "dump: failed to write page desc");
qiaonuohand12f57e2014-02-18 14:11:35 +08001438 goto out;
1439 }
1440 }
Peter Xu2264c2c2016-02-18 13:16:53 +08001441 s->written_size += s->dump_info.page_size;
qiaonuohand12f57e2014-02-18 14:11:35 +08001442 }
1443
1444 ret = write_cache(&page_desc, NULL, 0, true);
1445 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001446 error_setg(errp, "dump: failed to sync cache for page_desc");
qiaonuohand12f57e2014-02-18 14:11:35 +08001447 goto out;
1448 }
1449 ret = write_cache(&page_data, 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_data");
qiaonuohand12f57e2014-02-18 14:11:35 +08001452 goto out;
1453 }
1454
1455out:
1456 free_data_cache(&page_desc);
1457 free_data_cache(&page_data);
1458
1459#ifdef CONFIG_LZO
1460 g_free(wrkmem);
1461#endif
1462
1463 g_free(buf_out);
qiaonuohand12f57e2014-02-18 14:11:35 +08001464}
1465
zhanghailiang4c7e2512014-10-09 14:13:11 +08001466static void create_kdump_vmcore(DumpState *s, Error **errp)
qiaonuohanb53ccc32014-02-18 14:11:36 +08001467{
1468 int ret;
zhanghailiang4c7e2512014-10-09 14:13:11 +08001469 Error *local_err = NULL;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001470
1471 /*
1472 * the kdump-compressed format is:
1473 * File offset
1474 * +------------------------------------------+ 0x0
1475 * | main header (struct disk_dump_header) |
1476 * |------------------------------------------+ block 1
1477 * | sub header (struct kdump_sub_header) |
1478 * |------------------------------------------+ block 2
1479 * | 1st-dump_bitmap |
1480 * |------------------------------------------+ block 2 + X blocks
1481 * | 2nd-dump_bitmap | (aligned by block)
1482 * |------------------------------------------+ block 2 + 2 * X blocks
1483 * | page desc for pfn 0 (struct page_desc) | (aligned by block)
1484 * | page desc for pfn 1 (struct page_desc) |
1485 * | : |
1486 * |------------------------------------------| (not aligned by block)
1487 * | page data (pfn 0) |
1488 * | page data (pfn 1) |
1489 * | : |
1490 * +------------------------------------------+
1491 */
1492
1493 ret = write_start_flat_header(s->fd);
1494 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001495 error_setg(errp, "dump: failed to write start flat header");
zhanghailiang4c7e2512014-10-09 14:13:11 +08001496 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001497 }
1498
zhanghailiang4c7e2512014-10-09 14:13:11 +08001499 write_dump_header(s, &local_err);
1500 if (local_err) {
1501 error_propagate(errp, local_err);
1502 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001503 }
1504
zhanghailiang4c7e2512014-10-09 14:13:11 +08001505 write_dump_bitmap(s, &local_err);
1506 if (local_err) {
1507 error_propagate(errp, local_err);
1508 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001509 }
1510
zhanghailiang4c7e2512014-10-09 14:13:11 +08001511 write_dump_pages(s, &local_err);
1512 if (local_err) {
1513 error_propagate(errp, local_err);
1514 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001515 }
1516
1517 ret = write_end_flat_header(s->fd);
1518 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001519 error_setg(errp, "dump: failed to write end flat header");
zhanghailiang4c7e2512014-10-09 14:13:11 +08001520 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001521 }
qiaonuohanb53ccc32014-02-18 14:11:36 +08001522}
1523
Wen Congyang783e9b42012-05-07 12:10:47 +08001524static ram_addr_t get_start_block(DumpState *s)
1525{
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001526 GuestPhysBlock *block;
Wen Congyang783e9b42012-05-07 12:10:47 +08001527
1528 if (!s->has_filter) {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001529 s->next_block = QTAILQ_FIRST(&s->guest_phys_blocks.head);
Wen Congyang783e9b42012-05-07 12:10:47 +08001530 return 0;
1531 }
1532
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001533 QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
1534 if (block->target_start >= s->begin + s->length ||
1535 block->target_end <= s->begin) {
Wen Congyang783e9b42012-05-07 12:10:47 +08001536 /* This block is out of the range */
1537 continue;
1538 }
1539
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001540 s->next_block = block;
1541 if (s->begin > block->target_start) {
1542 s->start = s->begin - block->target_start;
Wen Congyang783e9b42012-05-07 12:10:47 +08001543 } else {
1544 s->start = 0;
1545 }
1546 return s->start;
1547 }
1548
1549 return -1;
1550}
1551
qiaonuohan7aad2482014-02-18 14:11:31 +08001552static void get_max_mapnr(DumpState *s)
1553{
1554 GuestPhysBlock *last_block;
1555
1556 last_block = QTAILQ_LAST(&s->guest_phys_blocks.head, GuestPhysBlockHead);
Andrew Jones8161bef2016-01-11 20:56:20 +01001557 s->max_mapnr = dump_paddr_to_pfn(s, last_block->target_end);
qiaonuohan7aad2482014-02-18 14:11:31 +08001558}
1559
Peter Xubaf28f52016-02-18 13:16:48 +08001560static DumpState dump_state_global = { .status = DUMP_STATUS_NONE };
1561
1562static void dump_state_prepare(DumpState *s)
1563{
1564 /* zero the struct, setting status to active */
1565 *s = (DumpState) { .status = DUMP_STATUS_ACTIVE };
1566}
1567
Peter Xu65d64f32016-02-18 13:16:49 +08001568bool dump_in_progress(void)
1569{
1570 DumpState *state = &dump_state_global;
Peter Xu39ba2ea2016-02-18 13:16:54 +08001571 return (atomic_read(&state->status) == DUMP_STATUS_ACTIVE);
Peter Xu65d64f32016-02-18 13:16:49 +08001572}
1573
Peter Xu2264c2c2016-02-18 13:16:53 +08001574/* calculate total size of memory to be dumped (taking filter into
1575 * acoount.) */
1576static int64_t dump_calculate_size(DumpState *s)
1577{
1578 GuestPhysBlock *block;
1579 int64_t size = 0, total = 0, left = 0, right = 0;
1580
1581 QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
1582 if (s->has_filter) {
1583 /* calculate the overlapped region. */
1584 left = MAX(s->begin, block->target_start);
1585 right = MIN(s->begin + s->length, block->target_end);
1586 size = right - left;
1587 size = size > 0 ? size : 0;
1588 } else {
1589 /* count the whole region in */
1590 size = (block->target_end - block->target_start);
1591 }
1592 total += size;
1593 }
1594
1595 return total;
1596}
1597
Marc-André Lureaud9feb512017-09-11 18:59:26 +02001598static void vmcoreinfo_update_phys_base(DumpState *s)
1599{
1600 uint64_t size, note_head_size, name_size, phys_base;
1601 char **lines;
1602 uint8_t *vmci;
1603 size_t i;
1604
1605 if (!note_name_equal(s, s->guest_note, "VMCOREINFO")) {
1606 return;
1607 }
1608
1609 get_note_sizes(s, s->guest_note, &note_head_size, &name_size, &size);
1610 note_head_size = ROUND_UP(note_head_size, 4);
1611
1612 vmci = s->guest_note + note_head_size + ROUND_UP(name_size, 4);
1613 *(vmci + size) = '\0';
1614
1615 lines = g_strsplit((char *)vmci, "\n", -1);
1616 for (i = 0; lines[i]; i++) {
Wei Huang68cbecf2018-03-09 12:03:23 -05001617 const char *prefix = NULL;
1618
1619 if (s->dump_info.d_machine == EM_X86_64) {
1620 prefix = "NUMBER(phys_base)=";
1621 } else if (s->dump_info.d_machine == EM_AARCH64) {
1622 prefix = "NUMBER(PHYS_OFFSET)=";
1623 }
1624
1625 if (prefix && g_str_has_prefix(lines[i], prefix)) {
1626 if (qemu_strtou64(lines[i] + strlen(prefix), NULL, 16,
Marc-André Lureaud9feb512017-09-11 18:59:26 +02001627 &phys_base) < 0) {
Wei Huang68cbecf2018-03-09 12:03:23 -05001628 warn_report("Failed to read %s", prefix);
Marc-André Lureaud9feb512017-09-11 18:59:26 +02001629 } else {
1630 s->dump_info.phys_base = phys_base;
1631 }
1632 break;
1633 }
1634 }
1635
1636 g_strfreev(lines);
1637}
1638
zhanghailiang4c7e2512014-10-09 14:13:11 +08001639static void dump_init(DumpState *s, int fd, bool has_format,
1640 DumpGuestMemoryFormat format, bool paging, bool has_filter,
1641 int64_t begin, int64_t length, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +08001642{
Marc-André Lureau903ef732017-09-11 18:59:25 +02001643 VMCoreInfoState *vmci = vmcoreinfo_find();
Andreas Färber182735ef2013-05-29 22:29:20 +02001644 CPUState *cpu;
Wen Congyang783e9b42012-05-07 12:10:47 +08001645 int nr_cpus;
Andreas Färber11ed09c2013-05-29 21:54:03 +02001646 Error *err = NULL;
Wen Congyang783e9b42012-05-07 12:10:47 +08001647 int ret;
1648
Peter Xuca1fc8c2016-02-18 13:16:50 +08001649 s->has_format = has_format;
1650 s->format = format;
Peter Xu2264c2c2016-02-18 13:16:53 +08001651 s->written_size = 0;
Peter Xuca1fc8c2016-02-18 13:16:50 +08001652
qiaonuohanb53ccc32014-02-18 14:11:36 +08001653 /* kdump-compressed is conflict with paging and filter */
1654 if (has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
1655 assert(!paging && !has_filter);
1656 }
1657
Wen Congyang783e9b42012-05-07 12:10:47 +08001658 if (runstate_is_running()) {
1659 vm_stop(RUN_STATE_SAVE_VM);
1660 s->resume = true;
1661 } else {
1662 s->resume = false;
1663 }
1664
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001665 /* If we use KVM, we should synchronize the registers before we get dump
1666 * info or physmap info.
Wen Congyang783e9b42012-05-07 12:10:47 +08001667 */
Andreas Färber1b3509c2013-06-09 16:48:29 +02001668 cpu_synchronize_all_states();
Wen Congyang783e9b42012-05-07 12:10:47 +08001669 nr_cpus = 0;
Andreas Färberbdc44642013-06-24 23:50:24 +02001670 CPU_FOREACH(cpu) {
Wen Congyang783e9b42012-05-07 12:10:47 +08001671 nr_cpus++;
1672 }
1673
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001674 s->fd = fd;
1675 s->has_filter = has_filter;
1676 s->begin = begin;
1677 s->length = length;
1678
Chen Gang29282072014-08-03 23:28:56 +08001679 memory_mapping_list_init(&s->list);
1680
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001681 guest_phys_blocks_init(&s->guest_phys_blocks);
Laszlo Ersekc5d7f602013-08-06 12:37:10 +02001682 guest_phys_blocks_append(&s->guest_phys_blocks);
Peter Xu2264c2c2016-02-18 13:16:53 +08001683 s->total_size = dump_calculate_size(s);
1684#ifdef DEBUG_DUMP_GUEST_MEMORY
1685 fprintf(stderr, "DUMP: total memory to dump: %lu\n", s->total_size);
1686#endif
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001687
Cornelia Huckd1e69942017-09-13 16:20:35 +02001688 /* it does not make sense to dump non-existent memory */
1689 if (!s->total_size) {
1690 error_setg(errp, "dump: no guest memory to dump");
1691 goto cleanup;
1692 }
1693
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001694 s->start = get_start_block(s);
1695 if (s->start == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001696 error_setg(errp, QERR_INVALID_PARAMETER, "begin");
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001697 goto cleanup;
1698 }
1699
1700 /* get dump info: endian, class and architecture.
1701 * If the target architecture is not supported, cpu_get_dump_info() will
1702 * return -1.
1703 */
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001704 ret = cpu_get_dump_info(&s->dump_info, &s->guest_phys_blocks);
Wen Congyang783e9b42012-05-07 12:10:47 +08001705 if (ret < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001706 error_setg(errp, QERR_UNSUPPORTED);
Wen Congyang783e9b42012-05-07 12:10:47 +08001707 goto cleanup;
1708 }
1709
Andrew Jones8161bef2016-01-11 20:56:20 +01001710 if (!s->dump_info.page_size) {
1711 s->dump_info.page_size = TARGET_PAGE_SIZE;
1712 }
1713
Paolo Bonzini4720bd02012-06-07 08:48:09 +02001714 s->note_size = cpu_get_note_size(s->dump_info.d_class,
1715 s->dump_info.d_machine, nr_cpus);
Aneesh Kumar K.Vbb6b6842013-10-01 21:49:32 +05301716 if (s->note_size < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001717 error_setg(errp, QERR_UNSUPPORTED);
Paolo Bonzini4720bd02012-06-07 08:48:09 +02001718 goto cleanup;
1719 }
1720
Marc-André Lureau903ef732017-09-11 18:59:25 +02001721 /*
Marc-André Lureaud9feb512017-09-11 18:59:26 +02001722 * The goal of this block is to (a) update the previously guessed
1723 * phys_base, (b) copy the guest note out of the guest.
1724 * Failure to do so is not fatal for dumping.
Marc-André Lureau903ef732017-09-11 18:59:25 +02001725 */
1726 if (vmci) {
1727 uint64_t addr, note_head_size, name_size, desc_size;
1728 uint32_t size;
1729 uint16_t format;
1730
1731 note_head_size = s->dump_info.d_class == ELFCLASS32 ?
1732 sizeof(Elf32_Nhdr) : sizeof(Elf64_Nhdr);
1733
1734 format = le16_to_cpu(vmci->vmcoreinfo.guest_format);
1735 size = le32_to_cpu(vmci->vmcoreinfo.size);
1736 addr = le64_to_cpu(vmci->vmcoreinfo.paddr);
1737 if (!vmci->has_vmcoreinfo) {
1738 warn_report("guest note is not present");
1739 } else if (size < note_head_size || size > MAX_GUEST_NOTE_SIZE) {
1740 warn_report("guest note size is invalid: %" PRIu32, size);
1741 } else if (format != VMCOREINFO_FORMAT_ELF) {
1742 warn_report("guest note format is unsupported: %" PRIu16, format);
1743 } else {
1744 s->guest_note = g_malloc(size + 1); /* +1 for adding \0 */
1745 cpu_physical_memory_read(addr, s->guest_note, size);
1746
1747 get_note_sizes(s, s->guest_note, NULL, &name_size, &desc_size);
1748 s->guest_note_size = ELF_NOTE_SIZE(note_head_size, name_size,
1749 desc_size);
1750 if (name_size > MAX_GUEST_NOTE_SIZE ||
1751 desc_size > MAX_GUEST_NOTE_SIZE ||
1752 s->guest_note_size > size) {
1753 warn_report("Invalid guest note header");
1754 g_free(s->guest_note);
1755 s->guest_note = NULL;
1756 } else {
Marc-André Lureaud9feb512017-09-11 18:59:26 +02001757 vmcoreinfo_update_phys_base(s);
Marc-André Lureau903ef732017-09-11 18:59:25 +02001758 s->note_size += s->guest_note_size;
1759 }
1760 }
1761 }
1762
Wen Congyang783e9b42012-05-07 12:10:47 +08001763 /* get memory mapping */
Wen Congyang783e9b42012-05-07 12:10:47 +08001764 if (paging) {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001765 qemu_get_guest_memory_mapping(&s->list, &s->guest_phys_blocks, &err);
Andreas Färber11ed09c2013-05-29 21:54:03 +02001766 if (err != NULL) {
1767 error_propagate(errp, err);
1768 goto cleanup;
1769 }
Wen Congyang783e9b42012-05-07 12:10:47 +08001770 } else {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001771 qemu_get_guest_simple_memory_mapping(&s->list, &s->guest_phys_blocks);
Wen Congyang783e9b42012-05-07 12:10:47 +08001772 }
1773
qiaonuohan7aad2482014-02-18 14:11:31 +08001774 s->nr_cpus = nr_cpus;
qiaonuohan7aad2482014-02-18 14:11:31 +08001775
1776 get_max_mapnr(s);
1777
1778 uint64_t tmp;
Andrew Jones8161bef2016-01-11 20:56:20 +01001779 tmp = DIV_ROUND_UP(DIV_ROUND_UP(s->max_mapnr, CHAR_BIT),
1780 s->dump_info.page_size);
1781 s->len_dump_bitmap = tmp * s->dump_info.page_size;
qiaonuohan7aad2482014-02-18 14:11:31 +08001782
qiaonuohanb53ccc32014-02-18 14:11:36 +08001783 /* init for kdump-compressed format */
1784 if (has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
1785 switch (format) {
1786 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB:
1787 s->flag_compress = DUMP_DH_COMPRESSED_ZLIB;
1788 break;
1789
1790 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO:
Laszlo Ersekc998acb2014-05-20 13:42:22 +02001791#ifdef CONFIG_LZO
1792 if (lzo_init() != LZO_E_OK) {
1793 error_setg(errp, "failed to initialize the LZO library");
1794 goto cleanup;
1795 }
1796#endif
qiaonuohanb53ccc32014-02-18 14:11:36 +08001797 s->flag_compress = DUMP_DH_COMPRESSED_LZO;
1798 break;
1799
1800 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY:
1801 s->flag_compress = DUMP_DH_COMPRESSED_SNAPPY;
1802 break;
1803
1804 default:
1805 s->flag_compress = 0;
1806 }
1807
zhanghailiang4c7e2512014-10-09 14:13:11 +08001808 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001809 }
1810
Wen Congyang783e9b42012-05-07 12:10:47 +08001811 if (s->has_filter) {
1812 memory_mapping_filter(&s->list, s->begin, s->length);
1813 }
1814
1815 /*
1816 * calculate phdr_num
1817 *
1818 * the type of ehdr->e_phnum is uint16_t, so we should avoid overflow
1819 */
1820 s->phdr_num = 1; /* PT_NOTE */
1821 if (s->list.num < UINT16_MAX - 2) {
1822 s->phdr_num += s->list.num;
1823 s->have_section = false;
1824 } else {
1825 s->have_section = true;
1826 s->phdr_num = PN_XNUM;
1827 s->sh_info = 1; /* PT_NOTE */
1828
1829 /* the type of shdr->sh_info is uint32_t, so we should avoid overflow */
1830 if (s->list.num <= UINT32_MAX - 1) {
1831 s->sh_info += s->list.num;
1832 } else {
1833 s->sh_info = UINT32_MAX;
1834 }
1835 }
1836
Wen Congyang783e9b42012-05-07 12:10:47 +08001837 if (s->dump_info.d_class == ELFCLASS64) {
1838 if (s->have_section) {
1839 s->memory_offset = sizeof(Elf64_Ehdr) +
1840 sizeof(Elf64_Phdr) * s->sh_info +
1841 sizeof(Elf64_Shdr) + s->note_size;
1842 } else {
1843 s->memory_offset = sizeof(Elf64_Ehdr) +
1844 sizeof(Elf64_Phdr) * s->phdr_num + s->note_size;
1845 }
1846 } else {
1847 if (s->have_section) {
1848 s->memory_offset = sizeof(Elf32_Ehdr) +
1849 sizeof(Elf32_Phdr) * s->sh_info +
1850 sizeof(Elf32_Shdr) + s->note_size;
1851 } else {
1852 s->memory_offset = sizeof(Elf32_Ehdr) +
1853 sizeof(Elf32_Phdr) * s->phdr_num + s->note_size;
1854 }
1855 }
1856
zhanghailiang4c7e2512014-10-09 14:13:11 +08001857 return;
Wen Congyang783e9b42012-05-07 12:10:47 +08001858
1859cleanup:
Chen Gang29282072014-08-03 23:28:56 +08001860 dump_cleanup(s);
Wen Congyang783e9b42012-05-07 12:10:47 +08001861}
1862
Peter Xuca1fc8c2016-02-18 13:16:50 +08001863/* this operation might be time consuming. */
1864static void dump_process(DumpState *s, Error **errp)
1865{
1866 Error *local_err = NULL;
Peter Xud42a0d12016-02-18 13:16:56 +08001867 DumpQueryResult *result = NULL;
Peter Xuca1fc8c2016-02-18 13:16:50 +08001868
1869 if (s->has_format && s->format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
1870 create_kdump_vmcore(s, &local_err);
1871 } else {
1872 create_vmcore(s, &local_err);
1873 }
1874
Peter Xu39ba2ea2016-02-18 13:16:54 +08001875 /* make sure status is written after written_size updates */
1876 smp_wmb();
1877 atomic_set(&s->status,
1878 (local_err ? DUMP_STATUS_FAILED : DUMP_STATUS_COMPLETED));
Peter Xuca1fc8c2016-02-18 13:16:50 +08001879
Peter Xud42a0d12016-02-18 13:16:56 +08001880 /* send DUMP_COMPLETED message (unconditionally) */
1881 result = qmp_query_dump(NULL);
1882 /* should never fail */
1883 assert(result);
1884 qapi_event_send_dump_completed(result, !!local_err, (local_err ? \
1885 error_get_pretty(local_err) : NULL),
1886 &error_abort);
1887 qapi_free_DumpQueryResult(result);
1888
Peter Xu39ba2ea2016-02-18 13:16:54 +08001889 error_propagate(errp, local_err);
Peter Xuca1fc8c2016-02-18 13:16:50 +08001890 dump_cleanup(s);
1891}
1892
Peter Xu1fbeff72016-02-18 13:16:52 +08001893static void *dump_thread(void *data)
1894{
Peter Xu1fbeff72016-02-18 13:16:52 +08001895 DumpState *s = (DumpState *)data;
Alberto Garciac3a8fe32017-08-29 15:08:36 +03001896 dump_process(s, NULL);
Peter Xu1fbeff72016-02-18 13:16:52 +08001897 return NULL;
1898}
1899
Peter Xu39ba2ea2016-02-18 13:16:54 +08001900DumpQueryResult *qmp_query_dump(Error **errp)
1901{
1902 DumpQueryResult *result = g_new(DumpQueryResult, 1);
1903 DumpState *state = &dump_state_global;
1904 result->status = atomic_read(&state->status);
1905 /* make sure we are reading status and written_size in order */
1906 smp_rmb();
1907 result->completed = state->written_size;
1908 result->total = state->total_size;
1909 return result;
1910}
1911
Peter Xu228de9c2016-02-18 13:16:47 +08001912void qmp_dump_guest_memory(bool paging, const char *file,
1913 bool has_detach, bool detach,
1914 bool has_begin, int64_t begin, bool has_length,
qiaonuohanb53ccc32014-02-18 14:11:36 +08001915 int64_t length, bool has_format,
1916 DumpGuestMemoryFormat format, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +08001917{
1918 const char *p;
1919 int fd = -1;
1920 DumpState *s;
zhanghailiang4c7e2512014-10-09 14:13:11 +08001921 Error *local_err = NULL;
Peter Xu1fbeff72016-02-18 13:16:52 +08001922 bool detach_p = false;
Wen Congyang783e9b42012-05-07 12:10:47 +08001923
Peter Xu63e27f22016-02-18 13:16:51 +08001924 if (runstate_check(RUN_STATE_INMIGRATE)) {
1925 error_setg(errp, "Dump not allowed during incoming migration.");
1926 return;
1927 }
1928
Peter Xu65d64f32016-02-18 13:16:49 +08001929 /* if there is a dump in background, we should wait until the dump
1930 * finished */
1931 if (dump_in_progress()) {
1932 error_setg(errp, "There is a dump in process, please wait.");
1933 return;
1934 }
1935
qiaonuohanb53ccc32014-02-18 14:11:36 +08001936 /*
1937 * kdump-compressed format need the whole memory dumped, so paging or
1938 * filter is not supported here.
1939 */
1940 if ((has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) &&
1941 (paging || has_begin || has_length)) {
1942 error_setg(errp, "kdump-compressed format doesn't support paging or "
1943 "filter");
1944 return;
1945 }
Wen Congyang783e9b42012-05-07 12:10:47 +08001946 if (has_begin && !has_length) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001947 error_setg(errp, QERR_MISSING_PARAMETER, "length");
Wen Congyang783e9b42012-05-07 12:10:47 +08001948 return;
1949 }
1950 if (!has_begin && has_length) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001951 error_setg(errp, QERR_MISSING_PARAMETER, "begin");
Wen Congyang783e9b42012-05-07 12:10:47 +08001952 return;
1953 }
Peter Xu1fbeff72016-02-18 13:16:52 +08001954 if (has_detach) {
1955 detach_p = detach;
1956 }
Wen Congyang783e9b42012-05-07 12:10:47 +08001957
qiaonuohanb53ccc32014-02-18 14:11:36 +08001958 /* check whether lzo/snappy is supported */
1959#ifndef CONFIG_LZO
1960 if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO) {
1961 error_setg(errp, "kdump-lzo is not available now");
1962 return;
1963 }
1964#endif
1965
1966#ifndef CONFIG_SNAPPY
1967 if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY) {
1968 error_setg(errp, "kdump-snappy is not available now");
1969 return;
1970 }
1971#endif
1972
Wen Congyang783e9b42012-05-07 12:10:47 +08001973#if !defined(WIN32)
1974 if (strstart(file, "fd:", &p)) {
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02001975 fd = monitor_get_fd(cur_mon, p, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +08001976 if (fd == -1) {
Wen Congyang783e9b42012-05-07 12:10:47 +08001977 return;
1978 }
1979 }
1980#endif
1981
1982 if (strstart(file, "file:", &p)) {
1983 fd = qemu_open(p, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, S_IRUSR);
1984 if (fd < 0) {
Luiz Capitulino75817662013-06-07 14:36:01 -04001985 error_setg_file_open(errp, errno, p);
Wen Congyang783e9b42012-05-07 12:10:47 +08001986 return;
1987 }
1988 }
1989
1990 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001991 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
Wen Congyang783e9b42012-05-07 12:10:47 +08001992 return;
1993 }
1994
Peter Xubaf28f52016-02-18 13:16:48 +08001995 s = &dump_state_global;
1996 dump_state_prepare(s);
Wen Congyang783e9b42012-05-07 12:10:47 +08001997
zhanghailiang4c7e2512014-10-09 14:13:11 +08001998 dump_init(s, fd, has_format, format, paging, has_begin,
1999 begin, length, &local_err);
2000 if (local_err) {
zhanghailiang4c7e2512014-10-09 14:13:11 +08002001 error_propagate(errp, local_err);
Peter Xu39ba2ea2016-02-18 13:16:54 +08002002 atomic_set(&s->status, DUMP_STATUS_FAILED);
Wen Congyang783e9b42012-05-07 12:10:47 +08002003 return;
2004 }
2005
Peter Xu1fbeff72016-02-18 13:16:52 +08002006 if (detach_p) {
2007 /* detached dump */
Fam Zheng6796b402017-05-03 15:28:19 +08002008 s->detached = true;
Peter Xu1fbeff72016-02-18 13:16:52 +08002009 qemu_thread_create(&s->dump_thread, "dump_thread", dump_thread,
2010 s, QEMU_THREAD_DETACHED);
2011 } else {
2012 /* sync dump */
2013 dump_process(s, errp);
2014 }
Wen Congyang783e9b42012-05-07 12:10:47 +08002015}
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002016
2017DumpGuestMemoryCapability *qmp_query_dump_guest_memory_capability(Error **errp)
2018{
2019 DumpGuestMemoryFormatList *item;
2020 DumpGuestMemoryCapability *cap =
2021 g_malloc0(sizeof(DumpGuestMemoryCapability));
2022
2023 /* elf is always available */
2024 item = g_malloc0(sizeof(DumpGuestMemoryFormatList));
2025 cap->formats = item;
2026 item->value = DUMP_GUEST_MEMORY_FORMAT_ELF;
2027
2028 /* kdump-zlib is always available */
2029 item->next = g_malloc0(sizeof(DumpGuestMemoryFormatList));
2030 item = item->next;
2031 item->value = DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB;
2032
2033 /* add new item if kdump-lzo is available */
2034#ifdef CONFIG_LZO
2035 item->next = g_malloc0(sizeof(DumpGuestMemoryFormatList));
2036 item = item->next;
2037 item->value = DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO;
2038#endif
2039
2040 /* add new item if kdump-snappy is available */
2041#ifdef CONFIG_SNAPPY
2042 item->next = g_malloc0(sizeof(DumpGuestMemoryFormatList));
2043 item = item->next;
2044 item->value = DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY;
2045#endif
2046
2047 return cap;
2048}