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