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