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