]>
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" |
783e9b48 | 15 | #include "qemu-common.h" |
783e9b48 | 16 | #include "elf.h" |
783e9b48 | 17 | #include "cpu.h" |
022c62cb PB |
18 | #include "exec/cpu-all.h" |
19 | #include "exec/hwaddr.h" | |
83c9089e | 20 | #include "monitor/monitor.h" |
9c17d615 PB |
21 | #include "sysemu/kvm.h" |
22 | #include "sysemu/dump.h" | |
23 | #include "sysemu/sysemu.h" | |
24 | #include "sysemu/memory_mapping.h" | |
1b3509ca | 25 | #include "sysemu/cpus.h" |
cc7a8ea7 | 26 | #include "qapi/qmp/qerror.h" |
783e9b48 | 27 | #include "qmp-commands.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 WC |
78 | if (s->resume) { |
79 | vm_start(); | |
80 | } | |
81 | ||
2928207a | 82 | return 0; |
783e9b48 WC |
83 | } |
84 | ||
37917da2 | 85 | static void dump_error(DumpState *s, const char *reason, Error **errp) |
783e9b48 WC |
86 | { |
87 | dump_cleanup(s); | |
37917da2 | 88 | error_setg(errp, "%s", reason); |
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) { | |
37917da2 | 131 | dump_error(s, "dump: failed to write elf header", errp); |
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) { | |
37917da2 | 162 | dump_error(s, "dump: failed to write elf header", errp); |
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) { | |
37917da2 | 185 | dump_error(s, "dump: failed to write program header table", errp); |
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) { | |
37917da2 | 208 | dump_error(s, "dump: failed to write program header table", errp); |
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) { | |
37917da2 | 228 | dump_error(s, "dump: failed to write program header table", errp); |
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) { |
37917da2 | 248 | dump_error(s, "dump: failed to write elf notes", errp); |
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) { |
37917da2 | 256 | dump_error(s, "dump: failed to write CPU status", errp); |
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) { | |
37917da2 | 278 | dump_error(s, "dump: failed to write program header table", errp); |
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) { |
37917da2 | 293 | dump_error(s, "dump: failed to write elf notes", errp); |
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) { |
37917da2 | 301 | dump_error(s, "dump: failed to write CPU status", errp); |
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) { | |
37917da2 | 329 | dump_error(s, "dump: failed to write section header table", errp); |
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) { | |
37917da2 | 339 | dump_error(s, "dump: failed to save memory", errp); |
783e9b48 | 340 | } |
783e9b48 WC |
341 | } |
342 | ||
4c7e251a HZ |
343 | /* write the memory to vmcore. 1 page per I/O. */ |
344 | static void write_memory(DumpState *s, GuestPhysBlock *block, ram_addr_t start, | |
345 | int64_t size, Error **errp) | |
783e9b48 WC |
346 | { |
347 | int64_t i; | |
4c7e251a | 348 | Error *local_err = NULL; |
783e9b48 | 349 | |
8161befd AJ |
350 | for (i = 0; i < size / s->dump_info.page_size; i++) { |
351 | write_data(s, block->host_addr + start + i * s->dump_info.page_size, | |
352 | s->dump_info.page_size, &local_err); | |
4c7e251a HZ |
353 | if (local_err) { |
354 | error_propagate(errp, local_err); | |
355 | return; | |
783e9b48 WC |
356 | } |
357 | } | |
358 | ||
8161befd AJ |
359 | if ((size % s->dump_info.page_size) != 0) { |
360 | write_data(s, block->host_addr + start + i * s->dump_info.page_size, | |
361 | size % s->dump_info.page_size, &local_err); | |
4c7e251a HZ |
362 | if (local_err) { |
363 | error_propagate(errp, local_err); | |
364 | return; | |
783e9b48 WC |
365 | } |
366 | } | |
783e9b48 WC |
367 | } |
368 | ||
2cac2607 LE |
369 | /* get the memory's offset and size in the vmcore */ |
370 | static void get_offset_range(hwaddr phys_addr, | |
371 | ram_addr_t mapping_length, | |
372 | DumpState *s, | |
373 | hwaddr *p_offset, | |
374 | hwaddr *p_filesz) | |
783e9b48 | 375 | { |
56c4bfb3 | 376 | GuestPhysBlock *block; |
a8170e5e | 377 | hwaddr offset = s->memory_offset; |
783e9b48 WC |
378 | int64_t size_in_block, start; |
379 | ||
2cac2607 LE |
380 | /* When the memory is not stored into vmcore, offset will be -1 */ |
381 | *p_offset = -1; | |
382 | *p_filesz = 0; | |
383 | ||
783e9b48 WC |
384 | if (s->has_filter) { |
385 | if (phys_addr < s->begin || phys_addr >= s->begin + s->length) { | |
2cac2607 | 386 | return; |
783e9b48 WC |
387 | } |
388 | } | |
389 | ||
56c4bfb3 | 390 | QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) { |
783e9b48 | 391 | if (s->has_filter) { |
56c4bfb3 LE |
392 | if (block->target_start >= s->begin + s->length || |
393 | block->target_end <= s->begin) { | |
783e9b48 WC |
394 | /* This block is out of the range */ |
395 | continue; | |
396 | } | |
397 | ||
56c4bfb3 LE |
398 | if (s->begin <= block->target_start) { |
399 | start = block->target_start; | |
783e9b48 WC |
400 | } else { |
401 | start = s->begin; | |
402 | } | |
403 | ||
56c4bfb3 LE |
404 | size_in_block = block->target_end - start; |
405 | if (s->begin + s->length < block->target_end) { | |
406 | size_in_block -= block->target_end - (s->begin + s->length); | |
783e9b48 WC |
407 | } |
408 | } else { | |
56c4bfb3 LE |
409 | start = block->target_start; |
410 | size_in_block = block->target_end - block->target_start; | |
783e9b48 WC |
411 | } |
412 | ||
413 | if (phys_addr >= start && phys_addr < start + size_in_block) { | |
2cac2607 LE |
414 | *p_offset = phys_addr - start + offset; |
415 | ||
416 | /* The offset range mapped from the vmcore file must not spill over | |
56c4bfb3 | 417 | * the GuestPhysBlock, clamp it. The rest of the mapping will be |
2cac2607 LE |
418 | * zero-filled in memory at load time; see |
419 | * <http://refspecs.linuxbase.org/elf/gabi4+/ch5.pheader.html>. | |
420 | */ | |
421 | *p_filesz = phys_addr + mapping_length <= start + size_in_block ? | |
422 | mapping_length : | |
423 | size_in_block - (phys_addr - start); | |
424 | return; | |
783e9b48 WC |
425 | } |
426 | ||
427 | offset += size_in_block; | |
428 | } | |
783e9b48 WC |
429 | } |
430 | ||
4c7e251a | 431 | static void write_elf_loads(DumpState *s, Error **errp) |
783e9b48 | 432 | { |
2cac2607 | 433 | hwaddr offset, filesz; |
783e9b48 WC |
434 | MemoryMapping *memory_mapping; |
435 | uint32_t phdr_index = 1; | |
783e9b48 | 436 | uint32_t max_index; |
4c7e251a | 437 | Error *local_err = NULL; |
783e9b48 WC |
438 | |
439 | if (s->have_section) { | |
440 | max_index = s->sh_info; | |
441 | } else { | |
442 | max_index = s->phdr_num; | |
443 | } | |
444 | ||
445 | QTAILQ_FOREACH(memory_mapping, &s->list.head, next) { | |
2cac2607 LE |
446 | get_offset_range(memory_mapping->phys_addr, |
447 | memory_mapping->length, | |
448 | s, &offset, &filesz); | |
783e9b48 | 449 | if (s->dump_info.d_class == ELFCLASS64) { |
4c7e251a HZ |
450 | write_elf64_load(s, memory_mapping, phdr_index++, offset, |
451 | filesz, &local_err); | |
783e9b48 | 452 | } else { |
4c7e251a HZ |
453 | write_elf32_load(s, memory_mapping, phdr_index++, offset, |
454 | filesz, &local_err); | |
783e9b48 WC |
455 | } |
456 | ||
4c7e251a HZ |
457 | if (local_err) { |
458 | error_propagate(errp, local_err); | |
459 | return; | |
783e9b48 WC |
460 | } |
461 | ||
462 | if (phdr_index >= max_index) { | |
463 | break; | |
464 | } | |
465 | } | |
783e9b48 WC |
466 | } |
467 | ||
468 | /* write elf header, PT_NOTE and elf note to vmcore. */ | |
4c7e251a | 469 | static void dump_begin(DumpState *s, Error **errp) |
783e9b48 | 470 | { |
4c7e251a | 471 | Error *local_err = NULL; |
783e9b48 WC |
472 | |
473 | /* | |
474 | * the vmcore's format is: | |
475 | * -------------- | |
476 | * | elf header | | |
477 | * -------------- | |
478 | * | PT_NOTE | | |
479 | * -------------- | |
480 | * | PT_LOAD | | |
481 | * -------------- | |
482 | * | ...... | | |
483 | * -------------- | |
484 | * | PT_LOAD | | |
485 | * -------------- | |
486 | * | sec_hdr | | |
487 | * -------------- | |
488 | * | elf note | | |
489 | * -------------- | |
490 | * | memory | | |
491 | * -------------- | |
492 | * | |
493 | * we only know where the memory is saved after we write elf note into | |
494 | * vmcore. | |
495 | */ | |
496 | ||
497 | /* write elf header to vmcore */ | |
498 | if (s->dump_info.d_class == ELFCLASS64) { | |
4c7e251a | 499 | write_elf64_header(s, &local_err); |
783e9b48 | 500 | } else { |
4c7e251a | 501 | write_elf32_header(s, &local_err); |
783e9b48 | 502 | } |
4c7e251a HZ |
503 | if (local_err) { |
504 | error_propagate(errp, local_err); | |
505 | return; | |
783e9b48 WC |
506 | } |
507 | ||
508 | if (s->dump_info.d_class == ELFCLASS64) { | |
509 | /* write PT_NOTE to vmcore */ | |
4c7e251a HZ |
510 | write_elf64_note(s, &local_err); |
511 | if (local_err) { | |
512 | error_propagate(errp, local_err); | |
513 | return; | |
783e9b48 WC |
514 | } |
515 | ||
516 | /* write all PT_LOAD to vmcore */ | |
4c7e251a HZ |
517 | write_elf_loads(s, &local_err); |
518 | if (local_err) { | |
519 | error_propagate(errp, local_err); | |
520 | return; | |
783e9b48 WC |
521 | } |
522 | ||
523 | /* write section to vmcore */ | |
524 | if (s->have_section) { | |
4c7e251a HZ |
525 | write_elf_section(s, 1, &local_err); |
526 | if (local_err) { | |
527 | error_propagate(errp, local_err); | |
528 | return; | |
783e9b48 WC |
529 | } |
530 | } | |
531 | ||
532 | /* write notes to vmcore */ | |
4c7e251a HZ |
533 | write_elf64_notes(fd_write_vmcore, s, &local_err); |
534 | if (local_err) { | |
535 | error_propagate(errp, local_err); | |
536 | return; | |
783e9b48 | 537 | } |
783e9b48 WC |
538 | } else { |
539 | /* write PT_NOTE to vmcore */ | |
4c7e251a HZ |
540 | write_elf32_note(s, &local_err); |
541 | if (local_err) { | |
542 | error_propagate(errp, local_err); | |
543 | return; | |
783e9b48 WC |
544 | } |
545 | ||
546 | /* write all PT_LOAD to vmcore */ | |
4c7e251a HZ |
547 | write_elf_loads(s, &local_err); |
548 | if (local_err) { | |
549 | error_propagate(errp, local_err); | |
550 | return; | |
783e9b48 WC |
551 | } |
552 | ||
553 | /* write section to vmcore */ | |
554 | if (s->have_section) { | |
4c7e251a HZ |
555 | write_elf_section(s, 0, &local_err); |
556 | if (local_err) { | |
557 | error_propagate(errp, local_err); | |
558 | return; | |
783e9b48 WC |
559 | } |
560 | } | |
561 | ||
562 | /* write notes to vmcore */ | |
4c7e251a HZ |
563 | write_elf32_notes(fd_write_vmcore, s, &local_err); |
564 | if (local_err) { | |
565 | error_propagate(errp, local_err); | |
566 | return; | |
783e9b48 WC |
567 | } |
568 | } | |
783e9b48 WC |
569 | } |
570 | ||
4c7e251a | 571 | static void dump_completed(DumpState *s) |
783e9b48 WC |
572 | { |
573 | dump_cleanup(s); | |
783e9b48 WC |
574 | } |
575 | ||
56c4bfb3 | 576 | static int get_next_block(DumpState *s, GuestPhysBlock *block) |
783e9b48 WC |
577 | { |
578 | while (1) { | |
a3161038 | 579 | block = QTAILQ_NEXT(block, next); |
783e9b48 WC |
580 | if (!block) { |
581 | /* no more block */ | |
582 | return 1; | |
583 | } | |
584 | ||
585 | s->start = 0; | |
56c4bfb3 | 586 | s->next_block = block; |
783e9b48 | 587 | if (s->has_filter) { |
56c4bfb3 LE |
588 | if (block->target_start >= s->begin + s->length || |
589 | block->target_end <= s->begin) { | |
783e9b48 WC |
590 | /* This block is out of the range */ |
591 | continue; | |
592 | } | |
593 | ||
56c4bfb3 LE |
594 | if (s->begin > block->target_start) { |
595 | s->start = s->begin - block->target_start; | |
783e9b48 WC |
596 | } |
597 | } | |
598 | ||
599 | return 0; | |
600 | } | |
601 | } | |
602 | ||
603 | /* write all memory to vmcore */ | |
4c7e251a | 604 | static void dump_iterate(DumpState *s, Error **errp) |
783e9b48 | 605 | { |
56c4bfb3 | 606 | GuestPhysBlock *block; |
783e9b48 | 607 | int64_t size; |
4c7e251a | 608 | Error *local_err = NULL; |
783e9b48 | 609 | |
08a655be | 610 | do { |
56c4bfb3 | 611 | block = s->next_block; |
783e9b48 | 612 | |
56c4bfb3 | 613 | size = block->target_end - block->target_start; |
783e9b48 WC |
614 | if (s->has_filter) { |
615 | size -= s->start; | |
56c4bfb3 LE |
616 | if (s->begin + s->length < block->target_end) { |
617 | size -= block->target_end - (s->begin + s->length); | |
783e9b48 WC |
618 | } |
619 | } | |
4c7e251a HZ |
620 | write_memory(s, block, s->start, size, &local_err); |
621 | if (local_err) { | |
622 | error_propagate(errp, local_err); | |
623 | return; | |
783e9b48 WC |
624 | } |
625 | ||
08a655be GA |
626 | } while (!get_next_block(s, block)); |
627 | ||
628 | dump_completed(s); | |
783e9b48 WC |
629 | } |
630 | ||
4c7e251a | 631 | static void create_vmcore(DumpState *s, Error **errp) |
783e9b48 | 632 | { |
4c7e251a | 633 | Error *local_err = NULL; |
783e9b48 | 634 | |
4c7e251a HZ |
635 | dump_begin(s, &local_err); |
636 | if (local_err) { | |
637 | error_propagate(errp, local_err); | |
638 | return; | |
783e9b48 WC |
639 | } |
640 | ||
4c7e251a | 641 | dump_iterate(s, errp); |
783e9b48 WC |
642 | } |
643 | ||
fda05387 QN |
644 | static int write_start_flat_header(int fd) |
645 | { | |
92ba1401 | 646 | MakedumpfileHeader *mh; |
fda05387 QN |
647 | int ret = 0; |
648 | ||
92ba1401 LE |
649 | QEMU_BUILD_BUG_ON(sizeof *mh > MAX_SIZE_MDF_HEADER); |
650 | mh = g_malloc0(MAX_SIZE_MDF_HEADER); | |
fda05387 | 651 | |
92ba1401 LE |
652 | memcpy(mh->signature, MAKEDUMPFILE_SIGNATURE, |
653 | MIN(sizeof mh->signature, sizeof MAKEDUMPFILE_SIGNATURE)); | |
fda05387 | 654 | |
92ba1401 LE |
655 | mh->type = cpu_to_be64(TYPE_FLAT_HEADER); |
656 | mh->version = cpu_to_be64(VERSION_FLAT_HEADER); | |
fda05387 QN |
657 | |
658 | size_t written_size; | |
92ba1401 | 659 | written_size = qemu_write_full(fd, mh, MAX_SIZE_MDF_HEADER); |
fda05387 QN |
660 | if (written_size != MAX_SIZE_MDF_HEADER) { |
661 | ret = -1; | |
662 | } | |
663 | ||
92ba1401 | 664 | g_free(mh); |
fda05387 QN |
665 | return ret; |
666 | } | |
667 | ||
668 | static int write_end_flat_header(int fd) | |
669 | { | |
670 | MakedumpfileDataHeader mdh; | |
671 | ||
672 | mdh.offset = END_FLAG_FLAT_HEADER; | |
673 | mdh.buf_size = END_FLAG_FLAT_HEADER; | |
674 | ||
675 | size_t written_size; | |
676 | written_size = qemu_write_full(fd, &mdh, sizeof(mdh)); | |
677 | if (written_size != sizeof(mdh)) { | |
678 | return -1; | |
679 | } | |
680 | ||
681 | return 0; | |
682 | } | |
683 | ||
5d31babe QN |
684 | static int write_buffer(int fd, off_t offset, const void *buf, size_t size) |
685 | { | |
686 | size_t written_size; | |
687 | MakedumpfileDataHeader mdh; | |
688 | ||
689 | mdh.offset = cpu_to_be64(offset); | |
690 | mdh.buf_size = cpu_to_be64(size); | |
691 | ||
692 | written_size = qemu_write_full(fd, &mdh, sizeof(mdh)); | |
693 | if (written_size != sizeof(mdh)) { | |
694 | return -1; | |
695 | } | |
696 | ||
697 | written_size = qemu_write_full(fd, buf, size); | |
698 | if (written_size != size) { | |
699 | return -1; | |
700 | } | |
701 | ||
702 | return 0; | |
703 | } | |
704 | ||
4835ef77 QN |
705 | static int buf_write_note(const void *buf, size_t size, void *opaque) |
706 | { | |
707 | DumpState *s = opaque; | |
708 | ||
709 | /* note_buf is not enough */ | |
710 | if (s->note_buf_offset + size > s->note_size) { | |
711 | return -1; | |
712 | } | |
713 | ||
714 | memcpy(s->note_buf + s->note_buf_offset, buf, size); | |
715 | ||
716 | s->note_buf_offset += size; | |
717 | ||
718 | return 0; | |
719 | } | |
720 | ||
298f1168 | 721 | /* write common header, sub header and elf note to vmcore */ |
4c7e251a | 722 | static void create_header32(DumpState *s, Error **errp) |
298f1168 | 723 | { |
298f1168 QN |
724 | DiskDumpHeader32 *dh = NULL; |
725 | KdumpSubHeader32 *kh = NULL; | |
726 | size_t size; | |
298f1168 QN |
727 | uint32_t block_size; |
728 | uint32_t sub_hdr_size; | |
729 | uint32_t bitmap_blocks; | |
730 | uint32_t status = 0; | |
731 | uint64_t offset_note; | |
4c7e251a | 732 | Error *local_err = NULL; |
298f1168 QN |
733 | |
734 | /* write common header, the version of kdump-compressed format is 6th */ | |
735 | size = sizeof(DiskDumpHeader32); | |
736 | dh = g_malloc0(size); | |
737 | ||
738 | strncpy(dh->signature, KDUMP_SIGNATURE, strlen(KDUMP_SIGNATURE)); | |
acb0ef58 | 739 | dh->header_version = cpu_to_dump32(s, 6); |
8161befd | 740 | block_size = s->dump_info.page_size; |
acb0ef58 | 741 | dh->block_size = cpu_to_dump32(s, block_size); |
298f1168 QN |
742 | sub_hdr_size = sizeof(struct KdumpSubHeader32) + s->note_size; |
743 | sub_hdr_size = DIV_ROUND_UP(sub_hdr_size, block_size); | |
acb0ef58 | 744 | dh->sub_hdr_size = cpu_to_dump32(s, sub_hdr_size); |
298f1168 | 745 | /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */ |
acb0ef58 BR |
746 | dh->max_mapnr = cpu_to_dump32(s, MIN(s->max_mapnr, UINT_MAX)); |
747 | dh->nr_cpus = cpu_to_dump32(s, s->nr_cpus); | |
298f1168 | 748 | bitmap_blocks = DIV_ROUND_UP(s->len_dump_bitmap, block_size) * 2; |
acb0ef58 | 749 | dh->bitmap_blocks = cpu_to_dump32(s, bitmap_blocks); |
4ab23a91 | 750 | strncpy(dh->utsname.machine, ELF_MACHINE_UNAME, sizeof(dh->utsname.machine)); |
298f1168 QN |
751 | |
752 | if (s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) { | |
753 | status |= DUMP_DH_COMPRESSED_ZLIB; | |
754 | } | |
755 | #ifdef CONFIG_LZO | |
756 | if (s->flag_compress & DUMP_DH_COMPRESSED_LZO) { | |
757 | status |= DUMP_DH_COMPRESSED_LZO; | |
758 | } | |
759 | #endif | |
760 | #ifdef CONFIG_SNAPPY | |
761 | if (s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) { | |
762 | status |= DUMP_DH_COMPRESSED_SNAPPY; | |
763 | } | |
764 | #endif | |
acb0ef58 | 765 | dh->status = cpu_to_dump32(s, status); |
298f1168 QN |
766 | |
767 | if (write_buffer(s->fd, 0, dh, size) < 0) { | |
37917da2 | 768 | dump_error(s, "dump: failed to write disk dump header", errp); |
298f1168 QN |
769 | goto out; |
770 | } | |
771 | ||
772 | /* write sub header */ | |
773 | size = sizeof(KdumpSubHeader32); | |
774 | kh = g_malloc0(size); | |
775 | ||
776 | /* 64bit max_mapnr_64 */ | |
acb0ef58 | 777 | kh->max_mapnr_64 = cpu_to_dump64(s, s->max_mapnr); |
b6e05aa4 | 778 | kh->phys_base = cpu_to_dump32(s, s->dump_info.phys_base); |
acb0ef58 | 779 | kh->dump_level = cpu_to_dump32(s, DUMP_LEVEL); |
298f1168 QN |
780 | |
781 | offset_note = DISKDUMP_HEADER_BLOCKS * block_size + size; | |
acb0ef58 BR |
782 | kh->offset_note = cpu_to_dump64(s, offset_note); |
783 | kh->note_size = cpu_to_dump32(s, s->note_size); | |
298f1168 QN |
784 | |
785 | if (write_buffer(s->fd, DISKDUMP_HEADER_BLOCKS * | |
786 | block_size, kh, size) < 0) { | |
37917da2 | 787 | dump_error(s, "dump: failed to write kdump sub header", errp); |
298f1168 QN |
788 | goto out; |
789 | } | |
790 | ||
791 | /* write note */ | |
792 | s->note_buf = g_malloc0(s->note_size); | |
793 | s->note_buf_offset = 0; | |
794 | ||
795 | /* use s->note_buf to store notes temporarily */ | |
4c7e251a HZ |
796 | write_elf32_notes(buf_write_note, s, &local_err); |
797 | if (local_err) { | |
798 | error_propagate(errp, local_err); | |
298f1168 QN |
799 | goto out; |
800 | } | |
298f1168 QN |
801 | if (write_buffer(s->fd, offset_note, s->note_buf, |
802 | s->note_size) < 0) { | |
37917da2 | 803 | dump_error(s, "dump: failed to write notes", errp); |
298f1168 QN |
804 | goto out; |
805 | } | |
806 | ||
807 | /* get offset of dump_bitmap */ | |
808 | s->offset_dump_bitmap = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size) * | |
809 | block_size; | |
810 | ||
811 | /* get offset of page */ | |
812 | s->offset_page = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size + bitmap_blocks) * | |
813 | block_size; | |
814 | ||
815 | out: | |
816 | g_free(dh); | |
817 | g_free(kh); | |
818 | g_free(s->note_buf); | |
298f1168 QN |
819 | } |
820 | ||
821 | /* write common header, sub header and elf note to vmcore */ | |
4c7e251a | 822 | static void create_header64(DumpState *s, Error **errp) |
298f1168 | 823 | { |
298f1168 QN |
824 | DiskDumpHeader64 *dh = NULL; |
825 | KdumpSubHeader64 *kh = NULL; | |
826 | size_t size; | |
298f1168 QN |
827 | uint32_t block_size; |
828 | uint32_t sub_hdr_size; | |
829 | uint32_t bitmap_blocks; | |
830 | uint32_t status = 0; | |
831 | uint64_t offset_note; | |
4c7e251a | 832 | Error *local_err = NULL; |
298f1168 QN |
833 | |
834 | /* write common header, the version of kdump-compressed format is 6th */ | |
835 | size = sizeof(DiskDumpHeader64); | |
836 | dh = g_malloc0(size); | |
837 | ||
838 | strncpy(dh->signature, KDUMP_SIGNATURE, strlen(KDUMP_SIGNATURE)); | |
acb0ef58 | 839 | dh->header_version = cpu_to_dump32(s, 6); |
8161befd | 840 | block_size = s->dump_info.page_size; |
acb0ef58 | 841 | dh->block_size = cpu_to_dump32(s, block_size); |
298f1168 QN |
842 | sub_hdr_size = sizeof(struct KdumpSubHeader64) + s->note_size; |
843 | sub_hdr_size = DIV_ROUND_UP(sub_hdr_size, block_size); | |
acb0ef58 | 844 | dh->sub_hdr_size = cpu_to_dump32(s, sub_hdr_size); |
298f1168 | 845 | /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */ |
acb0ef58 BR |
846 | dh->max_mapnr = cpu_to_dump32(s, MIN(s->max_mapnr, UINT_MAX)); |
847 | dh->nr_cpus = cpu_to_dump32(s, s->nr_cpus); | |
298f1168 | 848 | bitmap_blocks = DIV_ROUND_UP(s->len_dump_bitmap, block_size) * 2; |
acb0ef58 | 849 | dh->bitmap_blocks = cpu_to_dump32(s, bitmap_blocks); |
4ab23a91 | 850 | strncpy(dh->utsname.machine, ELF_MACHINE_UNAME, sizeof(dh->utsname.machine)); |
298f1168 QN |
851 | |
852 | if (s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) { | |
853 | status |= DUMP_DH_COMPRESSED_ZLIB; | |
854 | } | |
855 | #ifdef CONFIG_LZO | |
856 | if (s->flag_compress & DUMP_DH_COMPRESSED_LZO) { | |
857 | status |= DUMP_DH_COMPRESSED_LZO; | |
858 | } | |
859 | #endif | |
860 | #ifdef CONFIG_SNAPPY | |
861 | if (s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) { | |
862 | status |= DUMP_DH_COMPRESSED_SNAPPY; | |
863 | } | |
864 | #endif | |
acb0ef58 | 865 | dh->status = cpu_to_dump32(s, status); |
298f1168 QN |
866 | |
867 | if (write_buffer(s->fd, 0, dh, size) < 0) { | |
37917da2 | 868 | dump_error(s, "dump: failed to write disk dump header", errp); |
298f1168 QN |
869 | goto out; |
870 | } | |
871 | ||
872 | /* write sub header */ | |
873 | size = sizeof(KdumpSubHeader64); | |
874 | kh = g_malloc0(size); | |
875 | ||
876 | /* 64bit max_mapnr_64 */ | |
acb0ef58 | 877 | kh->max_mapnr_64 = cpu_to_dump64(s, s->max_mapnr); |
b6e05aa4 | 878 | kh->phys_base = cpu_to_dump64(s, s->dump_info.phys_base); |
acb0ef58 | 879 | kh->dump_level = cpu_to_dump32(s, DUMP_LEVEL); |
298f1168 QN |
880 | |
881 | offset_note = DISKDUMP_HEADER_BLOCKS * block_size + size; | |
acb0ef58 BR |
882 | kh->offset_note = cpu_to_dump64(s, offset_note); |
883 | kh->note_size = cpu_to_dump64(s, s->note_size); | |
298f1168 QN |
884 | |
885 | if (write_buffer(s->fd, DISKDUMP_HEADER_BLOCKS * | |
886 | block_size, kh, size) < 0) { | |
37917da2 | 887 | dump_error(s, "dump: failed to write kdump sub header", errp); |
298f1168 QN |
888 | goto out; |
889 | } | |
890 | ||
891 | /* write note */ | |
892 | s->note_buf = g_malloc0(s->note_size); | |
893 | s->note_buf_offset = 0; | |
894 | ||
895 | /* use s->note_buf to store notes temporarily */ | |
4c7e251a HZ |
896 | write_elf64_notes(buf_write_note, s, &local_err); |
897 | if (local_err) { | |
898 | error_propagate(errp, local_err); | |
298f1168 QN |
899 | goto out; |
900 | } | |
901 | ||
902 | if (write_buffer(s->fd, offset_note, s->note_buf, | |
903 | s->note_size) < 0) { | |
37917da2 | 904 | dump_error(s, "dump: failed to write notes", errp); |
298f1168 QN |
905 | goto out; |
906 | } | |
907 | ||
908 | /* get offset of dump_bitmap */ | |
909 | s->offset_dump_bitmap = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size) * | |
910 | block_size; | |
911 | ||
912 | /* get offset of page */ | |
913 | s->offset_page = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size + bitmap_blocks) * | |
914 | block_size; | |
915 | ||
916 | out: | |
917 | g_free(dh); | |
918 | g_free(kh); | |
919 | g_free(s->note_buf); | |
298f1168 QN |
920 | } |
921 | ||
4c7e251a | 922 | static void write_dump_header(DumpState *s, Error **errp) |
298f1168 | 923 | { |
4c7e251a HZ |
924 | Error *local_err = NULL; |
925 | ||
24aeeace | 926 | if (s->dump_info.d_class == ELFCLASS32) { |
4c7e251a | 927 | create_header32(s, &local_err); |
298f1168 | 928 | } else { |
4c7e251a HZ |
929 | create_header64(s, &local_err); |
930 | } | |
931 | if (local_err) { | |
932 | error_propagate(errp, local_err); | |
298f1168 QN |
933 | } |
934 | } | |
935 | ||
8161befd AJ |
936 | static size_t dump_bitmap_get_bufsize(DumpState *s) |
937 | { | |
938 | return s->dump_info.page_size; | |
939 | } | |
940 | ||
d0686c72 QN |
941 | /* |
942 | * set dump_bitmap sequencely. the bit before last_pfn is not allowed to be | |
943 | * rewritten, so if need to set the first bit, set last_pfn and pfn to 0. | |
944 | * set_dump_bitmap will always leave the recently set bit un-sync. And setting | |
945 | * (last bit + sizeof(buf) * 8) to 0 will do flushing the content in buf into | |
946 | * vmcore, ie. synchronizing un-sync bit into vmcore. | |
947 | */ | |
948 | static int set_dump_bitmap(uint64_t last_pfn, uint64_t pfn, bool value, | |
949 | uint8_t *buf, DumpState *s) | |
950 | { | |
951 | off_t old_offset, new_offset; | |
952 | off_t offset_bitmap1, offset_bitmap2; | |
953 | uint32_t byte, bit; | |
8161befd AJ |
954 | size_t bitmap_bufsize = dump_bitmap_get_bufsize(s); |
955 | size_t bits_per_buf = bitmap_bufsize * CHAR_BIT; | |
d0686c72 QN |
956 | |
957 | /* should not set the previous place */ | |
958 | assert(last_pfn <= pfn); | |
959 | ||
960 | /* | |
961 | * if the bit needed to be set is not cached in buf, flush the data in buf | |
962 | * to vmcore firstly. | |
963 | * making new_offset be bigger than old_offset can also sync remained data | |
964 | * into vmcore. | |
965 | */ | |
8161befd AJ |
966 | old_offset = bitmap_bufsize * (last_pfn / bits_per_buf); |
967 | new_offset = bitmap_bufsize * (pfn / bits_per_buf); | |
d0686c72 QN |
968 | |
969 | while (old_offset < new_offset) { | |
970 | /* calculate the offset and write dump_bitmap */ | |
971 | offset_bitmap1 = s->offset_dump_bitmap + old_offset; | |
972 | if (write_buffer(s->fd, offset_bitmap1, buf, | |
8161befd | 973 | bitmap_bufsize) < 0) { |
d0686c72 QN |
974 | return -1; |
975 | } | |
976 | ||
977 | /* dump level 1 is chosen, so 1st and 2nd bitmap are same */ | |
978 | offset_bitmap2 = s->offset_dump_bitmap + s->len_dump_bitmap + | |
979 | old_offset; | |
980 | if (write_buffer(s->fd, offset_bitmap2, buf, | |
8161befd | 981 | bitmap_bufsize) < 0) { |
d0686c72 QN |
982 | return -1; |
983 | } | |
984 | ||
8161befd AJ |
985 | memset(buf, 0, bitmap_bufsize); |
986 | old_offset += bitmap_bufsize; | |
d0686c72 QN |
987 | } |
988 | ||
989 | /* get the exact place of the bit in the buf, and set it */ | |
8161befd AJ |
990 | byte = (pfn % bits_per_buf) / CHAR_BIT; |
991 | bit = (pfn % bits_per_buf) % CHAR_BIT; | |
d0686c72 QN |
992 | if (value) { |
993 | buf[byte] |= 1u << bit; | |
994 | } else { | |
995 | buf[byte] &= ~(1u << bit); | |
996 | } | |
997 | ||
998 | return 0; | |
999 | } | |
1000 | ||
8161befd AJ |
1001 | static uint64_t dump_paddr_to_pfn(DumpState *s, uint64_t addr) |
1002 | { | |
1003 | int target_page_shift = ctz32(s->dump_info.page_size); | |
1004 | ||
1005 | return (addr >> target_page_shift) - ARCH_PFN_OFFSET; | |
1006 | } | |
1007 | ||
1008 | static uint64_t dump_pfn_to_paddr(DumpState *s, uint64_t pfn) | |
1009 | { | |
1010 | int target_page_shift = ctz32(s->dump_info.page_size); | |
1011 | ||
1012 | return (pfn + ARCH_PFN_OFFSET) << target_page_shift; | |
1013 | } | |
1014 | ||
d0686c72 QN |
1015 | /* |
1016 | * exam every page and return the page frame number and the address of the page. | |
1017 | * bufptr can be NULL. note: the blocks here is supposed to reflect guest-phys | |
1018 | * blocks, so block->target_start and block->target_end should be interal | |
1019 | * multiples of the target page size. | |
1020 | */ | |
1021 | static bool get_next_page(GuestPhysBlock **blockptr, uint64_t *pfnptr, | |
1022 | uint8_t **bufptr, DumpState *s) | |
1023 | { | |
1024 | GuestPhysBlock *block = *blockptr; | |
8161befd | 1025 | hwaddr addr, target_page_mask = ~((hwaddr)s->dump_info.page_size - 1); |
d0686c72 QN |
1026 | uint8_t *buf; |
1027 | ||
1028 | /* block == NULL means the start of the iteration */ | |
1029 | if (!block) { | |
1030 | block = QTAILQ_FIRST(&s->guest_phys_blocks.head); | |
1031 | *blockptr = block; | |
8161befd AJ |
1032 | assert((block->target_start & ~target_page_mask) == 0); |
1033 | assert((block->target_end & ~target_page_mask) == 0); | |
1034 | *pfnptr = dump_paddr_to_pfn(s, block->target_start); | |
d0686c72 QN |
1035 | if (bufptr) { |
1036 | *bufptr = block->host_addr; | |
1037 | } | |
1038 | return true; | |
1039 | } | |
1040 | ||
1041 | *pfnptr = *pfnptr + 1; | |
8161befd | 1042 | addr = dump_pfn_to_paddr(s, *pfnptr); |
d0686c72 QN |
1043 | |
1044 | if ((addr >= block->target_start) && | |
8161befd | 1045 | (addr + s->dump_info.page_size <= block->target_end)) { |
d0686c72 QN |
1046 | buf = block->host_addr + (addr - block->target_start); |
1047 | } else { | |
1048 | /* the next page is in the next block */ | |
1049 | block = QTAILQ_NEXT(block, next); | |
1050 | *blockptr = block; | |
1051 | if (!block) { | |
1052 | return false; | |
1053 | } | |
8161befd AJ |
1054 | assert((block->target_start & ~target_page_mask) == 0); |
1055 | assert((block->target_end & ~target_page_mask) == 0); | |
1056 | *pfnptr = dump_paddr_to_pfn(s, block->target_start); | |
d0686c72 QN |
1057 | buf = block->host_addr; |
1058 | } | |
1059 | ||
1060 | if (bufptr) { | |
1061 | *bufptr = buf; | |
1062 | } | |
1063 | ||
1064 | return true; | |
1065 | } | |
1066 | ||
4c7e251a | 1067 | static void write_dump_bitmap(DumpState *s, Error **errp) |
d0686c72 QN |
1068 | { |
1069 | int ret = 0; | |
1070 | uint64_t last_pfn, pfn; | |
1071 | void *dump_bitmap_buf; | |
1072 | size_t num_dumpable; | |
1073 | GuestPhysBlock *block_iter = NULL; | |
8161befd AJ |
1074 | size_t bitmap_bufsize = dump_bitmap_get_bufsize(s); |
1075 | size_t bits_per_buf = bitmap_bufsize * CHAR_BIT; | |
d0686c72 QN |
1076 | |
1077 | /* dump_bitmap_buf is used to store dump_bitmap temporarily */ | |
8161befd | 1078 | dump_bitmap_buf = g_malloc0(bitmap_bufsize); |
d0686c72 QN |
1079 | |
1080 | num_dumpable = 0; | |
1081 | last_pfn = 0; | |
1082 | ||
1083 | /* | |
1084 | * exam memory page by page, and set the bit in dump_bitmap corresponded | |
1085 | * to the existing page. | |
1086 | */ | |
1087 | while (get_next_page(&block_iter, &pfn, NULL, s)) { | |
1088 | ret = set_dump_bitmap(last_pfn, pfn, true, dump_bitmap_buf, s); | |
1089 | if (ret < 0) { | |
37917da2 | 1090 | dump_error(s, "dump: failed to set dump_bitmap", errp); |
d0686c72 QN |
1091 | goto out; |
1092 | } | |
1093 | ||
1094 | last_pfn = pfn; | |
1095 | num_dumpable++; | |
1096 | } | |
1097 | ||
1098 | /* | |
1099 | * set_dump_bitmap will always leave the recently set bit un-sync. Here we | |
8161befd AJ |
1100 | * set the remaining bits from last_pfn to the end of the bitmap buffer to |
1101 | * 0. With those set, the un-sync bit will be synchronized into the vmcore. | |
d0686c72 QN |
1102 | */ |
1103 | if (num_dumpable > 0) { | |
8161befd | 1104 | ret = set_dump_bitmap(last_pfn, last_pfn + bits_per_buf, false, |
d0686c72 QN |
1105 | dump_bitmap_buf, s); |
1106 | if (ret < 0) { | |
37917da2 | 1107 | dump_error(s, "dump: failed to sync dump_bitmap", errp); |
d0686c72 QN |
1108 | goto out; |
1109 | } | |
1110 | } | |
1111 | ||
1112 | /* number of dumpable pages that will be dumped later */ | |
1113 | s->num_dumpable = num_dumpable; | |
1114 | ||
1115 | out: | |
1116 | g_free(dump_bitmap_buf); | |
d0686c72 QN |
1117 | } |
1118 | ||
64cfba6a QN |
1119 | static void prepare_data_cache(DataCache *data_cache, DumpState *s, |
1120 | off_t offset) | |
1121 | { | |
1122 | data_cache->fd = s->fd; | |
1123 | data_cache->data_size = 0; | |
8161befd AJ |
1124 | data_cache->buf_size = 4 * dump_bitmap_get_bufsize(s); |
1125 | data_cache->buf = g_malloc0(data_cache->buf_size); | |
64cfba6a QN |
1126 | data_cache->offset = offset; |
1127 | } | |
1128 | ||
1129 | static int write_cache(DataCache *dc, const void *buf, size_t size, | |
1130 | bool flag_sync) | |
1131 | { | |
1132 | /* | |
1133 | * dc->buf_size should not be less than size, otherwise dc will never be | |
1134 | * enough | |
1135 | */ | |
1136 | assert(size <= dc->buf_size); | |
1137 | ||
1138 | /* | |
1139 | * if flag_sync is set, synchronize data in dc->buf into vmcore. | |
1140 | * otherwise check if the space is enough for caching data in buf, if not, | |
1141 | * write the data in dc->buf to dc->fd and reset dc->buf | |
1142 | */ | |
1143 | if ((!flag_sync && dc->data_size + size > dc->buf_size) || | |
1144 | (flag_sync && dc->data_size > 0)) { | |
1145 | if (write_buffer(dc->fd, dc->offset, dc->buf, dc->data_size) < 0) { | |
1146 | return -1; | |
1147 | } | |
1148 | ||
1149 | dc->offset += dc->data_size; | |
1150 | dc->data_size = 0; | |
1151 | } | |
1152 | ||
1153 | if (!flag_sync) { | |
1154 | memcpy(dc->buf + dc->data_size, buf, size); | |
1155 | dc->data_size += size; | |
1156 | } | |
1157 | ||
1158 | return 0; | |
1159 | } | |
1160 | ||
1161 | static void free_data_cache(DataCache *data_cache) | |
1162 | { | |
1163 | g_free(data_cache->buf); | |
1164 | } | |
1165 | ||
d12f57ec QN |
1166 | static size_t get_len_buf_out(size_t page_size, uint32_t flag_compress) |
1167 | { | |
b87ef351 LE |
1168 | switch (flag_compress) { |
1169 | case DUMP_DH_COMPRESSED_ZLIB: | |
1170 | return compressBound(page_size); | |
1171 | ||
1172 | case DUMP_DH_COMPRESSED_LZO: | |
1173 | /* | |
1174 | * LZO will expand incompressible data by a little amount. Please check | |
1175 | * the following URL to see the expansion calculation: | |
1176 | * http://www.oberhumer.com/opensource/lzo/lzofaq.php | |
1177 | */ | |
1178 | return page_size + page_size / 16 + 64 + 3; | |
d12f57ec QN |
1179 | |
1180 | #ifdef CONFIG_SNAPPY | |
b87ef351 LE |
1181 | case DUMP_DH_COMPRESSED_SNAPPY: |
1182 | return snappy_max_compressed_length(page_size); | |
d12f57ec | 1183 | #endif |
b87ef351 LE |
1184 | } |
1185 | return 0; | |
d12f57ec QN |
1186 | } |
1187 | ||
1188 | /* | |
1189 | * check if the page is all 0 | |
1190 | */ | |
1191 | static inline bool is_zero_page(const uint8_t *buf, size_t page_size) | |
1192 | { | |
1193 | return buffer_is_zero(buf, page_size); | |
1194 | } | |
1195 | ||
4c7e251a | 1196 | static void write_dump_pages(DumpState *s, Error **errp) |
d12f57ec QN |
1197 | { |
1198 | int ret = 0; | |
1199 | DataCache page_desc, page_data; | |
1200 | size_t len_buf_out, size_out; | |
1201 | #ifdef CONFIG_LZO | |
1202 | lzo_bytep wrkmem = NULL; | |
1203 | #endif | |
1204 | uint8_t *buf_out = NULL; | |
1205 | off_t offset_desc, offset_data; | |
1206 | PageDescriptor pd, pd_zero; | |
1207 | uint8_t *buf; | |
d12f57ec QN |
1208 | GuestPhysBlock *block_iter = NULL; |
1209 | uint64_t pfn_iter; | |
1210 | ||
1211 | /* get offset of page_desc and page_data in dump file */ | |
1212 | offset_desc = s->offset_page; | |
1213 | offset_data = offset_desc + sizeof(PageDescriptor) * s->num_dumpable; | |
1214 | ||
1215 | prepare_data_cache(&page_desc, s, offset_desc); | |
1216 | prepare_data_cache(&page_data, s, offset_data); | |
1217 | ||
1218 | /* prepare buffer to store compressed data */ | |
8161befd | 1219 | len_buf_out = get_len_buf_out(s->dump_info.page_size, s->flag_compress); |
b87ef351 | 1220 | assert(len_buf_out != 0); |
d12f57ec QN |
1221 | |
1222 | #ifdef CONFIG_LZO | |
1223 | wrkmem = g_malloc(LZO1X_1_MEM_COMPRESS); | |
1224 | #endif | |
1225 | ||
1226 | buf_out = g_malloc(len_buf_out); | |
1227 | ||
1228 | /* | |
1229 | * init zero page's page_desc and page_data, because every zero page | |
1230 | * uses the same page_data | |
1231 | */ | |
8161befd | 1232 | pd_zero.size = cpu_to_dump32(s, s->dump_info.page_size); |
acb0ef58 BR |
1233 | pd_zero.flags = cpu_to_dump32(s, 0); |
1234 | pd_zero.offset = cpu_to_dump64(s, offset_data); | |
1235 | pd_zero.page_flags = cpu_to_dump64(s, 0); | |
8161befd AJ |
1236 | buf = g_malloc0(s->dump_info.page_size); |
1237 | ret = write_cache(&page_data, buf, s->dump_info.page_size, false); | |
d12f57ec QN |
1238 | g_free(buf); |
1239 | if (ret < 0) { | |
37917da2 | 1240 | dump_error(s, "dump: failed to write page data (zero page)", errp); |
d12f57ec QN |
1241 | goto out; |
1242 | } | |
1243 | ||
8161befd | 1244 | offset_data += s->dump_info.page_size; |
d12f57ec QN |
1245 | |
1246 | /* | |
1247 | * dump memory to vmcore page by page. zero page will all be resided in the | |
1248 | * first page of page section | |
1249 | */ | |
1250 | while (get_next_page(&block_iter, &pfn_iter, &buf, s)) { | |
1251 | /* check zero page */ | |
8161befd | 1252 | if (is_zero_page(buf, s->dump_info.page_size)) { |
d12f57ec QN |
1253 | ret = write_cache(&page_desc, &pd_zero, sizeof(PageDescriptor), |
1254 | false); | |
1255 | if (ret < 0) { | |
37917da2 | 1256 | dump_error(s, "dump: failed to write page desc", errp); |
d12f57ec QN |
1257 | goto out; |
1258 | } | |
1259 | } else { | |
1260 | /* | |
1261 | * not zero page, then: | |
1262 | * 1. compress the page | |
1263 | * 2. write the compressed page into the cache of page_data | |
1264 | * 3. get page desc of the compressed page and write it into the | |
1265 | * cache of page_desc | |
1266 | * | |
1267 | * only one compression format will be used here, for | |
1268 | * s->flag_compress is set. But when compression fails to work, | |
1269 | * we fall back to save in plaintext. | |
1270 | */ | |
1271 | size_out = len_buf_out; | |
1272 | if ((s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) && | |
acb0ef58 | 1273 | (compress2(buf_out, (uLongf *)&size_out, buf, |
8161befd AJ |
1274 | s->dump_info.page_size, Z_BEST_SPEED) == Z_OK) && |
1275 | (size_out < s->dump_info.page_size)) { | |
acb0ef58 BR |
1276 | pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_ZLIB); |
1277 | pd.size = cpu_to_dump32(s, size_out); | |
d12f57ec QN |
1278 | |
1279 | ret = write_cache(&page_data, buf_out, size_out, false); | |
1280 | if (ret < 0) { | |
37917da2 | 1281 | dump_error(s, "dump: failed to write page data", errp); |
d12f57ec QN |
1282 | goto out; |
1283 | } | |
1284 | #ifdef CONFIG_LZO | |
1285 | } else if ((s->flag_compress & DUMP_DH_COMPRESSED_LZO) && | |
8161befd | 1286 | (lzo1x_1_compress(buf, s->dump_info.page_size, buf_out, |
d12f57ec | 1287 | (lzo_uint *)&size_out, wrkmem) == LZO_E_OK) && |
8161befd | 1288 | (size_out < s->dump_info.page_size)) { |
acb0ef58 BR |
1289 | pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_LZO); |
1290 | pd.size = cpu_to_dump32(s, size_out); | |
d12f57ec QN |
1291 | |
1292 | ret = write_cache(&page_data, buf_out, size_out, false); | |
1293 | if (ret < 0) { | |
37917da2 | 1294 | dump_error(s, "dump: failed to write page data", errp); |
d12f57ec QN |
1295 | goto out; |
1296 | } | |
1297 | #endif | |
1298 | #ifdef CONFIG_SNAPPY | |
1299 | } else if ((s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) && | |
8161befd | 1300 | (snappy_compress((char *)buf, s->dump_info.page_size, |
d12f57ec | 1301 | (char *)buf_out, &size_out) == SNAPPY_OK) && |
8161befd | 1302 | (size_out < s->dump_info.page_size)) { |
acb0ef58 BR |
1303 | pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_SNAPPY); |
1304 | pd.size = cpu_to_dump32(s, size_out); | |
d12f57ec QN |
1305 | |
1306 | ret = write_cache(&page_data, buf_out, size_out, false); | |
1307 | if (ret < 0) { | |
37917da2 | 1308 | dump_error(s, "dump: failed to write page data", errp); |
d12f57ec QN |
1309 | goto out; |
1310 | } | |
1311 | #endif | |
1312 | } else { | |
1313 | /* | |
1314 | * fall back to save in plaintext, size_out should be | |
8161befd | 1315 | * assigned the target's page size |
d12f57ec | 1316 | */ |
acb0ef58 | 1317 | pd.flags = cpu_to_dump32(s, 0); |
8161befd | 1318 | size_out = s->dump_info.page_size; |
acb0ef58 | 1319 | pd.size = cpu_to_dump32(s, size_out); |
d12f57ec | 1320 | |
8161befd AJ |
1321 | ret = write_cache(&page_data, buf, |
1322 | s->dump_info.page_size, false); | |
d12f57ec | 1323 | if (ret < 0) { |
37917da2 | 1324 | dump_error(s, "dump: failed to write page data", errp); |
d12f57ec QN |
1325 | goto out; |
1326 | } | |
1327 | } | |
1328 | ||
1329 | /* get and write page desc here */ | |
acb0ef58 BR |
1330 | pd.page_flags = cpu_to_dump64(s, 0); |
1331 | pd.offset = cpu_to_dump64(s, offset_data); | |
d12f57ec QN |
1332 | offset_data += size_out; |
1333 | ||
1334 | ret = write_cache(&page_desc, &pd, sizeof(PageDescriptor), false); | |
1335 | if (ret < 0) { | |
37917da2 | 1336 | dump_error(s, "dump: failed to write page desc", errp); |
d12f57ec QN |
1337 | goto out; |
1338 | } | |
1339 | } | |
1340 | } | |
1341 | ||
1342 | ret = write_cache(&page_desc, NULL, 0, true); | |
1343 | if (ret < 0) { | |
37917da2 | 1344 | dump_error(s, "dump: failed to sync cache for page_desc", errp); |
d12f57ec QN |
1345 | goto out; |
1346 | } | |
1347 | ret = write_cache(&page_data, NULL, 0, true); | |
1348 | if (ret < 0) { | |
37917da2 | 1349 | dump_error(s, "dump: failed to sync cache for page_data", errp); |
d12f57ec QN |
1350 | goto out; |
1351 | } | |
1352 | ||
1353 | out: | |
1354 | free_data_cache(&page_desc); | |
1355 | free_data_cache(&page_data); | |
1356 | ||
1357 | #ifdef CONFIG_LZO | |
1358 | g_free(wrkmem); | |
1359 | #endif | |
1360 | ||
1361 | g_free(buf_out); | |
d12f57ec QN |
1362 | } |
1363 | ||
4c7e251a | 1364 | static void create_kdump_vmcore(DumpState *s, Error **errp) |
b53ccc30 QN |
1365 | { |
1366 | int ret; | |
4c7e251a | 1367 | Error *local_err = NULL; |
b53ccc30 QN |
1368 | |
1369 | /* | |
1370 | * the kdump-compressed format is: | |
1371 | * File offset | |
1372 | * +------------------------------------------+ 0x0 | |
1373 | * | main header (struct disk_dump_header) | | |
1374 | * |------------------------------------------+ block 1 | |
1375 | * | sub header (struct kdump_sub_header) | | |
1376 | * |------------------------------------------+ block 2 | |
1377 | * | 1st-dump_bitmap | | |
1378 | * |------------------------------------------+ block 2 + X blocks | |
1379 | * | 2nd-dump_bitmap | (aligned by block) | |
1380 | * |------------------------------------------+ block 2 + 2 * X blocks | |
1381 | * | page desc for pfn 0 (struct page_desc) | (aligned by block) | |
1382 | * | page desc for pfn 1 (struct page_desc) | | |
1383 | * | : | | |
1384 | * |------------------------------------------| (not aligned by block) | |
1385 | * | page data (pfn 0) | | |
1386 | * | page data (pfn 1) | | |
1387 | * | : | | |
1388 | * +------------------------------------------+ | |
1389 | */ | |
1390 | ||
1391 | ret = write_start_flat_header(s->fd); | |
1392 | if (ret < 0) { | |
37917da2 | 1393 | dump_error(s, "dump: failed to write start flat header", errp); |
4c7e251a | 1394 | return; |
b53ccc30 QN |
1395 | } |
1396 | ||
4c7e251a HZ |
1397 | write_dump_header(s, &local_err); |
1398 | if (local_err) { | |
1399 | error_propagate(errp, local_err); | |
1400 | return; | |
b53ccc30 QN |
1401 | } |
1402 | ||
4c7e251a HZ |
1403 | write_dump_bitmap(s, &local_err); |
1404 | if (local_err) { | |
1405 | error_propagate(errp, local_err); | |
1406 | return; | |
b53ccc30 QN |
1407 | } |
1408 | ||
4c7e251a HZ |
1409 | write_dump_pages(s, &local_err); |
1410 | if (local_err) { | |
1411 | error_propagate(errp, local_err); | |
1412 | return; | |
b53ccc30 QN |
1413 | } |
1414 | ||
1415 | ret = write_end_flat_header(s->fd); | |
1416 | if (ret < 0) { | |
37917da2 | 1417 | dump_error(s, "dump: failed to write end flat header", errp); |
4c7e251a | 1418 | return; |
b53ccc30 QN |
1419 | } |
1420 | ||
1421 | dump_completed(s); | |
b53ccc30 QN |
1422 | } |
1423 | ||
783e9b48 WC |
1424 | static ram_addr_t get_start_block(DumpState *s) |
1425 | { | |
56c4bfb3 | 1426 | GuestPhysBlock *block; |
783e9b48 WC |
1427 | |
1428 | if (!s->has_filter) { | |
56c4bfb3 | 1429 | s->next_block = QTAILQ_FIRST(&s->guest_phys_blocks.head); |
783e9b48 WC |
1430 | return 0; |
1431 | } | |
1432 | ||
56c4bfb3 LE |
1433 | QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) { |
1434 | if (block->target_start >= s->begin + s->length || | |
1435 | block->target_end <= s->begin) { | |
783e9b48 WC |
1436 | /* This block is out of the range */ |
1437 | continue; | |
1438 | } | |
1439 | ||
56c4bfb3 LE |
1440 | s->next_block = block; |
1441 | if (s->begin > block->target_start) { | |
1442 | s->start = s->begin - block->target_start; | |
783e9b48 WC |
1443 | } else { |
1444 | s->start = 0; | |
1445 | } | |
1446 | return s->start; | |
1447 | } | |
1448 | ||
1449 | return -1; | |
1450 | } | |
1451 | ||
7aad248d QN |
1452 | static void get_max_mapnr(DumpState *s) |
1453 | { | |
1454 | GuestPhysBlock *last_block; | |
1455 | ||
1456 | last_block = QTAILQ_LAST(&s->guest_phys_blocks.head, GuestPhysBlockHead); | |
8161befd | 1457 | s->max_mapnr = dump_paddr_to_pfn(s, last_block->target_end); |
7aad248d QN |
1458 | } |
1459 | ||
4c7e251a HZ |
1460 | static void dump_init(DumpState *s, int fd, bool has_format, |
1461 | DumpGuestMemoryFormat format, bool paging, bool has_filter, | |
1462 | int64_t begin, int64_t length, Error **errp) | |
783e9b48 | 1463 | { |
182735ef | 1464 | CPUState *cpu; |
783e9b48 | 1465 | int nr_cpus; |
11ed09cf | 1466 | Error *err = NULL; |
783e9b48 WC |
1467 | int ret; |
1468 | ||
b53ccc30 QN |
1469 | /* kdump-compressed is conflict with paging and filter */ |
1470 | if (has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) { | |
1471 | assert(!paging && !has_filter); | |
1472 | } | |
1473 | ||
783e9b48 WC |
1474 | if (runstate_is_running()) { |
1475 | vm_stop(RUN_STATE_SAVE_VM); | |
1476 | s->resume = true; | |
1477 | } else { | |
1478 | s->resume = false; | |
1479 | } | |
1480 | ||
5ee163e8 LE |
1481 | /* If we use KVM, we should synchronize the registers before we get dump |
1482 | * info or physmap info. | |
1483 | */ | |
1484 | cpu_synchronize_all_states(); | |
1485 | nr_cpus = 0; | |
bdc44640 | 1486 | CPU_FOREACH(cpu) { |
5ee163e8 LE |
1487 | nr_cpus++; |
1488 | } | |
1489 | ||
783e9b48 WC |
1490 | s->fd = fd; |
1491 | s->has_filter = has_filter; | |
1492 | s->begin = begin; | |
1493 | s->length = length; | |
5ee163e8 | 1494 | |
2928207a CG |
1495 | memory_mapping_list_init(&s->list); |
1496 | ||
5ee163e8 | 1497 | guest_phys_blocks_init(&s->guest_phys_blocks); |
c5d7f60f | 1498 | guest_phys_blocks_append(&s->guest_phys_blocks); |
5ee163e8 | 1499 | |
783e9b48 WC |
1500 | s->start = get_start_block(s); |
1501 | if (s->start == -1) { | |
c6bd8c70 | 1502 | error_setg(errp, QERR_INVALID_PARAMETER, "begin"); |
783e9b48 WC |
1503 | goto cleanup; |
1504 | } | |
1505 | ||
5ee163e8 | 1506 | /* get dump info: endian, class and architecture. |
783e9b48 WC |
1507 | * If the target architecture is not supported, cpu_get_dump_info() will |
1508 | * return -1. | |
783e9b48 | 1509 | */ |
56c4bfb3 | 1510 | ret = cpu_get_dump_info(&s->dump_info, &s->guest_phys_blocks); |
783e9b48 | 1511 | if (ret < 0) { |
c6bd8c70 | 1512 | error_setg(errp, QERR_UNSUPPORTED); |
783e9b48 WC |
1513 | goto cleanup; |
1514 | } | |
1515 | ||
8161befd AJ |
1516 | if (!s->dump_info.page_size) { |
1517 | s->dump_info.page_size = TARGET_PAGE_SIZE; | |
1518 | } | |
1519 | ||
4720bd05 PB |
1520 | s->note_size = cpu_get_note_size(s->dump_info.d_class, |
1521 | s->dump_info.d_machine, nr_cpus); | |
bb6b6843 | 1522 | if (s->note_size < 0) { |
c6bd8c70 | 1523 | error_setg(errp, QERR_UNSUPPORTED); |
4720bd05 PB |
1524 | goto cleanup; |
1525 | } | |
1526 | ||
783e9b48 | 1527 | /* get memory mapping */ |
783e9b48 | 1528 | if (paging) { |
56c4bfb3 | 1529 | qemu_get_guest_memory_mapping(&s->list, &s->guest_phys_blocks, &err); |
11ed09cf AF |
1530 | if (err != NULL) { |
1531 | error_propagate(errp, err); | |
1532 | goto cleanup; | |
1533 | } | |
783e9b48 | 1534 | } else { |
56c4bfb3 | 1535 | qemu_get_guest_simple_memory_mapping(&s->list, &s->guest_phys_blocks); |
783e9b48 WC |
1536 | } |
1537 | ||
7aad248d | 1538 | s->nr_cpus = nr_cpus; |
7aad248d QN |
1539 | |
1540 | get_max_mapnr(s); | |
1541 | ||
1542 | uint64_t tmp; | |
8161befd AJ |
1543 | tmp = DIV_ROUND_UP(DIV_ROUND_UP(s->max_mapnr, CHAR_BIT), |
1544 | s->dump_info.page_size); | |
1545 | s->len_dump_bitmap = tmp * s->dump_info.page_size; | |
7aad248d | 1546 | |
b53ccc30 QN |
1547 | /* init for kdump-compressed format */ |
1548 | if (has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) { | |
1549 | switch (format) { | |
1550 | case DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB: | |
1551 | s->flag_compress = DUMP_DH_COMPRESSED_ZLIB; | |
1552 | break; | |
1553 | ||
1554 | case DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO: | |
c998acb0 LE |
1555 | #ifdef CONFIG_LZO |
1556 | if (lzo_init() != LZO_E_OK) { | |
1557 | error_setg(errp, "failed to initialize the LZO library"); | |
1558 | goto cleanup; | |
1559 | } | |
1560 | #endif | |
b53ccc30 QN |
1561 | s->flag_compress = DUMP_DH_COMPRESSED_LZO; |
1562 | break; | |
1563 | ||
1564 | case DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY: | |
1565 | s->flag_compress = DUMP_DH_COMPRESSED_SNAPPY; | |
1566 | break; | |
1567 | ||
1568 | default: | |
1569 | s->flag_compress = 0; | |
1570 | } | |
1571 | ||
4c7e251a | 1572 | return; |
b53ccc30 QN |
1573 | } |
1574 | ||
783e9b48 WC |
1575 | if (s->has_filter) { |
1576 | memory_mapping_filter(&s->list, s->begin, s->length); | |
1577 | } | |
1578 | ||
1579 | /* | |
1580 | * calculate phdr_num | |
1581 | * | |
1582 | * the type of ehdr->e_phnum is uint16_t, so we should avoid overflow | |
1583 | */ | |
1584 | s->phdr_num = 1; /* PT_NOTE */ | |
1585 | if (s->list.num < UINT16_MAX - 2) { | |
1586 | s->phdr_num += s->list.num; | |
1587 | s->have_section = false; | |
1588 | } else { | |
1589 | s->have_section = true; | |
1590 | s->phdr_num = PN_XNUM; | |
1591 | s->sh_info = 1; /* PT_NOTE */ | |
1592 | ||
1593 | /* the type of shdr->sh_info is uint32_t, so we should avoid overflow */ | |
1594 | if (s->list.num <= UINT32_MAX - 1) { | |
1595 | s->sh_info += s->list.num; | |
1596 | } else { | |
1597 | s->sh_info = UINT32_MAX; | |
1598 | } | |
1599 | } | |
1600 | ||
783e9b48 WC |
1601 | if (s->dump_info.d_class == ELFCLASS64) { |
1602 | if (s->have_section) { | |
1603 | s->memory_offset = sizeof(Elf64_Ehdr) + | |
1604 | sizeof(Elf64_Phdr) * s->sh_info + | |
1605 | sizeof(Elf64_Shdr) + s->note_size; | |
1606 | } else { | |
1607 | s->memory_offset = sizeof(Elf64_Ehdr) + | |
1608 | sizeof(Elf64_Phdr) * s->phdr_num + s->note_size; | |
1609 | } | |
1610 | } else { | |
1611 | if (s->have_section) { | |
1612 | s->memory_offset = sizeof(Elf32_Ehdr) + | |
1613 | sizeof(Elf32_Phdr) * s->sh_info + | |
1614 | sizeof(Elf32_Shdr) + s->note_size; | |
1615 | } else { | |
1616 | s->memory_offset = sizeof(Elf32_Ehdr) + | |
1617 | sizeof(Elf32_Phdr) * s->phdr_num + s->note_size; | |
1618 | } | |
1619 | } | |
1620 | ||
4c7e251a | 1621 | return; |
783e9b48 WC |
1622 | |
1623 | cleanup: | |
2928207a | 1624 | dump_cleanup(s); |
783e9b48 WC |
1625 | } |
1626 | ||
1627 | void qmp_dump_guest_memory(bool paging, const char *file, bool has_begin, | |
b53ccc30 QN |
1628 | int64_t begin, bool has_length, |
1629 | int64_t length, bool has_format, | |
1630 | DumpGuestMemoryFormat format, Error **errp) | |
783e9b48 WC |
1631 | { |
1632 | const char *p; | |
1633 | int fd = -1; | |
1634 | DumpState *s; | |
4c7e251a | 1635 | Error *local_err = NULL; |
783e9b48 | 1636 | |
b53ccc30 QN |
1637 | /* |
1638 | * kdump-compressed format need the whole memory dumped, so paging or | |
1639 | * filter is not supported here. | |
1640 | */ | |
1641 | if ((has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) && | |
1642 | (paging || has_begin || has_length)) { | |
1643 | error_setg(errp, "kdump-compressed format doesn't support paging or " | |
1644 | "filter"); | |
1645 | return; | |
1646 | } | |
783e9b48 | 1647 | if (has_begin && !has_length) { |
c6bd8c70 | 1648 | error_setg(errp, QERR_MISSING_PARAMETER, "length"); |
783e9b48 WC |
1649 | return; |
1650 | } | |
1651 | if (!has_begin && has_length) { | |
c6bd8c70 | 1652 | error_setg(errp, QERR_MISSING_PARAMETER, "begin"); |
783e9b48 WC |
1653 | return; |
1654 | } | |
1655 | ||
b53ccc30 QN |
1656 | /* check whether lzo/snappy is supported */ |
1657 | #ifndef CONFIG_LZO | |
1658 | if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO) { | |
1659 | error_setg(errp, "kdump-lzo is not available now"); | |
1660 | return; | |
1661 | } | |
1662 | #endif | |
1663 | ||
1664 | #ifndef CONFIG_SNAPPY | |
1665 | if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY) { | |
1666 | error_setg(errp, "kdump-snappy is not available now"); | |
1667 | return; | |
1668 | } | |
1669 | #endif | |
1670 | ||
783e9b48 WC |
1671 | #if !defined(WIN32) |
1672 | if (strstart(file, "fd:", &p)) { | |
a9940fc4 | 1673 | fd = monitor_get_fd(cur_mon, p, errp); |
783e9b48 | 1674 | if (fd == -1) { |
783e9b48 WC |
1675 | return; |
1676 | } | |
1677 | } | |
1678 | #endif | |
1679 | ||
1680 | if (strstart(file, "file:", &p)) { | |
1681 | fd = qemu_open(p, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, S_IRUSR); | |
1682 | if (fd < 0) { | |
7581766b | 1683 | error_setg_file_open(errp, errno, p); |
783e9b48 WC |
1684 | return; |
1685 | } | |
1686 | } | |
1687 | ||
1688 | if (fd == -1) { | |
c6bd8c70 | 1689 | error_setg(errp, QERR_INVALID_PARAMETER, "protocol"); |
783e9b48 WC |
1690 | return; |
1691 | } | |
1692 | ||
5ee163e8 | 1693 | s = g_malloc0(sizeof(DumpState)); |
783e9b48 | 1694 | |
4c7e251a HZ |
1695 | dump_init(s, fd, has_format, format, paging, has_begin, |
1696 | begin, length, &local_err); | |
1697 | if (local_err) { | |
783e9b48 | 1698 | g_free(s); |
4c7e251a | 1699 | error_propagate(errp, local_err); |
783e9b48 WC |
1700 | return; |
1701 | } | |
1702 | ||
b53ccc30 | 1703 | if (has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) { |
37917da2 | 1704 | create_kdump_vmcore(s, errp); |
b53ccc30 | 1705 | } else { |
37917da2 | 1706 | create_vmcore(s, errp); |
783e9b48 WC |
1707 | } |
1708 | ||
1709 | g_free(s); | |
1710 | } | |
7d6dc7f3 QN |
1711 | |
1712 | DumpGuestMemoryCapability *qmp_query_dump_guest_memory_capability(Error **errp) | |
1713 | { | |
1714 | DumpGuestMemoryFormatList *item; | |
1715 | DumpGuestMemoryCapability *cap = | |
1716 | g_malloc0(sizeof(DumpGuestMemoryCapability)); | |
1717 | ||
1718 | /* elf is always available */ | |
1719 | item = g_malloc0(sizeof(DumpGuestMemoryFormatList)); | |
1720 | cap->formats = item; | |
1721 | item->value = DUMP_GUEST_MEMORY_FORMAT_ELF; | |
1722 | ||
1723 | /* kdump-zlib is always available */ | |
1724 | item->next = g_malloc0(sizeof(DumpGuestMemoryFormatList)); | |
1725 | item = item->next; | |
1726 | item->value = DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB; | |
1727 | ||
1728 | /* add new item if kdump-lzo is available */ | |
1729 | #ifdef CONFIG_LZO | |
1730 | item->next = g_malloc0(sizeof(DumpGuestMemoryFormatList)); | |
1731 | item = item->next; | |
1732 | item->value = DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO; | |
1733 | #endif | |
1734 | ||
1735 | /* add new item if kdump-snappy is available */ | |
1736 | #ifdef CONFIG_SNAPPY | |
1737 | item->next = g_malloc0(sizeof(DumpGuestMemoryFormatList)); | |
1738 | item = item->next; | |
1739 | item->value = DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY; | |
1740 | #endif | |
1741 | ||
1742 | return cap; | |
1743 | } |