Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 1 | /* |
| 2 | * fs/proc/vmcore.c Interface for accessing the crash |
| 3 | * dump from the system's previous life. |
| 4 | * Heavily borrowed from fs/proc/kcore.c |
| 5 | * Created by: Hariprasad Nellitheertha (hari@in.ibm.com) |
| 6 | * Copyright (C) IBM Corporation, 2004. All rights reserved |
| 7 | * |
| 8 | */ |
| 9 | |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 10 | #include <linux/mm.h> |
David Howells | 2f96b8c | 2013-04-12 00:10:25 +0100 | [diff] [blame] | 11 | #include <linux/kcore.h> |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 12 | #include <linux/user.h> |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 13 | #include <linux/elf.h> |
| 14 | #include <linux/elfcore.h> |
Paul Gortmaker | afeacc8 | 2011-05-26 16:00:52 -0400 | [diff] [blame] | 15 | #include <linux/export.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 16 | #include <linux/slab.h> |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 17 | #include <linux/highmem.h> |
Andrew Morton | 87ebdc0 | 2013-02-27 17:03:16 -0800 | [diff] [blame] | 18 | #include <linux/printk.h> |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 19 | #include <linux/bootmem.h> |
| 20 | #include <linux/init.h> |
| 21 | #include <linux/crash_dump.h> |
| 22 | #include <linux/list.h> |
Rahul Lakkireddy | 2724273 | 2018-05-02 15:17:17 +0530 | [diff] [blame] | 23 | #include <linux/mutex.h> |
HATAYAMA Daisuke | 8308697 | 2013-07-03 15:02:23 -0700 | [diff] [blame] | 24 | #include <linux/vmalloc.h> |
Michael Holzheu | 9cb2181 | 2013-09-11 14:24:51 -0700 | [diff] [blame] | 25 | #include <linux/pagemap.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 26 | #include <linux/uaccess.h> |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 27 | #include <asm/io.h> |
David Howells | 2f96b8c | 2013-04-12 00:10:25 +0100 | [diff] [blame] | 28 | #include "internal.h" |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 29 | |
| 30 | /* List representing chunks of contiguous memory areas and their offsets in |
| 31 | * vmcore file. |
| 32 | */ |
| 33 | static LIST_HEAD(vmcore_list); |
| 34 | |
| 35 | /* Stores the pointer to the buffer containing kernel elf core headers. */ |
| 36 | static char *elfcorebuf; |
| 37 | static size_t elfcorebuf_sz; |
HATAYAMA Daisuke | f2bdacd | 2013-07-03 15:02:14 -0700 | [diff] [blame] | 38 | static size_t elfcorebuf_sz_orig; |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 39 | |
HATAYAMA Daisuke | 087350c | 2013-07-03 15:02:19 -0700 | [diff] [blame] | 40 | static char *elfnotes_buf; |
| 41 | static size_t elfnotes_sz; |
Rahul Lakkireddy | 7efe48d | 2018-05-02 15:17:18 +0530 | [diff] [blame] | 42 | /* Size of all notes minus the device dump notes */ |
| 43 | static size_t elfnotes_orig_sz; |
HATAYAMA Daisuke | 087350c | 2013-07-03 15:02:19 -0700 | [diff] [blame] | 44 | |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 45 | /* Total size of vmcore file. */ |
| 46 | static u64 vmcore_size; |
| 47 | |
Fabian Frederick | a05e16ad | 2014-06-06 14:37:04 -0700 | [diff] [blame] | 48 | static struct proc_dir_entry *proc_vmcore; |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 49 | |
Rahul Lakkireddy | 2724273 | 2018-05-02 15:17:17 +0530 | [diff] [blame] | 50 | #ifdef CONFIG_PROC_VMCORE_DEVICE_DUMP |
| 51 | /* Device Dump list and mutex to synchronize access to list */ |
| 52 | static LIST_HEAD(vmcoredd_list); |
| 53 | static DEFINE_MUTEX(vmcoredd_mutex); |
| 54 | #endif /* CONFIG_PROC_VMCORE_DEVICE_DUMP */ |
| 55 | |
Rahul Lakkireddy | 7efe48d | 2018-05-02 15:17:18 +0530 | [diff] [blame] | 56 | /* Device Dump Size */ |
| 57 | static size_t vmcoredd_orig_sz; |
| 58 | |
Olaf Hering | 997c136 | 2011-05-26 16:25:54 -0700 | [diff] [blame] | 59 | /* |
| 60 | * Returns > 0 for RAM pages, 0 for non-RAM pages, < 0 on error |
| 61 | * The called function has to take care of module refcounting. |
| 62 | */ |
| 63 | static int (*oldmem_pfn_is_ram)(unsigned long pfn); |
| 64 | |
| 65 | int register_oldmem_pfn_is_ram(int (*fn)(unsigned long pfn)) |
| 66 | { |
| 67 | if (oldmem_pfn_is_ram) |
| 68 | return -EBUSY; |
| 69 | oldmem_pfn_is_ram = fn; |
| 70 | return 0; |
| 71 | } |
| 72 | EXPORT_SYMBOL_GPL(register_oldmem_pfn_is_ram); |
| 73 | |
| 74 | void unregister_oldmem_pfn_is_ram(void) |
| 75 | { |
| 76 | oldmem_pfn_is_ram = NULL; |
| 77 | wmb(); |
| 78 | } |
| 79 | EXPORT_SYMBOL_GPL(unregister_oldmem_pfn_is_ram); |
| 80 | |
| 81 | static int pfn_is_ram(unsigned long pfn) |
| 82 | { |
| 83 | int (*fn)(unsigned long pfn); |
| 84 | /* pfn is ram unless fn() checks pagetype */ |
| 85 | int ret = 1; |
| 86 | |
| 87 | /* |
| 88 | * Ask hypervisor if the pfn is really ram. |
| 89 | * A ballooned page contains no data and reading from such a page |
| 90 | * will cause high load in the hypervisor. |
| 91 | */ |
| 92 | fn = oldmem_pfn_is_ram; |
| 93 | if (fn) |
| 94 | ret = fn(pfn); |
| 95 | |
| 96 | return ret; |
| 97 | } |
| 98 | |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 99 | /* Reads a page from the oldmem device from given offset. */ |
| 100 | static ssize_t read_from_oldmem(char *buf, size_t count, |
Vivek Goyal | 9e9e394 | 2006-01-11 12:17:37 -0800 | [diff] [blame] | 101 | u64 *ppos, int userbuf) |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 102 | { |
| 103 | unsigned long pfn, offset; |
| 104 | size_t nr_bytes; |
| 105 | ssize_t read = 0, tmp; |
| 106 | |
| 107 | if (!count) |
| 108 | return 0; |
| 109 | |
| 110 | offset = (unsigned long)(*ppos % PAGE_SIZE); |
| 111 | pfn = (unsigned long)(*ppos / PAGE_SIZE); |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 112 | |
| 113 | do { |
| 114 | if (count > (PAGE_SIZE - offset)) |
| 115 | nr_bytes = PAGE_SIZE - offset; |
| 116 | else |
| 117 | nr_bytes = count; |
| 118 | |
Olaf Hering | 997c136 | 2011-05-26 16:25:54 -0700 | [diff] [blame] | 119 | /* If pfn is not ram, return zeros for sparse dump files */ |
| 120 | if (pfn_is_ram(pfn) == 0) |
| 121 | memset(buf, 0, nr_bytes); |
| 122 | else { |
| 123 | tmp = copy_oldmem_page(pfn, buf, nr_bytes, |
| 124 | offset, userbuf); |
| 125 | if (tmp < 0) |
| 126 | return tmp; |
| 127 | } |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 128 | *ppos += nr_bytes; |
| 129 | count -= nr_bytes; |
| 130 | buf += nr_bytes; |
| 131 | read += nr_bytes; |
| 132 | ++pfn; |
| 133 | offset = 0; |
| 134 | } while (count); |
| 135 | |
| 136 | return read; |
| 137 | } |
| 138 | |
Michael Holzheu | be8a8d0 | 2013-09-11 14:24:49 -0700 | [diff] [blame] | 139 | /* |
| 140 | * Architectures may override this function to allocate ELF header in 2nd kernel |
| 141 | */ |
| 142 | int __weak elfcorehdr_alloc(unsigned long long *addr, unsigned long long *size) |
| 143 | { |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | /* |
| 148 | * Architectures may override this function to free header |
| 149 | */ |
| 150 | void __weak elfcorehdr_free(unsigned long long addr) |
| 151 | {} |
| 152 | |
| 153 | /* |
| 154 | * Architectures may override this function to read from ELF header |
| 155 | */ |
| 156 | ssize_t __weak elfcorehdr_read(char *buf, size_t count, u64 *ppos) |
| 157 | { |
| 158 | return read_from_oldmem(buf, count, ppos, 0); |
| 159 | } |
| 160 | |
| 161 | /* |
| 162 | * Architectures may override this function to read from notes sections |
| 163 | */ |
| 164 | ssize_t __weak elfcorehdr_read_notes(char *buf, size_t count, u64 *ppos) |
| 165 | { |
| 166 | return read_from_oldmem(buf, count, ppos, 0); |
| 167 | } |
| 168 | |
Michael Holzheu | 9cb2181 | 2013-09-11 14:24:51 -0700 | [diff] [blame] | 169 | /* |
| 170 | * Architectures may override this function to map oldmem |
| 171 | */ |
| 172 | int __weak remap_oldmem_pfn_range(struct vm_area_struct *vma, |
| 173 | unsigned long from, unsigned long pfn, |
| 174 | unsigned long size, pgprot_t prot) |
| 175 | { |
| 176 | return remap_pfn_range(vma, from, pfn, size, prot); |
| 177 | } |
| 178 | |
| 179 | /* |
| 180 | * Copy to either kernel or user space |
| 181 | */ |
| 182 | static int copy_to(void *target, void *src, size_t size, int userbuf) |
| 183 | { |
| 184 | if (userbuf) { |
| 185 | if (copy_to_user((char __user *) target, src, size)) |
| 186 | return -EFAULT; |
| 187 | } else { |
| 188 | memcpy(target, src, size); |
| 189 | } |
| 190 | return 0; |
| 191 | } |
| 192 | |
Rahul Lakkireddy | 7efe48d | 2018-05-02 15:17:18 +0530 | [diff] [blame] | 193 | #ifdef CONFIG_PROC_VMCORE_DEVICE_DUMP |
| 194 | static int vmcoredd_copy_dumps(void *dst, u64 start, size_t size, int userbuf) |
| 195 | { |
| 196 | struct vmcoredd_node *dump; |
| 197 | u64 offset = 0; |
| 198 | int ret = 0; |
| 199 | size_t tsz; |
| 200 | char *buf; |
| 201 | |
| 202 | mutex_lock(&vmcoredd_mutex); |
| 203 | list_for_each_entry(dump, &vmcoredd_list, list) { |
| 204 | if (start < offset + dump->size) { |
| 205 | tsz = min(offset + (u64)dump->size - start, (u64)size); |
| 206 | buf = dump->buf + start - offset; |
| 207 | if (copy_to(dst, buf, tsz, userbuf)) { |
| 208 | ret = -EFAULT; |
| 209 | goto out_unlock; |
| 210 | } |
| 211 | |
| 212 | size -= tsz; |
| 213 | start += tsz; |
| 214 | dst += tsz; |
| 215 | |
| 216 | /* Leave now if buffer filled already */ |
| 217 | if (!size) |
| 218 | goto out_unlock; |
| 219 | } |
| 220 | offset += dump->size; |
| 221 | } |
| 222 | |
| 223 | out_unlock: |
| 224 | mutex_unlock(&vmcoredd_mutex); |
| 225 | return ret; |
| 226 | } |
| 227 | |
| 228 | static int vmcoredd_mmap_dumps(struct vm_area_struct *vma, unsigned long dst, |
| 229 | u64 start, size_t size) |
| 230 | { |
| 231 | struct vmcoredd_node *dump; |
| 232 | u64 offset = 0; |
| 233 | int ret = 0; |
| 234 | size_t tsz; |
| 235 | char *buf; |
| 236 | |
| 237 | mutex_lock(&vmcoredd_mutex); |
| 238 | list_for_each_entry(dump, &vmcoredd_list, list) { |
| 239 | if (start < offset + dump->size) { |
| 240 | tsz = min(offset + (u64)dump->size - start, (u64)size); |
| 241 | buf = dump->buf + start - offset; |
| 242 | if (remap_vmalloc_range_partial(vma, dst, buf, tsz)) { |
| 243 | ret = -EFAULT; |
| 244 | goto out_unlock; |
| 245 | } |
| 246 | |
| 247 | size -= tsz; |
| 248 | start += tsz; |
| 249 | dst += tsz; |
| 250 | |
| 251 | /* Leave now if buffer filled already */ |
| 252 | if (!size) |
| 253 | goto out_unlock; |
| 254 | } |
| 255 | offset += dump->size; |
| 256 | } |
| 257 | |
| 258 | out_unlock: |
| 259 | mutex_unlock(&vmcoredd_mutex); |
| 260 | return ret; |
| 261 | } |
| 262 | #endif /* CONFIG_PROC_VMCORE_DEVICE_DUMP */ |
| 263 | |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 264 | /* Read from the ELF header and then the crash dump. On error, negative value is |
| 265 | * returned otherwise number of bytes read are returned. |
| 266 | */ |
Michael Holzheu | 9cb2181 | 2013-09-11 14:24:51 -0700 | [diff] [blame] | 267 | static ssize_t __read_vmcore(char *buffer, size_t buflen, loff_t *fpos, |
| 268 | int userbuf) |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 269 | { |
| 270 | ssize_t acc = 0, tmp; |
Vivek Goyal | 80e8ff6 | 2006-04-10 22:54:10 -0700 | [diff] [blame] | 271 | size_t tsz; |
HATAYAMA Daisuke | b27eb18 | 2013-07-03 15:02:13 -0700 | [diff] [blame] | 272 | u64 start; |
| 273 | struct vmcore *m = NULL; |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 274 | |
| 275 | if (buflen == 0 || *fpos >= vmcore_size) |
| 276 | return 0; |
| 277 | |
| 278 | /* trim buflen to not go beyond EOF */ |
| 279 | if (buflen > vmcore_size - *fpos) |
| 280 | buflen = vmcore_size - *fpos; |
| 281 | |
| 282 | /* Read ELF core header */ |
| 283 | if (*fpos < elfcorebuf_sz) { |
HATAYAMA Daisuke | 087350c | 2013-07-03 15:02:19 -0700 | [diff] [blame] | 284 | tsz = min(elfcorebuf_sz - (size_t)*fpos, buflen); |
Michael Holzheu | 9cb2181 | 2013-09-11 14:24:51 -0700 | [diff] [blame] | 285 | if (copy_to(buffer, elfcorebuf + *fpos, tsz, userbuf)) |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 286 | return -EFAULT; |
| 287 | buflen -= tsz; |
| 288 | *fpos += tsz; |
| 289 | buffer += tsz; |
| 290 | acc += tsz; |
| 291 | |
| 292 | /* leave now if filled buffer already */ |
| 293 | if (buflen == 0) |
| 294 | return acc; |
| 295 | } |
| 296 | |
HATAYAMA Daisuke | 087350c | 2013-07-03 15:02:19 -0700 | [diff] [blame] | 297 | /* Read Elf note segment */ |
| 298 | if (*fpos < elfcorebuf_sz + elfnotes_sz) { |
| 299 | void *kaddr; |
| 300 | |
Rahul Lakkireddy | 7efe48d | 2018-05-02 15:17:18 +0530 | [diff] [blame] | 301 | /* We add device dumps before other elf notes because the |
| 302 | * other elf notes may not fill the elf notes buffer |
| 303 | * completely and we will end up with zero-filled data |
| 304 | * between the elf notes and the device dumps. Tools will |
| 305 | * then try to decode this zero-filled data as valid notes |
| 306 | * and we don't want that. Hence, adding device dumps before |
| 307 | * the other elf notes ensure that zero-filled data can be |
| 308 | * avoided. |
| 309 | */ |
| 310 | #ifdef CONFIG_PROC_VMCORE_DEVICE_DUMP |
| 311 | /* Read device dumps */ |
| 312 | if (*fpos < elfcorebuf_sz + vmcoredd_orig_sz) { |
| 313 | tsz = min(elfcorebuf_sz + vmcoredd_orig_sz - |
| 314 | (size_t)*fpos, buflen); |
| 315 | start = *fpos - elfcorebuf_sz; |
| 316 | if (vmcoredd_copy_dumps(buffer, start, tsz, userbuf)) |
| 317 | return -EFAULT; |
| 318 | |
| 319 | buflen -= tsz; |
| 320 | *fpos += tsz; |
| 321 | buffer += tsz; |
| 322 | acc += tsz; |
| 323 | |
| 324 | /* leave now if filled buffer already */ |
| 325 | if (!buflen) |
| 326 | return acc; |
| 327 | } |
| 328 | #endif /* CONFIG_PROC_VMCORE_DEVICE_DUMP */ |
| 329 | |
| 330 | /* Read remaining elf notes */ |
HATAYAMA Daisuke | 087350c | 2013-07-03 15:02:19 -0700 | [diff] [blame] | 331 | tsz = min(elfcorebuf_sz + elfnotes_sz - (size_t)*fpos, buflen); |
Rahul Lakkireddy | 7efe48d | 2018-05-02 15:17:18 +0530 | [diff] [blame] | 332 | kaddr = elfnotes_buf + *fpos - elfcorebuf_sz - vmcoredd_orig_sz; |
Michael Holzheu | 9cb2181 | 2013-09-11 14:24:51 -0700 | [diff] [blame] | 333 | if (copy_to(buffer, kaddr, tsz, userbuf)) |
HATAYAMA Daisuke | 087350c | 2013-07-03 15:02:19 -0700 | [diff] [blame] | 334 | return -EFAULT; |
Rahul Lakkireddy | 7efe48d | 2018-05-02 15:17:18 +0530 | [diff] [blame] | 335 | |
HATAYAMA Daisuke | 087350c | 2013-07-03 15:02:19 -0700 | [diff] [blame] | 336 | buflen -= tsz; |
| 337 | *fpos += tsz; |
| 338 | buffer += tsz; |
| 339 | acc += tsz; |
| 340 | |
| 341 | /* leave now if filled buffer already */ |
| 342 | if (buflen == 0) |
| 343 | return acc; |
| 344 | } |
| 345 | |
HATAYAMA Daisuke | b27eb18 | 2013-07-03 15:02:13 -0700 | [diff] [blame] | 346 | list_for_each_entry(m, &vmcore_list, list) { |
| 347 | if (*fpos < m->offset + m->size) { |
Dave Young | 0b50a2d | 2016-03-17 14:21:03 -0700 | [diff] [blame] | 348 | tsz = (size_t)min_t(unsigned long long, |
| 349 | m->offset + m->size - *fpos, |
| 350 | buflen); |
HATAYAMA Daisuke | b27eb18 | 2013-07-03 15:02:13 -0700 | [diff] [blame] | 351 | start = m->paddr + *fpos - m->offset; |
Michael Holzheu | 9cb2181 | 2013-09-11 14:24:51 -0700 | [diff] [blame] | 352 | tmp = read_from_oldmem(buffer, tsz, &start, userbuf); |
HATAYAMA Daisuke | b27eb18 | 2013-07-03 15:02:13 -0700 | [diff] [blame] | 353 | if (tmp < 0) |
| 354 | return tmp; |
| 355 | buflen -= tsz; |
| 356 | *fpos += tsz; |
| 357 | buffer += tsz; |
| 358 | acc += tsz; |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 359 | |
HATAYAMA Daisuke | b27eb18 | 2013-07-03 15:02:13 -0700 | [diff] [blame] | 360 | /* leave now if filled buffer already */ |
| 361 | if (buflen == 0) |
| 362 | return acc; |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 363 | } |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 364 | } |
HATAYAMA Daisuke | b27eb18 | 2013-07-03 15:02:13 -0700 | [diff] [blame] | 365 | |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 366 | return acc; |
| 367 | } |
| 368 | |
Michael Holzheu | 9cb2181 | 2013-09-11 14:24:51 -0700 | [diff] [blame] | 369 | static ssize_t read_vmcore(struct file *file, char __user *buffer, |
| 370 | size_t buflen, loff_t *fpos) |
| 371 | { |
| 372 | return __read_vmcore((__force char *) buffer, buflen, fpos, 1); |
| 373 | } |
| 374 | |
| 375 | /* |
| 376 | * The vmcore fault handler uses the page cache and fills data using the |
| 377 | * standard __vmcore_read() function. |
| 378 | * |
| 379 | * On s390 the fault handler is used for memory regions that can't be mapped |
| 380 | * directly with remap_pfn_range(). |
| 381 | */ |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 382 | static int mmap_vmcore_fault(struct vm_fault *vmf) |
Michael Holzheu | 9cb2181 | 2013-09-11 14:24:51 -0700 | [diff] [blame] | 383 | { |
| 384 | #ifdef CONFIG_S390 |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 385 | struct address_space *mapping = vmf->vma->vm_file->f_mapping; |
Michael Holzheu | 9cb2181 | 2013-09-11 14:24:51 -0700 | [diff] [blame] | 386 | pgoff_t index = vmf->pgoff; |
| 387 | struct page *page; |
| 388 | loff_t offset; |
| 389 | char *buf; |
| 390 | int rc; |
| 391 | |
| 392 | page = find_or_create_page(mapping, index, GFP_KERNEL); |
| 393 | if (!page) |
| 394 | return VM_FAULT_OOM; |
| 395 | if (!PageUptodate(page)) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 396 | offset = (loff_t) index << PAGE_SHIFT; |
Michael Holzheu | 9cb2181 | 2013-09-11 14:24:51 -0700 | [diff] [blame] | 397 | buf = __va((page_to_pfn(page) << PAGE_SHIFT)); |
| 398 | rc = __read_vmcore(buf, PAGE_SIZE, &offset, 0); |
| 399 | if (rc < 0) { |
| 400 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 401 | put_page(page); |
Michael Holzheu | 9cb2181 | 2013-09-11 14:24:51 -0700 | [diff] [blame] | 402 | return (rc == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS; |
| 403 | } |
| 404 | SetPageUptodate(page); |
| 405 | } |
| 406 | unlock_page(page); |
| 407 | vmf->page = page; |
| 408 | return 0; |
| 409 | #else |
| 410 | return VM_FAULT_SIGBUS; |
| 411 | #endif |
| 412 | } |
| 413 | |
| 414 | static const struct vm_operations_struct vmcore_mmap_ops = { |
| 415 | .fault = mmap_vmcore_fault, |
| 416 | }; |
| 417 | |
HATAYAMA Daisuke | 8308697 | 2013-07-03 15:02:23 -0700 | [diff] [blame] | 418 | /** |
Rahul Lakkireddy | 2724273 | 2018-05-02 15:17:17 +0530 | [diff] [blame] | 419 | * vmcore_alloc_buf - allocate buffer in vmalloc memory |
| 420 | * @sizez: size of buffer |
HATAYAMA Daisuke | 8308697 | 2013-07-03 15:02:23 -0700 | [diff] [blame] | 421 | * |
| 422 | * If CONFIG_MMU is defined, use vmalloc_user() to allow users to mmap |
| 423 | * the buffer to user-space by means of remap_vmalloc_range(). |
| 424 | * |
| 425 | * If CONFIG_MMU is not defined, use vzalloc() since mmap_vmcore() is |
| 426 | * disabled and there's no need to allow users to mmap the buffer. |
| 427 | */ |
Rahul Lakkireddy | 2724273 | 2018-05-02 15:17:17 +0530 | [diff] [blame] | 428 | static inline char *vmcore_alloc_buf(size_t size) |
HATAYAMA Daisuke | 8308697 | 2013-07-03 15:02:23 -0700 | [diff] [blame] | 429 | { |
| 430 | #ifdef CONFIG_MMU |
Rahul Lakkireddy | 2724273 | 2018-05-02 15:17:17 +0530 | [diff] [blame] | 431 | return vmalloc_user(size); |
HATAYAMA Daisuke | 8308697 | 2013-07-03 15:02:23 -0700 | [diff] [blame] | 432 | #else |
Rahul Lakkireddy | 2724273 | 2018-05-02 15:17:17 +0530 | [diff] [blame] | 433 | return vzalloc(size); |
HATAYAMA Daisuke | 8308697 | 2013-07-03 15:02:23 -0700 | [diff] [blame] | 434 | #endif |
| 435 | } |
| 436 | |
| 437 | /* |
| 438 | * Disable mmap_vmcore() if CONFIG_MMU is not defined. MMU is |
| 439 | * essential for mmap_vmcore() in order to map physically |
| 440 | * non-contiguous objects (ELF header, ELF note segment and memory |
| 441 | * regions in the 1st kernel pointed to by PT_LOAD entries) into |
| 442 | * virtually contiguous user-space in ELF layout. |
| 443 | */ |
Michael Holzheu | 11e376a | 2013-09-11 14:24:53 -0700 | [diff] [blame] | 444 | #ifdef CONFIG_MMU |
Vitaly Kuznetsov | 0692ded | 2014-08-08 14:22:05 -0700 | [diff] [blame] | 445 | /* |
| 446 | * remap_oldmem_pfn_checked - do remap_oldmem_pfn_range replacing all pages |
| 447 | * reported as not being ram with the zero page. |
| 448 | * |
| 449 | * @vma: vm_area_struct describing requested mapping |
| 450 | * @from: start remapping from |
| 451 | * @pfn: page frame number to start remapping to |
| 452 | * @size: remapping size |
| 453 | * @prot: protection bits |
| 454 | * |
| 455 | * Returns zero on success, -EAGAIN on failure. |
| 456 | */ |
| 457 | static int remap_oldmem_pfn_checked(struct vm_area_struct *vma, |
| 458 | unsigned long from, unsigned long pfn, |
| 459 | unsigned long size, pgprot_t prot) |
| 460 | { |
| 461 | unsigned long map_size; |
| 462 | unsigned long pos_start, pos_end, pos; |
| 463 | unsigned long zeropage_pfn = my_zero_pfn(0); |
| 464 | size_t len = 0; |
| 465 | |
| 466 | pos_start = pfn; |
| 467 | pos_end = pfn + (size >> PAGE_SHIFT); |
| 468 | |
| 469 | for (pos = pos_start; pos < pos_end; ++pos) { |
| 470 | if (!pfn_is_ram(pos)) { |
| 471 | /* |
| 472 | * We hit a page which is not ram. Remap the continuous |
| 473 | * region between pos_start and pos-1 and replace |
| 474 | * the non-ram page at pos with the zero page. |
| 475 | */ |
| 476 | if (pos > pos_start) { |
| 477 | /* Remap continuous region */ |
| 478 | map_size = (pos - pos_start) << PAGE_SHIFT; |
| 479 | if (remap_oldmem_pfn_range(vma, from + len, |
| 480 | pos_start, map_size, |
| 481 | prot)) |
| 482 | goto fail; |
| 483 | len += map_size; |
| 484 | } |
| 485 | /* Remap the zero page */ |
| 486 | if (remap_oldmem_pfn_range(vma, from + len, |
| 487 | zeropage_pfn, |
| 488 | PAGE_SIZE, prot)) |
| 489 | goto fail; |
| 490 | len += PAGE_SIZE; |
| 491 | pos_start = pos + 1; |
| 492 | } |
| 493 | } |
| 494 | if (pos > pos_start) { |
| 495 | /* Remap the rest */ |
| 496 | map_size = (pos - pos_start) << PAGE_SHIFT; |
| 497 | if (remap_oldmem_pfn_range(vma, from + len, pos_start, |
| 498 | map_size, prot)) |
| 499 | goto fail; |
| 500 | } |
| 501 | return 0; |
| 502 | fail: |
Mike Rapoport | 897ab3e | 2017-02-24 14:58:22 -0800 | [diff] [blame] | 503 | do_munmap(vma->vm_mm, from, len, NULL); |
Vitaly Kuznetsov | 0692ded | 2014-08-08 14:22:05 -0700 | [diff] [blame] | 504 | return -EAGAIN; |
| 505 | } |
| 506 | |
| 507 | static int vmcore_remap_oldmem_pfn(struct vm_area_struct *vma, |
| 508 | unsigned long from, unsigned long pfn, |
| 509 | unsigned long size, pgprot_t prot) |
| 510 | { |
| 511 | /* |
| 512 | * Check if oldmem_pfn_is_ram was registered to avoid |
| 513 | * looping over all pages without a reason. |
| 514 | */ |
| 515 | if (oldmem_pfn_is_ram) |
| 516 | return remap_oldmem_pfn_checked(vma, from, pfn, size, prot); |
| 517 | else |
| 518 | return remap_oldmem_pfn_range(vma, from, pfn, size, prot); |
| 519 | } |
| 520 | |
HATAYAMA Daisuke | 8308697 | 2013-07-03 15:02:23 -0700 | [diff] [blame] | 521 | static int mmap_vmcore(struct file *file, struct vm_area_struct *vma) |
| 522 | { |
| 523 | size_t size = vma->vm_end - vma->vm_start; |
| 524 | u64 start, end, len, tsz; |
| 525 | struct vmcore *m; |
| 526 | |
| 527 | start = (u64)vma->vm_pgoff << PAGE_SHIFT; |
| 528 | end = start + size; |
| 529 | |
| 530 | if (size > vmcore_size || end > vmcore_size) |
| 531 | return -EINVAL; |
| 532 | |
| 533 | if (vma->vm_flags & (VM_WRITE | VM_EXEC)) |
| 534 | return -EPERM; |
| 535 | |
| 536 | vma->vm_flags &= ~(VM_MAYWRITE | VM_MAYEXEC); |
| 537 | vma->vm_flags |= VM_MIXEDMAP; |
Michael Holzheu | 9cb2181 | 2013-09-11 14:24:51 -0700 | [diff] [blame] | 538 | vma->vm_ops = &vmcore_mmap_ops; |
HATAYAMA Daisuke | 8308697 | 2013-07-03 15:02:23 -0700 | [diff] [blame] | 539 | |
| 540 | len = 0; |
| 541 | |
| 542 | if (start < elfcorebuf_sz) { |
| 543 | u64 pfn; |
| 544 | |
| 545 | tsz = min(elfcorebuf_sz - (size_t)start, size); |
| 546 | pfn = __pa(elfcorebuf + start) >> PAGE_SHIFT; |
| 547 | if (remap_pfn_range(vma, vma->vm_start, pfn, tsz, |
| 548 | vma->vm_page_prot)) |
| 549 | return -EAGAIN; |
| 550 | size -= tsz; |
| 551 | start += tsz; |
| 552 | len += tsz; |
| 553 | |
| 554 | if (size == 0) |
| 555 | return 0; |
| 556 | } |
| 557 | |
| 558 | if (start < elfcorebuf_sz + elfnotes_sz) { |
| 559 | void *kaddr; |
| 560 | |
Rahul Lakkireddy | 7efe48d | 2018-05-02 15:17:18 +0530 | [diff] [blame] | 561 | /* We add device dumps before other elf notes because the |
| 562 | * other elf notes may not fill the elf notes buffer |
| 563 | * completely and we will end up with zero-filled data |
| 564 | * between the elf notes and the device dumps. Tools will |
| 565 | * then try to decode this zero-filled data as valid notes |
| 566 | * and we don't want that. Hence, adding device dumps before |
| 567 | * the other elf notes ensure that zero-filled data can be |
| 568 | * avoided. This also ensures that the device dumps and |
| 569 | * other elf notes can be properly mmaped at page aligned |
| 570 | * address. |
| 571 | */ |
| 572 | #ifdef CONFIG_PROC_VMCORE_DEVICE_DUMP |
| 573 | /* Read device dumps */ |
| 574 | if (start < elfcorebuf_sz + vmcoredd_orig_sz) { |
| 575 | u64 start_off; |
| 576 | |
| 577 | tsz = min(elfcorebuf_sz + vmcoredd_orig_sz - |
| 578 | (size_t)start, size); |
| 579 | start_off = start - elfcorebuf_sz; |
| 580 | if (vmcoredd_mmap_dumps(vma, vma->vm_start + len, |
| 581 | start_off, tsz)) |
| 582 | goto fail; |
| 583 | |
| 584 | size -= tsz; |
| 585 | start += tsz; |
| 586 | len += tsz; |
| 587 | |
| 588 | /* leave now if filled buffer already */ |
| 589 | if (!size) |
| 590 | return 0; |
| 591 | } |
| 592 | #endif /* CONFIG_PROC_VMCORE_DEVICE_DUMP */ |
| 593 | |
| 594 | /* Read remaining elf notes */ |
HATAYAMA Daisuke | 8308697 | 2013-07-03 15:02:23 -0700 | [diff] [blame] | 595 | tsz = min(elfcorebuf_sz + elfnotes_sz - (size_t)start, size); |
Rahul Lakkireddy | 7efe48d | 2018-05-02 15:17:18 +0530 | [diff] [blame] | 596 | kaddr = elfnotes_buf + start - elfcorebuf_sz - vmcoredd_orig_sz; |
HATAYAMA Daisuke | 8308697 | 2013-07-03 15:02:23 -0700 | [diff] [blame] | 597 | if (remap_vmalloc_range_partial(vma, vma->vm_start + len, |
| 598 | kaddr, tsz)) |
| 599 | goto fail; |
Rahul Lakkireddy | 7efe48d | 2018-05-02 15:17:18 +0530 | [diff] [blame] | 600 | |
HATAYAMA Daisuke | 8308697 | 2013-07-03 15:02:23 -0700 | [diff] [blame] | 601 | size -= tsz; |
| 602 | start += tsz; |
| 603 | len += tsz; |
| 604 | |
| 605 | if (size == 0) |
| 606 | return 0; |
| 607 | } |
| 608 | |
| 609 | list_for_each_entry(m, &vmcore_list, list) { |
| 610 | if (start < m->offset + m->size) { |
| 611 | u64 paddr = 0; |
| 612 | |
Dave Young | 0b50a2d | 2016-03-17 14:21:03 -0700 | [diff] [blame] | 613 | tsz = (size_t)min_t(unsigned long long, |
| 614 | m->offset + m->size - start, size); |
HATAYAMA Daisuke | 8308697 | 2013-07-03 15:02:23 -0700 | [diff] [blame] | 615 | paddr = m->paddr + start - m->offset; |
Vitaly Kuznetsov | 0692ded | 2014-08-08 14:22:05 -0700 | [diff] [blame] | 616 | if (vmcore_remap_oldmem_pfn(vma, vma->vm_start + len, |
| 617 | paddr >> PAGE_SHIFT, tsz, |
| 618 | vma->vm_page_prot)) |
HATAYAMA Daisuke | 8308697 | 2013-07-03 15:02:23 -0700 | [diff] [blame] | 619 | goto fail; |
| 620 | size -= tsz; |
| 621 | start += tsz; |
| 622 | len += tsz; |
| 623 | |
| 624 | if (size == 0) |
| 625 | return 0; |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | return 0; |
| 630 | fail: |
Mike Rapoport | 897ab3e | 2017-02-24 14:58:22 -0800 | [diff] [blame] | 631 | do_munmap(vma->vm_mm, vma->vm_start, len, NULL); |
HATAYAMA Daisuke | 8308697 | 2013-07-03 15:02:23 -0700 | [diff] [blame] | 632 | return -EAGAIN; |
| 633 | } |
| 634 | #else |
| 635 | static int mmap_vmcore(struct file *file, struct vm_area_struct *vma) |
| 636 | { |
| 637 | return -ENOSYS; |
| 638 | } |
| 639 | #endif |
| 640 | |
Alexey Dobriyan | 5aa140c | 2008-10-06 14:36:31 +0400 | [diff] [blame] | 641 | static const struct file_operations proc_vmcore_operations = { |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 642 | .read = read_vmcore, |
Arnd Bergmann | c227e69 | 2010-09-22 13:04:54 -0700 | [diff] [blame] | 643 | .llseek = default_llseek, |
HATAYAMA Daisuke | 8308697 | 2013-07-03 15:02:23 -0700 | [diff] [blame] | 644 | .mmap = mmap_vmcore, |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 645 | }; |
| 646 | |
| 647 | static struct vmcore* __init get_new_element(void) |
| 648 | { |
Cyrill Gorcunov | 2f6d311 | 2009-06-17 16:26:00 -0700 | [diff] [blame] | 649 | return kzalloc(sizeof(struct vmcore), GFP_KERNEL); |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 650 | } |
| 651 | |
Rahul Lakkireddy | 44c752f | 2018-05-21 19:07:50 +0530 | [diff] [blame^] | 652 | static u64 get_vmcore_size(size_t elfsz, size_t elfnotesegsz, |
| 653 | struct list_head *vc_list) |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 654 | { |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 655 | u64 size; |
HATAYAMA Daisuke | 591ff71 | 2013-07-03 15:02:22 -0700 | [diff] [blame] | 656 | struct vmcore *m; |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 657 | |
HATAYAMA Daisuke | 591ff71 | 2013-07-03 15:02:22 -0700 | [diff] [blame] | 658 | size = elfsz + elfnotesegsz; |
| 659 | list_for_each_entry(m, vc_list, list) { |
| 660 | size += m->size; |
Vivek Goyal | 72658e9 | 2005-06-25 14:58:22 -0700 | [diff] [blame] | 661 | } |
| 662 | return size; |
| 663 | } |
| 664 | |
HATAYAMA Daisuke | 087350c | 2013-07-03 15:02:19 -0700 | [diff] [blame] | 665 | /** |
| 666 | * update_note_header_size_elf64 - update p_memsz member of each PT_NOTE entry |
| 667 | * |
| 668 | * @ehdr_ptr: ELF header |
| 669 | * |
| 670 | * This function updates p_memsz member of each PT_NOTE entry in the |
| 671 | * program header table pointed to by @ehdr_ptr to real size of ELF |
| 672 | * note segment. |
| 673 | */ |
| 674 | static int __init update_note_header_size_elf64(const Elf64_Ehdr *ehdr_ptr) |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 675 | { |
HATAYAMA Daisuke | 087350c | 2013-07-03 15:02:19 -0700 | [diff] [blame] | 676 | int i, rc=0; |
| 677 | Elf64_Phdr *phdr_ptr; |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 678 | Elf64_Nhdr *nhdr_ptr; |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 679 | |
HATAYAMA Daisuke | 087350c | 2013-07-03 15:02:19 -0700 | [diff] [blame] | 680 | phdr_ptr = (Elf64_Phdr *)(ehdr_ptr + 1); |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 681 | for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) { |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 682 | void *notes_section; |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 683 | u64 offset, max_sz, sz, real_sz = 0; |
| 684 | if (phdr_ptr->p_type != PT_NOTE) |
| 685 | continue; |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 686 | max_sz = phdr_ptr->p_memsz; |
| 687 | offset = phdr_ptr->p_offset; |
| 688 | notes_section = kmalloc(max_sz, GFP_KERNEL); |
| 689 | if (!notes_section) |
| 690 | return -ENOMEM; |
Michael Holzheu | be8a8d0 | 2013-09-11 14:24:49 -0700 | [diff] [blame] | 691 | rc = elfcorehdr_read_notes(notes_section, max_sz, &offset); |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 692 | if (rc < 0) { |
| 693 | kfree(notes_section); |
| 694 | return rc; |
| 695 | } |
| 696 | nhdr_ptr = notes_section; |
Greg Pearson | 38dfac8 | 2014-02-10 14:25:36 -0800 | [diff] [blame] | 697 | while (nhdr_ptr->n_namesz != 0) { |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 698 | sz = sizeof(Elf64_Nhdr) + |
WANG Chao | 34b4776 | 2015-02-17 13:46:01 -0800 | [diff] [blame] | 699 | (((u64)nhdr_ptr->n_namesz + 3) & ~3) + |
| 700 | (((u64)nhdr_ptr->n_descsz + 3) & ~3); |
Greg Pearson | 38dfac8 | 2014-02-10 14:25:36 -0800 | [diff] [blame] | 701 | if ((real_sz + sz) > max_sz) { |
| 702 | pr_warn("Warning: Exceeded p_memsz, dropping PT_NOTE entry n_namesz=0x%x, n_descsz=0x%x\n", |
| 703 | nhdr_ptr->n_namesz, nhdr_ptr->n_descsz); |
| 704 | break; |
| 705 | } |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 706 | real_sz += sz; |
| 707 | nhdr_ptr = (Elf64_Nhdr*)((char*)nhdr_ptr + sz); |
| 708 | } |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 709 | kfree(notes_section); |
HATAYAMA Daisuke | 087350c | 2013-07-03 15:02:19 -0700 | [diff] [blame] | 710 | phdr_ptr->p_memsz = real_sz; |
Greg Pearson | 38dfac8 | 2014-02-10 14:25:36 -0800 | [diff] [blame] | 711 | if (real_sz == 0) { |
| 712 | pr_warn("Warning: Zero PT_NOTE entries found\n"); |
Greg Pearson | 38dfac8 | 2014-02-10 14:25:36 -0800 | [diff] [blame] | 713 | } |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 714 | } |
| 715 | |
HATAYAMA Daisuke | 087350c | 2013-07-03 15:02:19 -0700 | [diff] [blame] | 716 | return 0; |
| 717 | } |
| 718 | |
| 719 | /** |
| 720 | * get_note_number_and_size_elf64 - get the number of PT_NOTE program |
| 721 | * headers and sum of real size of their ELF note segment headers and |
| 722 | * data. |
| 723 | * |
| 724 | * @ehdr_ptr: ELF header |
| 725 | * @nr_ptnote: buffer for the number of PT_NOTE program headers |
| 726 | * @sz_ptnote: buffer for size of unique PT_NOTE program header |
| 727 | * |
| 728 | * This function is used to merge multiple PT_NOTE program headers |
| 729 | * into a unique single one. The resulting unique entry will have |
| 730 | * @sz_ptnote in its phdr->p_mem. |
| 731 | * |
| 732 | * It is assumed that program headers with PT_NOTE type pointed to by |
| 733 | * @ehdr_ptr has already been updated by update_note_header_size_elf64 |
| 734 | * and each of PT_NOTE program headers has actual ELF note segment |
| 735 | * size in its p_memsz member. |
| 736 | */ |
| 737 | static int __init get_note_number_and_size_elf64(const Elf64_Ehdr *ehdr_ptr, |
| 738 | int *nr_ptnote, u64 *sz_ptnote) |
| 739 | { |
| 740 | int i; |
| 741 | Elf64_Phdr *phdr_ptr; |
| 742 | |
| 743 | *nr_ptnote = *sz_ptnote = 0; |
| 744 | |
| 745 | phdr_ptr = (Elf64_Phdr *)(ehdr_ptr + 1); |
| 746 | for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) { |
| 747 | if (phdr_ptr->p_type != PT_NOTE) |
| 748 | continue; |
| 749 | *nr_ptnote += 1; |
| 750 | *sz_ptnote += phdr_ptr->p_memsz; |
| 751 | } |
| 752 | |
| 753 | return 0; |
| 754 | } |
| 755 | |
| 756 | /** |
| 757 | * copy_notes_elf64 - copy ELF note segments in a given buffer |
| 758 | * |
| 759 | * @ehdr_ptr: ELF header |
| 760 | * @notes_buf: buffer into which ELF note segments are copied |
| 761 | * |
| 762 | * This function is used to copy ELF note segment in the 1st kernel |
| 763 | * into the buffer @notes_buf in the 2nd kernel. It is assumed that |
| 764 | * size of the buffer @notes_buf is equal to or larger than sum of the |
| 765 | * real ELF note segment headers and data. |
| 766 | * |
| 767 | * It is assumed that program headers with PT_NOTE type pointed to by |
| 768 | * @ehdr_ptr has already been updated by update_note_header_size_elf64 |
| 769 | * and each of PT_NOTE program headers has actual ELF note segment |
| 770 | * size in its p_memsz member. |
| 771 | */ |
| 772 | static int __init copy_notes_elf64(const Elf64_Ehdr *ehdr_ptr, char *notes_buf) |
| 773 | { |
| 774 | int i, rc=0; |
| 775 | Elf64_Phdr *phdr_ptr; |
| 776 | |
| 777 | phdr_ptr = (Elf64_Phdr*)(ehdr_ptr + 1); |
| 778 | |
| 779 | for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) { |
| 780 | u64 offset; |
| 781 | if (phdr_ptr->p_type != PT_NOTE) |
| 782 | continue; |
| 783 | offset = phdr_ptr->p_offset; |
Michael Holzheu | be8a8d0 | 2013-09-11 14:24:49 -0700 | [diff] [blame] | 784 | rc = elfcorehdr_read_notes(notes_buf, phdr_ptr->p_memsz, |
| 785 | &offset); |
HATAYAMA Daisuke | 087350c | 2013-07-03 15:02:19 -0700 | [diff] [blame] | 786 | if (rc < 0) |
| 787 | return rc; |
| 788 | notes_buf += phdr_ptr->p_memsz; |
| 789 | } |
| 790 | |
| 791 | return 0; |
| 792 | } |
| 793 | |
| 794 | /* Merges all the PT_NOTE headers into one. */ |
| 795 | static int __init merge_note_headers_elf64(char *elfptr, size_t *elfsz, |
| 796 | char **notes_buf, size_t *notes_sz) |
| 797 | { |
| 798 | int i, nr_ptnote=0, rc=0; |
| 799 | char *tmp; |
| 800 | Elf64_Ehdr *ehdr_ptr; |
| 801 | Elf64_Phdr phdr; |
| 802 | u64 phdr_sz = 0, note_off; |
| 803 | |
| 804 | ehdr_ptr = (Elf64_Ehdr *)elfptr; |
| 805 | |
| 806 | rc = update_note_header_size_elf64(ehdr_ptr); |
| 807 | if (rc < 0) |
| 808 | return rc; |
| 809 | |
| 810 | rc = get_note_number_and_size_elf64(ehdr_ptr, &nr_ptnote, &phdr_sz); |
| 811 | if (rc < 0) |
| 812 | return rc; |
| 813 | |
| 814 | *notes_sz = roundup(phdr_sz, PAGE_SIZE); |
Rahul Lakkireddy | 2724273 | 2018-05-02 15:17:17 +0530 | [diff] [blame] | 815 | *notes_buf = vmcore_alloc_buf(*notes_sz); |
HATAYAMA Daisuke | 087350c | 2013-07-03 15:02:19 -0700 | [diff] [blame] | 816 | if (!*notes_buf) |
| 817 | return -ENOMEM; |
| 818 | |
| 819 | rc = copy_notes_elf64(ehdr_ptr, *notes_buf); |
| 820 | if (rc < 0) |
| 821 | return rc; |
| 822 | |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 823 | /* Prepare merged PT_NOTE program header. */ |
| 824 | phdr.p_type = PT_NOTE; |
| 825 | phdr.p_flags = 0; |
| 826 | note_off = sizeof(Elf64_Ehdr) + |
| 827 | (ehdr_ptr->e_phnum - nr_ptnote +1) * sizeof(Elf64_Phdr); |
HATAYAMA Daisuke | 087350c | 2013-07-03 15:02:19 -0700 | [diff] [blame] | 828 | phdr.p_offset = roundup(note_off, PAGE_SIZE); |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 829 | phdr.p_vaddr = phdr.p_paddr = 0; |
| 830 | phdr.p_filesz = phdr.p_memsz = phdr_sz; |
| 831 | phdr.p_align = 0; |
| 832 | |
| 833 | /* Add merged PT_NOTE program header*/ |
| 834 | tmp = elfptr + sizeof(Elf64_Ehdr); |
| 835 | memcpy(tmp, &phdr, sizeof(phdr)); |
| 836 | tmp += sizeof(phdr); |
| 837 | |
| 838 | /* Remove unwanted PT_NOTE program headers. */ |
| 839 | i = (nr_ptnote - 1) * sizeof(Elf64_Phdr); |
| 840 | *elfsz = *elfsz - i; |
| 841 | memmove(tmp, tmp+i, ((*elfsz)-sizeof(Elf64_Ehdr)-sizeof(Elf64_Phdr))); |
HATAYAMA Daisuke | f2bdacd | 2013-07-03 15:02:14 -0700 | [diff] [blame] | 842 | memset(elfptr + *elfsz, 0, i); |
| 843 | *elfsz = roundup(*elfsz, PAGE_SIZE); |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 844 | |
| 845 | /* Modify e_phnum to reflect merged headers. */ |
| 846 | ehdr_ptr->e_phnum = ehdr_ptr->e_phnum - nr_ptnote + 1; |
| 847 | |
Rahul Lakkireddy | 7efe48d | 2018-05-02 15:17:18 +0530 | [diff] [blame] | 848 | /* Store the size of all notes. We need this to update the note |
| 849 | * header when the device dumps will be added. |
| 850 | */ |
| 851 | elfnotes_orig_sz = phdr.p_memsz; |
| 852 | |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 853 | return 0; |
| 854 | } |
| 855 | |
HATAYAMA Daisuke | 087350c | 2013-07-03 15:02:19 -0700 | [diff] [blame] | 856 | /** |
| 857 | * update_note_header_size_elf32 - update p_memsz member of each PT_NOTE entry |
| 858 | * |
| 859 | * @ehdr_ptr: ELF header |
| 860 | * |
| 861 | * This function updates p_memsz member of each PT_NOTE entry in the |
| 862 | * program header table pointed to by @ehdr_ptr to real size of ELF |
| 863 | * note segment. |
| 864 | */ |
| 865 | static int __init update_note_header_size_elf32(const Elf32_Ehdr *ehdr_ptr) |
Vivek Goyal | 72658e9 | 2005-06-25 14:58:22 -0700 | [diff] [blame] | 866 | { |
HATAYAMA Daisuke | 087350c | 2013-07-03 15:02:19 -0700 | [diff] [blame] | 867 | int i, rc=0; |
| 868 | Elf32_Phdr *phdr_ptr; |
Vivek Goyal | 72658e9 | 2005-06-25 14:58:22 -0700 | [diff] [blame] | 869 | Elf32_Nhdr *nhdr_ptr; |
Vivek Goyal | 72658e9 | 2005-06-25 14:58:22 -0700 | [diff] [blame] | 870 | |
HATAYAMA Daisuke | 087350c | 2013-07-03 15:02:19 -0700 | [diff] [blame] | 871 | phdr_ptr = (Elf32_Phdr *)(ehdr_ptr + 1); |
Vivek Goyal | 72658e9 | 2005-06-25 14:58:22 -0700 | [diff] [blame] | 872 | for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) { |
Vivek Goyal | 72658e9 | 2005-06-25 14:58:22 -0700 | [diff] [blame] | 873 | void *notes_section; |
Vivek Goyal | 72658e9 | 2005-06-25 14:58:22 -0700 | [diff] [blame] | 874 | u64 offset, max_sz, sz, real_sz = 0; |
| 875 | if (phdr_ptr->p_type != PT_NOTE) |
| 876 | continue; |
Vivek Goyal | 72658e9 | 2005-06-25 14:58:22 -0700 | [diff] [blame] | 877 | max_sz = phdr_ptr->p_memsz; |
| 878 | offset = phdr_ptr->p_offset; |
| 879 | notes_section = kmalloc(max_sz, GFP_KERNEL); |
| 880 | if (!notes_section) |
| 881 | return -ENOMEM; |
Michael Holzheu | be8a8d0 | 2013-09-11 14:24:49 -0700 | [diff] [blame] | 882 | rc = elfcorehdr_read_notes(notes_section, max_sz, &offset); |
Vivek Goyal | 72658e9 | 2005-06-25 14:58:22 -0700 | [diff] [blame] | 883 | if (rc < 0) { |
| 884 | kfree(notes_section); |
| 885 | return rc; |
| 886 | } |
| 887 | nhdr_ptr = notes_section; |
Greg Pearson | 38dfac8 | 2014-02-10 14:25:36 -0800 | [diff] [blame] | 888 | while (nhdr_ptr->n_namesz != 0) { |
Vivek Goyal | 72658e9 | 2005-06-25 14:58:22 -0700 | [diff] [blame] | 889 | sz = sizeof(Elf32_Nhdr) + |
WANG Chao | 34b4776 | 2015-02-17 13:46:01 -0800 | [diff] [blame] | 890 | (((u64)nhdr_ptr->n_namesz + 3) & ~3) + |
| 891 | (((u64)nhdr_ptr->n_descsz + 3) & ~3); |
Greg Pearson | 38dfac8 | 2014-02-10 14:25:36 -0800 | [diff] [blame] | 892 | if ((real_sz + sz) > max_sz) { |
| 893 | pr_warn("Warning: Exceeded p_memsz, dropping PT_NOTE entry n_namesz=0x%x, n_descsz=0x%x\n", |
| 894 | nhdr_ptr->n_namesz, nhdr_ptr->n_descsz); |
| 895 | break; |
| 896 | } |
Vivek Goyal | 72658e9 | 2005-06-25 14:58:22 -0700 | [diff] [blame] | 897 | real_sz += sz; |
| 898 | nhdr_ptr = (Elf32_Nhdr*)((char*)nhdr_ptr + sz); |
| 899 | } |
Vivek Goyal | 72658e9 | 2005-06-25 14:58:22 -0700 | [diff] [blame] | 900 | kfree(notes_section); |
HATAYAMA Daisuke | 087350c | 2013-07-03 15:02:19 -0700 | [diff] [blame] | 901 | phdr_ptr->p_memsz = real_sz; |
Greg Pearson | 38dfac8 | 2014-02-10 14:25:36 -0800 | [diff] [blame] | 902 | if (real_sz == 0) { |
| 903 | pr_warn("Warning: Zero PT_NOTE entries found\n"); |
Greg Pearson | 38dfac8 | 2014-02-10 14:25:36 -0800 | [diff] [blame] | 904 | } |
Vivek Goyal | 72658e9 | 2005-06-25 14:58:22 -0700 | [diff] [blame] | 905 | } |
| 906 | |
HATAYAMA Daisuke | 087350c | 2013-07-03 15:02:19 -0700 | [diff] [blame] | 907 | return 0; |
| 908 | } |
| 909 | |
| 910 | /** |
| 911 | * get_note_number_and_size_elf32 - get the number of PT_NOTE program |
| 912 | * headers and sum of real size of their ELF note segment headers and |
| 913 | * data. |
| 914 | * |
| 915 | * @ehdr_ptr: ELF header |
| 916 | * @nr_ptnote: buffer for the number of PT_NOTE program headers |
| 917 | * @sz_ptnote: buffer for size of unique PT_NOTE program header |
| 918 | * |
| 919 | * This function is used to merge multiple PT_NOTE program headers |
| 920 | * into a unique single one. The resulting unique entry will have |
| 921 | * @sz_ptnote in its phdr->p_mem. |
| 922 | * |
| 923 | * It is assumed that program headers with PT_NOTE type pointed to by |
| 924 | * @ehdr_ptr has already been updated by update_note_header_size_elf32 |
| 925 | * and each of PT_NOTE program headers has actual ELF note segment |
| 926 | * size in its p_memsz member. |
| 927 | */ |
| 928 | static int __init get_note_number_and_size_elf32(const Elf32_Ehdr *ehdr_ptr, |
| 929 | int *nr_ptnote, u64 *sz_ptnote) |
| 930 | { |
| 931 | int i; |
| 932 | Elf32_Phdr *phdr_ptr; |
| 933 | |
| 934 | *nr_ptnote = *sz_ptnote = 0; |
| 935 | |
| 936 | phdr_ptr = (Elf32_Phdr *)(ehdr_ptr + 1); |
| 937 | for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) { |
| 938 | if (phdr_ptr->p_type != PT_NOTE) |
| 939 | continue; |
| 940 | *nr_ptnote += 1; |
| 941 | *sz_ptnote += phdr_ptr->p_memsz; |
| 942 | } |
| 943 | |
| 944 | return 0; |
| 945 | } |
| 946 | |
| 947 | /** |
| 948 | * copy_notes_elf32 - copy ELF note segments in a given buffer |
| 949 | * |
| 950 | * @ehdr_ptr: ELF header |
| 951 | * @notes_buf: buffer into which ELF note segments are copied |
| 952 | * |
| 953 | * This function is used to copy ELF note segment in the 1st kernel |
| 954 | * into the buffer @notes_buf in the 2nd kernel. It is assumed that |
| 955 | * size of the buffer @notes_buf is equal to or larger than sum of the |
| 956 | * real ELF note segment headers and data. |
| 957 | * |
| 958 | * It is assumed that program headers with PT_NOTE type pointed to by |
| 959 | * @ehdr_ptr has already been updated by update_note_header_size_elf32 |
| 960 | * and each of PT_NOTE program headers has actual ELF note segment |
| 961 | * size in its p_memsz member. |
| 962 | */ |
| 963 | static int __init copy_notes_elf32(const Elf32_Ehdr *ehdr_ptr, char *notes_buf) |
| 964 | { |
| 965 | int i, rc=0; |
| 966 | Elf32_Phdr *phdr_ptr; |
| 967 | |
| 968 | phdr_ptr = (Elf32_Phdr*)(ehdr_ptr + 1); |
| 969 | |
| 970 | for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) { |
| 971 | u64 offset; |
| 972 | if (phdr_ptr->p_type != PT_NOTE) |
| 973 | continue; |
| 974 | offset = phdr_ptr->p_offset; |
Michael Holzheu | be8a8d0 | 2013-09-11 14:24:49 -0700 | [diff] [blame] | 975 | rc = elfcorehdr_read_notes(notes_buf, phdr_ptr->p_memsz, |
| 976 | &offset); |
HATAYAMA Daisuke | 087350c | 2013-07-03 15:02:19 -0700 | [diff] [blame] | 977 | if (rc < 0) |
| 978 | return rc; |
| 979 | notes_buf += phdr_ptr->p_memsz; |
| 980 | } |
| 981 | |
| 982 | return 0; |
| 983 | } |
| 984 | |
| 985 | /* Merges all the PT_NOTE headers into one. */ |
| 986 | static int __init merge_note_headers_elf32(char *elfptr, size_t *elfsz, |
| 987 | char **notes_buf, size_t *notes_sz) |
| 988 | { |
| 989 | int i, nr_ptnote=0, rc=0; |
| 990 | char *tmp; |
| 991 | Elf32_Ehdr *ehdr_ptr; |
| 992 | Elf32_Phdr phdr; |
| 993 | u64 phdr_sz = 0, note_off; |
| 994 | |
| 995 | ehdr_ptr = (Elf32_Ehdr *)elfptr; |
| 996 | |
| 997 | rc = update_note_header_size_elf32(ehdr_ptr); |
| 998 | if (rc < 0) |
| 999 | return rc; |
| 1000 | |
| 1001 | rc = get_note_number_and_size_elf32(ehdr_ptr, &nr_ptnote, &phdr_sz); |
| 1002 | if (rc < 0) |
| 1003 | return rc; |
| 1004 | |
| 1005 | *notes_sz = roundup(phdr_sz, PAGE_SIZE); |
Rahul Lakkireddy | 2724273 | 2018-05-02 15:17:17 +0530 | [diff] [blame] | 1006 | *notes_buf = vmcore_alloc_buf(*notes_sz); |
HATAYAMA Daisuke | 087350c | 2013-07-03 15:02:19 -0700 | [diff] [blame] | 1007 | if (!*notes_buf) |
| 1008 | return -ENOMEM; |
| 1009 | |
| 1010 | rc = copy_notes_elf32(ehdr_ptr, *notes_buf); |
| 1011 | if (rc < 0) |
| 1012 | return rc; |
| 1013 | |
Vivek Goyal | 72658e9 | 2005-06-25 14:58:22 -0700 | [diff] [blame] | 1014 | /* Prepare merged PT_NOTE program header. */ |
| 1015 | phdr.p_type = PT_NOTE; |
| 1016 | phdr.p_flags = 0; |
| 1017 | note_off = sizeof(Elf32_Ehdr) + |
| 1018 | (ehdr_ptr->e_phnum - nr_ptnote +1) * sizeof(Elf32_Phdr); |
HATAYAMA Daisuke | 087350c | 2013-07-03 15:02:19 -0700 | [diff] [blame] | 1019 | phdr.p_offset = roundup(note_off, PAGE_SIZE); |
Vivek Goyal | 72658e9 | 2005-06-25 14:58:22 -0700 | [diff] [blame] | 1020 | phdr.p_vaddr = phdr.p_paddr = 0; |
| 1021 | phdr.p_filesz = phdr.p_memsz = phdr_sz; |
| 1022 | phdr.p_align = 0; |
| 1023 | |
| 1024 | /* Add merged PT_NOTE program header*/ |
| 1025 | tmp = elfptr + sizeof(Elf32_Ehdr); |
| 1026 | memcpy(tmp, &phdr, sizeof(phdr)); |
| 1027 | tmp += sizeof(phdr); |
| 1028 | |
| 1029 | /* Remove unwanted PT_NOTE program headers. */ |
| 1030 | i = (nr_ptnote - 1) * sizeof(Elf32_Phdr); |
| 1031 | *elfsz = *elfsz - i; |
| 1032 | memmove(tmp, tmp+i, ((*elfsz)-sizeof(Elf32_Ehdr)-sizeof(Elf32_Phdr))); |
HATAYAMA Daisuke | f2bdacd | 2013-07-03 15:02:14 -0700 | [diff] [blame] | 1033 | memset(elfptr + *elfsz, 0, i); |
| 1034 | *elfsz = roundup(*elfsz, PAGE_SIZE); |
Vivek Goyal | 72658e9 | 2005-06-25 14:58:22 -0700 | [diff] [blame] | 1035 | |
| 1036 | /* Modify e_phnum to reflect merged headers. */ |
| 1037 | ehdr_ptr->e_phnum = ehdr_ptr->e_phnum - nr_ptnote + 1; |
| 1038 | |
Rahul Lakkireddy | 7efe48d | 2018-05-02 15:17:18 +0530 | [diff] [blame] | 1039 | /* Store the size of all notes. We need this to update the note |
| 1040 | * header when the device dumps will be added. |
| 1041 | */ |
| 1042 | elfnotes_orig_sz = phdr.p_memsz; |
| 1043 | |
Vivek Goyal | 72658e9 | 2005-06-25 14:58:22 -0700 | [diff] [blame] | 1044 | return 0; |
| 1045 | } |
| 1046 | |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 1047 | /* Add memory chunks represented by program headers to vmcore list. Also update |
| 1048 | * the new offset fields of exported program headers. */ |
| 1049 | static int __init process_ptload_program_headers_elf64(char *elfptr, |
| 1050 | size_t elfsz, |
HATAYAMA Daisuke | 087350c | 2013-07-03 15:02:19 -0700 | [diff] [blame] | 1051 | size_t elfnotes_sz, |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 1052 | struct list_head *vc_list) |
| 1053 | { |
| 1054 | int i; |
| 1055 | Elf64_Ehdr *ehdr_ptr; |
| 1056 | Elf64_Phdr *phdr_ptr; |
| 1057 | loff_t vmcore_off; |
| 1058 | struct vmcore *new; |
| 1059 | |
| 1060 | ehdr_ptr = (Elf64_Ehdr *)elfptr; |
| 1061 | phdr_ptr = (Elf64_Phdr*)(elfptr + sizeof(Elf64_Ehdr)); /* PT_NOTE hdr */ |
| 1062 | |
HATAYAMA Daisuke | 087350c | 2013-07-03 15:02:19 -0700 | [diff] [blame] | 1063 | /* Skip Elf header, program headers and Elf note segment. */ |
| 1064 | vmcore_off = elfsz + elfnotes_sz; |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 1065 | |
| 1066 | for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) { |
HATAYAMA Daisuke | 7f614cd | 2013-07-03 15:02:15 -0700 | [diff] [blame] | 1067 | u64 paddr, start, end, size; |
| 1068 | |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 1069 | if (phdr_ptr->p_type != PT_LOAD) |
| 1070 | continue; |
| 1071 | |
HATAYAMA Daisuke | 7f614cd | 2013-07-03 15:02:15 -0700 | [diff] [blame] | 1072 | paddr = phdr_ptr->p_offset; |
| 1073 | start = rounddown(paddr, PAGE_SIZE); |
| 1074 | end = roundup(paddr + phdr_ptr->p_memsz, PAGE_SIZE); |
| 1075 | size = end - start; |
| 1076 | |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 1077 | /* Add this contiguous chunk of memory to vmcore list.*/ |
| 1078 | new = get_new_element(); |
| 1079 | if (!new) |
| 1080 | return -ENOMEM; |
HATAYAMA Daisuke | 7f614cd | 2013-07-03 15:02:15 -0700 | [diff] [blame] | 1081 | new->paddr = start; |
| 1082 | new->size = size; |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 1083 | list_add_tail(&new->list, vc_list); |
| 1084 | |
| 1085 | /* Update the program header offset. */ |
HATAYAMA Daisuke | 7f614cd | 2013-07-03 15:02:15 -0700 | [diff] [blame] | 1086 | phdr_ptr->p_offset = vmcore_off + (paddr - start); |
| 1087 | vmcore_off = vmcore_off + size; |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 1088 | } |
| 1089 | return 0; |
| 1090 | } |
| 1091 | |
Vivek Goyal | 72658e9 | 2005-06-25 14:58:22 -0700 | [diff] [blame] | 1092 | static int __init process_ptload_program_headers_elf32(char *elfptr, |
| 1093 | size_t elfsz, |
HATAYAMA Daisuke | 087350c | 2013-07-03 15:02:19 -0700 | [diff] [blame] | 1094 | size_t elfnotes_sz, |
Vivek Goyal | 72658e9 | 2005-06-25 14:58:22 -0700 | [diff] [blame] | 1095 | struct list_head *vc_list) |
| 1096 | { |
| 1097 | int i; |
| 1098 | Elf32_Ehdr *ehdr_ptr; |
| 1099 | Elf32_Phdr *phdr_ptr; |
| 1100 | loff_t vmcore_off; |
| 1101 | struct vmcore *new; |
| 1102 | |
| 1103 | ehdr_ptr = (Elf32_Ehdr *)elfptr; |
| 1104 | phdr_ptr = (Elf32_Phdr*)(elfptr + sizeof(Elf32_Ehdr)); /* PT_NOTE hdr */ |
| 1105 | |
HATAYAMA Daisuke | 087350c | 2013-07-03 15:02:19 -0700 | [diff] [blame] | 1106 | /* Skip Elf header, program headers and Elf note segment. */ |
| 1107 | vmcore_off = elfsz + elfnotes_sz; |
Vivek Goyal | 72658e9 | 2005-06-25 14:58:22 -0700 | [diff] [blame] | 1108 | |
| 1109 | for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) { |
HATAYAMA Daisuke | 7f614cd | 2013-07-03 15:02:15 -0700 | [diff] [blame] | 1110 | u64 paddr, start, end, size; |
| 1111 | |
Vivek Goyal | 72658e9 | 2005-06-25 14:58:22 -0700 | [diff] [blame] | 1112 | if (phdr_ptr->p_type != PT_LOAD) |
| 1113 | continue; |
| 1114 | |
HATAYAMA Daisuke | 7f614cd | 2013-07-03 15:02:15 -0700 | [diff] [blame] | 1115 | paddr = phdr_ptr->p_offset; |
| 1116 | start = rounddown(paddr, PAGE_SIZE); |
| 1117 | end = roundup(paddr + phdr_ptr->p_memsz, PAGE_SIZE); |
| 1118 | size = end - start; |
| 1119 | |
Vivek Goyal | 72658e9 | 2005-06-25 14:58:22 -0700 | [diff] [blame] | 1120 | /* Add this contiguous chunk of memory to vmcore list.*/ |
| 1121 | new = get_new_element(); |
| 1122 | if (!new) |
| 1123 | return -ENOMEM; |
HATAYAMA Daisuke | 7f614cd | 2013-07-03 15:02:15 -0700 | [diff] [blame] | 1124 | new->paddr = start; |
| 1125 | new->size = size; |
Vivek Goyal | 72658e9 | 2005-06-25 14:58:22 -0700 | [diff] [blame] | 1126 | list_add_tail(&new->list, vc_list); |
| 1127 | |
| 1128 | /* Update the program header offset */ |
HATAYAMA Daisuke | 7f614cd | 2013-07-03 15:02:15 -0700 | [diff] [blame] | 1129 | phdr_ptr->p_offset = vmcore_off + (paddr - start); |
| 1130 | vmcore_off = vmcore_off + size; |
Vivek Goyal | 72658e9 | 2005-06-25 14:58:22 -0700 | [diff] [blame] | 1131 | } |
| 1132 | return 0; |
| 1133 | } |
| 1134 | |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 1135 | /* Sets offset fields of vmcore elements. */ |
Rahul Lakkireddy | 7efe48d | 2018-05-02 15:17:18 +0530 | [diff] [blame] | 1136 | static void set_vmcore_list_offsets(size_t elfsz, size_t elfnotes_sz, |
| 1137 | struct list_head *vc_list) |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 1138 | { |
| 1139 | loff_t vmcore_off; |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 1140 | struct vmcore *m; |
| 1141 | |
HATAYAMA Daisuke | 087350c | 2013-07-03 15:02:19 -0700 | [diff] [blame] | 1142 | /* Skip Elf header, program headers and Elf note segment. */ |
| 1143 | vmcore_off = elfsz + elfnotes_sz; |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 1144 | |
| 1145 | list_for_each_entry(m, vc_list, list) { |
| 1146 | m->offset = vmcore_off; |
| 1147 | vmcore_off += m->size; |
| 1148 | } |
| 1149 | } |
| 1150 | |
HATAYAMA Daisuke | f2bdacd | 2013-07-03 15:02:14 -0700 | [diff] [blame] | 1151 | static void free_elfcorebuf(void) |
Vivek Goyal | 72658e9 | 2005-06-25 14:58:22 -0700 | [diff] [blame] | 1152 | { |
HATAYAMA Daisuke | f2bdacd | 2013-07-03 15:02:14 -0700 | [diff] [blame] | 1153 | free_pages((unsigned long)elfcorebuf, get_order(elfcorebuf_sz_orig)); |
| 1154 | elfcorebuf = NULL; |
HATAYAMA Daisuke | 087350c | 2013-07-03 15:02:19 -0700 | [diff] [blame] | 1155 | vfree(elfnotes_buf); |
| 1156 | elfnotes_buf = NULL; |
Vivek Goyal | 72658e9 | 2005-06-25 14:58:22 -0700 | [diff] [blame] | 1157 | } |
| 1158 | |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 1159 | static int __init parse_crash_elf64_headers(void) |
| 1160 | { |
| 1161 | int rc=0; |
| 1162 | Elf64_Ehdr ehdr; |
| 1163 | u64 addr; |
| 1164 | |
| 1165 | addr = elfcorehdr_addr; |
| 1166 | |
| 1167 | /* Read Elf header */ |
Michael Holzheu | be8a8d0 | 2013-09-11 14:24:49 -0700 | [diff] [blame] | 1168 | rc = elfcorehdr_read((char *)&ehdr, sizeof(Elf64_Ehdr), &addr); |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 1169 | if (rc < 0) |
| 1170 | return rc; |
| 1171 | |
| 1172 | /* Do some basic Verification. */ |
| 1173 | if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0 || |
| 1174 | (ehdr.e_type != ET_CORE) || |
Mika Westerberg | 9833c39 | 2010-11-19 09:29:24 +0100 | [diff] [blame] | 1175 | !vmcore_elf64_check_arch(&ehdr) || |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 1176 | ehdr.e_ident[EI_CLASS] != ELFCLASS64 || |
| 1177 | ehdr.e_ident[EI_VERSION] != EV_CURRENT || |
| 1178 | ehdr.e_version != EV_CURRENT || |
| 1179 | ehdr.e_ehsize != sizeof(Elf64_Ehdr) || |
| 1180 | ehdr.e_phentsize != sizeof(Elf64_Phdr) || |
| 1181 | ehdr.e_phnum == 0) { |
Andrew Morton | 87ebdc0 | 2013-02-27 17:03:16 -0800 | [diff] [blame] | 1182 | pr_warn("Warning: Core image elf header is not sane\n"); |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 1183 | return -EINVAL; |
| 1184 | } |
| 1185 | |
| 1186 | /* Read in all elf headers. */ |
HATAYAMA Daisuke | f2bdacd | 2013-07-03 15:02:14 -0700 | [diff] [blame] | 1187 | elfcorebuf_sz_orig = sizeof(Elf64_Ehdr) + |
| 1188 | ehdr.e_phnum * sizeof(Elf64_Phdr); |
| 1189 | elfcorebuf_sz = elfcorebuf_sz_orig; |
| 1190 | elfcorebuf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, |
| 1191 | get_order(elfcorebuf_sz_orig)); |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 1192 | if (!elfcorebuf) |
| 1193 | return -ENOMEM; |
| 1194 | addr = elfcorehdr_addr; |
Michael Holzheu | be8a8d0 | 2013-09-11 14:24:49 -0700 | [diff] [blame] | 1195 | rc = elfcorehdr_read(elfcorebuf, elfcorebuf_sz_orig, &addr); |
HATAYAMA Daisuke | f2bdacd | 2013-07-03 15:02:14 -0700 | [diff] [blame] | 1196 | if (rc < 0) |
| 1197 | goto fail; |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 1198 | |
| 1199 | /* Merge all PT_NOTE headers into one. */ |
HATAYAMA Daisuke | 087350c | 2013-07-03 15:02:19 -0700 | [diff] [blame] | 1200 | rc = merge_note_headers_elf64(elfcorebuf, &elfcorebuf_sz, |
| 1201 | &elfnotes_buf, &elfnotes_sz); |
HATAYAMA Daisuke | f2bdacd | 2013-07-03 15:02:14 -0700 | [diff] [blame] | 1202 | if (rc) |
| 1203 | goto fail; |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 1204 | rc = process_ptload_program_headers_elf64(elfcorebuf, elfcorebuf_sz, |
HATAYAMA Daisuke | 087350c | 2013-07-03 15:02:19 -0700 | [diff] [blame] | 1205 | elfnotes_sz, &vmcore_list); |
HATAYAMA Daisuke | f2bdacd | 2013-07-03 15:02:14 -0700 | [diff] [blame] | 1206 | if (rc) |
| 1207 | goto fail; |
HATAYAMA Daisuke | 087350c | 2013-07-03 15:02:19 -0700 | [diff] [blame] | 1208 | set_vmcore_list_offsets(elfcorebuf_sz, elfnotes_sz, &vmcore_list); |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 1209 | return 0; |
HATAYAMA Daisuke | f2bdacd | 2013-07-03 15:02:14 -0700 | [diff] [blame] | 1210 | fail: |
| 1211 | free_elfcorebuf(); |
| 1212 | return rc; |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 1213 | } |
| 1214 | |
Vivek Goyal | 72658e9 | 2005-06-25 14:58:22 -0700 | [diff] [blame] | 1215 | static int __init parse_crash_elf32_headers(void) |
| 1216 | { |
| 1217 | int rc=0; |
| 1218 | Elf32_Ehdr ehdr; |
| 1219 | u64 addr; |
| 1220 | |
| 1221 | addr = elfcorehdr_addr; |
| 1222 | |
| 1223 | /* Read Elf header */ |
Michael Holzheu | be8a8d0 | 2013-09-11 14:24:49 -0700 | [diff] [blame] | 1224 | rc = elfcorehdr_read((char *)&ehdr, sizeof(Elf32_Ehdr), &addr); |
Vivek Goyal | 72658e9 | 2005-06-25 14:58:22 -0700 | [diff] [blame] | 1225 | if (rc < 0) |
| 1226 | return rc; |
| 1227 | |
| 1228 | /* Do some basic Verification. */ |
| 1229 | if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0 || |
| 1230 | (ehdr.e_type != ET_CORE) || |
Daniel Wagner | e55d531 | 2016-02-11 13:36:54 +0100 | [diff] [blame] | 1231 | !vmcore_elf32_check_arch(&ehdr) || |
Vivek Goyal | 72658e9 | 2005-06-25 14:58:22 -0700 | [diff] [blame] | 1232 | ehdr.e_ident[EI_CLASS] != ELFCLASS32|| |
| 1233 | ehdr.e_ident[EI_VERSION] != EV_CURRENT || |
| 1234 | ehdr.e_version != EV_CURRENT || |
| 1235 | ehdr.e_ehsize != sizeof(Elf32_Ehdr) || |
| 1236 | ehdr.e_phentsize != sizeof(Elf32_Phdr) || |
| 1237 | ehdr.e_phnum == 0) { |
Andrew Morton | 87ebdc0 | 2013-02-27 17:03:16 -0800 | [diff] [blame] | 1238 | pr_warn("Warning: Core image elf header is not sane\n"); |
Vivek Goyal | 72658e9 | 2005-06-25 14:58:22 -0700 | [diff] [blame] | 1239 | return -EINVAL; |
| 1240 | } |
| 1241 | |
| 1242 | /* Read in all elf headers. */ |
HATAYAMA Daisuke | f2bdacd | 2013-07-03 15:02:14 -0700 | [diff] [blame] | 1243 | elfcorebuf_sz_orig = sizeof(Elf32_Ehdr) + ehdr.e_phnum * sizeof(Elf32_Phdr); |
| 1244 | elfcorebuf_sz = elfcorebuf_sz_orig; |
| 1245 | elfcorebuf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, |
| 1246 | get_order(elfcorebuf_sz_orig)); |
Vivek Goyal | 72658e9 | 2005-06-25 14:58:22 -0700 | [diff] [blame] | 1247 | if (!elfcorebuf) |
| 1248 | return -ENOMEM; |
| 1249 | addr = elfcorehdr_addr; |
Michael Holzheu | be8a8d0 | 2013-09-11 14:24:49 -0700 | [diff] [blame] | 1250 | rc = elfcorehdr_read(elfcorebuf, elfcorebuf_sz_orig, &addr); |
HATAYAMA Daisuke | f2bdacd | 2013-07-03 15:02:14 -0700 | [diff] [blame] | 1251 | if (rc < 0) |
| 1252 | goto fail; |
Vivek Goyal | 72658e9 | 2005-06-25 14:58:22 -0700 | [diff] [blame] | 1253 | |
| 1254 | /* Merge all PT_NOTE headers into one. */ |
HATAYAMA Daisuke | 087350c | 2013-07-03 15:02:19 -0700 | [diff] [blame] | 1255 | rc = merge_note_headers_elf32(elfcorebuf, &elfcorebuf_sz, |
| 1256 | &elfnotes_buf, &elfnotes_sz); |
HATAYAMA Daisuke | f2bdacd | 2013-07-03 15:02:14 -0700 | [diff] [blame] | 1257 | if (rc) |
| 1258 | goto fail; |
Vivek Goyal | 72658e9 | 2005-06-25 14:58:22 -0700 | [diff] [blame] | 1259 | rc = process_ptload_program_headers_elf32(elfcorebuf, elfcorebuf_sz, |
HATAYAMA Daisuke | 087350c | 2013-07-03 15:02:19 -0700 | [diff] [blame] | 1260 | elfnotes_sz, &vmcore_list); |
HATAYAMA Daisuke | f2bdacd | 2013-07-03 15:02:14 -0700 | [diff] [blame] | 1261 | if (rc) |
| 1262 | goto fail; |
HATAYAMA Daisuke | 087350c | 2013-07-03 15:02:19 -0700 | [diff] [blame] | 1263 | set_vmcore_list_offsets(elfcorebuf_sz, elfnotes_sz, &vmcore_list); |
Vivek Goyal | 72658e9 | 2005-06-25 14:58:22 -0700 | [diff] [blame] | 1264 | return 0; |
HATAYAMA Daisuke | f2bdacd | 2013-07-03 15:02:14 -0700 | [diff] [blame] | 1265 | fail: |
| 1266 | free_elfcorebuf(); |
| 1267 | return rc; |
Vivek Goyal | 72658e9 | 2005-06-25 14:58:22 -0700 | [diff] [blame] | 1268 | } |
| 1269 | |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 1270 | static int __init parse_crash_elf_headers(void) |
| 1271 | { |
| 1272 | unsigned char e_ident[EI_NIDENT]; |
| 1273 | u64 addr; |
| 1274 | int rc=0; |
| 1275 | |
| 1276 | addr = elfcorehdr_addr; |
Michael Holzheu | be8a8d0 | 2013-09-11 14:24:49 -0700 | [diff] [blame] | 1277 | rc = elfcorehdr_read(e_ident, EI_NIDENT, &addr); |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 1278 | if (rc < 0) |
| 1279 | return rc; |
| 1280 | if (memcmp(e_ident, ELFMAG, SELFMAG) != 0) { |
Andrew Morton | 87ebdc0 | 2013-02-27 17:03:16 -0800 | [diff] [blame] | 1281 | pr_warn("Warning: Core image elf header not found\n"); |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 1282 | return -EINVAL; |
| 1283 | } |
| 1284 | |
| 1285 | if (e_ident[EI_CLASS] == ELFCLASS64) { |
| 1286 | rc = parse_crash_elf64_headers(); |
| 1287 | if (rc) |
| 1288 | return rc; |
Vivek Goyal | 72658e9 | 2005-06-25 14:58:22 -0700 | [diff] [blame] | 1289 | } else if (e_ident[EI_CLASS] == ELFCLASS32) { |
| 1290 | rc = parse_crash_elf32_headers(); |
| 1291 | if (rc) |
| 1292 | return rc; |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 1293 | } else { |
Andrew Morton | 87ebdc0 | 2013-02-27 17:03:16 -0800 | [diff] [blame] | 1294 | pr_warn("Warning: Core image elf header is not sane\n"); |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 1295 | return -EINVAL; |
| 1296 | } |
HATAYAMA Daisuke | 591ff71 | 2013-07-03 15:02:22 -0700 | [diff] [blame] | 1297 | |
| 1298 | /* Determine vmcore size. */ |
| 1299 | vmcore_size = get_vmcore_size(elfcorebuf_sz, elfnotes_sz, |
| 1300 | &vmcore_list); |
| 1301 | |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 1302 | return 0; |
| 1303 | } |
| 1304 | |
Rahul Lakkireddy | 2724273 | 2018-05-02 15:17:17 +0530 | [diff] [blame] | 1305 | #ifdef CONFIG_PROC_VMCORE_DEVICE_DUMP |
| 1306 | /** |
| 1307 | * vmcoredd_write_header - Write vmcore device dump header at the |
| 1308 | * beginning of the dump's buffer. |
| 1309 | * @buf: Output buffer where the note is written |
| 1310 | * @data: Dump info |
| 1311 | * @size: Size of the dump |
| 1312 | * |
| 1313 | * Fills beginning of the dump's buffer with vmcore device dump header. |
| 1314 | */ |
| 1315 | static void vmcoredd_write_header(void *buf, struct vmcoredd_data *data, |
| 1316 | u32 size) |
| 1317 | { |
| 1318 | struct vmcoredd_header *vdd_hdr = (struct vmcoredd_header *)buf; |
| 1319 | |
| 1320 | vdd_hdr->n_namesz = sizeof(vdd_hdr->name); |
| 1321 | vdd_hdr->n_descsz = size + sizeof(vdd_hdr->dump_name); |
| 1322 | vdd_hdr->n_type = NT_VMCOREDD; |
| 1323 | |
| 1324 | strncpy((char *)vdd_hdr->name, VMCOREDD_NOTE_NAME, |
| 1325 | sizeof(vdd_hdr->name)); |
| 1326 | memcpy(vdd_hdr->dump_name, data->dump_name, sizeof(vdd_hdr->dump_name)); |
| 1327 | } |
| 1328 | |
| 1329 | /** |
Rahul Lakkireddy | 7efe48d | 2018-05-02 15:17:18 +0530 | [diff] [blame] | 1330 | * vmcoredd_update_program_headers - Update all Elf program headers |
| 1331 | * @elfptr: Pointer to elf header |
| 1332 | * @elfnotesz: Size of elf notes aligned to page size |
| 1333 | * @vmcoreddsz: Size of device dumps to be added to elf note header |
| 1334 | * |
| 1335 | * Determine type of Elf header (Elf64 or Elf32) and update the elf note size. |
| 1336 | * Also update the offsets of all the program headers after the elf note header. |
| 1337 | */ |
| 1338 | static void vmcoredd_update_program_headers(char *elfptr, size_t elfnotesz, |
| 1339 | size_t vmcoreddsz) |
| 1340 | { |
| 1341 | unsigned char *e_ident = (unsigned char *)elfptr; |
| 1342 | u64 start, end, size; |
| 1343 | loff_t vmcore_off; |
| 1344 | u32 i; |
| 1345 | |
| 1346 | vmcore_off = elfcorebuf_sz + elfnotesz; |
| 1347 | |
| 1348 | if (e_ident[EI_CLASS] == ELFCLASS64) { |
| 1349 | Elf64_Ehdr *ehdr = (Elf64_Ehdr *)elfptr; |
| 1350 | Elf64_Phdr *phdr = (Elf64_Phdr *)(elfptr + sizeof(Elf64_Ehdr)); |
| 1351 | |
| 1352 | /* Update all program headers */ |
| 1353 | for (i = 0; i < ehdr->e_phnum; i++, phdr++) { |
| 1354 | if (phdr->p_type == PT_NOTE) { |
| 1355 | /* Update note size */ |
| 1356 | phdr->p_memsz = elfnotes_orig_sz + vmcoreddsz; |
| 1357 | phdr->p_filesz = phdr->p_memsz; |
| 1358 | continue; |
| 1359 | } |
| 1360 | |
| 1361 | start = rounddown(phdr->p_offset, PAGE_SIZE); |
| 1362 | end = roundup(phdr->p_offset + phdr->p_memsz, |
| 1363 | PAGE_SIZE); |
| 1364 | size = end - start; |
| 1365 | phdr->p_offset = vmcore_off + (phdr->p_offset - start); |
| 1366 | vmcore_off += size; |
| 1367 | } |
| 1368 | } else { |
| 1369 | Elf32_Ehdr *ehdr = (Elf32_Ehdr *)elfptr; |
| 1370 | Elf32_Phdr *phdr = (Elf32_Phdr *)(elfptr + sizeof(Elf32_Ehdr)); |
| 1371 | |
| 1372 | /* Update all program headers */ |
| 1373 | for (i = 0; i < ehdr->e_phnum; i++, phdr++) { |
| 1374 | if (phdr->p_type == PT_NOTE) { |
| 1375 | /* Update note size */ |
| 1376 | phdr->p_memsz = elfnotes_orig_sz + vmcoreddsz; |
| 1377 | phdr->p_filesz = phdr->p_memsz; |
| 1378 | continue; |
| 1379 | } |
| 1380 | |
| 1381 | start = rounddown(phdr->p_offset, PAGE_SIZE); |
| 1382 | end = roundup(phdr->p_offset + phdr->p_memsz, |
| 1383 | PAGE_SIZE); |
| 1384 | size = end - start; |
| 1385 | phdr->p_offset = vmcore_off + (phdr->p_offset - start); |
| 1386 | vmcore_off += size; |
| 1387 | } |
| 1388 | } |
| 1389 | } |
| 1390 | |
| 1391 | /** |
| 1392 | * vmcoredd_update_size - Update the total size of the device dumps and update |
| 1393 | * Elf header |
| 1394 | * @dump_size: Size of the current device dump to be added to total size |
| 1395 | * |
| 1396 | * Update the total size of all the device dumps and update the Elf program |
| 1397 | * headers. Calculate the new offsets for the vmcore list and update the |
| 1398 | * total vmcore size. |
| 1399 | */ |
| 1400 | static void vmcoredd_update_size(size_t dump_size) |
| 1401 | { |
| 1402 | vmcoredd_orig_sz += dump_size; |
| 1403 | elfnotes_sz = roundup(elfnotes_orig_sz, PAGE_SIZE) + vmcoredd_orig_sz; |
| 1404 | vmcoredd_update_program_headers(elfcorebuf, elfnotes_sz, |
| 1405 | vmcoredd_orig_sz); |
| 1406 | |
| 1407 | /* Update vmcore list offsets */ |
| 1408 | set_vmcore_list_offsets(elfcorebuf_sz, elfnotes_sz, &vmcore_list); |
| 1409 | |
| 1410 | vmcore_size = get_vmcore_size(elfcorebuf_sz, elfnotes_sz, |
| 1411 | &vmcore_list); |
| 1412 | proc_vmcore->size = vmcore_size; |
| 1413 | } |
| 1414 | |
| 1415 | /** |
Rahul Lakkireddy | 2724273 | 2018-05-02 15:17:17 +0530 | [diff] [blame] | 1416 | * vmcore_add_device_dump - Add a buffer containing device dump to vmcore |
| 1417 | * @data: dump info. |
| 1418 | * |
| 1419 | * Allocate a buffer and invoke the calling driver's dump collect routine. |
| 1420 | * Write Elf note at the beginning of the buffer to indicate vmcore device |
| 1421 | * dump and add the dump to global list. |
| 1422 | */ |
| 1423 | int vmcore_add_device_dump(struct vmcoredd_data *data) |
| 1424 | { |
| 1425 | struct vmcoredd_node *dump; |
| 1426 | void *buf = NULL; |
| 1427 | size_t data_size; |
| 1428 | int ret; |
| 1429 | |
| 1430 | if (!data || !strlen(data->dump_name) || |
| 1431 | !data->vmcoredd_callback || !data->size) |
| 1432 | return -EINVAL; |
| 1433 | |
| 1434 | dump = vzalloc(sizeof(*dump)); |
| 1435 | if (!dump) { |
| 1436 | ret = -ENOMEM; |
| 1437 | goto out_err; |
| 1438 | } |
| 1439 | |
| 1440 | /* Keep size of the buffer page aligned so that it can be mmaped */ |
| 1441 | data_size = roundup(sizeof(struct vmcoredd_header) + data->size, |
| 1442 | PAGE_SIZE); |
| 1443 | |
| 1444 | /* Allocate buffer for driver's to write their dumps */ |
| 1445 | buf = vmcore_alloc_buf(data_size); |
| 1446 | if (!buf) { |
| 1447 | ret = -ENOMEM; |
| 1448 | goto out_err; |
| 1449 | } |
| 1450 | |
| 1451 | vmcoredd_write_header(buf, data, data_size - |
| 1452 | sizeof(struct vmcoredd_header)); |
| 1453 | |
| 1454 | /* Invoke the driver's dump collection routing */ |
| 1455 | ret = data->vmcoredd_callback(data, buf + |
| 1456 | sizeof(struct vmcoredd_header)); |
| 1457 | if (ret) |
| 1458 | goto out_err; |
| 1459 | |
| 1460 | dump->buf = buf; |
| 1461 | dump->size = data_size; |
| 1462 | |
| 1463 | /* Add the dump to driver sysfs list */ |
| 1464 | mutex_lock(&vmcoredd_mutex); |
| 1465 | list_add_tail(&dump->list, &vmcoredd_list); |
| 1466 | mutex_unlock(&vmcoredd_mutex); |
| 1467 | |
Rahul Lakkireddy | 7efe48d | 2018-05-02 15:17:18 +0530 | [diff] [blame] | 1468 | vmcoredd_update_size(data_size); |
Rahul Lakkireddy | 2724273 | 2018-05-02 15:17:17 +0530 | [diff] [blame] | 1469 | return 0; |
| 1470 | |
| 1471 | out_err: |
| 1472 | if (buf) |
| 1473 | vfree(buf); |
| 1474 | |
| 1475 | if (dump) |
| 1476 | vfree(dump); |
| 1477 | |
| 1478 | return ret; |
| 1479 | } |
| 1480 | EXPORT_SYMBOL(vmcore_add_device_dump); |
| 1481 | #endif /* CONFIG_PROC_VMCORE_DEVICE_DUMP */ |
| 1482 | |
| 1483 | /* Free all dumps in vmcore device dump list */ |
| 1484 | static void vmcore_free_device_dumps(void) |
| 1485 | { |
| 1486 | #ifdef CONFIG_PROC_VMCORE_DEVICE_DUMP |
| 1487 | mutex_lock(&vmcoredd_mutex); |
| 1488 | while (!list_empty(&vmcoredd_list)) { |
| 1489 | struct vmcoredd_node *dump; |
| 1490 | |
| 1491 | dump = list_first_entry(&vmcoredd_list, struct vmcoredd_node, |
| 1492 | list); |
| 1493 | list_del(&dump->list); |
| 1494 | vfree(dump->buf); |
| 1495 | vfree(dump); |
| 1496 | } |
| 1497 | mutex_unlock(&vmcoredd_mutex); |
| 1498 | #endif /* CONFIG_PROC_VMCORE_DEVICE_DUMP */ |
| 1499 | } |
| 1500 | |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 1501 | /* Init function for vmcore module. */ |
| 1502 | static int __init vmcore_init(void) |
| 1503 | { |
| 1504 | int rc = 0; |
| 1505 | |
Michael Holzheu | be8a8d0 | 2013-09-11 14:24:49 -0700 | [diff] [blame] | 1506 | /* Allow architectures to allocate ELF header in 2nd kernel */ |
| 1507 | rc = elfcorehdr_alloc(&elfcorehdr_addr, &elfcorehdr_size); |
| 1508 | if (rc) |
| 1509 | return rc; |
| 1510 | /* |
| 1511 | * If elfcorehdr= has been passed in cmdline or created in 2nd kernel, |
| 1512 | * then capture the dump. |
| 1513 | */ |
Simon Horman | 85a0ee3 | 2008-10-18 20:28:29 -0700 | [diff] [blame] | 1514 | if (!(is_vmcore_usable())) |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 1515 | return rc; |
| 1516 | rc = parse_crash_elf_headers(); |
| 1517 | if (rc) { |
Andrew Morton | 87ebdc0 | 2013-02-27 17:03:16 -0800 | [diff] [blame] | 1518 | pr_warn("Kdump: vmcore not initialized\n"); |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 1519 | return rc; |
| 1520 | } |
Michael Holzheu | be8a8d0 | 2013-09-11 14:24:49 -0700 | [diff] [blame] | 1521 | elfcorehdr_free(elfcorehdr_addr); |
| 1522 | elfcorehdr_addr = ELFCORE_ADDR_ERR; |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 1523 | |
Alexey Dobriyan | 5aa140c | 2008-10-06 14:36:31 +0400 | [diff] [blame] | 1524 | proc_vmcore = proc_create("vmcore", S_IRUSR, NULL, &proc_vmcore_operations); |
Vivek Goyal | 666bfdd | 2005-06-25 14:58:21 -0700 | [diff] [blame] | 1525 | if (proc_vmcore) |
| 1526 | proc_vmcore->size = vmcore_size; |
| 1527 | return 0; |
| 1528 | } |
Paul Gortmaker | abaf378 | 2014-01-23 15:55:45 -0800 | [diff] [blame] | 1529 | fs_initcall(vmcore_init); |
Mahesh Salgaonkar | 16257393 | 2012-02-16 01:15:00 +0000 | [diff] [blame] | 1530 | |
| 1531 | /* Cleanup function for vmcore module. */ |
| 1532 | void vmcore_cleanup(void) |
| 1533 | { |
Mahesh Salgaonkar | 16257393 | 2012-02-16 01:15:00 +0000 | [diff] [blame] | 1534 | if (proc_vmcore) { |
David Howells | a8ca16e | 2013-04-12 17:27:28 +0100 | [diff] [blame] | 1535 | proc_remove(proc_vmcore); |
Mahesh Salgaonkar | 16257393 | 2012-02-16 01:15:00 +0000 | [diff] [blame] | 1536 | proc_vmcore = NULL; |
| 1537 | } |
| 1538 | |
| 1539 | /* clear the vmcore list. */ |
Alexey Dobriyan | 593bc69 | 2018-02-06 15:37:02 -0800 | [diff] [blame] | 1540 | while (!list_empty(&vmcore_list)) { |
Mahesh Salgaonkar | 16257393 | 2012-02-16 01:15:00 +0000 | [diff] [blame] | 1541 | struct vmcore *m; |
| 1542 | |
Alexey Dobriyan | 593bc69 | 2018-02-06 15:37:02 -0800 | [diff] [blame] | 1543 | m = list_first_entry(&vmcore_list, struct vmcore, list); |
Mahesh Salgaonkar | 16257393 | 2012-02-16 01:15:00 +0000 | [diff] [blame] | 1544 | list_del(&m->list); |
| 1545 | kfree(m); |
| 1546 | } |
HATAYAMA Daisuke | f2bdacd | 2013-07-03 15:02:14 -0700 | [diff] [blame] | 1547 | free_elfcorebuf(); |
Rahul Lakkireddy | 2724273 | 2018-05-02 15:17:17 +0530 | [diff] [blame] | 1548 | |
| 1549 | /* clear vmcore device dump list */ |
| 1550 | vmcore_free_device_dumps(); |
Mahesh Salgaonkar | 16257393 | 2012-02-16 01:15:00 +0000 | [diff] [blame] | 1551 | } |