]>
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 | |
783e9b48 WC |
28 | static uint16_t cpu_convert_to_target16(uint16_t val, int endian) |
29 | { | |
30 | if (endian == ELFDATA2LSB) { | |
31 | val = cpu_to_le16(val); | |
32 | } else { | |
33 | val = cpu_to_be16(val); | |
34 | } | |
35 | ||
36 | return val; | |
37 | } | |
38 | ||
39 | static uint32_t cpu_convert_to_target32(uint32_t val, int endian) | |
40 | { | |
41 | if (endian == ELFDATA2LSB) { | |
42 | val = cpu_to_le32(val); | |
43 | } else { | |
44 | val = cpu_to_be32(val); | |
45 | } | |
46 | ||
47 | return val; | |
48 | } | |
49 | ||
50 | static uint64_t cpu_convert_to_target64(uint64_t val, int endian) | |
51 | { | |
52 | if (endian == ELFDATA2LSB) { | |
53 | val = cpu_to_le64(val); | |
54 | } else { | |
55 | val = cpu_to_be64(val); | |
56 | } | |
57 | ||
58 | return val; | |
59 | } | |
60 | ||
61 | typedef struct DumpState { | |
62 | ArchDumpInfo dump_info; | |
63 | MemoryMappingList list; | |
64 | uint16_t phdr_num; | |
65 | uint32_t sh_info; | |
66 | bool have_section; | |
67 | bool resume; | |
68 | size_t note_size; | |
a8170e5e | 69 | hwaddr memory_offset; |
783e9b48 WC |
70 | int fd; |
71 | ||
72 | RAMBlock *block; | |
73 | ram_addr_t start; | |
74 | bool has_filter; | |
75 | int64_t begin; | |
76 | int64_t length; | |
77 | Error **errp; | |
78 | } DumpState; | |
79 | ||
80 | static int dump_cleanup(DumpState *s) | |
81 | { | |
82 | int ret = 0; | |
83 | ||
84 | memory_mapping_list_free(&s->list); | |
85 | if (s->fd != -1) { | |
86 | close(s->fd); | |
87 | } | |
88 | if (s->resume) { | |
89 | vm_start(); | |
90 | } | |
91 | ||
92 | return ret; | |
93 | } | |
94 | ||
95 | static void dump_error(DumpState *s, const char *reason) | |
96 | { | |
97 | dump_cleanup(s); | |
98 | } | |
99 | ||
100 | static int fd_write_vmcore(void *buf, size_t size, void *opaque) | |
101 | { | |
102 | DumpState *s = opaque; | |
2f61652d LC |
103 | size_t written_size; |
104 | ||
105 | written_size = qemu_write_full(s->fd, buf, size); | |
106 | if (written_size != size) { | |
107 | return -1; | |
783e9b48 WC |
108 | } |
109 | ||
110 | return 0; | |
111 | } | |
112 | ||
113 | static int write_elf64_header(DumpState *s) | |
114 | { | |
115 | Elf64_Ehdr elf_header; | |
116 | int ret; | |
117 | int endian = s->dump_info.d_endian; | |
118 | ||
119 | memset(&elf_header, 0, sizeof(Elf64_Ehdr)); | |
120 | memcpy(&elf_header, ELFMAG, SELFMAG); | |
121 | elf_header.e_ident[EI_CLASS] = ELFCLASS64; | |
122 | elf_header.e_ident[EI_DATA] = s->dump_info.d_endian; | |
123 | elf_header.e_ident[EI_VERSION] = EV_CURRENT; | |
124 | elf_header.e_type = cpu_convert_to_target16(ET_CORE, endian); | |
125 | elf_header.e_machine = cpu_convert_to_target16(s->dump_info.d_machine, | |
126 | endian); | |
127 | elf_header.e_version = cpu_convert_to_target32(EV_CURRENT, endian); | |
128 | elf_header.e_ehsize = cpu_convert_to_target16(sizeof(elf_header), endian); | |
129 | elf_header.e_phoff = cpu_convert_to_target64(sizeof(Elf64_Ehdr), endian); | |
130 | elf_header.e_phentsize = cpu_convert_to_target16(sizeof(Elf64_Phdr), | |
131 | endian); | |
132 | elf_header.e_phnum = cpu_convert_to_target16(s->phdr_num, endian); | |
133 | if (s->have_section) { | |
134 | uint64_t shoff = sizeof(Elf64_Ehdr) + sizeof(Elf64_Phdr) * s->sh_info; | |
135 | ||
136 | elf_header.e_shoff = cpu_convert_to_target64(shoff, endian); | |
137 | elf_header.e_shentsize = cpu_convert_to_target16(sizeof(Elf64_Shdr), | |
138 | endian); | |
139 | elf_header.e_shnum = cpu_convert_to_target16(1, endian); | |
140 | } | |
141 | ||
142 | ret = fd_write_vmcore(&elf_header, sizeof(elf_header), s); | |
143 | if (ret < 0) { | |
144 | dump_error(s, "dump: failed to write elf header.\n"); | |
145 | return -1; | |
146 | } | |
147 | ||
148 | return 0; | |
149 | } | |
150 | ||
151 | static int write_elf32_header(DumpState *s) | |
152 | { | |
153 | Elf32_Ehdr elf_header; | |
154 | int ret; | |
155 | int endian = s->dump_info.d_endian; | |
156 | ||
157 | memset(&elf_header, 0, sizeof(Elf32_Ehdr)); | |
158 | memcpy(&elf_header, ELFMAG, SELFMAG); | |
159 | elf_header.e_ident[EI_CLASS] = ELFCLASS32; | |
160 | elf_header.e_ident[EI_DATA] = endian; | |
161 | elf_header.e_ident[EI_VERSION] = EV_CURRENT; | |
162 | elf_header.e_type = cpu_convert_to_target16(ET_CORE, endian); | |
163 | elf_header.e_machine = cpu_convert_to_target16(s->dump_info.d_machine, | |
164 | endian); | |
165 | elf_header.e_version = cpu_convert_to_target32(EV_CURRENT, endian); | |
166 | elf_header.e_ehsize = cpu_convert_to_target16(sizeof(elf_header), endian); | |
167 | elf_header.e_phoff = cpu_convert_to_target32(sizeof(Elf32_Ehdr), endian); | |
168 | elf_header.e_phentsize = cpu_convert_to_target16(sizeof(Elf32_Phdr), | |
169 | endian); | |
170 | elf_header.e_phnum = cpu_convert_to_target16(s->phdr_num, endian); | |
171 | if (s->have_section) { | |
172 | uint32_t shoff = sizeof(Elf32_Ehdr) + sizeof(Elf32_Phdr) * s->sh_info; | |
173 | ||
174 | elf_header.e_shoff = cpu_convert_to_target32(shoff, endian); | |
175 | elf_header.e_shentsize = cpu_convert_to_target16(sizeof(Elf32_Shdr), | |
176 | endian); | |
177 | elf_header.e_shnum = cpu_convert_to_target16(1, endian); | |
178 | } | |
179 | ||
180 | ret = fd_write_vmcore(&elf_header, sizeof(elf_header), s); | |
181 | if (ret < 0) { | |
182 | dump_error(s, "dump: failed to write elf header.\n"); | |
183 | return -1; | |
184 | } | |
185 | ||
186 | return 0; | |
187 | } | |
188 | ||
189 | static int write_elf64_load(DumpState *s, MemoryMapping *memory_mapping, | |
a8170e5e | 190 | int phdr_index, hwaddr offset) |
783e9b48 WC |
191 | { |
192 | Elf64_Phdr phdr; | |
193 | int ret; | |
194 | int endian = s->dump_info.d_endian; | |
195 | ||
196 | memset(&phdr, 0, sizeof(Elf64_Phdr)); | |
197 | phdr.p_type = cpu_convert_to_target32(PT_LOAD, endian); | |
198 | phdr.p_offset = cpu_convert_to_target64(offset, endian); | |
199 | phdr.p_paddr = cpu_convert_to_target64(memory_mapping->phys_addr, endian); | |
200 | if (offset == -1) { | |
201 | /* When the memory is not stored into vmcore, offset will be -1 */ | |
202 | phdr.p_filesz = 0; | |
203 | } else { | |
204 | phdr.p_filesz = cpu_convert_to_target64(memory_mapping->length, endian); | |
205 | } | |
206 | phdr.p_memsz = cpu_convert_to_target64(memory_mapping->length, endian); | |
207 | phdr.p_vaddr = cpu_convert_to_target64(memory_mapping->virt_addr, endian); | |
208 | ||
209 | ret = fd_write_vmcore(&phdr, sizeof(Elf64_Phdr), s); | |
210 | if (ret < 0) { | |
211 | dump_error(s, "dump: failed to write program header table.\n"); | |
212 | return -1; | |
213 | } | |
214 | ||
215 | return 0; | |
216 | } | |
217 | ||
218 | static int write_elf32_load(DumpState *s, MemoryMapping *memory_mapping, | |
a8170e5e | 219 | int phdr_index, hwaddr offset) |
783e9b48 WC |
220 | { |
221 | Elf32_Phdr phdr; | |
222 | int ret; | |
223 | int endian = s->dump_info.d_endian; | |
224 | ||
225 | memset(&phdr, 0, sizeof(Elf32_Phdr)); | |
226 | phdr.p_type = cpu_convert_to_target32(PT_LOAD, endian); | |
227 | phdr.p_offset = cpu_convert_to_target32(offset, endian); | |
228 | phdr.p_paddr = cpu_convert_to_target32(memory_mapping->phys_addr, endian); | |
229 | if (offset == -1) { | |
230 | /* When the memory is not stored into vmcore, offset will be -1 */ | |
231 | phdr.p_filesz = 0; | |
232 | } else { | |
233 | phdr.p_filesz = cpu_convert_to_target32(memory_mapping->length, endian); | |
234 | } | |
235 | phdr.p_memsz = cpu_convert_to_target32(memory_mapping->length, endian); | |
236 | phdr.p_vaddr = cpu_convert_to_target32(memory_mapping->virt_addr, endian); | |
237 | ||
238 | ret = fd_write_vmcore(&phdr, sizeof(Elf32_Phdr), s); | |
239 | if (ret < 0) { | |
240 | dump_error(s, "dump: failed to write program header table.\n"); | |
241 | return -1; | |
242 | } | |
243 | ||
244 | return 0; | |
245 | } | |
246 | ||
247 | static int write_elf64_note(DumpState *s) | |
248 | { | |
249 | Elf64_Phdr phdr; | |
250 | int endian = s->dump_info.d_endian; | |
a8170e5e | 251 | hwaddr begin = s->memory_offset - s->note_size; |
783e9b48 WC |
252 | int ret; |
253 | ||
254 | memset(&phdr, 0, sizeof(Elf64_Phdr)); | |
255 | phdr.p_type = cpu_convert_to_target32(PT_NOTE, endian); | |
256 | phdr.p_offset = cpu_convert_to_target64(begin, endian); | |
257 | phdr.p_paddr = 0; | |
258 | phdr.p_filesz = cpu_convert_to_target64(s->note_size, endian); | |
259 | phdr.p_memsz = cpu_convert_to_target64(s->note_size, endian); | |
260 | phdr.p_vaddr = 0; | |
261 | ||
262 | ret = fd_write_vmcore(&phdr, sizeof(Elf64_Phdr), s); | |
263 | if (ret < 0) { | |
264 | dump_error(s, "dump: failed to write program header table.\n"); | |
265 | return -1; | |
266 | } | |
267 | ||
268 | return 0; | |
269 | } | |
270 | ||
0bc3cd62 PB |
271 | static inline int cpu_index(CPUState *cpu) |
272 | { | |
273 | return cpu->cpu_index + 1; | |
274 | } | |
275 | ||
783e9b48 WC |
276 | static int write_elf64_notes(DumpState *s) |
277 | { | |
0d34282f | 278 | CPUState *cpu; |
783e9b48 WC |
279 | int ret; |
280 | int id; | |
281 | ||
182735ef | 282 | for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) { |
0d34282f | 283 | id = cpu_index(cpu); |
c72bf468 | 284 | ret = cpu_write_elf64_note(fd_write_vmcore, cpu, id, s); |
783e9b48 WC |
285 | if (ret < 0) { |
286 | dump_error(s, "dump: failed to write elf notes.\n"); | |
287 | return -1; | |
288 | } | |
289 | } | |
290 | ||
182735ef | 291 | for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) { |
c72bf468 | 292 | ret = cpu_write_elf64_qemunote(fd_write_vmcore, cpu, s); |
783e9b48 WC |
293 | if (ret < 0) { |
294 | dump_error(s, "dump: failed to write CPU status.\n"); | |
295 | return -1; | |
296 | } | |
297 | } | |
298 | ||
299 | return 0; | |
300 | } | |
301 | ||
302 | static int write_elf32_note(DumpState *s) | |
303 | { | |
a8170e5e | 304 | hwaddr begin = s->memory_offset - s->note_size; |
783e9b48 WC |
305 | Elf32_Phdr phdr; |
306 | int endian = s->dump_info.d_endian; | |
307 | int ret; | |
308 | ||
309 | memset(&phdr, 0, sizeof(Elf32_Phdr)); | |
310 | phdr.p_type = cpu_convert_to_target32(PT_NOTE, endian); | |
311 | phdr.p_offset = cpu_convert_to_target32(begin, endian); | |
312 | phdr.p_paddr = 0; | |
313 | phdr.p_filesz = cpu_convert_to_target32(s->note_size, endian); | |
314 | phdr.p_memsz = cpu_convert_to_target32(s->note_size, endian); | |
315 | phdr.p_vaddr = 0; | |
316 | ||
317 | ret = fd_write_vmcore(&phdr, sizeof(Elf32_Phdr), s); | |
318 | if (ret < 0) { | |
319 | dump_error(s, "dump: failed to write program header table.\n"); | |
320 | return -1; | |
321 | } | |
322 | ||
323 | return 0; | |
324 | } | |
325 | ||
326 | static int write_elf32_notes(DumpState *s) | |
327 | { | |
0d34282f | 328 | CPUState *cpu; |
783e9b48 WC |
329 | int ret; |
330 | int id; | |
331 | ||
182735ef | 332 | for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) { |
0d34282f | 333 | id = cpu_index(cpu); |
c72bf468 | 334 | ret = cpu_write_elf32_note(fd_write_vmcore, cpu, id, s); |
783e9b48 WC |
335 | if (ret < 0) { |
336 | dump_error(s, "dump: failed to write elf notes.\n"); | |
337 | return -1; | |
338 | } | |
339 | } | |
340 | ||
182735ef | 341 | for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) { |
c72bf468 | 342 | ret = cpu_write_elf32_qemunote(fd_write_vmcore, cpu, s); |
783e9b48 WC |
343 | if (ret < 0) { |
344 | dump_error(s, "dump: failed to write CPU status.\n"); | |
345 | return -1; | |
346 | } | |
347 | } | |
348 | ||
349 | return 0; | |
350 | } | |
351 | ||
352 | static int write_elf_section(DumpState *s, int type) | |
353 | { | |
354 | Elf32_Shdr shdr32; | |
355 | Elf64_Shdr shdr64; | |
356 | int endian = s->dump_info.d_endian; | |
357 | int shdr_size; | |
358 | void *shdr; | |
359 | int ret; | |
360 | ||
361 | if (type == 0) { | |
362 | shdr_size = sizeof(Elf32_Shdr); | |
363 | memset(&shdr32, 0, shdr_size); | |
364 | shdr32.sh_info = cpu_convert_to_target32(s->sh_info, endian); | |
365 | shdr = &shdr32; | |
366 | } else { | |
367 | shdr_size = sizeof(Elf64_Shdr); | |
368 | memset(&shdr64, 0, shdr_size); | |
369 | shdr64.sh_info = cpu_convert_to_target32(s->sh_info, endian); | |
370 | shdr = &shdr64; | |
371 | } | |
372 | ||
373 | ret = fd_write_vmcore(&shdr, shdr_size, s); | |
374 | if (ret < 0) { | |
375 | dump_error(s, "dump: failed to write section header table.\n"); | |
376 | return -1; | |
377 | } | |
378 | ||
379 | return 0; | |
380 | } | |
381 | ||
382 | static int write_data(DumpState *s, void *buf, int length) | |
383 | { | |
384 | int ret; | |
385 | ||
386 | ret = fd_write_vmcore(buf, length, s); | |
387 | if (ret < 0) { | |
388 | dump_error(s, "dump: failed to save memory.\n"); | |
389 | return -1; | |
390 | } | |
391 | ||
392 | return 0; | |
393 | } | |
394 | ||
395 | /* write the memroy to vmcore. 1 page per I/O. */ | |
396 | static int write_memory(DumpState *s, RAMBlock *block, ram_addr_t start, | |
397 | int64_t size) | |
398 | { | |
399 | int64_t i; | |
400 | int ret; | |
401 | ||
402 | for (i = 0; i < size / TARGET_PAGE_SIZE; i++) { | |
403 | ret = write_data(s, block->host + start + i * TARGET_PAGE_SIZE, | |
404 | TARGET_PAGE_SIZE); | |
405 | if (ret < 0) { | |
406 | return ret; | |
407 | } | |
408 | } | |
409 | ||
410 | if ((size % TARGET_PAGE_SIZE) != 0) { | |
411 | ret = write_data(s, block->host + start + i * TARGET_PAGE_SIZE, | |
412 | size % TARGET_PAGE_SIZE); | |
413 | if (ret < 0) { | |
414 | return ret; | |
415 | } | |
416 | } | |
417 | ||
418 | return 0; | |
419 | } | |
420 | ||
421 | /* get the memory's offset in the vmcore */ | |
a8170e5e | 422 | static hwaddr get_offset(hwaddr phys_addr, |
783e9b48 WC |
423 | DumpState *s) |
424 | { | |
425 | RAMBlock *block; | |
a8170e5e | 426 | hwaddr offset = s->memory_offset; |
783e9b48 WC |
427 | int64_t size_in_block, start; |
428 | ||
429 | if (s->has_filter) { | |
430 | if (phys_addr < s->begin || phys_addr >= s->begin + s->length) { | |
431 | return -1; | |
432 | } | |
433 | } | |
434 | ||
a3161038 | 435 | QTAILQ_FOREACH(block, &ram_list.blocks, next) { |
783e9b48 WC |
436 | if (s->has_filter) { |
437 | if (block->offset >= s->begin + s->length || | |
438 | block->offset + block->length <= s->begin) { | |
439 | /* This block is out of the range */ | |
440 | continue; | |
441 | } | |
442 | ||
443 | if (s->begin <= block->offset) { | |
444 | start = block->offset; | |
445 | } else { | |
446 | start = s->begin; | |
447 | } | |
448 | ||
449 | size_in_block = block->length - (start - block->offset); | |
450 | if (s->begin + s->length < block->offset + block->length) { | |
451 | size_in_block -= block->offset + block->length - | |
452 | (s->begin + s->length); | |
453 | } | |
454 | } else { | |
455 | start = block->offset; | |
456 | size_in_block = block->length; | |
457 | } | |
458 | ||
459 | if (phys_addr >= start && phys_addr < start + size_in_block) { | |
460 | return phys_addr - start + offset; | |
461 | } | |
462 | ||
463 | offset += size_in_block; | |
464 | } | |
465 | ||
466 | return -1; | |
467 | } | |
468 | ||
469 | static int write_elf_loads(DumpState *s) | |
470 | { | |
a8170e5e | 471 | hwaddr offset; |
783e9b48 WC |
472 | MemoryMapping *memory_mapping; |
473 | uint32_t phdr_index = 1; | |
474 | int ret; | |
475 | uint32_t max_index; | |
476 | ||
477 | if (s->have_section) { | |
478 | max_index = s->sh_info; | |
479 | } else { | |
480 | max_index = s->phdr_num; | |
481 | } | |
482 | ||
483 | QTAILQ_FOREACH(memory_mapping, &s->list.head, next) { | |
484 | offset = get_offset(memory_mapping->phys_addr, s); | |
485 | if (s->dump_info.d_class == ELFCLASS64) { | |
486 | ret = write_elf64_load(s, memory_mapping, phdr_index++, offset); | |
487 | } else { | |
488 | ret = write_elf32_load(s, memory_mapping, phdr_index++, offset); | |
489 | } | |
490 | ||
491 | if (ret < 0) { | |
492 | return -1; | |
493 | } | |
494 | ||
495 | if (phdr_index >= max_index) { | |
496 | break; | |
497 | } | |
498 | } | |
499 | ||
500 | return 0; | |
501 | } | |
502 | ||
503 | /* write elf header, PT_NOTE and elf note to vmcore. */ | |
504 | static int dump_begin(DumpState *s) | |
505 | { | |
506 | int ret; | |
507 | ||
508 | /* | |
509 | * the vmcore's format is: | |
510 | * -------------- | |
511 | * | elf header | | |
512 | * -------------- | |
513 | * | PT_NOTE | | |
514 | * -------------- | |
515 | * | PT_LOAD | | |
516 | * -------------- | |
517 | * | ...... | | |
518 | * -------------- | |
519 | * | PT_LOAD | | |
520 | * -------------- | |
521 | * | sec_hdr | | |
522 | * -------------- | |
523 | * | elf note | | |
524 | * -------------- | |
525 | * | memory | | |
526 | * -------------- | |
527 | * | |
528 | * we only know where the memory is saved after we write elf note into | |
529 | * vmcore. | |
530 | */ | |
531 | ||
532 | /* write elf header to vmcore */ | |
533 | if (s->dump_info.d_class == ELFCLASS64) { | |
534 | ret = write_elf64_header(s); | |
535 | } else { | |
536 | ret = write_elf32_header(s); | |
537 | } | |
538 | if (ret < 0) { | |
539 | return -1; | |
540 | } | |
541 | ||
542 | if (s->dump_info.d_class == ELFCLASS64) { | |
543 | /* write PT_NOTE to vmcore */ | |
544 | if (write_elf64_note(s) < 0) { | |
545 | return -1; | |
546 | } | |
547 | ||
548 | /* write all PT_LOAD to vmcore */ | |
549 | if (write_elf_loads(s) < 0) { | |
550 | return -1; | |
551 | } | |
552 | ||
553 | /* write section to vmcore */ | |
554 | if (s->have_section) { | |
555 | if (write_elf_section(s, 1) < 0) { | |
556 | return -1; | |
557 | } | |
558 | } | |
559 | ||
560 | /* write notes to vmcore */ | |
561 | if (write_elf64_notes(s) < 0) { | |
562 | return -1; | |
563 | } | |
564 | ||
565 | } else { | |
566 | /* write PT_NOTE to vmcore */ | |
567 | if (write_elf32_note(s) < 0) { | |
568 | return -1; | |
569 | } | |
570 | ||
571 | /* write all PT_LOAD to vmcore */ | |
572 | if (write_elf_loads(s) < 0) { | |
573 | return -1; | |
574 | } | |
575 | ||
576 | /* write section to vmcore */ | |
577 | if (s->have_section) { | |
578 | if (write_elf_section(s, 0) < 0) { | |
579 | return -1; | |
580 | } | |
581 | } | |
582 | ||
583 | /* write notes to vmcore */ | |
584 | if (write_elf32_notes(s) < 0) { | |
585 | return -1; | |
586 | } | |
587 | } | |
588 | ||
589 | return 0; | |
590 | } | |
591 | ||
592 | /* write PT_LOAD to vmcore */ | |
593 | static int dump_completed(DumpState *s) | |
594 | { | |
595 | dump_cleanup(s); | |
596 | return 0; | |
597 | } | |
598 | ||
599 | static int get_next_block(DumpState *s, RAMBlock *block) | |
600 | { | |
601 | while (1) { | |
a3161038 | 602 | block = QTAILQ_NEXT(block, next); |
783e9b48 WC |
603 | if (!block) { |
604 | /* no more block */ | |
605 | return 1; | |
606 | } | |
607 | ||
608 | s->start = 0; | |
609 | s->block = block; | |
610 | if (s->has_filter) { | |
611 | if (block->offset >= s->begin + s->length || | |
612 | block->offset + block->length <= s->begin) { | |
613 | /* This block is out of the range */ | |
614 | continue; | |
615 | } | |
616 | ||
617 | if (s->begin > block->offset) { | |
618 | s->start = s->begin - block->offset; | |
619 | } | |
620 | } | |
621 | ||
622 | return 0; | |
623 | } | |
624 | } | |
625 | ||
626 | /* write all memory to vmcore */ | |
627 | static int dump_iterate(DumpState *s) | |
628 | { | |
629 | RAMBlock *block; | |
630 | int64_t size; | |
631 | int ret; | |
632 | ||
633 | while (1) { | |
634 | block = s->block; | |
635 | ||
636 | size = block->length; | |
637 | if (s->has_filter) { | |
638 | size -= s->start; | |
639 | if (s->begin + s->length < block->offset + block->length) { | |
640 | size -= block->offset + block->length - (s->begin + s->length); | |
641 | } | |
642 | } | |
643 | ret = write_memory(s, block, s->start, size); | |
644 | if (ret == -1) { | |
645 | return ret; | |
646 | } | |
647 | ||
648 | ret = get_next_block(s, block); | |
649 | if (ret == 1) { | |
650 | dump_completed(s); | |
651 | return 0; | |
652 | } | |
653 | } | |
654 | } | |
655 | ||
656 | static int create_vmcore(DumpState *s) | |
657 | { | |
658 | int ret; | |
659 | ||
660 | ret = dump_begin(s); | |
661 | if (ret < 0) { | |
662 | return -1; | |
663 | } | |
664 | ||
665 | ret = dump_iterate(s); | |
666 | if (ret < 0) { | |
667 | return -1; | |
668 | } | |
669 | ||
670 | return 0; | |
671 | } | |
672 | ||
673 | static ram_addr_t get_start_block(DumpState *s) | |
674 | { | |
675 | RAMBlock *block; | |
676 | ||
677 | if (!s->has_filter) { | |
a3161038 | 678 | s->block = QTAILQ_FIRST(&ram_list.blocks); |
783e9b48 WC |
679 | return 0; |
680 | } | |
681 | ||
a3161038 | 682 | QTAILQ_FOREACH(block, &ram_list.blocks, next) { |
783e9b48 WC |
683 | if (block->offset >= s->begin + s->length || |
684 | block->offset + block->length <= s->begin) { | |
685 | /* This block is out of the range */ | |
686 | continue; | |
687 | } | |
688 | ||
689 | s->block = block; | |
690 | if (s->begin > block->offset) { | |
691 | s->start = s->begin - block->offset; | |
692 | } else { | |
693 | s->start = 0; | |
694 | } | |
695 | return s->start; | |
696 | } | |
697 | ||
698 | return -1; | |
699 | } | |
700 | ||
701 | static int dump_init(DumpState *s, int fd, bool paging, bool has_filter, | |
702 | int64_t begin, int64_t length, Error **errp) | |
703 | { | |
182735ef | 704 | CPUState *cpu; |
783e9b48 | 705 | int nr_cpus; |
11ed09cf | 706 | Error *err = NULL; |
783e9b48 WC |
707 | int ret; |
708 | ||
709 | if (runstate_is_running()) { | |
710 | vm_stop(RUN_STATE_SAVE_VM); | |
711 | s->resume = true; | |
712 | } else { | |
713 | s->resume = false; | |
714 | } | |
715 | ||
716 | s->errp = errp; | |
717 | s->fd = fd; | |
718 | s->has_filter = has_filter; | |
719 | s->begin = begin; | |
720 | s->length = length; | |
721 | s->start = get_start_block(s); | |
722 | if (s->start == -1) { | |
723 | error_set(errp, QERR_INVALID_PARAMETER, "begin"); | |
724 | goto cleanup; | |
725 | } | |
726 | ||
727 | /* | |
728 | * get dump info: endian, class and architecture. | |
729 | * If the target architecture is not supported, cpu_get_dump_info() will | |
730 | * return -1. | |
731 | * | |
1b3509ca | 732 | * If we use KVM, we should synchronize the registers before we get dump |
783e9b48 WC |
733 | * info. |
734 | */ | |
1b3509ca | 735 | cpu_synchronize_all_states(); |
783e9b48 | 736 | nr_cpus = 0; |
182735ef | 737 | for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) { |
783e9b48 WC |
738 | nr_cpus++; |
739 | } | |
740 | ||
741 | ret = cpu_get_dump_info(&s->dump_info); | |
742 | if (ret < 0) { | |
743 | error_set(errp, QERR_UNSUPPORTED); | |
744 | goto cleanup; | |
745 | } | |
746 | ||
4720bd05 PB |
747 | s->note_size = cpu_get_note_size(s->dump_info.d_class, |
748 | s->dump_info.d_machine, nr_cpus); | |
749 | if (ret < 0) { | |
750 | error_set(errp, QERR_UNSUPPORTED); | |
751 | goto cleanup; | |
752 | } | |
753 | ||
783e9b48 WC |
754 | /* get memory mapping */ |
755 | memory_mapping_list_init(&s->list); | |
756 | if (paging) { | |
11ed09cf AF |
757 | qemu_get_guest_memory_mapping(&s->list, &err); |
758 | if (err != NULL) { | |
759 | error_propagate(errp, err); | |
760 | goto cleanup; | |
761 | } | |
783e9b48 WC |
762 | } else { |
763 | qemu_get_guest_simple_memory_mapping(&s->list); | |
764 | } | |
765 | ||
766 | if (s->has_filter) { | |
767 | memory_mapping_filter(&s->list, s->begin, s->length); | |
768 | } | |
769 | ||
770 | /* | |
771 | * calculate phdr_num | |
772 | * | |
773 | * the type of ehdr->e_phnum is uint16_t, so we should avoid overflow | |
774 | */ | |
775 | s->phdr_num = 1; /* PT_NOTE */ | |
776 | if (s->list.num < UINT16_MAX - 2) { | |
777 | s->phdr_num += s->list.num; | |
778 | s->have_section = false; | |
779 | } else { | |
780 | s->have_section = true; | |
781 | s->phdr_num = PN_XNUM; | |
782 | s->sh_info = 1; /* PT_NOTE */ | |
783 | ||
784 | /* the type of shdr->sh_info is uint32_t, so we should avoid overflow */ | |
785 | if (s->list.num <= UINT32_MAX - 1) { | |
786 | s->sh_info += s->list.num; | |
787 | } else { | |
788 | s->sh_info = UINT32_MAX; | |
789 | } | |
790 | } | |
791 | ||
783e9b48 WC |
792 | if (s->dump_info.d_class == ELFCLASS64) { |
793 | if (s->have_section) { | |
794 | s->memory_offset = sizeof(Elf64_Ehdr) + | |
795 | sizeof(Elf64_Phdr) * s->sh_info + | |
796 | sizeof(Elf64_Shdr) + s->note_size; | |
797 | } else { | |
798 | s->memory_offset = sizeof(Elf64_Ehdr) + | |
799 | sizeof(Elf64_Phdr) * s->phdr_num + s->note_size; | |
800 | } | |
801 | } else { | |
802 | if (s->have_section) { | |
803 | s->memory_offset = sizeof(Elf32_Ehdr) + | |
804 | sizeof(Elf32_Phdr) * s->sh_info + | |
805 | sizeof(Elf32_Shdr) + s->note_size; | |
806 | } else { | |
807 | s->memory_offset = sizeof(Elf32_Ehdr) + | |
808 | sizeof(Elf32_Phdr) * s->phdr_num + s->note_size; | |
809 | } | |
810 | } | |
811 | ||
812 | return 0; | |
813 | ||
814 | cleanup: | |
815 | if (s->resume) { | |
816 | vm_start(); | |
817 | } | |
818 | ||
819 | return -1; | |
820 | } | |
821 | ||
822 | void qmp_dump_guest_memory(bool paging, const char *file, bool has_begin, | |
823 | int64_t begin, bool has_length, int64_t length, | |
824 | Error **errp) | |
825 | { | |
826 | const char *p; | |
827 | int fd = -1; | |
828 | DumpState *s; | |
829 | int ret; | |
830 | ||
831 | if (has_begin && !has_length) { | |
832 | error_set(errp, QERR_MISSING_PARAMETER, "length"); | |
833 | return; | |
834 | } | |
835 | if (!has_begin && has_length) { | |
836 | error_set(errp, QERR_MISSING_PARAMETER, "begin"); | |
837 | return; | |
838 | } | |
839 | ||
840 | #if !defined(WIN32) | |
841 | if (strstart(file, "fd:", &p)) { | |
a9940fc4 | 842 | fd = monitor_get_fd(cur_mon, p, errp); |
783e9b48 | 843 | if (fd == -1) { |
783e9b48 WC |
844 | return; |
845 | } | |
846 | } | |
847 | #endif | |
848 | ||
849 | if (strstart(file, "file:", &p)) { | |
850 | fd = qemu_open(p, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, S_IRUSR); | |
851 | if (fd < 0) { | |
7581766b | 852 | error_setg_file_open(errp, errno, p); |
783e9b48 WC |
853 | return; |
854 | } | |
855 | } | |
856 | ||
857 | if (fd == -1) { | |
858 | error_set(errp, QERR_INVALID_PARAMETER, "protocol"); | |
859 | return; | |
860 | } | |
861 | ||
862 | s = g_malloc(sizeof(DumpState)); | |
863 | ||
864 | ret = dump_init(s, fd, paging, has_begin, begin, length, errp); | |
865 | if (ret < 0) { | |
866 | g_free(s); | |
867 | return; | |
868 | } | |
869 | ||
870 | if (create_vmcore(s) < 0 && !error_is_set(s->errp)) { | |
871 | error_set(errp, QERR_IO_ERROR); | |
872 | } | |
873 | ||
874 | g_free(s); | |
875 | } |