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