blob: 7ebcf553b2d3cae793075f51ed5fe19a87cf0548 [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 Armbrustercc7a8ea2015-03-17 17:22:46 +010025#include "qapi/qmp/qerror.h"
Wen Congyang783e9b42012-05-07 12:10:47 +080026#include "qmp-commands.h"
Peter Xud42a0d12016-02-18 13:16:56 +080027#include "qapi-event.h"
Wen Congyang783e9b42012-05-07 12:10:47 +080028
qiaonuohand12f57e2014-02-18 14:11:35 +080029#include <zlib.h>
30#ifdef CONFIG_LZO
31#include <lzo/lzo1x.h>
32#endif
33#ifdef CONFIG_SNAPPY
34#include <snappy-c.h>
35#endif
qiaonuohan4ab23a92014-02-18 14:11:37 +080036#ifndef ELF_MACHINE_UNAME
37#define ELF_MACHINE_UNAME "Unknown"
38#endif
qiaonuohand12f57e2014-02-18 14:11:35 +080039
Bharata B Raoacb0ef52014-05-19 19:57:44 +020040uint16_t cpu_to_dump16(DumpState *s, uint16_t val)
Wen Congyang783e9b42012-05-07 12:10:47 +080041{
Bharata B Raoacb0ef52014-05-19 19:57:44 +020042 if (s->dump_info.d_endian == ELFDATA2LSB) {
Wen Congyang783e9b42012-05-07 12:10:47 +080043 val = cpu_to_le16(val);
44 } else {
45 val = cpu_to_be16(val);
46 }
47
48 return val;
49}
50
Bharata B Raoacb0ef52014-05-19 19:57:44 +020051uint32_t cpu_to_dump32(DumpState *s, uint32_t val)
Wen Congyang783e9b42012-05-07 12:10:47 +080052{
Bharata B Raoacb0ef52014-05-19 19:57:44 +020053 if (s->dump_info.d_endian == ELFDATA2LSB) {
Wen Congyang783e9b42012-05-07 12:10:47 +080054 val = cpu_to_le32(val);
55 } else {
56 val = cpu_to_be32(val);
57 }
58
59 return val;
60}
61
Bharata B Raoacb0ef52014-05-19 19:57:44 +020062uint64_t cpu_to_dump64(DumpState *s, uint64_t val)
Wen Congyang783e9b42012-05-07 12:10:47 +080063{
Bharata B Raoacb0ef52014-05-19 19:57:44 +020064 if (s->dump_info.d_endian == ELFDATA2LSB) {
Wen Congyang783e9b42012-05-07 12:10:47 +080065 val = cpu_to_le64(val);
66 } else {
67 val = cpu_to_be64(val);
68 }
69
70 return val;
71}
72
Wen Congyang783e9b42012-05-07 12:10:47 +080073static int dump_cleanup(DumpState *s)
74{
Laszlo Ersek5ee163e2013-08-06 12:37:09 +020075 guest_phys_blocks_free(&s->guest_phys_blocks);
Wen Congyang783e9b42012-05-07 12:10:47 +080076 memory_mapping_list_free(&s->list);
Chen Gang29282072014-08-03 23:28:56 +080077 close(s->fd);
Wen Congyang783e9b42012-05-07 12:10:47 +080078 if (s->resume) {
Fam Zheng6796b402017-05-03 15:28:19 +080079 if (s->detached) {
80 qemu_mutex_lock_iothread();
81 }
Wen Congyang783e9b42012-05-07 12:10:47 +080082 vm_start();
Fam Zheng6796b402017-05-03 15:28:19 +080083 if (s->detached) {
84 qemu_mutex_unlock_iothread();
85 }
Wen Congyang783e9b42012-05-07 12:10:47 +080086 }
87
Chen Gang29282072014-08-03 23:28:56 +080088 return 0;
Wen Congyang783e9b42012-05-07 12:10:47 +080089}
90
qiaonuohanb5ba1cc2014-02-18 14:11:25 +080091static int fd_write_vmcore(const void *buf, size_t size, void *opaque)
Wen Congyang783e9b42012-05-07 12:10:47 +080092{
93 DumpState *s = opaque;
Luiz Capitulino2f616522012-09-21 13:17:55 -030094 size_t written_size;
Wen Congyang783e9b42012-05-07 12:10:47 +080095
Luiz Capitulino2f616522012-09-21 13:17:55 -030096 written_size = qemu_write_full(s->fd, buf, size);
97 if (written_size != size) {
98 return -1;
Wen Congyang783e9b42012-05-07 12:10:47 +080099 }
100
101 return 0;
102}
103
zhanghailiang4c7e2512014-10-09 14:13:11 +0800104static void write_elf64_header(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800105{
106 Elf64_Ehdr elf_header;
107 int ret;
Wen Congyang783e9b42012-05-07 12:10:47 +0800108
109 memset(&elf_header, 0, sizeof(Elf64_Ehdr));
110 memcpy(&elf_header, ELFMAG, SELFMAG);
111 elf_header.e_ident[EI_CLASS] = ELFCLASS64;
112 elf_header.e_ident[EI_DATA] = s->dump_info.d_endian;
113 elf_header.e_ident[EI_VERSION] = EV_CURRENT;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200114 elf_header.e_type = cpu_to_dump16(s, ET_CORE);
115 elf_header.e_machine = cpu_to_dump16(s, s->dump_info.d_machine);
116 elf_header.e_version = cpu_to_dump32(s, EV_CURRENT);
117 elf_header.e_ehsize = cpu_to_dump16(s, sizeof(elf_header));
118 elf_header.e_phoff = cpu_to_dump64(s, sizeof(Elf64_Ehdr));
119 elf_header.e_phentsize = cpu_to_dump16(s, sizeof(Elf64_Phdr));
120 elf_header.e_phnum = cpu_to_dump16(s, s->phdr_num);
Wen Congyang783e9b42012-05-07 12:10:47 +0800121 if (s->have_section) {
122 uint64_t shoff = sizeof(Elf64_Ehdr) + sizeof(Elf64_Phdr) * s->sh_info;
123
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200124 elf_header.e_shoff = cpu_to_dump64(s, shoff);
125 elf_header.e_shentsize = cpu_to_dump16(s, sizeof(Elf64_Shdr));
126 elf_header.e_shnum = cpu_to_dump16(s, 1);
Wen Congyang783e9b42012-05-07 12:10:47 +0800127 }
128
129 ret = fd_write_vmcore(&elf_header, sizeof(elf_header), s);
130 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800131 error_setg(errp, "dump: failed to write elf header");
Wen Congyang783e9b42012-05-07 12:10:47 +0800132 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800133}
134
zhanghailiang4c7e2512014-10-09 14:13:11 +0800135static void write_elf32_header(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800136{
137 Elf32_Ehdr elf_header;
138 int ret;
Wen Congyang783e9b42012-05-07 12:10:47 +0800139
140 memset(&elf_header, 0, sizeof(Elf32_Ehdr));
141 memcpy(&elf_header, ELFMAG, SELFMAG);
142 elf_header.e_ident[EI_CLASS] = ELFCLASS32;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200143 elf_header.e_ident[EI_DATA] = s->dump_info.d_endian;
Wen Congyang783e9b42012-05-07 12:10:47 +0800144 elf_header.e_ident[EI_VERSION] = EV_CURRENT;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200145 elf_header.e_type = cpu_to_dump16(s, ET_CORE);
146 elf_header.e_machine = cpu_to_dump16(s, s->dump_info.d_machine);
147 elf_header.e_version = cpu_to_dump32(s, EV_CURRENT);
148 elf_header.e_ehsize = cpu_to_dump16(s, sizeof(elf_header));
149 elf_header.e_phoff = cpu_to_dump32(s, sizeof(Elf32_Ehdr));
150 elf_header.e_phentsize = cpu_to_dump16(s, sizeof(Elf32_Phdr));
151 elf_header.e_phnum = cpu_to_dump16(s, s->phdr_num);
Wen Congyang783e9b42012-05-07 12:10:47 +0800152 if (s->have_section) {
153 uint32_t shoff = sizeof(Elf32_Ehdr) + sizeof(Elf32_Phdr) * s->sh_info;
154
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200155 elf_header.e_shoff = cpu_to_dump32(s, shoff);
156 elf_header.e_shentsize = cpu_to_dump16(s, sizeof(Elf32_Shdr));
157 elf_header.e_shnum = cpu_to_dump16(s, 1);
Wen Congyang783e9b42012-05-07 12:10:47 +0800158 }
159
160 ret = fd_write_vmcore(&elf_header, sizeof(elf_header), s);
161 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800162 error_setg(errp, "dump: failed to write elf header");
Wen Congyang783e9b42012-05-07 12:10:47 +0800163 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800164}
165
zhanghailiang4c7e2512014-10-09 14:13:11 +0800166static void write_elf64_load(DumpState *s, MemoryMapping *memory_mapping,
167 int phdr_index, hwaddr offset,
168 hwaddr filesz, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800169{
170 Elf64_Phdr phdr;
171 int ret;
Wen Congyang783e9b42012-05-07 12:10:47 +0800172
173 memset(&phdr, 0, sizeof(Elf64_Phdr));
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200174 phdr.p_type = cpu_to_dump32(s, PT_LOAD);
175 phdr.p_offset = cpu_to_dump64(s, offset);
176 phdr.p_paddr = cpu_to_dump64(s, memory_mapping->phys_addr);
177 phdr.p_filesz = cpu_to_dump64(s, filesz);
178 phdr.p_memsz = cpu_to_dump64(s, memory_mapping->length);
179 phdr.p_vaddr = cpu_to_dump64(s, memory_mapping->virt_addr);
Wen Congyang783e9b42012-05-07 12:10:47 +0800180
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200181 assert(memory_mapping->length >= filesz);
182
Wen Congyang783e9b42012-05-07 12:10:47 +0800183 ret = fd_write_vmcore(&phdr, sizeof(Elf64_Phdr), s);
184 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800185 error_setg(errp, "dump: failed to write program header table");
Wen Congyang783e9b42012-05-07 12:10:47 +0800186 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800187}
188
zhanghailiang4c7e2512014-10-09 14:13:11 +0800189static void write_elf32_load(DumpState *s, MemoryMapping *memory_mapping,
190 int phdr_index, hwaddr offset,
191 hwaddr filesz, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800192{
193 Elf32_Phdr phdr;
194 int ret;
Wen Congyang783e9b42012-05-07 12:10:47 +0800195
196 memset(&phdr, 0, sizeof(Elf32_Phdr));
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200197 phdr.p_type = cpu_to_dump32(s, PT_LOAD);
198 phdr.p_offset = cpu_to_dump32(s, offset);
199 phdr.p_paddr = cpu_to_dump32(s, memory_mapping->phys_addr);
200 phdr.p_filesz = cpu_to_dump32(s, filesz);
201 phdr.p_memsz = cpu_to_dump32(s, memory_mapping->length);
202 phdr.p_vaddr = cpu_to_dump32(s, memory_mapping->virt_addr);
Wen Congyang783e9b42012-05-07 12:10:47 +0800203
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200204 assert(memory_mapping->length >= filesz);
205
Wen Congyang783e9b42012-05-07 12:10:47 +0800206 ret = fd_write_vmcore(&phdr, sizeof(Elf32_Phdr), s);
207 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800208 error_setg(errp, "dump: failed to write program header table");
Wen Congyang783e9b42012-05-07 12:10:47 +0800209 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800210}
211
zhanghailiang4c7e2512014-10-09 14:13:11 +0800212static void write_elf64_note(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800213{
214 Elf64_Phdr phdr;
Avi Kivitya8170e52012-10-23 12:30:10 +0200215 hwaddr begin = s->memory_offset - s->note_size;
Wen Congyang783e9b42012-05-07 12:10:47 +0800216 int ret;
217
218 memset(&phdr, 0, sizeof(Elf64_Phdr));
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200219 phdr.p_type = cpu_to_dump32(s, PT_NOTE);
220 phdr.p_offset = cpu_to_dump64(s, begin);
Wen Congyang783e9b42012-05-07 12:10:47 +0800221 phdr.p_paddr = 0;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200222 phdr.p_filesz = cpu_to_dump64(s, s->note_size);
223 phdr.p_memsz = cpu_to_dump64(s, s->note_size);
Wen Congyang783e9b42012-05-07 12:10:47 +0800224 phdr.p_vaddr = 0;
225
226 ret = fd_write_vmcore(&phdr, sizeof(Elf64_Phdr), s);
227 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800228 error_setg(errp, "dump: failed to write program header table");
Wen Congyang783e9b42012-05-07 12:10:47 +0800229 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800230}
231
Paolo Bonzini0bc3cd62013-04-08 17:29:59 +0200232static inline int cpu_index(CPUState *cpu)
233{
234 return cpu->cpu_index + 1;
235}
236
zhanghailiang4c7e2512014-10-09 14:13:11 +0800237static void write_elf64_notes(WriteCoreDumpFunction f, DumpState *s,
238 Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800239{
Andreas Färber0d342822012-12-17 07:12:13 +0100240 CPUState *cpu;
Wen Congyang783e9b42012-05-07 12:10:47 +0800241 int ret;
242 int id;
243
Andreas Färberbdc44642013-06-24 23:50:24 +0200244 CPU_FOREACH(cpu) {
Andreas Färber0d342822012-12-17 07:12:13 +0100245 id = cpu_index(cpu);
qiaonuohan6a519912014-02-18 14:11:26 +0800246 ret = cpu_write_elf64_note(f, cpu, id, s);
Wen Congyang783e9b42012-05-07 12:10:47 +0800247 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800248 error_setg(errp, "dump: failed to write elf notes");
zhanghailiang4c7e2512014-10-09 14:13:11 +0800249 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800250 }
251 }
252
Andreas Färberbdc44642013-06-24 23:50:24 +0200253 CPU_FOREACH(cpu) {
qiaonuohan6a519912014-02-18 14:11:26 +0800254 ret = cpu_write_elf64_qemunote(f, cpu, s);
Wen Congyang783e9b42012-05-07 12:10:47 +0800255 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800256 error_setg(errp, "dump: failed to write CPU status");
zhanghailiang4c7e2512014-10-09 14:13:11 +0800257 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800258 }
259 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800260}
261
zhanghailiang4c7e2512014-10-09 14:13:11 +0800262static void write_elf32_note(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800263{
Avi Kivitya8170e52012-10-23 12:30:10 +0200264 hwaddr begin = s->memory_offset - s->note_size;
Wen Congyang783e9b42012-05-07 12:10:47 +0800265 Elf32_Phdr phdr;
Wen Congyang783e9b42012-05-07 12:10:47 +0800266 int ret;
267
268 memset(&phdr, 0, sizeof(Elf32_Phdr));
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200269 phdr.p_type = cpu_to_dump32(s, PT_NOTE);
270 phdr.p_offset = cpu_to_dump32(s, begin);
Wen Congyang783e9b42012-05-07 12:10:47 +0800271 phdr.p_paddr = 0;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200272 phdr.p_filesz = cpu_to_dump32(s, s->note_size);
273 phdr.p_memsz = cpu_to_dump32(s, s->note_size);
Wen Congyang783e9b42012-05-07 12:10:47 +0800274 phdr.p_vaddr = 0;
275
276 ret = fd_write_vmcore(&phdr, sizeof(Elf32_Phdr), s);
277 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800278 error_setg(errp, "dump: failed to write program header table");
Wen Congyang783e9b42012-05-07 12:10:47 +0800279 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800280}
281
zhanghailiang4c7e2512014-10-09 14:13:11 +0800282static void write_elf32_notes(WriteCoreDumpFunction f, DumpState *s,
283 Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800284{
Andreas Färber0d342822012-12-17 07:12:13 +0100285 CPUState *cpu;
Wen Congyang783e9b42012-05-07 12:10:47 +0800286 int ret;
287 int id;
288
Andreas Färberbdc44642013-06-24 23:50:24 +0200289 CPU_FOREACH(cpu) {
Andreas Färber0d342822012-12-17 07:12:13 +0100290 id = cpu_index(cpu);
qiaonuohan6a519912014-02-18 14:11:26 +0800291 ret = cpu_write_elf32_note(f, cpu, id, s);
Wen Congyang783e9b42012-05-07 12:10:47 +0800292 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800293 error_setg(errp, "dump: failed to write elf notes");
zhanghailiang4c7e2512014-10-09 14:13:11 +0800294 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800295 }
296 }
297
Andreas Färberbdc44642013-06-24 23:50:24 +0200298 CPU_FOREACH(cpu) {
qiaonuohan6a519912014-02-18 14:11:26 +0800299 ret = cpu_write_elf32_qemunote(f, cpu, s);
Wen Congyang783e9b42012-05-07 12:10:47 +0800300 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800301 error_setg(errp, "dump: failed to write CPU status");
zhanghailiang4c7e2512014-10-09 14:13:11 +0800302 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800303 }
304 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800305}
306
zhanghailiang4c7e2512014-10-09 14:13:11 +0800307static void write_elf_section(DumpState *s, int type, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800308{
309 Elf32_Shdr shdr32;
310 Elf64_Shdr shdr64;
Wen Congyang783e9b42012-05-07 12:10:47 +0800311 int shdr_size;
312 void *shdr;
313 int ret;
314
315 if (type == 0) {
316 shdr_size = sizeof(Elf32_Shdr);
317 memset(&shdr32, 0, shdr_size);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200318 shdr32.sh_info = cpu_to_dump32(s, s->sh_info);
Wen Congyang783e9b42012-05-07 12:10:47 +0800319 shdr = &shdr32;
320 } else {
321 shdr_size = sizeof(Elf64_Shdr);
322 memset(&shdr64, 0, shdr_size);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200323 shdr64.sh_info = cpu_to_dump32(s, s->sh_info);
Wen Congyang783e9b42012-05-07 12:10:47 +0800324 shdr = &shdr64;
325 }
326
327 ret = fd_write_vmcore(&shdr, shdr_size, s);
328 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800329 error_setg(errp, "dump: failed to write section header table");
Wen Congyang783e9b42012-05-07 12:10:47 +0800330 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800331}
332
zhanghailiang4c7e2512014-10-09 14:13:11 +0800333static void write_data(DumpState *s, void *buf, int length, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800334{
335 int ret;
336
337 ret = fd_write_vmcore(buf, length, s);
338 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800339 error_setg(errp, "dump: failed to save memory");
Peter Xu2264c2c2016-02-18 13:16:53 +0800340 } else {
341 s->written_size += length;
Wen Congyang783e9b42012-05-07 12:10:47 +0800342 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800343}
344
zhanghailiang4c7e2512014-10-09 14:13:11 +0800345/* write the memory to vmcore. 1 page per I/O. */
346static void write_memory(DumpState *s, GuestPhysBlock *block, ram_addr_t start,
347 int64_t size, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800348{
349 int64_t i;
zhanghailiang4c7e2512014-10-09 14:13:11 +0800350 Error *local_err = NULL;
Wen Congyang783e9b42012-05-07 12:10:47 +0800351
Andrew Jones8161bef2016-01-11 20:56:20 +0100352 for (i = 0; i < size / s->dump_info.page_size; i++) {
353 write_data(s, block->host_addr + start + i * s->dump_info.page_size,
354 s->dump_info.page_size, &local_err);
zhanghailiang4c7e2512014-10-09 14:13:11 +0800355 if (local_err) {
356 error_propagate(errp, local_err);
357 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800358 }
359 }
360
Andrew Jones8161bef2016-01-11 20:56:20 +0100361 if ((size % s->dump_info.page_size) != 0) {
362 write_data(s, block->host_addr + start + i * s->dump_info.page_size,
363 size % s->dump_info.page_size, &local_err);
zhanghailiang4c7e2512014-10-09 14:13:11 +0800364 if (local_err) {
365 error_propagate(errp, local_err);
366 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800367 }
368 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800369}
370
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200371/* get the memory's offset and size in the vmcore */
372static void get_offset_range(hwaddr phys_addr,
373 ram_addr_t mapping_length,
374 DumpState *s,
375 hwaddr *p_offset,
376 hwaddr *p_filesz)
Wen Congyang783e9b42012-05-07 12:10:47 +0800377{
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200378 GuestPhysBlock *block;
Avi Kivitya8170e52012-10-23 12:30:10 +0200379 hwaddr offset = s->memory_offset;
Wen Congyang783e9b42012-05-07 12:10:47 +0800380 int64_t size_in_block, start;
381
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200382 /* When the memory is not stored into vmcore, offset will be -1 */
383 *p_offset = -1;
384 *p_filesz = 0;
385
Wen Congyang783e9b42012-05-07 12:10:47 +0800386 if (s->has_filter) {
387 if (phys_addr < s->begin || phys_addr >= s->begin + s->length) {
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200388 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800389 }
390 }
391
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200392 QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
Wen Congyang783e9b42012-05-07 12:10:47 +0800393 if (s->has_filter) {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200394 if (block->target_start >= s->begin + s->length ||
395 block->target_end <= s->begin) {
Wen Congyang783e9b42012-05-07 12:10:47 +0800396 /* This block is out of the range */
397 continue;
398 }
399
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200400 if (s->begin <= block->target_start) {
401 start = block->target_start;
Wen Congyang783e9b42012-05-07 12:10:47 +0800402 } else {
403 start = s->begin;
404 }
405
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200406 size_in_block = block->target_end - start;
407 if (s->begin + s->length < block->target_end) {
408 size_in_block -= block->target_end - (s->begin + s->length);
Wen Congyang783e9b42012-05-07 12:10:47 +0800409 }
410 } else {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200411 start = block->target_start;
412 size_in_block = block->target_end - block->target_start;
Wen Congyang783e9b42012-05-07 12:10:47 +0800413 }
414
415 if (phys_addr >= start && phys_addr < start + size_in_block) {
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200416 *p_offset = phys_addr - start + offset;
417
418 /* The offset range mapped from the vmcore file must not spill over
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200419 * the GuestPhysBlock, clamp it. The rest of the mapping will be
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200420 * zero-filled in memory at load time; see
421 * <http://refspecs.linuxbase.org/elf/gabi4+/ch5.pheader.html>.
422 */
423 *p_filesz = phys_addr + mapping_length <= start + size_in_block ?
424 mapping_length :
425 size_in_block - (phys_addr - start);
426 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800427 }
428
429 offset += size_in_block;
430 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800431}
432
zhanghailiang4c7e2512014-10-09 14:13:11 +0800433static void write_elf_loads(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800434{
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200435 hwaddr offset, filesz;
Wen Congyang783e9b42012-05-07 12:10:47 +0800436 MemoryMapping *memory_mapping;
437 uint32_t phdr_index = 1;
Wen Congyang783e9b42012-05-07 12:10:47 +0800438 uint32_t max_index;
zhanghailiang4c7e2512014-10-09 14:13:11 +0800439 Error *local_err = NULL;
Wen Congyang783e9b42012-05-07 12:10:47 +0800440
441 if (s->have_section) {
442 max_index = s->sh_info;
443 } else {
444 max_index = s->phdr_num;
445 }
446
447 QTAILQ_FOREACH(memory_mapping, &s->list.head, next) {
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200448 get_offset_range(memory_mapping->phys_addr,
449 memory_mapping->length,
450 s, &offset, &filesz);
Wen Congyang783e9b42012-05-07 12:10:47 +0800451 if (s->dump_info.d_class == ELFCLASS64) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800452 write_elf64_load(s, memory_mapping, phdr_index++, offset,
453 filesz, &local_err);
Wen Congyang783e9b42012-05-07 12:10:47 +0800454 } else {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800455 write_elf32_load(s, memory_mapping, phdr_index++, offset,
456 filesz, &local_err);
Wen Congyang783e9b42012-05-07 12:10:47 +0800457 }
458
zhanghailiang4c7e2512014-10-09 14:13:11 +0800459 if (local_err) {
460 error_propagate(errp, local_err);
461 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800462 }
463
464 if (phdr_index >= max_index) {
465 break;
466 }
467 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800468}
469
470/* write elf header, PT_NOTE and elf note to vmcore. */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800471static void dump_begin(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800472{
zhanghailiang4c7e2512014-10-09 14:13:11 +0800473 Error *local_err = NULL;
Wen Congyang783e9b42012-05-07 12:10:47 +0800474
475 /*
476 * the vmcore's format is:
477 * --------------
478 * | elf header |
479 * --------------
480 * | PT_NOTE |
481 * --------------
482 * | PT_LOAD |
483 * --------------
484 * | ...... |
485 * --------------
486 * | PT_LOAD |
487 * --------------
488 * | sec_hdr |
489 * --------------
490 * | elf note |
491 * --------------
492 * | memory |
493 * --------------
494 *
495 * we only know where the memory is saved after we write elf note into
496 * vmcore.
497 */
498
499 /* write elf header to vmcore */
500 if (s->dump_info.d_class == ELFCLASS64) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800501 write_elf64_header(s, &local_err);
Wen Congyang783e9b42012-05-07 12:10:47 +0800502 } else {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800503 write_elf32_header(s, &local_err);
Wen Congyang783e9b42012-05-07 12:10:47 +0800504 }
zhanghailiang4c7e2512014-10-09 14:13:11 +0800505 if (local_err) {
506 error_propagate(errp, local_err);
507 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800508 }
509
510 if (s->dump_info.d_class == ELFCLASS64) {
511 /* write PT_NOTE to vmcore */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800512 write_elf64_note(s, &local_err);
513 if (local_err) {
514 error_propagate(errp, local_err);
515 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800516 }
517
518 /* write all PT_LOAD to vmcore */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800519 write_elf_loads(s, &local_err);
520 if (local_err) {
521 error_propagate(errp, local_err);
522 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800523 }
524
525 /* write section to vmcore */
526 if (s->have_section) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800527 write_elf_section(s, 1, &local_err);
528 if (local_err) {
529 error_propagate(errp, local_err);
530 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800531 }
532 }
533
534 /* write notes to vmcore */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800535 write_elf64_notes(fd_write_vmcore, s, &local_err);
536 if (local_err) {
537 error_propagate(errp, local_err);
538 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800539 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800540 } else {
541 /* write PT_NOTE to vmcore */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800542 write_elf32_note(s, &local_err);
543 if (local_err) {
544 error_propagate(errp, local_err);
545 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800546 }
547
548 /* write all PT_LOAD to vmcore */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800549 write_elf_loads(s, &local_err);
550 if (local_err) {
551 error_propagate(errp, local_err);
552 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800553 }
554
555 /* write section to vmcore */
556 if (s->have_section) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800557 write_elf_section(s, 0, &local_err);
558 if (local_err) {
559 error_propagate(errp, local_err);
560 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800561 }
562 }
563
564 /* write notes to vmcore */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800565 write_elf32_notes(fd_write_vmcore, s, &local_err);
566 if (local_err) {
567 error_propagate(errp, local_err);
568 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800569 }
570 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800571}
572
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200573static int get_next_block(DumpState *s, GuestPhysBlock *block)
Wen Congyang783e9b42012-05-07 12:10:47 +0800574{
575 while (1) {
Paolo Bonzinia3161032012-11-14 15:54:48 +0100576 block = QTAILQ_NEXT(block, next);
Wen Congyang783e9b42012-05-07 12:10:47 +0800577 if (!block) {
578 /* no more block */
579 return 1;
580 }
581
582 s->start = 0;
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200583 s->next_block = block;
Wen Congyang783e9b42012-05-07 12:10:47 +0800584 if (s->has_filter) {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200585 if (block->target_start >= s->begin + s->length ||
586 block->target_end <= s->begin) {
Wen Congyang783e9b42012-05-07 12:10:47 +0800587 /* This block is out of the range */
588 continue;
589 }
590
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200591 if (s->begin > block->target_start) {
592 s->start = s->begin - block->target_start;
Wen Congyang783e9b42012-05-07 12:10:47 +0800593 }
594 }
595
596 return 0;
597 }
598}
599
600/* write all memory to vmcore */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800601static void dump_iterate(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800602{
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200603 GuestPhysBlock *block;
Wen Congyang783e9b42012-05-07 12:10:47 +0800604 int64_t size;
zhanghailiang4c7e2512014-10-09 14:13:11 +0800605 Error *local_err = NULL;
Wen Congyang783e9b42012-05-07 12:10:47 +0800606
Gonglei08a655b2014-10-30 14:01:17 +0800607 do {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200608 block = s->next_block;
Wen Congyang783e9b42012-05-07 12:10:47 +0800609
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200610 size = block->target_end - block->target_start;
Wen Congyang783e9b42012-05-07 12:10:47 +0800611 if (s->has_filter) {
612 size -= s->start;
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200613 if (s->begin + s->length < block->target_end) {
614 size -= block->target_end - (s->begin + s->length);
Wen Congyang783e9b42012-05-07 12:10:47 +0800615 }
616 }
zhanghailiang4c7e2512014-10-09 14:13:11 +0800617 write_memory(s, block, s->start, size, &local_err);
618 if (local_err) {
619 error_propagate(errp, local_err);
620 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800621 }
622
Gonglei08a655b2014-10-30 14:01:17 +0800623 } while (!get_next_block(s, block));
Wen Congyang783e9b42012-05-07 12:10:47 +0800624}
625
zhanghailiang4c7e2512014-10-09 14:13:11 +0800626static void create_vmcore(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800627{
zhanghailiang4c7e2512014-10-09 14:13:11 +0800628 Error *local_err = NULL;
Wen Congyang783e9b42012-05-07 12:10:47 +0800629
zhanghailiang4c7e2512014-10-09 14:13:11 +0800630 dump_begin(s, &local_err);
631 if (local_err) {
632 error_propagate(errp, local_err);
633 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800634 }
635
zhanghailiang4c7e2512014-10-09 14:13:11 +0800636 dump_iterate(s, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800637}
638
qiaonuohanfda05382014-02-18 14:11:27 +0800639static int write_start_flat_header(int fd)
640{
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200641 MakedumpfileHeader *mh;
qiaonuohanfda05382014-02-18 14:11:27 +0800642 int ret = 0;
643
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200644 QEMU_BUILD_BUG_ON(sizeof *mh > MAX_SIZE_MDF_HEADER);
645 mh = g_malloc0(MAX_SIZE_MDF_HEADER);
qiaonuohanfda05382014-02-18 14:11:27 +0800646
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200647 memcpy(mh->signature, MAKEDUMPFILE_SIGNATURE,
648 MIN(sizeof mh->signature, sizeof MAKEDUMPFILE_SIGNATURE));
qiaonuohanfda05382014-02-18 14:11:27 +0800649
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200650 mh->type = cpu_to_be64(TYPE_FLAT_HEADER);
651 mh->version = cpu_to_be64(VERSION_FLAT_HEADER);
qiaonuohanfda05382014-02-18 14:11:27 +0800652
653 size_t written_size;
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200654 written_size = qemu_write_full(fd, mh, MAX_SIZE_MDF_HEADER);
qiaonuohanfda05382014-02-18 14:11:27 +0800655 if (written_size != MAX_SIZE_MDF_HEADER) {
656 ret = -1;
657 }
658
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200659 g_free(mh);
qiaonuohanfda05382014-02-18 14:11:27 +0800660 return ret;
661}
662
663static int write_end_flat_header(int fd)
664{
665 MakedumpfileDataHeader mdh;
666
667 mdh.offset = END_FLAG_FLAT_HEADER;
668 mdh.buf_size = END_FLAG_FLAT_HEADER;
669
670 size_t written_size;
671 written_size = qemu_write_full(fd, &mdh, sizeof(mdh));
672 if (written_size != sizeof(mdh)) {
673 return -1;
674 }
675
676 return 0;
677}
678
qiaonuohan5d31bab2014-02-18 14:11:28 +0800679static int write_buffer(int fd, off_t offset, const void *buf, size_t size)
680{
681 size_t written_size;
682 MakedumpfileDataHeader mdh;
683
684 mdh.offset = cpu_to_be64(offset);
685 mdh.buf_size = cpu_to_be64(size);
686
687 written_size = qemu_write_full(fd, &mdh, sizeof(mdh));
688 if (written_size != sizeof(mdh)) {
689 return -1;
690 }
691
692 written_size = qemu_write_full(fd, buf, size);
693 if (written_size != size) {
694 return -1;
695 }
696
697 return 0;
698}
699
qiaonuohan4835ef72014-02-18 14:11:29 +0800700static int buf_write_note(const void *buf, size_t size, void *opaque)
701{
702 DumpState *s = opaque;
703
704 /* note_buf is not enough */
705 if (s->note_buf_offset + size > s->note_size) {
706 return -1;
707 }
708
709 memcpy(s->note_buf + s->note_buf_offset, buf, size);
710
711 s->note_buf_offset += size;
712
713 return 0;
714}
715
qiaonuohan298f1162014-02-18 14:11:32 +0800716/* write common header, sub header and elf note to vmcore */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800717static void create_header32(DumpState *s, Error **errp)
qiaonuohan298f1162014-02-18 14:11:32 +0800718{
qiaonuohan298f1162014-02-18 14:11:32 +0800719 DiskDumpHeader32 *dh = NULL;
720 KdumpSubHeader32 *kh = NULL;
721 size_t size;
qiaonuohan298f1162014-02-18 14:11:32 +0800722 uint32_t block_size;
723 uint32_t sub_hdr_size;
724 uint32_t bitmap_blocks;
725 uint32_t status = 0;
726 uint64_t offset_note;
zhanghailiang4c7e2512014-10-09 14:13:11 +0800727 Error *local_err = NULL;
qiaonuohan298f1162014-02-18 14:11:32 +0800728
729 /* write common header, the version of kdump-compressed format is 6th */
730 size = sizeof(DiskDumpHeader32);
731 dh = g_malloc0(size);
732
733 strncpy(dh->signature, KDUMP_SIGNATURE, strlen(KDUMP_SIGNATURE));
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200734 dh->header_version = cpu_to_dump32(s, 6);
Andrew Jones8161bef2016-01-11 20:56:20 +0100735 block_size = s->dump_info.page_size;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200736 dh->block_size = cpu_to_dump32(s, block_size);
qiaonuohan298f1162014-02-18 14:11:32 +0800737 sub_hdr_size = sizeof(struct KdumpSubHeader32) + s->note_size;
738 sub_hdr_size = DIV_ROUND_UP(sub_hdr_size, block_size);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200739 dh->sub_hdr_size = cpu_to_dump32(s, sub_hdr_size);
qiaonuohan298f1162014-02-18 14:11:32 +0800740 /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200741 dh->max_mapnr = cpu_to_dump32(s, MIN(s->max_mapnr, UINT_MAX));
742 dh->nr_cpus = cpu_to_dump32(s, s->nr_cpus);
qiaonuohan298f1162014-02-18 14:11:32 +0800743 bitmap_blocks = DIV_ROUND_UP(s->len_dump_bitmap, block_size) * 2;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200744 dh->bitmap_blocks = cpu_to_dump32(s, bitmap_blocks);
qiaonuohan4ab23a92014-02-18 14:11:37 +0800745 strncpy(dh->utsname.machine, ELF_MACHINE_UNAME, sizeof(dh->utsname.machine));
qiaonuohan298f1162014-02-18 14:11:32 +0800746
747 if (s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) {
748 status |= DUMP_DH_COMPRESSED_ZLIB;
749 }
750#ifdef CONFIG_LZO
751 if (s->flag_compress & DUMP_DH_COMPRESSED_LZO) {
752 status |= DUMP_DH_COMPRESSED_LZO;
753 }
754#endif
755#ifdef CONFIG_SNAPPY
756 if (s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) {
757 status |= DUMP_DH_COMPRESSED_SNAPPY;
758 }
759#endif
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200760 dh->status = cpu_to_dump32(s, status);
qiaonuohan298f1162014-02-18 14:11:32 +0800761
762 if (write_buffer(s->fd, 0, dh, size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800763 error_setg(errp, "dump: failed to write disk dump header");
qiaonuohan298f1162014-02-18 14:11:32 +0800764 goto out;
765 }
766
767 /* write sub header */
768 size = sizeof(KdumpSubHeader32);
769 kh = g_malloc0(size);
770
771 /* 64bit max_mapnr_64 */
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200772 kh->max_mapnr_64 = cpu_to_dump64(s, s->max_mapnr);
Andrew Jonesb6e05aa2016-01-11 20:56:21 +0100773 kh->phys_base = cpu_to_dump32(s, s->dump_info.phys_base);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200774 kh->dump_level = cpu_to_dump32(s, DUMP_LEVEL);
qiaonuohan298f1162014-02-18 14:11:32 +0800775
776 offset_note = DISKDUMP_HEADER_BLOCKS * block_size + size;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200777 kh->offset_note = cpu_to_dump64(s, offset_note);
778 kh->note_size = cpu_to_dump32(s, s->note_size);
qiaonuohan298f1162014-02-18 14:11:32 +0800779
780 if (write_buffer(s->fd, DISKDUMP_HEADER_BLOCKS *
781 block_size, kh, size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800782 error_setg(errp, "dump: failed to write kdump sub header");
qiaonuohan298f1162014-02-18 14:11:32 +0800783 goto out;
784 }
785
786 /* write note */
787 s->note_buf = g_malloc0(s->note_size);
788 s->note_buf_offset = 0;
789
790 /* use s->note_buf to store notes temporarily */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800791 write_elf32_notes(buf_write_note, s, &local_err);
792 if (local_err) {
793 error_propagate(errp, local_err);
qiaonuohan298f1162014-02-18 14:11:32 +0800794 goto out;
795 }
qiaonuohan298f1162014-02-18 14:11:32 +0800796 if (write_buffer(s->fd, offset_note, s->note_buf,
797 s->note_size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800798 error_setg(errp, "dump: failed to write notes");
qiaonuohan298f1162014-02-18 14:11:32 +0800799 goto out;
800 }
801
802 /* get offset of dump_bitmap */
803 s->offset_dump_bitmap = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size) *
804 block_size;
805
806 /* get offset of page */
807 s->offset_page = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size + bitmap_blocks) *
808 block_size;
809
810out:
811 g_free(dh);
812 g_free(kh);
813 g_free(s->note_buf);
qiaonuohan298f1162014-02-18 14:11:32 +0800814}
815
816/* write common header, sub header and elf note to vmcore */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800817static void create_header64(DumpState *s, Error **errp)
qiaonuohan298f1162014-02-18 14:11:32 +0800818{
qiaonuohan298f1162014-02-18 14:11:32 +0800819 DiskDumpHeader64 *dh = NULL;
820 KdumpSubHeader64 *kh = NULL;
821 size_t size;
qiaonuohan298f1162014-02-18 14:11:32 +0800822 uint32_t block_size;
823 uint32_t sub_hdr_size;
824 uint32_t bitmap_blocks;
825 uint32_t status = 0;
826 uint64_t offset_note;
zhanghailiang4c7e2512014-10-09 14:13:11 +0800827 Error *local_err = NULL;
qiaonuohan298f1162014-02-18 14:11:32 +0800828
829 /* write common header, the version of kdump-compressed format is 6th */
830 size = sizeof(DiskDumpHeader64);
831 dh = g_malloc0(size);
832
833 strncpy(dh->signature, KDUMP_SIGNATURE, strlen(KDUMP_SIGNATURE));
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200834 dh->header_version = cpu_to_dump32(s, 6);
Andrew Jones8161bef2016-01-11 20:56:20 +0100835 block_size = s->dump_info.page_size;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200836 dh->block_size = cpu_to_dump32(s, block_size);
qiaonuohan298f1162014-02-18 14:11:32 +0800837 sub_hdr_size = sizeof(struct KdumpSubHeader64) + s->note_size;
838 sub_hdr_size = DIV_ROUND_UP(sub_hdr_size, block_size);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200839 dh->sub_hdr_size = cpu_to_dump32(s, sub_hdr_size);
qiaonuohan298f1162014-02-18 14:11:32 +0800840 /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200841 dh->max_mapnr = cpu_to_dump32(s, MIN(s->max_mapnr, UINT_MAX));
842 dh->nr_cpus = cpu_to_dump32(s, s->nr_cpus);
qiaonuohan298f1162014-02-18 14:11:32 +0800843 bitmap_blocks = DIV_ROUND_UP(s->len_dump_bitmap, block_size) * 2;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200844 dh->bitmap_blocks = cpu_to_dump32(s, bitmap_blocks);
qiaonuohan4ab23a92014-02-18 14:11:37 +0800845 strncpy(dh->utsname.machine, ELF_MACHINE_UNAME, sizeof(dh->utsname.machine));
qiaonuohan298f1162014-02-18 14:11:32 +0800846
847 if (s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) {
848 status |= DUMP_DH_COMPRESSED_ZLIB;
849 }
850#ifdef CONFIG_LZO
851 if (s->flag_compress & DUMP_DH_COMPRESSED_LZO) {
852 status |= DUMP_DH_COMPRESSED_LZO;
853 }
854#endif
855#ifdef CONFIG_SNAPPY
856 if (s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) {
857 status |= DUMP_DH_COMPRESSED_SNAPPY;
858 }
859#endif
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200860 dh->status = cpu_to_dump32(s, status);
qiaonuohan298f1162014-02-18 14:11:32 +0800861
862 if (write_buffer(s->fd, 0, dh, size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800863 error_setg(errp, "dump: failed to write disk dump header");
qiaonuohan298f1162014-02-18 14:11:32 +0800864 goto out;
865 }
866
867 /* write sub header */
868 size = sizeof(KdumpSubHeader64);
869 kh = g_malloc0(size);
870
871 /* 64bit max_mapnr_64 */
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200872 kh->max_mapnr_64 = cpu_to_dump64(s, s->max_mapnr);
Andrew Jonesb6e05aa2016-01-11 20:56:21 +0100873 kh->phys_base = cpu_to_dump64(s, s->dump_info.phys_base);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200874 kh->dump_level = cpu_to_dump32(s, DUMP_LEVEL);
qiaonuohan298f1162014-02-18 14:11:32 +0800875
876 offset_note = DISKDUMP_HEADER_BLOCKS * block_size + size;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200877 kh->offset_note = cpu_to_dump64(s, offset_note);
878 kh->note_size = cpu_to_dump64(s, s->note_size);
qiaonuohan298f1162014-02-18 14:11:32 +0800879
880 if (write_buffer(s->fd, DISKDUMP_HEADER_BLOCKS *
881 block_size, kh, size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800882 error_setg(errp, "dump: failed to write kdump sub header");
qiaonuohan298f1162014-02-18 14:11:32 +0800883 goto out;
884 }
885
886 /* write note */
887 s->note_buf = g_malloc0(s->note_size);
888 s->note_buf_offset = 0;
889
890 /* use s->note_buf to store notes temporarily */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800891 write_elf64_notes(buf_write_note, s, &local_err);
892 if (local_err) {
893 error_propagate(errp, local_err);
qiaonuohan298f1162014-02-18 14:11:32 +0800894 goto out;
895 }
896
897 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
zhanghailiang4c7e2512014-10-09 14:13:11 +0800917static void write_dump_header(DumpState *s, Error **errp)
qiaonuohan298f1162014-02-18 14:11:32 +0800918{
zhanghailiang4c7e2512014-10-09 14:13:11 +0800919 Error *local_err = NULL;
920
Laszlo Ersek24aeeac2014-05-20 13:39:45 +0200921 if (s->dump_info.d_class == ELFCLASS32) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800922 create_header32(s, &local_err);
qiaonuohan298f1162014-02-18 14:11:32 +0800923 } else {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800924 create_header64(s, &local_err);
925 }
Eduardo Habkost621ff942016-06-13 18:57:56 -0300926 error_propagate(errp, local_err);
qiaonuohan298f1162014-02-18 14:11:32 +0800927}
928
Andrew Jones8161bef2016-01-11 20:56:20 +0100929static size_t dump_bitmap_get_bufsize(DumpState *s)
930{
931 return s->dump_info.page_size;
932}
933
qiaonuohand0686c72014-02-18 14:11:33 +0800934/*
935 * set dump_bitmap sequencely. the bit before last_pfn is not allowed to be
936 * rewritten, so if need to set the first bit, set last_pfn and pfn to 0.
937 * set_dump_bitmap will always leave the recently set bit un-sync. And setting
938 * (last bit + sizeof(buf) * 8) to 0 will do flushing the content in buf into
939 * vmcore, ie. synchronizing un-sync bit into vmcore.
940 */
941static int set_dump_bitmap(uint64_t last_pfn, uint64_t pfn, bool value,
942 uint8_t *buf, DumpState *s)
943{
944 off_t old_offset, new_offset;
945 off_t offset_bitmap1, offset_bitmap2;
946 uint32_t byte, bit;
Andrew Jones8161bef2016-01-11 20:56:20 +0100947 size_t bitmap_bufsize = dump_bitmap_get_bufsize(s);
948 size_t bits_per_buf = bitmap_bufsize * CHAR_BIT;
qiaonuohand0686c72014-02-18 14:11:33 +0800949
950 /* should not set the previous place */
951 assert(last_pfn <= pfn);
952
953 /*
954 * if the bit needed to be set is not cached in buf, flush the data in buf
955 * to vmcore firstly.
956 * making new_offset be bigger than old_offset can also sync remained data
957 * into vmcore.
958 */
Andrew Jones8161bef2016-01-11 20:56:20 +0100959 old_offset = bitmap_bufsize * (last_pfn / bits_per_buf);
960 new_offset = bitmap_bufsize * (pfn / bits_per_buf);
qiaonuohand0686c72014-02-18 14:11:33 +0800961
962 while (old_offset < new_offset) {
963 /* calculate the offset and write dump_bitmap */
964 offset_bitmap1 = s->offset_dump_bitmap + old_offset;
965 if (write_buffer(s->fd, offset_bitmap1, buf,
Andrew Jones8161bef2016-01-11 20:56:20 +0100966 bitmap_bufsize) < 0) {
qiaonuohand0686c72014-02-18 14:11:33 +0800967 return -1;
968 }
969
970 /* dump level 1 is chosen, so 1st and 2nd bitmap are same */
971 offset_bitmap2 = s->offset_dump_bitmap + s->len_dump_bitmap +
972 old_offset;
973 if (write_buffer(s->fd, offset_bitmap2, buf,
Andrew Jones8161bef2016-01-11 20:56:20 +0100974 bitmap_bufsize) < 0) {
qiaonuohand0686c72014-02-18 14:11:33 +0800975 return -1;
976 }
977
Andrew Jones8161bef2016-01-11 20:56:20 +0100978 memset(buf, 0, bitmap_bufsize);
979 old_offset += bitmap_bufsize;
qiaonuohand0686c72014-02-18 14:11:33 +0800980 }
981
982 /* get the exact place of the bit in the buf, and set it */
Andrew Jones8161bef2016-01-11 20:56:20 +0100983 byte = (pfn % bits_per_buf) / CHAR_BIT;
984 bit = (pfn % bits_per_buf) % CHAR_BIT;
qiaonuohand0686c72014-02-18 14:11:33 +0800985 if (value) {
986 buf[byte] |= 1u << bit;
987 } else {
988 buf[byte] &= ~(1u << bit);
989 }
990
991 return 0;
992}
993
Andrew Jones8161bef2016-01-11 20:56:20 +0100994static uint64_t dump_paddr_to_pfn(DumpState *s, uint64_t addr)
995{
996 int target_page_shift = ctz32(s->dump_info.page_size);
997
998 return (addr >> target_page_shift) - ARCH_PFN_OFFSET;
999}
1000
1001static uint64_t dump_pfn_to_paddr(DumpState *s, uint64_t pfn)
1002{
1003 int target_page_shift = ctz32(s->dump_info.page_size);
1004
1005 return (pfn + ARCH_PFN_OFFSET) << target_page_shift;
1006}
1007
qiaonuohand0686c72014-02-18 14:11:33 +08001008/*
1009 * exam every page and return the page frame number and the address of the page.
1010 * bufptr can be NULL. note: the blocks here is supposed to reflect guest-phys
1011 * blocks, so block->target_start and block->target_end should be interal
1012 * multiples of the target page size.
1013 */
1014static bool get_next_page(GuestPhysBlock **blockptr, uint64_t *pfnptr,
1015 uint8_t **bufptr, DumpState *s)
1016{
1017 GuestPhysBlock *block = *blockptr;
Andrew Jones8161bef2016-01-11 20:56:20 +01001018 hwaddr addr, target_page_mask = ~((hwaddr)s->dump_info.page_size - 1);
qiaonuohand0686c72014-02-18 14:11:33 +08001019 uint8_t *buf;
1020
1021 /* block == NULL means the start of the iteration */
1022 if (!block) {
1023 block = QTAILQ_FIRST(&s->guest_phys_blocks.head);
1024 *blockptr = block;
Andrew Jones8161bef2016-01-11 20:56:20 +01001025 assert((block->target_start & ~target_page_mask) == 0);
1026 assert((block->target_end & ~target_page_mask) == 0);
1027 *pfnptr = dump_paddr_to_pfn(s, block->target_start);
qiaonuohand0686c72014-02-18 14:11:33 +08001028 if (bufptr) {
1029 *bufptr = block->host_addr;
1030 }
1031 return true;
1032 }
1033
1034 *pfnptr = *pfnptr + 1;
Andrew Jones8161bef2016-01-11 20:56:20 +01001035 addr = dump_pfn_to_paddr(s, *pfnptr);
qiaonuohand0686c72014-02-18 14:11:33 +08001036
1037 if ((addr >= block->target_start) &&
Andrew Jones8161bef2016-01-11 20:56:20 +01001038 (addr + s->dump_info.page_size <= block->target_end)) {
qiaonuohand0686c72014-02-18 14:11:33 +08001039 buf = block->host_addr + (addr - block->target_start);
1040 } else {
1041 /* the next page is in the next block */
1042 block = QTAILQ_NEXT(block, next);
1043 *blockptr = block;
1044 if (!block) {
1045 return false;
1046 }
Andrew Jones8161bef2016-01-11 20:56:20 +01001047 assert((block->target_start & ~target_page_mask) == 0);
1048 assert((block->target_end & ~target_page_mask) == 0);
1049 *pfnptr = dump_paddr_to_pfn(s, block->target_start);
qiaonuohand0686c72014-02-18 14:11:33 +08001050 buf = block->host_addr;
1051 }
1052
1053 if (bufptr) {
1054 *bufptr = buf;
1055 }
1056
1057 return true;
1058}
1059
zhanghailiang4c7e2512014-10-09 14:13:11 +08001060static void write_dump_bitmap(DumpState *s, Error **errp)
qiaonuohand0686c72014-02-18 14:11:33 +08001061{
1062 int ret = 0;
1063 uint64_t last_pfn, pfn;
1064 void *dump_bitmap_buf;
1065 size_t num_dumpable;
1066 GuestPhysBlock *block_iter = NULL;
Andrew Jones8161bef2016-01-11 20:56:20 +01001067 size_t bitmap_bufsize = dump_bitmap_get_bufsize(s);
1068 size_t bits_per_buf = bitmap_bufsize * CHAR_BIT;
qiaonuohand0686c72014-02-18 14:11:33 +08001069
1070 /* dump_bitmap_buf is used to store dump_bitmap temporarily */
Andrew Jones8161bef2016-01-11 20:56:20 +01001071 dump_bitmap_buf = g_malloc0(bitmap_bufsize);
qiaonuohand0686c72014-02-18 14:11:33 +08001072
1073 num_dumpable = 0;
1074 last_pfn = 0;
1075
1076 /*
1077 * exam memory page by page, and set the bit in dump_bitmap corresponded
1078 * to the existing page.
1079 */
1080 while (get_next_page(&block_iter, &pfn, NULL, s)) {
1081 ret = set_dump_bitmap(last_pfn, pfn, true, dump_bitmap_buf, s);
1082 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001083 error_setg(errp, "dump: failed to set dump_bitmap");
qiaonuohand0686c72014-02-18 14:11:33 +08001084 goto out;
1085 }
1086
1087 last_pfn = pfn;
1088 num_dumpable++;
1089 }
1090
1091 /*
1092 * set_dump_bitmap will always leave the recently set bit un-sync. Here we
Andrew Jones8161bef2016-01-11 20:56:20 +01001093 * set the remaining bits from last_pfn to the end of the bitmap buffer to
1094 * 0. With those set, the un-sync bit will be synchronized into the vmcore.
qiaonuohand0686c72014-02-18 14:11:33 +08001095 */
1096 if (num_dumpable > 0) {
Andrew Jones8161bef2016-01-11 20:56:20 +01001097 ret = set_dump_bitmap(last_pfn, last_pfn + bits_per_buf, false,
qiaonuohand0686c72014-02-18 14:11:33 +08001098 dump_bitmap_buf, s);
1099 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001100 error_setg(errp, "dump: failed to sync dump_bitmap");
qiaonuohand0686c72014-02-18 14:11:33 +08001101 goto out;
1102 }
1103 }
1104
1105 /* number of dumpable pages that will be dumped later */
1106 s->num_dumpable = num_dumpable;
1107
1108out:
1109 g_free(dump_bitmap_buf);
qiaonuohand0686c72014-02-18 14:11:33 +08001110}
1111
qiaonuohan64cfba62014-02-18 14:11:34 +08001112static void prepare_data_cache(DataCache *data_cache, DumpState *s,
1113 off_t offset)
1114{
1115 data_cache->fd = s->fd;
1116 data_cache->data_size = 0;
Andrew Jones8161bef2016-01-11 20:56:20 +01001117 data_cache->buf_size = 4 * dump_bitmap_get_bufsize(s);
1118 data_cache->buf = g_malloc0(data_cache->buf_size);
qiaonuohan64cfba62014-02-18 14:11:34 +08001119 data_cache->offset = offset;
1120}
1121
1122static int write_cache(DataCache *dc, const void *buf, size_t size,
1123 bool flag_sync)
1124{
1125 /*
1126 * dc->buf_size should not be less than size, otherwise dc will never be
1127 * enough
1128 */
1129 assert(size <= dc->buf_size);
1130
1131 /*
1132 * if flag_sync is set, synchronize data in dc->buf into vmcore.
1133 * otherwise check if the space is enough for caching data in buf, if not,
1134 * write the data in dc->buf to dc->fd and reset dc->buf
1135 */
1136 if ((!flag_sync && dc->data_size + size > dc->buf_size) ||
1137 (flag_sync && dc->data_size > 0)) {
1138 if (write_buffer(dc->fd, dc->offset, dc->buf, dc->data_size) < 0) {
1139 return -1;
1140 }
1141
1142 dc->offset += dc->data_size;
1143 dc->data_size = 0;
1144 }
1145
1146 if (!flag_sync) {
1147 memcpy(dc->buf + dc->data_size, buf, size);
1148 dc->data_size += size;
1149 }
1150
1151 return 0;
1152}
1153
1154static void free_data_cache(DataCache *data_cache)
1155{
1156 g_free(data_cache->buf);
1157}
1158
qiaonuohand12f57e2014-02-18 14:11:35 +08001159static size_t get_len_buf_out(size_t page_size, uint32_t flag_compress)
1160{
Laszlo Ersekb87ef352014-05-20 13:42:53 +02001161 switch (flag_compress) {
1162 case DUMP_DH_COMPRESSED_ZLIB:
1163 return compressBound(page_size);
qiaonuohand12f57e2014-02-18 14:11:35 +08001164
Laszlo Ersekb87ef352014-05-20 13:42:53 +02001165 case DUMP_DH_COMPRESSED_LZO:
1166 /*
1167 * LZO will expand incompressible data by a little amount. Please check
1168 * the following URL to see the expansion calculation:
1169 * http://www.oberhumer.com/opensource/lzo/lzofaq.php
1170 */
1171 return page_size + page_size / 16 + 64 + 3;
qiaonuohand12f57e2014-02-18 14:11:35 +08001172
1173#ifdef CONFIG_SNAPPY
Laszlo Ersekb87ef352014-05-20 13:42:53 +02001174 case DUMP_DH_COMPRESSED_SNAPPY:
1175 return snappy_max_compressed_length(page_size);
qiaonuohand12f57e2014-02-18 14:11:35 +08001176#endif
Laszlo Ersekb87ef352014-05-20 13:42:53 +02001177 }
1178 return 0;
qiaonuohand12f57e2014-02-18 14:11:35 +08001179}
1180
1181/*
1182 * check if the page is all 0
1183 */
1184static inline bool is_zero_page(const uint8_t *buf, size_t page_size)
1185{
1186 return buffer_is_zero(buf, page_size);
1187}
1188
zhanghailiang4c7e2512014-10-09 14:13:11 +08001189static void write_dump_pages(DumpState *s, Error **errp)
qiaonuohand12f57e2014-02-18 14:11:35 +08001190{
1191 int ret = 0;
1192 DataCache page_desc, page_data;
1193 size_t len_buf_out, size_out;
1194#ifdef CONFIG_LZO
1195 lzo_bytep wrkmem = NULL;
1196#endif
1197 uint8_t *buf_out = NULL;
1198 off_t offset_desc, offset_data;
1199 PageDescriptor pd, pd_zero;
1200 uint8_t *buf;
qiaonuohand12f57e2014-02-18 14:11:35 +08001201 GuestPhysBlock *block_iter = NULL;
1202 uint64_t pfn_iter;
1203
1204 /* get offset of page_desc and page_data in dump file */
1205 offset_desc = s->offset_page;
1206 offset_data = offset_desc + sizeof(PageDescriptor) * s->num_dumpable;
1207
1208 prepare_data_cache(&page_desc, s, offset_desc);
1209 prepare_data_cache(&page_data, s, offset_data);
1210
1211 /* prepare buffer to store compressed data */
Andrew Jones8161bef2016-01-11 20:56:20 +01001212 len_buf_out = get_len_buf_out(s->dump_info.page_size, s->flag_compress);
Laszlo Ersekb87ef352014-05-20 13:42:53 +02001213 assert(len_buf_out != 0);
qiaonuohand12f57e2014-02-18 14:11:35 +08001214
1215#ifdef CONFIG_LZO
1216 wrkmem = g_malloc(LZO1X_1_MEM_COMPRESS);
1217#endif
1218
1219 buf_out = g_malloc(len_buf_out);
1220
1221 /*
1222 * init zero page's page_desc and page_data, because every zero page
1223 * uses the same page_data
1224 */
Andrew Jones8161bef2016-01-11 20:56:20 +01001225 pd_zero.size = cpu_to_dump32(s, s->dump_info.page_size);
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001226 pd_zero.flags = cpu_to_dump32(s, 0);
1227 pd_zero.offset = cpu_to_dump64(s, offset_data);
1228 pd_zero.page_flags = cpu_to_dump64(s, 0);
Andrew Jones8161bef2016-01-11 20:56:20 +01001229 buf = g_malloc0(s->dump_info.page_size);
1230 ret = write_cache(&page_data, buf, s->dump_info.page_size, false);
qiaonuohand12f57e2014-02-18 14:11:35 +08001231 g_free(buf);
1232 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001233 error_setg(errp, "dump: failed to write page data (zero page)");
qiaonuohand12f57e2014-02-18 14:11:35 +08001234 goto out;
1235 }
1236
Andrew Jones8161bef2016-01-11 20:56:20 +01001237 offset_data += s->dump_info.page_size;
qiaonuohand12f57e2014-02-18 14:11:35 +08001238
1239 /*
1240 * dump memory to vmcore page by page. zero page will all be resided in the
1241 * first page of page section
1242 */
1243 while (get_next_page(&block_iter, &pfn_iter, &buf, s)) {
1244 /* check zero page */
Andrew Jones8161bef2016-01-11 20:56:20 +01001245 if (is_zero_page(buf, s->dump_info.page_size)) {
qiaonuohand12f57e2014-02-18 14:11:35 +08001246 ret = write_cache(&page_desc, &pd_zero, sizeof(PageDescriptor),
1247 false);
1248 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001249 error_setg(errp, "dump: failed to write page desc");
qiaonuohand12f57e2014-02-18 14:11:35 +08001250 goto out;
1251 }
1252 } else {
1253 /*
1254 * not zero page, then:
1255 * 1. compress the page
1256 * 2. write the compressed page into the cache of page_data
1257 * 3. get page desc of the compressed page and write it into the
1258 * cache of page_desc
1259 *
1260 * only one compression format will be used here, for
1261 * s->flag_compress is set. But when compression fails to work,
1262 * we fall back to save in plaintext.
1263 */
1264 size_out = len_buf_out;
1265 if ((s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) &&
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001266 (compress2(buf_out, (uLongf *)&size_out, buf,
Andrew Jones8161bef2016-01-11 20:56:20 +01001267 s->dump_info.page_size, Z_BEST_SPEED) == Z_OK) &&
1268 (size_out < s->dump_info.page_size)) {
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001269 pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_ZLIB);
1270 pd.size = cpu_to_dump32(s, size_out);
qiaonuohand12f57e2014-02-18 14:11:35 +08001271
1272 ret = write_cache(&page_data, buf_out, size_out, false);
1273 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001274 error_setg(errp, "dump: failed to write page data");
qiaonuohand12f57e2014-02-18 14:11:35 +08001275 goto out;
1276 }
1277#ifdef CONFIG_LZO
1278 } else if ((s->flag_compress & DUMP_DH_COMPRESSED_LZO) &&
Andrew Jones8161bef2016-01-11 20:56:20 +01001279 (lzo1x_1_compress(buf, s->dump_info.page_size, buf_out,
qiaonuohand12f57e2014-02-18 14:11:35 +08001280 (lzo_uint *)&size_out, wrkmem) == LZO_E_OK) &&
Andrew Jones8161bef2016-01-11 20:56:20 +01001281 (size_out < s->dump_info.page_size)) {
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001282 pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_LZO);
1283 pd.size = cpu_to_dump32(s, size_out);
qiaonuohand12f57e2014-02-18 14:11:35 +08001284
1285 ret = write_cache(&page_data, buf_out, size_out, false);
1286 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001287 error_setg(errp, "dump: failed to write page data");
qiaonuohand12f57e2014-02-18 14:11:35 +08001288 goto out;
1289 }
1290#endif
1291#ifdef CONFIG_SNAPPY
1292 } else if ((s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) &&
Andrew Jones8161bef2016-01-11 20:56:20 +01001293 (snappy_compress((char *)buf, s->dump_info.page_size,
qiaonuohand12f57e2014-02-18 14:11:35 +08001294 (char *)buf_out, &size_out) == SNAPPY_OK) &&
Andrew Jones8161bef2016-01-11 20:56:20 +01001295 (size_out < s->dump_info.page_size)) {
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001296 pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_SNAPPY);
1297 pd.size = cpu_to_dump32(s, size_out);
qiaonuohand12f57e2014-02-18 14:11:35 +08001298
1299 ret = write_cache(&page_data, buf_out, size_out, false);
1300 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001301 error_setg(errp, "dump: failed to write page data");
qiaonuohand12f57e2014-02-18 14:11:35 +08001302 goto out;
1303 }
1304#endif
1305 } else {
1306 /*
1307 * fall back to save in plaintext, size_out should be
Andrew Jones8161bef2016-01-11 20:56:20 +01001308 * assigned the target's page size
qiaonuohand12f57e2014-02-18 14:11:35 +08001309 */
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001310 pd.flags = cpu_to_dump32(s, 0);
Andrew Jones8161bef2016-01-11 20:56:20 +01001311 size_out = s->dump_info.page_size;
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001312 pd.size = cpu_to_dump32(s, size_out);
qiaonuohand12f57e2014-02-18 14:11:35 +08001313
Andrew Jones8161bef2016-01-11 20:56:20 +01001314 ret = write_cache(&page_data, buf,
1315 s->dump_info.page_size, false);
qiaonuohand12f57e2014-02-18 14:11:35 +08001316 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001317 error_setg(errp, "dump: failed to write page data");
qiaonuohand12f57e2014-02-18 14:11:35 +08001318 goto out;
1319 }
1320 }
1321
1322 /* get and write page desc here */
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001323 pd.page_flags = cpu_to_dump64(s, 0);
1324 pd.offset = cpu_to_dump64(s, offset_data);
qiaonuohand12f57e2014-02-18 14:11:35 +08001325 offset_data += size_out;
1326
1327 ret = write_cache(&page_desc, &pd, sizeof(PageDescriptor), false);
1328 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001329 error_setg(errp, "dump: failed to write page desc");
qiaonuohand12f57e2014-02-18 14:11:35 +08001330 goto out;
1331 }
1332 }
Peter Xu2264c2c2016-02-18 13:16:53 +08001333 s->written_size += s->dump_info.page_size;
qiaonuohand12f57e2014-02-18 14:11:35 +08001334 }
1335
1336 ret = write_cache(&page_desc, NULL, 0, true);
1337 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001338 error_setg(errp, "dump: failed to sync cache for page_desc");
qiaonuohand12f57e2014-02-18 14:11:35 +08001339 goto out;
1340 }
1341 ret = write_cache(&page_data, NULL, 0, true);
1342 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001343 error_setg(errp, "dump: failed to sync cache for page_data");
qiaonuohand12f57e2014-02-18 14:11:35 +08001344 goto out;
1345 }
1346
1347out:
1348 free_data_cache(&page_desc);
1349 free_data_cache(&page_data);
1350
1351#ifdef CONFIG_LZO
1352 g_free(wrkmem);
1353#endif
1354
1355 g_free(buf_out);
qiaonuohand12f57e2014-02-18 14:11:35 +08001356}
1357
zhanghailiang4c7e2512014-10-09 14:13:11 +08001358static void create_kdump_vmcore(DumpState *s, Error **errp)
qiaonuohanb53ccc32014-02-18 14:11:36 +08001359{
1360 int ret;
zhanghailiang4c7e2512014-10-09 14:13:11 +08001361 Error *local_err = NULL;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001362
1363 /*
1364 * the kdump-compressed format is:
1365 * File offset
1366 * +------------------------------------------+ 0x0
1367 * | main header (struct disk_dump_header) |
1368 * |------------------------------------------+ block 1
1369 * | sub header (struct kdump_sub_header) |
1370 * |------------------------------------------+ block 2
1371 * | 1st-dump_bitmap |
1372 * |------------------------------------------+ block 2 + X blocks
1373 * | 2nd-dump_bitmap | (aligned by block)
1374 * |------------------------------------------+ block 2 + 2 * X blocks
1375 * | page desc for pfn 0 (struct page_desc) | (aligned by block)
1376 * | page desc for pfn 1 (struct page_desc) |
1377 * | : |
1378 * |------------------------------------------| (not aligned by block)
1379 * | page data (pfn 0) |
1380 * | page data (pfn 1) |
1381 * | : |
1382 * +------------------------------------------+
1383 */
1384
1385 ret = write_start_flat_header(s->fd);
1386 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001387 error_setg(errp, "dump: failed to write start flat header");
zhanghailiang4c7e2512014-10-09 14:13:11 +08001388 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001389 }
1390
zhanghailiang4c7e2512014-10-09 14:13:11 +08001391 write_dump_header(s, &local_err);
1392 if (local_err) {
1393 error_propagate(errp, local_err);
1394 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001395 }
1396
zhanghailiang4c7e2512014-10-09 14:13:11 +08001397 write_dump_bitmap(s, &local_err);
1398 if (local_err) {
1399 error_propagate(errp, local_err);
1400 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001401 }
1402
zhanghailiang4c7e2512014-10-09 14:13:11 +08001403 write_dump_pages(s, &local_err);
1404 if (local_err) {
1405 error_propagate(errp, local_err);
1406 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001407 }
1408
1409 ret = write_end_flat_header(s->fd);
1410 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001411 error_setg(errp, "dump: failed to write end flat header");
zhanghailiang4c7e2512014-10-09 14:13:11 +08001412 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001413 }
qiaonuohanb53ccc32014-02-18 14:11:36 +08001414}
1415
Wen Congyang783e9b42012-05-07 12:10:47 +08001416static ram_addr_t get_start_block(DumpState *s)
1417{
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001418 GuestPhysBlock *block;
Wen Congyang783e9b42012-05-07 12:10:47 +08001419
1420 if (!s->has_filter) {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001421 s->next_block = QTAILQ_FIRST(&s->guest_phys_blocks.head);
Wen Congyang783e9b42012-05-07 12:10:47 +08001422 return 0;
1423 }
1424
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001425 QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
1426 if (block->target_start >= s->begin + s->length ||
1427 block->target_end <= s->begin) {
Wen Congyang783e9b42012-05-07 12:10:47 +08001428 /* This block is out of the range */
1429 continue;
1430 }
1431
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001432 s->next_block = block;
1433 if (s->begin > block->target_start) {
1434 s->start = s->begin - block->target_start;
Wen Congyang783e9b42012-05-07 12:10:47 +08001435 } else {
1436 s->start = 0;
1437 }
1438 return s->start;
1439 }
1440
1441 return -1;
1442}
1443
qiaonuohan7aad2482014-02-18 14:11:31 +08001444static void get_max_mapnr(DumpState *s)
1445{
1446 GuestPhysBlock *last_block;
1447
1448 last_block = QTAILQ_LAST(&s->guest_phys_blocks.head, GuestPhysBlockHead);
Andrew Jones8161bef2016-01-11 20:56:20 +01001449 s->max_mapnr = dump_paddr_to_pfn(s, last_block->target_end);
qiaonuohan7aad2482014-02-18 14:11:31 +08001450}
1451
Peter Xubaf28f52016-02-18 13:16:48 +08001452static DumpState dump_state_global = { .status = DUMP_STATUS_NONE };
1453
1454static void dump_state_prepare(DumpState *s)
1455{
1456 /* zero the struct, setting status to active */
1457 *s = (DumpState) { .status = DUMP_STATUS_ACTIVE };
1458}
1459
Peter Xu65d64f32016-02-18 13:16:49 +08001460bool dump_in_progress(void)
1461{
1462 DumpState *state = &dump_state_global;
Peter Xu39ba2ea2016-02-18 13:16:54 +08001463 return (atomic_read(&state->status) == DUMP_STATUS_ACTIVE);
Peter Xu65d64f32016-02-18 13:16:49 +08001464}
1465
Peter Xu2264c2c2016-02-18 13:16:53 +08001466/* calculate total size of memory to be dumped (taking filter into
1467 * acoount.) */
1468static int64_t dump_calculate_size(DumpState *s)
1469{
1470 GuestPhysBlock *block;
1471 int64_t size = 0, total = 0, left = 0, right = 0;
1472
1473 QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
1474 if (s->has_filter) {
1475 /* calculate the overlapped region. */
1476 left = MAX(s->begin, block->target_start);
1477 right = MIN(s->begin + s->length, block->target_end);
1478 size = right - left;
1479 size = size > 0 ? size : 0;
1480 } else {
1481 /* count the whole region in */
1482 size = (block->target_end - block->target_start);
1483 }
1484 total += size;
1485 }
1486
1487 return total;
1488}
1489
zhanghailiang4c7e2512014-10-09 14:13:11 +08001490static void dump_init(DumpState *s, int fd, bool has_format,
1491 DumpGuestMemoryFormat format, bool paging, bool has_filter,
1492 int64_t begin, int64_t length, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +08001493{
Andreas Färber182735e2013-05-29 22:29:20 +02001494 CPUState *cpu;
Wen Congyang783e9b42012-05-07 12:10:47 +08001495 int nr_cpus;
Andreas Färber11ed09c2013-05-29 21:54:03 +02001496 Error *err = NULL;
Wen Congyang783e9b42012-05-07 12:10:47 +08001497 int ret;
1498
Peter Xuca1fc8c2016-02-18 13:16:50 +08001499 s->has_format = has_format;
1500 s->format = format;
Peter Xu2264c2c2016-02-18 13:16:53 +08001501 s->written_size = 0;
Peter Xuca1fc8c2016-02-18 13:16:50 +08001502
qiaonuohanb53ccc32014-02-18 14:11:36 +08001503 /* kdump-compressed is conflict with paging and filter */
1504 if (has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
1505 assert(!paging && !has_filter);
1506 }
1507
Wen Congyang783e9b42012-05-07 12:10:47 +08001508 if (runstate_is_running()) {
1509 vm_stop(RUN_STATE_SAVE_VM);
1510 s->resume = true;
1511 } else {
1512 s->resume = false;
1513 }
1514
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001515 /* If we use KVM, we should synchronize the registers before we get dump
1516 * info or physmap info.
Wen Congyang783e9b42012-05-07 12:10:47 +08001517 */
Andreas Färber1b3509c2013-06-09 16:48:29 +02001518 cpu_synchronize_all_states();
Wen Congyang783e9b42012-05-07 12:10:47 +08001519 nr_cpus = 0;
Andreas Färberbdc44642013-06-24 23:50:24 +02001520 CPU_FOREACH(cpu) {
Wen Congyang783e9b42012-05-07 12:10:47 +08001521 nr_cpus++;
1522 }
1523
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001524 s->fd = fd;
1525 s->has_filter = has_filter;
1526 s->begin = begin;
1527 s->length = length;
1528
Chen Gang29282072014-08-03 23:28:56 +08001529 memory_mapping_list_init(&s->list);
1530
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001531 guest_phys_blocks_init(&s->guest_phys_blocks);
Laszlo Ersekc5d7f602013-08-06 12:37:10 +02001532 guest_phys_blocks_append(&s->guest_phys_blocks);
Peter Xu2264c2c2016-02-18 13:16:53 +08001533 s->total_size = dump_calculate_size(s);
1534#ifdef DEBUG_DUMP_GUEST_MEMORY
1535 fprintf(stderr, "DUMP: total memory to dump: %lu\n", s->total_size);
1536#endif
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001537
Cornelia Huckd1e69942017-09-13 16:20:35 +02001538 /* it does not make sense to dump non-existent memory */
1539 if (!s->total_size) {
1540 error_setg(errp, "dump: no guest memory to dump");
1541 goto cleanup;
1542 }
1543
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001544 s->start = get_start_block(s);
1545 if (s->start == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001546 error_setg(errp, QERR_INVALID_PARAMETER, "begin");
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001547 goto cleanup;
1548 }
1549
1550 /* get dump info: endian, class and architecture.
1551 * If the target architecture is not supported, cpu_get_dump_info() will
1552 * return -1.
1553 */
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001554 ret = cpu_get_dump_info(&s->dump_info, &s->guest_phys_blocks);
Wen Congyang783e9b42012-05-07 12:10:47 +08001555 if (ret < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001556 error_setg(errp, QERR_UNSUPPORTED);
Wen Congyang783e9b42012-05-07 12:10:47 +08001557 goto cleanup;
1558 }
1559
Andrew Jones8161bef2016-01-11 20:56:20 +01001560 if (!s->dump_info.page_size) {
1561 s->dump_info.page_size = TARGET_PAGE_SIZE;
1562 }
1563
Paolo Bonzini4720bd02012-06-07 08:48:09 +02001564 s->note_size = cpu_get_note_size(s->dump_info.d_class,
1565 s->dump_info.d_machine, nr_cpus);
Aneesh Kumar K.Vbb6b6842013-10-01 21:49:32 +05301566 if (s->note_size < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001567 error_setg(errp, QERR_UNSUPPORTED);
Paolo Bonzini4720bd02012-06-07 08:48:09 +02001568 goto cleanup;
1569 }
1570
Wen Congyang783e9b42012-05-07 12:10:47 +08001571 /* get memory mapping */
Wen Congyang783e9b42012-05-07 12:10:47 +08001572 if (paging) {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001573 qemu_get_guest_memory_mapping(&s->list, &s->guest_phys_blocks, &err);
Andreas Färber11ed09c2013-05-29 21:54:03 +02001574 if (err != NULL) {
1575 error_propagate(errp, err);
1576 goto cleanup;
1577 }
Wen Congyang783e9b42012-05-07 12:10:47 +08001578 } else {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001579 qemu_get_guest_simple_memory_mapping(&s->list, &s->guest_phys_blocks);
Wen Congyang783e9b42012-05-07 12:10:47 +08001580 }
1581
qiaonuohan7aad2482014-02-18 14:11:31 +08001582 s->nr_cpus = nr_cpus;
qiaonuohan7aad2482014-02-18 14:11:31 +08001583
1584 get_max_mapnr(s);
1585
1586 uint64_t tmp;
Andrew Jones8161bef2016-01-11 20:56:20 +01001587 tmp = DIV_ROUND_UP(DIV_ROUND_UP(s->max_mapnr, CHAR_BIT),
1588 s->dump_info.page_size);
1589 s->len_dump_bitmap = tmp * s->dump_info.page_size;
qiaonuohan7aad2482014-02-18 14:11:31 +08001590
qiaonuohanb53ccc32014-02-18 14:11:36 +08001591 /* init for kdump-compressed format */
1592 if (has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
1593 switch (format) {
1594 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB:
1595 s->flag_compress = DUMP_DH_COMPRESSED_ZLIB;
1596 break;
1597
1598 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO:
Laszlo Ersekc998acb2014-05-20 13:42:22 +02001599#ifdef CONFIG_LZO
1600 if (lzo_init() != LZO_E_OK) {
1601 error_setg(errp, "failed to initialize the LZO library");
1602 goto cleanup;
1603 }
1604#endif
qiaonuohanb53ccc32014-02-18 14:11:36 +08001605 s->flag_compress = DUMP_DH_COMPRESSED_LZO;
1606 break;
1607
1608 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY:
1609 s->flag_compress = DUMP_DH_COMPRESSED_SNAPPY;
1610 break;
1611
1612 default:
1613 s->flag_compress = 0;
1614 }
1615
zhanghailiang4c7e2512014-10-09 14:13:11 +08001616 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001617 }
1618
Wen Congyang783e9b42012-05-07 12:10:47 +08001619 if (s->has_filter) {
1620 memory_mapping_filter(&s->list, s->begin, s->length);
1621 }
1622
1623 /*
1624 * calculate phdr_num
1625 *
1626 * the type of ehdr->e_phnum is uint16_t, so we should avoid overflow
1627 */
1628 s->phdr_num = 1; /* PT_NOTE */
1629 if (s->list.num < UINT16_MAX - 2) {
1630 s->phdr_num += s->list.num;
1631 s->have_section = false;
1632 } else {
1633 s->have_section = true;
1634 s->phdr_num = PN_XNUM;
1635 s->sh_info = 1; /* PT_NOTE */
1636
1637 /* the type of shdr->sh_info is uint32_t, so we should avoid overflow */
1638 if (s->list.num <= UINT32_MAX - 1) {
1639 s->sh_info += s->list.num;
1640 } else {
1641 s->sh_info = UINT32_MAX;
1642 }
1643 }
1644
Wen Congyang783e9b42012-05-07 12:10:47 +08001645 if (s->dump_info.d_class == ELFCLASS64) {
1646 if (s->have_section) {
1647 s->memory_offset = sizeof(Elf64_Ehdr) +
1648 sizeof(Elf64_Phdr) * s->sh_info +
1649 sizeof(Elf64_Shdr) + s->note_size;
1650 } else {
1651 s->memory_offset = sizeof(Elf64_Ehdr) +
1652 sizeof(Elf64_Phdr) * s->phdr_num + s->note_size;
1653 }
1654 } else {
1655 if (s->have_section) {
1656 s->memory_offset = sizeof(Elf32_Ehdr) +
1657 sizeof(Elf32_Phdr) * s->sh_info +
1658 sizeof(Elf32_Shdr) + s->note_size;
1659 } else {
1660 s->memory_offset = sizeof(Elf32_Ehdr) +
1661 sizeof(Elf32_Phdr) * s->phdr_num + s->note_size;
1662 }
1663 }
1664
zhanghailiang4c7e2512014-10-09 14:13:11 +08001665 return;
Wen Congyang783e9b42012-05-07 12:10:47 +08001666
1667cleanup:
Chen Gang29282072014-08-03 23:28:56 +08001668 dump_cleanup(s);
Wen Congyang783e9b42012-05-07 12:10:47 +08001669}
1670
Peter Xuca1fc8c2016-02-18 13:16:50 +08001671/* this operation might be time consuming. */
1672static void dump_process(DumpState *s, Error **errp)
1673{
1674 Error *local_err = NULL;
Peter Xud42a0d12016-02-18 13:16:56 +08001675 DumpQueryResult *result = NULL;
Peter Xuca1fc8c2016-02-18 13:16:50 +08001676
1677 if (s->has_format && s->format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
1678 create_kdump_vmcore(s, &local_err);
1679 } else {
1680 create_vmcore(s, &local_err);
1681 }
1682
Peter Xu39ba2ea2016-02-18 13:16:54 +08001683 /* make sure status is written after written_size updates */
1684 smp_wmb();
1685 atomic_set(&s->status,
1686 (local_err ? DUMP_STATUS_FAILED : DUMP_STATUS_COMPLETED));
Peter Xuca1fc8c2016-02-18 13:16:50 +08001687
Peter Xud42a0d12016-02-18 13:16:56 +08001688 /* send DUMP_COMPLETED message (unconditionally) */
1689 result = qmp_query_dump(NULL);
1690 /* should never fail */
1691 assert(result);
1692 qapi_event_send_dump_completed(result, !!local_err, (local_err ? \
1693 error_get_pretty(local_err) : NULL),
1694 &error_abort);
1695 qapi_free_DumpQueryResult(result);
1696
Peter Xu39ba2ea2016-02-18 13:16:54 +08001697 error_propagate(errp, local_err);
Peter Xuca1fc8c2016-02-18 13:16:50 +08001698 dump_cleanup(s);
1699}
1700
Peter Xu1fbeff72016-02-18 13:16:52 +08001701static void *dump_thread(void *data)
1702{
Peter Xu1fbeff72016-02-18 13:16:52 +08001703 DumpState *s = (DumpState *)data;
Alberto Garciac3a8fe32017-08-29 15:08:36 +03001704 dump_process(s, NULL);
Peter Xu1fbeff72016-02-18 13:16:52 +08001705 return NULL;
1706}
1707
Peter Xu39ba2ea2016-02-18 13:16:54 +08001708DumpQueryResult *qmp_query_dump(Error **errp)
1709{
1710 DumpQueryResult *result = g_new(DumpQueryResult, 1);
1711 DumpState *state = &dump_state_global;
1712 result->status = atomic_read(&state->status);
1713 /* make sure we are reading status and written_size in order */
1714 smp_rmb();
1715 result->completed = state->written_size;
1716 result->total = state->total_size;
1717 return result;
1718}
1719
Peter Xu228de9c2016-02-18 13:16:47 +08001720void qmp_dump_guest_memory(bool paging, const char *file,
1721 bool has_detach, bool detach,
1722 bool has_begin, int64_t begin, bool has_length,
qiaonuohanb53ccc32014-02-18 14:11:36 +08001723 int64_t length, bool has_format,
1724 DumpGuestMemoryFormat format, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +08001725{
1726 const char *p;
1727 int fd = -1;
1728 DumpState *s;
zhanghailiang4c7e2512014-10-09 14:13:11 +08001729 Error *local_err = NULL;
Peter Xu1fbeff72016-02-18 13:16:52 +08001730 bool detach_p = false;
Wen Congyang783e9b42012-05-07 12:10:47 +08001731
Peter Xu63e27f22016-02-18 13:16:51 +08001732 if (runstate_check(RUN_STATE_INMIGRATE)) {
1733 error_setg(errp, "Dump not allowed during incoming migration.");
1734 return;
1735 }
1736
Peter Xu65d64f32016-02-18 13:16:49 +08001737 /* if there is a dump in background, we should wait until the dump
1738 * finished */
1739 if (dump_in_progress()) {
1740 error_setg(errp, "There is a dump in process, please wait.");
1741 return;
1742 }
1743
qiaonuohanb53ccc32014-02-18 14:11:36 +08001744 /*
1745 * kdump-compressed format need the whole memory dumped, so paging or
1746 * filter is not supported here.
1747 */
1748 if ((has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) &&
1749 (paging || has_begin || has_length)) {
1750 error_setg(errp, "kdump-compressed format doesn't support paging or "
1751 "filter");
1752 return;
1753 }
Wen Congyang783e9b42012-05-07 12:10:47 +08001754 if (has_begin && !has_length) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001755 error_setg(errp, QERR_MISSING_PARAMETER, "length");
Wen Congyang783e9b42012-05-07 12:10:47 +08001756 return;
1757 }
1758 if (!has_begin && has_length) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001759 error_setg(errp, QERR_MISSING_PARAMETER, "begin");
Wen Congyang783e9b42012-05-07 12:10:47 +08001760 return;
1761 }
Peter Xu1fbeff72016-02-18 13:16:52 +08001762 if (has_detach) {
1763 detach_p = detach;
1764 }
Wen Congyang783e9b42012-05-07 12:10:47 +08001765
qiaonuohanb53ccc32014-02-18 14:11:36 +08001766 /* check whether lzo/snappy is supported */
1767#ifndef CONFIG_LZO
1768 if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO) {
1769 error_setg(errp, "kdump-lzo is not available now");
1770 return;
1771 }
1772#endif
1773
1774#ifndef CONFIG_SNAPPY
1775 if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY) {
1776 error_setg(errp, "kdump-snappy is not available now");
1777 return;
1778 }
1779#endif
1780
Wen Congyang783e9b42012-05-07 12:10:47 +08001781#if !defined(WIN32)
1782 if (strstart(file, "fd:", &p)) {
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02001783 fd = monitor_get_fd(cur_mon, p, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +08001784 if (fd == -1) {
Wen Congyang783e9b42012-05-07 12:10:47 +08001785 return;
1786 }
1787 }
1788#endif
1789
1790 if (strstart(file, "file:", &p)) {
1791 fd = qemu_open(p, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, S_IRUSR);
1792 if (fd < 0) {
Luiz Capitulino75817662013-06-07 14:36:01 -04001793 error_setg_file_open(errp, errno, p);
Wen Congyang783e9b42012-05-07 12:10:47 +08001794 return;
1795 }
1796 }
1797
1798 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001799 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
Wen Congyang783e9b42012-05-07 12:10:47 +08001800 return;
1801 }
1802
Peter Xubaf28f52016-02-18 13:16:48 +08001803 s = &dump_state_global;
1804 dump_state_prepare(s);
Wen Congyang783e9b42012-05-07 12:10:47 +08001805
zhanghailiang4c7e2512014-10-09 14:13:11 +08001806 dump_init(s, fd, has_format, format, paging, has_begin,
1807 begin, length, &local_err);
1808 if (local_err) {
zhanghailiang4c7e2512014-10-09 14:13:11 +08001809 error_propagate(errp, local_err);
Peter Xu39ba2ea2016-02-18 13:16:54 +08001810 atomic_set(&s->status, DUMP_STATUS_FAILED);
Wen Congyang783e9b42012-05-07 12:10:47 +08001811 return;
1812 }
1813
Peter Xu1fbeff72016-02-18 13:16:52 +08001814 if (detach_p) {
1815 /* detached dump */
Fam Zheng6796b402017-05-03 15:28:19 +08001816 s->detached = true;
Peter Xu1fbeff72016-02-18 13:16:52 +08001817 qemu_thread_create(&s->dump_thread, "dump_thread", dump_thread,
1818 s, QEMU_THREAD_DETACHED);
1819 } else {
1820 /* sync dump */
1821 dump_process(s, errp);
1822 }
Wen Congyang783e9b42012-05-07 12:10:47 +08001823}
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08001824
1825DumpGuestMemoryCapability *qmp_query_dump_guest_memory_capability(Error **errp)
1826{
1827 DumpGuestMemoryFormatList *item;
1828 DumpGuestMemoryCapability *cap =
1829 g_malloc0(sizeof(DumpGuestMemoryCapability));
1830
1831 /* elf is always available */
1832 item = g_malloc0(sizeof(DumpGuestMemoryFormatList));
1833 cap->formats = item;
1834 item->value = DUMP_GUEST_MEMORY_FORMAT_ELF;
1835
1836 /* kdump-zlib is always available */
1837 item->next = g_malloc0(sizeof(DumpGuestMemoryFormatList));
1838 item = item->next;
1839 item->value = DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB;
1840
1841 /* add new item if kdump-lzo is available */
1842#ifdef CONFIG_LZO
1843 item->next = g_malloc0(sizeof(DumpGuestMemoryFormatList));
1844 item = item->next;
1845 item->value = DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO;
1846#endif
1847
1848 /* add new item if kdump-snappy is available */
1849#ifdef CONFIG_SNAPPY
1850 item->next = g_malloc0(sizeof(DumpGuestMemoryFormatList));
1851 item = item->next;
1852 item->value = DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY;
1853#endif
1854
1855 return cap;
1856}