]>
Commit | Line | Data |
---|---|---|
783e9b48 WC |
1 | /* |
2 | * QEMU dump | |
3 | * | |
4 | * Copyright Fujitsu, Corp. 2011, 2012 | |
5 | * | |
6 | * Authors: | |
7 | * Wen Congyang <wency@cn.fujitsu.com> | |
8 | * | |
352666e2 SW |
9 | * This work is licensed under the terms of the GNU GPL, version 2 or later. |
10 | * See the COPYING file in the top-level directory. | |
783e9b48 WC |
11 | * |
12 | */ | |
13 | ||
d38ea87a | 14 | #include "qemu/osdep.h" |
f348b6d1 | 15 | #include "qemu/cutils.h" |
783e9b48 | 16 | #include "elf.h" |
022c62cb | 17 | #include "exec/hwaddr.h" |
83c9089e | 18 | #include "monitor/monitor.h" |
9c17d615 PB |
19 | #include "sysemu/kvm.h" |
20 | #include "sysemu/dump.h" | |
9c17d615 | 21 | #include "sysemu/memory_mapping.h" |
54d31236 | 22 | #include "sysemu/runstate.h" |
1b3509ca | 23 | #include "sysemu/cpus.h" |
e688df6b | 24 | #include "qapi/error.h" |
d06b747b MA |
25 | #include "qapi/qapi-commands-dump.h" |
26 | #include "qapi/qapi-events-dump.h" | |
cc7a8ea7 | 27 | #include "qapi/qmp/qerror.h" |
903ef734 | 28 | #include "qemu/error-report.h" |
db725815 | 29 | #include "qemu/main-loop.h" |
903ef734 | 30 | #include "hw/misc/vmcoreinfo.h" |
b7bc6b18 | 31 | #include "migration/blocker.h" |
783e9b48 | 32 | |
2da91b54 VP |
33 | #ifdef TARGET_X86_64 |
34 | #include "win_dump.h" | |
35 | #endif | |
36 | ||
d12f57ec QN |
37 | #include <zlib.h> |
38 | #ifdef CONFIG_LZO | |
39 | #include <lzo/lzo1x.h> | |
40 | #endif | |
41 | #ifdef CONFIG_SNAPPY | |
42 | #include <snappy-c.h> | |
43 | #endif | |
4ab23a91 QN |
44 | #ifndef ELF_MACHINE_UNAME |
45 | #define ELF_MACHINE_UNAME "Unknown" | |
46 | #endif | |
d12f57ec | 47 | |
903ef734 MAL |
48 | #define MAX_GUEST_NOTE_SIZE (1 << 20) /* 1MB should be enough */ |
49 | ||
b7bc6b18 PX |
50 | static Error *dump_migration_blocker; |
51 | ||
903ef734 MAL |
52 | #define ELF_NOTE_SIZE(hdr_size, name_size, desc_size) \ |
53 | ((DIV_ROUND_UP((hdr_size), 4) + \ | |
54 | DIV_ROUND_UP((name_size), 4) + \ | |
55 | DIV_ROUND_UP((desc_size), 4)) * 4) | |
56 | ||
05bbaa50 JF |
57 | static inline bool dump_is_64bit(DumpState *s) |
58 | { | |
59 | return s->dump_info.d_class == ELFCLASS64; | |
60 | } | |
61 | ||
dddf725f JF |
62 | static inline bool dump_has_filter(DumpState *s) |
63 | { | |
64 | return s->filter_area_length > 0; | |
65 | } | |
66 | ||
acb0ef58 | 67 | uint16_t cpu_to_dump16(DumpState *s, uint16_t val) |
783e9b48 | 68 | { |
acb0ef58 | 69 | if (s->dump_info.d_endian == ELFDATA2LSB) { |
783e9b48 WC |
70 | val = cpu_to_le16(val); |
71 | } else { | |
72 | val = cpu_to_be16(val); | |
73 | } | |
74 | ||
75 | return val; | |
76 | } | |
77 | ||
acb0ef58 | 78 | uint32_t cpu_to_dump32(DumpState *s, uint32_t val) |
783e9b48 | 79 | { |
acb0ef58 | 80 | if (s->dump_info.d_endian == ELFDATA2LSB) { |
783e9b48 WC |
81 | val = cpu_to_le32(val); |
82 | } else { | |
83 | val = cpu_to_be32(val); | |
84 | } | |
85 | ||
86 | return val; | |
87 | } | |
88 | ||
acb0ef58 | 89 | uint64_t cpu_to_dump64(DumpState *s, uint64_t val) |
783e9b48 | 90 | { |
acb0ef58 | 91 | if (s->dump_info.d_endian == ELFDATA2LSB) { |
783e9b48 WC |
92 | val = cpu_to_le64(val); |
93 | } else { | |
94 | val = cpu_to_be64(val); | |
95 | } | |
96 | ||
97 | return val; | |
98 | } | |
99 | ||
783e9b48 WC |
100 | static int dump_cleanup(DumpState *s) |
101 | { | |
5ee163e8 | 102 | guest_phys_blocks_free(&s->guest_phys_blocks); |
783e9b48 | 103 | memory_mapping_list_free(&s->list); |
2928207a | 104 | close(s->fd); |
903ef734 | 105 | g_free(s->guest_note); |
9b72224f | 106 | g_array_unref(s->string_table_buf); |
903ef734 | 107 | s->guest_note = NULL; |
783e9b48 | 108 | if (s->resume) { |
6796b400 FZ |
109 | if (s->detached) { |
110 | qemu_mutex_lock_iothread(); | |
111 | } | |
783e9b48 | 112 | vm_start(); |
6796b400 FZ |
113 | if (s->detached) { |
114 | qemu_mutex_unlock_iothread(); | |
115 | } | |
783e9b48 | 116 | } |
b7bc6b18 | 117 | migrate_del_blocker(dump_migration_blocker); |
783e9b48 | 118 | |
2928207a | 119 | return 0; |
783e9b48 WC |
120 | } |
121 | ||
b5ba1cc6 | 122 | static int fd_write_vmcore(const void *buf, size_t size, void *opaque) |
783e9b48 WC |
123 | { |
124 | DumpState *s = opaque; | |
2f61652d LC |
125 | size_t written_size; |
126 | ||
127 | written_size = qemu_write_full(s->fd, buf, size); | |
128 | if (written_size != size) { | |
0c33659d | 129 | return -errno; |
783e9b48 WC |
130 | } |
131 | ||
132 | return 0; | |
133 | } | |
134 | ||
670e7699 | 135 | static void prepare_elf64_header(DumpState *s, Elf64_Ehdr *elf_header) |
783e9b48 | 136 | { |
046bc416 JF |
137 | /* |
138 | * phnum in the elf header is 16 bit, if we have more segments we | |
139 | * set phnum to PN_XNUM and write the real number of segments to a | |
140 | * special section. | |
141 | */ | |
142 | uint16_t phnum = MIN(s->phdr_num, PN_XNUM); | |
783e9b48 | 143 | |
670e7699 JF |
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_to_dump16(s, ET_CORE); | |
150 | elf_header->e_machine = cpu_to_dump16(s, s->dump_info.d_machine); | |
151 | elf_header->e_version = cpu_to_dump32(s, EV_CURRENT); | |
152 | elf_header->e_ehsize = cpu_to_dump16(s, sizeof(elf_header)); | |
153 | elf_header->e_phoff = cpu_to_dump64(s, s->phdr_offset); | |
154 | elf_header->e_phentsize = cpu_to_dump16(s, sizeof(Elf64_Phdr)); | |
155 | elf_header->e_phnum = cpu_to_dump16(s, phnum); | |
9b72224f JF |
156 | elf_header->e_shoff = cpu_to_dump64(s, s->shdr_offset); |
157 | elf_header->e_shentsize = cpu_to_dump16(s, sizeof(Elf64_Shdr)); | |
158 | elf_header->e_shnum = cpu_to_dump16(s, s->shdr_num); | |
159 | elf_header->e_shstrndx = cpu_to_dump16(s, s->shdr_num - 1); | |
783e9b48 WC |
160 | } |
161 | ||
670e7699 | 162 | static void prepare_elf32_header(DumpState *s, Elf32_Ehdr *elf_header) |
783e9b48 | 163 | { |
046bc416 JF |
164 | /* |
165 | * phnum in the elf header is 16 bit, if we have more segments we | |
166 | * set phnum to PN_XNUM and write the real number of segments to a | |
167 | * special section. | |
168 | */ | |
169 | uint16_t phnum = MIN(s->phdr_num, PN_XNUM); | |
783e9b48 | 170 | |
670e7699 JF |
171 | memset(elf_header, 0, sizeof(Elf32_Ehdr)); |
172 | memcpy(elf_header, ELFMAG, SELFMAG); | |
173 | elf_header->e_ident[EI_CLASS] = ELFCLASS32; | |
174 | elf_header->e_ident[EI_DATA] = s->dump_info.d_endian; | |
175 | elf_header->e_ident[EI_VERSION] = EV_CURRENT; | |
176 | elf_header->e_type = cpu_to_dump16(s, ET_CORE); | |
177 | elf_header->e_machine = cpu_to_dump16(s, s->dump_info.d_machine); | |
178 | elf_header->e_version = cpu_to_dump32(s, EV_CURRENT); | |
179 | elf_header->e_ehsize = cpu_to_dump16(s, sizeof(elf_header)); | |
180 | elf_header->e_phoff = cpu_to_dump32(s, s->phdr_offset); | |
181 | elf_header->e_phentsize = cpu_to_dump16(s, sizeof(Elf32_Phdr)); | |
182 | elf_header->e_phnum = cpu_to_dump16(s, phnum); | |
9b72224f JF |
183 | elf_header->e_shoff = cpu_to_dump32(s, s->shdr_offset); |
184 | elf_header->e_shentsize = cpu_to_dump16(s, sizeof(Elf32_Shdr)); | |
185 | elf_header->e_shnum = cpu_to_dump16(s, s->shdr_num); | |
186 | elf_header->e_shstrndx = cpu_to_dump16(s, s->shdr_num - 1); | |
670e7699 | 187 | } |
783e9b48 | 188 | |
670e7699 JF |
189 | static void write_elf_header(DumpState *s, Error **errp) |
190 | { | |
191 | Elf32_Ehdr elf32_header; | |
192 | Elf64_Ehdr elf64_header; | |
193 | size_t header_size; | |
194 | void *header_ptr; | |
195 | int ret; | |
196 | ||
9b72224f JF |
197 | /* The NULL header and the shstrtab are always defined */ |
198 | assert(s->shdr_num >= 2); | |
670e7699 JF |
199 | if (dump_is_64bit(s)) { |
200 | prepare_elf64_header(s, &elf64_header); | |
201 | header_size = sizeof(elf64_header); | |
202 | header_ptr = &elf64_header; | |
203 | } else { | |
204 | prepare_elf32_header(s, &elf32_header); | |
205 | header_size = sizeof(elf32_header); | |
206 | header_ptr = &elf32_header; | |
207 | } | |
208 | ||
209 | ret = fd_write_vmcore(header_ptr, header_size, s); | |
783e9b48 | 210 | if (ret < 0) { |
0c33659d | 211 | error_setg_errno(errp, -ret, "dump: failed to write elf header"); |
783e9b48 | 212 | } |
783e9b48 WC |
213 | } |
214 | ||
4c7e251a HZ |
215 | static void write_elf64_load(DumpState *s, MemoryMapping *memory_mapping, |
216 | int phdr_index, hwaddr offset, | |
217 | hwaddr filesz, Error **errp) | |
783e9b48 WC |
218 | { |
219 | Elf64_Phdr phdr; | |
220 | int ret; | |
783e9b48 WC |
221 | |
222 | memset(&phdr, 0, sizeof(Elf64_Phdr)); | |
acb0ef58 BR |
223 | phdr.p_type = cpu_to_dump32(s, PT_LOAD); |
224 | phdr.p_offset = cpu_to_dump64(s, offset); | |
225 | phdr.p_paddr = cpu_to_dump64(s, memory_mapping->phys_addr); | |
226 | phdr.p_filesz = cpu_to_dump64(s, filesz); | |
227 | phdr.p_memsz = cpu_to_dump64(s, memory_mapping->length); | |
e17bebd0 | 228 | phdr.p_vaddr = cpu_to_dump64(s, memory_mapping->virt_addr) ?: phdr.p_paddr; |
783e9b48 | 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) { | |
0c33659d YB |
234 | error_setg_errno(errp, -ret, |
235 | "dump: failed to write program header table"); | |
783e9b48 | 236 | } |
783e9b48 WC |
237 | } |
238 | ||
4c7e251a HZ |
239 | static void write_elf32_load(DumpState *s, MemoryMapping *memory_mapping, |
240 | int phdr_index, hwaddr offset, | |
241 | hwaddr filesz, Error **errp) | |
783e9b48 WC |
242 | { |
243 | Elf32_Phdr phdr; | |
244 | int ret; | |
783e9b48 WC |
245 | |
246 | memset(&phdr, 0, sizeof(Elf32_Phdr)); | |
acb0ef58 BR |
247 | phdr.p_type = cpu_to_dump32(s, PT_LOAD); |
248 | phdr.p_offset = cpu_to_dump32(s, offset); | |
249 | phdr.p_paddr = cpu_to_dump32(s, memory_mapping->phys_addr); | |
250 | phdr.p_filesz = cpu_to_dump32(s, filesz); | |
251 | phdr.p_memsz = cpu_to_dump32(s, memory_mapping->length); | |
e17bebd0 JD |
252 | phdr.p_vaddr = |
253 | cpu_to_dump32(s, memory_mapping->virt_addr) ?: phdr.p_paddr; | |
783e9b48 | 254 | |
2cac2607 LE |
255 | assert(memory_mapping->length >= filesz); |
256 | ||
783e9b48 WC |
257 | ret = fd_write_vmcore(&phdr, sizeof(Elf32_Phdr), s); |
258 | if (ret < 0) { | |
0c33659d YB |
259 | error_setg_errno(errp, -ret, |
260 | "dump: failed to write program header table"); | |
783e9b48 | 261 | } |
783e9b48 WC |
262 | } |
263 | ||
2341a94d | 264 | static void prepare_elf64_phdr_note(DumpState *s, Elf64_Phdr *phdr) |
783e9b48 | 265 | { |
bc7d5580 JF |
266 | memset(phdr, 0, sizeof(*phdr)); |
267 | phdr->p_type = cpu_to_dump32(s, PT_NOTE); | |
268 | phdr->p_offset = cpu_to_dump64(s, s->note_offset); | |
269 | phdr->p_paddr = 0; | |
270 | phdr->p_filesz = cpu_to_dump64(s, s->note_size); | |
271 | phdr->p_memsz = cpu_to_dump64(s, s->note_size); | |
272 | phdr->p_vaddr = 0; | |
783e9b48 WC |
273 | } |
274 | ||
0bc3cd62 PB |
275 | static inline int cpu_index(CPUState *cpu) |
276 | { | |
277 | return cpu->cpu_index + 1; | |
278 | } | |
279 | ||
903ef734 MAL |
280 | static void write_guest_note(WriteCoreDumpFunction f, DumpState *s, |
281 | Error **errp) | |
282 | { | |
283 | int ret; | |
284 | ||
285 | if (s->guest_note) { | |
286 | ret = f(s->guest_note, s->guest_note_size, s); | |
287 | if (ret < 0) { | |
288 | error_setg(errp, "dump: failed to write guest note"); | |
289 | } | |
290 | } | |
291 | } | |
292 | ||
4c7e251a HZ |
293 | static void write_elf64_notes(WriteCoreDumpFunction f, DumpState *s, |
294 | Error **errp) | |
783e9b48 | 295 | { |
0d34282f | 296 | CPUState *cpu; |
783e9b48 WC |
297 | int ret; |
298 | int id; | |
299 | ||
bdc44640 | 300 | CPU_FOREACH(cpu) { |
0d34282f | 301 | id = cpu_index(cpu); |
6a519918 | 302 | ret = cpu_write_elf64_note(f, cpu, id, s); |
783e9b48 | 303 | if (ret < 0) { |
e3517a52 | 304 | error_setg(errp, "dump: failed to write elf notes"); |
4c7e251a | 305 | return; |
783e9b48 WC |
306 | } |
307 | } | |
308 | ||
bdc44640 | 309 | CPU_FOREACH(cpu) { |
6a519918 | 310 | ret = cpu_write_elf64_qemunote(f, cpu, s); |
783e9b48 | 311 | if (ret < 0) { |
e3517a52 | 312 | error_setg(errp, "dump: failed to write CPU status"); |
4c7e251a | 313 | return; |
783e9b48 WC |
314 | } |
315 | } | |
903ef734 MAL |
316 | |
317 | write_guest_note(f, s, errp); | |
783e9b48 WC |
318 | } |
319 | ||
2341a94d | 320 | static void prepare_elf32_phdr_note(DumpState *s, Elf32_Phdr *phdr) |
783e9b48 | 321 | { |
bc7d5580 JF |
322 | memset(phdr, 0, sizeof(*phdr)); |
323 | phdr->p_type = cpu_to_dump32(s, PT_NOTE); | |
324 | phdr->p_offset = cpu_to_dump32(s, s->note_offset); | |
325 | phdr->p_paddr = 0; | |
326 | phdr->p_filesz = cpu_to_dump32(s, s->note_size); | |
327 | phdr->p_memsz = cpu_to_dump32(s, s->note_size); | |
328 | phdr->p_vaddr = 0; | |
783e9b48 WC |
329 | } |
330 | ||
4c7e251a HZ |
331 | static void write_elf32_notes(WriteCoreDumpFunction f, DumpState *s, |
332 | Error **errp) | |
783e9b48 | 333 | { |
0d34282f | 334 | CPUState *cpu; |
783e9b48 WC |
335 | int ret; |
336 | int id; | |
337 | ||
bdc44640 | 338 | CPU_FOREACH(cpu) { |
0d34282f | 339 | id = cpu_index(cpu); |
6a519918 | 340 | ret = cpu_write_elf32_note(f, cpu, id, s); |
783e9b48 | 341 | if (ret < 0) { |
e3517a52 | 342 | error_setg(errp, "dump: failed to write elf notes"); |
4c7e251a | 343 | return; |
783e9b48 WC |
344 | } |
345 | } | |
346 | ||
bdc44640 | 347 | CPU_FOREACH(cpu) { |
6a519918 | 348 | ret = cpu_write_elf32_qemunote(f, cpu, s); |
783e9b48 | 349 | if (ret < 0) { |
e3517a52 | 350 | error_setg(errp, "dump: failed to write CPU status"); |
4c7e251a | 351 | return; |
783e9b48 WC |
352 | } |
353 | } | |
903ef734 MAL |
354 | |
355 | write_guest_note(f, s, errp); | |
783e9b48 WC |
356 | } |
357 | ||
bc7d5580 JF |
358 | static void write_elf_phdr_note(DumpState *s, Error **errp) |
359 | { | |
360 | ERRP_GUARD(); | |
361 | Elf32_Phdr phdr32; | |
362 | Elf64_Phdr phdr64; | |
363 | void *phdr; | |
364 | size_t size; | |
365 | int ret; | |
366 | ||
367 | if (dump_is_64bit(s)) { | |
2341a94d | 368 | prepare_elf64_phdr_note(s, &phdr64); |
bc7d5580 JF |
369 | size = sizeof(phdr64); |
370 | phdr = &phdr64; | |
371 | } else { | |
2341a94d | 372 | prepare_elf32_phdr_note(s, &phdr32); |
bc7d5580 JF |
373 | size = sizeof(phdr32); |
374 | phdr = &phdr32; | |
375 | } | |
376 | ||
377 | ret = fd_write_vmcore(phdr, size, s); | |
378 | if (ret < 0) { | |
379 | error_setg_errno(errp, -ret, | |
380 | "dump: failed to write program header table"); | |
381 | } | |
382 | } | |
383 | ||
e41ed29b | 384 | static void prepare_elf_section_hdr_zero(DumpState *s) |
783e9b48 | 385 | { |
e41ed29b JF |
386 | if (dump_is_64bit(s)) { |
387 | Elf64_Shdr *shdr64 = s->elf_section_hdrs; | |
783e9b48 | 388 | |
e41ed29b | 389 | shdr64->sh_info = cpu_to_dump32(s, s->phdr_num); |
783e9b48 | 390 | } else { |
e41ed29b JF |
391 | Elf32_Shdr *shdr32 = s->elf_section_hdrs; |
392 | ||
393 | shdr32->sh_info = cpu_to_dump32(s, s->phdr_num); | |
394 | } | |
395 | } | |
396 | ||
9b72224f JF |
397 | static void prepare_elf_section_hdr_string(DumpState *s, void *buff) |
398 | { | |
399 | uint64_t index = s->string_table_buf->len; | |
400 | const char strtab[] = ".shstrtab"; | |
401 | Elf32_Shdr shdr32 = {}; | |
402 | Elf64_Shdr shdr64 = {}; | |
403 | int shdr_size; | |
404 | void *shdr; | |
405 | ||
406 | g_array_append_vals(s->string_table_buf, strtab, sizeof(strtab)); | |
407 | if (dump_is_64bit(s)) { | |
408 | shdr_size = sizeof(Elf64_Shdr); | |
409 | shdr64.sh_type = SHT_STRTAB; | |
410 | shdr64.sh_offset = s->section_offset + s->elf_section_data_size; | |
411 | shdr64.sh_name = index; | |
412 | shdr64.sh_size = s->string_table_buf->len; | |
413 | shdr = &shdr64; | |
414 | } else { | |
415 | shdr_size = sizeof(Elf32_Shdr); | |
416 | shdr32.sh_type = SHT_STRTAB; | |
417 | shdr32.sh_offset = s->section_offset + s->elf_section_data_size; | |
418 | shdr32.sh_name = index; | |
419 | shdr32.sh_size = s->string_table_buf->len; | |
420 | shdr = &shdr32; | |
421 | } | |
422 | memcpy(buff, shdr, shdr_size); | |
423 | } | |
424 | ||
425 | static bool prepare_elf_section_hdrs(DumpState *s, Error **errp) | |
e41ed29b JF |
426 | { |
427 | size_t len, sizeof_shdr; | |
9b72224f | 428 | void *buff_hdr; |
e41ed29b JF |
429 | |
430 | /* | |
431 | * Section ordering: | |
432 | * - HDR zero | |
9b72224f JF |
433 | * - Arch section hdrs |
434 | * - String table hdr | |
e41ed29b JF |
435 | */ |
436 | sizeof_shdr = dump_is_64bit(s) ? sizeof(Elf64_Shdr) : sizeof(Elf32_Shdr); | |
437 | len = sizeof_shdr * s->shdr_num; | |
438 | s->elf_section_hdrs = g_malloc0(len); | |
9b72224f | 439 | buff_hdr = s->elf_section_hdrs; |
e41ed29b JF |
440 | |
441 | /* | |
442 | * The first section header is ALWAYS a special initial section | |
443 | * header. | |
444 | * | |
445 | * The header should be 0 with one exception being that if | |
446 | * phdr_num is PN_XNUM then the sh_info field contains the real | |
447 | * number of segment entries. | |
448 | * | |
449 | * As we zero allocate the buffer we will only need to modify | |
450 | * sh_info for the PN_XNUM case. | |
451 | */ | |
452 | if (s->phdr_num >= PN_XNUM) { | |
453 | prepare_elf_section_hdr_zero(s); | |
783e9b48 | 454 | } |
9b72224f JF |
455 | buff_hdr += sizeof_shdr; |
456 | ||
457 | /* Add architecture defined section headers */ | |
458 | if (s->dump_info.arch_sections_write_hdr_fn | |
459 | && s->shdr_num > 2) { | |
460 | buff_hdr += s->dump_info.arch_sections_write_hdr_fn(s, buff_hdr); | |
461 | ||
462 | if (s->shdr_num >= SHN_LORESERVE) { | |
463 | error_setg_errno(errp, EINVAL, | |
464 | "dump: too many architecture defined sections"); | |
465 | return false; | |
466 | } | |
467 | } | |
468 | ||
469 | /* | |
470 | * String table is the last section since strings are added via | |
471 | * arch_sections_write_hdr(). | |
472 | */ | |
473 | prepare_elf_section_hdr_string(s, buff_hdr); | |
474 | return true; | |
e41ed29b | 475 | } |
783e9b48 | 476 | |
e41ed29b JF |
477 | static void write_elf_section_headers(DumpState *s, Error **errp) |
478 | { | |
479 | size_t sizeof_shdr = dump_is_64bit(s) ? sizeof(Elf64_Shdr) : sizeof(Elf32_Shdr); | |
480 | int ret; | |
481 | ||
9b72224f JF |
482 | if (!prepare_elf_section_hdrs(s, errp)) { |
483 | return; | |
484 | } | |
e41ed29b JF |
485 | |
486 | ret = fd_write_vmcore(s->elf_section_hdrs, s->shdr_num * sizeof_shdr, s); | |
783e9b48 | 487 | if (ret < 0) { |
e41ed29b | 488 | error_setg_errno(errp, -ret, "dump: failed to write section headers"); |
783e9b48 | 489 | } |
e41ed29b JF |
490 | |
491 | g_free(s->elf_section_hdrs); | |
783e9b48 WC |
492 | } |
493 | ||
9b72224f JF |
494 | static void write_elf_sections(DumpState *s, Error **errp) |
495 | { | |
496 | int ret; | |
497 | ||
498 | if (s->elf_section_data_size) { | |
499 | /* Write architecture section data */ | |
500 | ret = fd_write_vmcore(s->elf_section_data, | |
501 | s->elf_section_data_size, s); | |
502 | if (ret < 0) { | |
503 | error_setg_errno(errp, -ret, | |
504 | "dump: failed to write architecture section data"); | |
505 | return; | |
506 | } | |
507 | } | |
508 | ||
509 | /* Write string table */ | |
510 | ret = fd_write_vmcore(s->string_table_buf->data, | |
511 | s->string_table_buf->len, s); | |
512 | if (ret < 0) { | |
513 | error_setg_errno(errp, -ret, "dump: failed to write string table data"); | |
514 | } | |
515 | } | |
516 | ||
4c7e251a | 517 | static void write_data(DumpState *s, void *buf, int length, Error **errp) |
783e9b48 WC |
518 | { |
519 | int ret; | |
520 | ||
521 | ret = fd_write_vmcore(buf, length, s); | |
522 | if (ret < 0) { | |
0c33659d | 523 | error_setg_errno(errp, -ret, "dump: failed to save memory"); |
2264c2c9 PX |
524 | } else { |
525 | s->written_size += length; | |
783e9b48 | 526 | } |
783e9b48 WC |
527 | } |
528 | ||
4c7e251a HZ |
529 | /* write the memory to vmcore. 1 page per I/O. */ |
530 | static void write_memory(DumpState *s, GuestPhysBlock *block, ram_addr_t start, | |
531 | int64_t size, Error **errp) | |
783e9b48 | 532 | { |
86a518bb | 533 | ERRP_GUARD(); |
783e9b48 | 534 | int64_t i; |
783e9b48 | 535 | |
8161befd AJ |
536 | for (i = 0; i < size / s->dump_info.page_size; i++) { |
537 | write_data(s, block->host_addr + start + i * s->dump_info.page_size, | |
86a518bb JF |
538 | s->dump_info.page_size, errp); |
539 | if (*errp) { | |
4c7e251a | 540 | return; |
783e9b48 WC |
541 | } |
542 | } | |
543 | ||
8161befd AJ |
544 | if ((size % s->dump_info.page_size) != 0) { |
545 | write_data(s, block->host_addr + start + i * s->dump_info.page_size, | |
86a518bb JF |
546 | size % s->dump_info.page_size, errp); |
547 | if (*errp) { | |
4c7e251a | 548 | return; |
783e9b48 WC |
549 | } |
550 | } | |
783e9b48 WC |
551 | } |
552 | ||
2cac2607 LE |
553 | /* get the memory's offset and size in the vmcore */ |
554 | static void get_offset_range(hwaddr phys_addr, | |
555 | ram_addr_t mapping_length, | |
556 | DumpState *s, | |
557 | hwaddr *p_offset, | |
558 | hwaddr *p_filesz) | |
783e9b48 | 559 | { |
56c4bfb3 | 560 | GuestPhysBlock *block; |
a8170e5e | 561 | hwaddr offset = s->memory_offset; |
783e9b48 WC |
562 | int64_t size_in_block, start; |
563 | ||
2cac2607 LE |
564 | /* When the memory is not stored into vmcore, offset will be -1 */ |
565 | *p_offset = -1; | |
566 | *p_filesz = 0; | |
567 | ||
dddf725f JF |
568 | if (dump_has_filter(s)) { |
569 | if (phys_addr < s->filter_area_begin || | |
570 | phys_addr >= s->filter_area_begin + s->filter_area_length) { | |
2cac2607 | 571 | return; |
783e9b48 WC |
572 | } |
573 | } | |
574 | ||
56c4bfb3 | 575 | QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) { |
dddf725f JF |
576 | if (dump_has_filter(s)) { |
577 | if (block->target_start >= s->filter_area_begin + s->filter_area_length || | |
578 | block->target_end <= s->filter_area_begin) { | |
783e9b48 WC |
579 | /* This block is out of the range */ |
580 | continue; | |
581 | } | |
582 | ||
dddf725f | 583 | if (s->filter_area_begin <= block->target_start) { |
56c4bfb3 | 584 | start = block->target_start; |
783e9b48 | 585 | } else { |
dddf725f | 586 | start = s->filter_area_begin; |
783e9b48 WC |
587 | } |
588 | ||
56c4bfb3 | 589 | size_in_block = block->target_end - start; |
dddf725f JF |
590 | if (s->filter_area_begin + s->filter_area_length < block->target_end) { |
591 | size_in_block -= block->target_end - (s->filter_area_begin + s->filter_area_length); | |
783e9b48 WC |
592 | } |
593 | } else { | |
56c4bfb3 LE |
594 | start = block->target_start; |
595 | size_in_block = block->target_end - block->target_start; | |
783e9b48 WC |
596 | } |
597 | ||
598 | if (phys_addr >= start && phys_addr < start + size_in_block) { | |
2cac2607 LE |
599 | *p_offset = phys_addr - start + offset; |
600 | ||
601 | /* The offset range mapped from the vmcore file must not spill over | |
56c4bfb3 | 602 | * the GuestPhysBlock, clamp it. The rest of the mapping will be |
2cac2607 LE |
603 | * zero-filled in memory at load time; see |
604 | * <http://refspecs.linuxbase.org/elf/gabi4+/ch5.pheader.html>. | |
605 | */ | |
606 | *p_filesz = phys_addr + mapping_length <= start + size_in_block ? | |
607 | mapping_length : | |
608 | size_in_block - (phys_addr - start); | |
609 | return; | |
783e9b48 WC |
610 | } |
611 | ||
612 | offset += size_in_block; | |
613 | } | |
783e9b48 WC |
614 | } |
615 | ||
afae6056 | 616 | static void write_elf_phdr_loads(DumpState *s, Error **errp) |
783e9b48 | 617 | { |
86a518bb | 618 | ERRP_GUARD(); |
2cac2607 | 619 | hwaddr offset, filesz; |
783e9b48 WC |
620 | MemoryMapping *memory_mapping; |
621 | uint32_t phdr_index = 1; | |
783e9b48 WC |
622 | |
623 | QTAILQ_FOREACH(memory_mapping, &s->list.head, next) { | |
2cac2607 LE |
624 | get_offset_range(memory_mapping->phys_addr, |
625 | memory_mapping->length, | |
626 | s, &offset, &filesz); | |
05bbaa50 | 627 | if (dump_is_64bit(s)) { |
4c7e251a | 628 | write_elf64_load(s, memory_mapping, phdr_index++, offset, |
86a518bb | 629 | filesz, errp); |
783e9b48 | 630 | } else { |
4c7e251a | 631 | write_elf32_load(s, memory_mapping, phdr_index++, offset, |
86a518bb | 632 | filesz, errp); |
783e9b48 WC |
633 | } |
634 | ||
86a518bb | 635 | if (*errp) { |
4c7e251a | 636 | return; |
783e9b48 WC |
637 | } |
638 | ||
046bc416 | 639 | if (phdr_index >= s->phdr_num) { |
783e9b48 WC |
640 | break; |
641 | } | |
642 | } | |
783e9b48 WC |
643 | } |
644 | ||
c6812473 JF |
645 | static void write_elf_notes(DumpState *s, Error **errp) |
646 | { | |
647 | if (dump_is_64bit(s)) { | |
648 | write_elf64_notes(fd_write_vmcore, s, errp); | |
649 | } else { | |
650 | write_elf32_notes(fd_write_vmcore, s, errp); | |
651 | } | |
652 | } | |
653 | ||
783e9b48 | 654 | /* write elf header, PT_NOTE and elf note to vmcore. */ |
4c7e251a | 655 | static void dump_begin(DumpState *s, Error **errp) |
783e9b48 | 656 | { |
86a518bb | 657 | ERRP_GUARD(); |
783e9b48 WC |
658 | |
659 | /* | |
660 | * the vmcore's format is: | |
661 | * -------------- | |
662 | * | elf header | | |
663 | * -------------- | |
cb415fd6 JF |
664 | * | sctn_hdr | |
665 | * -------------- | |
783e9b48 WC |
666 | * | PT_NOTE | |
667 | * -------------- | |
668 | * | PT_LOAD | | |
669 | * -------------- | |
670 | * | ...... | | |
671 | * -------------- | |
672 | * | PT_LOAD | | |
673 | * -------------- | |
783e9b48 WC |
674 | * | elf note | |
675 | * -------------- | |
676 | * | memory | | |
677 | * -------------- | |
678 | * | |
679 | * we only know where the memory is saved after we write elf note into | |
680 | * vmcore. | |
681 | */ | |
682 | ||
683 | /* write elf header to vmcore */ | |
670e7699 | 684 | write_elf_header(s, errp); |
86a518bb | 685 | if (*errp) { |
4c7e251a | 686 | return; |
783e9b48 WC |
687 | } |
688 | ||
cb415fd6 JF |
689 | /* write section headers to vmcore */ |
690 | write_elf_section_headers(s, errp); | |
bc7d5580 JF |
691 | if (*errp) { |
692 | return; | |
693 | } | |
783e9b48 | 694 | |
cb415fd6 JF |
695 | /* write PT_NOTE to vmcore */ |
696 | write_elf_phdr_note(s, errp); | |
5ff2e5a3 JF |
697 | if (*errp) { |
698 | return; | |
699 | } | |
700 | ||
cb415fd6 JF |
701 | /* write all PT_LOADs to vmcore */ |
702 | write_elf_phdr_loads(s, errp); | |
e41ed29b JF |
703 | if (*errp) { |
704 | return; | |
5ff2e5a3 | 705 | } |
783e9b48 | 706 | |
c6812473 JF |
707 | /* write notes to vmcore */ |
708 | write_elf_notes(s, errp); | |
783e9b48 WC |
709 | } |
710 | ||
113d8f4e JF |
711 | int64_t dump_filtered_memblock_size(GuestPhysBlock *block, |
712 | int64_t filter_area_start, | |
713 | int64_t filter_area_length) | |
783e9b48 | 714 | { |
1e811303 | 715 | int64_t size, left, right; |
783e9b48 | 716 | |
1e811303 JF |
717 | /* No filter, return full size */ |
718 | if (!filter_area_length) { | |
719 | return block->target_end - block->target_start; | |
720 | } | |
783e9b48 | 721 | |
1e811303 JF |
722 | /* calculate the overlapped region. */ |
723 | left = MAX(filter_area_start, block->target_start); | |
724 | right = MIN(filter_area_start + filter_area_length, block->target_end); | |
725 | size = right - left; | |
726 | size = size > 0 ? size : 0; | |
727 | ||
728 | return size; | |
729 | } | |
730 | ||
113d8f4e JF |
731 | int64_t dump_filtered_memblock_start(GuestPhysBlock *block, |
732 | int64_t filter_area_start, | |
733 | int64_t filter_area_length) | |
1e811303 JF |
734 | { |
735 | if (filter_area_length) { | |
736 | /* return -1 if the block is not within filter area */ | |
737 | if (block->target_start >= filter_area_start + filter_area_length || | |
738 | block->target_end <= filter_area_start) { | |
739 | return -1; | |
783e9b48 WC |
740 | } |
741 | ||
1e811303 JF |
742 | if (filter_area_start > block->target_start) { |
743 | return filter_area_start - block->target_start; | |
744 | } | |
783e9b48 | 745 | } |
1e811303 JF |
746 | |
747 | return 0; | |
783e9b48 WC |
748 | } |
749 | ||
750 | /* write all memory to vmcore */ | |
4c7e251a | 751 | static void dump_iterate(DumpState *s, Error **errp) |
783e9b48 | 752 | { |
86a518bb | 753 | ERRP_GUARD(); |
56c4bfb3 | 754 | GuestPhysBlock *block; |
1e811303 | 755 | int64_t memblock_size, memblock_start; |
783e9b48 | 756 | |
1e811303 | 757 | QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) { |
dddf725f | 758 | memblock_start = dump_filtered_memblock_start(block, s->filter_area_begin, s->filter_area_length); |
1e811303 JF |
759 | if (memblock_start == -1) { |
760 | continue; | |
783e9b48 | 761 | } |
1e811303 | 762 | |
dddf725f | 763 | memblock_size = dump_filtered_memblock_size(block, s->filter_area_begin, s->filter_area_length); |
1e811303 JF |
764 | |
765 | /* Write the memory to file */ | |
766 | write_memory(s, block, memblock_start, memblock_size, errp); | |
86a518bb | 767 | if (*errp) { |
4c7e251a | 768 | return; |
783e9b48 | 769 | } |
1e811303 | 770 | } |
783e9b48 WC |
771 | } |
772 | ||
9b72224f JF |
773 | static void dump_end(DumpState *s, Error **errp) |
774 | { | |
775 | int rc; | |
776 | ERRP_GUARD(); | |
777 | ||
778 | if (s->elf_section_data_size) { | |
779 | s->elf_section_data = g_malloc0(s->elf_section_data_size); | |
780 | } | |
781 | ||
782 | /* Adds the architecture defined section data to s->elf_section_data */ | |
783 | if (s->dump_info.arch_sections_write_fn && | |
784 | s->elf_section_data_size) { | |
785 | rc = s->dump_info.arch_sections_write_fn(s, s->elf_section_data); | |
786 | if (rc) { | |
787 | error_setg_errno(errp, rc, | |
788 | "dump: failed to get arch section data"); | |
789 | g_free(s->elf_section_data); | |
790 | return; | |
791 | } | |
792 | } | |
793 | ||
794 | /* write sections to vmcore */ | |
795 | write_elf_sections(s, errp); | |
796 | } | |
797 | ||
4c7e251a | 798 | static void create_vmcore(DumpState *s, Error **errp) |
783e9b48 | 799 | { |
86a518bb | 800 | ERRP_GUARD(); |
783e9b48 | 801 | |
86a518bb JF |
802 | dump_begin(s, errp); |
803 | if (*errp) { | |
4c7e251a | 804 | return; |
783e9b48 WC |
805 | } |
806 | ||
9b72224f | 807 | /* Iterate over memory and dump it to file */ |
4c7e251a | 808 | dump_iterate(s, errp); |
9b72224f JF |
809 | if (*errp) { |
810 | return; | |
811 | } | |
812 | ||
813 | /* Write the section data */ | |
814 | dump_end(s, errp); | |
783e9b48 WC |
815 | } |
816 | ||
fda05387 QN |
817 | static int write_start_flat_header(int fd) |
818 | { | |
92ba1401 | 819 | MakedumpfileHeader *mh; |
fda05387 QN |
820 | int ret = 0; |
821 | ||
92ba1401 LE |
822 | QEMU_BUILD_BUG_ON(sizeof *mh > MAX_SIZE_MDF_HEADER); |
823 | mh = g_malloc0(MAX_SIZE_MDF_HEADER); | |
fda05387 | 824 | |
92ba1401 LE |
825 | memcpy(mh->signature, MAKEDUMPFILE_SIGNATURE, |
826 | MIN(sizeof mh->signature, sizeof MAKEDUMPFILE_SIGNATURE)); | |
fda05387 | 827 | |
92ba1401 LE |
828 | mh->type = cpu_to_be64(TYPE_FLAT_HEADER); |
829 | mh->version = cpu_to_be64(VERSION_FLAT_HEADER); | |
fda05387 QN |
830 | |
831 | size_t written_size; | |
92ba1401 | 832 | written_size = qemu_write_full(fd, mh, MAX_SIZE_MDF_HEADER); |
fda05387 QN |
833 | if (written_size != MAX_SIZE_MDF_HEADER) { |
834 | ret = -1; | |
835 | } | |
836 | ||
92ba1401 | 837 | g_free(mh); |
fda05387 QN |
838 | return ret; |
839 | } | |
840 | ||
841 | static int write_end_flat_header(int fd) | |
842 | { | |
843 | MakedumpfileDataHeader mdh; | |
844 | ||
845 | mdh.offset = END_FLAG_FLAT_HEADER; | |
846 | mdh.buf_size = END_FLAG_FLAT_HEADER; | |
847 | ||
848 | size_t written_size; | |
849 | written_size = qemu_write_full(fd, &mdh, sizeof(mdh)); | |
850 | if (written_size != sizeof(mdh)) { | |
851 | return -1; | |
852 | } | |
853 | ||
854 | return 0; | |
855 | } | |
856 | ||
5d31babe QN |
857 | static int write_buffer(int fd, off_t offset, const void *buf, size_t size) |
858 | { | |
859 | size_t written_size; | |
860 | MakedumpfileDataHeader mdh; | |
861 | ||
862 | mdh.offset = cpu_to_be64(offset); | |
863 | mdh.buf_size = cpu_to_be64(size); | |
864 | ||
865 | written_size = qemu_write_full(fd, &mdh, sizeof(mdh)); | |
866 | if (written_size != sizeof(mdh)) { | |
867 | return -1; | |
868 | } | |
869 | ||
870 | written_size = qemu_write_full(fd, buf, size); | |
871 | if (written_size != size) { | |
872 | return -1; | |
873 | } | |
874 | ||
875 | return 0; | |
876 | } | |
877 | ||
4835ef77 QN |
878 | static int buf_write_note(const void *buf, size_t size, void *opaque) |
879 | { | |
880 | DumpState *s = opaque; | |
881 | ||
882 | /* note_buf is not enough */ | |
883 | if (s->note_buf_offset + size > s->note_size) { | |
884 | return -1; | |
885 | } | |
886 | ||
887 | memcpy(s->note_buf + s->note_buf_offset, buf, size); | |
888 | ||
889 | s->note_buf_offset += size; | |
890 | ||
891 | return 0; | |
892 | } | |
893 | ||
903ef734 MAL |
894 | /* |
895 | * This function retrieves various sizes from an elf header. | |
896 | * | |
897 | * @note has to be a valid ELF note. The return sizes are unmodified | |
898 | * (not padded or rounded up to be multiple of 4). | |
899 | */ | |
900 | static void get_note_sizes(DumpState *s, const void *note, | |
901 | uint64_t *note_head_size, | |
902 | uint64_t *name_size, | |
903 | uint64_t *desc_size) | |
904 | { | |
905 | uint64_t note_head_sz; | |
906 | uint64_t name_sz; | |
907 | uint64_t desc_sz; | |
908 | ||
05bbaa50 | 909 | if (dump_is_64bit(s)) { |
903ef734 MAL |
910 | const Elf64_Nhdr *hdr = note; |
911 | note_head_sz = sizeof(Elf64_Nhdr); | |
912 | name_sz = tswap64(hdr->n_namesz); | |
913 | desc_sz = tswap64(hdr->n_descsz); | |
914 | } else { | |
915 | const Elf32_Nhdr *hdr = note; | |
916 | note_head_sz = sizeof(Elf32_Nhdr); | |
917 | name_sz = tswap32(hdr->n_namesz); | |
918 | desc_sz = tswap32(hdr->n_descsz); | |
919 | } | |
920 | ||
921 | if (note_head_size) { | |
922 | *note_head_size = note_head_sz; | |
923 | } | |
924 | if (name_size) { | |
925 | *name_size = name_sz; | |
926 | } | |
927 | if (desc_size) { | |
928 | *desc_size = desc_sz; | |
929 | } | |
930 | } | |
931 | ||
d9feb517 MAL |
932 | static bool note_name_equal(DumpState *s, |
933 | const uint8_t *note, const char *name) | |
934 | { | |
935 | int len = strlen(name) + 1; | |
936 | uint64_t head_size, name_size; | |
937 | ||
938 | get_note_sizes(s, note, &head_size, &name_size, NULL); | |
939 | head_size = ROUND_UP(head_size, 4); | |
940 | ||
c983ca84 | 941 | return name_size == len && memcmp(note + head_size, name, len) == 0; |
d9feb517 MAL |
942 | } |
943 | ||
298f1168 | 944 | /* write common header, sub header and elf note to vmcore */ |
4c7e251a | 945 | static void create_header32(DumpState *s, Error **errp) |
298f1168 | 946 | { |
86a518bb | 947 | ERRP_GUARD(); |
298f1168 QN |
948 | DiskDumpHeader32 *dh = NULL; |
949 | KdumpSubHeader32 *kh = NULL; | |
950 | size_t size; | |
298f1168 QN |
951 | uint32_t block_size; |
952 | uint32_t sub_hdr_size; | |
953 | uint32_t bitmap_blocks; | |
954 | uint32_t status = 0; | |
955 | uint64_t offset_note; | |
956 | ||
957 | /* write common header, the version of kdump-compressed format is 6th */ | |
958 | size = sizeof(DiskDumpHeader32); | |
959 | dh = g_malloc0(size); | |
960 | ||
84c868f6 | 961 | memcpy(dh->signature, KDUMP_SIGNATURE, SIG_LEN); |
acb0ef58 | 962 | dh->header_version = cpu_to_dump32(s, 6); |
8161befd | 963 | block_size = s->dump_info.page_size; |
acb0ef58 | 964 | dh->block_size = cpu_to_dump32(s, block_size); |
298f1168 QN |
965 | sub_hdr_size = sizeof(struct KdumpSubHeader32) + s->note_size; |
966 | sub_hdr_size = DIV_ROUND_UP(sub_hdr_size, block_size); | |
acb0ef58 | 967 | dh->sub_hdr_size = cpu_to_dump32(s, sub_hdr_size); |
298f1168 | 968 | /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */ |
acb0ef58 BR |
969 | dh->max_mapnr = cpu_to_dump32(s, MIN(s->max_mapnr, UINT_MAX)); |
970 | dh->nr_cpus = cpu_to_dump32(s, s->nr_cpus); | |
298f1168 | 971 | bitmap_blocks = DIV_ROUND_UP(s->len_dump_bitmap, block_size) * 2; |
acb0ef58 | 972 | dh->bitmap_blocks = cpu_to_dump32(s, bitmap_blocks); |
4ab23a91 | 973 | strncpy(dh->utsname.machine, ELF_MACHINE_UNAME, sizeof(dh->utsname.machine)); |
298f1168 QN |
974 | |
975 | if (s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) { | |
976 | status |= DUMP_DH_COMPRESSED_ZLIB; | |
977 | } | |
978 | #ifdef CONFIG_LZO | |
979 | if (s->flag_compress & DUMP_DH_COMPRESSED_LZO) { | |
980 | status |= DUMP_DH_COMPRESSED_LZO; | |
981 | } | |
982 | #endif | |
983 | #ifdef CONFIG_SNAPPY | |
984 | if (s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) { | |
985 | status |= DUMP_DH_COMPRESSED_SNAPPY; | |
986 | } | |
987 | #endif | |
acb0ef58 | 988 | dh->status = cpu_to_dump32(s, status); |
298f1168 QN |
989 | |
990 | if (write_buffer(s->fd, 0, dh, size) < 0) { | |
e3517a52 | 991 | error_setg(errp, "dump: failed to write disk dump header"); |
298f1168 QN |
992 | goto out; |
993 | } | |
994 | ||
995 | /* write sub header */ | |
996 | size = sizeof(KdumpSubHeader32); | |
997 | kh = g_malloc0(size); | |
998 | ||
999 | /* 64bit max_mapnr_64 */ | |
acb0ef58 | 1000 | kh->max_mapnr_64 = cpu_to_dump64(s, s->max_mapnr); |
b6e05aa4 | 1001 | kh->phys_base = cpu_to_dump32(s, s->dump_info.phys_base); |
acb0ef58 | 1002 | kh->dump_level = cpu_to_dump32(s, DUMP_LEVEL); |
298f1168 QN |
1003 | |
1004 | offset_note = DISKDUMP_HEADER_BLOCKS * block_size + size; | |
9ada575b MAL |
1005 | if (s->guest_note && |
1006 | note_name_equal(s, s->guest_note, "VMCOREINFO")) { | |
1007 | uint64_t hsize, name_size, size_vmcoreinfo_desc, offset_vmcoreinfo; | |
1008 | ||
1009 | get_note_sizes(s, s->guest_note, | |
1010 | &hsize, &name_size, &size_vmcoreinfo_desc); | |
1011 | offset_vmcoreinfo = offset_note + s->note_size - s->guest_note_size + | |
1012 | (DIV_ROUND_UP(hsize, 4) + DIV_ROUND_UP(name_size, 4)) * 4; | |
1013 | kh->offset_vmcoreinfo = cpu_to_dump64(s, offset_vmcoreinfo); | |
1014 | kh->size_vmcoreinfo = cpu_to_dump32(s, size_vmcoreinfo_desc); | |
1015 | } | |
1016 | ||
acb0ef58 BR |
1017 | kh->offset_note = cpu_to_dump64(s, offset_note); |
1018 | kh->note_size = cpu_to_dump32(s, s->note_size); | |
298f1168 QN |
1019 | |
1020 | if (write_buffer(s->fd, DISKDUMP_HEADER_BLOCKS * | |
1021 | block_size, kh, size) < 0) { | |
e3517a52 | 1022 | error_setg(errp, "dump: failed to write kdump sub header"); |
298f1168 QN |
1023 | goto out; |
1024 | } | |
1025 | ||
1026 | /* write note */ | |
1027 | s->note_buf = g_malloc0(s->note_size); | |
1028 | s->note_buf_offset = 0; | |
1029 | ||
1030 | /* use s->note_buf to store notes temporarily */ | |
86a518bb JF |
1031 | write_elf32_notes(buf_write_note, s, errp); |
1032 | if (*errp) { | |
298f1168 QN |
1033 | goto out; |
1034 | } | |
298f1168 QN |
1035 | if (write_buffer(s->fd, offset_note, s->note_buf, |
1036 | s->note_size) < 0) { | |
e3517a52 | 1037 | error_setg(errp, "dump: failed to write notes"); |
298f1168 QN |
1038 | goto out; |
1039 | } | |
1040 | ||
1041 | /* get offset of dump_bitmap */ | |
1042 | s->offset_dump_bitmap = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size) * | |
1043 | block_size; | |
1044 | ||
1045 | /* get offset of page */ | |
1046 | s->offset_page = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size + bitmap_blocks) * | |
1047 | block_size; | |
1048 | ||
1049 | out: | |
1050 | g_free(dh); | |
1051 | g_free(kh); | |
1052 | g_free(s->note_buf); | |
298f1168 QN |
1053 | } |
1054 | ||
1055 | /* write common header, sub header and elf note to vmcore */ | |
4c7e251a | 1056 | static void create_header64(DumpState *s, Error **errp) |
298f1168 | 1057 | { |
86a518bb | 1058 | ERRP_GUARD(); |
298f1168 QN |
1059 | DiskDumpHeader64 *dh = NULL; |
1060 | KdumpSubHeader64 *kh = NULL; | |
1061 | size_t size; | |
298f1168 QN |
1062 | uint32_t block_size; |
1063 | uint32_t sub_hdr_size; | |
1064 | uint32_t bitmap_blocks; | |
1065 | uint32_t status = 0; | |
1066 | uint64_t offset_note; | |
1067 | ||
1068 | /* write common header, the version of kdump-compressed format is 6th */ | |
1069 | size = sizeof(DiskDumpHeader64); | |
1070 | dh = g_malloc0(size); | |
1071 | ||
84c868f6 | 1072 | memcpy(dh->signature, KDUMP_SIGNATURE, SIG_LEN); |
acb0ef58 | 1073 | dh->header_version = cpu_to_dump32(s, 6); |
8161befd | 1074 | block_size = s->dump_info.page_size; |
acb0ef58 | 1075 | dh->block_size = cpu_to_dump32(s, block_size); |
298f1168 QN |
1076 | sub_hdr_size = sizeof(struct KdumpSubHeader64) + s->note_size; |
1077 | sub_hdr_size = DIV_ROUND_UP(sub_hdr_size, block_size); | |
acb0ef58 | 1078 | dh->sub_hdr_size = cpu_to_dump32(s, sub_hdr_size); |
298f1168 | 1079 | /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */ |
acb0ef58 BR |
1080 | dh->max_mapnr = cpu_to_dump32(s, MIN(s->max_mapnr, UINT_MAX)); |
1081 | dh->nr_cpus = cpu_to_dump32(s, s->nr_cpus); | |
298f1168 | 1082 | bitmap_blocks = DIV_ROUND_UP(s->len_dump_bitmap, block_size) * 2; |
acb0ef58 | 1083 | dh->bitmap_blocks = cpu_to_dump32(s, bitmap_blocks); |
4ab23a91 | 1084 | strncpy(dh->utsname.machine, ELF_MACHINE_UNAME, sizeof(dh->utsname.machine)); |
298f1168 QN |
1085 | |
1086 | if (s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) { | |
1087 | status |= DUMP_DH_COMPRESSED_ZLIB; | |
1088 | } | |
1089 | #ifdef CONFIG_LZO | |
1090 | if (s->flag_compress & DUMP_DH_COMPRESSED_LZO) { | |
1091 | status |= DUMP_DH_COMPRESSED_LZO; | |
1092 | } | |
1093 | #endif | |
1094 | #ifdef CONFIG_SNAPPY | |
1095 | if (s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) { | |
1096 | status |= DUMP_DH_COMPRESSED_SNAPPY; | |
1097 | } | |
1098 | #endif | |
acb0ef58 | 1099 | dh->status = cpu_to_dump32(s, status); |
298f1168 QN |
1100 | |
1101 | if (write_buffer(s->fd, 0, dh, size) < 0) { | |
e3517a52 | 1102 | error_setg(errp, "dump: failed to write disk dump header"); |
298f1168 QN |
1103 | goto out; |
1104 | } | |
1105 | ||
1106 | /* write sub header */ | |
1107 | size = sizeof(KdumpSubHeader64); | |
1108 | kh = g_malloc0(size); | |
1109 | ||
1110 | /* 64bit max_mapnr_64 */ | |
acb0ef58 | 1111 | kh->max_mapnr_64 = cpu_to_dump64(s, s->max_mapnr); |
b6e05aa4 | 1112 | kh->phys_base = cpu_to_dump64(s, s->dump_info.phys_base); |
acb0ef58 | 1113 | kh->dump_level = cpu_to_dump32(s, DUMP_LEVEL); |
298f1168 QN |
1114 | |
1115 | offset_note = DISKDUMP_HEADER_BLOCKS * block_size + size; | |
9ada575b MAL |
1116 | if (s->guest_note && |
1117 | note_name_equal(s, s->guest_note, "VMCOREINFO")) { | |
1118 | uint64_t hsize, name_size, size_vmcoreinfo_desc, offset_vmcoreinfo; | |
1119 | ||
1120 | get_note_sizes(s, s->guest_note, | |
1121 | &hsize, &name_size, &size_vmcoreinfo_desc); | |
1122 | offset_vmcoreinfo = offset_note + s->note_size - s->guest_note_size + | |
1123 | (DIV_ROUND_UP(hsize, 4) + DIV_ROUND_UP(name_size, 4)) * 4; | |
1124 | kh->offset_vmcoreinfo = cpu_to_dump64(s, offset_vmcoreinfo); | |
1125 | kh->size_vmcoreinfo = cpu_to_dump64(s, size_vmcoreinfo_desc); | |
1126 | } | |
1127 | ||
acb0ef58 BR |
1128 | kh->offset_note = cpu_to_dump64(s, offset_note); |
1129 | kh->note_size = cpu_to_dump64(s, s->note_size); | |
298f1168 QN |
1130 | |
1131 | if (write_buffer(s->fd, DISKDUMP_HEADER_BLOCKS * | |
1132 | block_size, kh, size) < 0) { | |
e3517a52 | 1133 | error_setg(errp, "dump: failed to write kdump sub header"); |
298f1168 QN |
1134 | goto out; |
1135 | } | |
1136 | ||
1137 | /* write note */ | |
1138 | s->note_buf = g_malloc0(s->note_size); | |
1139 | s->note_buf_offset = 0; | |
1140 | ||
1141 | /* use s->note_buf to store notes temporarily */ | |
86a518bb JF |
1142 | write_elf64_notes(buf_write_note, s, errp); |
1143 | if (*errp) { | |
298f1168 QN |
1144 | goto out; |
1145 | } | |
1146 | ||
1147 | if (write_buffer(s->fd, offset_note, s->note_buf, | |
1148 | s->note_size) < 0) { | |
e3517a52 | 1149 | error_setg(errp, "dump: failed to write notes"); |
298f1168 QN |
1150 | goto out; |
1151 | } | |
1152 | ||
1153 | /* get offset of dump_bitmap */ | |
1154 | s->offset_dump_bitmap = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size) * | |
1155 | block_size; | |
1156 | ||
1157 | /* get offset of page */ | |
1158 | s->offset_page = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size + bitmap_blocks) * | |
1159 | block_size; | |
1160 | ||
1161 | out: | |
1162 | g_free(dh); | |
1163 | g_free(kh); | |
1164 | g_free(s->note_buf); | |
298f1168 QN |
1165 | } |
1166 | ||
4c7e251a | 1167 | static void write_dump_header(DumpState *s, Error **errp) |
298f1168 | 1168 | { |
05bbaa50 | 1169 | if (dump_is_64bit(s)) { |
992861fb | 1170 | create_header64(s, errp); |
05bbaa50 JF |
1171 | } else { |
1172 | create_header32(s, errp); | |
4c7e251a | 1173 | } |
298f1168 QN |
1174 | } |
1175 | ||
8161befd AJ |
1176 | static size_t dump_bitmap_get_bufsize(DumpState *s) |
1177 | { | |
1178 | return s->dump_info.page_size; | |
1179 | } | |
1180 | ||
d0686c72 QN |
1181 | /* |
1182 | * set dump_bitmap sequencely. the bit before last_pfn is not allowed to be | |
1183 | * rewritten, so if need to set the first bit, set last_pfn and pfn to 0. | |
1184 | * set_dump_bitmap will always leave the recently set bit un-sync. And setting | |
1185 | * (last bit + sizeof(buf) * 8) to 0 will do flushing the content in buf into | |
1186 | * vmcore, ie. synchronizing un-sync bit into vmcore. | |
1187 | */ | |
1188 | static int set_dump_bitmap(uint64_t last_pfn, uint64_t pfn, bool value, | |
1189 | uint8_t *buf, DumpState *s) | |
1190 | { | |
1191 | off_t old_offset, new_offset; | |
1192 | off_t offset_bitmap1, offset_bitmap2; | |
1193 | uint32_t byte, bit; | |
8161befd AJ |
1194 | size_t bitmap_bufsize = dump_bitmap_get_bufsize(s); |
1195 | size_t bits_per_buf = bitmap_bufsize * CHAR_BIT; | |
d0686c72 QN |
1196 | |
1197 | /* should not set the previous place */ | |
1198 | assert(last_pfn <= pfn); | |
1199 | ||
1200 | /* | |
1201 | * if the bit needed to be set is not cached in buf, flush the data in buf | |
1202 | * to vmcore firstly. | |
1203 | * making new_offset be bigger than old_offset can also sync remained data | |
1204 | * into vmcore. | |
1205 | */ | |
8161befd AJ |
1206 | old_offset = bitmap_bufsize * (last_pfn / bits_per_buf); |
1207 | new_offset = bitmap_bufsize * (pfn / bits_per_buf); | |
d0686c72 QN |
1208 | |
1209 | while (old_offset < new_offset) { | |
1210 | /* calculate the offset and write dump_bitmap */ | |
1211 | offset_bitmap1 = s->offset_dump_bitmap + old_offset; | |
1212 | if (write_buffer(s->fd, offset_bitmap1, buf, | |
8161befd | 1213 | bitmap_bufsize) < 0) { |
d0686c72 QN |
1214 | return -1; |
1215 | } | |
1216 | ||
1217 | /* dump level 1 is chosen, so 1st and 2nd bitmap are same */ | |
1218 | offset_bitmap2 = s->offset_dump_bitmap + s->len_dump_bitmap + | |
1219 | old_offset; | |
1220 | if (write_buffer(s->fd, offset_bitmap2, buf, | |
8161befd | 1221 | bitmap_bufsize) < 0) { |
d0686c72 QN |
1222 | return -1; |
1223 | } | |
1224 | ||
8161befd AJ |
1225 | memset(buf, 0, bitmap_bufsize); |
1226 | old_offset += bitmap_bufsize; | |
d0686c72 QN |
1227 | } |
1228 | ||
1229 | /* get the exact place of the bit in the buf, and set it */ | |
8161befd AJ |
1230 | byte = (pfn % bits_per_buf) / CHAR_BIT; |
1231 | bit = (pfn % bits_per_buf) % CHAR_BIT; | |
d0686c72 QN |
1232 | if (value) { |
1233 | buf[byte] |= 1u << bit; | |
1234 | } else { | |
1235 | buf[byte] &= ~(1u << bit); | |
1236 | } | |
1237 | ||
1238 | return 0; | |
1239 | } | |
1240 | ||
8161befd AJ |
1241 | static uint64_t dump_paddr_to_pfn(DumpState *s, uint64_t addr) |
1242 | { | |
1243 | int target_page_shift = ctz32(s->dump_info.page_size); | |
1244 | ||
1245 | return (addr >> target_page_shift) - ARCH_PFN_OFFSET; | |
1246 | } | |
1247 | ||
1248 | static uint64_t dump_pfn_to_paddr(DumpState *s, uint64_t pfn) | |
1249 | { | |
1250 | int target_page_shift = ctz32(s->dump_info.page_size); | |
1251 | ||
1252 | return (pfn + ARCH_PFN_OFFSET) << target_page_shift; | |
1253 | } | |
1254 | ||
d0686c72 | 1255 | /* |
94d78840 MAL |
1256 | * Return the page frame number and the page content in *bufptr. bufptr can be |
1257 | * NULL. If not NULL, *bufptr must contains a target page size of pre-allocated | |
1258 | * memory. This is not necessarily the memory returned. | |
d0686c72 QN |
1259 | */ |
1260 | static bool get_next_page(GuestPhysBlock **blockptr, uint64_t *pfnptr, | |
1261 | uint8_t **bufptr, DumpState *s) | |
1262 | { | |
1263 | GuestPhysBlock *block = *blockptr; | |
94d78840 MAL |
1264 | uint32_t page_size = s->dump_info.page_size; |
1265 | uint8_t *buf = NULL, *hbuf; | |
1266 | hwaddr addr; | |
d0686c72 QN |
1267 | |
1268 | /* block == NULL means the start of the iteration */ | |
1269 | if (!block) { | |
1270 | block = QTAILQ_FIRST(&s->guest_phys_blocks.head); | |
1271 | *blockptr = block; | |
08df3438 | 1272 | addr = block->target_start; |
94d78840 | 1273 | *pfnptr = dump_paddr_to_pfn(s, addr); |
08df3438 | 1274 | } else { |
94d78840 MAL |
1275 | *pfnptr += 1; |
1276 | addr = dump_pfn_to_paddr(s, *pfnptr); | |
d0686c72 | 1277 | } |
08df3438 | 1278 | assert(block != NULL); |
d0686c72 | 1279 | |
94d78840 MAL |
1280 | while (1) { |
1281 | if (addr >= block->target_start && addr < block->target_end) { | |
1282 | size_t n = MIN(block->target_end - addr, page_size - addr % page_size); | |
1283 | hbuf = block->host_addr + (addr - block->target_start); | |
1284 | if (!buf) { | |
1285 | if (n == page_size) { | |
1286 | /* this is a whole target page, go for it */ | |
1287 | assert(addr % page_size == 0); | |
1288 | buf = hbuf; | |
1289 | break; | |
1290 | } else if (bufptr) { | |
1291 | assert(*bufptr); | |
1292 | buf = *bufptr; | |
1293 | memset(buf, 0, page_size); | |
1294 | } else { | |
1295 | return true; | |
1296 | } | |
1297 | } | |
1298 | ||
1299 | memcpy(buf + addr % page_size, hbuf, n); | |
1300 | addr += n; | |
1301 | if (addr % page_size == 0) { | |
1302 | /* we filled up the page */ | |
1303 | break; | |
1304 | } | |
1305 | } else { | |
1306 | /* the next page is in the next block */ | |
1307 | *blockptr = block = QTAILQ_NEXT(block, next); | |
1308 | if (!block) { | |
1309 | break; | |
1310 | } | |
1311 | ||
1312 | addr = block->target_start; | |
1313 | /* are we still in the same page? */ | |
1314 | if (dump_paddr_to_pfn(s, addr) != *pfnptr) { | |
1315 | if (buf) { | |
1316 | /* no, but we already filled something earlier, return it */ | |
1317 | break; | |
1318 | } else { | |
1319 | /* else continue from there */ | |
1320 | *pfnptr = dump_paddr_to_pfn(s, addr); | |
1321 | } | |
1322 | } | |
d0686c72 | 1323 | } |
d0686c72 QN |
1324 | } |
1325 | ||
1326 | if (bufptr) { | |
1327 | *bufptr = buf; | |
1328 | } | |
1329 | ||
94d78840 | 1330 | return buf != NULL; |
d0686c72 QN |
1331 | } |
1332 | ||
4c7e251a | 1333 | static void write_dump_bitmap(DumpState *s, Error **errp) |
d0686c72 QN |
1334 | { |
1335 | int ret = 0; | |
1336 | uint64_t last_pfn, pfn; | |
1337 | void *dump_bitmap_buf; | |
1338 | size_t num_dumpable; | |
1339 | GuestPhysBlock *block_iter = NULL; | |
8161befd AJ |
1340 | size_t bitmap_bufsize = dump_bitmap_get_bufsize(s); |
1341 | size_t bits_per_buf = bitmap_bufsize * CHAR_BIT; | |
d0686c72 QN |
1342 | |
1343 | /* dump_bitmap_buf is used to store dump_bitmap temporarily */ | |
8161befd | 1344 | dump_bitmap_buf = g_malloc0(bitmap_bufsize); |
d0686c72 QN |
1345 | |
1346 | num_dumpable = 0; | |
1347 | last_pfn = 0; | |
1348 | ||
1349 | /* | |
1350 | * exam memory page by page, and set the bit in dump_bitmap corresponded | |
1351 | * to the existing page. | |
1352 | */ | |
1353 | while (get_next_page(&block_iter, &pfn, NULL, s)) { | |
1354 | ret = set_dump_bitmap(last_pfn, pfn, true, dump_bitmap_buf, s); | |
1355 | if (ret < 0) { | |
e3517a52 | 1356 | error_setg(errp, "dump: failed to set dump_bitmap"); |
d0686c72 QN |
1357 | goto out; |
1358 | } | |
1359 | ||
1360 | last_pfn = pfn; | |
1361 | num_dumpable++; | |
1362 | } | |
1363 | ||
1364 | /* | |
1365 | * set_dump_bitmap will always leave the recently set bit un-sync. Here we | |
8161befd AJ |
1366 | * set the remaining bits from last_pfn to the end of the bitmap buffer to |
1367 | * 0. With those set, the un-sync bit will be synchronized into the vmcore. | |
d0686c72 QN |
1368 | */ |
1369 | if (num_dumpable > 0) { | |
8161befd | 1370 | ret = set_dump_bitmap(last_pfn, last_pfn + bits_per_buf, false, |
d0686c72 QN |
1371 | dump_bitmap_buf, s); |
1372 | if (ret < 0) { | |
e3517a52 | 1373 | error_setg(errp, "dump: failed to sync dump_bitmap"); |
d0686c72 QN |
1374 | goto out; |
1375 | } | |
1376 | } | |
1377 | ||
1378 | /* number of dumpable pages that will be dumped later */ | |
1379 | s->num_dumpable = num_dumpable; | |
1380 | ||
1381 | out: | |
1382 | g_free(dump_bitmap_buf); | |
d0686c72 QN |
1383 | } |
1384 | ||
64cfba6a QN |
1385 | static void prepare_data_cache(DataCache *data_cache, DumpState *s, |
1386 | off_t offset) | |
1387 | { | |
1388 | data_cache->fd = s->fd; | |
1389 | data_cache->data_size = 0; | |
8161befd AJ |
1390 | data_cache->buf_size = 4 * dump_bitmap_get_bufsize(s); |
1391 | data_cache->buf = g_malloc0(data_cache->buf_size); | |
64cfba6a QN |
1392 | data_cache->offset = offset; |
1393 | } | |
1394 | ||
1395 | static int write_cache(DataCache *dc, const void *buf, size_t size, | |
1396 | bool flag_sync) | |
1397 | { | |
1398 | /* | |
1399 | * dc->buf_size should not be less than size, otherwise dc will never be | |
1400 | * enough | |
1401 | */ | |
1402 | assert(size <= dc->buf_size); | |
1403 | ||
1404 | /* | |
1405 | * if flag_sync is set, synchronize data in dc->buf into vmcore. | |
1406 | * otherwise check if the space is enough for caching data in buf, if not, | |
1407 | * write the data in dc->buf to dc->fd and reset dc->buf | |
1408 | */ | |
1409 | if ((!flag_sync && dc->data_size + size > dc->buf_size) || | |
1410 | (flag_sync && dc->data_size > 0)) { | |
1411 | if (write_buffer(dc->fd, dc->offset, dc->buf, dc->data_size) < 0) { | |
1412 | return -1; | |
1413 | } | |
1414 | ||
1415 | dc->offset += dc->data_size; | |
1416 | dc->data_size = 0; | |
1417 | } | |
1418 | ||
1419 | if (!flag_sync) { | |
1420 | memcpy(dc->buf + dc->data_size, buf, size); | |
1421 | dc->data_size += size; | |
1422 | } | |
1423 | ||
1424 | return 0; | |
1425 | } | |
1426 | ||
1427 | static void free_data_cache(DataCache *data_cache) | |
1428 | { | |
1429 | g_free(data_cache->buf); | |
1430 | } | |
1431 | ||
d12f57ec QN |
1432 | static size_t get_len_buf_out(size_t page_size, uint32_t flag_compress) |
1433 | { | |
b87ef351 LE |
1434 | switch (flag_compress) { |
1435 | case DUMP_DH_COMPRESSED_ZLIB: | |
1436 | return compressBound(page_size); | |
1437 | ||
1438 | case DUMP_DH_COMPRESSED_LZO: | |
1439 | /* | |
1440 | * LZO will expand incompressible data by a little amount. Please check | |
1441 | * the following URL to see the expansion calculation: | |
1442 | * http://www.oberhumer.com/opensource/lzo/lzofaq.php | |
1443 | */ | |
1444 | return page_size + page_size / 16 + 64 + 3; | |
d12f57ec QN |
1445 | |
1446 | #ifdef CONFIG_SNAPPY | |
b87ef351 LE |
1447 | case DUMP_DH_COMPRESSED_SNAPPY: |
1448 | return snappy_max_compressed_length(page_size); | |
d12f57ec | 1449 | #endif |
b87ef351 LE |
1450 | } |
1451 | return 0; | |
d12f57ec QN |
1452 | } |
1453 | ||
4c7e251a | 1454 | static void write_dump_pages(DumpState *s, Error **errp) |
d12f57ec QN |
1455 | { |
1456 | int ret = 0; | |
1457 | DataCache page_desc, page_data; | |
1458 | size_t len_buf_out, size_out; | |
1459 | #ifdef CONFIG_LZO | |
1460 | lzo_bytep wrkmem = NULL; | |
1461 | #endif | |
1462 | uint8_t *buf_out = NULL; | |
1463 | off_t offset_desc, offset_data; | |
1464 | PageDescriptor pd, pd_zero; | |
1465 | uint8_t *buf; | |
d12f57ec QN |
1466 | GuestPhysBlock *block_iter = NULL; |
1467 | uint64_t pfn_iter; | |
94d78840 | 1468 | g_autofree uint8_t *page = NULL; |
d12f57ec QN |
1469 | |
1470 | /* get offset of page_desc and page_data in dump file */ | |
1471 | offset_desc = s->offset_page; | |
1472 | offset_data = offset_desc + sizeof(PageDescriptor) * s->num_dumpable; | |
1473 | ||
1474 | prepare_data_cache(&page_desc, s, offset_desc); | |
1475 | prepare_data_cache(&page_data, s, offset_data); | |
1476 | ||
1477 | /* prepare buffer to store compressed data */ | |
8161befd | 1478 | len_buf_out = get_len_buf_out(s->dump_info.page_size, s->flag_compress); |
b87ef351 | 1479 | assert(len_buf_out != 0); |
d12f57ec QN |
1480 | |
1481 | #ifdef CONFIG_LZO | |
1482 | wrkmem = g_malloc(LZO1X_1_MEM_COMPRESS); | |
1483 | #endif | |
1484 | ||
1485 | buf_out = g_malloc(len_buf_out); | |
1486 | ||
1487 | /* | |
1488 | * init zero page's page_desc and page_data, because every zero page | |
1489 | * uses the same page_data | |
1490 | */ | |
8161befd | 1491 | pd_zero.size = cpu_to_dump32(s, s->dump_info.page_size); |
acb0ef58 BR |
1492 | pd_zero.flags = cpu_to_dump32(s, 0); |
1493 | pd_zero.offset = cpu_to_dump64(s, offset_data); | |
1494 | pd_zero.page_flags = cpu_to_dump64(s, 0); | |
8161befd AJ |
1495 | buf = g_malloc0(s->dump_info.page_size); |
1496 | ret = write_cache(&page_data, buf, s->dump_info.page_size, false); | |
d12f57ec QN |
1497 | g_free(buf); |
1498 | if (ret < 0) { | |
e3517a52 | 1499 | error_setg(errp, "dump: failed to write page data (zero page)"); |
d12f57ec QN |
1500 | goto out; |
1501 | } | |
1502 | ||
8161befd | 1503 | offset_data += s->dump_info.page_size; |
94d78840 | 1504 | page = g_malloc(s->dump_info.page_size); |
d12f57ec QN |
1505 | |
1506 | /* | |
1507 | * dump memory to vmcore page by page. zero page will all be resided in the | |
1508 | * first page of page section | |
1509 | */ | |
94d78840 | 1510 | for (buf = page; get_next_page(&block_iter, &pfn_iter, &buf, s); buf = page) { |
d12f57ec | 1511 | /* check zero page */ |
f13f22ba | 1512 | if (buffer_is_zero(buf, s->dump_info.page_size)) { |
d12f57ec QN |
1513 | ret = write_cache(&page_desc, &pd_zero, sizeof(PageDescriptor), |
1514 | false); | |
1515 | if (ret < 0) { | |
e3517a52 | 1516 | error_setg(errp, "dump: failed to write page desc"); |
d12f57ec QN |
1517 | goto out; |
1518 | } | |
1519 | } else { | |
1520 | /* | |
1521 | * not zero page, then: | |
1522 | * 1. compress the page | |
1523 | * 2. write the compressed page into the cache of page_data | |
1524 | * 3. get page desc of the compressed page and write it into the | |
1525 | * cache of page_desc | |
1526 | * | |
1527 | * only one compression format will be used here, for | |
1528 | * s->flag_compress is set. But when compression fails to work, | |
1529 | * we fall back to save in plaintext. | |
1530 | */ | |
1531 | size_out = len_buf_out; | |
1532 | if ((s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) && | |
acb0ef58 | 1533 | (compress2(buf_out, (uLongf *)&size_out, buf, |
8161befd AJ |
1534 | s->dump_info.page_size, Z_BEST_SPEED) == Z_OK) && |
1535 | (size_out < s->dump_info.page_size)) { | |
acb0ef58 BR |
1536 | pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_ZLIB); |
1537 | pd.size = cpu_to_dump32(s, size_out); | |
d12f57ec QN |
1538 | |
1539 | ret = write_cache(&page_data, buf_out, size_out, false); | |
1540 | if (ret < 0) { | |
e3517a52 | 1541 | error_setg(errp, "dump: failed to write page data"); |
d12f57ec QN |
1542 | goto out; |
1543 | } | |
1544 | #ifdef CONFIG_LZO | |
1545 | } else if ((s->flag_compress & DUMP_DH_COMPRESSED_LZO) && | |
8161befd | 1546 | (lzo1x_1_compress(buf, s->dump_info.page_size, buf_out, |
d12f57ec | 1547 | (lzo_uint *)&size_out, wrkmem) == LZO_E_OK) && |
8161befd | 1548 | (size_out < s->dump_info.page_size)) { |
acb0ef58 BR |
1549 | pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_LZO); |
1550 | pd.size = cpu_to_dump32(s, size_out); | |
d12f57ec QN |
1551 | |
1552 | ret = write_cache(&page_data, buf_out, size_out, false); | |
1553 | if (ret < 0) { | |
e3517a52 | 1554 | error_setg(errp, "dump: failed to write page data"); |
d12f57ec QN |
1555 | goto out; |
1556 | } | |
1557 | #endif | |
1558 | #ifdef CONFIG_SNAPPY | |
1559 | } else if ((s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) && | |
8161befd | 1560 | (snappy_compress((char *)buf, s->dump_info.page_size, |
d12f57ec | 1561 | (char *)buf_out, &size_out) == SNAPPY_OK) && |
8161befd | 1562 | (size_out < s->dump_info.page_size)) { |
acb0ef58 BR |
1563 | pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_SNAPPY); |
1564 | pd.size = cpu_to_dump32(s, size_out); | |
d12f57ec QN |
1565 | |
1566 | ret = write_cache(&page_data, buf_out, size_out, false); | |
1567 | if (ret < 0) { | |
e3517a52 | 1568 | error_setg(errp, "dump: failed to write page data"); |
d12f57ec QN |
1569 | goto out; |
1570 | } | |
1571 | #endif | |
1572 | } else { | |
1573 | /* | |
1574 | * fall back to save in plaintext, size_out should be | |
8161befd | 1575 | * assigned the target's page size |
d12f57ec | 1576 | */ |
acb0ef58 | 1577 | pd.flags = cpu_to_dump32(s, 0); |
8161befd | 1578 | size_out = s->dump_info.page_size; |
acb0ef58 | 1579 | pd.size = cpu_to_dump32(s, size_out); |
d12f57ec | 1580 | |
8161befd AJ |
1581 | ret = write_cache(&page_data, buf, |
1582 | s->dump_info.page_size, false); | |
d12f57ec | 1583 | if (ret < 0) { |
e3517a52 | 1584 | error_setg(errp, "dump: failed to write page data"); |
d12f57ec QN |
1585 | goto out; |
1586 | } | |
1587 | } | |
1588 | ||
1589 | /* get and write page desc here */ | |
acb0ef58 BR |
1590 | pd.page_flags = cpu_to_dump64(s, 0); |
1591 | pd.offset = cpu_to_dump64(s, offset_data); | |
d12f57ec QN |
1592 | offset_data += size_out; |
1593 | ||
1594 | ret = write_cache(&page_desc, &pd, sizeof(PageDescriptor), false); | |
1595 | if (ret < 0) { | |
e3517a52 | 1596 | error_setg(errp, "dump: failed to write page desc"); |
d12f57ec QN |
1597 | goto out; |
1598 | } | |
1599 | } | |
2264c2c9 | 1600 | s->written_size += s->dump_info.page_size; |
d12f57ec QN |
1601 | } |
1602 | ||
1603 | ret = write_cache(&page_desc, NULL, 0, true); | |
1604 | if (ret < 0) { | |
e3517a52 | 1605 | error_setg(errp, "dump: failed to sync cache for page_desc"); |
d12f57ec QN |
1606 | goto out; |
1607 | } | |
1608 | ret = write_cache(&page_data, NULL, 0, true); | |
1609 | if (ret < 0) { | |
e3517a52 | 1610 | error_setg(errp, "dump: failed to sync cache for page_data"); |
d12f57ec QN |
1611 | goto out; |
1612 | } | |
1613 | ||
1614 | out: | |
1615 | free_data_cache(&page_desc); | |
1616 | free_data_cache(&page_data); | |
1617 | ||
1618 | #ifdef CONFIG_LZO | |
1619 | g_free(wrkmem); | |
1620 | #endif | |
1621 | ||
1622 | g_free(buf_out); | |
d12f57ec QN |
1623 | } |
1624 | ||
4c7e251a | 1625 | static void create_kdump_vmcore(DumpState *s, Error **errp) |
b53ccc30 | 1626 | { |
86a518bb | 1627 | ERRP_GUARD(); |
b53ccc30 QN |
1628 | int ret; |
1629 | ||
1630 | /* | |
1631 | * the kdump-compressed format is: | |
1632 | * File offset | |
1633 | * +------------------------------------------+ 0x0 | |
1634 | * | main header (struct disk_dump_header) | | |
1635 | * |------------------------------------------+ block 1 | |
1636 | * | sub header (struct kdump_sub_header) | | |
1637 | * |------------------------------------------+ block 2 | |
1638 | * | 1st-dump_bitmap | | |
1639 | * |------------------------------------------+ block 2 + X blocks | |
1640 | * | 2nd-dump_bitmap | (aligned by block) | |
1641 | * |------------------------------------------+ block 2 + 2 * X blocks | |
1642 | * | page desc for pfn 0 (struct page_desc) | (aligned by block) | |
1643 | * | page desc for pfn 1 (struct page_desc) | | |
1644 | * | : | | |
1645 | * |------------------------------------------| (not aligned by block) | |
1646 | * | page data (pfn 0) | | |
1647 | * | page data (pfn 1) | | |
1648 | * | : | | |
1649 | * +------------------------------------------+ | |
1650 | */ | |
1651 | ||
1652 | ret = write_start_flat_header(s->fd); | |
1653 | if (ret < 0) { | |
e3517a52 | 1654 | error_setg(errp, "dump: failed to write start flat header"); |
4c7e251a | 1655 | return; |
b53ccc30 QN |
1656 | } |
1657 | ||
86a518bb JF |
1658 | write_dump_header(s, errp); |
1659 | if (*errp) { | |
4c7e251a | 1660 | return; |
b53ccc30 QN |
1661 | } |
1662 | ||
86a518bb JF |
1663 | write_dump_bitmap(s, errp); |
1664 | if (*errp) { | |
4c7e251a | 1665 | return; |
b53ccc30 QN |
1666 | } |
1667 | ||
86a518bb JF |
1668 | write_dump_pages(s, errp); |
1669 | if (*errp) { | |
4c7e251a | 1670 | return; |
b53ccc30 QN |
1671 | } |
1672 | ||
1673 | ret = write_end_flat_header(s->fd); | |
1674 | if (ret < 0) { | |
e3517a52 | 1675 | error_setg(errp, "dump: failed to write end flat header"); |
4c7e251a | 1676 | return; |
b53ccc30 | 1677 | } |
b53ccc30 QN |
1678 | } |
1679 | ||
0c2994ac | 1680 | static int validate_start_block(DumpState *s) |
783e9b48 | 1681 | { |
56c4bfb3 | 1682 | GuestPhysBlock *block; |
783e9b48 | 1683 | |
dddf725f | 1684 | if (!dump_has_filter(s)) { |
783e9b48 WC |
1685 | return 0; |
1686 | } | |
1687 | ||
56c4bfb3 | 1688 | QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) { |
0c2994ac | 1689 | /* This block is out of the range */ |
dddf725f JF |
1690 | if (block->target_start >= s->filter_area_begin + s->filter_area_length || |
1691 | block->target_end <= s->filter_area_begin) { | |
783e9b48 WC |
1692 | continue; |
1693 | } | |
0c2994ac JF |
1694 | return 0; |
1695 | } | |
783e9b48 WC |
1696 | |
1697 | return -1; | |
1698 | } | |
1699 | ||
7aad248d QN |
1700 | static void get_max_mapnr(DumpState *s) |
1701 | { | |
1702 | GuestPhysBlock *last_block; | |
1703 | ||
eae3eb3e | 1704 | last_block = QTAILQ_LAST(&s->guest_phys_blocks.head); |
8161befd | 1705 | s->max_mapnr = dump_paddr_to_pfn(s, last_block->target_end); |
7aad248d QN |
1706 | } |
1707 | ||
baf28f57 PX |
1708 | static DumpState dump_state_global = { .status = DUMP_STATUS_NONE }; |
1709 | ||
1710 | static void dump_state_prepare(DumpState *s) | |
1711 | { | |
1712 | /* zero the struct, setting status to active */ | |
1713 | *s = (DumpState) { .status = DUMP_STATUS_ACTIVE }; | |
1714 | } | |
1715 | ||
544803c7 | 1716 | bool qemu_system_dump_in_progress(void) |
65d64f36 PX |
1717 | { |
1718 | DumpState *state = &dump_state_global; | |
d73415a3 | 1719 | return (qatomic_read(&state->status) == DUMP_STATUS_ACTIVE); |
65d64f36 PX |
1720 | } |
1721 | ||
c370d530 JF |
1722 | /* |
1723 | * calculate total size of memory to be dumped (taking filter into | |
1724 | * account.) | |
1725 | */ | |
2264c2c9 PX |
1726 | static int64_t dump_calculate_size(DumpState *s) |
1727 | { | |
1728 | GuestPhysBlock *block; | |
c370d530 | 1729 | int64_t total = 0; |
2264c2c9 PX |
1730 | |
1731 | QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) { | |
c370d530 JF |
1732 | total += dump_filtered_memblock_size(block, |
1733 | s->filter_area_begin, | |
1734 | s->filter_area_length); | |
2264c2c9 PX |
1735 | } |
1736 | ||
1737 | return total; | |
1738 | } | |
1739 | ||
d9feb517 MAL |
1740 | static void vmcoreinfo_update_phys_base(DumpState *s) |
1741 | { | |
1742 | uint64_t size, note_head_size, name_size, phys_base; | |
1743 | char **lines; | |
1744 | uint8_t *vmci; | |
1745 | size_t i; | |
1746 | ||
1747 | if (!note_name_equal(s, s->guest_note, "VMCOREINFO")) { | |
1748 | return; | |
1749 | } | |
1750 | ||
1751 | get_note_sizes(s, s->guest_note, ¬e_head_size, &name_size, &size); | |
1752 | note_head_size = ROUND_UP(note_head_size, 4); | |
1753 | ||
1754 | vmci = s->guest_note + note_head_size + ROUND_UP(name_size, 4); | |
1755 | *(vmci + size) = '\0'; | |
1756 | ||
1757 | lines = g_strsplit((char *)vmci, "\n", -1); | |
1758 | for (i = 0; lines[i]; i++) { | |
68cbecfd WH |
1759 | const char *prefix = NULL; |
1760 | ||
1761 | if (s->dump_info.d_machine == EM_X86_64) { | |
1762 | prefix = "NUMBER(phys_base)="; | |
1763 | } else if (s->dump_info.d_machine == EM_AARCH64) { | |
1764 | prefix = "NUMBER(PHYS_OFFSET)="; | |
1765 | } | |
1766 | ||
1767 | if (prefix && g_str_has_prefix(lines[i], prefix)) { | |
1768 | if (qemu_strtou64(lines[i] + strlen(prefix), NULL, 16, | |
d9feb517 | 1769 | &phys_base) < 0) { |
68cbecfd | 1770 | warn_report("Failed to read %s", prefix); |
d9feb517 MAL |
1771 | } else { |
1772 | s->dump_info.phys_base = phys_base; | |
1773 | } | |
1774 | break; | |
1775 | } | |
1776 | } | |
1777 | ||
1778 | g_strfreev(lines); | |
1779 | } | |
1780 | ||
4c7e251a HZ |
1781 | static void dump_init(DumpState *s, int fd, bool has_format, |
1782 | DumpGuestMemoryFormat format, bool paging, bool has_filter, | |
1783 | int64_t begin, int64_t length, Error **errp) | |
783e9b48 | 1784 | { |
86a518bb | 1785 | ERRP_GUARD(); |
903ef734 | 1786 | VMCoreInfoState *vmci = vmcoreinfo_find(); |
182735ef | 1787 | CPUState *cpu; |
783e9b48 WC |
1788 | int nr_cpus; |
1789 | int ret; | |
1790 | ||
ca1fc8c9 PX |
1791 | s->has_format = has_format; |
1792 | s->format = format; | |
2264c2c9 | 1793 | s->written_size = 0; |
ca1fc8c9 | 1794 | |
b53ccc30 QN |
1795 | /* kdump-compressed is conflict with paging and filter */ |
1796 | if (has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) { | |
1797 | assert(!paging && !has_filter); | |
1798 | } | |
1799 | ||
783e9b48 WC |
1800 | if (runstate_is_running()) { |
1801 | vm_stop(RUN_STATE_SAVE_VM); | |
1802 | s->resume = true; | |
1803 | } else { | |
1804 | s->resume = false; | |
1805 | } | |
1806 | ||
5ee163e8 LE |
1807 | /* If we use KVM, we should synchronize the registers before we get dump |
1808 | * info or physmap info. | |
1809 | */ | |
1810 | cpu_synchronize_all_states(); | |
1811 | nr_cpus = 0; | |
bdc44640 | 1812 | CPU_FOREACH(cpu) { |
5ee163e8 LE |
1813 | nr_cpus++; |
1814 | } | |
1815 | ||
783e9b48 | 1816 | s->fd = fd; |
dddf725f JF |
1817 | if (has_filter && !length) { |
1818 | error_setg(errp, QERR_INVALID_PARAMETER, "length"); | |
1819 | goto cleanup; | |
1820 | } | |
1821 | s->filter_area_begin = begin; | |
1822 | s->filter_area_length = length; | |
5ee163e8 | 1823 | |
9b72224f JF |
1824 | /* First index is 0, it's the special null name */ |
1825 | s->string_table_buf = g_array_new(FALSE, TRUE, 1); | |
1826 | /* | |
1827 | * Allocate the null name, due to the clearing option set to true | |
1828 | * it will be 0. | |
1829 | */ | |
1830 | g_array_set_size(s->string_table_buf, 1); | |
1831 | ||
2928207a CG |
1832 | memory_mapping_list_init(&s->list); |
1833 | ||
5ee163e8 | 1834 | guest_phys_blocks_init(&s->guest_phys_blocks); |
c5d7f60f | 1835 | guest_phys_blocks_append(&s->guest_phys_blocks); |
2264c2c9 PX |
1836 | s->total_size = dump_calculate_size(s); |
1837 | #ifdef DEBUG_DUMP_GUEST_MEMORY | |
1838 | fprintf(stderr, "DUMP: total memory to dump: %lu\n", s->total_size); | |
1839 | #endif | |
5ee163e8 | 1840 | |
d1e6994a CH |
1841 | /* it does not make sense to dump non-existent memory */ |
1842 | if (!s->total_size) { | |
1843 | error_setg(errp, "dump: no guest memory to dump"); | |
1844 | goto cleanup; | |
1845 | } | |
1846 | ||
0c2994ac JF |
1847 | /* Is the filter filtering everything? */ |
1848 | if (validate_start_block(s) == -1) { | |
c6bd8c70 | 1849 | error_setg(errp, QERR_INVALID_PARAMETER, "begin"); |
783e9b48 WC |
1850 | goto cleanup; |
1851 | } | |
1852 | ||
5ee163e8 | 1853 | /* get dump info: endian, class and architecture. |
783e9b48 WC |
1854 | * If the target architecture is not supported, cpu_get_dump_info() will |
1855 | * return -1. | |
783e9b48 | 1856 | */ |
56c4bfb3 | 1857 | ret = cpu_get_dump_info(&s->dump_info, &s->guest_phys_blocks); |
783e9b48 | 1858 | if (ret < 0) { |
c6bd8c70 | 1859 | error_setg(errp, QERR_UNSUPPORTED); |
783e9b48 WC |
1860 | goto cleanup; |
1861 | } | |
1862 | ||
8161befd AJ |
1863 | if (!s->dump_info.page_size) { |
1864 | s->dump_info.page_size = TARGET_PAGE_SIZE; | |
1865 | } | |
1866 | ||
4720bd05 PB |
1867 | s->note_size = cpu_get_note_size(s->dump_info.d_class, |
1868 | s->dump_info.d_machine, nr_cpus); | |
bb6b6843 | 1869 | if (s->note_size < 0) { |
c6bd8c70 | 1870 | error_setg(errp, QERR_UNSUPPORTED); |
4720bd05 PB |
1871 | goto cleanup; |
1872 | } | |
1873 | ||
903ef734 | 1874 | /* |
d9feb517 MAL |
1875 | * The goal of this block is to (a) update the previously guessed |
1876 | * phys_base, (b) copy the guest note out of the guest. | |
1877 | * Failure to do so is not fatal for dumping. | |
903ef734 MAL |
1878 | */ |
1879 | if (vmci) { | |
1880 | uint64_t addr, note_head_size, name_size, desc_size; | |
1881 | uint32_t size; | |
1882 | uint16_t format; | |
1883 | ||
05bbaa50 JF |
1884 | note_head_size = dump_is_64bit(s) ? |
1885 | sizeof(Elf64_Nhdr) : sizeof(Elf32_Nhdr); | |
903ef734 MAL |
1886 | |
1887 | format = le16_to_cpu(vmci->vmcoreinfo.guest_format); | |
1888 | size = le32_to_cpu(vmci->vmcoreinfo.size); | |
1889 | addr = le64_to_cpu(vmci->vmcoreinfo.paddr); | |
1890 | if (!vmci->has_vmcoreinfo) { | |
1891 | warn_report("guest note is not present"); | |
1892 | } else if (size < note_head_size || size > MAX_GUEST_NOTE_SIZE) { | |
1893 | warn_report("guest note size is invalid: %" PRIu32, size); | |
5be5df72 | 1894 | } else if (format != FW_CFG_VMCOREINFO_FORMAT_ELF) { |
903ef734 MAL |
1895 | warn_report("guest note format is unsupported: %" PRIu16, format); |
1896 | } else { | |
1897 | s->guest_note = g_malloc(size + 1); /* +1 for adding \0 */ | |
1898 | cpu_physical_memory_read(addr, s->guest_note, size); | |
1899 | ||
1900 | get_note_sizes(s, s->guest_note, NULL, &name_size, &desc_size); | |
1901 | s->guest_note_size = ELF_NOTE_SIZE(note_head_size, name_size, | |
1902 | desc_size); | |
1903 | if (name_size > MAX_GUEST_NOTE_SIZE || | |
1904 | desc_size > MAX_GUEST_NOTE_SIZE || | |
1905 | s->guest_note_size > size) { | |
1906 | warn_report("Invalid guest note header"); | |
1907 | g_free(s->guest_note); | |
1908 | s->guest_note = NULL; | |
1909 | } else { | |
d9feb517 | 1910 | vmcoreinfo_update_phys_base(s); |
903ef734 MAL |
1911 | s->note_size += s->guest_note_size; |
1912 | } | |
1913 | } | |
1914 | } | |
1915 | ||
783e9b48 | 1916 | /* get memory mapping */ |
783e9b48 | 1917 | if (paging) { |
86a518bb JF |
1918 | qemu_get_guest_memory_mapping(&s->list, &s->guest_phys_blocks, errp); |
1919 | if (*errp) { | |
11ed09cf AF |
1920 | goto cleanup; |
1921 | } | |
783e9b48 | 1922 | } else { |
56c4bfb3 | 1923 | qemu_get_guest_simple_memory_mapping(&s->list, &s->guest_phys_blocks); |
783e9b48 WC |
1924 | } |
1925 | ||
7aad248d | 1926 | s->nr_cpus = nr_cpus; |
7aad248d QN |
1927 | |
1928 | get_max_mapnr(s); | |
1929 | ||
1930 | uint64_t tmp; | |
8161befd AJ |
1931 | tmp = DIV_ROUND_UP(DIV_ROUND_UP(s->max_mapnr, CHAR_BIT), |
1932 | s->dump_info.page_size); | |
1933 | s->len_dump_bitmap = tmp * s->dump_info.page_size; | |
7aad248d | 1934 | |
b53ccc30 QN |
1935 | /* init for kdump-compressed format */ |
1936 | if (has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) { | |
1937 | switch (format) { | |
1938 | case DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB: | |
1939 | s->flag_compress = DUMP_DH_COMPRESSED_ZLIB; | |
1940 | break; | |
1941 | ||
1942 | case DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO: | |
c998acb0 LE |
1943 | #ifdef CONFIG_LZO |
1944 | if (lzo_init() != LZO_E_OK) { | |
1945 | error_setg(errp, "failed to initialize the LZO library"); | |
1946 | goto cleanup; | |
1947 | } | |
1948 | #endif | |
b53ccc30 QN |
1949 | s->flag_compress = DUMP_DH_COMPRESSED_LZO; |
1950 | break; | |
1951 | ||
1952 | case DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY: | |
1953 | s->flag_compress = DUMP_DH_COMPRESSED_SNAPPY; | |
1954 | break; | |
1955 | ||
1956 | default: | |
1957 | s->flag_compress = 0; | |
1958 | } | |
1959 | ||
4c7e251a | 1960 | return; |
b53ccc30 QN |
1961 | } |
1962 | ||
dddf725f JF |
1963 | if (dump_has_filter(s)) { |
1964 | memory_mapping_filter(&s->list, s->filter_area_begin, s->filter_area_length); | |
783e9b48 WC |
1965 | } |
1966 | ||
1967 | /* | |
9b72224f JF |
1968 | * The first section header is always a special one in which most |
1969 | * fields are 0. The section header string table is also always | |
1970 | * set. | |
1971 | */ | |
1972 | s->shdr_num = 2; | |
1973 | ||
1974 | /* | |
1975 | * Adds the number of architecture sections to shdr_num and sets | |
1976 | * elf_section_data_size so we know the offsets and sizes of all | |
1977 | * parts. | |
1978 | */ | |
1979 | if (s->dump_info.arch_sections_add_fn) { | |
1980 | s->dump_info.arch_sections_add_fn(s); | |
1981 | } | |
1982 | ||
1983 | /* | |
1984 | * calculate shdr_num so we know the offsets and sizes of all | |
1985 | * parts. | |
1986 | * Calculate phdr_num | |
783e9b48 | 1987 | * |
9b72224f JF |
1988 | * The absolute maximum amount of phdrs is UINT32_MAX - 1 as |
1989 | * sh_info is 32 bit. There's special handling once we go over | |
1990 | * UINT16_MAX - 1 but that is handled in the ehdr and section | |
1991 | * code. | |
783e9b48 | 1992 | */ |
9b72224f JF |
1993 | s->phdr_num = 1; /* Reserve PT_NOTE */ |
1994 | if (s->list.num <= UINT32_MAX - 1) { | |
783e9b48 | 1995 | s->phdr_num += s->list.num; |
783e9b48 | 1996 | } else { |
9b72224f | 1997 | s->phdr_num = UINT32_MAX; |
783e9b48 WC |
1998 | } |
1999 | ||
9b72224f JF |
2000 | /* |
2001 | * Now that the number of section and program headers is known we | |
2002 | * can calculate the offsets of the headers and data. | |
2003 | */ | |
05bbaa50 | 2004 | if (dump_is_64bit(s)) { |
cb415fd6 JF |
2005 | s->shdr_offset = sizeof(Elf64_Ehdr); |
2006 | s->phdr_offset = s->shdr_offset + sizeof(Elf64_Shdr) * s->shdr_num; | |
2007 | s->note_offset = s->phdr_offset + sizeof(Elf64_Phdr) * s->phdr_num; | |
783e9b48 | 2008 | } else { |
cb415fd6 JF |
2009 | s->shdr_offset = sizeof(Elf32_Ehdr); |
2010 | s->phdr_offset = s->shdr_offset + sizeof(Elf32_Shdr) * s->shdr_num; | |
2011 | s->note_offset = s->phdr_offset + sizeof(Elf32_Phdr) * s->phdr_num; | |
783e9b48 | 2012 | } |
13fd417d JF |
2013 | s->memory_offset = s->note_offset + s->note_size; |
2014 | s->section_offset = s->memory_offset + s->total_size; | |
783e9b48 | 2015 | |
4c7e251a | 2016 | return; |
783e9b48 WC |
2017 | |
2018 | cleanup: | |
2928207a | 2019 | dump_cleanup(s); |
783e9b48 WC |
2020 | } |
2021 | ||
ca1fc8c9 PX |
2022 | /* this operation might be time consuming. */ |
2023 | static void dump_process(DumpState *s, Error **errp) | |
2024 | { | |
86a518bb | 2025 | ERRP_GUARD(); |
d42a0d14 | 2026 | DumpQueryResult *result = NULL; |
ca1fc8c9 | 2027 | |
2da91b54 VP |
2028 | if (s->has_format && s->format == DUMP_GUEST_MEMORY_FORMAT_WIN_DMP) { |
2029 | #ifdef TARGET_X86_64 | |
86a518bb | 2030 | create_win_dump(s, errp); |
2da91b54 VP |
2031 | #endif |
2032 | } else if (s->has_format && s->format != DUMP_GUEST_MEMORY_FORMAT_ELF) { | |
86a518bb | 2033 | create_kdump_vmcore(s, errp); |
ca1fc8c9 | 2034 | } else { |
86a518bb | 2035 | create_vmcore(s, errp); |
ca1fc8c9 PX |
2036 | } |
2037 | ||
39ba2ea6 PX |
2038 | /* make sure status is written after written_size updates */ |
2039 | smp_wmb(); | |
d73415a3 | 2040 | qatomic_set(&s->status, |
86a518bb | 2041 | (*errp ? DUMP_STATUS_FAILED : DUMP_STATUS_COMPLETED)); |
ca1fc8c9 | 2042 | |
d42a0d14 PX |
2043 | /* send DUMP_COMPLETED message (unconditionally) */ |
2044 | result = qmp_query_dump(NULL); | |
2045 | /* should never fail */ | |
2046 | assert(result); | |
d4f8bdc7 MA |
2047 | qapi_event_send_dump_completed(result, |
2048 | *errp ? error_get_pretty(*errp) : NULL); | |
d42a0d14 PX |
2049 | qapi_free_DumpQueryResult(result); |
2050 | ||
ca1fc8c9 PX |
2051 | dump_cleanup(s); |
2052 | } | |
2053 | ||
1fbeff72 PX |
2054 | static void *dump_thread(void *data) |
2055 | { | |
1fbeff72 | 2056 | DumpState *s = (DumpState *)data; |
c3a8fe33 | 2057 | dump_process(s, NULL); |
1fbeff72 PX |
2058 | return NULL; |
2059 | } | |
2060 | ||
39ba2ea6 PX |
2061 | DumpQueryResult *qmp_query_dump(Error **errp) |
2062 | { | |
2063 | DumpQueryResult *result = g_new(DumpQueryResult, 1); | |
2064 | DumpState *state = &dump_state_global; | |
d73415a3 | 2065 | result->status = qatomic_read(&state->status); |
39ba2ea6 PX |
2066 | /* make sure we are reading status and written_size in order */ |
2067 | smp_rmb(); | |
2068 | result->completed = state->written_size; | |
2069 | result->total = state->total_size; | |
2070 | return result; | |
2071 | } | |
2072 | ||
228de9cf PX |
2073 | void qmp_dump_guest_memory(bool paging, const char *file, |
2074 | bool has_detach, bool detach, | |
2075 | bool has_begin, int64_t begin, bool has_length, | |
b53ccc30 QN |
2076 | int64_t length, bool has_format, |
2077 | DumpGuestMemoryFormat format, Error **errp) | |
783e9b48 | 2078 | { |
86a518bb | 2079 | ERRP_GUARD(); |
783e9b48 WC |
2080 | const char *p; |
2081 | int fd = -1; | |
2082 | DumpState *s; | |
1fbeff72 | 2083 | bool detach_p = false; |
783e9b48 | 2084 | |
63e27f28 PX |
2085 | if (runstate_check(RUN_STATE_INMIGRATE)) { |
2086 | error_setg(errp, "Dump not allowed during incoming migration."); | |
2087 | return; | |
2088 | } | |
2089 | ||
65d64f36 PX |
2090 | /* if there is a dump in background, we should wait until the dump |
2091 | * finished */ | |
544803c7 | 2092 | if (qemu_system_dump_in_progress()) { |
65d64f36 PX |
2093 | error_setg(errp, "There is a dump in process, please wait."); |
2094 | return; | |
2095 | } | |
2096 | ||
b53ccc30 QN |
2097 | /* |
2098 | * kdump-compressed format need the whole memory dumped, so paging or | |
2099 | * filter is not supported here. | |
2100 | */ | |
2101 | if ((has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) && | |
2102 | (paging || has_begin || has_length)) { | |
2103 | error_setg(errp, "kdump-compressed format doesn't support paging or " | |
2104 | "filter"); | |
2105 | return; | |
2106 | } | |
783e9b48 | 2107 | if (has_begin && !has_length) { |
c6bd8c70 | 2108 | error_setg(errp, QERR_MISSING_PARAMETER, "length"); |
783e9b48 WC |
2109 | return; |
2110 | } | |
2111 | if (!has_begin && has_length) { | |
c6bd8c70 | 2112 | error_setg(errp, QERR_MISSING_PARAMETER, "begin"); |
783e9b48 WC |
2113 | return; |
2114 | } | |
1fbeff72 PX |
2115 | if (has_detach) { |
2116 | detach_p = detach; | |
2117 | } | |
783e9b48 | 2118 | |
b53ccc30 QN |
2119 | /* check whether lzo/snappy is supported */ |
2120 | #ifndef CONFIG_LZO | |
2121 | if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO) { | |
2122 | error_setg(errp, "kdump-lzo is not available now"); | |
2123 | return; | |
2124 | } | |
2125 | #endif | |
2126 | ||
2127 | #ifndef CONFIG_SNAPPY | |
2128 | if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY) { | |
2129 | error_setg(errp, "kdump-snappy is not available now"); | |
2130 | return; | |
2131 | } | |
2132 | #endif | |
2133 | ||
2da91b54 VP |
2134 | #ifndef TARGET_X86_64 |
2135 | if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_WIN_DMP) { | |
2136 | error_setg(errp, "Windows dump is only available for x86-64"); | |
2137 | return; | |
2138 | } | |
2139 | #endif | |
2140 | ||
783e9b48 WC |
2141 | #if !defined(WIN32) |
2142 | if (strstart(file, "fd:", &p)) { | |
947e4744 | 2143 | fd = monitor_get_fd(monitor_cur(), p, errp); |
783e9b48 | 2144 | if (fd == -1) { |
783e9b48 WC |
2145 | return; |
2146 | } | |
2147 | } | |
2148 | #endif | |
2149 | ||
2150 | if (strstart(file, "file:", &p)) { | |
448058aa | 2151 | fd = qemu_open_old(p, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, S_IRUSR); |
783e9b48 | 2152 | if (fd < 0) { |
7581766b | 2153 | error_setg_file_open(errp, errno, p); |
783e9b48 WC |
2154 | return; |
2155 | } | |
2156 | } | |
2157 | ||
2158 | if (fd == -1) { | |
c6bd8c70 | 2159 | error_setg(errp, QERR_INVALID_PARAMETER, "protocol"); |
783e9b48 WC |
2160 | return; |
2161 | } | |
2162 | ||
b7bc6b18 PX |
2163 | if (!dump_migration_blocker) { |
2164 | error_setg(&dump_migration_blocker, | |
2165 | "Live migration disabled: dump-guest-memory in progress"); | |
2166 | } | |
2167 | ||
2168 | /* | |
2169 | * Allows even for -only-migratable, but forbid migration during the | |
2170 | * process of dump guest memory. | |
2171 | */ | |
2172 | if (migrate_add_blocker_internal(dump_migration_blocker, errp)) { | |
2173 | /* Remember to release the fd before passing it over to dump state */ | |
2174 | close(fd); | |
2175 | return; | |
2176 | } | |
2177 | ||
baf28f57 PX |
2178 | s = &dump_state_global; |
2179 | dump_state_prepare(s); | |
783e9b48 | 2180 | |
4c7e251a | 2181 | dump_init(s, fd, has_format, format, paging, has_begin, |
86a518bb JF |
2182 | begin, length, errp); |
2183 | if (*errp) { | |
d73415a3 | 2184 | qatomic_set(&s->status, DUMP_STATUS_FAILED); |
783e9b48 WC |
2185 | return; |
2186 | } | |
2187 | ||
1fbeff72 PX |
2188 | if (detach_p) { |
2189 | /* detached dump */ | |
6796b400 | 2190 | s->detached = true; |
1fbeff72 PX |
2191 | qemu_thread_create(&s->dump_thread, "dump_thread", dump_thread, |
2192 | s, QEMU_THREAD_DETACHED); | |
2193 | } else { | |
2194 | /* sync dump */ | |
2195 | dump_process(s, errp); | |
2196 | } | |
783e9b48 | 2197 | } |
7d6dc7f3 QN |
2198 | |
2199 | DumpGuestMemoryCapability *qmp_query_dump_guest_memory_capability(Error **errp) | |
2200 | { | |
7d6dc7f3 | 2201 | DumpGuestMemoryCapability *cap = |
b21e2380 | 2202 | g_new0(DumpGuestMemoryCapability, 1); |
95b3a8c8 | 2203 | DumpGuestMemoryFormatList **tail = &cap->formats; |
7d6dc7f3 QN |
2204 | |
2205 | /* elf is always available */ | |
95b3a8c8 | 2206 | QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_ELF); |
7d6dc7f3 QN |
2207 | |
2208 | /* kdump-zlib is always available */ | |
95b3a8c8 | 2209 | QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB); |
7d6dc7f3 QN |
2210 | |
2211 | /* add new item if kdump-lzo is available */ | |
2212 | #ifdef CONFIG_LZO | |
95b3a8c8 | 2213 | QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO); |
7d6dc7f3 QN |
2214 | #endif |
2215 | ||
2216 | /* add new item if kdump-snappy is available */ | |
2217 | #ifdef CONFIG_SNAPPY | |
95b3a8c8 | 2218 | QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY); |
7d6dc7f3 QN |
2219 | #endif |
2220 | ||
2da91b54 VP |
2221 | /* Windows dump is available only if target is x86_64 */ |
2222 | #ifdef TARGET_X86_64 | |
95b3a8c8 | 2223 | QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_WIN_DMP); |
2da91b54 VP |
2224 | #endif |
2225 | ||
7d6dc7f3 QN |
2226 | return cap; |
2227 | } |