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