Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 1 | /* |
| 2 | * QEMU dump |
| 3 | * |
| 4 | * Copyright Fujitsu, Corp. 2011, 2012 |
| 5 | * |
| 6 | * Authors: |
| 7 | * Wen Congyang <wency@cn.fujitsu.com> |
| 8 | * |
Stefan Weil | 352666e | 2012-06-10 19:34:04 +0000 | [diff] [blame] | 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. |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 11 | * |
| 12 | */ |
| 13 | |
Peter Maydell | d38ea87 | 2016-01-29 17:50:05 +0000 | [diff] [blame] | 14 | #include "qemu/osdep.h" |
Veronia Bahaa | f348b6d | 2016-03-20 19:16:19 +0200 | [diff] [blame] | 15 | #include "qemu/cutils.h" |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 16 | #include "elf.h" |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 17 | #include "cpu.h" |
Paolo Bonzini | 022c62c | 2012-12-17 18:19:49 +0100 | [diff] [blame] | 18 | #include "exec/cpu-all.h" |
| 19 | #include "exec/hwaddr.h" |
Paolo Bonzini | 83c9089 | 2012-12-17 18:19:49 +0100 | [diff] [blame] | 20 | #include "monitor/monitor.h" |
Paolo Bonzini | 9c17d61 | 2012-12-17 18:20:04 +0100 | [diff] [blame] | 21 | #include "sysemu/kvm.h" |
| 22 | #include "sysemu/dump.h" |
| 23 | #include "sysemu/sysemu.h" |
| 24 | #include "sysemu/memory_mapping.h" |
Andreas Färber | 1b3509c | 2013-06-09 16:48:29 +0200 | [diff] [blame] | 25 | #include "sysemu/cpus.h" |
Markus Armbruster | cc7a8ea | 2015-03-17 17:22:46 +0100 | [diff] [blame] | 26 | #include "qapi/qmp/qerror.h" |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 27 | #include "qmp-commands.h" |
Peter Xu | d42a0d1 | 2016-02-18 13:16:56 +0800 | [diff] [blame] | 28 | #include "qapi-event.h" |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 29 | |
qiaonuohan | d12f57e | 2014-02-18 14:11:35 +0800 | [diff] [blame] | 30 | #include <zlib.h> |
| 31 | #ifdef CONFIG_LZO |
| 32 | #include <lzo/lzo1x.h> |
| 33 | #endif |
| 34 | #ifdef CONFIG_SNAPPY |
| 35 | #include <snappy-c.h> |
| 36 | #endif |
qiaonuohan | 4ab23a9 | 2014-02-18 14:11:37 +0800 | [diff] [blame] | 37 | #ifndef ELF_MACHINE_UNAME |
| 38 | #define ELF_MACHINE_UNAME "Unknown" |
| 39 | #endif |
qiaonuohan | d12f57e | 2014-02-18 14:11:35 +0800 | [diff] [blame] | 40 | |
Bharata B Rao | acb0ef5 | 2014-05-19 19:57:44 +0200 | [diff] [blame] | 41 | uint16_t cpu_to_dump16(DumpState *s, uint16_t val) |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 42 | { |
Bharata B Rao | acb0ef5 | 2014-05-19 19:57:44 +0200 | [diff] [blame] | 43 | if (s->dump_info.d_endian == ELFDATA2LSB) { |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 44 | val = cpu_to_le16(val); |
| 45 | } else { |
| 46 | val = cpu_to_be16(val); |
| 47 | } |
| 48 | |
| 49 | return val; |
| 50 | } |
| 51 | |
Bharata B Rao | acb0ef5 | 2014-05-19 19:57:44 +0200 | [diff] [blame] | 52 | uint32_t cpu_to_dump32(DumpState *s, uint32_t val) |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 53 | { |
Bharata B Rao | acb0ef5 | 2014-05-19 19:57:44 +0200 | [diff] [blame] | 54 | if (s->dump_info.d_endian == ELFDATA2LSB) { |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 55 | val = cpu_to_le32(val); |
| 56 | } else { |
| 57 | val = cpu_to_be32(val); |
| 58 | } |
| 59 | |
| 60 | return val; |
| 61 | } |
| 62 | |
Bharata B Rao | acb0ef5 | 2014-05-19 19:57:44 +0200 | [diff] [blame] | 63 | uint64_t cpu_to_dump64(DumpState *s, uint64_t val) |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 64 | { |
Bharata B Rao | acb0ef5 | 2014-05-19 19:57:44 +0200 | [diff] [blame] | 65 | if (s->dump_info.d_endian == ELFDATA2LSB) { |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 66 | val = cpu_to_le64(val); |
| 67 | } else { |
| 68 | val = cpu_to_be64(val); |
| 69 | } |
| 70 | |
| 71 | return val; |
| 72 | } |
| 73 | |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 74 | static int dump_cleanup(DumpState *s) |
| 75 | { |
Laszlo Ersek | 5ee163e | 2013-08-06 12:37:09 +0200 | [diff] [blame] | 76 | guest_phys_blocks_free(&s->guest_phys_blocks); |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 77 | memory_mapping_list_free(&s->list); |
Chen Gang | 2928207 | 2014-08-03 23:28:56 +0800 | [diff] [blame] | 78 | close(s->fd); |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 79 | if (s->resume) { |
| 80 | vm_start(); |
| 81 | } |
| 82 | |
Chen Gang | 2928207 | 2014-08-03 23:28:56 +0800 | [diff] [blame] | 83 | return 0; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 84 | } |
| 85 | |
qiaonuohan | b5ba1cc | 2014-02-18 14:11:25 +0800 | [diff] [blame] | 86 | static int fd_write_vmcore(const void *buf, size_t size, void *opaque) |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 87 | { |
| 88 | DumpState *s = opaque; |
Luiz Capitulino | 2f61652 | 2012-09-21 13:17:55 -0300 | [diff] [blame] | 89 | size_t written_size; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 90 | |
Luiz Capitulino | 2f61652 | 2012-09-21 13:17:55 -0300 | [diff] [blame] | 91 | written_size = qemu_write_full(s->fd, buf, size); |
| 92 | if (written_size != size) { |
| 93 | return -1; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | return 0; |
| 97 | } |
| 98 | |
zhanghailiang | 4c7e251 | 2014-10-09 14:13:11 +0800 | [diff] [blame] | 99 | static void write_elf64_header(DumpState *s, Error **errp) |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 100 | { |
| 101 | Elf64_Ehdr elf_header; |
| 102 | int ret; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 103 | |
| 104 | memset(&elf_header, 0, sizeof(Elf64_Ehdr)); |
| 105 | memcpy(&elf_header, ELFMAG, SELFMAG); |
| 106 | elf_header.e_ident[EI_CLASS] = ELFCLASS64; |
| 107 | elf_header.e_ident[EI_DATA] = s->dump_info.d_endian; |
| 108 | elf_header.e_ident[EI_VERSION] = EV_CURRENT; |
Bharata B Rao | acb0ef5 | 2014-05-19 19:57:44 +0200 | [diff] [blame] | 109 | elf_header.e_type = cpu_to_dump16(s, ET_CORE); |
| 110 | elf_header.e_machine = cpu_to_dump16(s, s->dump_info.d_machine); |
| 111 | elf_header.e_version = cpu_to_dump32(s, EV_CURRENT); |
| 112 | elf_header.e_ehsize = cpu_to_dump16(s, sizeof(elf_header)); |
| 113 | elf_header.e_phoff = cpu_to_dump64(s, sizeof(Elf64_Ehdr)); |
| 114 | elf_header.e_phentsize = cpu_to_dump16(s, sizeof(Elf64_Phdr)); |
| 115 | elf_header.e_phnum = cpu_to_dump16(s, s->phdr_num); |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 116 | if (s->have_section) { |
| 117 | uint64_t shoff = sizeof(Elf64_Ehdr) + sizeof(Elf64_Phdr) * s->sh_info; |
| 118 | |
Bharata B Rao | acb0ef5 | 2014-05-19 19:57:44 +0200 | [diff] [blame] | 119 | elf_header.e_shoff = cpu_to_dump64(s, shoff); |
| 120 | elf_header.e_shentsize = cpu_to_dump16(s, sizeof(Elf64_Shdr)); |
| 121 | elf_header.e_shnum = cpu_to_dump16(s, 1); |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | ret = fd_write_vmcore(&elf_header, sizeof(elf_header), s); |
| 125 | if (ret < 0) { |
Peter Xu | e3517a5 | 2016-02-18 13:16:46 +0800 | [diff] [blame] | 126 | error_setg(errp, "dump: failed to write elf header"); |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 127 | } |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 128 | } |
| 129 | |
zhanghailiang | 4c7e251 | 2014-10-09 14:13:11 +0800 | [diff] [blame] | 130 | static void write_elf32_header(DumpState *s, Error **errp) |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 131 | { |
| 132 | Elf32_Ehdr elf_header; |
| 133 | int ret; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 134 | |
| 135 | memset(&elf_header, 0, sizeof(Elf32_Ehdr)); |
| 136 | memcpy(&elf_header, ELFMAG, SELFMAG); |
| 137 | elf_header.e_ident[EI_CLASS] = ELFCLASS32; |
Bharata B Rao | acb0ef5 | 2014-05-19 19:57:44 +0200 | [diff] [blame] | 138 | elf_header.e_ident[EI_DATA] = s->dump_info.d_endian; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 139 | elf_header.e_ident[EI_VERSION] = EV_CURRENT; |
Bharata B Rao | acb0ef5 | 2014-05-19 19:57:44 +0200 | [diff] [blame] | 140 | elf_header.e_type = cpu_to_dump16(s, ET_CORE); |
| 141 | elf_header.e_machine = cpu_to_dump16(s, s->dump_info.d_machine); |
| 142 | elf_header.e_version = cpu_to_dump32(s, EV_CURRENT); |
| 143 | elf_header.e_ehsize = cpu_to_dump16(s, sizeof(elf_header)); |
| 144 | elf_header.e_phoff = cpu_to_dump32(s, sizeof(Elf32_Ehdr)); |
| 145 | elf_header.e_phentsize = cpu_to_dump16(s, sizeof(Elf32_Phdr)); |
| 146 | elf_header.e_phnum = cpu_to_dump16(s, s->phdr_num); |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 147 | if (s->have_section) { |
| 148 | uint32_t shoff = sizeof(Elf32_Ehdr) + sizeof(Elf32_Phdr) * s->sh_info; |
| 149 | |
Bharata B Rao | acb0ef5 | 2014-05-19 19:57:44 +0200 | [diff] [blame] | 150 | elf_header.e_shoff = cpu_to_dump32(s, shoff); |
| 151 | elf_header.e_shentsize = cpu_to_dump16(s, sizeof(Elf32_Shdr)); |
| 152 | elf_header.e_shnum = cpu_to_dump16(s, 1); |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | ret = fd_write_vmcore(&elf_header, sizeof(elf_header), s); |
| 156 | if (ret < 0) { |
Peter Xu | e3517a5 | 2016-02-18 13:16:46 +0800 | [diff] [blame] | 157 | error_setg(errp, "dump: failed to write elf header"); |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 158 | } |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 159 | } |
| 160 | |
zhanghailiang | 4c7e251 | 2014-10-09 14:13:11 +0800 | [diff] [blame] | 161 | static void write_elf64_load(DumpState *s, MemoryMapping *memory_mapping, |
| 162 | int phdr_index, hwaddr offset, |
| 163 | hwaddr filesz, Error **errp) |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 164 | { |
| 165 | Elf64_Phdr phdr; |
| 166 | int ret; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 167 | |
| 168 | memset(&phdr, 0, sizeof(Elf64_Phdr)); |
Bharata B Rao | acb0ef5 | 2014-05-19 19:57:44 +0200 | [diff] [blame] | 169 | phdr.p_type = cpu_to_dump32(s, PT_LOAD); |
| 170 | phdr.p_offset = cpu_to_dump64(s, offset); |
| 171 | phdr.p_paddr = cpu_to_dump64(s, memory_mapping->phys_addr); |
| 172 | phdr.p_filesz = cpu_to_dump64(s, filesz); |
| 173 | phdr.p_memsz = cpu_to_dump64(s, memory_mapping->length); |
| 174 | phdr.p_vaddr = cpu_to_dump64(s, memory_mapping->virt_addr); |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 175 | |
Laszlo Ersek | 2cac260 | 2013-08-06 12:37:08 +0200 | [diff] [blame] | 176 | assert(memory_mapping->length >= filesz); |
| 177 | |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 178 | ret = fd_write_vmcore(&phdr, sizeof(Elf64_Phdr), s); |
| 179 | if (ret < 0) { |
Peter Xu | e3517a5 | 2016-02-18 13:16:46 +0800 | [diff] [blame] | 180 | error_setg(errp, "dump: failed to write program header table"); |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 181 | } |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 182 | } |
| 183 | |
zhanghailiang | 4c7e251 | 2014-10-09 14:13:11 +0800 | [diff] [blame] | 184 | static void write_elf32_load(DumpState *s, MemoryMapping *memory_mapping, |
| 185 | int phdr_index, hwaddr offset, |
| 186 | hwaddr filesz, Error **errp) |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 187 | { |
| 188 | Elf32_Phdr phdr; |
| 189 | int ret; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 190 | |
| 191 | memset(&phdr, 0, sizeof(Elf32_Phdr)); |
Bharata B Rao | acb0ef5 | 2014-05-19 19:57:44 +0200 | [diff] [blame] | 192 | phdr.p_type = cpu_to_dump32(s, PT_LOAD); |
| 193 | phdr.p_offset = cpu_to_dump32(s, offset); |
| 194 | phdr.p_paddr = cpu_to_dump32(s, memory_mapping->phys_addr); |
| 195 | phdr.p_filesz = cpu_to_dump32(s, filesz); |
| 196 | phdr.p_memsz = cpu_to_dump32(s, memory_mapping->length); |
| 197 | phdr.p_vaddr = cpu_to_dump32(s, memory_mapping->virt_addr); |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 198 | |
Laszlo Ersek | 2cac260 | 2013-08-06 12:37:08 +0200 | [diff] [blame] | 199 | assert(memory_mapping->length >= filesz); |
| 200 | |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 201 | ret = fd_write_vmcore(&phdr, sizeof(Elf32_Phdr), s); |
| 202 | if (ret < 0) { |
Peter Xu | e3517a5 | 2016-02-18 13:16:46 +0800 | [diff] [blame] | 203 | error_setg(errp, "dump: failed to write program header table"); |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 204 | } |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 205 | } |
| 206 | |
zhanghailiang | 4c7e251 | 2014-10-09 14:13:11 +0800 | [diff] [blame] | 207 | static void write_elf64_note(DumpState *s, Error **errp) |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 208 | { |
| 209 | Elf64_Phdr phdr; |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 210 | hwaddr begin = s->memory_offset - s->note_size; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 211 | int ret; |
| 212 | |
| 213 | memset(&phdr, 0, sizeof(Elf64_Phdr)); |
Bharata B Rao | acb0ef5 | 2014-05-19 19:57:44 +0200 | [diff] [blame] | 214 | phdr.p_type = cpu_to_dump32(s, PT_NOTE); |
| 215 | phdr.p_offset = cpu_to_dump64(s, begin); |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 216 | phdr.p_paddr = 0; |
Bharata B Rao | acb0ef5 | 2014-05-19 19:57:44 +0200 | [diff] [blame] | 217 | phdr.p_filesz = cpu_to_dump64(s, s->note_size); |
| 218 | phdr.p_memsz = cpu_to_dump64(s, s->note_size); |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 219 | phdr.p_vaddr = 0; |
| 220 | |
| 221 | ret = fd_write_vmcore(&phdr, sizeof(Elf64_Phdr), s); |
| 222 | if (ret < 0) { |
Peter Xu | e3517a5 | 2016-02-18 13:16:46 +0800 | [diff] [blame] | 223 | error_setg(errp, "dump: failed to write program header table"); |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 224 | } |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 225 | } |
| 226 | |
Paolo Bonzini | 0bc3cd6 | 2013-04-08 17:29:59 +0200 | [diff] [blame] | 227 | static inline int cpu_index(CPUState *cpu) |
| 228 | { |
| 229 | return cpu->cpu_index + 1; |
| 230 | } |
| 231 | |
zhanghailiang | 4c7e251 | 2014-10-09 14:13:11 +0800 | [diff] [blame] | 232 | static void write_elf64_notes(WriteCoreDumpFunction f, DumpState *s, |
| 233 | Error **errp) |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 234 | { |
Andreas Färber | 0d34282 | 2012-12-17 07:12:13 +0100 | [diff] [blame] | 235 | CPUState *cpu; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 236 | int ret; |
| 237 | int id; |
| 238 | |
Andreas Färber | bdc4464 | 2013-06-24 23:50:24 +0200 | [diff] [blame] | 239 | CPU_FOREACH(cpu) { |
Andreas Färber | 0d34282 | 2012-12-17 07:12:13 +0100 | [diff] [blame] | 240 | id = cpu_index(cpu); |
qiaonuohan | 6a51991 | 2014-02-18 14:11:26 +0800 | [diff] [blame] | 241 | ret = cpu_write_elf64_note(f, cpu, id, s); |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 242 | if (ret < 0) { |
Peter Xu | e3517a5 | 2016-02-18 13:16:46 +0800 | [diff] [blame] | 243 | error_setg(errp, "dump: failed to write elf notes"); |
zhanghailiang | 4c7e251 | 2014-10-09 14:13:11 +0800 | [diff] [blame] | 244 | return; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | |
Andreas Färber | bdc4464 | 2013-06-24 23:50:24 +0200 | [diff] [blame] | 248 | CPU_FOREACH(cpu) { |
qiaonuohan | 6a51991 | 2014-02-18 14:11:26 +0800 | [diff] [blame] | 249 | ret = cpu_write_elf64_qemunote(f, cpu, s); |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 250 | if (ret < 0) { |
Peter Xu | e3517a5 | 2016-02-18 13:16:46 +0800 | [diff] [blame] | 251 | error_setg(errp, "dump: failed to write CPU status"); |
zhanghailiang | 4c7e251 | 2014-10-09 14:13:11 +0800 | [diff] [blame] | 252 | return; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 253 | } |
| 254 | } |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 255 | } |
| 256 | |
zhanghailiang | 4c7e251 | 2014-10-09 14:13:11 +0800 | [diff] [blame] | 257 | static void write_elf32_note(DumpState *s, Error **errp) |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 258 | { |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 259 | hwaddr begin = s->memory_offset - s->note_size; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 260 | Elf32_Phdr phdr; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 261 | int ret; |
| 262 | |
| 263 | memset(&phdr, 0, sizeof(Elf32_Phdr)); |
Bharata B Rao | acb0ef5 | 2014-05-19 19:57:44 +0200 | [diff] [blame] | 264 | phdr.p_type = cpu_to_dump32(s, PT_NOTE); |
| 265 | phdr.p_offset = cpu_to_dump32(s, begin); |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 266 | phdr.p_paddr = 0; |
Bharata B Rao | acb0ef5 | 2014-05-19 19:57:44 +0200 | [diff] [blame] | 267 | phdr.p_filesz = cpu_to_dump32(s, s->note_size); |
| 268 | phdr.p_memsz = cpu_to_dump32(s, s->note_size); |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 269 | phdr.p_vaddr = 0; |
| 270 | |
| 271 | ret = fd_write_vmcore(&phdr, sizeof(Elf32_Phdr), s); |
| 272 | if (ret < 0) { |
Peter Xu | e3517a5 | 2016-02-18 13:16:46 +0800 | [diff] [blame] | 273 | error_setg(errp, "dump: failed to write program header table"); |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 274 | } |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 275 | } |
| 276 | |
zhanghailiang | 4c7e251 | 2014-10-09 14:13:11 +0800 | [diff] [blame] | 277 | static void write_elf32_notes(WriteCoreDumpFunction f, DumpState *s, |
| 278 | Error **errp) |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 279 | { |
Andreas Färber | 0d34282 | 2012-12-17 07:12:13 +0100 | [diff] [blame] | 280 | CPUState *cpu; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 281 | int ret; |
| 282 | int id; |
| 283 | |
Andreas Färber | bdc4464 | 2013-06-24 23:50:24 +0200 | [diff] [blame] | 284 | CPU_FOREACH(cpu) { |
Andreas Färber | 0d34282 | 2012-12-17 07:12:13 +0100 | [diff] [blame] | 285 | id = cpu_index(cpu); |
qiaonuohan | 6a51991 | 2014-02-18 14:11:26 +0800 | [diff] [blame] | 286 | ret = cpu_write_elf32_note(f, cpu, id, s); |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 287 | if (ret < 0) { |
Peter Xu | e3517a5 | 2016-02-18 13:16:46 +0800 | [diff] [blame] | 288 | error_setg(errp, "dump: failed to write elf notes"); |
zhanghailiang | 4c7e251 | 2014-10-09 14:13:11 +0800 | [diff] [blame] | 289 | return; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | |
Andreas Färber | bdc4464 | 2013-06-24 23:50:24 +0200 | [diff] [blame] | 293 | CPU_FOREACH(cpu) { |
qiaonuohan | 6a51991 | 2014-02-18 14:11:26 +0800 | [diff] [blame] | 294 | ret = cpu_write_elf32_qemunote(f, cpu, s); |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 295 | if (ret < 0) { |
Peter Xu | e3517a5 | 2016-02-18 13:16:46 +0800 | [diff] [blame] | 296 | error_setg(errp, "dump: failed to write CPU status"); |
zhanghailiang | 4c7e251 | 2014-10-09 14:13:11 +0800 | [diff] [blame] | 297 | return; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 298 | } |
| 299 | } |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 300 | } |
| 301 | |
zhanghailiang | 4c7e251 | 2014-10-09 14:13:11 +0800 | [diff] [blame] | 302 | static void write_elf_section(DumpState *s, int type, Error **errp) |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 303 | { |
| 304 | Elf32_Shdr shdr32; |
| 305 | Elf64_Shdr shdr64; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 306 | int shdr_size; |
| 307 | void *shdr; |
| 308 | int ret; |
| 309 | |
| 310 | if (type == 0) { |
| 311 | shdr_size = sizeof(Elf32_Shdr); |
| 312 | memset(&shdr32, 0, shdr_size); |
Bharata B Rao | acb0ef5 | 2014-05-19 19:57:44 +0200 | [diff] [blame] | 313 | shdr32.sh_info = cpu_to_dump32(s, s->sh_info); |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 314 | shdr = &shdr32; |
| 315 | } else { |
| 316 | shdr_size = sizeof(Elf64_Shdr); |
| 317 | memset(&shdr64, 0, shdr_size); |
Bharata B Rao | acb0ef5 | 2014-05-19 19:57:44 +0200 | [diff] [blame] | 318 | shdr64.sh_info = cpu_to_dump32(s, s->sh_info); |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 319 | shdr = &shdr64; |
| 320 | } |
| 321 | |
| 322 | ret = fd_write_vmcore(&shdr, shdr_size, s); |
| 323 | if (ret < 0) { |
Peter Xu | e3517a5 | 2016-02-18 13:16:46 +0800 | [diff] [blame] | 324 | error_setg(errp, "dump: failed to write section header table"); |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 325 | } |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 326 | } |
| 327 | |
zhanghailiang | 4c7e251 | 2014-10-09 14:13:11 +0800 | [diff] [blame] | 328 | static void write_data(DumpState *s, void *buf, int length, Error **errp) |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 329 | { |
| 330 | int ret; |
| 331 | |
| 332 | ret = fd_write_vmcore(buf, length, s); |
| 333 | if (ret < 0) { |
Peter Xu | e3517a5 | 2016-02-18 13:16:46 +0800 | [diff] [blame] | 334 | error_setg(errp, "dump: failed to save memory"); |
Peter Xu | 2264c2c | 2016-02-18 13:16:53 +0800 | [diff] [blame] | 335 | } else { |
| 336 | s->written_size += length; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 337 | } |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 338 | } |
| 339 | |
zhanghailiang | 4c7e251 | 2014-10-09 14:13:11 +0800 | [diff] [blame] | 340 | /* write the memory to vmcore. 1 page per I/O. */ |
| 341 | static void write_memory(DumpState *s, GuestPhysBlock *block, ram_addr_t start, |
| 342 | int64_t size, Error **errp) |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 343 | { |
| 344 | int64_t i; |
zhanghailiang | 4c7e251 | 2014-10-09 14:13:11 +0800 | [diff] [blame] | 345 | Error *local_err = NULL; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 346 | |
Andrew Jones | 8161bef | 2016-01-11 20:56:20 +0100 | [diff] [blame] | 347 | for (i = 0; i < size / s->dump_info.page_size; i++) { |
| 348 | write_data(s, block->host_addr + start + i * s->dump_info.page_size, |
| 349 | s->dump_info.page_size, &local_err); |
zhanghailiang | 4c7e251 | 2014-10-09 14:13:11 +0800 | [diff] [blame] | 350 | if (local_err) { |
| 351 | error_propagate(errp, local_err); |
| 352 | return; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 353 | } |
| 354 | } |
| 355 | |
Andrew Jones | 8161bef | 2016-01-11 20:56:20 +0100 | [diff] [blame] | 356 | if ((size % s->dump_info.page_size) != 0) { |
| 357 | write_data(s, block->host_addr + start + i * s->dump_info.page_size, |
| 358 | size % s->dump_info.page_size, &local_err); |
zhanghailiang | 4c7e251 | 2014-10-09 14:13:11 +0800 | [diff] [blame] | 359 | if (local_err) { |
| 360 | error_propagate(errp, local_err); |
| 361 | return; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 362 | } |
| 363 | } |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 364 | } |
| 365 | |
Laszlo Ersek | 2cac260 | 2013-08-06 12:37:08 +0200 | [diff] [blame] | 366 | /* get the memory's offset and size in the vmcore */ |
| 367 | static void get_offset_range(hwaddr phys_addr, |
| 368 | ram_addr_t mapping_length, |
| 369 | DumpState *s, |
| 370 | hwaddr *p_offset, |
| 371 | hwaddr *p_filesz) |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 372 | { |
Laszlo Ersek | 56c4bfb | 2013-08-06 12:37:11 +0200 | [diff] [blame] | 373 | GuestPhysBlock *block; |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 374 | hwaddr offset = s->memory_offset; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 375 | int64_t size_in_block, start; |
| 376 | |
Laszlo Ersek | 2cac260 | 2013-08-06 12:37:08 +0200 | [diff] [blame] | 377 | /* When the memory is not stored into vmcore, offset will be -1 */ |
| 378 | *p_offset = -1; |
| 379 | *p_filesz = 0; |
| 380 | |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 381 | if (s->has_filter) { |
| 382 | if (phys_addr < s->begin || phys_addr >= s->begin + s->length) { |
Laszlo Ersek | 2cac260 | 2013-08-06 12:37:08 +0200 | [diff] [blame] | 383 | return; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 384 | } |
| 385 | } |
| 386 | |
Laszlo Ersek | 56c4bfb | 2013-08-06 12:37:11 +0200 | [diff] [blame] | 387 | QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) { |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 388 | if (s->has_filter) { |
Laszlo Ersek | 56c4bfb | 2013-08-06 12:37:11 +0200 | [diff] [blame] | 389 | if (block->target_start >= s->begin + s->length || |
| 390 | block->target_end <= s->begin) { |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 391 | /* This block is out of the range */ |
| 392 | continue; |
| 393 | } |
| 394 | |
Laszlo Ersek | 56c4bfb | 2013-08-06 12:37:11 +0200 | [diff] [blame] | 395 | if (s->begin <= block->target_start) { |
| 396 | start = block->target_start; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 397 | } else { |
| 398 | start = s->begin; |
| 399 | } |
| 400 | |
Laszlo Ersek | 56c4bfb | 2013-08-06 12:37:11 +0200 | [diff] [blame] | 401 | size_in_block = block->target_end - start; |
| 402 | if (s->begin + s->length < block->target_end) { |
| 403 | size_in_block -= block->target_end - (s->begin + s->length); |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 404 | } |
| 405 | } else { |
Laszlo Ersek | 56c4bfb | 2013-08-06 12:37:11 +0200 | [diff] [blame] | 406 | start = block->target_start; |
| 407 | size_in_block = block->target_end - block->target_start; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | if (phys_addr >= start && phys_addr < start + size_in_block) { |
Laszlo Ersek | 2cac260 | 2013-08-06 12:37:08 +0200 | [diff] [blame] | 411 | *p_offset = phys_addr - start + offset; |
| 412 | |
| 413 | /* The offset range mapped from the vmcore file must not spill over |
Laszlo Ersek | 56c4bfb | 2013-08-06 12:37:11 +0200 | [diff] [blame] | 414 | * the GuestPhysBlock, clamp it. The rest of the mapping will be |
Laszlo Ersek | 2cac260 | 2013-08-06 12:37:08 +0200 | [diff] [blame] | 415 | * zero-filled in memory at load time; see |
| 416 | * <http://refspecs.linuxbase.org/elf/gabi4+/ch5.pheader.html>. |
| 417 | */ |
| 418 | *p_filesz = phys_addr + mapping_length <= start + size_in_block ? |
| 419 | mapping_length : |
| 420 | size_in_block - (phys_addr - start); |
| 421 | return; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | offset += size_in_block; |
| 425 | } |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 426 | } |
| 427 | |
zhanghailiang | 4c7e251 | 2014-10-09 14:13:11 +0800 | [diff] [blame] | 428 | static void write_elf_loads(DumpState *s, Error **errp) |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 429 | { |
Laszlo Ersek | 2cac260 | 2013-08-06 12:37:08 +0200 | [diff] [blame] | 430 | hwaddr offset, filesz; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 431 | MemoryMapping *memory_mapping; |
| 432 | uint32_t phdr_index = 1; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 433 | uint32_t max_index; |
zhanghailiang | 4c7e251 | 2014-10-09 14:13:11 +0800 | [diff] [blame] | 434 | Error *local_err = NULL; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 435 | |
| 436 | if (s->have_section) { |
| 437 | max_index = s->sh_info; |
| 438 | } else { |
| 439 | max_index = s->phdr_num; |
| 440 | } |
| 441 | |
| 442 | QTAILQ_FOREACH(memory_mapping, &s->list.head, next) { |
Laszlo Ersek | 2cac260 | 2013-08-06 12:37:08 +0200 | [diff] [blame] | 443 | get_offset_range(memory_mapping->phys_addr, |
| 444 | memory_mapping->length, |
| 445 | s, &offset, &filesz); |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 446 | if (s->dump_info.d_class == ELFCLASS64) { |
zhanghailiang | 4c7e251 | 2014-10-09 14:13:11 +0800 | [diff] [blame] | 447 | write_elf64_load(s, memory_mapping, phdr_index++, offset, |
| 448 | filesz, &local_err); |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 449 | } else { |
zhanghailiang | 4c7e251 | 2014-10-09 14:13:11 +0800 | [diff] [blame] | 450 | write_elf32_load(s, memory_mapping, phdr_index++, offset, |
| 451 | filesz, &local_err); |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 452 | } |
| 453 | |
zhanghailiang | 4c7e251 | 2014-10-09 14:13:11 +0800 | [diff] [blame] | 454 | if (local_err) { |
| 455 | error_propagate(errp, local_err); |
| 456 | return; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | if (phdr_index >= max_index) { |
| 460 | break; |
| 461 | } |
| 462 | } |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | /* write elf header, PT_NOTE and elf note to vmcore. */ |
zhanghailiang | 4c7e251 | 2014-10-09 14:13:11 +0800 | [diff] [blame] | 466 | static void dump_begin(DumpState *s, Error **errp) |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 467 | { |
zhanghailiang | 4c7e251 | 2014-10-09 14:13:11 +0800 | [diff] [blame] | 468 | Error *local_err = NULL; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 469 | |
| 470 | /* |
| 471 | * the vmcore's format is: |
| 472 | * -------------- |
| 473 | * | elf header | |
| 474 | * -------------- |
| 475 | * | PT_NOTE | |
| 476 | * -------------- |
| 477 | * | PT_LOAD | |
| 478 | * -------------- |
| 479 | * | ...... | |
| 480 | * -------------- |
| 481 | * | PT_LOAD | |
| 482 | * -------------- |
| 483 | * | sec_hdr | |
| 484 | * -------------- |
| 485 | * | elf note | |
| 486 | * -------------- |
| 487 | * | memory | |
| 488 | * -------------- |
| 489 | * |
| 490 | * we only know where the memory is saved after we write elf note into |
| 491 | * vmcore. |
| 492 | */ |
| 493 | |
| 494 | /* write elf header to vmcore */ |
| 495 | if (s->dump_info.d_class == ELFCLASS64) { |
zhanghailiang | 4c7e251 | 2014-10-09 14:13:11 +0800 | [diff] [blame] | 496 | write_elf64_header(s, &local_err); |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 497 | } else { |
zhanghailiang | 4c7e251 | 2014-10-09 14:13:11 +0800 | [diff] [blame] | 498 | write_elf32_header(s, &local_err); |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 499 | } |
zhanghailiang | 4c7e251 | 2014-10-09 14:13:11 +0800 | [diff] [blame] | 500 | if (local_err) { |
| 501 | error_propagate(errp, local_err); |
| 502 | return; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | if (s->dump_info.d_class == ELFCLASS64) { |
| 506 | /* write PT_NOTE to vmcore */ |
zhanghailiang | 4c7e251 | 2014-10-09 14:13:11 +0800 | [diff] [blame] | 507 | write_elf64_note(s, &local_err); |
| 508 | if (local_err) { |
| 509 | error_propagate(errp, local_err); |
| 510 | return; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | /* write all PT_LOAD to vmcore */ |
zhanghailiang | 4c7e251 | 2014-10-09 14:13:11 +0800 | [diff] [blame] | 514 | write_elf_loads(s, &local_err); |
| 515 | if (local_err) { |
| 516 | error_propagate(errp, local_err); |
| 517 | return; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | /* write section to vmcore */ |
| 521 | if (s->have_section) { |
zhanghailiang | 4c7e251 | 2014-10-09 14:13:11 +0800 | [diff] [blame] | 522 | write_elf_section(s, 1, &local_err); |
| 523 | if (local_err) { |
| 524 | error_propagate(errp, local_err); |
| 525 | return; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 526 | } |
| 527 | } |
| 528 | |
| 529 | /* write notes to vmcore */ |
zhanghailiang | 4c7e251 | 2014-10-09 14:13:11 +0800 | [diff] [blame] | 530 | write_elf64_notes(fd_write_vmcore, s, &local_err); |
| 531 | if (local_err) { |
| 532 | error_propagate(errp, local_err); |
| 533 | return; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 534 | } |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 535 | } else { |
| 536 | /* write PT_NOTE to vmcore */ |
zhanghailiang | 4c7e251 | 2014-10-09 14:13:11 +0800 | [diff] [blame] | 537 | write_elf32_note(s, &local_err); |
| 538 | if (local_err) { |
| 539 | error_propagate(errp, local_err); |
| 540 | return; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | /* write all PT_LOAD to vmcore */ |
zhanghailiang | 4c7e251 | 2014-10-09 14:13:11 +0800 | [diff] [blame] | 544 | write_elf_loads(s, &local_err); |
| 545 | if (local_err) { |
| 546 | error_propagate(errp, local_err); |
| 547 | return; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | /* write section to vmcore */ |
| 551 | if (s->have_section) { |
zhanghailiang | 4c7e251 | 2014-10-09 14:13:11 +0800 | [diff] [blame] | 552 | write_elf_section(s, 0, &local_err); |
| 553 | if (local_err) { |
| 554 | error_propagate(errp, local_err); |
| 555 | return; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 556 | } |
| 557 | } |
| 558 | |
| 559 | /* write notes to vmcore */ |
zhanghailiang | 4c7e251 | 2014-10-09 14:13:11 +0800 | [diff] [blame] | 560 | write_elf32_notes(fd_write_vmcore, s, &local_err); |
| 561 | if (local_err) { |
| 562 | error_propagate(errp, local_err); |
| 563 | return; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 564 | } |
| 565 | } |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 566 | } |
| 567 | |
Laszlo Ersek | 56c4bfb | 2013-08-06 12:37:11 +0200 | [diff] [blame] | 568 | static int get_next_block(DumpState *s, GuestPhysBlock *block) |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 569 | { |
| 570 | while (1) { |
Paolo Bonzini | a316103 | 2012-11-14 15:54:48 +0100 | [diff] [blame] | 571 | block = QTAILQ_NEXT(block, next); |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 572 | if (!block) { |
| 573 | /* no more block */ |
| 574 | return 1; |
| 575 | } |
| 576 | |
| 577 | s->start = 0; |
Laszlo Ersek | 56c4bfb | 2013-08-06 12:37:11 +0200 | [diff] [blame] | 578 | s->next_block = block; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 579 | if (s->has_filter) { |
Laszlo Ersek | 56c4bfb | 2013-08-06 12:37:11 +0200 | [diff] [blame] | 580 | if (block->target_start >= s->begin + s->length || |
| 581 | block->target_end <= s->begin) { |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 582 | /* This block is out of the range */ |
| 583 | continue; |
| 584 | } |
| 585 | |
Laszlo Ersek | 56c4bfb | 2013-08-06 12:37:11 +0200 | [diff] [blame] | 586 | if (s->begin > block->target_start) { |
| 587 | s->start = s->begin - block->target_start; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 588 | } |
| 589 | } |
| 590 | |
| 591 | return 0; |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | /* write all memory to vmcore */ |
zhanghailiang | 4c7e251 | 2014-10-09 14:13:11 +0800 | [diff] [blame] | 596 | static void dump_iterate(DumpState *s, Error **errp) |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 597 | { |
Laszlo Ersek | 56c4bfb | 2013-08-06 12:37:11 +0200 | [diff] [blame] | 598 | GuestPhysBlock *block; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 599 | int64_t size; |
zhanghailiang | 4c7e251 | 2014-10-09 14:13:11 +0800 | [diff] [blame] | 600 | Error *local_err = NULL; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 601 | |
Gonglei | 08a655b | 2014-10-30 14:01:17 +0800 | [diff] [blame] | 602 | do { |
Laszlo Ersek | 56c4bfb | 2013-08-06 12:37:11 +0200 | [diff] [blame] | 603 | block = s->next_block; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 604 | |
Laszlo Ersek | 56c4bfb | 2013-08-06 12:37:11 +0200 | [diff] [blame] | 605 | size = block->target_end - block->target_start; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 606 | if (s->has_filter) { |
| 607 | size -= s->start; |
Laszlo Ersek | 56c4bfb | 2013-08-06 12:37:11 +0200 | [diff] [blame] | 608 | if (s->begin + s->length < block->target_end) { |
| 609 | size -= block->target_end - (s->begin + s->length); |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 610 | } |
| 611 | } |
zhanghailiang | 4c7e251 | 2014-10-09 14:13:11 +0800 | [diff] [blame] | 612 | write_memory(s, block, s->start, size, &local_err); |
| 613 | if (local_err) { |
| 614 | error_propagate(errp, local_err); |
| 615 | return; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 616 | } |
| 617 | |
Gonglei | 08a655b | 2014-10-30 14:01:17 +0800 | [diff] [blame] | 618 | } while (!get_next_block(s, block)); |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 619 | } |
| 620 | |
zhanghailiang | 4c7e251 | 2014-10-09 14:13:11 +0800 | [diff] [blame] | 621 | static void create_vmcore(DumpState *s, Error **errp) |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 622 | { |
zhanghailiang | 4c7e251 | 2014-10-09 14:13:11 +0800 | [diff] [blame] | 623 | Error *local_err = NULL; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 624 | |
zhanghailiang | 4c7e251 | 2014-10-09 14:13:11 +0800 | [diff] [blame] | 625 | dump_begin(s, &local_err); |
| 626 | if (local_err) { |
| 627 | error_propagate(errp, local_err); |
| 628 | return; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 629 | } |
| 630 | |
zhanghailiang | 4c7e251 | 2014-10-09 14:13:11 +0800 | [diff] [blame] | 631 | dump_iterate(s, errp); |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 632 | } |
| 633 | |
qiaonuohan | fda0538 | 2014-02-18 14:11:27 +0800 | [diff] [blame] | 634 | static int write_start_flat_header(int fd) |
| 635 | { |
Laszlo Ersek | 92ba140 | 2014-05-20 13:39:42 +0200 | [diff] [blame] | 636 | MakedumpfileHeader *mh; |
qiaonuohan | fda0538 | 2014-02-18 14:11:27 +0800 | [diff] [blame] | 637 | int ret = 0; |
| 638 | |
Laszlo Ersek | 92ba140 | 2014-05-20 13:39:42 +0200 | [diff] [blame] | 639 | QEMU_BUILD_BUG_ON(sizeof *mh > MAX_SIZE_MDF_HEADER); |
| 640 | mh = g_malloc0(MAX_SIZE_MDF_HEADER); |
qiaonuohan | fda0538 | 2014-02-18 14:11:27 +0800 | [diff] [blame] | 641 | |
Laszlo Ersek | 92ba140 | 2014-05-20 13:39:42 +0200 | [diff] [blame] | 642 | memcpy(mh->signature, MAKEDUMPFILE_SIGNATURE, |
| 643 | MIN(sizeof mh->signature, sizeof MAKEDUMPFILE_SIGNATURE)); |
qiaonuohan | fda0538 | 2014-02-18 14:11:27 +0800 | [diff] [blame] | 644 | |
Laszlo Ersek | 92ba140 | 2014-05-20 13:39:42 +0200 | [diff] [blame] | 645 | mh->type = cpu_to_be64(TYPE_FLAT_HEADER); |
| 646 | mh->version = cpu_to_be64(VERSION_FLAT_HEADER); |
qiaonuohan | fda0538 | 2014-02-18 14:11:27 +0800 | [diff] [blame] | 647 | |
| 648 | size_t written_size; |
Laszlo Ersek | 92ba140 | 2014-05-20 13:39:42 +0200 | [diff] [blame] | 649 | written_size = qemu_write_full(fd, mh, MAX_SIZE_MDF_HEADER); |
qiaonuohan | fda0538 | 2014-02-18 14:11:27 +0800 | [diff] [blame] | 650 | if (written_size != MAX_SIZE_MDF_HEADER) { |
| 651 | ret = -1; |
| 652 | } |
| 653 | |
Laszlo Ersek | 92ba140 | 2014-05-20 13:39:42 +0200 | [diff] [blame] | 654 | g_free(mh); |
qiaonuohan | fda0538 | 2014-02-18 14:11:27 +0800 | [diff] [blame] | 655 | return ret; |
| 656 | } |
| 657 | |
| 658 | static int write_end_flat_header(int fd) |
| 659 | { |
| 660 | MakedumpfileDataHeader mdh; |
| 661 | |
| 662 | mdh.offset = END_FLAG_FLAT_HEADER; |
| 663 | mdh.buf_size = END_FLAG_FLAT_HEADER; |
| 664 | |
| 665 | size_t written_size; |
| 666 | written_size = qemu_write_full(fd, &mdh, sizeof(mdh)); |
| 667 | if (written_size != sizeof(mdh)) { |
| 668 | return -1; |
| 669 | } |
| 670 | |
| 671 | return 0; |
| 672 | } |
| 673 | |
qiaonuohan | 5d31bab | 2014-02-18 14:11:28 +0800 | [diff] [blame] | 674 | static int write_buffer(int fd, off_t offset, const void *buf, size_t size) |
| 675 | { |
| 676 | size_t written_size; |
| 677 | MakedumpfileDataHeader mdh; |
| 678 | |
| 679 | mdh.offset = cpu_to_be64(offset); |
| 680 | mdh.buf_size = cpu_to_be64(size); |
| 681 | |
| 682 | written_size = qemu_write_full(fd, &mdh, sizeof(mdh)); |
| 683 | if (written_size != sizeof(mdh)) { |
| 684 | return -1; |
| 685 | } |
| 686 | |
| 687 | written_size = qemu_write_full(fd, buf, size); |
| 688 | if (written_size != size) { |
| 689 | return -1; |
| 690 | } |
| 691 | |
| 692 | return 0; |
| 693 | } |
| 694 | |
qiaonuohan | 4835ef7 | 2014-02-18 14:11:29 +0800 | [diff] [blame] | 695 | static int buf_write_note(const void *buf, size_t size, void *opaque) |
| 696 | { |
| 697 | DumpState *s = opaque; |
| 698 | |
| 699 | /* note_buf is not enough */ |
| 700 | if (s->note_buf_offset + size > s->note_size) { |
| 701 | return -1; |
| 702 | } |
| 703 | |
| 704 | memcpy(s->note_buf + s->note_buf_offset, buf, size); |
| 705 | |
| 706 | s->note_buf_offset += size; |
| 707 | |
| 708 | return 0; |
| 709 | } |
| 710 | |
qiaonuohan | 298f116 | 2014-02-18 14:11:32 +0800 | [diff] [blame] | 711 | /* write common header, sub header and elf note to vmcore */ |
zhanghailiang | 4c7e251 | 2014-10-09 14:13:11 +0800 | [diff] [blame] | 712 | static void create_header32(DumpState *s, Error **errp) |
qiaonuohan | 298f116 | 2014-02-18 14:11:32 +0800 | [diff] [blame] | 713 | { |
qiaonuohan | 298f116 | 2014-02-18 14:11:32 +0800 | [diff] [blame] | 714 | DiskDumpHeader32 *dh = NULL; |
| 715 | KdumpSubHeader32 *kh = NULL; |
| 716 | size_t size; |
qiaonuohan | 298f116 | 2014-02-18 14:11:32 +0800 | [diff] [blame] | 717 | uint32_t block_size; |
| 718 | uint32_t sub_hdr_size; |
| 719 | uint32_t bitmap_blocks; |
| 720 | uint32_t status = 0; |
| 721 | uint64_t offset_note; |
zhanghailiang | 4c7e251 | 2014-10-09 14:13:11 +0800 | [diff] [blame] | 722 | Error *local_err = NULL; |
qiaonuohan | 298f116 | 2014-02-18 14:11:32 +0800 | [diff] [blame] | 723 | |
| 724 | /* write common header, the version of kdump-compressed format is 6th */ |
| 725 | size = sizeof(DiskDumpHeader32); |
| 726 | dh = g_malloc0(size); |
| 727 | |
| 728 | strncpy(dh->signature, KDUMP_SIGNATURE, strlen(KDUMP_SIGNATURE)); |
Bharata B Rao | acb0ef5 | 2014-05-19 19:57:44 +0200 | [diff] [blame] | 729 | dh->header_version = cpu_to_dump32(s, 6); |
Andrew Jones | 8161bef | 2016-01-11 20:56:20 +0100 | [diff] [blame] | 730 | block_size = s->dump_info.page_size; |
Bharata B Rao | acb0ef5 | 2014-05-19 19:57:44 +0200 | [diff] [blame] | 731 | dh->block_size = cpu_to_dump32(s, block_size); |
qiaonuohan | 298f116 | 2014-02-18 14:11:32 +0800 | [diff] [blame] | 732 | sub_hdr_size = sizeof(struct KdumpSubHeader32) + s->note_size; |
| 733 | sub_hdr_size = DIV_ROUND_UP(sub_hdr_size, block_size); |
Bharata B Rao | acb0ef5 | 2014-05-19 19:57:44 +0200 | [diff] [blame] | 734 | dh->sub_hdr_size = cpu_to_dump32(s, sub_hdr_size); |
qiaonuohan | 298f116 | 2014-02-18 14:11:32 +0800 | [diff] [blame] | 735 | /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */ |
Bharata B Rao | acb0ef5 | 2014-05-19 19:57:44 +0200 | [diff] [blame] | 736 | dh->max_mapnr = cpu_to_dump32(s, MIN(s->max_mapnr, UINT_MAX)); |
| 737 | dh->nr_cpus = cpu_to_dump32(s, s->nr_cpus); |
qiaonuohan | 298f116 | 2014-02-18 14:11:32 +0800 | [diff] [blame] | 738 | bitmap_blocks = DIV_ROUND_UP(s->len_dump_bitmap, block_size) * 2; |
Bharata B Rao | acb0ef5 | 2014-05-19 19:57:44 +0200 | [diff] [blame] | 739 | dh->bitmap_blocks = cpu_to_dump32(s, bitmap_blocks); |
qiaonuohan | 4ab23a9 | 2014-02-18 14:11:37 +0800 | [diff] [blame] | 740 | strncpy(dh->utsname.machine, ELF_MACHINE_UNAME, sizeof(dh->utsname.machine)); |
qiaonuohan | 298f116 | 2014-02-18 14:11:32 +0800 | [diff] [blame] | 741 | |
| 742 | if (s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) { |
| 743 | status |= DUMP_DH_COMPRESSED_ZLIB; |
| 744 | } |
| 745 | #ifdef CONFIG_LZO |
| 746 | if (s->flag_compress & DUMP_DH_COMPRESSED_LZO) { |
| 747 | status |= DUMP_DH_COMPRESSED_LZO; |
| 748 | } |
| 749 | #endif |
| 750 | #ifdef CONFIG_SNAPPY |
| 751 | if (s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) { |
| 752 | status |= DUMP_DH_COMPRESSED_SNAPPY; |
| 753 | } |
| 754 | #endif |
Bharata B Rao | acb0ef5 | 2014-05-19 19:57:44 +0200 | [diff] [blame] | 755 | dh->status = cpu_to_dump32(s, status); |
qiaonuohan | 298f116 | 2014-02-18 14:11:32 +0800 | [diff] [blame] | 756 | |
| 757 | if (write_buffer(s->fd, 0, dh, size) < 0) { |
Peter Xu | e3517a5 | 2016-02-18 13:16:46 +0800 | [diff] [blame] | 758 | error_setg(errp, "dump: failed to write disk dump header"); |
qiaonuohan | 298f116 | 2014-02-18 14:11:32 +0800 | [diff] [blame] | 759 | goto out; |
| 760 | } |
| 761 | |
| 762 | /* write sub header */ |
| 763 | size = sizeof(KdumpSubHeader32); |
| 764 | kh = g_malloc0(size); |
| 765 | |
| 766 | /* 64bit max_mapnr_64 */ |
Bharata B Rao | acb0ef5 | 2014-05-19 19:57:44 +0200 | [diff] [blame] | 767 | kh->max_mapnr_64 = cpu_to_dump64(s, s->max_mapnr); |
Andrew Jones | b6e05aa | 2016-01-11 20:56:21 +0100 | [diff] [blame] | 768 | kh->phys_base = cpu_to_dump32(s, s->dump_info.phys_base); |
Bharata B Rao | acb0ef5 | 2014-05-19 19:57:44 +0200 | [diff] [blame] | 769 | kh->dump_level = cpu_to_dump32(s, DUMP_LEVEL); |
qiaonuohan | 298f116 | 2014-02-18 14:11:32 +0800 | [diff] [blame] | 770 | |
| 771 | offset_note = DISKDUMP_HEADER_BLOCKS * block_size + size; |
Bharata B Rao | acb0ef5 | 2014-05-19 19:57:44 +0200 | [diff] [blame] | 772 | kh->offset_note = cpu_to_dump64(s, offset_note); |
| 773 | kh->note_size = cpu_to_dump32(s, s->note_size); |
qiaonuohan | 298f116 | 2014-02-18 14:11:32 +0800 | [diff] [blame] | 774 | |
| 775 | if (write_buffer(s->fd, DISKDUMP_HEADER_BLOCKS * |
| 776 | block_size, kh, size) < 0) { |
Peter Xu | e3517a5 | 2016-02-18 13:16:46 +0800 | [diff] [blame] | 777 | error_setg(errp, "dump: failed to write kdump sub header"); |
qiaonuohan | 298f116 | 2014-02-18 14:11:32 +0800 | [diff] [blame] | 778 | goto out; |
| 779 | } |
| 780 | |
| 781 | /* write note */ |
| 782 | s->note_buf = g_malloc0(s->note_size); |
| 783 | s->note_buf_offset = 0; |
| 784 | |
| 785 | /* use s->note_buf to store notes temporarily */ |
zhanghailiang | 4c7e251 | 2014-10-09 14:13:11 +0800 | [diff] [blame] | 786 | write_elf32_notes(buf_write_note, s, &local_err); |
| 787 | if (local_err) { |
| 788 | error_propagate(errp, local_err); |
qiaonuohan | 298f116 | 2014-02-18 14:11:32 +0800 | [diff] [blame] | 789 | goto out; |
| 790 | } |
qiaonuohan | 298f116 | 2014-02-18 14:11:32 +0800 | [diff] [blame] | 791 | if (write_buffer(s->fd, offset_note, s->note_buf, |
| 792 | s->note_size) < 0) { |
Peter Xu | e3517a5 | 2016-02-18 13:16:46 +0800 | [diff] [blame] | 793 | error_setg(errp, "dump: failed to write notes"); |
qiaonuohan | 298f116 | 2014-02-18 14:11:32 +0800 | [diff] [blame] | 794 | goto out; |
| 795 | } |
| 796 | |
| 797 | /* get offset of dump_bitmap */ |
| 798 | s->offset_dump_bitmap = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size) * |
| 799 | block_size; |
| 800 | |
| 801 | /* get offset of page */ |
| 802 | s->offset_page = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size + bitmap_blocks) * |
| 803 | block_size; |
| 804 | |
| 805 | out: |
| 806 | g_free(dh); |
| 807 | g_free(kh); |
| 808 | g_free(s->note_buf); |
qiaonuohan | 298f116 | 2014-02-18 14:11:32 +0800 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | /* write common header, sub header and elf note to vmcore */ |
zhanghailiang | 4c7e251 | 2014-10-09 14:13:11 +0800 | [diff] [blame] | 812 | static void create_header64(DumpState *s, Error **errp) |
qiaonuohan | 298f116 | 2014-02-18 14:11:32 +0800 | [diff] [blame] | 813 | { |
qiaonuohan | 298f116 | 2014-02-18 14:11:32 +0800 | [diff] [blame] | 814 | DiskDumpHeader64 *dh = NULL; |
| 815 | KdumpSubHeader64 *kh = NULL; |
| 816 | size_t size; |
qiaonuohan | 298f116 | 2014-02-18 14:11:32 +0800 | [diff] [blame] | 817 | uint32_t block_size; |
| 818 | uint32_t sub_hdr_size; |
| 819 | uint32_t bitmap_blocks; |
| 820 | uint32_t status = 0; |
| 821 | uint64_t offset_note; |
zhanghailiang | 4c7e251 | 2014-10-09 14:13:11 +0800 | [diff] [blame] | 822 | Error *local_err = NULL; |
qiaonuohan | 298f116 | 2014-02-18 14:11:32 +0800 | [diff] [blame] | 823 | |
| 824 | /* write common header, the version of kdump-compressed format is 6th */ |
| 825 | size = sizeof(DiskDumpHeader64); |
| 826 | dh = g_malloc0(size); |
| 827 | |
| 828 | strncpy(dh->signature, KDUMP_SIGNATURE, strlen(KDUMP_SIGNATURE)); |
Bharata B Rao | acb0ef5 | 2014-05-19 19:57:44 +0200 | [diff] [blame] | 829 | dh->header_version = cpu_to_dump32(s, 6); |
Andrew Jones | 8161bef | 2016-01-11 20:56:20 +0100 | [diff] [blame] | 830 | block_size = s->dump_info.page_size; |
Bharata B Rao | acb0ef5 | 2014-05-19 19:57:44 +0200 | [diff] [blame] | 831 | dh->block_size = cpu_to_dump32(s, block_size); |
qiaonuohan | 298f116 | 2014-02-18 14:11:32 +0800 | [diff] [blame] | 832 | sub_hdr_size = sizeof(struct KdumpSubHeader64) + s->note_size; |
| 833 | sub_hdr_size = DIV_ROUND_UP(sub_hdr_size, block_size); |
Bharata B Rao | acb0ef5 | 2014-05-19 19:57:44 +0200 | [diff] [blame] | 834 | dh->sub_hdr_size = cpu_to_dump32(s, sub_hdr_size); |
qiaonuohan | 298f116 | 2014-02-18 14:11:32 +0800 | [diff] [blame] | 835 | /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */ |
Bharata B Rao | acb0ef5 | 2014-05-19 19:57:44 +0200 | [diff] [blame] | 836 | dh->max_mapnr = cpu_to_dump32(s, MIN(s->max_mapnr, UINT_MAX)); |
| 837 | dh->nr_cpus = cpu_to_dump32(s, s->nr_cpus); |
qiaonuohan | 298f116 | 2014-02-18 14:11:32 +0800 | [diff] [blame] | 838 | bitmap_blocks = DIV_ROUND_UP(s->len_dump_bitmap, block_size) * 2; |
Bharata B Rao | acb0ef5 | 2014-05-19 19:57:44 +0200 | [diff] [blame] | 839 | dh->bitmap_blocks = cpu_to_dump32(s, bitmap_blocks); |
qiaonuohan | 4ab23a9 | 2014-02-18 14:11:37 +0800 | [diff] [blame] | 840 | strncpy(dh->utsname.machine, ELF_MACHINE_UNAME, sizeof(dh->utsname.machine)); |
qiaonuohan | 298f116 | 2014-02-18 14:11:32 +0800 | [diff] [blame] | 841 | |
| 842 | if (s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) { |
| 843 | status |= DUMP_DH_COMPRESSED_ZLIB; |
| 844 | } |
| 845 | #ifdef CONFIG_LZO |
| 846 | if (s->flag_compress & DUMP_DH_COMPRESSED_LZO) { |
| 847 | status |= DUMP_DH_COMPRESSED_LZO; |
| 848 | } |
| 849 | #endif |
| 850 | #ifdef CONFIG_SNAPPY |
| 851 | if (s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) { |
| 852 | status |= DUMP_DH_COMPRESSED_SNAPPY; |
| 853 | } |
| 854 | #endif |
Bharata B Rao | acb0ef5 | 2014-05-19 19:57:44 +0200 | [diff] [blame] | 855 | dh->status = cpu_to_dump32(s, status); |
qiaonuohan | 298f116 | 2014-02-18 14:11:32 +0800 | [diff] [blame] | 856 | |
| 857 | if (write_buffer(s->fd, 0, dh, size) < 0) { |
Peter Xu | e3517a5 | 2016-02-18 13:16:46 +0800 | [diff] [blame] | 858 | error_setg(errp, "dump: failed to write disk dump header"); |
qiaonuohan | 298f116 | 2014-02-18 14:11:32 +0800 | [diff] [blame] | 859 | goto out; |
| 860 | } |
| 861 | |
| 862 | /* write sub header */ |
| 863 | size = sizeof(KdumpSubHeader64); |
| 864 | kh = g_malloc0(size); |
| 865 | |
| 866 | /* 64bit max_mapnr_64 */ |
Bharata B Rao | acb0ef5 | 2014-05-19 19:57:44 +0200 | [diff] [blame] | 867 | kh->max_mapnr_64 = cpu_to_dump64(s, s->max_mapnr); |
Andrew Jones | b6e05aa | 2016-01-11 20:56:21 +0100 | [diff] [blame] | 868 | kh->phys_base = cpu_to_dump64(s, s->dump_info.phys_base); |
Bharata B Rao | acb0ef5 | 2014-05-19 19:57:44 +0200 | [diff] [blame] | 869 | kh->dump_level = cpu_to_dump32(s, DUMP_LEVEL); |
qiaonuohan | 298f116 | 2014-02-18 14:11:32 +0800 | [diff] [blame] | 870 | |
| 871 | offset_note = DISKDUMP_HEADER_BLOCKS * block_size + size; |
Bharata B Rao | acb0ef5 | 2014-05-19 19:57:44 +0200 | [diff] [blame] | 872 | kh->offset_note = cpu_to_dump64(s, offset_note); |
| 873 | kh->note_size = cpu_to_dump64(s, s->note_size); |
qiaonuohan | 298f116 | 2014-02-18 14:11:32 +0800 | [diff] [blame] | 874 | |
| 875 | if (write_buffer(s->fd, DISKDUMP_HEADER_BLOCKS * |
| 876 | block_size, kh, size) < 0) { |
Peter Xu | e3517a5 | 2016-02-18 13:16:46 +0800 | [diff] [blame] | 877 | error_setg(errp, "dump: failed to write kdump sub header"); |
qiaonuohan | 298f116 | 2014-02-18 14:11:32 +0800 | [diff] [blame] | 878 | goto out; |
| 879 | } |
| 880 | |
| 881 | /* write note */ |
| 882 | s->note_buf = g_malloc0(s->note_size); |
| 883 | s->note_buf_offset = 0; |
| 884 | |
| 885 | /* use s->note_buf to store notes temporarily */ |
zhanghailiang | 4c7e251 | 2014-10-09 14:13:11 +0800 | [diff] [blame] | 886 | write_elf64_notes(buf_write_note, s, &local_err); |
| 887 | if (local_err) { |
| 888 | error_propagate(errp, local_err); |
qiaonuohan | 298f116 | 2014-02-18 14:11:32 +0800 | [diff] [blame] | 889 | goto out; |
| 890 | } |
| 891 | |
| 892 | if (write_buffer(s->fd, offset_note, s->note_buf, |
| 893 | s->note_size) < 0) { |
Peter Xu | e3517a5 | 2016-02-18 13:16:46 +0800 | [diff] [blame] | 894 | error_setg(errp, "dump: failed to write notes"); |
qiaonuohan | 298f116 | 2014-02-18 14:11:32 +0800 | [diff] [blame] | 895 | goto out; |
| 896 | } |
| 897 | |
| 898 | /* get offset of dump_bitmap */ |
| 899 | s->offset_dump_bitmap = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size) * |
| 900 | block_size; |
| 901 | |
| 902 | /* get offset of page */ |
| 903 | s->offset_page = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size + bitmap_blocks) * |
| 904 | block_size; |
| 905 | |
| 906 | out: |
| 907 | g_free(dh); |
| 908 | g_free(kh); |
| 909 | g_free(s->note_buf); |
qiaonuohan | 298f116 | 2014-02-18 14:11:32 +0800 | [diff] [blame] | 910 | } |
| 911 | |
zhanghailiang | 4c7e251 | 2014-10-09 14:13:11 +0800 | [diff] [blame] | 912 | static void write_dump_header(DumpState *s, Error **errp) |
qiaonuohan | 298f116 | 2014-02-18 14:11:32 +0800 | [diff] [blame] | 913 | { |
zhanghailiang | 4c7e251 | 2014-10-09 14:13:11 +0800 | [diff] [blame] | 914 | Error *local_err = NULL; |
| 915 | |
Laszlo Ersek | 24aeeac | 2014-05-20 13:39:45 +0200 | [diff] [blame] | 916 | if (s->dump_info.d_class == ELFCLASS32) { |
zhanghailiang | 4c7e251 | 2014-10-09 14:13:11 +0800 | [diff] [blame] | 917 | create_header32(s, &local_err); |
qiaonuohan | 298f116 | 2014-02-18 14:11:32 +0800 | [diff] [blame] | 918 | } else { |
zhanghailiang | 4c7e251 | 2014-10-09 14:13:11 +0800 | [diff] [blame] | 919 | create_header64(s, &local_err); |
| 920 | } |
| 921 | if (local_err) { |
| 922 | error_propagate(errp, local_err); |
qiaonuohan | 298f116 | 2014-02-18 14:11:32 +0800 | [diff] [blame] | 923 | } |
| 924 | } |
| 925 | |
Andrew Jones | 8161bef | 2016-01-11 20:56:20 +0100 | [diff] [blame] | 926 | static size_t dump_bitmap_get_bufsize(DumpState *s) |
| 927 | { |
| 928 | return s->dump_info.page_size; |
| 929 | } |
| 930 | |
qiaonuohan | d0686c7 | 2014-02-18 14:11:33 +0800 | [diff] [blame] | 931 | /* |
| 932 | * set dump_bitmap sequencely. the bit before last_pfn is not allowed to be |
| 933 | * rewritten, so if need to set the first bit, set last_pfn and pfn to 0. |
| 934 | * set_dump_bitmap will always leave the recently set bit un-sync. And setting |
| 935 | * (last bit + sizeof(buf) * 8) to 0 will do flushing the content in buf into |
| 936 | * vmcore, ie. synchronizing un-sync bit into vmcore. |
| 937 | */ |
| 938 | static int set_dump_bitmap(uint64_t last_pfn, uint64_t pfn, bool value, |
| 939 | uint8_t *buf, DumpState *s) |
| 940 | { |
| 941 | off_t old_offset, new_offset; |
| 942 | off_t offset_bitmap1, offset_bitmap2; |
| 943 | uint32_t byte, bit; |
Andrew Jones | 8161bef | 2016-01-11 20:56:20 +0100 | [diff] [blame] | 944 | size_t bitmap_bufsize = dump_bitmap_get_bufsize(s); |
| 945 | size_t bits_per_buf = bitmap_bufsize * CHAR_BIT; |
qiaonuohan | d0686c7 | 2014-02-18 14:11:33 +0800 | [diff] [blame] | 946 | |
| 947 | /* should not set the previous place */ |
| 948 | assert(last_pfn <= pfn); |
| 949 | |
| 950 | /* |
| 951 | * if the bit needed to be set is not cached in buf, flush the data in buf |
| 952 | * to vmcore firstly. |
| 953 | * making new_offset be bigger than old_offset can also sync remained data |
| 954 | * into vmcore. |
| 955 | */ |
Andrew Jones | 8161bef | 2016-01-11 20:56:20 +0100 | [diff] [blame] | 956 | old_offset = bitmap_bufsize * (last_pfn / bits_per_buf); |
| 957 | new_offset = bitmap_bufsize * (pfn / bits_per_buf); |
qiaonuohan | d0686c7 | 2014-02-18 14:11:33 +0800 | [diff] [blame] | 958 | |
| 959 | while (old_offset < new_offset) { |
| 960 | /* calculate the offset and write dump_bitmap */ |
| 961 | offset_bitmap1 = s->offset_dump_bitmap + old_offset; |
| 962 | if (write_buffer(s->fd, offset_bitmap1, buf, |
Andrew Jones | 8161bef | 2016-01-11 20:56:20 +0100 | [diff] [blame] | 963 | bitmap_bufsize) < 0) { |
qiaonuohan | d0686c7 | 2014-02-18 14:11:33 +0800 | [diff] [blame] | 964 | return -1; |
| 965 | } |
| 966 | |
| 967 | /* dump level 1 is chosen, so 1st and 2nd bitmap are same */ |
| 968 | offset_bitmap2 = s->offset_dump_bitmap + s->len_dump_bitmap + |
| 969 | old_offset; |
| 970 | if (write_buffer(s->fd, offset_bitmap2, buf, |
Andrew Jones | 8161bef | 2016-01-11 20:56:20 +0100 | [diff] [blame] | 971 | bitmap_bufsize) < 0) { |
qiaonuohan | d0686c7 | 2014-02-18 14:11:33 +0800 | [diff] [blame] | 972 | return -1; |
| 973 | } |
| 974 | |
Andrew Jones | 8161bef | 2016-01-11 20:56:20 +0100 | [diff] [blame] | 975 | memset(buf, 0, bitmap_bufsize); |
| 976 | old_offset += bitmap_bufsize; |
qiaonuohan | d0686c7 | 2014-02-18 14:11:33 +0800 | [diff] [blame] | 977 | } |
| 978 | |
| 979 | /* get the exact place of the bit in the buf, and set it */ |
Andrew Jones | 8161bef | 2016-01-11 20:56:20 +0100 | [diff] [blame] | 980 | byte = (pfn % bits_per_buf) / CHAR_BIT; |
| 981 | bit = (pfn % bits_per_buf) % CHAR_BIT; |
qiaonuohan | d0686c7 | 2014-02-18 14:11:33 +0800 | [diff] [blame] | 982 | if (value) { |
| 983 | buf[byte] |= 1u << bit; |
| 984 | } else { |
| 985 | buf[byte] &= ~(1u << bit); |
| 986 | } |
| 987 | |
| 988 | return 0; |
| 989 | } |
| 990 | |
Andrew Jones | 8161bef | 2016-01-11 20:56:20 +0100 | [diff] [blame] | 991 | static uint64_t dump_paddr_to_pfn(DumpState *s, uint64_t addr) |
| 992 | { |
| 993 | int target_page_shift = ctz32(s->dump_info.page_size); |
| 994 | |
| 995 | return (addr >> target_page_shift) - ARCH_PFN_OFFSET; |
| 996 | } |
| 997 | |
| 998 | static uint64_t dump_pfn_to_paddr(DumpState *s, uint64_t pfn) |
| 999 | { |
| 1000 | int target_page_shift = ctz32(s->dump_info.page_size); |
| 1001 | |
| 1002 | return (pfn + ARCH_PFN_OFFSET) << target_page_shift; |
| 1003 | } |
| 1004 | |
qiaonuohan | d0686c7 | 2014-02-18 14:11:33 +0800 | [diff] [blame] | 1005 | /* |
| 1006 | * exam every page and return the page frame number and the address of the page. |
| 1007 | * bufptr can be NULL. note: the blocks here is supposed to reflect guest-phys |
| 1008 | * blocks, so block->target_start and block->target_end should be interal |
| 1009 | * multiples of the target page size. |
| 1010 | */ |
| 1011 | static bool get_next_page(GuestPhysBlock **blockptr, uint64_t *pfnptr, |
| 1012 | uint8_t **bufptr, DumpState *s) |
| 1013 | { |
| 1014 | GuestPhysBlock *block = *blockptr; |
Andrew Jones | 8161bef | 2016-01-11 20:56:20 +0100 | [diff] [blame] | 1015 | hwaddr addr, target_page_mask = ~((hwaddr)s->dump_info.page_size - 1); |
qiaonuohan | d0686c7 | 2014-02-18 14:11:33 +0800 | [diff] [blame] | 1016 | uint8_t *buf; |
| 1017 | |
| 1018 | /* block == NULL means the start of the iteration */ |
| 1019 | if (!block) { |
| 1020 | block = QTAILQ_FIRST(&s->guest_phys_blocks.head); |
| 1021 | *blockptr = block; |
Andrew Jones | 8161bef | 2016-01-11 20:56:20 +0100 | [diff] [blame] | 1022 | assert((block->target_start & ~target_page_mask) == 0); |
| 1023 | assert((block->target_end & ~target_page_mask) == 0); |
| 1024 | *pfnptr = dump_paddr_to_pfn(s, block->target_start); |
qiaonuohan | d0686c7 | 2014-02-18 14:11:33 +0800 | [diff] [blame] | 1025 | if (bufptr) { |
| 1026 | *bufptr = block->host_addr; |
| 1027 | } |
| 1028 | return true; |
| 1029 | } |
| 1030 | |
| 1031 | *pfnptr = *pfnptr + 1; |
Andrew Jones | 8161bef | 2016-01-11 20:56:20 +0100 | [diff] [blame] | 1032 | addr = dump_pfn_to_paddr(s, *pfnptr); |
qiaonuohan | d0686c7 | 2014-02-18 14:11:33 +0800 | [diff] [blame] | 1033 | |
| 1034 | if ((addr >= block->target_start) && |
Andrew Jones | 8161bef | 2016-01-11 20:56:20 +0100 | [diff] [blame] | 1035 | (addr + s->dump_info.page_size <= block->target_end)) { |
qiaonuohan | d0686c7 | 2014-02-18 14:11:33 +0800 | [diff] [blame] | 1036 | buf = block->host_addr + (addr - block->target_start); |
| 1037 | } else { |
| 1038 | /* the next page is in the next block */ |
| 1039 | block = QTAILQ_NEXT(block, next); |
| 1040 | *blockptr = block; |
| 1041 | if (!block) { |
| 1042 | return false; |
| 1043 | } |
Andrew Jones | 8161bef | 2016-01-11 20:56:20 +0100 | [diff] [blame] | 1044 | assert((block->target_start & ~target_page_mask) == 0); |
| 1045 | assert((block->target_end & ~target_page_mask) == 0); |
| 1046 | *pfnptr = dump_paddr_to_pfn(s, block->target_start); |
qiaonuohan | d0686c7 | 2014-02-18 14:11:33 +0800 | [diff] [blame] | 1047 | buf = block->host_addr; |
| 1048 | } |
| 1049 | |
| 1050 | if (bufptr) { |
| 1051 | *bufptr = buf; |
| 1052 | } |
| 1053 | |
| 1054 | return true; |
| 1055 | } |
| 1056 | |
zhanghailiang | 4c7e251 | 2014-10-09 14:13:11 +0800 | [diff] [blame] | 1057 | static void write_dump_bitmap(DumpState *s, Error **errp) |
qiaonuohan | d0686c7 | 2014-02-18 14:11:33 +0800 | [diff] [blame] | 1058 | { |
| 1059 | int ret = 0; |
| 1060 | uint64_t last_pfn, pfn; |
| 1061 | void *dump_bitmap_buf; |
| 1062 | size_t num_dumpable; |
| 1063 | GuestPhysBlock *block_iter = NULL; |
Andrew Jones | 8161bef | 2016-01-11 20:56:20 +0100 | [diff] [blame] | 1064 | size_t bitmap_bufsize = dump_bitmap_get_bufsize(s); |
| 1065 | size_t bits_per_buf = bitmap_bufsize * CHAR_BIT; |
qiaonuohan | d0686c7 | 2014-02-18 14:11:33 +0800 | [diff] [blame] | 1066 | |
| 1067 | /* dump_bitmap_buf is used to store dump_bitmap temporarily */ |
Andrew Jones | 8161bef | 2016-01-11 20:56:20 +0100 | [diff] [blame] | 1068 | dump_bitmap_buf = g_malloc0(bitmap_bufsize); |
qiaonuohan | d0686c7 | 2014-02-18 14:11:33 +0800 | [diff] [blame] | 1069 | |
| 1070 | num_dumpable = 0; |
| 1071 | last_pfn = 0; |
| 1072 | |
| 1073 | /* |
| 1074 | * exam memory page by page, and set the bit in dump_bitmap corresponded |
| 1075 | * to the existing page. |
| 1076 | */ |
| 1077 | while (get_next_page(&block_iter, &pfn, NULL, s)) { |
| 1078 | ret = set_dump_bitmap(last_pfn, pfn, true, dump_bitmap_buf, s); |
| 1079 | if (ret < 0) { |
Peter Xu | e3517a5 | 2016-02-18 13:16:46 +0800 | [diff] [blame] | 1080 | error_setg(errp, "dump: failed to set dump_bitmap"); |
qiaonuohan | d0686c7 | 2014-02-18 14:11:33 +0800 | [diff] [blame] | 1081 | goto out; |
| 1082 | } |
| 1083 | |
| 1084 | last_pfn = pfn; |
| 1085 | num_dumpable++; |
| 1086 | } |
| 1087 | |
| 1088 | /* |
| 1089 | * set_dump_bitmap will always leave the recently set bit un-sync. Here we |
Andrew Jones | 8161bef | 2016-01-11 20:56:20 +0100 | [diff] [blame] | 1090 | * set the remaining bits from last_pfn to the end of the bitmap buffer to |
| 1091 | * 0. With those set, the un-sync bit will be synchronized into the vmcore. |
qiaonuohan | d0686c7 | 2014-02-18 14:11:33 +0800 | [diff] [blame] | 1092 | */ |
| 1093 | if (num_dumpable > 0) { |
Andrew Jones | 8161bef | 2016-01-11 20:56:20 +0100 | [diff] [blame] | 1094 | ret = set_dump_bitmap(last_pfn, last_pfn + bits_per_buf, false, |
qiaonuohan | d0686c7 | 2014-02-18 14:11:33 +0800 | [diff] [blame] | 1095 | dump_bitmap_buf, s); |
| 1096 | if (ret < 0) { |
Peter Xu | e3517a5 | 2016-02-18 13:16:46 +0800 | [diff] [blame] | 1097 | error_setg(errp, "dump: failed to sync dump_bitmap"); |
qiaonuohan | d0686c7 | 2014-02-18 14:11:33 +0800 | [diff] [blame] | 1098 | goto out; |
| 1099 | } |
| 1100 | } |
| 1101 | |
| 1102 | /* number of dumpable pages that will be dumped later */ |
| 1103 | s->num_dumpable = num_dumpable; |
| 1104 | |
| 1105 | out: |
| 1106 | g_free(dump_bitmap_buf); |
qiaonuohan | d0686c7 | 2014-02-18 14:11:33 +0800 | [diff] [blame] | 1107 | } |
| 1108 | |
qiaonuohan | 64cfba6 | 2014-02-18 14:11:34 +0800 | [diff] [blame] | 1109 | static void prepare_data_cache(DataCache *data_cache, DumpState *s, |
| 1110 | off_t offset) |
| 1111 | { |
| 1112 | data_cache->fd = s->fd; |
| 1113 | data_cache->data_size = 0; |
Andrew Jones | 8161bef | 2016-01-11 20:56:20 +0100 | [diff] [blame] | 1114 | data_cache->buf_size = 4 * dump_bitmap_get_bufsize(s); |
| 1115 | data_cache->buf = g_malloc0(data_cache->buf_size); |
qiaonuohan | 64cfba6 | 2014-02-18 14:11:34 +0800 | [diff] [blame] | 1116 | data_cache->offset = offset; |
| 1117 | } |
| 1118 | |
| 1119 | static int write_cache(DataCache *dc, const void *buf, size_t size, |
| 1120 | bool flag_sync) |
| 1121 | { |
| 1122 | /* |
| 1123 | * dc->buf_size should not be less than size, otherwise dc will never be |
| 1124 | * enough |
| 1125 | */ |
| 1126 | assert(size <= dc->buf_size); |
| 1127 | |
| 1128 | /* |
| 1129 | * if flag_sync is set, synchronize data in dc->buf into vmcore. |
| 1130 | * otherwise check if the space is enough for caching data in buf, if not, |
| 1131 | * write the data in dc->buf to dc->fd and reset dc->buf |
| 1132 | */ |
| 1133 | if ((!flag_sync && dc->data_size + size > dc->buf_size) || |
| 1134 | (flag_sync && dc->data_size > 0)) { |
| 1135 | if (write_buffer(dc->fd, dc->offset, dc->buf, dc->data_size) < 0) { |
| 1136 | return -1; |
| 1137 | } |
| 1138 | |
| 1139 | dc->offset += dc->data_size; |
| 1140 | dc->data_size = 0; |
| 1141 | } |
| 1142 | |
| 1143 | if (!flag_sync) { |
| 1144 | memcpy(dc->buf + dc->data_size, buf, size); |
| 1145 | dc->data_size += size; |
| 1146 | } |
| 1147 | |
| 1148 | return 0; |
| 1149 | } |
| 1150 | |
| 1151 | static void free_data_cache(DataCache *data_cache) |
| 1152 | { |
| 1153 | g_free(data_cache->buf); |
| 1154 | } |
| 1155 | |
qiaonuohan | d12f57e | 2014-02-18 14:11:35 +0800 | [diff] [blame] | 1156 | static size_t get_len_buf_out(size_t page_size, uint32_t flag_compress) |
| 1157 | { |
Laszlo Ersek | b87ef35 | 2014-05-20 13:42:53 +0200 | [diff] [blame] | 1158 | switch (flag_compress) { |
| 1159 | case DUMP_DH_COMPRESSED_ZLIB: |
| 1160 | return compressBound(page_size); |
qiaonuohan | d12f57e | 2014-02-18 14:11:35 +0800 | [diff] [blame] | 1161 | |
Laszlo Ersek | b87ef35 | 2014-05-20 13:42:53 +0200 | [diff] [blame] | 1162 | case DUMP_DH_COMPRESSED_LZO: |
| 1163 | /* |
| 1164 | * LZO will expand incompressible data by a little amount. Please check |
| 1165 | * the following URL to see the expansion calculation: |
| 1166 | * http://www.oberhumer.com/opensource/lzo/lzofaq.php |
| 1167 | */ |
| 1168 | return page_size + page_size / 16 + 64 + 3; |
qiaonuohan | d12f57e | 2014-02-18 14:11:35 +0800 | [diff] [blame] | 1169 | |
| 1170 | #ifdef CONFIG_SNAPPY |
Laszlo Ersek | b87ef35 | 2014-05-20 13:42:53 +0200 | [diff] [blame] | 1171 | case DUMP_DH_COMPRESSED_SNAPPY: |
| 1172 | return snappy_max_compressed_length(page_size); |
qiaonuohan | d12f57e | 2014-02-18 14:11:35 +0800 | [diff] [blame] | 1173 | #endif |
Laszlo Ersek | b87ef35 | 2014-05-20 13:42:53 +0200 | [diff] [blame] | 1174 | } |
| 1175 | return 0; |
qiaonuohan | d12f57e | 2014-02-18 14:11:35 +0800 | [diff] [blame] | 1176 | } |
| 1177 | |
| 1178 | /* |
| 1179 | * check if the page is all 0 |
| 1180 | */ |
| 1181 | static inline bool is_zero_page(const uint8_t *buf, size_t page_size) |
| 1182 | { |
| 1183 | return buffer_is_zero(buf, page_size); |
| 1184 | } |
| 1185 | |
zhanghailiang | 4c7e251 | 2014-10-09 14:13:11 +0800 | [diff] [blame] | 1186 | static void write_dump_pages(DumpState *s, Error **errp) |
qiaonuohan | d12f57e | 2014-02-18 14:11:35 +0800 | [diff] [blame] | 1187 | { |
| 1188 | int ret = 0; |
| 1189 | DataCache page_desc, page_data; |
| 1190 | size_t len_buf_out, size_out; |
| 1191 | #ifdef CONFIG_LZO |
| 1192 | lzo_bytep wrkmem = NULL; |
| 1193 | #endif |
| 1194 | uint8_t *buf_out = NULL; |
| 1195 | off_t offset_desc, offset_data; |
| 1196 | PageDescriptor pd, pd_zero; |
| 1197 | uint8_t *buf; |
qiaonuohan | d12f57e | 2014-02-18 14:11:35 +0800 | [diff] [blame] | 1198 | GuestPhysBlock *block_iter = NULL; |
| 1199 | uint64_t pfn_iter; |
| 1200 | |
| 1201 | /* get offset of page_desc and page_data in dump file */ |
| 1202 | offset_desc = s->offset_page; |
| 1203 | offset_data = offset_desc + sizeof(PageDescriptor) * s->num_dumpable; |
| 1204 | |
| 1205 | prepare_data_cache(&page_desc, s, offset_desc); |
| 1206 | prepare_data_cache(&page_data, s, offset_data); |
| 1207 | |
| 1208 | /* prepare buffer to store compressed data */ |
Andrew Jones | 8161bef | 2016-01-11 20:56:20 +0100 | [diff] [blame] | 1209 | len_buf_out = get_len_buf_out(s->dump_info.page_size, s->flag_compress); |
Laszlo Ersek | b87ef35 | 2014-05-20 13:42:53 +0200 | [diff] [blame] | 1210 | assert(len_buf_out != 0); |
qiaonuohan | d12f57e | 2014-02-18 14:11:35 +0800 | [diff] [blame] | 1211 | |
| 1212 | #ifdef CONFIG_LZO |
| 1213 | wrkmem = g_malloc(LZO1X_1_MEM_COMPRESS); |
| 1214 | #endif |
| 1215 | |
| 1216 | buf_out = g_malloc(len_buf_out); |
| 1217 | |
| 1218 | /* |
| 1219 | * init zero page's page_desc and page_data, because every zero page |
| 1220 | * uses the same page_data |
| 1221 | */ |
Andrew Jones | 8161bef | 2016-01-11 20:56:20 +0100 | [diff] [blame] | 1222 | pd_zero.size = cpu_to_dump32(s, s->dump_info.page_size); |
Bharata B Rao | acb0ef5 | 2014-05-19 19:57:44 +0200 | [diff] [blame] | 1223 | pd_zero.flags = cpu_to_dump32(s, 0); |
| 1224 | pd_zero.offset = cpu_to_dump64(s, offset_data); |
| 1225 | pd_zero.page_flags = cpu_to_dump64(s, 0); |
Andrew Jones | 8161bef | 2016-01-11 20:56:20 +0100 | [diff] [blame] | 1226 | buf = g_malloc0(s->dump_info.page_size); |
| 1227 | ret = write_cache(&page_data, buf, s->dump_info.page_size, false); |
qiaonuohan | d12f57e | 2014-02-18 14:11:35 +0800 | [diff] [blame] | 1228 | g_free(buf); |
| 1229 | if (ret < 0) { |
Peter Xu | e3517a5 | 2016-02-18 13:16:46 +0800 | [diff] [blame] | 1230 | error_setg(errp, "dump: failed to write page data (zero page)"); |
qiaonuohan | d12f57e | 2014-02-18 14:11:35 +0800 | [diff] [blame] | 1231 | goto out; |
| 1232 | } |
| 1233 | |
Andrew Jones | 8161bef | 2016-01-11 20:56:20 +0100 | [diff] [blame] | 1234 | offset_data += s->dump_info.page_size; |
qiaonuohan | d12f57e | 2014-02-18 14:11:35 +0800 | [diff] [blame] | 1235 | |
| 1236 | /* |
| 1237 | * dump memory to vmcore page by page. zero page will all be resided in the |
| 1238 | * first page of page section |
| 1239 | */ |
| 1240 | while (get_next_page(&block_iter, &pfn_iter, &buf, s)) { |
| 1241 | /* check zero page */ |
Andrew Jones | 8161bef | 2016-01-11 20:56:20 +0100 | [diff] [blame] | 1242 | if (is_zero_page(buf, s->dump_info.page_size)) { |
qiaonuohan | d12f57e | 2014-02-18 14:11:35 +0800 | [diff] [blame] | 1243 | ret = write_cache(&page_desc, &pd_zero, sizeof(PageDescriptor), |
| 1244 | false); |
| 1245 | if (ret < 0) { |
Peter Xu | e3517a5 | 2016-02-18 13:16:46 +0800 | [diff] [blame] | 1246 | error_setg(errp, "dump: failed to write page desc"); |
qiaonuohan | d12f57e | 2014-02-18 14:11:35 +0800 | [diff] [blame] | 1247 | goto out; |
| 1248 | } |
| 1249 | } else { |
| 1250 | /* |
| 1251 | * not zero page, then: |
| 1252 | * 1. compress the page |
| 1253 | * 2. write the compressed page into the cache of page_data |
| 1254 | * 3. get page desc of the compressed page and write it into the |
| 1255 | * cache of page_desc |
| 1256 | * |
| 1257 | * only one compression format will be used here, for |
| 1258 | * s->flag_compress is set. But when compression fails to work, |
| 1259 | * we fall back to save in plaintext. |
| 1260 | */ |
| 1261 | size_out = len_buf_out; |
| 1262 | if ((s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) && |
Bharata B Rao | acb0ef5 | 2014-05-19 19:57:44 +0200 | [diff] [blame] | 1263 | (compress2(buf_out, (uLongf *)&size_out, buf, |
Andrew Jones | 8161bef | 2016-01-11 20:56:20 +0100 | [diff] [blame] | 1264 | s->dump_info.page_size, Z_BEST_SPEED) == Z_OK) && |
| 1265 | (size_out < s->dump_info.page_size)) { |
Bharata B Rao | acb0ef5 | 2014-05-19 19:57:44 +0200 | [diff] [blame] | 1266 | pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_ZLIB); |
| 1267 | pd.size = cpu_to_dump32(s, size_out); |
qiaonuohan | d12f57e | 2014-02-18 14:11:35 +0800 | [diff] [blame] | 1268 | |
| 1269 | ret = write_cache(&page_data, buf_out, size_out, false); |
| 1270 | if (ret < 0) { |
Peter Xu | e3517a5 | 2016-02-18 13:16:46 +0800 | [diff] [blame] | 1271 | error_setg(errp, "dump: failed to write page data"); |
qiaonuohan | d12f57e | 2014-02-18 14:11:35 +0800 | [diff] [blame] | 1272 | goto out; |
| 1273 | } |
| 1274 | #ifdef CONFIG_LZO |
| 1275 | } else if ((s->flag_compress & DUMP_DH_COMPRESSED_LZO) && |
Andrew Jones | 8161bef | 2016-01-11 20:56:20 +0100 | [diff] [blame] | 1276 | (lzo1x_1_compress(buf, s->dump_info.page_size, buf_out, |
qiaonuohan | d12f57e | 2014-02-18 14:11:35 +0800 | [diff] [blame] | 1277 | (lzo_uint *)&size_out, wrkmem) == LZO_E_OK) && |
Andrew Jones | 8161bef | 2016-01-11 20:56:20 +0100 | [diff] [blame] | 1278 | (size_out < s->dump_info.page_size)) { |
Bharata B Rao | acb0ef5 | 2014-05-19 19:57:44 +0200 | [diff] [blame] | 1279 | pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_LZO); |
| 1280 | pd.size = cpu_to_dump32(s, size_out); |
qiaonuohan | d12f57e | 2014-02-18 14:11:35 +0800 | [diff] [blame] | 1281 | |
| 1282 | ret = write_cache(&page_data, buf_out, size_out, false); |
| 1283 | if (ret < 0) { |
Peter Xu | e3517a5 | 2016-02-18 13:16:46 +0800 | [diff] [blame] | 1284 | error_setg(errp, "dump: failed to write page data"); |
qiaonuohan | d12f57e | 2014-02-18 14:11:35 +0800 | [diff] [blame] | 1285 | goto out; |
| 1286 | } |
| 1287 | #endif |
| 1288 | #ifdef CONFIG_SNAPPY |
| 1289 | } else if ((s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) && |
Andrew Jones | 8161bef | 2016-01-11 20:56:20 +0100 | [diff] [blame] | 1290 | (snappy_compress((char *)buf, s->dump_info.page_size, |
qiaonuohan | d12f57e | 2014-02-18 14:11:35 +0800 | [diff] [blame] | 1291 | (char *)buf_out, &size_out) == SNAPPY_OK) && |
Andrew Jones | 8161bef | 2016-01-11 20:56:20 +0100 | [diff] [blame] | 1292 | (size_out < s->dump_info.page_size)) { |
Bharata B Rao | acb0ef5 | 2014-05-19 19:57:44 +0200 | [diff] [blame] | 1293 | pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_SNAPPY); |
| 1294 | pd.size = cpu_to_dump32(s, size_out); |
qiaonuohan | d12f57e | 2014-02-18 14:11:35 +0800 | [diff] [blame] | 1295 | |
| 1296 | ret = write_cache(&page_data, buf_out, size_out, false); |
| 1297 | if (ret < 0) { |
Peter Xu | e3517a5 | 2016-02-18 13:16:46 +0800 | [diff] [blame] | 1298 | error_setg(errp, "dump: failed to write page data"); |
qiaonuohan | d12f57e | 2014-02-18 14:11:35 +0800 | [diff] [blame] | 1299 | goto out; |
| 1300 | } |
| 1301 | #endif |
| 1302 | } else { |
| 1303 | /* |
| 1304 | * fall back to save in plaintext, size_out should be |
Andrew Jones | 8161bef | 2016-01-11 20:56:20 +0100 | [diff] [blame] | 1305 | * assigned the target's page size |
qiaonuohan | d12f57e | 2014-02-18 14:11:35 +0800 | [diff] [blame] | 1306 | */ |
Bharata B Rao | acb0ef5 | 2014-05-19 19:57:44 +0200 | [diff] [blame] | 1307 | pd.flags = cpu_to_dump32(s, 0); |
Andrew Jones | 8161bef | 2016-01-11 20:56:20 +0100 | [diff] [blame] | 1308 | size_out = s->dump_info.page_size; |
Bharata B Rao | acb0ef5 | 2014-05-19 19:57:44 +0200 | [diff] [blame] | 1309 | pd.size = cpu_to_dump32(s, size_out); |
qiaonuohan | d12f57e | 2014-02-18 14:11:35 +0800 | [diff] [blame] | 1310 | |
Andrew Jones | 8161bef | 2016-01-11 20:56:20 +0100 | [diff] [blame] | 1311 | ret = write_cache(&page_data, buf, |
| 1312 | s->dump_info.page_size, false); |
qiaonuohan | d12f57e | 2014-02-18 14:11:35 +0800 | [diff] [blame] | 1313 | if (ret < 0) { |
Peter Xu | e3517a5 | 2016-02-18 13:16:46 +0800 | [diff] [blame] | 1314 | error_setg(errp, "dump: failed to write page data"); |
qiaonuohan | d12f57e | 2014-02-18 14:11:35 +0800 | [diff] [blame] | 1315 | goto out; |
| 1316 | } |
| 1317 | } |
| 1318 | |
| 1319 | /* get and write page desc here */ |
Bharata B Rao | acb0ef5 | 2014-05-19 19:57:44 +0200 | [diff] [blame] | 1320 | pd.page_flags = cpu_to_dump64(s, 0); |
| 1321 | pd.offset = cpu_to_dump64(s, offset_data); |
qiaonuohan | d12f57e | 2014-02-18 14:11:35 +0800 | [diff] [blame] | 1322 | offset_data += size_out; |
| 1323 | |
| 1324 | ret = write_cache(&page_desc, &pd, sizeof(PageDescriptor), false); |
| 1325 | if (ret < 0) { |
Peter Xu | e3517a5 | 2016-02-18 13:16:46 +0800 | [diff] [blame] | 1326 | error_setg(errp, "dump: failed to write page desc"); |
qiaonuohan | d12f57e | 2014-02-18 14:11:35 +0800 | [diff] [blame] | 1327 | goto out; |
| 1328 | } |
| 1329 | } |
Peter Xu | 2264c2c | 2016-02-18 13:16:53 +0800 | [diff] [blame] | 1330 | s->written_size += s->dump_info.page_size; |
qiaonuohan | d12f57e | 2014-02-18 14:11:35 +0800 | [diff] [blame] | 1331 | } |
| 1332 | |
| 1333 | ret = write_cache(&page_desc, NULL, 0, true); |
| 1334 | if (ret < 0) { |
Peter Xu | e3517a5 | 2016-02-18 13:16:46 +0800 | [diff] [blame] | 1335 | error_setg(errp, "dump: failed to sync cache for page_desc"); |
qiaonuohan | d12f57e | 2014-02-18 14:11:35 +0800 | [diff] [blame] | 1336 | goto out; |
| 1337 | } |
| 1338 | ret = write_cache(&page_data, NULL, 0, true); |
| 1339 | if (ret < 0) { |
Peter Xu | e3517a5 | 2016-02-18 13:16:46 +0800 | [diff] [blame] | 1340 | error_setg(errp, "dump: failed to sync cache for page_data"); |
qiaonuohan | d12f57e | 2014-02-18 14:11:35 +0800 | [diff] [blame] | 1341 | goto out; |
| 1342 | } |
| 1343 | |
| 1344 | out: |
| 1345 | free_data_cache(&page_desc); |
| 1346 | free_data_cache(&page_data); |
| 1347 | |
| 1348 | #ifdef CONFIG_LZO |
| 1349 | g_free(wrkmem); |
| 1350 | #endif |
| 1351 | |
| 1352 | g_free(buf_out); |
qiaonuohan | d12f57e | 2014-02-18 14:11:35 +0800 | [diff] [blame] | 1353 | } |
| 1354 | |
zhanghailiang | 4c7e251 | 2014-10-09 14:13:11 +0800 | [diff] [blame] | 1355 | static void create_kdump_vmcore(DumpState *s, Error **errp) |
qiaonuohan | b53ccc3 | 2014-02-18 14:11:36 +0800 | [diff] [blame] | 1356 | { |
| 1357 | int ret; |
zhanghailiang | 4c7e251 | 2014-10-09 14:13:11 +0800 | [diff] [blame] | 1358 | Error *local_err = NULL; |
qiaonuohan | b53ccc3 | 2014-02-18 14:11:36 +0800 | [diff] [blame] | 1359 | |
| 1360 | /* |
| 1361 | * the kdump-compressed format is: |
| 1362 | * File offset |
| 1363 | * +------------------------------------------+ 0x0 |
| 1364 | * | main header (struct disk_dump_header) | |
| 1365 | * |------------------------------------------+ block 1 |
| 1366 | * | sub header (struct kdump_sub_header) | |
| 1367 | * |------------------------------------------+ block 2 |
| 1368 | * | 1st-dump_bitmap | |
| 1369 | * |------------------------------------------+ block 2 + X blocks |
| 1370 | * | 2nd-dump_bitmap | (aligned by block) |
| 1371 | * |------------------------------------------+ block 2 + 2 * X blocks |
| 1372 | * | page desc for pfn 0 (struct page_desc) | (aligned by block) |
| 1373 | * | page desc for pfn 1 (struct page_desc) | |
| 1374 | * | : | |
| 1375 | * |------------------------------------------| (not aligned by block) |
| 1376 | * | page data (pfn 0) | |
| 1377 | * | page data (pfn 1) | |
| 1378 | * | : | |
| 1379 | * +------------------------------------------+ |
| 1380 | */ |
| 1381 | |
| 1382 | ret = write_start_flat_header(s->fd); |
| 1383 | if (ret < 0) { |
Peter Xu | e3517a5 | 2016-02-18 13:16:46 +0800 | [diff] [blame] | 1384 | error_setg(errp, "dump: failed to write start flat header"); |
zhanghailiang | 4c7e251 | 2014-10-09 14:13:11 +0800 | [diff] [blame] | 1385 | return; |
qiaonuohan | b53ccc3 | 2014-02-18 14:11:36 +0800 | [diff] [blame] | 1386 | } |
| 1387 | |
zhanghailiang | 4c7e251 | 2014-10-09 14:13:11 +0800 | [diff] [blame] | 1388 | write_dump_header(s, &local_err); |
| 1389 | if (local_err) { |
| 1390 | error_propagate(errp, local_err); |
| 1391 | return; |
qiaonuohan | b53ccc3 | 2014-02-18 14:11:36 +0800 | [diff] [blame] | 1392 | } |
| 1393 | |
zhanghailiang | 4c7e251 | 2014-10-09 14:13:11 +0800 | [diff] [blame] | 1394 | write_dump_bitmap(s, &local_err); |
| 1395 | if (local_err) { |
| 1396 | error_propagate(errp, local_err); |
| 1397 | return; |
qiaonuohan | b53ccc3 | 2014-02-18 14:11:36 +0800 | [diff] [blame] | 1398 | } |
| 1399 | |
zhanghailiang | 4c7e251 | 2014-10-09 14:13:11 +0800 | [diff] [blame] | 1400 | write_dump_pages(s, &local_err); |
| 1401 | if (local_err) { |
| 1402 | error_propagate(errp, local_err); |
| 1403 | return; |
qiaonuohan | b53ccc3 | 2014-02-18 14:11:36 +0800 | [diff] [blame] | 1404 | } |
| 1405 | |
| 1406 | ret = write_end_flat_header(s->fd); |
| 1407 | if (ret < 0) { |
Peter Xu | e3517a5 | 2016-02-18 13:16:46 +0800 | [diff] [blame] | 1408 | error_setg(errp, "dump: failed to write end flat header"); |
zhanghailiang | 4c7e251 | 2014-10-09 14:13:11 +0800 | [diff] [blame] | 1409 | return; |
qiaonuohan | b53ccc3 | 2014-02-18 14:11:36 +0800 | [diff] [blame] | 1410 | } |
qiaonuohan | b53ccc3 | 2014-02-18 14:11:36 +0800 | [diff] [blame] | 1411 | } |
| 1412 | |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 1413 | static ram_addr_t get_start_block(DumpState *s) |
| 1414 | { |
Laszlo Ersek | 56c4bfb | 2013-08-06 12:37:11 +0200 | [diff] [blame] | 1415 | GuestPhysBlock *block; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 1416 | |
| 1417 | if (!s->has_filter) { |
Laszlo Ersek | 56c4bfb | 2013-08-06 12:37:11 +0200 | [diff] [blame] | 1418 | s->next_block = QTAILQ_FIRST(&s->guest_phys_blocks.head); |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 1419 | return 0; |
| 1420 | } |
| 1421 | |
Laszlo Ersek | 56c4bfb | 2013-08-06 12:37:11 +0200 | [diff] [blame] | 1422 | QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) { |
| 1423 | if (block->target_start >= s->begin + s->length || |
| 1424 | block->target_end <= s->begin) { |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 1425 | /* This block is out of the range */ |
| 1426 | continue; |
| 1427 | } |
| 1428 | |
Laszlo Ersek | 56c4bfb | 2013-08-06 12:37:11 +0200 | [diff] [blame] | 1429 | s->next_block = block; |
| 1430 | if (s->begin > block->target_start) { |
| 1431 | s->start = s->begin - block->target_start; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 1432 | } else { |
| 1433 | s->start = 0; |
| 1434 | } |
| 1435 | return s->start; |
| 1436 | } |
| 1437 | |
| 1438 | return -1; |
| 1439 | } |
| 1440 | |
qiaonuohan | 7aad248 | 2014-02-18 14:11:31 +0800 | [diff] [blame] | 1441 | static void get_max_mapnr(DumpState *s) |
| 1442 | { |
| 1443 | GuestPhysBlock *last_block; |
| 1444 | |
| 1445 | last_block = QTAILQ_LAST(&s->guest_phys_blocks.head, GuestPhysBlockHead); |
Andrew Jones | 8161bef | 2016-01-11 20:56:20 +0100 | [diff] [blame] | 1446 | s->max_mapnr = dump_paddr_to_pfn(s, last_block->target_end); |
qiaonuohan | 7aad248 | 2014-02-18 14:11:31 +0800 | [diff] [blame] | 1447 | } |
| 1448 | |
Peter Xu | baf28f5 | 2016-02-18 13:16:48 +0800 | [diff] [blame] | 1449 | static DumpState dump_state_global = { .status = DUMP_STATUS_NONE }; |
| 1450 | |
| 1451 | static void dump_state_prepare(DumpState *s) |
| 1452 | { |
| 1453 | /* zero the struct, setting status to active */ |
| 1454 | *s = (DumpState) { .status = DUMP_STATUS_ACTIVE }; |
| 1455 | } |
| 1456 | |
Peter Xu | 65d64f3 | 2016-02-18 13:16:49 +0800 | [diff] [blame] | 1457 | bool dump_in_progress(void) |
| 1458 | { |
| 1459 | DumpState *state = &dump_state_global; |
Peter Xu | 39ba2ea | 2016-02-18 13:16:54 +0800 | [diff] [blame] | 1460 | return (atomic_read(&state->status) == DUMP_STATUS_ACTIVE); |
Peter Xu | 65d64f3 | 2016-02-18 13:16:49 +0800 | [diff] [blame] | 1461 | } |
| 1462 | |
Peter Xu | 2264c2c | 2016-02-18 13:16:53 +0800 | [diff] [blame] | 1463 | /* calculate total size of memory to be dumped (taking filter into |
| 1464 | * acoount.) */ |
| 1465 | static int64_t dump_calculate_size(DumpState *s) |
| 1466 | { |
| 1467 | GuestPhysBlock *block; |
| 1468 | int64_t size = 0, total = 0, left = 0, right = 0; |
| 1469 | |
| 1470 | QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) { |
| 1471 | if (s->has_filter) { |
| 1472 | /* calculate the overlapped region. */ |
| 1473 | left = MAX(s->begin, block->target_start); |
| 1474 | right = MIN(s->begin + s->length, block->target_end); |
| 1475 | size = right - left; |
| 1476 | size = size > 0 ? size : 0; |
| 1477 | } else { |
| 1478 | /* count the whole region in */ |
| 1479 | size = (block->target_end - block->target_start); |
| 1480 | } |
| 1481 | total += size; |
| 1482 | } |
| 1483 | |
| 1484 | return total; |
| 1485 | } |
| 1486 | |
zhanghailiang | 4c7e251 | 2014-10-09 14:13:11 +0800 | [diff] [blame] | 1487 | static void dump_init(DumpState *s, int fd, bool has_format, |
| 1488 | DumpGuestMemoryFormat format, bool paging, bool has_filter, |
| 1489 | int64_t begin, int64_t length, Error **errp) |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 1490 | { |
Andreas Färber | 182735e | 2013-05-29 22:29:20 +0200 | [diff] [blame] | 1491 | CPUState *cpu; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 1492 | int nr_cpus; |
Andreas Färber | 11ed09c | 2013-05-29 21:54:03 +0200 | [diff] [blame] | 1493 | Error *err = NULL; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 1494 | int ret; |
| 1495 | |
Peter Xu | ca1fc8c | 2016-02-18 13:16:50 +0800 | [diff] [blame] | 1496 | s->has_format = has_format; |
| 1497 | s->format = format; |
Peter Xu | 2264c2c | 2016-02-18 13:16:53 +0800 | [diff] [blame] | 1498 | s->written_size = 0; |
Peter Xu | ca1fc8c | 2016-02-18 13:16:50 +0800 | [diff] [blame] | 1499 | |
qiaonuohan | b53ccc3 | 2014-02-18 14:11:36 +0800 | [diff] [blame] | 1500 | /* kdump-compressed is conflict with paging and filter */ |
| 1501 | if (has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) { |
| 1502 | assert(!paging && !has_filter); |
| 1503 | } |
| 1504 | |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 1505 | if (runstate_is_running()) { |
| 1506 | vm_stop(RUN_STATE_SAVE_VM); |
| 1507 | s->resume = true; |
| 1508 | } else { |
| 1509 | s->resume = false; |
| 1510 | } |
| 1511 | |
Laszlo Ersek | 5ee163e | 2013-08-06 12:37:09 +0200 | [diff] [blame] | 1512 | /* If we use KVM, we should synchronize the registers before we get dump |
| 1513 | * info or physmap info. |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 1514 | */ |
Andreas Färber | 1b3509c | 2013-06-09 16:48:29 +0200 | [diff] [blame] | 1515 | cpu_synchronize_all_states(); |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 1516 | nr_cpus = 0; |
Andreas Färber | bdc4464 | 2013-06-24 23:50:24 +0200 | [diff] [blame] | 1517 | CPU_FOREACH(cpu) { |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 1518 | nr_cpus++; |
| 1519 | } |
| 1520 | |
Laszlo Ersek | 5ee163e | 2013-08-06 12:37:09 +0200 | [diff] [blame] | 1521 | s->fd = fd; |
| 1522 | s->has_filter = has_filter; |
| 1523 | s->begin = begin; |
| 1524 | s->length = length; |
| 1525 | |
Chen Gang | 2928207 | 2014-08-03 23:28:56 +0800 | [diff] [blame] | 1526 | memory_mapping_list_init(&s->list); |
| 1527 | |
Laszlo Ersek | 5ee163e | 2013-08-06 12:37:09 +0200 | [diff] [blame] | 1528 | guest_phys_blocks_init(&s->guest_phys_blocks); |
Laszlo Ersek | c5d7f60 | 2013-08-06 12:37:10 +0200 | [diff] [blame] | 1529 | guest_phys_blocks_append(&s->guest_phys_blocks); |
Peter Xu | 2264c2c | 2016-02-18 13:16:53 +0800 | [diff] [blame] | 1530 | s->total_size = dump_calculate_size(s); |
| 1531 | #ifdef DEBUG_DUMP_GUEST_MEMORY |
| 1532 | fprintf(stderr, "DUMP: total memory to dump: %lu\n", s->total_size); |
| 1533 | #endif |
Laszlo Ersek | 5ee163e | 2013-08-06 12:37:09 +0200 | [diff] [blame] | 1534 | |
| 1535 | s->start = get_start_block(s); |
| 1536 | if (s->start == -1) { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 1537 | error_setg(errp, QERR_INVALID_PARAMETER, "begin"); |
Laszlo Ersek | 5ee163e | 2013-08-06 12:37:09 +0200 | [diff] [blame] | 1538 | goto cleanup; |
| 1539 | } |
| 1540 | |
| 1541 | /* get dump info: endian, class and architecture. |
| 1542 | * If the target architecture is not supported, cpu_get_dump_info() will |
| 1543 | * return -1. |
| 1544 | */ |
Laszlo Ersek | 56c4bfb | 2013-08-06 12:37:11 +0200 | [diff] [blame] | 1545 | ret = cpu_get_dump_info(&s->dump_info, &s->guest_phys_blocks); |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 1546 | if (ret < 0) { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 1547 | error_setg(errp, QERR_UNSUPPORTED); |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 1548 | goto cleanup; |
| 1549 | } |
| 1550 | |
Andrew Jones | 8161bef | 2016-01-11 20:56:20 +0100 | [diff] [blame] | 1551 | if (!s->dump_info.page_size) { |
| 1552 | s->dump_info.page_size = TARGET_PAGE_SIZE; |
| 1553 | } |
| 1554 | |
Paolo Bonzini | 4720bd0 | 2012-06-07 08:48:09 +0200 | [diff] [blame] | 1555 | s->note_size = cpu_get_note_size(s->dump_info.d_class, |
| 1556 | s->dump_info.d_machine, nr_cpus); |
Aneesh Kumar K.V | bb6b684 | 2013-10-01 21:49:32 +0530 | [diff] [blame] | 1557 | if (s->note_size < 0) { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 1558 | error_setg(errp, QERR_UNSUPPORTED); |
Paolo Bonzini | 4720bd0 | 2012-06-07 08:48:09 +0200 | [diff] [blame] | 1559 | goto cleanup; |
| 1560 | } |
| 1561 | |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 1562 | /* get memory mapping */ |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 1563 | if (paging) { |
Laszlo Ersek | 56c4bfb | 2013-08-06 12:37:11 +0200 | [diff] [blame] | 1564 | qemu_get_guest_memory_mapping(&s->list, &s->guest_phys_blocks, &err); |
Andreas Färber | 11ed09c | 2013-05-29 21:54:03 +0200 | [diff] [blame] | 1565 | if (err != NULL) { |
| 1566 | error_propagate(errp, err); |
| 1567 | goto cleanup; |
| 1568 | } |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 1569 | } else { |
Laszlo Ersek | 56c4bfb | 2013-08-06 12:37:11 +0200 | [diff] [blame] | 1570 | qemu_get_guest_simple_memory_mapping(&s->list, &s->guest_phys_blocks); |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 1571 | } |
| 1572 | |
qiaonuohan | 7aad248 | 2014-02-18 14:11:31 +0800 | [diff] [blame] | 1573 | s->nr_cpus = nr_cpus; |
qiaonuohan | 7aad248 | 2014-02-18 14:11:31 +0800 | [diff] [blame] | 1574 | |
| 1575 | get_max_mapnr(s); |
| 1576 | |
| 1577 | uint64_t tmp; |
Andrew Jones | 8161bef | 2016-01-11 20:56:20 +0100 | [diff] [blame] | 1578 | tmp = DIV_ROUND_UP(DIV_ROUND_UP(s->max_mapnr, CHAR_BIT), |
| 1579 | s->dump_info.page_size); |
| 1580 | s->len_dump_bitmap = tmp * s->dump_info.page_size; |
qiaonuohan | 7aad248 | 2014-02-18 14:11:31 +0800 | [diff] [blame] | 1581 | |
qiaonuohan | b53ccc3 | 2014-02-18 14:11:36 +0800 | [diff] [blame] | 1582 | /* init for kdump-compressed format */ |
| 1583 | if (has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) { |
| 1584 | switch (format) { |
| 1585 | case DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB: |
| 1586 | s->flag_compress = DUMP_DH_COMPRESSED_ZLIB; |
| 1587 | break; |
| 1588 | |
| 1589 | case DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO: |
Laszlo Ersek | c998acb | 2014-05-20 13:42:22 +0200 | [diff] [blame] | 1590 | #ifdef CONFIG_LZO |
| 1591 | if (lzo_init() != LZO_E_OK) { |
| 1592 | error_setg(errp, "failed to initialize the LZO library"); |
| 1593 | goto cleanup; |
| 1594 | } |
| 1595 | #endif |
qiaonuohan | b53ccc3 | 2014-02-18 14:11:36 +0800 | [diff] [blame] | 1596 | s->flag_compress = DUMP_DH_COMPRESSED_LZO; |
| 1597 | break; |
| 1598 | |
| 1599 | case DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY: |
| 1600 | s->flag_compress = DUMP_DH_COMPRESSED_SNAPPY; |
| 1601 | break; |
| 1602 | |
| 1603 | default: |
| 1604 | s->flag_compress = 0; |
| 1605 | } |
| 1606 | |
zhanghailiang | 4c7e251 | 2014-10-09 14:13:11 +0800 | [diff] [blame] | 1607 | return; |
qiaonuohan | b53ccc3 | 2014-02-18 14:11:36 +0800 | [diff] [blame] | 1608 | } |
| 1609 | |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 1610 | if (s->has_filter) { |
| 1611 | memory_mapping_filter(&s->list, s->begin, s->length); |
| 1612 | } |
| 1613 | |
| 1614 | /* |
| 1615 | * calculate phdr_num |
| 1616 | * |
| 1617 | * the type of ehdr->e_phnum is uint16_t, so we should avoid overflow |
| 1618 | */ |
| 1619 | s->phdr_num = 1; /* PT_NOTE */ |
| 1620 | if (s->list.num < UINT16_MAX - 2) { |
| 1621 | s->phdr_num += s->list.num; |
| 1622 | s->have_section = false; |
| 1623 | } else { |
| 1624 | s->have_section = true; |
| 1625 | s->phdr_num = PN_XNUM; |
| 1626 | s->sh_info = 1; /* PT_NOTE */ |
| 1627 | |
| 1628 | /* the type of shdr->sh_info is uint32_t, so we should avoid overflow */ |
| 1629 | if (s->list.num <= UINT32_MAX - 1) { |
| 1630 | s->sh_info += s->list.num; |
| 1631 | } else { |
| 1632 | s->sh_info = UINT32_MAX; |
| 1633 | } |
| 1634 | } |
| 1635 | |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 1636 | if (s->dump_info.d_class == ELFCLASS64) { |
| 1637 | if (s->have_section) { |
| 1638 | s->memory_offset = sizeof(Elf64_Ehdr) + |
| 1639 | sizeof(Elf64_Phdr) * s->sh_info + |
| 1640 | sizeof(Elf64_Shdr) + s->note_size; |
| 1641 | } else { |
| 1642 | s->memory_offset = sizeof(Elf64_Ehdr) + |
| 1643 | sizeof(Elf64_Phdr) * s->phdr_num + s->note_size; |
| 1644 | } |
| 1645 | } else { |
| 1646 | if (s->have_section) { |
| 1647 | s->memory_offset = sizeof(Elf32_Ehdr) + |
| 1648 | sizeof(Elf32_Phdr) * s->sh_info + |
| 1649 | sizeof(Elf32_Shdr) + s->note_size; |
| 1650 | } else { |
| 1651 | s->memory_offset = sizeof(Elf32_Ehdr) + |
| 1652 | sizeof(Elf32_Phdr) * s->phdr_num + s->note_size; |
| 1653 | } |
| 1654 | } |
| 1655 | |
zhanghailiang | 4c7e251 | 2014-10-09 14:13:11 +0800 | [diff] [blame] | 1656 | return; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 1657 | |
| 1658 | cleanup: |
Chen Gang | 2928207 | 2014-08-03 23:28:56 +0800 | [diff] [blame] | 1659 | dump_cleanup(s); |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 1660 | } |
| 1661 | |
Peter Xu | ca1fc8c | 2016-02-18 13:16:50 +0800 | [diff] [blame] | 1662 | /* this operation might be time consuming. */ |
| 1663 | static void dump_process(DumpState *s, Error **errp) |
| 1664 | { |
| 1665 | Error *local_err = NULL; |
Peter Xu | d42a0d1 | 2016-02-18 13:16:56 +0800 | [diff] [blame] | 1666 | DumpQueryResult *result = NULL; |
Peter Xu | ca1fc8c | 2016-02-18 13:16:50 +0800 | [diff] [blame] | 1667 | |
| 1668 | if (s->has_format && s->format != DUMP_GUEST_MEMORY_FORMAT_ELF) { |
| 1669 | create_kdump_vmcore(s, &local_err); |
| 1670 | } else { |
| 1671 | create_vmcore(s, &local_err); |
| 1672 | } |
| 1673 | |
Peter Xu | 39ba2ea | 2016-02-18 13:16:54 +0800 | [diff] [blame] | 1674 | /* make sure status is written after written_size updates */ |
| 1675 | smp_wmb(); |
| 1676 | atomic_set(&s->status, |
| 1677 | (local_err ? DUMP_STATUS_FAILED : DUMP_STATUS_COMPLETED)); |
Peter Xu | ca1fc8c | 2016-02-18 13:16:50 +0800 | [diff] [blame] | 1678 | |
Peter Xu | d42a0d1 | 2016-02-18 13:16:56 +0800 | [diff] [blame] | 1679 | /* send DUMP_COMPLETED message (unconditionally) */ |
| 1680 | result = qmp_query_dump(NULL); |
| 1681 | /* should never fail */ |
| 1682 | assert(result); |
| 1683 | qapi_event_send_dump_completed(result, !!local_err, (local_err ? \ |
| 1684 | error_get_pretty(local_err) : NULL), |
| 1685 | &error_abort); |
| 1686 | qapi_free_DumpQueryResult(result); |
| 1687 | |
Peter Xu | 39ba2ea | 2016-02-18 13:16:54 +0800 | [diff] [blame] | 1688 | error_propagate(errp, local_err); |
Peter Xu | ca1fc8c | 2016-02-18 13:16:50 +0800 | [diff] [blame] | 1689 | dump_cleanup(s); |
| 1690 | } |
| 1691 | |
Peter Xu | 1fbeff7 | 2016-02-18 13:16:52 +0800 | [diff] [blame] | 1692 | static void *dump_thread(void *data) |
| 1693 | { |
| 1694 | Error *err = NULL; |
| 1695 | DumpState *s = (DumpState *)data; |
Peter Xu | 1fbeff7 | 2016-02-18 13:16:52 +0800 | [diff] [blame] | 1696 | dump_process(s, &err); |
Peter Xu | d42a0d1 | 2016-02-18 13:16:56 +0800 | [diff] [blame] | 1697 | error_free(err); |
Peter Xu | 1fbeff7 | 2016-02-18 13:16:52 +0800 | [diff] [blame] | 1698 | return NULL; |
| 1699 | } |
| 1700 | |
Peter Xu | 39ba2ea | 2016-02-18 13:16:54 +0800 | [diff] [blame] | 1701 | DumpQueryResult *qmp_query_dump(Error **errp) |
| 1702 | { |
| 1703 | DumpQueryResult *result = g_new(DumpQueryResult, 1); |
| 1704 | DumpState *state = &dump_state_global; |
| 1705 | result->status = atomic_read(&state->status); |
| 1706 | /* make sure we are reading status and written_size in order */ |
| 1707 | smp_rmb(); |
| 1708 | result->completed = state->written_size; |
| 1709 | result->total = state->total_size; |
| 1710 | return result; |
| 1711 | } |
| 1712 | |
Peter Xu | 228de9c | 2016-02-18 13:16:47 +0800 | [diff] [blame] | 1713 | void qmp_dump_guest_memory(bool paging, const char *file, |
| 1714 | bool has_detach, bool detach, |
| 1715 | bool has_begin, int64_t begin, bool has_length, |
qiaonuohan | b53ccc3 | 2014-02-18 14:11:36 +0800 | [diff] [blame] | 1716 | int64_t length, bool has_format, |
| 1717 | DumpGuestMemoryFormat format, Error **errp) |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 1718 | { |
| 1719 | const char *p; |
| 1720 | int fd = -1; |
| 1721 | DumpState *s; |
zhanghailiang | 4c7e251 | 2014-10-09 14:13:11 +0800 | [diff] [blame] | 1722 | Error *local_err = NULL; |
Peter Xu | 1fbeff7 | 2016-02-18 13:16:52 +0800 | [diff] [blame] | 1723 | bool detach_p = false; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 1724 | |
Peter Xu | 63e27f2 | 2016-02-18 13:16:51 +0800 | [diff] [blame] | 1725 | if (runstate_check(RUN_STATE_INMIGRATE)) { |
| 1726 | error_setg(errp, "Dump not allowed during incoming migration."); |
| 1727 | return; |
| 1728 | } |
| 1729 | |
Peter Xu | 65d64f3 | 2016-02-18 13:16:49 +0800 | [diff] [blame] | 1730 | /* if there is a dump in background, we should wait until the dump |
| 1731 | * finished */ |
| 1732 | if (dump_in_progress()) { |
| 1733 | error_setg(errp, "There is a dump in process, please wait."); |
| 1734 | return; |
| 1735 | } |
| 1736 | |
qiaonuohan | b53ccc3 | 2014-02-18 14:11:36 +0800 | [diff] [blame] | 1737 | /* |
| 1738 | * kdump-compressed format need the whole memory dumped, so paging or |
| 1739 | * filter is not supported here. |
| 1740 | */ |
| 1741 | if ((has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) && |
| 1742 | (paging || has_begin || has_length)) { |
| 1743 | error_setg(errp, "kdump-compressed format doesn't support paging or " |
| 1744 | "filter"); |
| 1745 | return; |
| 1746 | } |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 1747 | if (has_begin && !has_length) { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 1748 | error_setg(errp, QERR_MISSING_PARAMETER, "length"); |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 1749 | return; |
| 1750 | } |
| 1751 | if (!has_begin && has_length) { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 1752 | error_setg(errp, QERR_MISSING_PARAMETER, "begin"); |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 1753 | return; |
| 1754 | } |
Peter Xu | 1fbeff7 | 2016-02-18 13:16:52 +0800 | [diff] [blame] | 1755 | if (has_detach) { |
| 1756 | detach_p = detach; |
| 1757 | } |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 1758 | |
qiaonuohan | b53ccc3 | 2014-02-18 14:11:36 +0800 | [diff] [blame] | 1759 | /* check whether lzo/snappy is supported */ |
| 1760 | #ifndef CONFIG_LZO |
| 1761 | if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO) { |
| 1762 | error_setg(errp, "kdump-lzo is not available now"); |
| 1763 | return; |
| 1764 | } |
| 1765 | #endif |
| 1766 | |
| 1767 | #ifndef CONFIG_SNAPPY |
| 1768 | if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY) { |
| 1769 | error_setg(errp, "kdump-snappy is not available now"); |
| 1770 | return; |
| 1771 | } |
| 1772 | #endif |
| 1773 | |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 1774 | #if !defined(WIN32) |
| 1775 | if (strstart(file, "fd:", &p)) { |
Paolo Bonzini | a9940fc | 2012-09-20 16:50:32 +0200 | [diff] [blame] | 1776 | fd = monitor_get_fd(cur_mon, p, errp); |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 1777 | if (fd == -1) { |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 1778 | return; |
| 1779 | } |
| 1780 | } |
| 1781 | #endif |
| 1782 | |
| 1783 | if (strstart(file, "file:", &p)) { |
| 1784 | fd = qemu_open(p, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, S_IRUSR); |
| 1785 | if (fd < 0) { |
Luiz Capitulino | 7581766 | 2013-06-07 14:36:01 -0400 | [diff] [blame] | 1786 | error_setg_file_open(errp, errno, p); |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 1787 | return; |
| 1788 | } |
| 1789 | } |
| 1790 | |
| 1791 | if (fd == -1) { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 1792 | error_setg(errp, QERR_INVALID_PARAMETER, "protocol"); |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 1793 | return; |
| 1794 | } |
| 1795 | |
Peter Xu | baf28f5 | 2016-02-18 13:16:48 +0800 | [diff] [blame] | 1796 | s = &dump_state_global; |
| 1797 | dump_state_prepare(s); |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 1798 | |
zhanghailiang | 4c7e251 | 2014-10-09 14:13:11 +0800 | [diff] [blame] | 1799 | dump_init(s, fd, has_format, format, paging, has_begin, |
| 1800 | begin, length, &local_err); |
| 1801 | if (local_err) { |
zhanghailiang | 4c7e251 | 2014-10-09 14:13:11 +0800 | [diff] [blame] | 1802 | error_propagate(errp, local_err); |
Peter Xu | 39ba2ea | 2016-02-18 13:16:54 +0800 | [diff] [blame] | 1803 | atomic_set(&s->status, DUMP_STATUS_FAILED); |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 1804 | return; |
| 1805 | } |
| 1806 | |
Peter Xu | 1fbeff7 | 2016-02-18 13:16:52 +0800 | [diff] [blame] | 1807 | if (detach_p) { |
| 1808 | /* detached dump */ |
| 1809 | qemu_thread_create(&s->dump_thread, "dump_thread", dump_thread, |
| 1810 | s, QEMU_THREAD_DETACHED); |
| 1811 | } else { |
| 1812 | /* sync dump */ |
| 1813 | dump_process(s, errp); |
| 1814 | } |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 1815 | } |
qiaonuohan | 7d6dc7f | 2014-02-18 14:11:38 +0800 | [diff] [blame] | 1816 | |
| 1817 | DumpGuestMemoryCapability *qmp_query_dump_guest_memory_capability(Error **errp) |
| 1818 | { |
| 1819 | DumpGuestMemoryFormatList *item; |
| 1820 | DumpGuestMemoryCapability *cap = |
| 1821 | g_malloc0(sizeof(DumpGuestMemoryCapability)); |
| 1822 | |
| 1823 | /* elf is always available */ |
| 1824 | item = g_malloc0(sizeof(DumpGuestMemoryFormatList)); |
| 1825 | cap->formats = item; |
| 1826 | item->value = DUMP_GUEST_MEMORY_FORMAT_ELF; |
| 1827 | |
| 1828 | /* kdump-zlib is always available */ |
| 1829 | item->next = g_malloc0(sizeof(DumpGuestMemoryFormatList)); |
| 1830 | item = item->next; |
| 1831 | item->value = DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB; |
| 1832 | |
| 1833 | /* add new item if kdump-lzo is available */ |
| 1834 | #ifdef CONFIG_LZO |
| 1835 | item->next = g_malloc0(sizeof(DumpGuestMemoryFormatList)); |
| 1836 | item = item->next; |
| 1837 | item->value = DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO; |
| 1838 | #endif |
| 1839 | |
| 1840 | /* add new item if kdump-snappy is available */ |
| 1841 | #ifdef CONFIG_SNAPPY |
| 1842 | item->next = g_malloc0(sizeof(DumpGuestMemoryFormatList)); |
| 1843 | item = item->next; |
| 1844 | item->value = DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY; |
| 1845 | #endif |
| 1846 | |
| 1847 | return cap; |
| 1848 | } |