blob: 6dec8d2cf1859e37984fa104e66a3cc5ae1bb824 [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
14#include "qemu-common.h"
Wen Congyang783e9b42012-05-07 12:10:47 +080015#include "elf.h"
Wen Congyang783e9b42012-05-07 12:10:47 +080016#include "cpu.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010017#include "exec/cpu-all.h"
18#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"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010025#include "qapi/error.h"
Wen Congyang783e9b42012-05-07 12:10:47 +080026#include "qmp-commands.h"
Wen Congyang783e9b42012-05-07 12:10:47 +080027
qiaonuohand12f57e2014-02-18 14:11:35 +080028#include <zlib.h>
29#ifdef CONFIG_LZO
30#include <lzo/lzo1x.h>
31#endif
32#ifdef CONFIG_SNAPPY
33#include <snappy-c.h>
34#endif
qiaonuohan4ab23a92014-02-18 14:11:37 +080035#ifndef ELF_MACHINE_UNAME
36#define ELF_MACHINE_UNAME "Unknown"
37#endif
qiaonuohand12f57e2014-02-18 14:11:35 +080038
Wen Congyang783e9b42012-05-07 12:10:47 +080039static uint16_t cpu_convert_to_target16(uint16_t val, int endian)
40{
41 if (endian == ELFDATA2LSB) {
42 val = cpu_to_le16(val);
43 } else {
44 val = cpu_to_be16(val);
45 }
46
47 return val;
48}
49
50static uint32_t cpu_convert_to_target32(uint32_t val, int endian)
51{
52 if (endian == ELFDATA2LSB) {
53 val = cpu_to_le32(val);
54 } else {
55 val = cpu_to_be32(val);
56 }
57
58 return val;
59}
60
61static uint64_t cpu_convert_to_target64(uint64_t val, int endian)
62{
63 if (endian == ELFDATA2LSB) {
64 val = cpu_to_le64(val);
65 } else {
66 val = cpu_to_be64(val);
67 }
68
69 return val;
70}
71
72typedef struct DumpState {
Laszlo Ersek5ee163e2013-08-06 12:37:09 +020073 GuestPhysBlockList guest_phys_blocks;
Wen Congyang783e9b42012-05-07 12:10:47 +080074 ArchDumpInfo dump_info;
75 MemoryMappingList list;
76 uint16_t phdr_num;
77 uint32_t sh_info;
78 bool have_section;
79 bool resume;
Aneesh Kumar K.Vbb6b6842013-10-01 21:49:32 +053080 ssize_t note_size;
Avi Kivitya8170e52012-10-23 12:30:10 +020081 hwaddr memory_offset;
Wen Congyang783e9b42012-05-07 12:10:47 +080082 int fd;
83
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +020084 GuestPhysBlock *next_block;
Wen Congyang783e9b42012-05-07 12:10:47 +080085 ram_addr_t start;
86 bool has_filter;
87 int64_t begin;
88 int64_t length;
qiaonuohan4835ef72014-02-18 14:11:29 +080089
90 uint8_t *note_buf; /* buffer for notes */
91 size_t note_buf_offset; /* the writing place in note_buf */
qiaonuohan7aad2482014-02-18 14:11:31 +080092 uint32_t nr_cpus; /* number of guest's cpu */
93 size_t page_size; /* guest's page size */
94 uint32_t page_shift; /* guest's page shift */
95 uint64_t max_mapnr; /* the biggest guest's phys-mem's number */
96 size_t len_dump_bitmap; /* the size of the place used to store
97 dump_bitmap in vmcore */
98 off_t offset_dump_bitmap; /* offset of dump_bitmap part in vmcore */
99 off_t offset_page; /* offset of page part in vmcore */
100 size_t num_dumpable; /* number of page that can be dumped */
101 uint32_t flag_compress; /* indicate the compression format */
Wen Congyang783e9b42012-05-07 12:10:47 +0800102} DumpState;
103
104static int dump_cleanup(DumpState *s)
105{
106 int ret = 0;
107
Laszlo Ersek5ee163e2013-08-06 12:37:09 +0200108 guest_phys_blocks_free(&s->guest_phys_blocks);
Wen Congyang783e9b42012-05-07 12:10:47 +0800109 memory_mapping_list_free(&s->list);
110 if (s->fd != -1) {
111 close(s->fd);
112 }
113 if (s->resume) {
114 vm_start();
115 }
116
117 return ret;
118}
119
120static void dump_error(DumpState *s, const char *reason)
121{
122 dump_cleanup(s);
123}
124
qiaonuohanb5ba1cc2014-02-18 14:11:25 +0800125static int fd_write_vmcore(const void *buf, size_t size, void *opaque)
Wen Congyang783e9b42012-05-07 12:10:47 +0800126{
127 DumpState *s = opaque;
Luiz Capitulino2f616522012-09-21 13:17:55 -0300128 size_t written_size;
Wen Congyang783e9b42012-05-07 12:10:47 +0800129
Luiz Capitulino2f616522012-09-21 13:17:55 -0300130 written_size = qemu_write_full(s->fd, buf, size);
131 if (written_size != size) {
132 return -1;
Wen Congyang783e9b42012-05-07 12:10:47 +0800133 }
134
135 return 0;
136}
137
138static int write_elf64_header(DumpState *s)
139{
140 Elf64_Ehdr elf_header;
141 int ret;
142 int endian = s->dump_info.d_endian;
143
144 memset(&elf_header, 0, sizeof(Elf64_Ehdr));
145 memcpy(&elf_header, ELFMAG, SELFMAG);
146 elf_header.e_ident[EI_CLASS] = ELFCLASS64;
147 elf_header.e_ident[EI_DATA] = s->dump_info.d_endian;
148 elf_header.e_ident[EI_VERSION] = EV_CURRENT;
149 elf_header.e_type = cpu_convert_to_target16(ET_CORE, endian);
150 elf_header.e_machine = cpu_convert_to_target16(s->dump_info.d_machine,
151 endian);
152 elf_header.e_version = cpu_convert_to_target32(EV_CURRENT, endian);
153 elf_header.e_ehsize = cpu_convert_to_target16(sizeof(elf_header), endian);
154 elf_header.e_phoff = cpu_convert_to_target64(sizeof(Elf64_Ehdr), endian);
155 elf_header.e_phentsize = cpu_convert_to_target16(sizeof(Elf64_Phdr),
156 endian);
157 elf_header.e_phnum = cpu_convert_to_target16(s->phdr_num, endian);
158 if (s->have_section) {
159 uint64_t shoff = sizeof(Elf64_Ehdr) + sizeof(Elf64_Phdr) * s->sh_info;
160
161 elf_header.e_shoff = cpu_convert_to_target64(shoff, endian);
162 elf_header.e_shentsize = cpu_convert_to_target16(sizeof(Elf64_Shdr),
163 endian);
164 elf_header.e_shnum = cpu_convert_to_target16(1, endian);
165 }
166
167 ret = fd_write_vmcore(&elf_header, sizeof(elf_header), s);
168 if (ret < 0) {
169 dump_error(s, "dump: failed to write elf header.\n");
170 return -1;
171 }
172
173 return 0;
174}
175
176static int write_elf32_header(DumpState *s)
177{
178 Elf32_Ehdr elf_header;
179 int ret;
180 int endian = s->dump_info.d_endian;
181
182 memset(&elf_header, 0, sizeof(Elf32_Ehdr));
183 memcpy(&elf_header, ELFMAG, SELFMAG);
184 elf_header.e_ident[EI_CLASS] = ELFCLASS32;
185 elf_header.e_ident[EI_DATA] = endian;
186 elf_header.e_ident[EI_VERSION] = EV_CURRENT;
187 elf_header.e_type = cpu_convert_to_target16(ET_CORE, endian);
188 elf_header.e_machine = cpu_convert_to_target16(s->dump_info.d_machine,
189 endian);
190 elf_header.e_version = cpu_convert_to_target32(EV_CURRENT, endian);
191 elf_header.e_ehsize = cpu_convert_to_target16(sizeof(elf_header), endian);
192 elf_header.e_phoff = cpu_convert_to_target32(sizeof(Elf32_Ehdr), endian);
193 elf_header.e_phentsize = cpu_convert_to_target16(sizeof(Elf32_Phdr),
194 endian);
195 elf_header.e_phnum = cpu_convert_to_target16(s->phdr_num, endian);
196 if (s->have_section) {
197 uint32_t shoff = sizeof(Elf32_Ehdr) + sizeof(Elf32_Phdr) * s->sh_info;
198
199 elf_header.e_shoff = cpu_convert_to_target32(shoff, endian);
200 elf_header.e_shentsize = cpu_convert_to_target16(sizeof(Elf32_Shdr),
201 endian);
202 elf_header.e_shnum = cpu_convert_to_target16(1, endian);
203 }
204
205 ret = fd_write_vmcore(&elf_header, sizeof(elf_header), s);
206 if (ret < 0) {
207 dump_error(s, "dump: failed to write elf header.\n");
208 return -1;
209 }
210
211 return 0;
212}
213
214static int write_elf64_load(DumpState *s, MemoryMapping *memory_mapping,
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200215 int phdr_index, hwaddr offset,
216 hwaddr filesz)
Wen Congyang783e9b42012-05-07 12:10:47 +0800217{
218 Elf64_Phdr phdr;
219 int ret;
220 int endian = s->dump_info.d_endian;
221
222 memset(&phdr, 0, sizeof(Elf64_Phdr));
223 phdr.p_type = cpu_convert_to_target32(PT_LOAD, endian);
224 phdr.p_offset = cpu_convert_to_target64(offset, endian);
225 phdr.p_paddr = cpu_convert_to_target64(memory_mapping->phys_addr, endian);
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200226 phdr.p_filesz = cpu_convert_to_target64(filesz, endian);
Wen Congyang783e9b42012-05-07 12:10:47 +0800227 phdr.p_memsz = cpu_convert_to_target64(memory_mapping->length, endian);
228 phdr.p_vaddr = cpu_convert_to_target64(memory_mapping->virt_addr, endian);
229
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200230 assert(memory_mapping->length >= filesz);
231
Wen Congyang783e9b42012-05-07 12:10:47 +0800232 ret = fd_write_vmcore(&phdr, sizeof(Elf64_Phdr), s);
233 if (ret < 0) {
234 dump_error(s, "dump: failed to write program header table.\n");
235 return -1;
236 }
237
238 return 0;
239}
240
241static int write_elf32_load(DumpState *s, MemoryMapping *memory_mapping,
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200242 int phdr_index, hwaddr offset,
243 hwaddr filesz)
Wen Congyang783e9b42012-05-07 12:10:47 +0800244{
245 Elf32_Phdr phdr;
246 int ret;
247 int endian = s->dump_info.d_endian;
248
249 memset(&phdr, 0, sizeof(Elf32_Phdr));
250 phdr.p_type = cpu_convert_to_target32(PT_LOAD, endian);
251 phdr.p_offset = cpu_convert_to_target32(offset, endian);
252 phdr.p_paddr = cpu_convert_to_target32(memory_mapping->phys_addr, endian);
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200253 phdr.p_filesz = cpu_convert_to_target32(filesz, endian);
Wen Congyang783e9b42012-05-07 12:10:47 +0800254 phdr.p_memsz = cpu_convert_to_target32(memory_mapping->length, endian);
255 phdr.p_vaddr = cpu_convert_to_target32(memory_mapping->virt_addr, endian);
256
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200257 assert(memory_mapping->length >= filesz);
258
Wen Congyang783e9b42012-05-07 12:10:47 +0800259 ret = fd_write_vmcore(&phdr, sizeof(Elf32_Phdr), s);
260 if (ret < 0) {
261 dump_error(s, "dump: failed to write program header table.\n");
262 return -1;
263 }
264
265 return 0;
266}
267
268static int write_elf64_note(DumpState *s)
269{
270 Elf64_Phdr phdr;
271 int endian = s->dump_info.d_endian;
Avi Kivitya8170e52012-10-23 12:30:10 +0200272 hwaddr begin = s->memory_offset - s->note_size;
Wen Congyang783e9b42012-05-07 12:10:47 +0800273 int ret;
274
275 memset(&phdr, 0, sizeof(Elf64_Phdr));
276 phdr.p_type = cpu_convert_to_target32(PT_NOTE, endian);
277 phdr.p_offset = cpu_convert_to_target64(begin, endian);
278 phdr.p_paddr = 0;
279 phdr.p_filesz = cpu_convert_to_target64(s->note_size, endian);
280 phdr.p_memsz = cpu_convert_to_target64(s->note_size, endian);
281 phdr.p_vaddr = 0;
282
283 ret = fd_write_vmcore(&phdr, sizeof(Elf64_Phdr), s);
284 if (ret < 0) {
285 dump_error(s, "dump: failed to write program header table.\n");
286 return -1;
287 }
288
289 return 0;
290}
291
Paolo Bonzini0bc3cd62013-04-08 17:29:59 +0200292static inline int cpu_index(CPUState *cpu)
293{
294 return cpu->cpu_index + 1;
295}
296
qiaonuohan6a519912014-02-18 14:11:26 +0800297static int write_elf64_notes(WriteCoreDumpFunction f, DumpState *s)
Wen Congyang783e9b42012-05-07 12:10:47 +0800298{
Andreas Färber0d342822012-12-17 07:12:13 +0100299 CPUState *cpu;
Wen Congyang783e9b42012-05-07 12:10:47 +0800300 int ret;
301 int id;
302
Andreas Färberbdc44642013-06-24 23:50:24 +0200303 CPU_FOREACH(cpu) {
Andreas Färber0d342822012-12-17 07:12:13 +0100304 id = cpu_index(cpu);
qiaonuohan6a519912014-02-18 14:11:26 +0800305 ret = cpu_write_elf64_note(f, cpu, id, s);
Wen Congyang783e9b42012-05-07 12:10:47 +0800306 if (ret < 0) {
307 dump_error(s, "dump: failed to write elf notes.\n");
308 return -1;
309 }
310 }
311
Andreas Färberbdc44642013-06-24 23:50:24 +0200312 CPU_FOREACH(cpu) {
qiaonuohan6a519912014-02-18 14:11:26 +0800313 ret = cpu_write_elf64_qemunote(f, cpu, s);
Wen Congyang783e9b42012-05-07 12:10:47 +0800314 if (ret < 0) {
315 dump_error(s, "dump: failed to write CPU status.\n");
316 return -1;
317 }
318 }
319
320 return 0;
321}
322
323static int write_elf32_note(DumpState *s)
324{
Avi Kivitya8170e52012-10-23 12:30:10 +0200325 hwaddr begin = s->memory_offset - s->note_size;
Wen Congyang783e9b42012-05-07 12:10:47 +0800326 Elf32_Phdr phdr;
327 int endian = s->dump_info.d_endian;
328 int ret;
329
330 memset(&phdr, 0, sizeof(Elf32_Phdr));
331 phdr.p_type = cpu_convert_to_target32(PT_NOTE, endian);
332 phdr.p_offset = cpu_convert_to_target32(begin, endian);
333 phdr.p_paddr = 0;
334 phdr.p_filesz = cpu_convert_to_target32(s->note_size, endian);
335 phdr.p_memsz = cpu_convert_to_target32(s->note_size, endian);
336 phdr.p_vaddr = 0;
337
338 ret = fd_write_vmcore(&phdr, sizeof(Elf32_Phdr), s);
339 if (ret < 0) {
340 dump_error(s, "dump: failed to write program header table.\n");
341 return -1;
342 }
343
344 return 0;
345}
346
qiaonuohan6a519912014-02-18 14:11:26 +0800347static int write_elf32_notes(WriteCoreDumpFunction f, DumpState *s)
Wen Congyang783e9b42012-05-07 12:10:47 +0800348{
Andreas Färber0d342822012-12-17 07:12:13 +0100349 CPUState *cpu;
Wen Congyang783e9b42012-05-07 12:10:47 +0800350 int ret;
351 int id;
352
Andreas Färberbdc44642013-06-24 23:50:24 +0200353 CPU_FOREACH(cpu) {
Andreas Färber0d342822012-12-17 07:12:13 +0100354 id = cpu_index(cpu);
qiaonuohan6a519912014-02-18 14:11:26 +0800355 ret = cpu_write_elf32_note(f, cpu, id, s);
Wen Congyang783e9b42012-05-07 12:10:47 +0800356 if (ret < 0) {
357 dump_error(s, "dump: failed to write elf notes.\n");
358 return -1;
359 }
360 }
361
Andreas Färberbdc44642013-06-24 23:50:24 +0200362 CPU_FOREACH(cpu) {
qiaonuohan6a519912014-02-18 14:11:26 +0800363 ret = cpu_write_elf32_qemunote(f, cpu, s);
Wen Congyang783e9b42012-05-07 12:10:47 +0800364 if (ret < 0) {
365 dump_error(s, "dump: failed to write CPU status.\n");
366 return -1;
367 }
368 }
369
370 return 0;
371}
372
373static int write_elf_section(DumpState *s, int type)
374{
375 Elf32_Shdr shdr32;
376 Elf64_Shdr shdr64;
377 int endian = s->dump_info.d_endian;
378 int shdr_size;
379 void *shdr;
380 int ret;
381
382 if (type == 0) {
383 shdr_size = sizeof(Elf32_Shdr);
384 memset(&shdr32, 0, shdr_size);
385 shdr32.sh_info = cpu_convert_to_target32(s->sh_info, endian);
386 shdr = &shdr32;
387 } else {
388 shdr_size = sizeof(Elf64_Shdr);
389 memset(&shdr64, 0, shdr_size);
390 shdr64.sh_info = cpu_convert_to_target32(s->sh_info, endian);
391 shdr = &shdr64;
392 }
393
394 ret = fd_write_vmcore(&shdr, shdr_size, s);
395 if (ret < 0) {
396 dump_error(s, "dump: failed to write section header table.\n");
397 return -1;
398 }
399
400 return 0;
401}
402
403static int write_data(DumpState *s, void *buf, int length)
404{
405 int ret;
406
407 ret = fd_write_vmcore(buf, length, s);
408 if (ret < 0) {
409 dump_error(s, "dump: failed to save memory.\n");
410 return -1;
411 }
412
413 return 0;
414}
415
416/* write the memroy to vmcore. 1 page per I/O. */
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200417static int write_memory(DumpState *s, GuestPhysBlock *block, ram_addr_t start,
Wen Congyang783e9b42012-05-07 12:10:47 +0800418 int64_t size)
419{
420 int64_t i;
421 int ret;
422
423 for (i = 0; i < size / TARGET_PAGE_SIZE; i++) {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200424 ret = write_data(s, block->host_addr + start + i * TARGET_PAGE_SIZE,
Wen Congyang783e9b42012-05-07 12:10:47 +0800425 TARGET_PAGE_SIZE);
426 if (ret < 0) {
427 return ret;
428 }
429 }
430
431 if ((size % TARGET_PAGE_SIZE) != 0) {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200432 ret = write_data(s, block->host_addr + start + i * TARGET_PAGE_SIZE,
Wen Congyang783e9b42012-05-07 12:10:47 +0800433 size % TARGET_PAGE_SIZE);
434 if (ret < 0) {
435 return ret;
436 }
437 }
438
439 return 0;
440}
441
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200442/* get the memory's offset and size in the vmcore */
443static void get_offset_range(hwaddr phys_addr,
444 ram_addr_t mapping_length,
445 DumpState *s,
446 hwaddr *p_offset,
447 hwaddr *p_filesz)
Wen Congyang783e9b42012-05-07 12:10:47 +0800448{
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200449 GuestPhysBlock *block;
Avi Kivitya8170e52012-10-23 12:30:10 +0200450 hwaddr offset = s->memory_offset;
Wen Congyang783e9b42012-05-07 12:10:47 +0800451 int64_t size_in_block, start;
452
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200453 /* When the memory is not stored into vmcore, offset will be -1 */
454 *p_offset = -1;
455 *p_filesz = 0;
456
Wen Congyang783e9b42012-05-07 12:10:47 +0800457 if (s->has_filter) {
458 if (phys_addr < s->begin || phys_addr >= s->begin + s->length) {
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200459 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800460 }
461 }
462
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200463 QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
Wen Congyang783e9b42012-05-07 12:10:47 +0800464 if (s->has_filter) {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200465 if (block->target_start >= s->begin + s->length ||
466 block->target_end <= s->begin) {
Wen Congyang783e9b42012-05-07 12:10:47 +0800467 /* This block is out of the range */
468 continue;
469 }
470
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200471 if (s->begin <= block->target_start) {
472 start = block->target_start;
Wen Congyang783e9b42012-05-07 12:10:47 +0800473 } else {
474 start = s->begin;
475 }
476
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200477 size_in_block = block->target_end - start;
478 if (s->begin + s->length < block->target_end) {
479 size_in_block -= block->target_end - (s->begin + s->length);
Wen Congyang783e9b42012-05-07 12:10:47 +0800480 }
481 } else {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200482 start = block->target_start;
483 size_in_block = block->target_end - block->target_start;
Wen Congyang783e9b42012-05-07 12:10:47 +0800484 }
485
486 if (phys_addr >= start && phys_addr < start + size_in_block) {
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200487 *p_offset = phys_addr - start + offset;
488
489 /* The offset range mapped from the vmcore file must not spill over
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200490 * the GuestPhysBlock, clamp it. The rest of the mapping will be
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200491 * zero-filled in memory at load time; see
492 * <http://refspecs.linuxbase.org/elf/gabi4+/ch5.pheader.html>.
493 */
494 *p_filesz = phys_addr + mapping_length <= start + size_in_block ?
495 mapping_length :
496 size_in_block - (phys_addr - start);
497 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800498 }
499
500 offset += size_in_block;
501 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800502}
503
504static int write_elf_loads(DumpState *s)
505{
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200506 hwaddr offset, filesz;
Wen Congyang783e9b42012-05-07 12:10:47 +0800507 MemoryMapping *memory_mapping;
508 uint32_t phdr_index = 1;
509 int ret;
510 uint32_t max_index;
511
512 if (s->have_section) {
513 max_index = s->sh_info;
514 } else {
515 max_index = s->phdr_num;
516 }
517
518 QTAILQ_FOREACH(memory_mapping, &s->list.head, next) {
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200519 get_offset_range(memory_mapping->phys_addr,
520 memory_mapping->length,
521 s, &offset, &filesz);
Wen Congyang783e9b42012-05-07 12:10:47 +0800522 if (s->dump_info.d_class == ELFCLASS64) {
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200523 ret = write_elf64_load(s, memory_mapping, phdr_index++, offset,
524 filesz);
Wen Congyang783e9b42012-05-07 12:10:47 +0800525 } else {
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200526 ret = write_elf32_load(s, memory_mapping, phdr_index++, offset,
527 filesz);
Wen Congyang783e9b42012-05-07 12:10:47 +0800528 }
529
530 if (ret < 0) {
531 return -1;
532 }
533
534 if (phdr_index >= max_index) {
535 break;
536 }
537 }
538
539 return 0;
540}
541
542/* write elf header, PT_NOTE and elf note to vmcore. */
543static int dump_begin(DumpState *s)
544{
545 int ret;
546
547 /*
548 * the vmcore's format is:
549 * --------------
550 * | elf header |
551 * --------------
552 * | PT_NOTE |
553 * --------------
554 * | PT_LOAD |
555 * --------------
556 * | ...... |
557 * --------------
558 * | PT_LOAD |
559 * --------------
560 * | sec_hdr |
561 * --------------
562 * | elf note |
563 * --------------
564 * | memory |
565 * --------------
566 *
567 * we only know where the memory is saved after we write elf note into
568 * vmcore.
569 */
570
571 /* write elf header to vmcore */
572 if (s->dump_info.d_class == ELFCLASS64) {
573 ret = write_elf64_header(s);
574 } else {
575 ret = write_elf32_header(s);
576 }
577 if (ret < 0) {
578 return -1;
579 }
580
581 if (s->dump_info.d_class == ELFCLASS64) {
582 /* write PT_NOTE to vmcore */
583 if (write_elf64_note(s) < 0) {
584 return -1;
585 }
586
587 /* write all PT_LOAD to vmcore */
588 if (write_elf_loads(s) < 0) {
589 return -1;
590 }
591
592 /* write section to vmcore */
593 if (s->have_section) {
594 if (write_elf_section(s, 1) < 0) {
595 return -1;
596 }
597 }
598
599 /* write notes to vmcore */
qiaonuohan6a519912014-02-18 14:11:26 +0800600 if (write_elf64_notes(fd_write_vmcore, s) < 0) {
Wen Congyang783e9b42012-05-07 12:10:47 +0800601 return -1;
602 }
603
604 } else {
605 /* write PT_NOTE to vmcore */
606 if (write_elf32_note(s) < 0) {
607 return -1;
608 }
609
610 /* write all PT_LOAD to vmcore */
611 if (write_elf_loads(s) < 0) {
612 return -1;
613 }
614
615 /* write section to vmcore */
616 if (s->have_section) {
617 if (write_elf_section(s, 0) < 0) {
618 return -1;
619 }
620 }
621
622 /* write notes to vmcore */
qiaonuohan6a519912014-02-18 14:11:26 +0800623 if (write_elf32_notes(fd_write_vmcore, s) < 0) {
Wen Congyang783e9b42012-05-07 12:10:47 +0800624 return -1;
625 }
626 }
627
628 return 0;
629}
630
631/* write PT_LOAD to vmcore */
632static int dump_completed(DumpState *s)
633{
634 dump_cleanup(s);
635 return 0;
636}
637
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200638static int get_next_block(DumpState *s, GuestPhysBlock *block)
Wen Congyang783e9b42012-05-07 12:10:47 +0800639{
640 while (1) {
Paolo Bonzinia3161032012-11-14 15:54:48 +0100641 block = QTAILQ_NEXT(block, next);
Wen Congyang783e9b42012-05-07 12:10:47 +0800642 if (!block) {
643 /* no more block */
644 return 1;
645 }
646
647 s->start = 0;
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200648 s->next_block = block;
Wen Congyang783e9b42012-05-07 12:10:47 +0800649 if (s->has_filter) {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200650 if (block->target_start >= s->begin + s->length ||
651 block->target_end <= s->begin) {
Wen Congyang783e9b42012-05-07 12:10:47 +0800652 /* This block is out of the range */
653 continue;
654 }
655
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200656 if (s->begin > block->target_start) {
657 s->start = s->begin - block->target_start;
Wen Congyang783e9b42012-05-07 12:10:47 +0800658 }
659 }
660
661 return 0;
662 }
663}
664
665/* write all memory to vmcore */
666static int dump_iterate(DumpState *s)
667{
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200668 GuestPhysBlock *block;
Wen Congyang783e9b42012-05-07 12:10:47 +0800669 int64_t size;
670 int ret;
671
672 while (1) {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200673 block = s->next_block;
Wen Congyang783e9b42012-05-07 12:10:47 +0800674
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200675 size = block->target_end - block->target_start;
Wen Congyang783e9b42012-05-07 12:10:47 +0800676 if (s->has_filter) {
677 size -= s->start;
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200678 if (s->begin + s->length < block->target_end) {
679 size -= block->target_end - (s->begin + s->length);
Wen Congyang783e9b42012-05-07 12:10:47 +0800680 }
681 }
682 ret = write_memory(s, block, s->start, size);
683 if (ret == -1) {
684 return ret;
685 }
686
687 ret = get_next_block(s, block);
688 if (ret == 1) {
689 dump_completed(s);
690 return 0;
691 }
692 }
693}
694
695static int create_vmcore(DumpState *s)
696{
697 int ret;
698
699 ret = dump_begin(s);
700 if (ret < 0) {
701 return -1;
702 }
703
704 ret = dump_iterate(s);
705 if (ret < 0) {
706 return -1;
707 }
708
709 return 0;
710}
711
qiaonuohanfda05382014-02-18 14:11:27 +0800712static int write_start_flat_header(int fd)
713{
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200714 MakedumpfileHeader *mh;
qiaonuohanfda05382014-02-18 14:11:27 +0800715 int ret = 0;
716
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200717 QEMU_BUILD_BUG_ON(sizeof *mh > MAX_SIZE_MDF_HEADER);
718 mh = g_malloc0(MAX_SIZE_MDF_HEADER);
qiaonuohanfda05382014-02-18 14:11:27 +0800719
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200720 memcpy(mh->signature, MAKEDUMPFILE_SIGNATURE,
721 MIN(sizeof mh->signature, sizeof MAKEDUMPFILE_SIGNATURE));
qiaonuohanfda05382014-02-18 14:11:27 +0800722
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200723 mh->type = cpu_to_be64(TYPE_FLAT_HEADER);
724 mh->version = cpu_to_be64(VERSION_FLAT_HEADER);
qiaonuohanfda05382014-02-18 14:11:27 +0800725
726 size_t written_size;
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200727 written_size = qemu_write_full(fd, mh, MAX_SIZE_MDF_HEADER);
qiaonuohanfda05382014-02-18 14:11:27 +0800728 if (written_size != MAX_SIZE_MDF_HEADER) {
729 ret = -1;
730 }
731
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200732 g_free(mh);
qiaonuohanfda05382014-02-18 14:11:27 +0800733 return ret;
734}
735
736static int write_end_flat_header(int fd)
737{
738 MakedumpfileDataHeader mdh;
739
740 mdh.offset = END_FLAG_FLAT_HEADER;
741 mdh.buf_size = END_FLAG_FLAT_HEADER;
742
743 size_t written_size;
744 written_size = qemu_write_full(fd, &mdh, sizeof(mdh));
745 if (written_size != sizeof(mdh)) {
746 return -1;
747 }
748
749 return 0;
750}
751
qiaonuohan5d31bab2014-02-18 14:11:28 +0800752static int write_buffer(int fd, off_t offset, const void *buf, size_t size)
753{
754 size_t written_size;
755 MakedumpfileDataHeader mdh;
756
757 mdh.offset = cpu_to_be64(offset);
758 mdh.buf_size = cpu_to_be64(size);
759
760 written_size = qemu_write_full(fd, &mdh, sizeof(mdh));
761 if (written_size != sizeof(mdh)) {
762 return -1;
763 }
764
765 written_size = qemu_write_full(fd, buf, size);
766 if (written_size != size) {
767 return -1;
768 }
769
770 return 0;
771}
772
qiaonuohan4835ef72014-02-18 14:11:29 +0800773static int buf_write_note(const void *buf, size_t size, void *opaque)
774{
775 DumpState *s = opaque;
776
777 /* note_buf is not enough */
778 if (s->note_buf_offset + size > s->note_size) {
779 return -1;
780 }
781
782 memcpy(s->note_buf + s->note_buf_offset, buf, size);
783
784 s->note_buf_offset += size;
785
786 return 0;
787}
788
qiaonuohan298f1162014-02-18 14:11:32 +0800789/* write common header, sub header and elf note to vmcore */
790static int create_header32(DumpState *s)
791{
792 int ret = 0;
793 DiskDumpHeader32 *dh = NULL;
794 KdumpSubHeader32 *kh = NULL;
795 size_t size;
796 int endian = s->dump_info.d_endian;
797 uint32_t block_size;
798 uint32_t sub_hdr_size;
799 uint32_t bitmap_blocks;
800 uint32_t status = 0;
801 uint64_t offset_note;
802
803 /* write common header, the version of kdump-compressed format is 6th */
804 size = sizeof(DiskDumpHeader32);
805 dh = g_malloc0(size);
806
807 strncpy(dh->signature, KDUMP_SIGNATURE, strlen(KDUMP_SIGNATURE));
808 dh->header_version = cpu_convert_to_target32(6, endian);
809 block_size = s->page_size;
810 dh->block_size = cpu_convert_to_target32(block_size, endian);
811 sub_hdr_size = sizeof(struct KdumpSubHeader32) + s->note_size;
812 sub_hdr_size = DIV_ROUND_UP(sub_hdr_size, block_size);
813 dh->sub_hdr_size = cpu_convert_to_target32(sub_hdr_size, endian);
814 /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
815 dh->max_mapnr = cpu_convert_to_target32(MIN(s->max_mapnr, UINT_MAX),
816 endian);
817 dh->nr_cpus = cpu_convert_to_target32(s->nr_cpus, endian);
818 bitmap_blocks = DIV_ROUND_UP(s->len_dump_bitmap, block_size) * 2;
819 dh->bitmap_blocks = cpu_convert_to_target32(bitmap_blocks, endian);
qiaonuohan4ab23a92014-02-18 14:11:37 +0800820 strncpy(dh->utsname.machine, ELF_MACHINE_UNAME, sizeof(dh->utsname.machine));
qiaonuohan298f1162014-02-18 14:11:32 +0800821
822 if (s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) {
823 status |= DUMP_DH_COMPRESSED_ZLIB;
824 }
825#ifdef CONFIG_LZO
826 if (s->flag_compress & DUMP_DH_COMPRESSED_LZO) {
827 status |= DUMP_DH_COMPRESSED_LZO;
828 }
829#endif
830#ifdef CONFIG_SNAPPY
831 if (s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) {
832 status |= DUMP_DH_COMPRESSED_SNAPPY;
833 }
834#endif
835 dh->status = cpu_convert_to_target32(status, endian);
836
837 if (write_buffer(s->fd, 0, dh, size) < 0) {
838 dump_error(s, "dump: failed to write disk dump header.\n");
839 ret = -1;
840 goto out;
841 }
842
843 /* write sub header */
844 size = sizeof(KdumpSubHeader32);
845 kh = g_malloc0(size);
846
847 /* 64bit max_mapnr_64 */
848 kh->max_mapnr_64 = cpu_convert_to_target64(s->max_mapnr, endian);
849 kh->phys_base = cpu_convert_to_target32(PHYS_BASE, endian);
850 kh->dump_level = cpu_convert_to_target32(DUMP_LEVEL, endian);
851
852 offset_note = DISKDUMP_HEADER_BLOCKS * block_size + size;
853 kh->offset_note = cpu_convert_to_target64(offset_note, endian);
854 kh->note_size = cpu_convert_to_target32(s->note_size, endian);
855
856 if (write_buffer(s->fd, DISKDUMP_HEADER_BLOCKS *
857 block_size, kh, size) < 0) {
858 dump_error(s, "dump: failed to write kdump sub header.\n");
859 ret = -1;
860 goto out;
861 }
862
863 /* write note */
864 s->note_buf = g_malloc0(s->note_size);
865 s->note_buf_offset = 0;
866
867 /* use s->note_buf to store notes temporarily */
868 if (write_elf32_notes(buf_write_note, s) < 0) {
869 ret = -1;
870 goto out;
871 }
872
873 if (write_buffer(s->fd, offset_note, s->note_buf,
874 s->note_size) < 0) {
875 dump_error(s, "dump: failed to write notes");
876 ret = -1;
877 goto out;
878 }
879
880 /* get offset of dump_bitmap */
881 s->offset_dump_bitmap = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size) *
882 block_size;
883
884 /* get offset of page */
885 s->offset_page = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size + bitmap_blocks) *
886 block_size;
887
888out:
889 g_free(dh);
890 g_free(kh);
891 g_free(s->note_buf);
892
893 return ret;
894}
895
896/* write common header, sub header and elf note to vmcore */
897static int create_header64(DumpState *s)
898{
899 int ret = 0;
900 DiskDumpHeader64 *dh = NULL;
901 KdumpSubHeader64 *kh = NULL;
902 size_t size;
903 int endian = s->dump_info.d_endian;
904 uint32_t block_size;
905 uint32_t sub_hdr_size;
906 uint32_t bitmap_blocks;
907 uint32_t status = 0;
908 uint64_t offset_note;
909
910 /* write common header, the version of kdump-compressed format is 6th */
911 size = sizeof(DiskDumpHeader64);
912 dh = g_malloc0(size);
913
914 strncpy(dh->signature, KDUMP_SIGNATURE, strlen(KDUMP_SIGNATURE));
915 dh->header_version = cpu_convert_to_target32(6, endian);
916 block_size = s->page_size;
917 dh->block_size = cpu_convert_to_target32(block_size, endian);
918 sub_hdr_size = sizeof(struct KdumpSubHeader64) + s->note_size;
919 sub_hdr_size = DIV_ROUND_UP(sub_hdr_size, block_size);
920 dh->sub_hdr_size = cpu_convert_to_target32(sub_hdr_size, endian);
921 /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
922 dh->max_mapnr = cpu_convert_to_target32(MIN(s->max_mapnr, UINT_MAX),
923 endian);
924 dh->nr_cpus = cpu_convert_to_target32(s->nr_cpus, endian);
925 bitmap_blocks = DIV_ROUND_UP(s->len_dump_bitmap, block_size) * 2;
926 dh->bitmap_blocks = cpu_convert_to_target32(bitmap_blocks, endian);
qiaonuohan4ab23a92014-02-18 14:11:37 +0800927 strncpy(dh->utsname.machine, ELF_MACHINE_UNAME, sizeof(dh->utsname.machine));
qiaonuohan298f1162014-02-18 14:11:32 +0800928
929 if (s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) {
930 status |= DUMP_DH_COMPRESSED_ZLIB;
931 }
932#ifdef CONFIG_LZO
933 if (s->flag_compress & DUMP_DH_COMPRESSED_LZO) {
934 status |= DUMP_DH_COMPRESSED_LZO;
935 }
936#endif
937#ifdef CONFIG_SNAPPY
938 if (s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) {
939 status |= DUMP_DH_COMPRESSED_SNAPPY;
940 }
941#endif
942 dh->status = cpu_convert_to_target32(status, endian);
943
944 if (write_buffer(s->fd, 0, dh, size) < 0) {
945 dump_error(s, "dump: failed to write disk dump header.\n");
946 ret = -1;
947 goto out;
948 }
949
950 /* write sub header */
951 size = sizeof(KdumpSubHeader64);
952 kh = g_malloc0(size);
953
954 /* 64bit max_mapnr_64 */
955 kh->max_mapnr_64 = cpu_convert_to_target64(s->max_mapnr, endian);
956 kh->phys_base = cpu_convert_to_target64(PHYS_BASE, endian);
957 kh->dump_level = cpu_convert_to_target32(DUMP_LEVEL, endian);
958
959 offset_note = DISKDUMP_HEADER_BLOCKS * block_size + size;
960 kh->offset_note = cpu_convert_to_target64(offset_note, endian);
961 kh->note_size = cpu_convert_to_target64(s->note_size, endian);
962
963 if (write_buffer(s->fd, DISKDUMP_HEADER_BLOCKS *
964 block_size, kh, size) < 0) {
965 dump_error(s, "dump: failed to write kdump sub header.\n");
966 ret = -1;
967 goto out;
968 }
969
970 /* write note */
971 s->note_buf = g_malloc0(s->note_size);
972 s->note_buf_offset = 0;
973
974 /* use s->note_buf to store notes temporarily */
975 if (write_elf64_notes(buf_write_note, s) < 0) {
976 ret = -1;
977 goto out;
978 }
979
980 if (write_buffer(s->fd, offset_note, s->note_buf,
981 s->note_size) < 0) {
982 dump_error(s, "dump: failed to write notes");
983 ret = -1;
984 goto out;
985 }
986
987 /* get offset of dump_bitmap */
988 s->offset_dump_bitmap = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size) *
989 block_size;
990
991 /* get offset of page */
992 s->offset_page = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size + bitmap_blocks) *
993 block_size;
994
995out:
996 g_free(dh);
997 g_free(kh);
998 g_free(s->note_buf);
999
1000 return ret;
1001}
1002
1003static int write_dump_header(DumpState *s)
1004{
1005 if (s->dump_info.d_machine == EM_386) {
1006 return create_header32(s);
1007 } else {
1008 return create_header64(s);
1009 }
1010}
1011
qiaonuohand0686c72014-02-18 14:11:33 +08001012/*
1013 * set dump_bitmap sequencely. the bit before last_pfn is not allowed to be
1014 * rewritten, so if need to set the first bit, set last_pfn and pfn to 0.
1015 * set_dump_bitmap will always leave the recently set bit un-sync. And setting
1016 * (last bit + sizeof(buf) * 8) to 0 will do flushing the content in buf into
1017 * vmcore, ie. synchronizing un-sync bit into vmcore.
1018 */
1019static int set_dump_bitmap(uint64_t last_pfn, uint64_t pfn, bool value,
1020 uint8_t *buf, DumpState *s)
1021{
1022 off_t old_offset, new_offset;
1023 off_t offset_bitmap1, offset_bitmap2;
1024 uint32_t byte, bit;
1025
1026 /* should not set the previous place */
1027 assert(last_pfn <= pfn);
1028
1029 /*
1030 * if the bit needed to be set is not cached in buf, flush the data in buf
1031 * to vmcore firstly.
1032 * making new_offset be bigger than old_offset can also sync remained data
1033 * into vmcore.
1034 */
1035 old_offset = BUFSIZE_BITMAP * (last_pfn / PFN_BUFBITMAP);
1036 new_offset = BUFSIZE_BITMAP * (pfn / PFN_BUFBITMAP);
1037
1038 while (old_offset < new_offset) {
1039 /* calculate the offset and write dump_bitmap */
1040 offset_bitmap1 = s->offset_dump_bitmap + old_offset;
1041 if (write_buffer(s->fd, offset_bitmap1, buf,
1042 BUFSIZE_BITMAP) < 0) {
1043 return -1;
1044 }
1045
1046 /* dump level 1 is chosen, so 1st and 2nd bitmap are same */
1047 offset_bitmap2 = s->offset_dump_bitmap + s->len_dump_bitmap +
1048 old_offset;
1049 if (write_buffer(s->fd, offset_bitmap2, buf,
1050 BUFSIZE_BITMAP) < 0) {
1051 return -1;
1052 }
1053
1054 memset(buf, 0, BUFSIZE_BITMAP);
1055 old_offset += BUFSIZE_BITMAP;
1056 }
1057
1058 /* get the exact place of the bit in the buf, and set it */
1059 byte = (pfn % PFN_BUFBITMAP) / CHAR_BIT;
1060 bit = (pfn % PFN_BUFBITMAP) % CHAR_BIT;
1061 if (value) {
1062 buf[byte] |= 1u << bit;
1063 } else {
1064 buf[byte] &= ~(1u << bit);
1065 }
1066
1067 return 0;
1068}
1069
1070/*
1071 * exam every page and return the page frame number and the address of the page.
1072 * bufptr can be NULL. note: the blocks here is supposed to reflect guest-phys
1073 * blocks, so block->target_start and block->target_end should be interal
1074 * multiples of the target page size.
1075 */
1076static bool get_next_page(GuestPhysBlock **blockptr, uint64_t *pfnptr,
1077 uint8_t **bufptr, DumpState *s)
1078{
1079 GuestPhysBlock *block = *blockptr;
1080 hwaddr addr;
1081 uint8_t *buf;
1082
1083 /* block == NULL means the start of the iteration */
1084 if (!block) {
1085 block = QTAILQ_FIRST(&s->guest_phys_blocks.head);
1086 *blockptr = block;
1087 assert(block->target_start % s->page_size == 0);
1088 assert(block->target_end % s->page_size == 0);
1089 *pfnptr = paddr_to_pfn(block->target_start, s->page_shift);
1090 if (bufptr) {
1091 *bufptr = block->host_addr;
1092 }
1093 return true;
1094 }
1095
1096 *pfnptr = *pfnptr + 1;
1097 addr = pfn_to_paddr(*pfnptr, s->page_shift);
1098
1099 if ((addr >= block->target_start) &&
1100 (addr + s->page_size <= block->target_end)) {
1101 buf = block->host_addr + (addr - block->target_start);
1102 } else {
1103 /* the next page is in the next block */
1104 block = QTAILQ_NEXT(block, next);
1105 *blockptr = block;
1106 if (!block) {
1107 return false;
1108 }
1109 assert(block->target_start % s->page_size == 0);
1110 assert(block->target_end % s->page_size == 0);
1111 *pfnptr = paddr_to_pfn(block->target_start, s->page_shift);
1112 buf = block->host_addr;
1113 }
1114
1115 if (bufptr) {
1116 *bufptr = buf;
1117 }
1118
1119 return true;
1120}
1121
1122static int write_dump_bitmap(DumpState *s)
1123{
1124 int ret = 0;
1125 uint64_t last_pfn, pfn;
1126 void *dump_bitmap_buf;
1127 size_t num_dumpable;
1128 GuestPhysBlock *block_iter = NULL;
1129
1130 /* dump_bitmap_buf is used to store dump_bitmap temporarily */
1131 dump_bitmap_buf = g_malloc0(BUFSIZE_BITMAP);
1132
1133 num_dumpable = 0;
1134 last_pfn = 0;
1135
1136 /*
1137 * exam memory page by page, and set the bit in dump_bitmap corresponded
1138 * to the existing page.
1139 */
1140 while (get_next_page(&block_iter, &pfn, NULL, s)) {
1141 ret = set_dump_bitmap(last_pfn, pfn, true, dump_bitmap_buf, s);
1142 if (ret < 0) {
1143 dump_error(s, "dump: failed to set dump_bitmap.\n");
1144 ret = -1;
1145 goto out;
1146 }
1147
1148 last_pfn = pfn;
1149 num_dumpable++;
1150 }
1151
1152 /*
1153 * set_dump_bitmap will always leave the recently set bit un-sync. Here we
1154 * set last_pfn + PFN_BUFBITMAP to 0 and those set but un-sync bit will be
1155 * synchronized into vmcore.
1156 */
1157 if (num_dumpable > 0) {
1158 ret = set_dump_bitmap(last_pfn, last_pfn + PFN_BUFBITMAP, false,
1159 dump_bitmap_buf, s);
1160 if (ret < 0) {
1161 dump_error(s, "dump: failed to sync dump_bitmap.\n");
1162 ret = -1;
1163 goto out;
1164 }
1165 }
1166
1167 /* number of dumpable pages that will be dumped later */
1168 s->num_dumpable = num_dumpable;
1169
1170out:
1171 g_free(dump_bitmap_buf);
1172
1173 return ret;
1174}
1175
qiaonuohan64cfba62014-02-18 14:11:34 +08001176static void prepare_data_cache(DataCache *data_cache, DumpState *s,
1177 off_t offset)
1178{
1179 data_cache->fd = s->fd;
1180 data_cache->data_size = 0;
1181 data_cache->buf_size = BUFSIZE_DATA_CACHE;
1182 data_cache->buf = g_malloc0(BUFSIZE_DATA_CACHE);
1183 data_cache->offset = offset;
1184}
1185
1186static int write_cache(DataCache *dc, const void *buf, size_t size,
1187 bool flag_sync)
1188{
1189 /*
1190 * dc->buf_size should not be less than size, otherwise dc will never be
1191 * enough
1192 */
1193 assert(size <= dc->buf_size);
1194
1195 /*
1196 * if flag_sync is set, synchronize data in dc->buf into vmcore.
1197 * otherwise check if the space is enough for caching data in buf, if not,
1198 * write the data in dc->buf to dc->fd and reset dc->buf
1199 */
1200 if ((!flag_sync && dc->data_size + size > dc->buf_size) ||
1201 (flag_sync && dc->data_size > 0)) {
1202 if (write_buffer(dc->fd, dc->offset, dc->buf, dc->data_size) < 0) {
1203 return -1;
1204 }
1205
1206 dc->offset += dc->data_size;
1207 dc->data_size = 0;
1208 }
1209
1210 if (!flag_sync) {
1211 memcpy(dc->buf + dc->data_size, buf, size);
1212 dc->data_size += size;
1213 }
1214
1215 return 0;
1216}
1217
1218static void free_data_cache(DataCache *data_cache)
1219{
1220 g_free(data_cache->buf);
1221}
1222
qiaonuohand12f57e2014-02-18 14:11:35 +08001223static size_t get_len_buf_out(size_t page_size, uint32_t flag_compress)
1224{
1225 size_t len_buf_out_zlib, len_buf_out_lzo, len_buf_out_snappy;
1226 size_t len_buf_out;
1227
1228 /* init buf_out */
1229 len_buf_out_zlib = len_buf_out_lzo = len_buf_out_snappy = 0;
1230
1231 /* buf size for zlib */
1232 len_buf_out_zlib = compressBound(page_size);
1233
1234 /* buf size for lzo */
1235#ifdef CONFIG_LZO
1236 if (flag_compress & DUMP_DH_COMPRESSED_LZO) {
1237 if (lzo_init() != LZO_E_OK) {
1238 /* return 0 to indicate lzo is unavailable */
1239 return 0;
1240 }
1241 }
1242
1243 /*
1244 * LZO will expand incompressible data by a little amount. please check the
1245 * following URL to see the expansion calculation:
1246 * http://www.oberhumer.com/opensource/lzo/lzofaq.php
1247 */
1248 len_buf_out_lzo = page_size + page_size / 16 + 64 + 3;
1249#endif
1250
1251#ifdef CONFIG_SNAPPY
1252 /* buf size for snappy */
1253 len_buf_out_snappy = snappy_max_compressed_length(page_size);
1254#endif
1255
1256 /* get the biggest that can store all kinds of compressed page */
1257 len_buf_out = MAX(len_buf_out_zlib,
1258 MAX(len_buf_out_lzo, len_buf_out_snappy));
1259
1260 return len_buf_out;
1261}
1262
1263/*
1264 * check if the page is all 0
1265 */
1266static inline bool is_zero_page(const uint8_t *buf, size_t page_size)
1267{
1268 return buffer_is_zero(buf, page_size);
1269}
1270
1271static int write_dump_pages(DumpState *s)
1272{
1273 int ret = 0;
1274 DataCache page_desc, page_data;
1275 size_t len_buf_out, size_out;
1276#ifdef CONFIG_LZO
1277 lzo_bytep wrkmem = NULL;
1278#endif
1279 uint8_t *buf_out = NULL;
1280 off_t offset_desc, offset_data;
1281 PageDescriptor pd, pd_zero;
1282 uint8_t *buf;
1283 int endian = s->dump_info.d_endian;
1284 GuestPhysBlock *block_iter = NULL;
1285 uint64_t pfn_iter;
1286
1287 /* get offset of page_desc and page_data in dump file */
1288 offset_desc = s->offset_page;
1289 offset_data = offset_desc + sizeof(PageDescriptor) * s->num_dumpable;
1290
1291 prepare_data_cache(&page_desc, s, offset_desc);
1292 prepare_data_cache(&page_data, s, offset_data);
1293
1294 /* prepare buffer to store compressed data */
1295 len_buf_out = get_len_buf_out(s->page_size, s->flag_compress);
1296 if (len_buf_out == 0) {
1297 dump_error(s, "dump: failed to get length of output buffer.\n");
1298 goto out;
1299 }
1300
1301#ifdef CONFIG_LZO
1302 wrkmem = g_malloc(LZO1X_1_MEM_COMPRESS);
1303#endif
1304
1305 buf_out = g_malloc(len_buf_out);
1306
1307 /*
1308 * init zero page's page_desc and page_data, because every zero page
1309 * uses the same page_data
1310 */
1311 pd_zero.size = cpu_convert_to_target32(s->page_size, endian);
1312 pd_zero.flags = cpu_convert_to_target32(0, endian);
1313 pd_zero.offset = cpu_convert_to_target64(offset_data, endian);
1314 pd_zero.page_flags = cpu_convert_to_target64(0, endian);
1315 buf = g_malloc0(s->page_size);
1316 ret = write_cache(&page_data, buf, s->page_size, false);
1317 g_free(buf);
1318 if (ret < 0) {
1319 dump_error(s, "dump: failed to write page data(zero page).\n");
1320 goto out;
1321 }
1322
1323 offset_data += s->page_size;
1324
1325 /*
1326 * dump memory to vmcore page by page. zero page will all be resided in the
1327 * first page of page section
1328 */
1329 while (get_next_page(&block_iter, &pfn_iter, &buf, s)) {
1330 /* check zero page */
1331 if (is_zero_page(buf, s->page_size)) {
1332 ret = write_cache(&page_desc, &pd_zero, sizeof(PageDescriptor),
1333 false);
1334 if (ret < 0) {
1335 dump_error(s, "dump: failed to write page desc.\n");
1336 goto out;
1337 }
1338 } else {
1339 /*
1340 * not zero page, then:
1341 * 1. compress the page
1342 * 2. write the compressed page into the cache of page_data
1343 * 3. get page desc of the compressed page and write it into the
1344 * cache of page_desc
1345 *
1346 * only one compression format will be used here, for
1347 * s->flag_compress is set. But when compression fails to work,
1348 * we fall back to save in plaintext.
1349 */
1350 size_out = len_buf_out;
1351 if ((s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) &&
1352 (compress2(buf_out, (uLongf *)&size_out, buf, s->page_size,
1353 Z_BEST_SPEED) == Z_OK) && (size_out < s->page_size)) {
1354 pd.flags = cpu_convert_to_target32(DUMP_DH_COMPRESSED_ZLIB,
1355 endian);
1356 pd.size = cpu_convert_to_target32(size_out, endian);
1357
1358 ret = write_cache(&page_data, buf_out, size_out, false);
1359 if (ret < 0) {
1360 dump_error(s, "dump: failed to write page data.\n");
1361 goto out;
1362 }
1363#ifdef CONFIG_LZO
1364 } else if ((s->flag_compress & DUMP_DH_COMPRESSED_LZO) &&
1365 (lzo1x_1_compress(buf, s->page_size, buf_out,
1366 (lzo_uint *)&size_out, wrkmem) == LZO_E_OK) &&
1367 (size_out < s->page_size)) {
1368 pd.flags = cpu_convert_to_target32(DUMP_DH_COMPRESSED_LZO,
1369 endian);
1370 pd.size = cpu_convert_to_target32(size_out, endian);
1371
1372 ret = write_cache(&page_data, buf_out, size_out, false);
1373 if (ret < 0) {
1374 dump_error(s, "dump: failed to write page data.\n");
1375 goto out;
1376 }
1377#endif
1378#ifdef CONFIG_SNAPPY
1379 } else if ((s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) &&
1380 (snappy_compress((char *)buf, s->page_size,
1381 (char *)buf_out, &size_out) == SNAPPY_OK) &&
1382 (size_out < s->page_size)) {
1383 pd.flags = cpu_convert_to_target32(
1384 DUMP_DH_COMPRESSED_SNAPPY, endian);
1385 pd.size = cpu_convert_to_target32(size_out, endian);
1386
1387 ret = write_cache(&page_data, buf_out, size_out, false);
1388 if (ret < 0) {
1389 dump_error(s, "dump: failed to write page data.\n");
1390 goto out;
1391 }
1392#endif
1393 } else {
1394 /*
1395 * fall back to save in plaintext, size_out should be
1396 * assigned to s->page_size
1397 */
1398 pd.flags = cpu_convert_to_target32(0, endian);
1399 size_out = s->page_size;
1400 pd.size = cpu_convert_to_target32(size_out, endian);
1401
1402 ret = write_cache(&page_data, buf, s->page_size, false);
1403 if (ret < 0) {
1404 dump_error(s, "dump: failed to write page data.\n");
1405 goto out;
1406 }
1407 }
1408
1409 /* get and write page desc here */
1410 pd.page_flags = cpu_convert_to_target64(0, endian);
1411 pd.offset = cpu_convert_to_target64(offset_data, endian);
1412 offset_data += size_out;
1413
1414 ret = write_cache(&page_desc, &pd, sizeof(PageDescriptor), false);
1415 if (ret < 0) {
1416 dump_error(s, "dump: failed to write page desc.\n");
1417 goto out;
1418 }
1419 }
1420 }
1421
1422 ret = write_cache(&page_desc, NULL, 0, true);
1423 if (ret < 0) {
1424 dump_error(s, "dump: failed to sync cache for page_desc.\n");
1425 goto out;
1426 }
1427 ret = write_cache(&page_data, NULL, 0, true);
1428 if (ret < 0) {
1429 dump_error(s, "dump: failed to sync cache for page_data.\n");
1430 goto out;
1431 }
1432
1433out:
1434 free_data_cache(&page_desc);
1435 free_data_cache(&page_data);
1436
1437#ifdef CONFIG_LZO
1438 g_free(wrkmem);
1439#endif
1440
1441 g_free(buf_out);
1442
1443 return ret;
1444}
1445
qiaonuohanb53ccc32014-02-18 14:11:36 +08001446static int create_kdump_vmcore(DumpState *s)
1447{
1448 int ret;
1449
1450 /*
1451 * the kdump-compressed format is:
1452 * File offset
1453 * +------------------------------------------+ 0x0
1454 * | main header (struct disk_dump_header) |
1455 * |------------------------------------------+ block 1
1456 * | sub header (struct kdump_sub_header) |
1457 * |------------------------------------------+ block 2
1458 * | 1st-dump_bitmap |
1459 * |------------------------------------------+ block 2 + X blocks
1460 * | 2nd-dump_bitmap | (aligned by block)
1461 * |------------------------------------------+ block 2 + 2 * X blocks
1462 * | page desc for pfn 0 (struct page_desc) | (aligned by block)
1463 * | page desc for pfn 1 (struct page_desc) |
1464 * | : |
1465 * |------------------------------------------| (not aligned by block)
1466 * | page data (pfn 0) |
1467 * | page data (pfn 1) |
1468 * | : |
1469 * +------------------------------------------+
1470 */
1471
1472 ret = write_start_flat_header(s->fd);
1473 if (ret < 0) {
1474 dump_error(s, "dump: failed to write start flat header.\n");
1475 return -1;
1476 }
1477
1478 ret = write_dump_header(s);
1479 if (ret < 0) {
1480 return -1;
1481 }
1482
1483 ret = write_dump_bitmap(s);
1484 if (ret < 0) {
1485 return -1;
1486 }
1487
1488 ret = write_dump_pages(s);
1489 if (ret < 0) {
1490 return -1;
1491 }
1492
1493 ret = write_end_flat_header(s->fd);
1494 if (ret < 0) {
1495 dump_error(s, "dump: failed to write end flat header.\n");
1496 return -1;
1497 }
1498
1499 dump_completed(s);
1500
1501 return 0;
1502}
1503
Wen Congyang783e9b42012-05-07 12:10:47 +08001504static ram_addr_t get_start_block(DumpState *s)
1505{
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001506 GuestPhysBlock *block;
Wen Congyang783e9b42012-05-07 12:10:47 +08001507
1508 if (!s->has_filter) {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001509 s->next_block = QTAILQ_FIRST(&s->guest_phys_blocks.head);
Wen Congyang783e9b42012-05-07 12:10:47 +08001510 return 0;
1511 }
1512
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001513 QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
1514 if (block->target_start >= s->begin + s->length ||
1515 block->target_end <= s->begin) {
Wen Congyang783e9b42012-05-07 12:10:47 +08001516 /* This block is out of the range */
1517 continue;
1518 }
1519
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001520 s->next_block = block;
1521 if (s->begin > block->target_start) {
1522 s->start = s->begin - block->target_start;
Wen Congyang783e9b42012-05-07 12:10:47 +08001523 } else {
1524 s->start = 0;
1525 }
1526 return s->start;
1527 }
1528
1529 return -1;
1530}
1531
qiaonuohan7aad2482014-02-18 14:11:31 +08001532static void get_max_mapnr(DumpState *s)
1533{
1534 GuestPhysBlock *last_block;
1535
1536 last_block = QTAILQ_LAST(&s->guest_phys_blocks.head, GuestPhysBlockHead);
1537 s->max_mapnr = paddr_to_pfn(last_block->target_end, s->page_shift);
1538}
1539
qiaonuohanb53ccc32014-02-18 14:11:36 +08001540static int dump_init(DumpState *s, int fd, bool has_format,
1541 DumpGuestMemoryFormat format, bool paging, bool has_filter,
Wen Congyang783e9b42012-05-07 12:10:47 +08001542 int64_t begin, int64_t length, Error **errp)
1543{
Andreas Färber182735e2013-05-29 22:29:20 +02001544 CPUState *cpu;
Wen Congyang783e9b42012-05-07 12:10:47 +08001545 int nr_cpus;
Andreas Färber11ed09c2013-05-29 21:54:03 +02001546 Error *err = NULL;
Wen Congyang783e9b42012-05-07 12:10:47 +08001547 int ret;
1548
qiaonuohanb53ccc32014-02-18 14:11:36 +08001549 /* kdump-compressed is conflict with paging and filter */
1550 if (has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
1551 assert(!paging && !has_filter);
1552 }
1553
Wen Congyang783e9b42012-05-07 12:10:47 +08001554 if (runstate_is_running()) {
1555 vm_stop(RUN_STATE_SAVE_VM);
1556 s->resume = true;
1557 } else {
1558 s->resume = false;
1559 }
1560
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001561 /* If we use KVM, we should synchronize the registers before we get dump
1562 * info or physmap info.
Wen Congyang783e9b42012-05-07 12:10:47 +08001563 */
Andreas Färber1b3509c2013-06-09 16:48:29 +02001564 cpu_synchronize_all_states();
Wen Congyang783e9b42012-05-07 12:10:47 +08001565 nr_cpus = 0;
Andreas Färberbdc44642013-06-24 23:50:24 +02001566 CPU_FOREACH(cpu) {
Wen Congyang783e9b42012-05-07 12:10:47 +08001567 nr_cpus++;
1568 }
1569
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001570 s->fd = fd;
1571 s->has_filter = has_filter;
1572 s->begin = begin;
1573 s->length = length;
1574
1575 guest_phys_blocks_init(&s->guest_phys_blocks);
Laszlo Ersekc5d7f602013-08-06 12:37:10 +02001576 guest_phys_blocks_append(&s->guest_phys_blocks);
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001577
1578 s->start = get_start_block(s);
1579 if (s->start == -1) {
1580 error_set(errp, QERR_INVALID_PARAMETER, "begin");
1581 goto cleanup;
1582 }
1583
1584 /* get dump info: endian, class and architecture.
1585 * If the target architecture is not supported, cpu_get_dump_info() will
1586 * return -1.
1587 */
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001588 ret = cpu_get_dump_info(&s->dump_info, &s->guest_phys_blocks);
Wen Congyang783e9b42012-05-07 12:10:47 +08001589 if (ret < 0) {
1590 error_set(errp, QERR_UNSUPPORTED);
1591 goto cleanup;
1592 }
1593
Paolo Bonzini4720bd02012-06-07 08:48:09 +02001594 s->note_size = cpu_get_note_size(s->dump_info.d_class,
1595 s->dump_info.d_machine, nr_cpus);
Aneesh Kumar K.Vbb6b6842013-10-01 21:49:32 +05301596 if (s->note_size < 0) {
Paolo Bonzini4720bd02012-06-07 08:48:09 +02001597 error_set(errp, QERR_UNSUPPORTED);
1598 goto cleanup;
1599 }
1600
Wen Congyang783e9b42012-05-07 12:10:47 +08001601 /* get memory mapping */
1602 memory_mapping_list_init(&s->list);
1603 if (paging) {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001604 qemu_get_guest_memory_mapping(&s->list, &s->guest_phys_blocks, &err);
Andreas Färber11ed09c2013-05-29 21:54:03 +02001605 if (err != NULL) {
1606 error_propagate(errp, err);
1607 goto cleanup;
1608 }
Wen Congyang783e9b42012-05-07 12:10:47 +08001609 } else {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001610 qemu_get_guest_simple_memory_mapping(&s->list, &s->guest_phys_blocks);
Wen Congyang783e9b42012-05-07 12:10:47 +08001611 }
1612
qiaonuohan7aad2482014-02-18 14:11:31 +08001613 s->nr_cpus = nr_cpus;
1614 s->page_size = TARGET_PAGE_SIZE;
1615 s->page_shift = ffs(s->page_size) - 1;
1616
1617 get_max_mapnr(s);
1618
1619 uint64_t tmp;
1620 tmp = DIV_ROUND_UP(DIV_ROUND_UP(s->max_mapnr, CHAR_BIT), s->page_size);
1621 s->len_dump_bitmap = tmp * s->page_size;
1622
qiaonuohanb53ccc32014-02-18 14:11:36 +08001623 /* init for kdump-compressed format */
1624 if (has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
1625 switch (format) {
1626 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB:
1627 s->flag_compress = DUMP_DH_COMPRESSED_ZLIB;
1628 break;
1629
1630 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO:
1631 s->flag_compress = DUMP_DH_COMPRESSED_LZO;
1632 break;
1633
1634 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY:
1635 s->flag_compress = DUMP_DH_COMPRESSED_SNAPPY;
1636 break;
1637
1638 default:
1639 s->flag_compress = 0;
1640 }
1641
1642 return 0;
1643 }
1644
Wen Congyang783e9b42012-05-07 12:10:47 +08001645 if (s->has_filter) {
1646 memory_mapping_filter(&s->list, s->begin, s->length);
1647 }
1648
1649 /*
1650 * calculate phdr_num
1651 *
1652 * the type of ehdr->e_phnum is uint16_t, so we should avoid overflow
1653 */
1654 s->phdr_num = 1; /* PT_NOTE */
1655 if (s->list.num < UINT16_MAX - 2) {
1656 s->phdr_num += s->list.num;
1657 s->have_section = false;
1658 } else {
1659 s->have_section = true;
1660 s->phdr_num = PN_XNUM;
1661 s->sh_info = 1; /* PT_NOTE */
1662
1663 /* the type of shdr->sh_info is uint32_t, so we should avoid overflow */
1664 if (s->list.num <= UINT32_MAX - 1) {
1665 s->sh_info += s->list.num;
1666 } else {
1667 s->sh_info = UINT32_MAX;
1668 }
1669 }
1670
Wen Congyang783e9b42012-05-07 12:10:47 +08001671 if (s->dump_info.d_class == ELFCLASS64) {
1672 if (s->have_section) {
1673 s->memory_offset = sizeof(Elf64_Ehdr) +
1674 sizeof(Elf64_Phdr) * s->sh_info +
1675 sizeof(Elf64_Shdr) + s->note_size;
1676 } else {
1677 s->memory_offset = sizeof(Elf64_Ehdr) +
1678 sizeof(Elf64_Phdr) * s->phdr_num + s->note_size;
1679 }
1680 } else {
1681 if (s->have_section) {
1682 s->memory_offset = sizeof(Elf32_Ehdr) +
1683 sizeof(Elf32_Phdr) * s->sh_info +
1684 sizeof(Elf32_Shdr) + s->note_size;
1685 } else {
1686 s->memory_offset = sizeof(Elf32_Ehdr) +
1687 sizeof(Elf32_Phdr) * s->phdr_num + s->note_size;
1688 }
1689 }
1690
1691 return 0;
1692
1693cleanup:
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001694 guest_phys_blocks_free(&s->guest_phys_blocks);
1695
Wen Congyang783e9b42012-05-07 12:10:47 +08001696 if (s->resume) {
1697 vm_start();
1698 }
1699
1700 return -1;
1701}
1702
1703void qmp_dump_guest_memory(bool paging, const char *file, bool has_begin,
qiaonuohanb53ccc32014-02-18 14:11:36 +08001704 int64_t begin, bool has_length,
1705 int64_t length, bool has_format,
1706 DumpGuestMemoryFormat format, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +08001707{
1708 const char *p;
1709 int fd = -1;
1710 DumpState *s;
1711 int ret;
1712
qiaonuohanb53ccc32014-02-18 14:11:36 +08001713 /*
1714 * kdump-compressed format need the whole memory dumped, so paging or
1715 * filter is not supported here.
1716 */
1717 if ((has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) &&
1718 (paging || has_begin || has_length)) {
1719 error_setg(errp, "kdump-compressed format doesn't support paging or "
1720 "filter");
1721 return;
1722 }
Wen Congyang783e9b42012-05-07 12:10:47 +08001723 if (has_begin && !has_length) {
1724 error_set(errp, QERR_MISSING_PARAMETER, "length");
1725 return;
1726 }
1727 if (!has_begin && has_length) {
1728 error_set(errp, QERR_MISSING_PARAMETER, "begin");
1729 return;
1730 }
1731
qiaonuohanb53ccc32014-02-18 14:11:36 +08001732 /* check whether lzo/snappy is supported */
1733#ifndef CONFIG_LZO
1734 if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO) {
1735 error_setg(errp, "kdump-lzo is not available now");
1736 return;
1737 }
1738#endif
1739
1740#ifndef CONFIG_SNAPPY
1741 if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY) {
1742 error_setg(errp, "kdump-snappy is not available now");
1743 return;
1744 }
1745#endif
1746
Wen Congyang783e9b42012-05-07 12:10:47 +08001747#if !defined(WIN32)
1748 if (strstart(file, "fd:", &p)) {
Paolo Bonzinia9940fc2012-09-20 16:50:32 +02001749 fd = monitor_get_fd(cur_mon, p, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +08001750 if (fd == -1) {
Wen Congyang783e9b42012-05-07 12:10:47 +08001751 return;
1752 }
1753 }
1754#endif
1755
1756 if (strstart(file, "file:", &p)) {
1757 fd = qemu_open(p, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, S_IRUSR);
1758 if (fd < 0) {
Luiz Capitulino75817662013-06-07 14:36:01 -04001759 error_setg_file_open(errp, errno, p);
Wen Congyang783e9b42012-05-07 12:10:47 +08001760 return;
1761 }
1762 }
1763
1764 if (fd == -1) {
1765 error_set(errp, QERR_INVALID_PARAMETER, "protocol");
1766 return;
1767 }
1768
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001769 s = g_malloc0(sizeof(DumpState));
Wen Congyang783e9b42012-05-07 12:10:47 +08001770
qiaonuohanb53ccc32014-02-18 14:11:36 +08001771 ret = dump_init(s, fd, has_format, format, paging, has_begin,
1772 begin, length, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +08001773 if (ret < 0) {
1774 g_free(s);
1775 return;
1776 }
1777
qiaonuohanb53ccc32014-02-18 14:11:36 +08001778 if (has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
Markus Armbruster66ef8bd2014-05-02 13:26:41 +02001779 if (create_kdump_vmcore(s) < 0) {
qiaonuohanb53ccc32014-02-18 14:11:36 +08001780 error_set(errp, QERR_IO_ERROR);
1781 }
1782 } else {
Markus Armbruster66ef8bd2014-05-02 13:26:41 +02001783 if (create_vmcore(s) < 0) {
qiaonuohanb53ccc32014-02-18 14:11:36 +08001784 error_set(errp, QERR_IO_ERROR);
1785 }
Wen Congyang783e9b42012-05-07 12:10:47 +08001786 }
1787
1788 g_free(s);
1789}
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08001790
1791DumpGuestMemoryCapability *qmp_query_dump_guest_memory_capability(Error **errp)
1792{
1793 DumpGuestMemoryFormatList *item;
1794 DumpGuestMemoryCapability *cap =
1795 g_malloc0(sizeof(DumpGuestMemoryCapability));
1796
1797 /* elf is always available */
1798 item = g_malloc0(sizeof(DumpGuestMemoryFormatList));
1799 cap->formats = item;
1800 item->value = DUMP_GUEST_MEMORY_FORMAT_ELF;
1801
1802 /* kdump-zlib is always available */
1803 item->next = g_malloc0(sizeof(DumpGuestMemoryFormatList));
1804 item = item->next;
1805 item->value = DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB;
1806
1807 /* add new item if kdump-lzo is available */
1808#ifdef CONFIG_LZO
1809 item->next = g_malloc0(sizeof(DumpGuestMemoryFormatList));
1810 item = item->next;
1811 item->value = DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO;
1812#endif
1813
1814 /* add new item if kdump-snappy is available */
1815#ifdef CONFIG_SNAPPY
1816 item->next = g_malloc0(sizeof(DumpGuestMemoryFormatList));
1817 item = item->next;
1818 item->value = DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY;
1819#endif
1820
1821 return cap;
1822}