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