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