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