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