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