]>
Commit | Line | Data |
---|---|---|
783e9b48 WC |
1 | /* |
2 | * QEMU dump | |
3 | * | |
4 | * Copyright Fujitsu, Corp. 2011, 2012 | |
5 | * | |
6 | * Authors: | |
7 | * Wen Congyang <wency@cn.fujitsu.com> | |
8 | * | |
352666e2 SW |
9 | * 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. | |
783e9b48 WC |
11 | * |
12 | */ | |
13 | ||
14 | #include "qemu-common.h" | |
783e9b48 | 15 | #include "elf.h" |
783e9b48 | 16 | #include "cpu.h" |
022c62cb PB |
17 | #include "exec/cpu-all.h" |
18 | #include "exec/hwaddr.h" | |
83c9089e | 19 | #include "monitor/monitor.h" |
9c17d615 PB |
20 | #include "sysemu/kvm.h" |
21 | #include "sysemu/dump.h" | |
22 | #include "sysemu/sysemu.h" | |
23 | #include "sysemu/memory_mapping.h" | |
1b3509ca | 24 | #include "sysemu/cpus.h" |
7b1b5d19 | 25 | #include "qapi/error.h" |
783e9b48 | 26 | #include "qmp-commands.h" |
783e9b48 | 27 | |
d12f57ec QN |
28 | #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 | |
4ab23a91 QN |
35 | #ifndef ELF_MACHINE_UNAME |
36 | #define ELF_MACHINE_UNAME "Unknown" | |
37 | #endif | |
d12f57ec | 38 | |
783e9b48 WC |
39 | static 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 | ||
50 | static 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 | ||
61 | static 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 | ||
72 | typedef struct DumpState { | |
5ee163e8 | 73 | GuestPhysBlockList guest_phys_blocks; |
783e9b48 WC |
74 | ArchDumpInfo dump_info; |
75 | MemoryMappingList list; | |
76 | uint16_t phdr_num; | |
77 | uint32_t sh_info; | |
78 | bool have_section; | |
79 | bool resume; | |
bb6b6843 | 80 | ssize_t note_size; |
a8170e5e | 81 | hwaddr memory_offset; |
783e9b48 WC |
82 | int fd; |
83 | ||
56c4bfb3 | 84 | GuestPhysBlock *next_block; |
783e9b48 WC |
85 | ram_addr_t start; |
86 | bool has_filter; | |
87 | int64_t begin; | |
88 | int64_t length; | |
4835ef77 QN |
89 | |
90 | uint8_t *note_buf; /* buffer for notes */ | |
91 | size_t note_buf_offset; /* the writing place in note_buf */ | |
7aad248d | 92 | uint32_t nr_cpus; /* number of guest's cpu */ |
7aad248d QN |
93 | uint64_t max_mapnr; /* the biggest guest's phys-mem's number */ |
94 | size_t len_dump_bitmap; /* the size of the place used to store | |
95 | dump_bitmap in vmcore */ | |
96 | off_t offset_dump_bitmap; /* offset of dump_bitmap part in vmcore */ | |
97 | off_t offset_page; /* offset of page part in vmcore */ | |
98 | size_t num_dumpable; /* number of page that can be dumped */ | |
99 | uint32_t flag_compress; /* indicate the compression format */ | |
783e9b48 WC |
100 | } DumpState; |
101 | ||
102 | static int dump_cleanup(DumpState *s) | |
103 | { | |
104 | int ret = 0; | |
105 | ||
5ee163e8 | 106 | guest_phys_blocks_free(&s->guest_phys_blocks); |
783e9b48 WC |
107 | memory_mapping_list_free(&s->list); |
108 | if (s->fd != -1) { | |
109 | close(s->fd); | |
110 | } | |
111 | if (s->resume) { | |
112 | vm_start(); | |
113 | } | |
114 | ||
115 | return ret; | |
116 | } | |
117 | ||
118 | static void dump_error(DumpState *s, const char *reason) | |
119 | { | |
120 | dump_cleanup(s); | |
121 | } | |
122 | ||
b5ba1cc6 | 123 | static int fd_write_vmcore(const void *buf, size_t size, void *opaque) |
783e9b48 WC |
124 | { |
125 | DumpState *s = opaque; | |
2f61652d LC |
126 | size_t written_size; |
127 | ||
128 | written_size = qemu_write_full(s->fd, buf, size); | |
129 | if (written_size != size) { | |
130 | return -1; | |
783e9b48 WC |
131 | } |
132 | ||
133 | return 0; | |
134 | } | |
135 | ||
136 | static int write_elf64_header(DumpState *s) | |
137 | { | |
138 | Elf64_Ehdr elf_header; | |
139 | int ret; | |
140 | int endian = s->dump_info.d_endian; | |
141 | ||
142 | memset(&elf_header, 0, sizeof(Elf64_Ehdr)); | |
143 | memcpy(&elf_header, ELFMAG, SELFMAG); | |
144 | elf_header.e_ident[EI_CLASS] = ELFCLASS64; | |
145 | elf_header.e_ident[EI_DATA] = s->dump_info.d_endian; | |
146 | elf_header.e_ident[EI_VERSION] = EV_CURRENT; | |
147 | elf_header.e_type = cpu_convert_to_target16(ET_CORE, endian); | |
148 | elf_header.e_machine = cpu_convert_to_target16(s->dump_info.d_machine, | |
149 | endian); | |
150 | elf_header.e_version = cpu_convert_to_target32(EV_CURRENT, endian); | |
151 | elf_header.e_ehsize = cpu_convert_to_target16(sizeof(elf_header), endian); | |
152 | elf_header.e_phoff = cpu_convert_to_target64(sizeof(Elf64_Ehdr), endian); | |
153 | elf_header.e_phentsize = cpu_convert_to_target16(sizeof(Elf64_Phdr), | |
154 | endian); | |
155 | elf_header.e_phnum = cpu_convert_to_target16(s->phdr_num, endian); | |
156 | if (s->have_section) { | |
157 | uint64_t shoff = sizeof(Elf64_Ehdr) + sizeof(Elf64_Phdr) * s->sh_info; | |
158 | ||
159 | elf_header.e_shoff = cpu_convert_to_target64(shoff, endian); | |
160 | elf_header.e_shentsize = cpu_convert_to_target16(sizeof(Elf64_Shdr), | |
161 | endian); | |
162 | elf_header.e_shnum = cpu_convert_to_target16(1, endian); | |
163 | } | |
164 | ||
165 | ret = fd_write_vmcore(&elf_header, sizeof(elf_header), s); | |
166 | if (ret < 0) { | |
167 | dump_error(s, "dump: failed to write elf header.\n"); | |
168 | return -1; | |
169 | } | |
170 | ||
171 | return 0; | |
172 | } | |
173 | ||
174 | static int write_elf32_header(DumpState *s) | |
175 | { | |
176 | Elf32_Ehdr elf_header; | |
177 | int ret; | |
178 | int endian = s->dump_info.d_endian; | |
179 | ||
180 | memset(&elf_header, 0, sizeof(Elf32_Ehdr)); | |
181 | memcpy(&elf_header, ELFMAG, SELFMAG); | |
182 | elf_header.e_ident[EI_CLASS] = ELFCLASS32; | |
183 | elf_header.e_ident[EI_DATA] = endian; | |
184 | elf_header.e_ident[EI_VERSION] = EV_CURRENT; | |
185 | elf_header.e_type = cpu_convert_to_target16(ET_CORE, endian); | |
186 | elf_header.e_machine = cpu_convert_to_target16(s->dump_info.d_machine, | |
187 | endian); | |
188 | elf_header.e_version = cpu_convert_to_target32(EV_CURRENT, endian); | |
189 | elf_header.e_ehsize = cpu_convert_to_target16(sizeof(elf_header), endian); | |
190 | elf_header.e_phoff = cpu_convert_to_target32(sizeof(Elf32_Ehdr), endian); | |
191 | elf_header.e_phentsize = cpu_convert_to_target16(sizeof(Elf32_Phdr), | |
192 | endian); | |
193 | elf_header.e_phnum = cpu_convert_to_target16(s->phdr_num, endian); | |
194 | if (s->have_section) { | |
195 | uint32_t shoff = sizeof(Elf32_Ehdr) + sizeof(Elf32_Phdr) * s->sh_info; | |
196 | ||
197 | elf_header.e_shoff = cpu_convert_to_target32(shoff, endian); | |
198 | elf_header.e_shentsize = cpu_convert_to_target16(sizeof(Elf32_Shdr), | |
199 | endian); | |
200 | elf_header.e_shnum = cpu_convert_to_target16(1, endian); | |
201 | } | |
202 | ||
203 | ret = fd_write_vmcore(&elf_header, sizeof(elf_header), s); | |
204 | if (ret < 0) { | |
205 | dump_error(s, "dump: failed to write elf header.\n"); | |
206 | return -1; | |
207 | } | |
208 | ||
209 | return 0; | |
210 | } | |
211 | ||
212 | static int write_elf64_load(DumpState *s, MemoryMapping *memory_mapping, | |
2cac2607 LE |
213 | int phdr_index, hwaddr offset, |
214 | hwaddr filesz) | |
783e9b48 WC |
215 | { |
216 | Elf64_Phdr phdr; | |
217 | int ret; | |
218 | int endian = s->dump_info.d_endian; | |
219 | ||
220 | memset(&phdr, 0, sizeof(Elf64_Phdr)); | |
221 | phdr.p_type = cpu_convert_to_target32(PT_LOAD, endian); | |
222 | phdr.p_offset = cpu_convert_to_target64(offset, endian); | |
223 | phdr.p_paddr = cpu_convert_to_target64(memory_mapping->phys_addr, endian); | |
2cac2607 | 224 | phdr.p_filesz = cpu_convert_to_target64(filesz, endian); |
783e9b48 WC |
225 | phdr.p_memsz = cpu_convert_to_target64(memory_mapping->length, endian); |
226 | phdr.p_vaddr = cpu_convert_to_target64(memory_mapping->virt_addr, endian); | |
227 | ||
2cac2607 LE |
228 | assert(memory_mapping->length >= filesz); |
229 | ||
783e9b48 WC |
230 | ret = fd_write_vmcore(&phdr, sizeof(Elf64_Phdr), s); |
231 | if (ret < 0) { | |
232 | dump_error(s, "dump: failed to write program header table.\n"); | |
233 | return -1; | |
234 | } | |
235 | ||
236 | return 0; | |
237 | } | |
238 | ||
239 | static int write_elf32_load(DumpState *s, MemoryMapping *memory_mapping, | |
2cac2607 LE |
240 | int phdr_index, hwaddr offset, |
241 | hwaddr filesz) | |
783e9b48 WC |
242 | { |
243 | Elf32_Phdr phdr; | |
244 | int ret; | |
245 | int endian = s->dump_info.d_endian; | |
246 | ||
247 | memset(&phdr, 0, sizeof(Elf32_Phdr)); | |
248 | phdr.p_type = cpu_convert_to_target32(PT_LOAD, endian); | |
249 | phdr.p_offset = cpu_convert_to_target32(offset, endian); | |
250 | phdr.p_paddr = cpu_convert_to_target32(memory_mapping->phys_addr, endian); | |
2cac2607 | 251 | phdr.p_filesz = cpu_convert_to_target32(filesz, endian); |
783e9b48 WC |
252 | phdr.p_memsz = cpu_convert_to_target32(memory_mapping->length, endian); |
253 | phdr.p_vaddr = cpu_convert_to_target32(memory_mapping->virt_addr, endian); | |
254 | ||
2cac2607 LE |
255 | assert(memory_mapping->length >= filesz); |
256 | ||
783e9b48 WC |
257 | ret = fd_write_vmcore(&phdr, sizeof(Elf32_Phdr), s); |
258 | if (ret < 0) { | |
259 | dump_error(s, "dump: failed to write program header table.\n"); | |
260 | return -1; | |
261 | } | |
262 | ||
263 | return 0; | |
264 | } | |
265 | ||
266 | static int write_elf64_note(DumpState *s) | |
267 | { | |
268 | Elf64_Phdr phdr; | |
269 | int endian = s->dump_info.d_endian; | |
a8170e5e | 270 | hwaddr begin = s->memory_offset - s->note_size; |
783e9b48 WC |
271 | int ret; |
272 | ||
273 | memset(&phdr, 0, sizeof(Elf64_Phdr)); | |
274 | phdr.p_type = cpu_convert_to_target32(PT_NOTE, endian); | |
275 | phdr.p_offset = cpu_convert_to_target64(begin, endian); | |
276 | phdr.p_paddr = 0; | |
277 | phdr.p_filesz = cpu_convert_to_target64(s->note_size, endian); | |
278 | phdr.p_memsz = cpu_convert_to_target64(s->note_size, endian); | |
279 | phdr.p_vaddr = 0; | |
280 | ||
281 | ret = fd_write_vmcore(&phdr, sizeof(Elf64_Phdr), s); | |
282 | if (ret < 0) { | |
283 | dump_error(s, "dump: failed to write program header table.\n"); | |
284 | return -1; | |
285 | } | |
286 | ||
287 | return 0; | |
288 | } | |
289 | ||
0bc3cd62 PB |
290 | static inline int cpu_index(CPUState *cpu) |
291 | { | |
292 | return cpu->cpu_index + 1; | |
293 | } | |
294 | ||
6a519918 | 295 | static int write_elf64_notes(WriteCoreDumpFunction f, DumpState *s) |
783e9b48 | 296 | { |
0d34282f | 297 | CPUState *cpu; |
783e9b48 WC |
298 | int ret; |
299 | int id; | |
300 | ||
bdc44640 | 301 | CPU_FOREACH(cpu) { |
0d34282f | 302 | id = cpu_index(cpu); |
6a519918 | 303 | ret = cpu_write_elf64_note(f, cpu, id, s); |
783e9b48 WC |
304 | if (ret < 0) { |
305 | dump_error(s, "dump: failed to write elf notes.\n"); | |
306 | return -1; | |
307 | } | |
308 | } | |
309 | ||
bdc44640 | 310 | CPU_FOREACH(cpu) { |
6a519918 | 311 | ret = cpu_write_elf64_qemunote(f, cpu, s); |
783e9b48 WC |
312 | if (ret < 0) { |
313 | dump_error(s, "dump: failed to write CPU status.\n"); | |
314 | return -1; | |
315 | } | |
316 | } | |
317 | ||
318 | return 0; | |
319 | } | |
320 | ||
321 | static int write_elf32_note(DumpState *s) | |
322 | { | |
a8170e5e | 323 | hwaddr begin = s->memory_offset - s->note_size; |
783e9b48 WC |
324 | Elf32_Phdr phdr; |
325 | int endian = s->dump_info.d_endian; | |
326 | int ret; | |
327 | ||
328 | memset(&phdr, 0, sizeof(Elf32_Phdr)); | |
329 | phdr.p_type = cpu_convert_to_target32(PT_NOTE, endian); | |
330 | phdr.p_offset = cpu_convert_to_target32(begin, endian); | |
331 | phdr.p_paddr = 0; | |
332 | phdr.p_filesz = cpu_convert_to_target32(s->note_size, endian); | |
333 | phdr.p_memsz = cpu_convert_to_target32(s->note_size, endian); | |
334 | phdr.p_vaddr = 0; | |
335 | ||
336 | ret = fd_write_vmcore(&phdr, sizeof(Elf32_Phdr), s); | |
337 | if (ret < 0) { | |
338 | dump_error(s, "dump: failed to write program header table.\n"); | |
339 | return -1; | |
340 | } | |
341 | ||
342 | return 0; | |
343 | } | |
344 | ||
6a519918 | 345 | static int write_elf32_notes(WriteCoreDumpFunction f, DumpState *s) |
783e9b48 | 346 | { |
0d34282f | 347 | CPUState *cpu; |
783e9b48 WC |
348 | int ret; |
349 | int id; | |
350 | ||
bdc44640 | 351 | CPU_FOREACH(cpu) { |
0d34282f | 352 | id = cpu_index(cpu); |
6a519918 | 353 | ret = cpu_write_elf32_note(f, cpu, id, s); |
783e9b48 WC |
354 | if (ret < 0) { |
355 | dump_error(s, "dump: failed to write elf notes.\n"); | |
356 | return -1; | |
357 | } | |
358 | } | |
359 | ||
bdc44640 | 360 | CPU_FOREACH(cpu) { |
6a519918 | 361 | ret = cpu_write_elf32_qemunote(f, cpu, s); |
783e9b48 WC |
362 | if (ret < 0) { |
363 | dump_error(s, "dump: failed to write CPU status.\n"); | |
364 | return -1; | |
365 | } | |
366 | } | |
367 | ||
368 | return 0; | |
369 | } | |
370 | ||
371 | static int write_elf_section(DumpState *s, int type) | |
372 | { | |
373 | Elf32_Shdr shdr32; | |
374 | Elf64_Shdr shdr64; | |
375 | int endian = s->dump_info.d_endian; | |
376 | int shdr_size; | |
377 | void *shdr; | |
378 | int ret; | |
379 | ||
380 | if (type == 0) { | |
381 | shdr_size = sizeof(Elf32_Shdr); | |
382 | memset(&shdr32, 0, shdr_size); | |
383 | shdr32.sh_info = cpu_convert_to_target32(s->sh_info, endian); | |
384 | shdr = &shdr32; | |
385 | } else { | |
386 | shdr_size = sizeof(Elf64_Shdr); | |
387 | memset(&shdr64, 0, shdr_size); | |
388 | shdr64.sh_info = cpu_convert_to_target32(s->sh_info, endian); | |
389 | shdr = &shdr64; | |
390 | } | |
391 | ||
392 | ret = fd_write_vmcore(&shdr, shdr_size, s); | |
393 | if (ret < 0) { | |
394 | dump_error(s, "dump: failed to write section header table.\n"); | |
395 | return -1; | |
396 | } | |
397 | ||
398 | return 0; | |
399 | } | |
400 | ||
401 | static int write_data(DumpState *s, void *buf, int length) | |
402 | { | |
403 | int ret; | |
404 | ||
405 | ret = fd_write_vmcore(buf, length, s); | |
406 | if (ret < 0) { | |
407 | dump_error(s, "dump: failed to save memory.\n"); | |
408 | return -1; | |
409 | } | |
410 | ||
411 | return 0; | |
412 | } | |
413 | ||
414 | /* write the memroy to vmcore. 1 page per I/O. */ | |
56c4bfb3 | 415 | static int write_memory(DumpState *s, GuestPhysBlock *block, ram_addr_t start, |
783e9b48 WC |
416 | int64_t size) |
417 | { | |
418 | int64_t i; | |
419 | int ret; | |
420 | ||
421 | for (i = 0; i < size / TARGET_PAGE_SIZE; i++) { | |
56c4bfb3 | 422 | ret = write_data(s, block->host_addr + start + i * TARGET_PAGE_SIZE, |
783e9b48 WC |
423 | TARGET_PAGE_SIZE); |
424 | if (ret < 0) { | |
425 | return ret; | |
426 | } | |
427 | } | |
428 | ||
429 | if ((size % TARGET_PAGE_SIZE) != 0) { | |
56c4bfb3 | 430 | ret = write_data(s, block->host_addr + start + i * TARGET_PAGE_SIZE, |
783e9b48 WC |
431 | size % TARGET_PAGE_SIZE); |
432 | if (ret < 0) { | |
433 | return ret; | |
434 | } | |
435 | } | |
436 | ||
437 | return 0; | |
438 | } | |
439 | ||
2cac2607 LE |
440 | /* get the memory's offset and size in the vmcore */ |
441 | static void get_offset_range(hwaddr phys_addr, | |
442 | ram_addr_t mapping_length, | |
443 | DumpState *s, | |
444 | hwaddr *p_offset, | |
445 | hwaddr *p_filesz) | |
783e9b48 | 446 | { |
56c4bfb3 | 447 | GuestPhysBlock *block; |
a8170e5e | 448 | hwaddr offset = s->memory_offset; |
783e9b48 WC |
449 | int64_t size_in_block, start; |
450 | ||
2cac2607 LE |
451 | /* When the memory is not stored into vmcore, offset will be -1 */ |
452 | *p_offset = -1; | |
453 | *p_filesz = 0; | |
454 | ||
783e9b48 WC |
455 | if (s->has_filter) { |
456 | if (phys_addr < s->begin || phys_addr >= s->begin + s->length) { | |
2cac2607 | 457 | return; |
783e9b48 WC |
458 | } |
459 | } | |
460 | ||
56c4bfb3 | 461 | QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) { |
783e9b48 | 462 | if (s->has_filter) { |
56c4bfb3 LE |
463 | if (block->target_start >= s->begin + s->length || |
464 | block->target_end <= s->begin) { | |
783e9b48 WC |
465 | /* This block is out of the range */ |
466 | continue; | |
467 | } | |
468 | ||
56c4bfb3 LE |
469 | if (s->begin <= block->target_start) { |
470 | start = block->target_start; | |
783e9b48 WC |
471 | } else { |
472 | start = s->begin; | |
473 | } | |
474 | ||
56c4bfb3 LE |
475 | size_in_block = block->target_end - start; |
476 | if (s->begin + s->length < block->target_end) { | |
477 | size_in_block -= block->target_end - (s->begin + s->length); | |
783e9b48 WC |
478 | } |
479 | } else { | |
56c4bfb3 LE |
480 | start = block->target_start; |
481 | size_in_block = block->target_end - block->target_start; | |
783e9b48 WC |
482 | } |
483 | ||
484 | if (phys_addr >= start && phys_addr < start + size_in_block) { | |
2cac2607 LE |
485 | *p_offset = phys_addr - start + offset; |
486 | ||
487 | /* The offset range mapped from the vmcore file must not spill over | |
56c4bfb3 | 488 | * the GuestPhysBlock, clamp it. The rest of the mapping will be |
2cac2607 LE |
489 | * zero-filled in memory at load time; see |
490 | * <http://refspecs.linuxbase.org/elf/gabi4+/ch5.pheader.html>. | |
491 | */ | |
492 | *p_filesz = phys_addr + mapping_length <= start + size_in_block ? | |
493 | mapping_length : | |
494 | size_in_block - (phys_addr - start); | |
495 | return; | |
783e9b48 WC |
496 | } |
497 | ||
498 | offset += size_in_block; | |
499 | } | |
783e9b48 WC |
500 | } |
501 | ||
502 | static int write_elf_loads(DumpState *s) | |
503 | { | |
2cac2607 | 504 | hwaddr offset, filesz; |
783e9b48 WC |
505 | MemoryMapping *memory_mapping; |
506 | uint32_t phdr_index = 1; | |
507 | int ret; | |
508 | uint32_t max_index; | |
509 | ||
510 | if (s->have_section) { | |
511 | max_index = s->sh_info; | |
512 | } else { | |
513 | max_index = s->phdr_num; | |
514 | } | |
515 | ||
516 | QTAILQ_FOREACH(memory_mapping, &s->list.head, next) { | |
2cac2607 LE |
517 | get_offset_range(memory_mapping->phys_addr, |
518 | memory_mapping->length, | |
519 | s, &offset, &filesz); | |
783e9b48 | 520 | if (s->dump_info.d_class == ELFCLASS64) { |
2cac2607 LE |
521 | ret = write_elf64_load(s, memory_mapping, phdr_index++, offset, |
522 | filesz); | |
783e9b48 | 523 | } else { |
2cac2607 LE |
524 | ret = write_elf32_load(s, memory_mapping, phdr_index++, offset, |
525 | filesz); | |
783e9b48 WC |
526 | } |
527 | ||
528 | if (ret < 0) { | |
529 | return -1; | |
530 | } | |
531 | ||
532 | if (phdr_index >= max_index) { | |
533 | break; | |
534 | } | |
535 | } | |
536 | ||
537 | return 0; | |
538 | } | |
539 | ||
540 | /* write elf header, PT_NOTE and elf note to vmcore. */ | |
541 | static int dump_begin(DumpState *s) | |
542 | { | |
543 | int ret; | |
544 | ||
545 | /* | |
546 | * the vmcore's format is: | |
547 | * -------------- | |
548 | * | elf header | | |
549 | * -------------- | |
550 | * | PT_NOTE | | |
551 | * -------------- | |
552 | * | PT_LOAD | | |
553 | * -------------- | |
554 | * | ...... | | |
555 | * -------------- | |
556 | * | PT_LOAD | | |
557 | * -------------- | |
558 | * | sec_hdr | | |
559 | * -------------- | |
560 | * | elf note | | |
561 | * -------------- | |
562 | * | memory | | |
563 | * -------------- | |
564 | * | |
565 | * we only know where the memory is saved after we write elf note into | |
566 | * vmcore. | |
567 | */ | |
568 | ||
569 | /* write elf header to vmcore */ | |
570 | if (s->dump_info.d_class == ELFCLASS64) { | |
571 | ret = write_elf64_header(s); | |
572 | } else { | |
573 | ret = write_elf32_header(s); | |
574 | } | |
575 | if (ret < 0) { | |
576 | return -1; | |
577 | } | |
578 | ||
579 | if (s->dump_info.d_class == ELFCLASS64) { | |
580 | /* write PT_NOTE to vmcore */ | |
581 | if (write_elf64_note(s) < 0) { | |
582 | return -1; | |
583 | } | |
584 | ||
585 | /* write all PT_LOAD to vmcore */ | |
586 | if (write_elf_loads(s) < 0) { | |
587 | return -1; | |
588 | } | |
589 | ||
590 | /* write section to vmcore */ | |
591 | if (s->have_section) { | |
592 | if (write_elf_section(s, 1) < 0) { | |
593 | return -1; | |
594 | } | |
595 | } | |
596 | ||
597 | /* write notes to vmcore */ | |
6a519918 | 598 | if (write_elf64_notes(fd_write_vmcore, s) < 0) { |
783e9b48 WC |
599 | return -1; |
600 | } | |
601 | ||
602 | } else { | |
603 | /* write PT_NOTE to vmcore */ | |
604 | if (write_elf32_note(s) < 0) { | |
605 | return -1; | |
606 | } | |
607 | ||
608 | /* write all PT_LOAD to vmcore */ | |
609 | if (write_elf_loads(s) < 0) { | |
610 | return -1; | |
611 | } | |
612 | ||
613 | /* write section to vmcore */ | |
614 | if (s->have_section) { | |
615 | if (write_elf_section(s, 0) < 0) { | |
616 | return -1; | |
617 | } | |
618 | } | |
619 | ||
620 | /* write notes to vmcore */ | |
6a519918 | 621 | if (write_elf32_notes(fd_write_vmcore, s) < 0) { |
783e9b48 WC |
622 | return -1; |
623 | } | |
624 | } | |
625 | ||
626 | return 0; | |
627 | } | |
628 | ||
629 | /* write PT_LOAD to vmcore */ | |
630 | static int dump_completed(DumpState *s) | |
631 | { | |
632 | dump_cleanup(s); | |
633 | return 0; | |
634 | } | |
635 | ||
56c4bfb3 | 636 | static int get_next_block(DumpState *s, GuestPhysBlock *block) |
783e9b48 WC |
637 | { |
638 | while (1) { | |
a3161038 | 639 | block = QTAILQ_NEXT(block, next); |
783e9b48 WC |
640 | if (!block) { |
641 | /* no more block */ | |
642 | return 1; | |
643 | } | |
644 | ||
645 | s->start = 0; | |
56c4bfb3 | 646 | s->next_block = block; |
783e9b48 | 647 | if (s->has_filter) { |
56c4bfb3 LE |
648 | if (block->target_start >= s->begin + s->length || |
649 | block->target_end <= s->begin) { | |
783e9b48 WC |
650 | /* This block is out of the range */ |
651 | continue; | |
652 | } | |
653 | ||
56c4bfb3 LE |
654 | if (s->begin > block->target_start) { |
655 | s->start = s->begin - block->target_start; | |
783e9b48 WC |
656 | } |
657 | } | |
658 | ||
659 | return 0; | |
660 | } | |
661 | } | |
662 | ||
663 | /* write all memory to vmcore */ | |
664 | static int dump_iterate(DumpState *s) | |
665 | { | |
56c4bfb3 | 666 | GuestPhysBlock *block; |
783e9b48 WC |
667 | int64_t size; |
668 | int ret; | |
669 | ||
670 | while (1) { | |
56c4bfb3 | 671 | block = s->next_block; |
783e9b48 | 672 | |
56c4bfb3 | 673 | size = block->target_end - block->target_start; |
783e9b48 WC |
674 | if (s->has_filter) { |
675 | size -= s->start; | |
56c4bfb3 LE |
676 | if (s->begin + s->length < block->target_end) { |
677 | size -= block->target_end - (s->begin + s->length); | |
783e9b48 WC |
678 | } |
679 | } | |
680 | ret = write_memory(s, block, s->start, size); | |
681 | if (ret == -1) { | |
682 | return ret; | |
683 | } | |
684 | ||
685 | ret = get_next_block(s, block); | |
686 | if (ret == 1) { | |
687 | dump_completed(s); | |
688 | return 0; | |
689 | } | |
690 | } | |
691 | } | |
692 | ||
693 | static int create_vmcore(DumpState *s) | |
694 | { | |
695 | int ret; | |
696 | ||
697 | ret = dump_begin(s); | |
698 | if (ret < 0) { | |
699 | return -1; | |
700 | } | |
701 | ||
702 | ret = dump_iterate(s); | |
703 | if (ret < 0) { | |
704 | return -1; | |
705 | } | |
706 | ||
707 | return 0; | |
708 | } | |
709 | ||
fda05387 QN |
710 | static int write_start_flat_header(int fd) |
711 | { | |
92ba1401 | 712 | MakedumpfileHeader *mh; |
fda05387 QN |
713 | int ret = 0; |
714 | ||
92ba1401 LE |
715 | QEMU_BUILD_BUG_ON(sizeof *mh > MAX_SIZE_MDF_HEADER); |
716 | mh = g_malloc0(MAX_SIZE_MDF_HEADER); | |
fda05387 | 717 | |
92ba1401 LE |
718 | memcpy(mh->signature, MAKEDUMPFILE_SIGNATURE, |
719 | MIN(sizeof mh->signature, sizeof MAKEDUMPFILE_SIGNATURE)); | |
fda05387 | 720 | |
92ba1401 LE |
721 | mh->type = cpu_to_be64(TYPE_FLAT_HEADER); |
722 | mh->version = cpu_to_be64(VERSION_FLAT_HEADER); | |
fda05387 QN |
723 | |
724 | size_t written_size; | |
92ba1401 | 725 | written_size = qemu_write_full(fd, mh, MAX_SIZE_MDF_HEADER); |
fda05387 QN |
726 | if (written_size != MAX_SIZE_MDF_HEADER) { |
727 | ret = -1; | |
728 | } | |
729 | ||
92ba1401 | 730 | g_free(mh); |
fda05387 QN |
731 | return ret; |
732 | } | |
733 | ||
734 | static int write_end_flat_header(int fd) | |
735 | { | |
736 | MakedumpfileDataHeader mdh; | |
737 | ||
738 | mdh.offset = END_FLAG_FLAT_HEADER; | |
739 | mdh.buf_size = END_FLAG_FLAT_HEADER; | |
740 | ||
741 | size_t written_size; | |
742 | written_size = qemu_write_full(fd, &mdh, sizeof(mdh)); | |
743 | if (written_size != sizeof(mdh)) { | |
744 | return -1; | |
745 | } | |
746 | ||
747 | return 0; | |
748 | } | |
749 | ||
5d31babe QN |
750 | static int write_buffer(int fd, off_t offset, const void *buf, size_t size) |
751 | { | |
752 | size_t written_size; | |
753 | MakedumpfileDataHeader mdh; | |
754 | ||
755 | mdh.offset = cpu_to_be64(offset); | |
756 | mdh.buf_size = cpu_to_be64(size); | |
757 | ||
758 | written_size = qemu_write_full(fd, &mdh, sizeof(mdh)); | |
759 | if (written_size != sizeof(mdh)) { | |
760 | return -1; | |
761 | } | |
762 | ||
763 | written_size = qemu_write_full(fd, buf, size); | |
764 | if (written_size != size) { | |
765 | return -1; | |
766 | } | |
767 | ||
768 | return 0; | |
769 | } | |
770 | ||
4835ef77 QN |
771 | static int buf_write_note(const void *buf, size_t size, void *opaque) |
772 | { | |
773 | DumpState *s = opaque; | |
774 | ||
775 | /* note_buf is not enough */ | |
776 | if (s->note_buf_offset + size > s->note_size) { | |
777 | return -1; | |
778 | } | |
779 | ||
780 | memcpy(s->note_buf + s->note_buf_offset, buf, size); | |
781 | ||
782 | s->note_buf_offset += size; | |
783 | ||
784 | return 0; | |
785 | } | |
786 | ||
298f1168 QN |
787 | /* write common header, sub header and elf note to vmcore */ |
788 | static int create_header32(DumpState *s) | |
789 | { | |
790 | int ret = 0; | |
791 | DiskDumpHeader32 *dh = NULL; | |
792 | KdumpSubHeader32 *kh = NULL; | |
793 | size_t size; | |
794 | int endian = s->dump_info.d_endian; | |
795 | uint32_t block_size; | |
796 | uint32_t sub_hdr_size; | |
797 | uint32_t bitmap_blocks; | |
798 | uint32_t status = 0; | |
799 | uint64_t offset_note; | |
800 | ||
801 | /* write common header, the version of kdump-compressed format is 6th */ | |
802 | size = sizeof(DiskDumpHeader32); | |
803 | dh = g_malloc0(size); | |
804 | ||
805 | strncpy(dh->signature, KDUMP_SIGNATURE, strlen(KDUMP_SIGNATURE)); | |
806 | dh->header_version = cpu_convert_to_target32(6, endian); | |
2f859f80 | 807 | block_size = TARGET_PAGE_SIZE; |
298f1168 QN |
808 | dh->block_size = cpu_convert_to_target32(block_size, endian); |
809 | sub_hdr_size = sizeof(struct KdumpSubHeader32) + s->note_size; | |
810 | sub_hdr_size = DIV_ROUND_UP(sub_hdr_size, block_size); | |
811 | dh->sub_hdr_size = cpu_convert_to_target32(sub_hdr_size, endian); | |
812 | /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */ | |
813 | dh->max_mapnr = cpu_convert_to_target32(MIN(s->max_mapnr, UINT_MAX), | |
814 | endian); | |
815 | dh->nr_cpus = cpu_convert_to_target32(s->nr_cpus, endian); | |
816 | bitmap_blocks = DIV_ROUND_UP(s->len_dump_bitmap, block_size) * 2; | |
817 | dh->bitmap_blocks = cpu_convert_to_target32(bitmap_blocks, endian); | |
4ab23a91 | 818 | strncpy(dh->utsname.machine, ELF_MACHINE_UNAME, sizeof(dh->utsname.machine)); |
298f1168 QN |
819 | |
820 | if (s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) { | |
821 | status |= DUMP_DH_COMPRESSED_ZLIB; | |
822 | } | |
823 | #ifdef CONFIG_LZO | |
824 | if (s->flag_compress & DUMP_DH_COMPRESSED_LZO) { | |
825 | status |= DUMP_DH_COMPRESSED_LZO; | |
826 | } | |
827 | #endif | |
828 | #ifdef CONFIG_SNAPPY | |
829 | if (s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) { | |
830 | status |= DUMP_DH_COMPRESSED_SNAPPY; | |
831 | } | |
832 | #endif | |
833 | dh->status = cpu_convert_to_target32(status, endian); | |
834 | ||
835 | if (write_buffer(s->fd, 0, dh, size) < 0) { | |
836 | dump_error(s, "dump: failed to write disk dump header.\n"); | |
837 | ret = -1; | |
838 | goto out; | |
839 | } | |
840 | ||
841 | /* write sub header */ | |
842 | size = sizeof(KdumpSubHeader32); | |
843 | kh = g_malloc0(size); | |
844 | ||
845 | /* 64bit max_mapnr_64 */ | |
846 | kh->max_mapnr_64 = cpu_convert_to_target64(s->max_mapnr, endian); | |
847 | kh->phys_base = cpu_convert_to_target32(PHYS_BASE, endian); | |
848 | kh->dump_level = cpu_convert_to_target32(DUMP_LEVEL, endian); | |
849 | ||
850 | offset_note = DISKDUMP_HEADER_BLOCKS * block_size + size; | |
851 | kh->offset_note = cpu_convert_to_target64(offset_note, endian); | |
852 | kh->note_size = cpu_convert_to_target32(s->note_size, endian); | |
853 | ||
854 | if (write_buffer(s->fd, DISKDUMP_HEADER_BLOCKS * | |
855 | block_size, kh, size) < 0) { | |
856 | dump_error(s, "dump: failed to write kdump sub header.\n"); | |
857 | ret = -1; | |
858 | goto out; | |
859 | } | |
860 | ||
861 | /* write note */ | |
862 | s->note_buf = g_malloc0(s->note_size); | |
863 | s->note_buf_offset = 0; | |
864 | ||
865 | /* use s->note_buf to store notes temporarily */ | |
866 | if (write_elf32_notes(buf_write_note, s) < 0) { | |
867 | ret = -1; | |
868 | goto out; | |
869 | } | |
870 | ||
871 | if (write_buffer(s->fd, offset_note, s->note_buf, | |
872 | s->note_size) < 0) { | |
873 | dump_error(s, "dump: failed to write notes"); | |
874 | ret = -1; | |
875 | goto out; | |
876 | } | |
877 | ||
878 | /* get offset of dump_bitmap */ | |
879 | s->offset_dump_bitmap = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size) * | |
880 | block_size; | |
881 | ||
882 | /* get offset of page */ | |
883 | s->offset_page = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size + bitmap_blocks) * | |
884 | block_size; | |
885 | ||
886 | out: | |
887 | g_free(dh); | |
888 | g_free(kh); | |
889 | g_free(s->note_buf); | |
890 | ||
891 | return ret; | |
892 | } | |
893 | ||
894 | /* write common header, sub header and elf note to vmcore */ | |
895 | static int create_header64(DumpState *s) | |
896 | { | |
897 | int ret = 0; | |
898 | DiskDumpHeader64 *dh = NULL; | |
899 | KdumpSubHeader64 *kh = NULL; | |
900 | size_t size; | |
901 | int endian = s->dump_info.d_endian; | |
902 | uint32_t block_size; | |
903 | uint32_t sub_hdr_size; | |
904 | uint32_t bitmap_blocks; | |
905 | uint32_t status = 0; | |
906 | uint64_t offset_note; | |
907 | ||
908 | /* write common header, the version of kdump-compressed format is 6th */ | |
909 | size = sizeof(DiskDumpHeader64); | |
910 | dh = g_malloc0(size); | |
911 | ||
912 | strncpy(dh->signature, KDUMP_SIGNATURE, strlen(KDUMP_SIGNATURE)); | |
913 | dh->header_version = cpu_convert_to_target32(6, endian); | |
2f859f80 | 914 | block_size = TARGET_PAGE_SIZE; |
298f1168 QN |
915 | dh->block_size = cpu_convert_to_target32(block_size, endian); |
916 | sub_hdr_size = sizeof(struct KdumpSubHeader64) + s->note_size; | |
917 | sub_hdr_size = DIV_ROUND_UP(sub_hdr_size, block_size); | |
918 | dh->sub_hdr_size = cpu_convert_to_target32(sub_hdr_size, endian); | |
919 | /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */ | |
920 | dh->max_mapnr = cpu_convert_to_target32(MIN(s->max_mapnr, UINT_MAX), | |
921 | endian); | |
922 | dh->nr_cpus = cpu_convert_to_target32(s->nr_cpus, endian); | |
923 | bitmap_blocks = DIV_ROUND_UP(s->len_dump_bitmap, block_size) * 2; | |
924 | dh->bitmap_blocks = cpu_convert_to_target32(bitmap_blocks, endian); | |
4ab23a91 | 925 | strncpy(dh->utsname.machine, ELF_MACHINE_UNAME, sizeof(dh->utsname.machine)); |
298f1168 QN |
926 | |
927 | if (s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) { | |
928 | status |= DUMP_DH_COMPRESSED_ZLIB; | |
929 | } | |
930 | #ifdef CONFIG_LZO | |
931 | if (s->flag_compress & DUMP_DH_COMPRESSED_LZO) { | |
932 | status |= DUMP_DH_COMPRESSED_LZO; | |
933 | } | |
934 | #endif | |
935 | #ifdef CONFIG_SNAPPY | |
936 | if (s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) { | |
937 | status |= DUMP_DH_COMPRESSED_SNAPPY; | |
938 | } | |
939 | #endif | |
940 | dh->status = cpu_convert_to_target32(status, endian); | |
941 | ||
942 | if (write_buffer(s->fd, 0, dh, size) < 0) { | |
943 | dump_error(s, "dump: failed to write disk dump header.\n"); | |
944 | ret = -1; | |
945 | goto out; | |
946 | } | |
947 | ||
948 | /* write sub header */ | |
949 | size = sizeof(KdumpSubHeader64); | |
950 | kh = g_malloc0(size); | |
951 | ||
952 | /* 64bit max_mapnr_64 */ | |
953 | kh->max_mapnr_64 = cpu_convert_to_target64(s->max_mapnr, endian); | |
954 | kh->phys_base = cpu_convert_to_target64(PHYS_BASE, endian); | |
955 | kh->dump_level = cpu_convert_to_target32(DUMP_LEVEL, endian); | |
956 | ||
957 | offset_note = DISKDUMP_HEADER_BLOCKS * block_size + size; | |
958 | kh->offset_note = cpu_convert_to_target64(offset_note, endian); | |
959 | kh->note_size = cpu_convert_to_target64(s->note_size, endian); | |
960 | ||
961 | if (write_buffer(s->fd, DISKDUMP_HEADER_BLOCKS * | |
962 | block_size, kh, size) < 0) { | |
963 | dump_error(s, "dump: failed to write kdump sub header.\n"); | |
964 | ret = -1; | |
965 | goto out; | |
966 | } | |
967 | ||
968 | /* write note */ | |
969 | s->note_buf = g_malloc0(s->note_size); | |
970 | s->note_buf_offset = 0; | |
971 | ||
972 | /* use s->note_buf to store notes temporarily */ | |
973 | if (write_elf64_notes(buf_write_note, s) < 0) { | |
974 | ret = -1; | |
975 | goto out; | |
976 | } | |
977 | ||
978 | if (write_buffer(s->fd, offset_note, s->note_buf, | |
979 | s->note_size) < 0) { | |
980 | dump_error(s, "dump: failed to write notes"); | |
981 | ret = -1; | |
982 | goto out; | |
983 | } | |
984 | ||
985 | /* get offset of dump_bitmap */ | |
986 | s->offset_dump_bitmap = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size) * | |
987 | block_size; | |
988 | ||
989 | /* get offset of page */ | |
990 | s->offset_page = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size + bitmap_blocks) * | |
991 | block_size; | |
992 | ||
993 | out: | |
994 | g_free(dh); | |
995 | g_free(kh); | |
996 | g_free(s->note_buf); | |
997 | ||
998 | return ret; | |
999 | } | |
1000 | ||
1001 | static int write_dump_header(DumpState *s) | |
1002 | { | |
24aeeace | 1003 | if (s->dump_info.d_class == ELFCLASS32) { |
298f1168 QN |
1004 | return create_header32(s); |
1005 | } else { | |
1006 | return create_header64(s); | |
1007 | } | |
1008 | } | |
1009 | ||
d0686c72 QN |
1010 | /* |
1011 | * set dump_bitmap sequencely. the bit before last_pfn is not allowed to be | |
1012 | * rewritten, so if need to set the first bit, set last_pfn and pfn to 0. | |
1013 | * set_dump_bitmap will always leave the recently set bit un-sync. And setting | |
1014 | * (last bit + sizeof(buf) * 8) to 0 will do flushing the content in buf into | |
1015 | * vmcore, ie. synchronizing un-sync bit into vmcore. | |
1016 | */ | |
1017 | static int set_dump_bitmap(uint64_t last_pfn, uint64_t pfn, bool value, | |
1018 | uint8_t *buf, DumpState *s) | |
1019 | { | |
1020 | off_t old_offset, new_offset; | |
1021 | off_t offset_bitmap1, offset_bitmap2; | |
1022 | uint32_t byte, bit; | |
1023 | ||
1024 | /* should not set the previous place */ | |
1025 | assert(last_pfn <= pfn); | |
1026 | ||
1027 | /* | |
1028 | * if the bit needed to be set is not cached in buf, flush the data in buf | |
1029 | * to vmcore firstly. | |
1030 | * making new_offset be bigger than old_offset can also sync remained data | |
1031 | * into vmcore. | |
1032 | */ | |
1033 | old_offset = BUFSIZE_BITMAP * (last_pfn / PFN_BUFBITMAP); | |
1034 | new_offset = BUFSIZE_BITMAP * (pfn / PFN_BUFBITMAP); | |
1035 | ||
1036 | while (old_offset < new_offset) { | |
1037 | /* calculate the offset and write dump_bitmap */ | |
1038 | offset_bitmap1 = s->offset_dump_bitmap + old_offset; | |
1039 | if (write_buffer(s->fd, offset_bitmap1, buf, | |
1040 | BUFSIZE_BITMAP) < 0) { | |
1041 | return -1; | |
1042 | } | |
1043 | ||
1044 | /* dump level 1 is chosen, so 1st and 2nd bitmap are same */ | |
1045 | offset_bitmap2 = s->offset_dump_bitmap + s->len_dump_bitmap + | |
1046 | old_offset; | |
1047 | if (write_buffer(s->fd, offset_bitmap2, buf, | |
1048 | BUFSIZE_BITMAP) < 0) { | |
1049 | return -1; | |
1050 | } | |
1051 | ||
1052 | memset(buf, 0, BUFSIZE_BITMAP); | |
1053 | old_offset += BUFSIZE_BITMAP; | |
1054 | } | |
1055 | ||
1056 | /* get the exact place of the bit in the buf, and set it */ | |
1057 | byte = (pfn % PFN_BUFBITMAP) / CHAR_BIT; | |
1058 | bit = (pfn % PFN_BUFBITMAP) % CHAR_BIT; | |
1059 | if (value) { | |
1060 | buf[byte] |= 1u << bit; | |
1061 | } else { | |
1062 | buf[byte] &= ~(1u << bit); | |
1063 | } | |
1064 | ||
1065 | return 0; | |
1066 | } | |
1067 | ||
1068 | /* | |
1069 | * exam every page and return the page frame number and the address of the page. | |
1070 | * bufptr can be NULL. note: the blocks here is supposed to reflect guest-phys | |
1071 | * blocks, so block->target_start and block->target_end should be interal | |
1072 | * multiples of the target page size. | |
1073 | */ | |
1074 | static bool get_next_page(GuestPhysBlock **blockptr, uint64_t *pfnptr, | |
1075 | uint8_t **bufptr, DumpState *s) | |
1076 | { | |
1077 | GuestPhysBlock *block = *blockptr; | |
1078 | hwaddr addr; | |
1079 | uint8_t *buf; | |
1080 | ||
1081 | /* block == NULL means the start of the iteration */ | |
1082 | if (!block) { | |
1083 | block = QTAILQ_FIRST(&s->guest_phys_blocks.head); | |
1084 | *blockptr = block; | |
2f859f80 LE |
1085 | assert((block->target_start & ~TARGET_PAGE_MASK) == 0); |
1086 | assert((block->target_end & ~TARGET_PAGE_MASK) == 0); | |
22227f12 | 1087 | *pfnptr = paddr_to_pfn(block->target_start); |
d0686c72 QN |
1088 | if (bufptr) { |
1089 | *bufptr = block->host_addr; | |
1090 | } | |
1091 | return true; | |
1092 | } | |
1093 | ||
1094 | *pfnptr = *pfnptr + 1; | |
22227f12 | 1095 | addr = pfn_to_paddr(*pfnptr); |
d0686c72 QN |
1096 | |
1097 | if ((addr >= block->target_start) && | |
2f859f80 | 1098 | (addr + TARGET_PAGE_SIZE <= block->target_end)) { |
d0686c72 QN |
1099 | buf = block->host_addr + (addr - block->target_start); |
1100 | } else { | |
1101 | /* the next page is in the next block */ | |
1102 | block = QTAILQ_NEXT(block, next); | |
1103 | *blockptr = block; | |
1104 | if (!block) { | |
1105 | return false; | |
1106 | } | |
2f859f80 LE |
1107 | assert((block->target_start & ~TARGET_PAGE_MASK) == 0); |
1108 | assert((block->target_end & ~TARGET_PAGE_MASK) == 0); | |
22227f12 | 1109 | *pfnptr = paddr_to_pfn(block->target_start); |
d0686c72 QN |
1110 | buf = block->host_addr; |
1111 | } | |
1112 | ||
1113 | if (bufptr) { | |
1114 | *bufptr = buf; | |
1115 | } | |
1116 | ||
1117 | return true; | |
1118 | } | |
1119 | ||
1120 | static int write_dump_bitmap(DumpState *s) | |
1121 | { | |
1122 | int ret = 0; | |
1123 | uint64_t last_pfn, pfn; | |
1124 | void *dump_bitmap_buf; | |
1125 | size_t num_dumpable; | |
1126 | GuestPhysBlock *block_iter = NULL; | |
1127 | ||
1128 | /* dump_bitmap_buf is used to store dump_bitmap temporarily */ | |
1129 | dump_bitmap_buf = g_malloc0(BUFSIZE_BITMAP); | |
1130 | ||
1131 | num_dumpable = 0; | |
1132 | last_pfn = 0; | |
1133 | ||
1134 | /* | |
1135 | * exam memory page by page, and set the bit in dump_bitmap corresponded | |
1136 | * to the existing page. | |
1137 | */ | |
1138 | while (get_next_page(&block_iter, &pfn, NULL, s)) { | |
1139 | ret = set_dump_bitmap(last_pfn, pfn, true, dump_bitmap_buf, s); | |
1140 | if (ret < 0) { | |
1141 | dump_error(s, "dump: failed to set dump_bitmap.\n"); | |
1142 | ret = -1; | |
1143 | goto out; | |
1144 | } | |
1145 | ||
1146 | last_pfn = pfn; | |
1147 | num_dumpable++; | |
1148 | } | |
1149 | ||
1150 | /* | |
1151 | * set_dump_bitmap will always leave the recently set bit un-sync. Here we | |
1152 | * set last_pfn + PFN_BUFBITMAP to 0 and those set but un-sync bit will be | |
1153 | * synchronized into vmcore. | |
1154 | */ | |
1155 | if (num_dumpable > 0) { | |
1156 | ret = set_dump_bitmap(last_pfn, last_pfn + PFN_BUFBITMAP, false, | |
1157 | dump_bitmap_buf, s); | |
1158 | if (ret < 0) { | |
1159 | dump_error(s, "dump: failed to sync dump_bitmap.\n"); | |
1160 | ret = -1; | |
1161 | goto out; | |
1162 | } | |
1163 | } | |
1164 | ||
1165 | /* number of dumpable pages that will be dumped later */ | |
1166 | s->num_dumpable = num_dumpable; | |
1167 | ||
1168 | out: | |
1169 | g_free(dump_bitmap_buf); | |
1170 | ||
1171 | return ret; | |
1172 | } | |
1173 | ||
64cfba6a QN |
1174 | static void prepare_data_cache(DataCache *data_cache, DumpState *s, |
1175 | off_t offset) | |
1176 | { | |
1177 | data_cache->fd = s->fd; | |
1178 | data_cache->data_size = 0; | |
1179 | data_cache->buf_size = BUFSIZE_DATA_CACHE; | |
1180 | data_cache->buf = g_malloc0(BUFSIZE_DATA_CACHE); | |
1181 | data_cache->offset = offset; | |
1182 | } | |
1183 | ||
1184 | static int write_cache(DataCache *dc, const void *buf, size_t size, | |
1185 | bool flag_sync) | |
1186 | { | |
1187 | /* | |
1188 | * dc->buf_size should not be less than size, otherwise dc will never be | |
1189 | * enough | |
1190 | */ | |
1191 | assert(size <= dc->buf_size); | |
1192 | ||
1193 | /* | |
1194 | * if flag_sync is set, synchronize data in dc->buf into vmcore. | |
1195 | * otherwise check if the space is enough for caching data in buf, if not, | |
1196 | * write the data in dc->buf to dc->fd and reset dc->buf | |
1197 | */ | |
1198 | if ((!flag_sync && dc->data_size + size > dc->buf_size) || | |
1199 | (flag_sync && dc->data_size > 0)) { | |
1200 | if (write_buffer(dc->fd, dc->offset, dc->buf, dc->data_size) < 0) { | |
1201 | return -1; | |
1202 | } | |
1203 | ||
1204 | dc->offset += dc->data_size; | |
1205 | dc->data_size = 0; | |
1206 | } | |
1207 | ||
1208 | if (!flag_sync) { | |
1209 | memcpy(dc->buf + dc->data_size, buf, size); | |
1210 | dc->data_size += size; | |
1211 | } | |
1212 | ||
1213 | return 0; | |
1214 | } | |
1215 | ||
1216 | static void free_data_cache(DataCache *data_cache) | |
1217 | { | |
1218 | g_free(data_cache->buf); | |
1219 | } | |
1220 | ||
d12f57ec QN |
1221 | static size_t get_len_buf_out(size_t page_size, uint32_t flag_compress) |
1222 | { | |
b87ef351 LE |
1223 | switch (flag_compress) { |
1224 | case DUMP_DH_COMPRESSED_ZLIB: | |
1225 | return compressBound(page_size); | |
1226 | ||
1227 | case DUMP_DH_COMPRESSED_LZO: | |
1228 | /* | |
1229 | * LZO will expand incompressible data by a little amount. Please check | |
1230 | * the following URL to see the expansion calculation: | |
1231 | * http://www.oberhumer.com/opensource/lzo/lzofaq.php | |
1232 | */ | |
1233 | return page_size + page_size / 16 + 64 + 3; | |
d12f57ec QN |
1234 | |
1235 | #ifdef CONFIG_SNAPPY | |
b87ef351 LE |
1236 | case DUMP_DH_COMPRESSED_SNAPPY: |
1237 | return snappy_max_compressed_length(page_size); | |
d12f57ec | 1238 | #endif |
b87ef351 LE |
1239 | } |
1240 | return 0; | |
d12f57ec QN |
1241 | } |
1242 | ||
1243 | /* | |
1244 | * check if the page is all 0 | |
1245 | */ | |
1246 | static inline bool is_zero_page(const uint8_t *buf, size_t page_size) | |
1247 | { | |
1248 | return buffer_is_zero(buf, page_size); | |
1249 | } | |
1250 | ||
1251 | static int write_dump_pages(DumpState *s) | |
1252 | { | |
1253 | int ret = 0; | |
1254 | DataCache page_desc, page_data; | |
1255 | size_t len_buf_out, size_out; | |
1256 | #ifdef CONFIG_LZO | |
1257 | lzo_bytep wrkmem = NULL; | |
1258 | #endif | |
1259 | uint8_t *buf_out = NULL; | |
1260 | off_t offset_desc, offset_data; | |
1261 | PageDescriptor pd, pd_zero; | |
1262 | uint8_t *buf; | |
1263 | int endian = s->dump_info.d_endian; | |
1264 | GuestPhysBlock *block_iter = NULL; | |
1265 | uint64_t pfn_iter; | |
1266 | ||
1267 | /* get offset of page_desc and page_data in dump file */ | |
1268 | offset_desc = s->offset_page; | |
1269 | offset_data = offset_desc + sizeof(PageDescriptor) * s->num_dumpable; | |
1270 | ||
1271 | prepare_data_cache(&page_desc, s, offset_desc); | |
1272 | prepare_data_cache(&page_data, s, offset_data); | |
1273 | ||
1274 | /* prepare buffer to store compressed data */ | |
2f859f80 | 1275 | len_buf_out = get_len_buf_out(TARGET_PAGE_SIZE, s->flag_compress); |
b87ef351 | 1276 | assert(len_buf_out != 0); |
d12f57ec QN |
1277 | |
1278 | #ifdef CONFIG_LZO | |
1279 | wrkmem = g_malloc(LZO1X_1_MEM_COMPRESS); | |
1280 | #endif | |
1281 | ||
1282 | buf_out = g_malloc(len_buf_out); | |
1283 | ||
1284 | /* | |
1285 | * init zero page's page_desc and page_data, because every zero page | |
1286 | * uses the same page_data | |
1287 | */ | |
2f859f80 | 1288 | pd_zero.size = cpu_convert_to_target32(TARGET_PAGE_SIZE, endian); |
d12f57ec QN |
1289 | pd_zero.flags = cpu_convert_to_target32(0, endian); |
1290 | pd_zero.offset = cpu_convert_to_target64(offset_data, endian); | |
1291 | pd_zero.page_flags = cpu_convert_to_target64(0, endian); | |
2f859f80 LE |
1292 | buf = g_malloc0(TARGET_PAGE_SIZE); |
1293 | ret = write_cache(&page_data, buf, TARGET_PAGE_SIZE, false); | |
d12f57ec QN |
1294 | g_free(buf); |
1295 | if (ret < 0) { | |
1296 | dump_error(s, "dump: failed to write page data(zero page).\n"); | |
1297 | goto out; | |
1298 | } | |
1299 | ||
2f859f80 | 1300 | offset_data += TARGET_PAGE_SIZE; |
d12f57ec QN |
1301 | |
1302 | /* | |
1303 | * dump memory to vmcore page by page. zero page will all be resided in the | |
1304 | * first page of page section | |
1305 | */ | |
1306 | while (get_next_page(&block_iter, &pfn_iter, &buf, s)) { | |
1307 | /* check zero page */ | |
2f859f80 | 1308 | if (is_zero_page(buf, TARGET_PAGE_SIZE)) { |
d12f57ec QN |
1309 | ret = write_cache(&page_desc, &pd_zero, sizeof(PageDescriptor), |
1310 | false); | |
1311 | if (ret < 0) { | |
1312 | dump_error(s, "dump: failed to write page desc.\n"); | |
1313 | goto out; | |
1314 | } | |
1315 | } else { | |
1316 | /* | |
1317 | * not zero page, then: | |
1318 | * 1. compress the page | |
1319 | * 2. write the compressed page into the cache of page_data | |
1320 | * 3. get page desc of the compressed page and write it into the | |
1321 | * cache of page_desc | |
1322 | * | |
1323 | * only one compression format will be used here, for | |
1324 | * s->flag_compress is set. But when compression fails to work, | |
1325 | * we fall back to save in plaintext. | |
1326 | */ | |
1327 | size_out = len_buf_out; | |
1328 | if ((s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) && | |
2f859f80 LE |
1329 | (compress2(buf_out, (uLongf *)&size_out, buf, |
1330 | TARGET_PAGE_SIZE, Z_BEST_SPEED) == Z_OK) && | |
1331 | (size_out < TARGET_PAGE_SIZE)) { | |
d12f57ec QN |
1332 | pd.flags = cpu_convert_to_target32(DUMP_DH_COMPRESSED_ZLIB, |
1333 | endian); | |
1334 | pd.size = cpu_convert_to_target32(size_out, endian); | |
1335 | ||
1336 | ret = write_cache(&page_data, buf_out, size_out, false); | |
1337 | if (ret < 0) { | |
1338 | dump_error(s, "dump: failed to write page data.\n"); | |
1339 | goto out; | |
1340 | } | |
1341 | #ifdef CONFIG_LZO | |
1342 | } else if ((s->flag_compress & DUMP_DH_COMPRESSED_LZO) && | |
2f859f80 | 1343 | (lzo1x_1_compress(buf, TARGET_PAGE_SIZE, buf_out, |
d12f57ec | 1344 | (lzo_uint *)&size_out, wrkmem) == LZO_E_OK) && |
2f859f80 | 1345 | (size_out < TARGET_PAGE_SIZE)) { |
d12f57ec QN |
1346 | pd.flags = cpu_convert_to_target32(DUMP_DH_COMPRESSED_LZO, |
1347 | endian); | |
1348 | pd.size = cpu_convert_to_target32(size_out, endian); | |
1349 | ||
1350 | ret = write_cache(&page_data, buf_out, size_out, false); | |
1351 | if (ret < 0) { | |
1352 | dump_error(s, "dump: failed to write page data.\n"); | |
1353 | goto out; | |
1354 | } | |
1355 | #endif | |
1356 | #ifdef CONFIG_SNAPPY | |
1357 | } else if ((s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) && | |
2f859f80 | 1358 | (snappy_compress((char *)buf, TARGET_PAGE_SIZE, |
d12f57ec | 1359 | (char *)buf_out, &size_out) == SNAPPY_OK) && |
2f859f80 | 1360 | (size_out < TARGET_PAGE_SIZE)) { |
d12f57ec QN |
1361 | pd.flags = cpu_convert_to_target32( |
1362 | DUMP_DH_COMPRESSED_SNAPPY, endian); | |
1363 | pd.size = cpu_convert_to_target32(size_out, endian); | |
1364 | ||
1365 | ret = write_cache(&page_data, buf_out, size_out, false); | |
1366 | if (ret < 0) { | |
1367 | dump_error(s, "dump: failed to write page data.\n"); | |
1368 | goto out; | |
1369 | } | |
1370 | #endif | |
1371 | } else { | |
1372 | /* | |
1373 | * fall back to save in plaintext, size_out should be | |
2f859f80 | 1374 | * assigned TARGET_PAGE_SIZE |
d12f57ec QN |
1375 | */ |
1376 | pd.flags = cpu_convert_to_target32(0, endian); | |
2f859f80 | 1377 | size_out = TARGET_PAGE_SIZE; |
d12f57ec QN |
1378 | pd.size = cpu_convert_to_target32(size_out, endian); |
1379 | ||
2f859f80 | 1380 | ret = write_cache(&page_data, buf, TARGET_PAGE_SIZE, false); |
d12f57ec QN |
1381 | if (ret < 0) { |
1382 | dump_error(s, "dump: failed to write page data.\n"); | |
1383 | goto out; | |
1384 | } | |
1385 | } | |
1386 | ||
1387 | /* get and write page desc here */ | |
1388 | pd.page_flags = cpu_convert_to_target64(0, endian); | |
1389 | pd.offset = cpu_convert_to_target64(offset_data, endian); | |
1390 | offset_data += size_out; | |
1391 | ||
1392 | ret = write_cache(&page_desc, &pd, sizeof(PageDescriptor), false); | |
1393 | if (ret < 0) { | |
1394 | dump_error(s, "dump: failed to write page desc.\n"); | |
1395 | goto out; | |
1396 | } | |
1397 | } | |
1398 | } | |
1399 | ||
1400 | ret = write_cache(&page_desc, NULL, 0, true); | |
1401 | if (ret < 0) { | |
1402 | dump_error(s, "dump: failed to sync cache for page_desc.\n"); | |
1403 | goto out; | |
1404 | } | |
1405 | ret = write_cache(&page_data, NULL, 0, true); | |
1406 | if (ret < 0) { | |
1407 | dump_error(s, "dump: failed to sync cache for page_data.\n"); | |
1408 | goto out; | |
1409 | } | |
1410 | ||
1411 | out: | |
1412 | free_data_cache(&page_desc); | |
1413 | free_data_cache(&page_data); | |
1414 | ||
1415 | #ifdef CONFIG_LZO | |
1416 | g_free(wrkmem); | |
1417 | #endif | |
1418 | ||
1419 | g_free(buf_out); | |
1420 | ||
1421 | return ret; | |
1422 | } | |
1423 | ||
b53ccc30 QN |
1424 | static int create_kdump_vmcore(DumpState *s) |
1425 | { | |
1426 | int ret; | |
1427 | ||
1428 | /* | |
1429 | * the kdump-compressed format is: | |
1430 | * File offset | |
1431 | * +------------------------------------------+ 0x0 | |
1432 | * | main header (struct disk_dump_header) | | |
1433 | * |------------------------------------------+ block 1 | |
1434 | * | sub header (struct kdump_sub_header) | | |
1435 | * |------------------------------------------+ block 2 | |
1436 | * | 1st-dump_bitmap | | |
1437 | * |------------------------------------------+ block 2 + X blocks | |
1438 | * | 2nd-dump_bitmap | (aligned by block) | |
1439 | * |------------------------------------------+ block 2 + 2 * X blocks | |
1440 | * | page desc for pfn 0 (struct page_desc) | (aligned by block) | |
1441 | * | page desc for pfn 1 (struct page_desc) | | |
1442 | * | : | | |
1443 | * |------------------------------------------| (not aligned by block) | |
1444 | * | page data (pfn 0) | | |
1445 | * | page data (pfn 1) | | |
1446 | * | : | | |
1447 | * +------------------------------------------+ | |
1448 | */ | |
1449 | ||
1450 | ret = write_start_flat_header(s->fd); | |
1451 | if (ret < 0) { | |
1452 | dump_error(s, "dump: failed to write start flat header.\n"); | |
1453 | return -1; | |
1454 | } | |
1455 | ||
1456 | ret = write_dump_header(s); | |
1457 | if (ret < 0) { | |
1458 | return -1; | |
1459 | } | |
1460 | ||
1461 | ret = write_dump_bitmap(s); | |
1462 | if (ret < 0) { | |
1463 | return -1; | |
1464 | } | |
1465 | ||
1466 | ret = write_dump_pages(s); | |
1467 | if (ret < 0) { | |
1468 | return -1; | |
1469 | } | |
1470 | ||
1471 | ret = write_end_flat_header(s->fd); | |
1472 | if (ret < 0) { | |
1473 | dump_error(s, "dump: failed to write end flat header.\n"); | |
1474 | return -1; | |
1475 | } | |
1476 | ||
1477 | dump_completed(s); | |
1478 | ||
1479 | return 0; | |
1480 | } | |
1481 | ||
783e9b48 WC |
1482 | static ram_addr_t get_start_block(DumpState *s) |
1483 | { | |
56c4bfb3 | 1484 | GuestPhysBlock *block; |
783e9b48 WC |
1485 | |
1486 | if (!s->has_filter) { | |
56c4bfb3 | 1487 | s->next_block = QTAILQ_FIRST(&s->guest_phys_blocks.head); |
783e9b48 WC |
1488 | return 0; |
1489 | } | |
1490 | ||
56c4bfb3 LE |
1491 | QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) { |
1492 | if (block->target_start >= s->begin + s->length || | |
1493 | block->target_end <= s->begin) { | |
783e9b48 WC |
1494 | /* This block is out of the range */ |
1495 | continue; | |
1496 | } | |
1497 | ||
56c4bfb3 LE |
1498 | s->next_block = block; |
1499 | if (s->begin > block->target_start) { | |
1500 | s->start = s->begin - block->target_start; | |
783e9b48 WC |
1501 | } else { |
1502 | s->start = 0; | |
1503 | } | |
1504 | return s->start; | |
1505 | } | |
1506 | ||
1507 | return -1; | |
1508 | } | |
1509 | ||
7aad248d QN |
1510 | static void get_max_mapnr(DumpState *s) |
1511 | { | |
1512 | GuestPhysBlock *last_block; | |
1513 | ||
1514 | last_block = QTAILQ_LAST(&s->guest_phys_blocks.head, GuestPhysBlockHead); | |
22227f12 | 1515 | s->max_mapnr = paddr_to_pfn(last_block->target_end); |
7aad248d QN |
1516 | } |
1517 | ||
b53ccc30 QN |
1518 | static int dump_init(DumpState *s, int fd, bool has_format, |
1519 | DumpGuestMemoryFormat format, bool paging, bool has_filter, | |
783e9b48 WC |
1520 | int64_t begin, int64_t length, Error **errp) |
1521 | { | |
182735ef | 1522 | CPUState *cpu; |
783e9b48 | 1523 | int nr_cpus; |
11ed09cf | 1524 | Error *err = NULL; |
783e9b48 WC |
1525 | int ret; |
1526 | ||
b53ccc30 QN |
1527 | /* kdump-compressed is conflict with paging and filter */ |
1528 | if (has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) { | |
1529 | assert(!paging && !has_filter); | |
1530 | } | |
1531 | ||
783e9b48 WC |
1532 | if (runstate_is_running()) { |
1533 | vm_stop(RUN_STATE_SAVE_VM); | |
1534 | s->resume = true; | |
1535 | } else { | |
1536 | s->resume = false; | |
1537 | } | |
1538 | ||
5ee163e8 LE |
1539 | /* If we use KVM, we should synchronize the registers before we get dump |
1540 | * info or physmap info. | |
1541 | */ | |
1542 | cpu_synchronize_all_states(); | |
1543 | nr_cpus = 0; | |
bdc44640 | 1544 | CPU_FOREACH(cpu) { |
5ee163e8 LE |
1545 | nr_cpus++; |
1546 | } | |
1547 | ||
783e9b48 WC |
1548 | s->fd = fd; |
1549 | s->has_filter = has_filter; | |
1550 | s->begin = begin; | |
1551 | s->length = length; | |
5ee163e8 LE |
1552 | |
1553 | guest_phys_blocks_init(&s->guest_phys_blocks); | |
c5d7f60f | 1554 | guest_phys_blocks_append(&s->guest_phys_blocks); |
5ee163e8 | 1555 | |
783e9b48 WC |
1556 | s->start = get_start_block(s); |
1557 | if (s->start == -1) { | |
1558 | error_set(errp, QERR_INVALID_PARAMETER, "begin"); | |
1559 | goto cleanup; | |
1560 | } | |
1561 | ||
5ee163e8 | 1562 | /* get dump info: endian, class and architecture. |
783e9b48 WC |
1563 | * If the target architecture is not supported, cpu_get_dump_info() will |
1564 | * return -1. | |
783e9b48 | 1565 | */ |
56c4bfb3 | 1566 | ret = cpu_get_dump_info(&s->dump_info, &s->guest_phys_blocks); |
783e9b48 WC |
1567 | if (ret < 0) { |
1568 | error_set(errp, QERR_UNSUPPORTED); | |
1569 | goto cleanup; | |
1570 | } | |
1571 | ||
4720bd05 PB |
1572 | s->note_size = cpu_get_note_size(s->dump_info.d_class, |
1573 | s->dump_info.d_machine, nr_cpus); | |
bb6b6843 | 1574 | if (s->note_size < 0) { |
4720bd05 PB |
1575 | error_set(errp, QERR_UNSUPPORTED); |
1576 | goto cleanup; | |
1577 | } | |
1578 | ||
783e9b48 WC |
1579 | /* get memory mapping */ |
1580 | memory_mapping_list_init(&s->list); | |
1581 | if (paging) { | |
56c4bfb3 | 1582 | qemu_get_guest_memory_mapping(&s->list, &s->guest_phys_blocks, &err); |
11ed09cf AF |
1583 | if (err != NULL) { |
1584 | error_propagate(errp, err); | |
1585 | goto cleanup; | |
1586 | } | |
783e9b48 | 1587 | } else { |
56c4bfb3 | 1588 | qemu_get_guest_simple_memory_mapping(&s->list, &s->guest_phys_blocks); |
783e9b48 WC |
1589 | } |
1590 | ||
7aad248d | 1591 | s->nr_cpus = nr_cpus; |
7aad248d QN |
1592 | |
1593 | get_max_mapnr(s); | |
1594 | ||
1595 | uint64_t tmp; | |
2f859f80 LE |
1596 | tmp = DIV_ROUND_UP(DIV_ROUND_UP(s->max_mapnr, CHAR_BIT), TARGET_PAGE_SIZE); |
1597 | s->len_dump_bitmap = tmp * TARGET_PAGE_SIZE; | |
7aad248d | 1598 | |
b53ccc30 QN |
1599 | /* init for kdump-compressed format */ |
1600 | if (has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) { | |
1601 | switch (format) { | |
1602 | case DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB: | |
1603 | s->flag_compress = DUMP_DH_COMPRESSED_ZLIB; | |
1604 | break; | |
1605 | ||
1606 | case DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO: | |
c998acb0 LE |
1607 | #ifdef CONFIG_LZO |
1608 | if (lzo_init() != LZO_E_OK) { | |
1609 | error_setg(errp, "failed to initialize the LZO library"); | |
1610 | goto cleanup; | |
1611 | } | |
1612 | #endif | |
b53ccc30 QN |
1613 | s->flag_compress = DUMP_DH_COMPRESSED_LZO; |
1614 | break; | |
1615 | ||
1616 | case DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY: | |
1617 | s->flag_compress = DUMP_DH_COMPRESSED_SNAPPY; | |
1618 | break; | |
1619 | ||
1620 | default: | |
1621 | s->flag_compress = 0; | |
1622 | } | |
1623 | ||
1624 | return 0; | |
1625 | } | |
1626 | ||
783e9b48 WC |
1627 | if (s->has_filter) { |
1628 | memory_mapping_filter(&s->list, s->begin, s->length); | |
1629 | } | |
1630 | ||
1631 | /* | |
1632 | * calculate phdr_num | |
1633 | * | |
1634 | * the type of ehdr->e_phnum is uint16_t, so we should avoid overflow | |
1635 | */ | |
1636 | s->phdr_num = 1; /* PT_NOTE */ | |
1637 | if (s->list.num < UINT16_MAX - 2) { | |
1638 | s->phdr_num += s->list.num; | |
1639 | s->have_section = false; | |
1640 | } else { | |
1641 | s->have_section = true; | |
1642 | s->phdr_num = PN_XNUM; | |
1643 | s->sh_info = 1; /* PT_NOTE */ | |
1644 | ||
1645 | /* the type of shdr->sh_info is uint32_t, so we should avoid overflow */ | |
1646 | if (s->list.num <= UINT32_MAX - 1) { | |
1647 | s->sh_info += s->list.num; | |
1648 | } else { | |
1649 | s->sh_info = UINT32_MAX; | |
1650 | } | |
1651 | } | |
1652 | ||
783e9b48 WC |
1653 | if (s->dump_info.d_class == ELFCLASS64) { |
1654 | if (s->have_section) { | |
1655 | s->memory_offset = sizeof(Elf64_Ehdr) + | |
1656 | sizeof(Elf64_Phdr) * s->sh_info + | |
1657 | sizeof(Elf64_Shdr) + s->note_size; | |
1658 | } else { | |
1659 | s->memory_offset = sizeof(Elf64_Ehdr) + | |
1660 | sizeof(Elf64_Phdr) * s->phdr_num + s->note_size; | |
1661 | } | |
1662 | } else { | |
1663 | if (s->have_section) { | |
1664 | s->memory_offset = sizeof(Elf32_Ehdr) + | |
1665 | sizeof(Elf32_Phdr) * s->sh_info + | |
1666 | sizeof(Elf32_Shdr) + s->note_size; | |
1667 | } else { | |
1668 | s->memory_offset = sizeof(Elf32_Ehdr) + | |
1669 | sizeof(Elf32_Phdr) * s->phdr_num + s->note_size; | |
1670 | } | |
1671 | } | |
1672 | ||
1673 | return 0; | |
1674 | ||
1675 | cleanup: | |
5ee163e8 LE |
1676 | guest_phys_blocks_free(&s->guest_phys_blocks); |
1677 | ||
783e9b48 WC |
1678 | if (s->resume) { |
1679 | vm_start(); | |
1680 | } | |
1681 | ||
1682 | return -1; | |
1683 | } | |
1684 | ||
1685 | void qmp_dump_guest_memory(bool paging, const char *file, bool has_begin, | |
b53ccc30 QN |
1686 | int64_t begin, bool has_length, |
1687 | int64_t length, bool has_format, | |
1688 | DumpGuestMemoryFormat format, Error **errp) | |
783e9b48 WC |
1689 | { |
1690 | const char *p; | |
1691 | int fd = -1; | |
1692 | DumpState *s; | |
1693 | int ret; | |
1694 | ||
b53ccc30 QN |
1695 | /* |
1696 | * kdump-compressed format need the whole memory dumped, so paging or | |
1697 | * filter is not supported here. | |
1698 | */ | |
1699 | if ((has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) && | |
1700 | (paging || has_begin || has_length)) { | |
1701 | error_setg(errp, "kdump-compressed format doesn't support paging or " | |
1702 | "filter"); | |
1703 | return; | |
1704 | } | |
783e9b48 WC |
1705 | if (has_begin && !has_length) { |
1706 | error_set(errp, QERR_MISSING_PARAMETER, "length"); | |
1707 | return; | |
1708 | } | |
1709 | if (!has_begin && has_length) { | |
1710 | error_set(errp, QERR_MISSING_PARAMETER, "begin"); | |
1711 | return; | |
1712 | } | |
1713 | ||
b53ccc30 QN |
1714 | /* check whether lzo/snappy is supported */ |
1715 | #ifndef CONFIG_LZO | |
1716 | if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO) { | |
1717 | error_setg(errp, "kdump-lzo is not available now"); | |
1718 | return; | |
1719 | } | |
1720 | #endif | |
1721 | ||
1722 | #ifndef CONFIG_SNAPPY | |
1723 | if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY) { | |
1724 | error_setg(errp, "kdump-snappy is not available now"); | |
1725 | return; | |
1726 | } | |
1727 | #endif | |
1728 | ||
783e9b48 WC |
1729 | #if !defined(WIN32) |
1730 | if (strstart(file, "fd:", &p)) { | |
a9940fc4 | 1731 | fd = monitor_get_fd(cur_mon, p, errp); |
783e9b48 | 1732 | if (fd == -1) { |
783e9b48 WC |
1733 | return; |
1734 | } | |
1735 | } | |
1736 | #endif | |
1737 | ||
1738 | if (strstart(file, "file:", &p)) { | |
1739 | fd = qemu_open(p, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, S_IRUSR); | |
1740 | if (fd < 0) { | |
7581766b | 1741 | error_setg_file_open(errp, errno, p); |
783e9b48 WC |
1742 | return; |
1743 | } | |
1744 | } | |
1745 | ||
1746 | if (fd == -1) { | |
1747 | error_set(errp, QERR_INVALID_PARAMETER, "protocol"); | |
1748 | return; | |
1749 | } | |
1750 | ||
5ee163e8 | 1751 | s = g_malloc0(sizeof(DumpState)); |
783e9b48 | 1752 | |
b53ccc30 QN |
1753 | ret = dump_init(s, fd, has_format, format, paging, has_begin, |
1754 | begin, length, errp); | |
783e9b48 WC |
1755 | if (ret < 0) { |
1756 | g_free(s); | |
1757 | return; | |
1758 | } | |
1759 | ||
b53ccc30 | 1760 | if (has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) { |
66ef8bd9 | 1761 | if (create_kdump_vmcore(s) < 0) { |
b53ccc30 QN |
1762 | error_set(errp, QERR_IO_ERROR); |
1763 | } | |
1764 | } else { | |
66ef8bd9 | 1765 | if (create_vmcore(s) < 0) { |
b53ccc30 QN |
1766 | error_set(errp, QERR_IO_ERROR); |
1767 | } | |
783e9b48 WC |
1768 | } |
1769 | ||
1770 | g_free(s); | |
1771 | } | |
7d6dc7f3 QN |
1772 | |
1773 | DumpGuestMemoryCapability *qmp_query_dump_guest_memory_capability(Error **errp) | |
1774 | { | |
1775 | DumpGuestMemoryFormatList *item; | |
1776 | DumpGuestMemoryCapability *cap = | |
1777 | g_malloc0(sizeof(DumpGuestMemoryCapability)); | |
1778 | ||
1779 | /* elf is always available */ | |
1780 | item = g_malloc0(sizeof(DumpGuestMemoryFormatList)); | |
1781 | cap->formats = item; | |
1782 | item->value = DUMP_GUEST_MEMORY_FORMAT_ELF; | |
1783 | ||
1784 | /* kdump-zlib is always available */ | |
1785 | item->next = g_malloc0(sizeof(DumpGuestMemoryFormatList)); | |
1786 | item = item->next; | |
1787 | item->value = DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB; | |
1788 | ||
1789 | /* add new item if kdump-lzo is available */ | |
1790 | #ifdef CONFIG_LZO | |
1791 | item->next = g_malloc0(sizeof(DumpGuestMemoryFormatList)); | |
1792 | item = item->next; | |
1793 | item->value = DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO; | |
1794 | #endif | |
1795 | ||
1796 | /* add new item if kdump-snappy is available */ | |
1797 | #ifdef CONFIG_SNAPPY | |
1798 | item->next = g_malloc0(sizeof(DumpGuestMemoryFormatList)); | |
1799 | item = item->next; | |
1800 | item->value = DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY; | |
1801 | #endif | |
1802 | ||
1803 | return cap; | |
1804 | } |