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