~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/proc/vmcore.c

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /fs/proc/vmcore.c (Version linux-6.12-rc7) and /fs/proc/vmcore.c (Version linux-5.1.21)


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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php