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