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