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