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 | |
| 14 | #include "qemu-common.h" |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 15 | #include "elf.h" |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 16 | #include "cpu.h" |
Paolo Bonzini | 022c62c | 2012-12-17 18:19:49 +0100 | [diff] [blame] | 17 | #include "exec/cpu-all.h" |
| 18 | #include "exec/hwaddr.h" |
Paolo Bonzini | 83c9089 | 2012-12-17 18:19:49 +0100 | [diff] [blame] | 19 | #include "monitor/monitor.h" |
Paolo Bonzini | 9c17d61 | 2012-12-17 18:20:04 +0100 | [diff] [blame] | 20 | #include "sysemu/kvm.h" |
| 21 | #include "sysemu/dump.h" |
| 22 | #include "sysemu/sysemu.h" |
| 23 | #include "sysemu/memory_mapping.h" |
Andreas Färber | 1b3509c | 2013-06-09 16:48:29 +0200 | [diff] [blame] | 24 | #include "sysemu/cpus.h" |
Paolo Bonzini | 7b1b5d1 | 2012-12-17 18:19:43 +0100 | [diff] [blame] | 25 | #include "qapi/error.h" |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 26 | #include "qmp-commands.h" |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 27 | |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 28 | static uint16_t cpu_convert_to_target16(uint16_t val, int endian) |
| 29 | { |
| 30 | if (endian == ELFDATA2LSB) { |
| 31 | val = cpu_to_le16(val); |
| 32 | } else { |
| 33 | val = cpu_to_be16(val); |
| 34 | } |
| 35 | |
| 36 | return val; |
| 37 | } |
| 38 | |
| 39 | static uint32_t cpu_convert_to_target32(uint32_t val, int endian) |
| 40 | { |
| 41 | if (endian == ELFDATA2LSB) { |
| 42 | val = cpu_to_le32(val); |
| 43 | } else { |
| 44 | val = cpu_to_be32(val); |
| 45 | } |
| 46 | |
| 47 | return val; |
| 48 | } |
| 49 | |
| 50 | static uint64_t cpu_convert_to_target64(uint64_t val, int endian) |
| 51 | { |
| 52 | if (endian == ELFDATA2LSB) { |
| 53 | val = cpu_to_le64(val); |
| 54 | } else { |
| 55 | val = cpu_to_be64(val); |
| 56 | } |
| 57 | |
| 58 | return val; |
| 59 | } |
| 60 | |
| 61 | typedef struct DumpState { |
Laszlo Ersek | 5ee163e | 2013-08-06 12:37:09 +0200 | [diff] [blame] | 62 | GuestPhysBlockList guest_phys_blocks; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 63 | ArchDumpInfo dump_info; |
| 64 | MemoryMappingList list; |
| 65 | uint16_t phdr_num; |
| 66 | uint32_t sh_info; |
| 67 | bool have_section; |
| 68 | bool resume; |
Aneesh Kumar K.V | bb6b684 | 2013-10-01 21:49:32 +0530 | [diff] [blame] | 69 | ssize_t note_size; |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 70 | hwaddr memory_offset; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 71 | int fd; |
| 72 | |
Laszlo Ersek | 56c4bfb | 2013-08-06 12:37:11 +0200 | [diff] [blame] | 73 | GuestPhysBlock *next_block; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 74 | ram_addr_t start; |
| 75 | bool has_filter; |
| 76 | int64_t begin; |
| 77 | int64_t length; |
| 78 | Error **errp; |
qiaonuohan | 4835ef7 | 2014-02-18 14:11:29 +0800 | [diff] [blame] | 79 | |
| 80 | uint8_t *note_buf; /* buffer for notes */ |
| 81 | size_t note_buf_offset; /* the writing place in note_buf */ |
qiaonuohan | 7aad248 | 2014-02-18 14:11:31 +0800 | [diff] [blame] | 82 | uint32_t nr_cpus; /* number of guest's cpu */ |
| 83 | size_t page_size; /* guest's page size */ |
| 84 | uint32_t page_shift; /* guest's page shift */ |
| 85 | uint64_t max_mapnr; /* the biggest guest's phys-mem's number */ |
| 86 | size_t len_dump_bitmap; /* the size of the place used to store |
| 87 | dump_bitmap in vmcore */ |
| 88 | off_t offset_dump_bitmap; /* offset of dump_bitmap part in vmcore */ |
| 89 | off_t offset_page; /* offset of page part in vmcore */ |
| 90 | size_t num_dumpable; /* number of page that can be dumped */ |
| 91 | uint32_t flag_compress; /* indicate the compression format */ |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 92 | } DumpState; |
| 93 | |
| 94 | static int dump_cleanup(DumpState *s) |
| 95 | { |
| 96 | int ret = 0; |
| 97 | |
Laszlo Ersek | 5ee163e | 2013-08-06 12:37:09 +0200 | [diff] [blame] | 98 | guest_phys_blocks_free(&s->guest_phys_blocks); |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 99 | memory_mapping_list_free(&s->list); |
| 100 | if (s->fd != -1) { |
| 101 | close(s->fd); |
| 102 | } |
| 103 | if (s->resume) { |
| 104 | vm_start(); |
| 105 | } |
| 106 | |
| 107 | return ret; |
| 108 | } |
| 109 | |
| 110 | static void dump_error(DumpState *s, const char *reason) |
| 111 | { |
| 112 | dump_cleanup(s); |
| 113 | } |
| 114 | |
qiaonuohan | b5ba1cc | 2014-02-18 14:11:25 +0800 | [diff] [blame] | 115 | 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] | 116 | { |
| 117 | DumpState *s = opaque; |
Luiz Capitulino | 2f61652 | 2012-09-21 13:17:55 -0300 | [diff] [blame] | 118 | size_t written_size; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 119 | |
Luiz Capitulino | 2f61652 | 2012-09-21 13:17:55 -0300 | [diff] [blame] | 120 | written_size = qemu_write_full(s->fd, buf, size); |
| 121 | if (written_size != size) { |
| 122 | return -1; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | static int write_elf64_header(DumpState *s) |
| 129 | { |
| 130 | Elf64_Ehdr elf_header; |
| 131 | int ret; |
| 132 | int endian = s->dump_info.d_endian; |
| 133 | |
| 134 | memset(&elf_header, 0, sizeof(Elf64_Ehdr)); |
| 135 | memcpy(&elf_header, ELFMAG, SELFMAG); |
| 136 | elf_header.e_ident[EI_CLASS] = ELFCLASS64; |
| 137 | elf_header.e_ident[EI_DATA] = s->dump_info.d_endian; |
| 138 | elf_header.e_ident[EI_VERSION] = EV_CURRENT; |
| 139 | elf_header.e_type = cpu_convert_to_target16(ET_CORE, endian); |
| 140 | elf_header.e_machine = cpu_convert_to_target16(s->dump_info.d_machine, |
| 141 | endian); |
| 142 | elf_header.e_version = cpu_convert_to_target32(EV_CURRENT, endian); |
| 143 | elf_header.e_ehsize = cpu_convert_to_target16(sizeof(elf_header), endian); |
| 144 | elf_header.e_phoff = cpu_convert_to_target64(sizeof(Elf64_Ehdr), endian); |
| 145 | elf_header.e_phentsize = cpu_convert_to_target16(sizeof(Elf64_Phdr), |
| 146 | endian); |
| 147 | elf_header.e_phnum = cpu_convert_to_target16(s->phdr_num, endian); |
| 148 | if (s->have_section) { |
| 149 | uint64_t shoff = sizeof(Elf64_Ehdr) + sizeof(Elf64_Phdr) * s->sh_info; |
| 150 | |
| 151 | elf_header.e_shoff = cpu_convert_to_target64(shoff, endian); |
| 152 | elf_header.e_shentsize = cpu_convert_to_target16(sizeof(Elf64_Shdr), |
| 153 | endian); |
| 154 | elf_header.e_shnum = cpu_convert_to_target16(1, endian); |
| 155 | } |
| 156 | |
| 157 | ret = fd_write_vmcore(&elf_header, sizeof(elf_header), s); |
| 158 | if (ret < 0) { |
| 159 | dump_error(s, "dump: failed to write elf header.\n"); |
| 160 | return -1; |
| 161 | } |
| 162 | |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | static int write_elf32_header(DumpState *s) |
| 167 | { |
| 168 | Elf32_Ehdr elf_header; |
| 169 | int ret; |
| 170 | int endian = s->dump_info.d_endian; |
| 171 | |
| 172 | memset(&elf_header, 0, sizeof(Elf32_Ehdr)); |
| 173 | memcpy(&elf_header, ELFMAG, SELFMAG); |
| 174 | elf_header.e_ident[EI_CLASS] = ELFCLASS32; |
| 175 | elf_header.e_ident[EI_DATA] = endian; |
| 176 | elf_header.e_ident[EI_VERSION] = EV_CURRENT; |
| 177 | elf_header.e_type = cpu_convert_to_target16(ET_CORE, endian); |
| 178 | elf_header.e_machine = cpu_convert_to_target16(s->dump_info.d_machine, |
| 179 | endian); |
| 180 | elf_header.e_version = cpu_convert_to_target32(EV_CURRENT, endian); |
| 181 | elf_header.e_ehsize = cpu_convert_to_target16(sizeof(elf_header), endian); |
| 182 | elf_header.e_phoff = cpu_convert_to_target32(sizeof(Elf32_Ehdr), endian); |
| 183 | elf_header.e_phentsize = cpu_convert_to_target16(sizeof(Elf32_Phdr), |
| 184 | endian); |
| 185 | elf_header.e_phnum = cpu_convert_to_target16(s->phdr_num, endian); |
| 186 | if (s->have_section) { |
| 187 | uint32_t shoff = sizeof(Elf32_Ehdr) + sizeof(Elf32_Phdr) * s->sh_info; |
| 188 | |
| 189 | elf_header.e_shoff = cpu_convert_to_target32(shoff, endian); |
| 190 | elf_header.e_shentsize = cpu_convert_to_target16(sizeof(Elf32_Shdr), |
| 191 | endian); |
| 192 | elf_header.e_shnum = cpu_convert_to_target16(1, endian); |
| 193 | } |
| 194 | |
| 195 | ret = fd_write_vmcore(&elf_header, sizeof(elf_header), s); |
| 196 | if (ret < 0) { |
| 197 | dump_error(s, "dump: failed to write elf header.\n"); |
| 198 | return -1; |
| 199 | } |
| 200 | |
| 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | static int write_elf64_load(DumpState *s, MemoryMapping *memory_mapping, |
Laszlo Ersek | 2cac260 | 2013-08-06 12:37:08 +0200 | [diff] [blame] | 205 | int phdr_index, hwaddr offset, |
| 206 | hwaddr filesz) |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 207 | { |
| 208 | Elf64_Phdr phdr; |
| 209 | int ret; |
| 210 | int endian = s->dump_info.d_endian; |
| 211 | |
| 212 | memset(&phdr, 0, sizeof(Elf64_Phdr)); |
| 213 | phdr.p_type = cpu_convert_to_target32(PT_LOAD, endian); |
| 214 | phdr.p_offset = cpu_convert_to_target64(offset, endian); |
| 215 | phdr.p_paddr = cpu_convert_to_target64(memory_mapping->phys_addr, endian); |
Laszlo Ersek | 2cac260 | 2013-08-06 12:37:08 +0200 | [diff] [blame] | 216 | phdr.p_filesz = cpu_convert_to_target64(filesz, endian); |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 217 | phdr.p_memsz = cpu_convert_to_target64(memory_mapping->length, endian); |
| 218 | phdr.p_vaddr = cpu_convert_to_target64(memory_mapping->virt_addr, endian); |
| 219 | |
Laszlo Ersek | 2cac260 | 2013-08-06 12:37:08 +0200 | [diff] [blame] | 220 | assert(memory_mapping->length >= filesz); |
| 221 | |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 222 | ret = fd_write_vmcore(&phdr, sizeof(Elf64_Phdr), s); |
| 223 | if (ret < 0) { |
| 224 | dump_error(s, "dump: failed to write program header table.\n"); |
| 225 | return -1; |
| 226 | } |
| 227 | |
| 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | static int write_elf32_load(DumpState *s, MemoryMapping *memory_mapping, |
Laszlo Ersek | 2cac260 | 2013-08-06 12:37:08 +0200 | [diff] [blame] | 232 | int phdr_index, hwaddr offset, |
| 233 | hwaddr filesz) |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 234 | { |
| 235 | Elf32_Phdr phdr; |
| 236 | int ret; |
| 237 | int endian = s->dump_info.d_endian; |
| 238 | |
| 239 | memset(&phdr, 0, sizeof(Elf32_Phdr)); |
| 240 | phdr.p_type = cpu_convert_to_target32(PT_LOAD, endian); |
| 241 | phdr.p_offset = cpu_convert_to_target32(offset, endian); |
| 242 | phdr.p_paddr = cpu_convert_to_target32(memory_mapping->phys_addr, endian); |
Laszlo Ersek | 2cac260 | 2013-08-06 12:37:08 +0200 | [diff] [blame] | 243 | phdr.p_filesz = cpu_convert_to_target32(filesz, endian); |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 244 | phdr.p_memsz = cpu_convert_to_target32(memory_mapping->length, endian); |
| 245 | phdr.p_vaddr = cpu_convert_to_target32(memory_mapping->virt_addr, endian); |
| 246 | |
Laszlo Ersek | 2cac260 | 2013-08-06 12:37:08 +0200 | [diff] [blame] | 247 | assert(memory_mapping->length >= filesz); |
| 248 | |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 249 | ret = fd_write_vmcore(&phdr, sizeof(Elf32_Phdr), s); |
| 250 | if (ret < 0) { |
| 251 | dump_error(s, "dump: failed to write program header table.\n"); |
| 252 | return -1; |
| 253 | } |
| 254 | |
| 255 | return 0; |
| 256 | } |
| 257 | |
| 258 | static int write_elf64_note(DumpState *s) |
| 259 | { |
| 260 | Elf64_Phdr phdr; |
| 261 | int endian = s->dump_info.d_endian; |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 262 | hwaddr begin = s->memory_offset - s->note_size; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 263 | int ret; |
| 264 | |
| 265 | memset(&phdr, 0, sizeof(Elf64_Phdr)); |
| 266 | phdr.p_type = cpu_convert_to_target32(PT_NOTE, endian); |
| 267 | phdr.p_offset = cpu_convert_to_target64(begin, endian); |
| 268 | phdr.p_paddr = 0; |
| 269 | phdr.p_filesz = cpu_convert_to_target64(s->note_size, endian); |
| 270 | phdr.p_memsz = cpu_convert_to_target64(s->note_size, endian); |
| 271 | phdr.p_vaddr = 0; |
| 272 | |
| 273 | ret = fd_write_vmcore(&phdr, sizeof(Elf64_Phdr), s); |
| 274 | if (ret < 0) { |
| 275 | dump_error(s, "dump: failed to write program header table.\n"); |
| 276 | return -1; |
| 277 | } |
| 278 | |
| 279 | return 0; |
| 280 | } |
| 281 | |
Paolo Bonzini | 0bc3cd6 | 2013-04-08 17:29:59 +0200 | [diff] [blame] | 282 | static inline int cpu_index(CPUState *cpu) |
| 283 | { |
| 284 | return cpu->cpu_index + 1; |
| 285 | } |
| 286 | |
qiaonuohan | 6a51991 | 2014-02-18 14:11:26 +0800 | [diff] [blame] | 287 | static int write_elf64_notes(WriteCoreDumpFunction f, DumpState *s) |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 288 | { |
Andreas Färber | 0d34282 | 2012-12-17 07:12:13 +0100 | [diff] [blame] | 289 | CPUState *cpu; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 290 | int ret; |
| 291 | int id; |
| 292 | |
Andreas Färber | bdc4464 | 2013-06-24 23:50:24 +0200 | [diff] [blame] | 293 | CPU_FOREACH(cpu) { |
Andreas Färber | 0d34282 | 2012-12-17 07:12:13 +0100 | [diff] [blame] | 294 | id = cpu_index(cpu); |
qiaonuohan | 6a51991 | 2014-02-18 14:11:26 +0800 | [diff] [blame] | 295 | ret = cpu_write_elf64_note(f, cpu, id, s); |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 296 | if (ret < 0) { |
| 297 | dump_error(s, "dump: failed to write elf notes.\n"); |
| 298 | return -1; |
| 299 | } |
| 300 | } |
| 301 | |
Andreas Färber | bdc4464 | 2013-06-24 23:50:24 +0200 | [diff] [blame] | 302 | CPU_FOREACH(cpu) { |
qiaonuohan | 6a51991 | 2014-02-18 14:11:26 +0800 | [diff] [blame] | 303 | ret = cpu_write_elf64_qemunote(f, cpu, s); |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 304 | if (ret < 0) { |
| 305 | dump_error(s, "dump: failed to write CPU status.\n"); |
| 306 | return -1; |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | return 0; |
| 311 | } |
| 312 | |
| 313 | static int write_elf32_note(DumpState *s) |
| 314 | { |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 315 | hwaddr begin = s->memory_offset - s->note_size; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 316 | Elf32_Phdr phdr; |
| 317 | int endian = s->dump_info.d_endian; |
| 318 | int ret; |
| 319 | |
| 320 | memset(&phdr, 0, sizeof(Elf32_Phdr)); |
| 321 | phdr.p_type = cpu_convert_to_target32(PT_NOTE, endian); |
| 322 | phdr.p_offset = cpu_convert_to_target32(begin, endian); |
| 323 | phdr.p_paddr = 0; |
| 324 | phdr.p_filesz = cpu_convert_to_target32(s->note_size, endian); |
| 325 | phdr.p_memsz = cpu_convert_to_target32(s->note_size, endian); |
| 326 | phdr.p_vaddr = 0; |
| 327 | |
| 328 | ret = fd_write_vmcore(&phdr, sizeof(Elf32_Phdr), s); |
| 329 | if (ret < 0) { |
| 330 | dump_error(s, "dump: failed to write program header table.\n"); |
| 331 | return -1; |
| 332 | } |
| 333 | |
| 334 | return 0; |
| 335 | } |
| 336 | |
qiaonuohan | 6a51991 | 2014-02-18 14:11:26 +0800 | [diff] [blame] | 337 | static int write_elf32_notes(WriteCoreDumpFunction f, DumpState *s) |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 338 | { |
Andreas Färber | 0d34282 | 2012-12-17 07:12:13 +0100 | [diff] [blame] | 339 | CPUState *cpu; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 340 | int ret; |
| 341 | int id; |
| 342 | |
Andreas Färber | bdc4464 | 2013-06-24 23:50:24 +0200 | [diff] [blame] | 343 | CPU_FOREACH(cpu) { |
Andreas Färber | 0d34282 | 2012-12-17 07:12:13 +0100 | [diff] [blame] | 344 | id = cpu_index(cpu); |
qiaonuohan | 6a51991 | 2014-02-18 14:11:26 +0800 | [diff] [blame] | 345 | ret = cpu_write_elf32_note(f, cpu, id, s); |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 346 | if (ret < 0) { |
| 347 | dump_error(s, "dump: failed to write elf notes.\n"); |
| 348 | return -1; |
| 349 | } |
| 350 | } |
| 351 | |
Andreas Färber | bdc4464 | 2013-06-24 23:50:24 +0200 | [diff] [blame] | 352 | CPU_FOREACH(cpu) { |
qiaonuohan | 6a51991 | 2014-02-18 14:11:26 +0800 | [diff] [blame] | 353 | ret = cpu_write_elf32_qemunote(f, cpu, s); |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 354 | if (ret < 0) { |
| 355 | dump_error(s, "dump: failed to write CPU status.\n"); |
| 356 | return -1; |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | return 0; |
| 361 | } |
| 362 | |
| 363 | static int write_elf_section(DumpState *s, int type) |
| 364 | { |
| 365 | Elf32_Shdr shdr32; |
| 366 | Elf64_Shdr shdr64; |
| 367 | int endian = s->dump_info.d_endian; |
| 368 | int shdr_size; |
| 369 | void *shdr; |
| 370 | int ret; |
| 371 | |
| 372 | if (type == 0) { |
| 373 | shdr_size = sizeof(Elf32_Shdr); |
| 374 | memset(&shdr32, 0, shdr_size); |
| 375 | shdr32.sh_info = cpu_convert_to_target32(s->sh_info, endian); |
| 376 | shdr = &shdr32; |
| 377 | } else { |
| 378 | shdr_size = sizeof(Elf64_Shdr); |
| 379 | memset(&shdr64, 0, shdr_size); |
| 380 | shdr64.sh_info = cpu_convert_to_target32(s->sh_info, endian); |
| 381 | shdr = &shdr64; |
| 382 | } |
| 383 | |
| 384 | ret = fd_write_vmcore(&shdr, shdr_size, s); |
| 385 | if (ret < 0) { |
| 386 | dump_error(s, "dump: failed to write section header table.\n"); |
| 387 | return -1; |
| 388 | } |
| 389 | |
| 390 | return 0; |
| 391 | } |
| 392 | |
| 393 | static int write_data(DumpState *s, void *buf, int length) |
| 394 | { |
| 395 | int ret; |
| 396 | |
| 397 | ret = fd_write_vmcore(buf, length, s); |
| 398 | if (ret < 0) { |
| 399 | dump_error(s, "dump: failed to save memory.\n"); |
| 400 | return -1; |
| 401 | } |
| 402 | |
| 403 | return 0; |
| 404 | } |
| 405 | |
| 406 | /* write the memroy to vmcore. 1 page per I/O. */ |
Laszlo Ersek | 56c4bfb | 2013-08-06 12:37:11 +0200 | [diff] [blame] | 407 | static int write_memory(DumpState *s, GuestPhysBlock *block, ram_addr_t start, |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 408 | int64_t size) |
| 409 | { |
| 410 | int64_t i; |
| 411 | int ret; |
| 412 | |
| 413 | for (i = 0; i < size / TARGET_PAGE_SIZE; i++) { |
Laszlo Ersek | 56c4bfb | 2013-08-06 12:37:11 +0200 | [diff] [blame] | 414 | ret = write_data(s, block->host_addr + start + i * TARGET_PAGE_SIZE, |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 415 | TARGET_PAGE_SIZE); |
| 416 | if (ret < 0) { |
| 417 | return ret; |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | if ((size % TARGET_PAGE_SIZE) != 0) { |
Laszlo Ersek | 56c4bfb | 2013-08-06 12:37:11 +0200 | [diff] [blame] | 422 | ret = write_data(s, block->host_addr + start + i * TARGET_PAGE_SIZE, |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 423 | size % TARGET_PAGE_SIZE); |
| 424 | if (ret < 0) { |
| 425 | return ret; |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | return 0; |
| 430 | } |
| 431 | |
Laszlo Ersek | 2cac260 | 2013-08-06 12:37:08 +0200 | [diff] [blame] | 432 | /* get the memory's offset and size in the vmcore */ |
| 433 | static void get_offset_range(hwaddr phys_addr, |
| 434 | ram_addr_t mapping_length, |
| 435 | DumpState *s, |
| 436 | hwaddr *p_offset, |
| 437 | hwaddr *p_filesz) |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 438 | { |
Laszlo Ersek | 56c4bfb | 2013-08-06 12:37:11 +0200 | [diff] [blame] | 439 | GuestPhysBlock *block; |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 440 | hwaddr offset = s->memory_offset; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 441 | int64_t size_in_block, start; |
| 442 | |
Laszlo Ersek | 2cac260 | 2013-08-06 12:37:08 +0200 | [diff] [blame] | 443 | /* When the memory is not stored into vmcore, offset will be -1 */ |
| 444 | *p_offset = -1; |
| 445 | *p_filesz = 0; |
| 446 | |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 447 | if (s->has_filter) { |
| 448 | if (phys_addr < s->begin || phys_addr >= s->begin + s->length) { |
Laszlo Ersek | 2cac260 | 2013-08-06 12:37:08 +0200 | [diff] [blame] | 449 | return; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 450 | } |
| 451 | } |
| 452 | |
Laszlo Ersek | 56c4bfb | 2013-08-06 12:37:11 +0200 | [diff] [blame] | 453 | QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) { |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 454 | if (s->has_filter) { |
Laszlo Ersek | 56c4bfb | 2013-08-06 12:37:11 +0200 | [diff] [blame] | 455 | if (block->target_start >= s->begin + s->length || |
| 456 | block->target_end <= s->begin) { |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 457 | /* This block is out of the range */ |
| 458 | continue; |
| 459 | } |
| 460 | |
Laszlo Ersek | 56c4bfb | 2013-08-06 12:37:11 +0200 | [diff] [blame] | 461 | if (s->begin <= block->target_start) { |
| 462 | start = block->target_start; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 463 | } else { |
| 464 | start = s->begin; |
| 465 | } |
| 466 | |
Laszlo Ersek | 56c4bfb | 2013-08-06 12:37:11 +0200 | [diff] [blame] | 467 | size_in_block = block->target_end - start; |
| 468 | if (s->begin + s->length < block->target_end) { |
| 469 | size_in_block -= block->target_end - (s->begin + s->length); |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 470 | } |
| 471 | } else { |
Laszlo Ersek | 56c4bfb | 2013-08-06 12:37:11 +0200 | [diff] [blame] | 472 | start = block->target_start; |
| 473 | size_in_block = block->target_end - block->target_start; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | if (phys_addr >= start && phys_addr < start + size_in_block) { |
Laszlo Ersek | 2cac260 | 2013-08-06 12:37:08 +0200 | [diff] [blame] | 477 | *p_offset = phys_addr - start + offset; |
| 478 | |
| 479 | /* The offset range mapped from the vmcore file must not spill over |
Laszlo Ersek | 56c4bfb | 2013-08-06 12:37:11 +0200 | [diff] [blame] | 480 | * the GuestPhysBlock, clamp it. The rest of the mapping will be |
Laszlo Ersek | 2cac260 | 2013-08-06 12:37:08 +0200 | [diff] [blame] | 481 | * zero-filled in memory at load time; see |
| 482 | * <http://refspecs.linuxbase.org/elf/gabi4+/ch5.pheader.html>. |
| 483 | */ |
| 484 | *p_filesz = phys_addr + mapping_length <= start + size_in_block ? |
| 485 | mapping_length : |
| 486 | size_in_block - (phys_addr - start); |
| 487 | return; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | offset += size_in_block; |
| 491 | } |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | static int write_elf_loads(DumpState *s) |
| 495 | { |
Laszlo Ersek | 2cac260 | 2013-08-06 12:37:08 +0200 | [diff] [blame] | 496 | hwaddr offset, filesz; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 497 | MemoryMapping *memory_mapping; |
| 498 | uint32_t phdr_index = 1; |
| 499 | int ret; |
| 500 | uint32_t max_index; |
| 501 | |
| 502 | if (s->have_section) { |
| 503 | max_index = s->sh_info; |
| 504 | } else { |
| 505 | max_index = s->phdr_num; |
| 506 | } |
| 507 | |
| 508 | QTAILQ_FOREACH(memory_mapping, &s->list.head, next) { |
Laszlo Ersek | 2cac260 | 2013-08-06 12:37:08 +0200 | [diff] [blame] | 509 | get_offset_range(memory_mapping->phys_addr, |
| 510 | memory_mapping->length, |
| 511 | s, &offset, &filesz); |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 512 | if (s->dump_info.d_class == ELFCLASS64) { |
Laszlo Ersek | 2cac260 | 2013-08-06 12:37:08 +0200 | [diff] [blame] | 513 | ret = write_elf64_load(s, memory_mapping, phdr_index++, offset, |
| 514 | filesz); |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 515 | } else { |
Laszlo Ersek | 2cac260 | 2013-08-06 12:37:08 +0200 | [diff] [blame] | 516 | ret = write_elf32_load(s, memory_mapping, phdr_index++, offset, |
| 517 | filesz); |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | if (ret < 0) { |
| 521 | return -1; |
| 522 | } |
| 523 | |
| 524 | if (phdr_index >= max_index) { |
| 525 | break; |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | return 0; |
| 530 | } |
| 531 | |
| 532 | /* write elf header, PT_NOTE and elf note to vmcore. */ |
| 533 | static int dump_begin(DumpState *s) |
| 534 | { |
| 535 | int ret; |
| 536 | |
| 537 | /* |
| 538 | * the vmcore's format is: |
| 539 | * -------------- |
| 540 | * | elf header | |
| 541 | * -------------- |
| 542 | * | PT_NOTE | |
| 543 | * -------------- |
| 544 | * | PT_LOAD | |
| 545 | * -------------- |
| 546 | * | ...... | |
| 547 | * -------------- |
| 548 | * | PT_LOAD | |
| 549 | * -------------- |
| 550 | * | sec_hdr | |
| 551 | * -------------- |
| 552 | * | elf note | |
| 553 | * -------------- |
| 554 | * | memory | |
| 555 | * -------------- |
| 556 | * |
| 557 | * we only know where the memory is saved after we write elf note into |
| 558 | * vmcore. |
| 559 | */ |
| 560 | |
| 561 | /* write elf header to vmcore */ |
| 562 | if (s->dump_info.d_class == ELFCLASS64) { |
| 563 | ret = write_elf64_header(s); |
| 564 | } else { |
| 565 | ret = write_elf32_header(s); |
| 566 | } |
| 567 | if (ret < 0) { |
| 568 | return -1; |
| 569 | } |
| 570 | |
| 571 | if (s->dump_info.d_class == ELFCLASS64) { |
| 572 | /* write PT_NOTE to vmcore */ |
| 573 | if (write_elf64_note(s) < 0) { |
| 574 | return -1; |
| 575 | } |
| 576 | |
| 577 | /* write all PT_LOAD to vmcore */ |
| 578 | if (write_elf_loads(s) < 0) { |
| 579 | return -1; |
| 580 | } |
| 581 | |
| 582 | /* write section to vmcore */ |
| 583 | if (s->have_section) { |
| 584 | if (write_elf_section(s, 1) < 0) { |
| 585 | return -1; |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | /* write notes to vmcore */ |
qiaonuohan | 6a51991 | 2014-02-18 14:11:26 +0800 | [diff] [blame] | 590 | if (write_elf64_notes(fd_write_vmcore, s) < 0) { |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 591 | return -1; |
| 592 | } |
| 593 | |
| 594 | } else { |
| 595 | /* write PT_NOTE to vmcore */ |
| 596 | if (write_elf32_note(s) < 0) { |
| 597 | return -1; |
| 598 | } |
| 599 | |
| 600 | /* write all PT_LOAD to vmcore */ |
| 601 | if (write_elf_loads(s) < 0) { |
| 602 | return -1; |
| 603 | } |
| 604 | |
| 605 | /* write section to vmcore */ |
| 606 | if (s->have_section) { |
| 607 | if (write_elf_section(s, 0) < 0) { |
| 608 | return -1; |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | /* write notes to vmcore */ |
qiaonuohan | 6a51991 | 2014-02-18 14:11:26 +0800 | [diff] [blame] | 613 | if (write_elf32_notes(fd_write_vmcore, s) < 0) { |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 614 | return -1; |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | return 0; |
| 619 | } |
| 620 | |
| 621 | /* write PT_LOAD to vmcore */ |
| 622 | static int dump_completed(DumpState *s) |
| 623 | { |
| 624 | dump_cleanup(s); |
| 625 | return 0; |
| 626 | } |
| 627 | |
Laszlo Ersek | 56c4bfb | 2013-08-06 12:37:11 +0200 | [diff] [blame] | 628 | static int get_next_block(DumpState *s, GuestPhysBlock *block) |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 629 | { |
| 630 | while (1) { |
Paolo Bonzini | a316103 | 2012-11-14 15:54:48 +0100 | [diff] [blame] | 631 | block = QTAILQ_NEXT(block, next); |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 632 | if (!block) { |
| 633 | /* no more block */ |
| 634 | return 1; |
| 635 | } |
| 636 | |
| 637 | s->start = 0; |
Laszlo Ersek | 56c4bfb | 2013-08-06 12:37:11 +0200 | [diff] [blame] | 638 | s->next_block = block; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 639 | if (s->has_filter) { |
Laszlo Ersek | 56c4bfb | 2013-08-06 12:37:11 +0200 | [diff] [blame] | 640 | if (block->target_start >= s->begin + s->length || |
| 641 | block->target_end <= s->begin) { |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 642 | /* This block is out of the range */ |
| 643 | continue; |
| 644 | } |
| 645 | |
Laszlo Ersek | 56c4bfb | 2013-08-06 12:37:11 +0200 | [diff] [blame] | 646 | if (s->begin > block->target_start) { |
| 647 | s->start = s->begin - block->target_start; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 648 | } |
| 649 | } |
| 650 | |
| 651 | return 0; |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | /* write all memory to vmcore */ |
| 656 | static int dump_iterate(DumpState *s) |
| 657 | { |
Laszlo Ersek | 56c4bfb | 2013-08-06 12:37:11 +0200 | [diff] [blame] | 658 | GuestPhysBlock *block; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 659 | int64_t size; |
| 660 | int ret; |
| 661 | |
| 662 | while (1) { |
Laszlo Ersek | 56c4bfb | 2013-08-06 12:37:11 +0200 | [diff] [blame] | 663 | block = s->next_block; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 664 | |
Laszlo Ersek | 56c4bfb | 2013-08-06 12:37:11 +0200 | [diff] [blame] | 665 | size = block->target_end - block->target_start; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 666 | if (s->has_filter) { |
| 667 | size -= s->start; |
Laszlo Ersek | 56c4bfb | 2013-08-06 12:37:11 +0200 | [diff] [blame] | 668 | if (s->begin + s->length < block->target_end) { |
| 669 | size -= block->target_end - (s->begin + s->length); |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 670 | } |
| 671 | } |
| 672 | ret = write_memory(s, block, s->start, size); |
| 673 | if (ret == -1) { |
| 674 | return ret; |
| 675 | } |
| 676 | |
| 677 | ret = get_next_block(s, block); |
| 678 | if (ret == 1) { |
| 679 | dump_completed(s); |
| 680 | return 0; |
| 681 | } |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | static int create_vmcore(DumpState *s) |
| 686 | { |
| 687 | int ret; |
| 688 | |
| 689 | ret = dump_begin(s); |
| 690 | if (ret < 0) { |
| 691 | return -1; |
| 692 | } |
| 693 | |
| 694 | ret = dump_iterate(s); |
| 695 | if (ret < 0) { |
| 696 | return -1; |
| 697 | } |
| 698 | |
| 699 | return 0; |
| 700 | } |
| 701 | |
qiaonuohan | fda0538 | 2014-02-18 14:11:27 +0800 | [diff] [blame] | 702 | static int write_start_flat_header(int fd) |
| 703 | { |
| 704 | uint8_t *buf; |
| 705 | MakedumpfileHeader mh; |
| 706 | int ret = 0; |
| 707 | |
| 708 | memset(&mh, 0, sizeof(mh)); |
| 709 | strncpy(mh.signature, MAKEDUMPFILE_SIGNATURE, |
| 710 | strlen(MAKEDUMPFILE_SIGNATURE)); |
| 711 | |
| 712 | mh.type = cpu_to_be64(TYPE_FLAT_HEADER); |
| 713 | mh.version = cpu_to_be64(VERSION_FLAT_HEADER); |
| 714 | |
| 715 | buf = g_malloc0(MAX_SIZE_MDF_HEADER); |
| 716 | memcpy(buf, &mh, sizeof(mh)); |
| 717 | |
| 718 | size_t written_size; |
| 719 | written_size = qemu_write_full(fd, buf, MAX_SIZE_MDF_HEADER); |
| 720 | if (written_size != MAX_SIZE_MDF_HEADER) { |
| 721 | ret = -1; |
| 722 | } |
| 723 | |
| 724 | g_free(buf); |
| 725 | return ret; |
| 726 | } |
| 727 | |
| 728 | static int write_end_flat_header(int fd) |
| 729 | { |
| 730 | MakedumpfileDataHeader mdh; |
| 731 | |
| 732 | mdh.offset = END_FLAG_FLAT_HEADER; |
| 733 | mdh.buf_size = END_FLAG_FLAT_HEADER; |
| 734 | |
| 735 | size_t written_size; |
| 736 | written_size = qemu_write_full(fd, &mdh, sizeof(mdh)); |
| 737 | if (written_size != sizeof(mdh)) { |
| 738 | return -1; |
| 739 | } |
| 740 | |
| 741 | return 0; |
| 742 | } |
| 743 | |
qiaonuohan | 5d31bab | 2014-02-18 14:11:28 +0800 | [diff] [blame] | 744 | static int write_buffer(int fd, off_t offset, const void *buf, size_t size) |
| 745 | { |
| 746 | size_t written_size; |
| 747 | MakedumpfileDataHeader mdh; |
| 748 | |
| 749 | mdh.offset = cpu_to_be64(offset); |
| 750 | mdh.buf_size = cpu_to_be64(size); |
| 751 | |
| 752 | written_size = qemu_write_full(fd, &mdh, sizeof(mdh)); |
| 753 | if (written_size != sizeof(mdh)) { |
| 754 | return -1; |
| 755 | } |
| 756 | |
| 757 | written_size = qemu_write_full(fd, buf, size); |
| 758 | if (written_size != size) { |
| 759 | return -1; |
| 760 | } |
| 761 | |
| 762 | return 0; |
| 763 | } |
| 764 | |
qiaonuohan | 4835ef7 | 2014-02-18 14:11:29 +0800 | [diff] [blame] | 765 | static int buf_write_note(const void *buf, size_t size, void *opaque) |
| 766 | { |
| 767 | DumpState *s = opaque; |
| 768 | |
| 769 | /* note_buf is not enough */ |
| 770 | if (s->note_buf_offset + size > s->note_size) { |
| 771 | return -1; |
| 772 | } |
| 773 | |
| 774 | memcpy(s->note_buf + s->note_buf_offset, buf, size); |
| 775 | |
| 776 | s->note_buf_offset += size; |
| 777 | |
| 778 | return 0; |
| 779 | } |
| 780 | |
qiaonuohan | 298f116 | 2014-02-18 14:11:32 +0800 | [diff] [blame^] | 781 | /* write common header, sub header and elf note to vmcore */ |
| 782 | static int create_header32(DumpState *s) |
| 783 | { |
| 784 | int ret = 0; |
| 785 | DiskDumpHeader32 *dh = NULL; |
| 786 | KdumpSubHeader32 *kh = NULL; |
| 787 | size_t size; |
| 788 | int endian = s->dump_info.d_endian; |
| 789 | uint32_t block_size; |
| 790 | uint32_t sub_hdr_size; |
| 791 | uint32_t bitmap_blocks; |
| 792 | uint32_t status = 0; |
| 793 | uint64_t offset_note; |
| 794 | |
| 795 | /* write common header, the version of kdump-compressed format is 6th */ |
| 796 | size = sizeof(DiskDumpHeader32); |
| 797 | dh = g_malloc0(size); |
| 798 | |
| 799 | strncpy(dh->signature, KDUMP_SIGNATURE, strlen(KDUMP_SIGNATURE)); |
| 800 | dh->header_version = cpu_convert_to_target32(6, endian); |
| 801 | block_size = s->page_size; |
| 802 | dh->block_size = cpu_convert_to_target32(block_size, endian); |
| 803 | sub_hdr_size = sizeof(struct KdumpSubHeader32) + s->note_size; |
| 804 | sub_hdr_size = DIV_ROUND_UP(sub_hdr_size, block_size); |
| 805 | dh->sub_hdr_size = cpu_convert_to_target32(sub_hdr_size, endian); |
| 806 | /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */ |
| 807 | dh->max_mapnr = cpu_convert_to_target32(MIN(s->max_mapnr, UINT_MAX), |
| 808 | endian); |
| 809 | dh->nr_cpus = cpu_convert_to_target32(s->nr_cpus, endian); |
| 810 | bitmap_blocks = DIV_ROUND_UP(s->len_dump_bitmap, block_size) * 2; |
| 811 | dh->bitmap_blocks = cpu_convert_to_target32(bitmap_blocks, endian); |
| 812 | memcpy(&(dh->utsname.machine), "i686", 4); |
| 813 | |
| 814 | if (s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) { |
| 815 | status |= DUMP_DH_COMPRESSED_ZLIB; |
| 816 | } |
| 817 | #ifdef CONFIG_LZO |
| 818 | if (s->flag_compress & DUMP_DH_COMPRESSED_LZO) { |
| 819 | status |= DUMP_DH_COMPRESSED_LZO; |
| 820 | } |
| 821 | #endif |
| 822 | #ifdef CONFIG_SNAPPY |
| 823 | if (s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) { |
| 824 | status |= DUMP_DH_COMPRESSED_SNAPPY; |
| 825 | } |
| 826 | #endif |
| 827 | dh->status = cpu_convert_to_target32(status, endian); |
| 828 | |
| 829 | if (write_buffer(s->fd, 0, dh, size) < 0) { |
| 830 | dump_error(s, "dump: failed to write disk dump header.\n"); |
| 831 | ret = -1; |
| 832 | goto out; |
| 833 | } |
| 834 | |
| 835 | /* write sub header */ |
| 836 | size = sizeof(KdumpSubHeader32); |
| 837 | kh = g_malloc0(size); |
| 838 | |
| 839 | /* 64bit max_mapnr_64 */ |
| 840 | kh->max_mapnr_64 = cpu_convert_to_target64(s->max_mapnr, endian); |
| 841 | kh->phys_base = cpu_convert_to_target32(PHYS_BASE, endian); |
| 842 | kh->dump_level = cpu_convert_to_target32(DUMP_LEVEL, endian); |
| 843 | |
| 844 | offset_note = DISKDUMP_HEADER_BLOCKS * block_size + size; |
| 845 | kh->offset_note = cpu_convert_to_target64(offset_note, endian); |
| 846 | kh->note_size = cpu_convert_to_target32(s->note_size, endian); |
| 847 | |
| 848 | if (write_buffer(s->fd, DISKDUMP_HEADER_BLOCKS * |
| 849 | block_size, kh, size) < 0) { |
| 850 | dump_error(s, "dump: failed to write kdump sub header.\n"); |
| 851 | ret = -1; |
| 852 | goto out; |
| 853 | } |
| 854 | |
| 855 | /* write note */ |
| 856 | s->note_buf = g_malloc0(s->note_size); |
| 857 | s->note_buf_offset = 0; |
| 858 | |
| 859 | /* use s->note_buf to store notes temporarily */ |
| 860 | if (write_elf32_notes(buf_write_note, s) < 0) { |
| 861 | ret = -1; |
| 862 | goto out; |
| 863 | } |
| 864 | |
| 865 | if (write_buffer(s->fd, offset_note, s->note_buf, |
| 866 | s->note_size) < 0) { |
| 867 | dump_error(s, "dump: failed to write notes"); |
| 868 | ret = -1; |
| 869 | goto out; |
| 870 | } |
| 871 | |
| 872 | /* get offset of dump_bitmap */ |
| 873 | s->offset_dump_bitmap = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size) * |
| 874 | block_size; |
| 875 | |
| 876 | /* get offset of page */ |
| 877 | s->offset_page = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size + bitmap_blocks) * |
| 878 | block_size; |
| 879 | |
| 880 | out: |
| 881 | g_free(dh); |
| 882 | g_free(kh); |
| 883 | g_free(s->note_buf); |
| 884 | |
| 885 | return ret; |
| 886 | } |
| 887 | |
| 888 | /* write common header, sub header and elf note to vmcore */ |
| 889 | static int create_header64(DumpState *s) |
| 890 | { |
| 891 | int ret = 0; |
| 892 | DiskDumpHeader64 *dh = NULL; |
| 893 | KdumpSubHeader64 *kh = NULL; |
| 894 | size_t size; |
| 895 | int endian = s->dump_info.d_endian; |
| 896 | uint32_t block_size; |
| 897 | uint32_t sub_hdr_size; |
| 898 | uint32_t bitmap_blocks; |
| 899 | uint32_t status = 0; |
| 900 | uint64_t offset_note; |
| 901 | |
| 902 | /* write common header, the version of kdump-compressed format is 6th */ |
| 903 | size = sizeof(DiskDumpHeader64); |
| 904 | dh = g_malloc0(size); |
| 905 | |
| 906 | strncpy(dh->signature, KDUMP_SIGNATURE, strlen(KDUMP_SIGNATURE)); |
| 907 | dh->header_version = cpu_convert_to_target32(6, endian); |
| 908 | block_size = s->page_size; |
| 909 | dh->block_size = cpu_convert_to_target32(block_size, endian); |
| 910 | sub_hdr_size = sizeof(struct KdumpSubHeader64) + s->note_size; |
| 911 | sub_hdr_size = DIV_ROUND_UP(sub_hdr_size, block_size); |
| 912 | dh->sub_hdr_size = cpu_convert_to_target32(sub_hdr_size, endian); |
| 913 | /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */ |
| 914 | dh->max_mapnr = cpu_convert_to_target32(MIN(s->max_mapnr, UINT_MAX), |
| 915 | endian); |
| 916 | dh->nr_cpus = cpu_convert_to_target32(s->nr_cpus, endian); |
| 917 | bitmap_blocks = DIV_ROUND_UP(s->len_dump_bitmap, block_size) * 2; |
| 918 | dh->bitmap_blocks = cpu_convert_to_target32(bitmap_blocks, endian); |
| 919 | memcpy(&(dh->utsname.machine), "x86_64", 6); |
| 920 | |
| 921 | if (s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) { |
| 922 | status |= DUMP_DH_COMPRESSED_ZLIB; |
| 923 | } |
| 924 | #ifdef CONFIG_LZO |
| 925 | if (s->flag_compress & DUMP_DH_COMPRESSED_LZO) { |
| 926 | status |= DUMP_DH_COMPRESSED_LZO; |
| 927 | } |
| 928 | #endif |
| 929 | #ifdef CONFIG_SNAPPY |
| 930 | if (s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) { |
| 931 | status |= DUMP_DH_COMPRESSED_SNAPPY; |
| 932 | } |
| 933 | #endif |
| 934 | dh->status = cpu_convert_to_target32(status, endian); |
| 935 | |
| 936 | if (write_buffer(s->fd, 0, dh, size) < 0) { |
| 937 | dump_error(s, "dump: failed to write disk dump header.\n"); |
| 938 | ret = -1; |
| 939 | goto out; |
| 940 | } |
| 941 | |
| 942 | /* write sub header */ |
| 943 | size = sizeof(KdumpSubHeader64); |
| 944 | kh = g_malloc0(size); |
| 945 | |
| 946 | /* 64bit max_mapnr_64 */ |
| 947 | kh->max_mapnr_64 = cpu_convert_to_target64(s->max_mapnr, endian); |
| 948 | kh->phys_base = cpu_convert_to_target64(PHYS_BASE, endian); |
| 949 | kh->dump_level = cpu_convert_to_target32(DUMP_LEVEL, endian); |
| 950 | |
| 951 | offset_note = DISKDUMP_HEADER_BLOCKS * block_size + size; |
| 952 | kh->offset_note = cpu_convert_to_target64(offset_note, endian); |
| 953 | kh->note_size = cpu_convert_to_target64(s->note_size, endian); |
| 954 | |
| 955 | if (write_buffer(s->fd, DISKDUMP_HEADER_BLOCKS * |
| 956 | block_size, kh, size) < 0) { |
| 957 | dump_error(s, "dump: failed to write kdump sub header.\n"); |
| 958 | ret = -1; |
| 959 | goto out; |
| 960 | } |
| 961 | |
| 962 | /* write note */ |
| 963 | s->note_buf = g_malloc0(s->note_size); |
| 964 | s->note_buf_offset = 0; |
| 965 | |
| 966 | /* use s->note_buf to store notes temporarily */ |
| 967 | if (write_elf64_notes(buf_write_note, s) < 0) { |
| 968 | ret = -1; |
| 969 | goto out; |
| 970 | } |
| 971 | |
| 972 | if (write_buffer(s->fd, offset_note, s->note_buf, |
| 973 | s->note_size) < 0) { |
| 974 | dump_error(s, "dump: failed to write notes"); |
| 975 | ret = -1; |
| 976 | goto out; |
| 977 | } |
| 978 | |
| 979 | /* get offset of dump_bitmap */ |
| 980 | s->offset_dump_bitmap = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size) * |
| 981 | block_size; |
| 982 | |
| 983 | /* get offset of page */ |
| 984 | s->offset_page = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size + bitmap_blocks) * |
| 985 | block_size; |
| 986 | |
| 987 | out: |
| 988 | g_free(dh); |
| 989 | g_free(kh); |
| 990 | g_free(s->note_buf); |
| 991 | |
| 992 | return ret; |
| 993 | } |
| 994 | |
| 995 | static int write_dump_header(DumpState *s) |
| 996 | { |
| 997 | if (s->dump_info.d_machine == EM_386) { |
| 998 | return create_header32(s); |
| 999 | } else { |
| 1000 | return create_header64(s); |
| 1001 | } |
| 1002 | } |
| 1003 | |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 1004 | static ram_addr_t get_start_block(DumpState *s) |
| 1005 | { |
Laszlo Ersek | 56c4bfb | 2013-08-06 12:37:11 +0200 | [diff] [blame] | 1006 | GuestPhysBlock *block; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 1007 | |
| 1008 | if (!s->has_filter) { |
Laszlo Ersek | 56c4bfb | 2013-08-06 12:37:11 +0200 | [diff] [blame] | 1009 | s->next_block = QTAILQ_FIRST(&s->guest_phys_blocks.head); |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 1010 | return 0; |
| 1011 | } |
| 1012 | |
Laszlo Ersek | 56c4bfb | 2013-08-06 12:37:11 +0200 | [diff] [blame] | 1013 | QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) { |
| 1014 | if (block->target_start >= s->begin + s->length || |
| 1015 | block->target_end <= s->begin) { |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 1016 | /* This block is out of the range */ |
| 1017 | continue; |
| 1018 | } |
| 1019 | |
Laszlo Ersek | 56c4bfb | 2013-08-06 12:37:11 +0200 | [diff] [blame] | 1020 | s->next_block = block; |
| 1021 | if (s->begin > block->target_start) { |
| 1022 | s->start = s->begin - block->target_start; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 1023 | } else { |
| 1024 | s->start = 0; |
| 1025 | } |
| 1026 | return s->start; |
| 1027 | } |
| 1028 | |
| 1029 | return -1; |
| 1030 | } |
| 1031 | |
qiaonuohan | 7aad248 | 2014-02-18 14:11:31 +0800 | [diff] [blame] | 1032 | static void get_max_mapnr(DumpState *s) |
| 1033 | { |
| 1034 | GuestPhysBlock *last_block; |
| 1035 | |
| 1036 | last_block = QTAILQ_LAST(&s->guest_phys_blocks.head, GuestPhysBlockHead); |
| 1037 | s->max_mapnr = paddr_to_pfn(last_block->target_end, s->page_shift); |
| 1038 | } |
| 1039 | |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 1040 | static int dump_init(DumpState *s, int fd, bool paging, bool has_filter, |
| 1041 | int64_t begin, int64_t length, Error **errp) |
| 1042 | { |
Andreas Färber | 182735e | 2013-05-29 22:29:20 +0200 | [diff] [blame] | 1043 | CPUState *cpu; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 1044 | int nr_cpus; |
Andreas Färber | 11ed09c | 2013-05-29 21:54:03 +0200 | [diff] [blame] | 1045 | Error *err = NULL; |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 1046 | int ret; |
| 1047 | |
| 1048 | if (runstate_is_running()) { |
| 1049 | vm_stop(RUN_STATE_SAVE_VM); |
| 1050 | s->resume = true; |
| 1051 | } else { |
| 1052 | s->resume = false; |
| 1053 | } |
| 1054 | |
Laszlo Ersek | 5ee163e | 2013-08-06 12:37:09 +0200 | [diff] [blame] | 1055 | /* If we use KVM, we should synchronize the registers before we get dump |
| 1056 | * info or physmap info. |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 1057 | */ |
Andreas Färber | 1b3509c | 2013-06-09 16:48:29 +0200 | [diff] [blame] | 1058 | cpu_synchronize_all_states(); |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 1059 | nr_cpus = 0; |
Andreas Färber | bdc4464 | 2013-06-24 23:50:24 +0200 | [diff] [blame] | 1060 | CPU_FOREACH(cpu) { |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 1061 | nr_cpus++; |
| 1062 | } |
| 1063 | |
Laszlo Ersek | 5ee163e | 2013-08-06 12:37:09 +0200 | [diff] [blame] | 1064 | s->errp = errp; |
| 1065 | s->fd = fd; |
| 1066 | s->has_filter = has_filter; |
| 1067 | s->begin = begin; |
| 1068 | s->length = length; |
| 1069 | |
| 1070 | guest_phys_blocks_init(&s->guest_phys_blocks); |
Laszlo Ersek | c5d7f60 | 2013-08-06 12:37:10 +0200 | [diff] [blame] | 1071 | guest_phys_blocks_append(&s->guest_phys_blocks); |
Laszlo Ersek | 5ee163e | 2013-08-06 12:37:09 +0200 | [diff] [blame] | 1072 | |
| 1073 | s->start = get_start_block(s); |
| 1074 | if (s->start == -1) { |
| 1075 | error_set(errp, QERR_INVALID_PARAMETER, "begin"); |
| 1076 | goto cleanup; |
| 1077 | } |
| 1078 | |
| 1079 | /* get dump info: endian, class and architecture. |
| 1080 | * If the target architecture is not supported, cpu_get_dump_info() will |
| 1081 | * return -1. |
| 1082 | */ |
Laszlo Ersek | 56c4bfb | 2013-08-06 12:37:11 +0200 | [diff] [blame] | 1083 | ret = cpu_get_dump_info(&s->dump_info, &s->guest_phys_blocks); |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 1084 | if (ret < 0) { |
| 1085 | error_set(errp, QERR_UNSUPPORTED); |
| 1086 | goto cleanup; |
| 1087 | } |
| 1088 | |
Paolo Bonzini | 4720bd0 | 2012-06-07 08:48:09 +0200 | [diff] [blame] | 1089 | s->note_size = cpu_get_note_size(s->dump_info.d_class, |
| 1090 | s->dump_info.d_machine, nr_cpus); |
Aneesh Kumar K.V | bb6b684 | 2013-10-01 21:49:32 +0530 | [diff] [blame] | 1091 | if (s->note_size < 0) { |
Paolo Bonzini | 4720bd0 | 2012-06-07 08:48:09 +0200 | [diff] [blame] | 1092 | error_set(errp, QERR_UNSUPPORTED); |
| 1093 | goto cleanup; |
| 1094 | } |
| 1095 | |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 1096 | /* get memory mapping */ |
| 1097 | memory_mapping_list_init(&s->list); |
| 1098 | if (paging) { |
Laszlo Ersek | 56c4bfb | 2013-08-06 12:37:11 +0200 | [diff] [blame] | 1099 | 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] | 1100 | if (err != NULL) { |
| 1101 | error_propagate(errp, err); |
| 1102 | goto cleanup; |
| 1103 | } |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 1104 | } else { |
Laszlo Ersek | 56c4bfb | 2013-08-06 12:37:11 +0200 | [diff] [blame] | 1105 | qemu_get_guest_simple_memory_mapping(&s->list, &s->guest_phys_blocks); |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 1106 | } |
| 1107 | |
qiaonuohan | 7aad248 | 2014-02-18 14:11:31 +0800 | [diff] [blame] | 1108 | s->nr_cpus = nr_cpus; |
| 1109 | s->page_size = TARGET_PAGE_SIZE; |
| 1110 | s->page_shift = ffs(s->page_size) - 1; |
| 1111 | |
| 1112 | get_max_mapnr(s); |
| 1113 | |
| 1114 | uint64_t tmp; |
| 1115 | tmp = DIV_ROUND_UP(DIV_ROUND_UP(s->max_mapnr, CHAR_BIT), s->page_size); |
| 1116 | s->len_dump_bitmap = tmp * s->page_size; |
| 1117 | |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 1118 | if (s->has_filter) { |
| 1119 | memory_mapping_filter(&s->list, s->begin, s->length); |
| 1120 | } |
| 1121 | |
| 1122 | /* |
| 1123 | * calculate phdr_num |
| 1124 | * |
| 1125 | * the type of ehdr->e_phnum is uint16_t, so we should avoid overflow |
| 1126 | */ |
| 1127 | s->phdr_num = 1; /* PT_NOTE */ |
| 1128 | if (s->list.num < UINT16_MAX - 2) { |
| 1129 | s->phdr_num += s->list.num; |
| 1130 | s->have_section = false; |
| 1131 | } else { |
| 1132 | s->have_section = true; |
| 1133 | s->phdr_num = PN_XNUM; |
| 1134 | s->sh_info = 1; /* PT_NOTE */ |
| 1135 | |
| 1136 | /* the type of shdr->sh_info is uint32_t, so we should avoid overflow */ |
| 1137 | if (s->list.num <= UINT32_MAX - 1) { |
| 1138 | s->sh_info += s->list.num; |
| 1139 | } else { |
| 1140 | s->sh_info = UINT32_MAX; |
| 1141 | } |
| 1142 | } |
| 1143 | |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 1144 | if (s->dump_info.d_class == ELFCLASS64) { |
| 1145 | if (s->have_section) { |
| 1146 | s->memory_offset = sizeof(Elf64_Ehdr) + |
| 1147 | sizeof(Elf64_Phdr) * s->sh_info + |
| 1148 | sizeof(Elf64_Shdr) + s->note_size; |
| 1149 | } else { |
| 1150 | s->memory_offset = sizeof(Elf64_Ehdr) + |
| 1151 | sizeof(Elf64_Phdr) * s->phdr_num + s->note_size; |
| 1152 | } |
| 1153 | } else { |
| 1154 | if (s->have_section) { |
| 1155 | s->memory_offset = sizeof(Elf32_Ehdr) + |
| 1156 | sizeof(Elf32_Phdr) * s->sh_info + |
| 1157 | sizeof(Elf32_Shdr) + s->note_size; |
| 1158 | } else { |
| 1159 | s->memory_offset = sizeof(Elf32_Ehdr) + |
| 1160 | sizeof(Elf32_Phdr) * s->phdr_num + s->note_size; |
| 1161 | } |
| 1162 | } |
| 1163 | |
| 1164 | return 0; |
| 1165 | |
| 1166 | cleanup: |
Laszlo Ersek | 5ee163e | 2013-08-06 12:37:09 +0200 | [diff] [blame] | 1167 | guest_phys_blocks_free(&s->guest_phys_blocks); |
| 1168 | |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 1169 | if (s->resume) { |
| 1170 | vm_start(); |
| 1171 | } |
| 1172 | |
| 1173 | return -1; |
| 1174 | } |
| 1175 | |
| 1176 | void qmp_dump_guest_memory(bool paging, const char *file, bool has_begin, |
| 1177 | int64_t begin, bool has_length, int64_t length, |
| 1178 | Error **errp) |
| 1179 | { |
| 1180 | const char *p; |
| 1181 | int fd = -1; |
| 1182 | DumpState *s; |
| 1183 | int ret; |
| 1184 | |
| 1185 | if (has_begin && !has_length) { |
| 1186 | error_set(errp, QERR_MISSING_PARAMETER, "length"); |
| 1187 | return; |
| 1188 | } |
| 1189 | if (!has_begin && has_length) { |
| 1190 | error_set(errp, QERR_MISSING_PARAMETER, "begin"); |
| 1191 | return; |
| 1192 | } |
| 1193 | |
| 1194 | #if !defined(WIN32) |
| 1195 | if (strstart(file, "fd:", &p)) { |
Paolo Bonzini | a9940fc | 2012-09-20 16:50:32 +0200 | [diff] [blame] | 1196 | fd = monitor_get_fd(cur_mon, p, errp); |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 1197 | if (fd == -1) { |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 1198 | return; |
| 1199 | } |
| 1200 | } |
| 1201 | #endif |
| 1202 | |
| 1203 | if (strstart(file, "file:", &p)) { |
| 1204 | fd = qemu_open(p, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, S_IRUSR); |
| 1205 | if (fd < 0) { |
Luiz Capitulino | 7581766 | 2013-06-07 14:36:01 -0400 | [diff] [blame] | 1206 | error_setg_file_open(errp, errno, p); |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 1207 | return; |
| 1208 | } |
| 1209 | } |
| 1210 | |
| 1211 | if (fd == -1) { |
| 1212 | error_set(errp, QERR_INVALID_PARAMETER, "protocol"); |
| 1213 | return; |
| 1214 | } |
| 1215 | |
Laszlo Ersek | 5ee163e | 2013-08-06 12:37:09 +0200 | [diff] [blame] | 1216 | s = g_malloc0(sizeof(DumpState)); |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 1217 | |
| 1218 | ret = dump_init(s, fd, paging, has_begin, begin, length, errp); |
| 1219 | if (ret < 0) { |
| 1220 | g_free(s); |
| 1221 | return; |
| 1222 | } |
| 1223 | |
| 1224 | if (create_vmcore(s) < 0 && !error_is_set(s->errp)) { |
| 1225 | error_set(errp, QERR_IO_ERROR); |
| 1226 | } |
| 1227 | |
| 1228 | g_free(s); |
| 1229 | } |