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