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