diff options
Diffstat (limited to 'fs/proc/vmcore.c')
| -rw-r--r-- | fs/proc/vmcore.c | 694 |
1 files changed, 484 insertions, 210 deletions
diff --git a/fs/proc/vmcore.c b/fs/proc/vmcore.c index 17f7e080d7ff..28503172f2e4 100644 --- a/fs/proc/vmcore.c +++ b/fs/proc/vmcore.c | |||
| @@ -20,6 +20,7 @@ | |||
| 20 | #include <linux/init.h> | 20 | #include <linux/init.h> |
| 21 | #include <linux/crash_dump.h> | 21 | #include <linux/crash_dump.h> |
| 22 | #include <linux/list.h> | 22 | #include <linux/list.h> |
| 23 | #include <linux/vmalloc.h> | ||
| 23 | #include <asm/uaccess.h> | 24 | #include <asm/uaccess.h> |
| 24 | #include <asm/io.h> | 25 | #include <asm/io.h> |
| 25 | #include "internal.h" | 26 | #include "internal.h" |
| @@ -32,6 +33,10 @@ static LIST_HEAD(vmcore_list); | |||
| 32 | /* Stores the pointer to the buffer containing kernel elf core headers. */ | 33 | /* Stores the pointer to the buffer containing kernel elf core headers. */ |
| 33 | static char *elfcorebuf; | 34 | static char *elfcorebuf; |
| 34 | static size_t elfcorebuf_sz; | 35 | static size_t elfcorebuf_sz; |
| 36 | static size_t elfcorebuf_sz_orig; | ||
| 37 | |||
| 38 | static char *elfnotes_buf; | ||
| 39 | static size_t elfnotes_sz; | ||
| 35 | 40 | ||
| 36 | /* Total size of vmcore file. */ | 41 | /* Total size of vmcore file. */ |
| 37 | static u64 vmcore_size; | 42 | static u64 vmcore_size; |
| @@ -118,27 +123,6 @@ static ssize_t read_from_oldmem(char *buf, size_t count, | |||
| 118 | return read; | 123 | return read; |
| 119 | } | 124 | } |
| 120 | 125 | ||
| 121 | /* Maps vmcore file offset to respective physical address in memroy. */ | ||
| 122 | static u64 map_offset_to_paddr(loff_t offset, struct list_head *vc_list, | ||
| 123 | struct vmcore **m_ptr) | ||
| 124 | { | ||
| 125 | struct vmcore *m; | ||
| 126 | u64 paddr; | ||
| 127 | |||
| 128 | list_for_each_entry(m, vc_list, list) { | ||
| 129 | u64 start, end; | ||
| 130 | start = m->offset; | ||
| 131 | end = m->offset + m->size - 1; | ||
| 132 | if (offset >= start && offset <= end) { | ||
| 133 | paddr = m->paddr + offset - start; | ||
| 134 | *m_ptr = m; | ||
| 135 | return paddr; | ||
| 136 | } | ||
| 137 | } | ||
| 138 | *m_ptr = NULL; | ||
| 139 | return 0; | ||
| 140 | } | ||
| 141 | |||
| 142 | /* Read from the ELF header and then the crash dump. On error, negative value is | 126 | /* Read from the ELF header and then the crash dump. On error, negative value is |
| 143 | * returned otherwise number of bytes read are returned. | 127 | * returned otherwise number of bytes read are returned. |
| 144 | */ | 128 | */ |
| @@ -147,8 +131,8 @@ static ssize_t read_vmcore(struct file *file, char __user *buffer, | |||
| 147 | { | 131 | { |
| 148 | ssize_t acc = 0, tmp; | 132 | ssize_t acc = 0, tmp; |
| 149 | size_t tsz; | 133 | size_t tsz; |
| 150 | u64 start, nr_bytes; | 134 | u64 start; |
| 151 | struct vmcore *curr_m = NULL; | 135 | struct vmcore *m = NULL; |
| 152 | 136 | ||
| 153 | if (buflen == 0 || *fpos >= vmcore_size) | 137 | if (buflen == 0 || *fpos >= vmcore_size) |
| 154 | return 0; | 138 | return 0; |
| @@ -159,9 +143,7 @@ static ssize_t read_vmcore(struct file *file, char __user *buffer, | |||
| 159 | 143 | ||
| 160 | /* Read ELF core header */ | 144 | /* Read ELF core header */ |
| 161 | if (*fpos < elfcorebuf_sz) { | 145 | if (*fpos < elfcorebuf_sz) { |
| 162 | tsz = elfcorebuf_sz - *fpos; | 146 | tsz = min(elfcorebuf_sz - (size_t)*fpos, buflen); |
| 163 | if (buflen < tsz) | ||
| 164 | tsz = buflen; | ||
| 165 | if (copy_to_user(buffer, elfcorebuf + *fpos, tsz)) | 147 | if (copy_to_user(buffer, elfcorebuf + *fpos, tsz)) |
| 166 | return -EFAULT; | 148 | return -EFAULT; |
| 167 | buflen -= tsz; | 149 | buflen -= tsz; |
| @@ -174,39 +156,161 @@ static ssize_t read_vmcore(struct file *file, char __user *buffer, | |||
| 174 | return acc; | 156 | return acc; |
| 175 | } | 157 | } |
| 176 | 158 | ||
| 177 | start = map_offset_to_paddr(*fpos, &vmcore_list, &curr_m); | 159 | /* Read Elf note segment */ |
| 178 | if (!curr_m) | 160 | if (*fpos < elfcorebuf_sz + elfnotes_sz) { |
| 179 | return -EINVAL; | 161 | void *kaddr; |
| 180 | |||
| 181 | while (buflen) { | ||
| 182 | tsz = min_t(size_t, buflen, PAGE_SIZE - (start & ~PAGE_MASK)); | ||
| 183 | 162 | ||
| 184 | /* Calculate left bytes in current memory segment. */ | 163 | tsz = min(elfcorebuf_sz + elfnotes_sz - (size_t)*fpos, buflen); |
| 185 | nr_bytes = (curr_m->size - (start - curr_m->paddr)); | 164 | kaddr = elfnotes_buf + *fpos - elfcorebuf_sz; |
| 186 | if (tsz > nr_bytes) | 165 | if (copy_to_user(buffer, kaddr, tsz)) |
| 187 | tsz = nr_bytes; | 166 | return -EFAULT; |
| 188 | |||
| 189 | tmp = read_from_oldmem(buffer, tsz, &start, 1); | ||
| 190 | if (tmp < 0) | ||
| 191 | return tmp; | ||
| 192 | buflen -= tsz; | 167 | buflen -= tsz; |
| 193 | *fpos += tsz; | 168 | *fpos += tsz; |
| 194 | buffer += tsz; | 169 | buffer += tsz; |
| 195 | acc += tsz; | 170 | acc += tsz; |
| 196 | if (start >= (curr_m->paddr + curr_m->size)) { | 171 | |
| 197 | if (curr_m->list.next == &vmcore_list) | 172 | /* leave now if filled buffer already */ |
| 198 | return acc; /*EOF*/ | 173 | if (buflen == 0) |
| 199 | curr_m = list_entry(curr_m->list.next, | 174 | return acc; |
| 200 | struct vmcore, list); | 175 | } |
| 201 | start = curr_m->paddr; | 176 | |
| 177 | list_for_each_entry(m, &vmcore_list, list) { | ||
| 178 | if (*fpos < m->offset + m->size) { | ||
| 179 | tsz = min_t(size_t, m->offset + m->size - *fpos, buflen); | ||
| 180 | start = m->paddr + *fpos - m->offset; | ||
| 181 | tmp = read_from_oldmem(buffer, tsz, &start, 1); | ||
| 182 | if (tmp < 0) | ||
| 183 | return tmp; | ||
| 184 | buflen -= tsz; | ||
| 185 | *fpos += tsz; | ||
| 186 | buffer += tsz; | ||
| 187 | acc += tsz; | ||
| 188 | |||
| 189 | /* leave now if filled buffer already */ | ||
| 190 | if (buflen == 0) | ||
| 191 | return acc; | ||
| 202 | } | 192 | } |
| 203 | } | 193 | } |
| 194 | |||
| 204 | return acc; | 195 | return acc; |
| 205 | } | 196 | } |
| 206 | 197 | ||
| 198 | /** | ||
| 199 | * alloc_elfnotes_buf - allocate buffer for ELF note segment in | ||
| 200 | * vmalloc memory | ||
| 201 | * | ||
| 202 | * @notes_sz: size of buffer | ||
| 203 | * | ||
| 204 | * If CONFIG_MMU is defined, use vmalloc_user() to allow users to mmap | ||
| 205 | * the buffer to user-space by means of remap_vmalloc_range(). | ||
| 206 | * | ||
| 207 | * If CONFIG_MMU is not defined, use vzalloc() since mmap_vmcore() is | ||
| 208 | * disabled and there's no need to allow users to mmap the buffer. | ||
| 209 | */ | ||
| 210 | static inline char *alloc_elfnotes_buf(size_t notes_sz) | ||
| 211 | { | ||
| 212 | #ifdef CONFIG_MMU | ||
| 213 | return vmalloc_user(notes_sz); | ||
| 214 | #else | ||
| 215 | return vzalloc(notes_sz); | ||
| 216 | #endif | ||
| 217 | } | ||
| 218 | |||
| 219 | /* | ||
| 220 | * Disable mmap_vmcore() if CONFIG_MMU is not defined. MMU is | ||
| 221 | * essential for mmap_vmcore() in order to map physically | ||
| 222 | * non-contiguous objects (ELF header, ELF note segment and memory | ||
| 223 | * regions in the 1st kernel pointed to by PT_LOAD entries) into | ||
| 224 | * virtually contiguous user-space in ELF layout. | ||
| 225 | */ | ||
| 226 | #ifdef CONFIG_MMU | ||
| 227 | static int mmap_vmcore(struct file *file, struct vm_area_struct *vma) | ||
| 228 | { | ||
| 229 | size_t size = vma->vm_end - vma->vm_start; | ||
| 230 | u64 start, end, len, tsz; | ||
| 231 | struct vmcore *m; | ||
| 232 | |||
| 233 | start = (u64)vma->vm_pgoff << PAGE_SHIFT; | ||
| 234 | end = start + size; | ||
| 235 | |||
| 236 | if (size > vmcore_size || end > vmcore_size) | ||
| 237 | return -EINVAL; | ||
| 238 | |||
| 239 | if (vma->vm_flags & (VM_WRITE | VM_EXEC)) | ||
| 240 | return -EPERM; | ||
| 241 | |||
| 242 | vma->vm_flags &= ~(VM_MAYWRITE | VM_MAYEXEC); | ||
| 243 | vma->vm_flags |= VM_MIXEDMAP; | ||
| 244 | |||
| 245 | len = 0; | ||
| 246 | |||
| 247 | if (start < elfcorebuf_sz) { | ||
| 248 | u64 pfn; | ||
| 249 | |||
| 250 | tsz = min(elfcorebuf_sz - (size_t)start, size); | ||
| 251 | pfn = __pa(elfcorebuf + start) >> PAGE_SHIFT; | ||
| 252 | if (remap_pfn_range(vma, vma->vm_start, pfn, tsz, | ||
| 253 | vma->vm_page_prot)) | ||
| 254 | return -EAGAIN; | ||
| 255 | size -= tsz; | ||
| 256 | start += tsz; | ||
| 257 | len += tsz; | ||
| 258 | |||
| 259 | if (size == 0) | ||
| 260 | return 0; | ||
| 261 | } | ||
| 262 | |||
| 263 | if (start < elfcorebuf_sz + elfnotes_sz) { | ||
| 264 | void *kaddr; | ||
| 265 | |||
| 266 | tsz = min(elfcorebuf_sz + elfnotes_sz - (size_t)start, size); | ||
| 267 | kaddr = elfnotes_buf + start - elfcorebuf_sz; | ||
| 268 | if (remap_vmalloc_range_partial(vma, vma->vm_start + len, | ||
| 269 | kaddr, tsz)) | ||
| 270 | goto fail; | ||
| 271 | size -= tsz; | ||
| 272 | start += tsz; | ||
| 273 | len += tsz; | ||
| 274 | |||
| 275 | if (size == 0) | ||
| 276 | return 0; | ||
| 277 | } | ||
| 278 | |||
| 279 | list_for_each_entry(m, &vmcore_list, list) { | ||
| 280 | if (start < m->offset + m->size) { | ||
| 281 | u64 paddr = 0; | ||
| 282 | |||
| 283 | tsz = min_t(size_t, m->offset + m->size - start, size); | ||
| 284 | paddr = m->paddr + start - m->offset; | ||
| 285 | if (remap_pfn_range(vma, vma->vm_start + len, | ||
| 286 | paddr >> PAGE_SHIFT, tsz, | ||
| 287 | vma->vm_page_prot)) | ||
| 288 | goto fail; | ||
| 289 | size -= tsz; | ||
| 290 | start += tsz; | ||
| 291 | len += tsz; | ||
| 292 | |||
| 293 | if (size == 0) | ||
| 294 | return 0; | ||
| 295 | } | ||
| 296 | } | ||
| 297 | |||
| 298 | return 0; | ||
| 299 | fail: | ||
| 300 | do_munmap(vma->vm_mm, vma->vm_start, len); | ||
| 301 | return -EAGAIN; | ||
| 302 | } | ||
| 303 | #else | ||
| 304 | static int mmap_vmcore(struct file *file, struct vm_area_struct *vma) | ||
| 305 | { | ||
| 306 | return -ENOSYS; | ||
| 307 | } | ||
| 308 | #endif | ||
| 309 | |||
| 207 | static const struct file_operations proc_vmcore_operations = { | 310 | static const struct file_operations proc_vmcore_operations = { |
| 208 | .read = read_vmcore, | 311 | .read = read_vmcore, |
| 209 | .llseek = default_llseek, | 312 | .llseek = default_llseek, |
| 313 | .mmap = mmap_vmcore, | ||
| 210 | }; | 314 | }; |
| 211 | 315 | ||
| 212 | static struct vmcore* __init get_new_element(void) | 316 | static struct vmcore* __init get_new_element(void) |
| @@ -214,61 +318,40 @@ static struct vmcore* __init get_new_element(void) | |||
| 214 | return kzalloc(sizeof(struct vmcore), GFP_KERNEL); | 318 | return kzalloc(sizeof(struct vmcore), GFP_KERNEL); |
| 215 | } | 319 | } |
| 216 | 320 | ||
| 217 | static u64 __init get_vmcore_size_elf64(char *elfptr) | 321 | static u64 __init get_vmcore_size(size_t elfsz, size_t elfnotesegsz, |
| 322 | struct list_head *vc_list) | ||
| 218 | { | 323 | { |
| 219 | int i; | ||
| 220 | u64 size; | ||
| 221 | Elf64_Ehdr *ehdr_ptr; | ||
| 222 | Elf64_Phdr *phdr_ptr; | ||
| 223 | |||
| 224 | ehdr_ptr = (Elf64_Ehdr *)elfptr; | ||
| 225 | phdr_ptr = (Elf64_Phdr*)(elfptr + sizeof(Elf64_Ehdr)); | ||
| 226 | size = sizeof(Elf64_Ehdr) + ((ehdr_ptr->e_phnum) * sizeof(Elf64_Phdr)); | ||
| 227 | for (i = 0; i < ehdr_ptr->e_phnum; i++) { | ||
| 228 | size += phdr_ptr->p_memsz; | ||
| 229 | phdr_ptr++; | ||
| 230 | } | ||
| 231 | return size; | ||
| 232 | } | ||
| 233 | |||
| 234 | static u64 __init get_vmcore_size_elf32(char *elfptr) | ||
| 235 | { | ||
| 236 | int i; | ||
| 237 | u64 size; | 324 | u64 size; |
| 238 | Elf32_Ehdr *ehdr_ptr; | 325 | struct vmcore *m; |
| 239 | Elf32_Phdr *phdr_ptr; | ||
| 240 | 326 | ||
| 241 | ehdr_ptr = (Elf32_Ehdr *)elfptr; | 327 | size = elfsz + elfnotesegsz; |
| 242 | phdr_ptr = (Elf32_Phdr*)(elfptr + sizeof(Elf32_Ehdr)); | 328 | list_for_each_entry(m, vc_list, list) { |
| 243 | size = sizeof(Elf32_Ehdr) + ((ehdr_ptr->e_phnum) * sizeof(Elf32_Phdr)); | 329 | size += m->size; |
| 244 | for (i = 0; i < ehdr_ptr->e_phnum; i++) { | ||
| 245 | size += phdr_ptr->p_memsz; | ||
| 246 | phdr_ptr++; | ||
| 247 | } | 330 | } |
| 248 | return size; | 331 | return size; |
| 249 | } | 332 | } |
| 250 | 333 | ||
| 251 | /* Merges all the PT_NOTE headers into one. */ | 334 | /** |
| 252 | static int __init merge_note_headers_elf64(char *elfptr, size_t *elfsz, | 335 | * update_note_header_size_elf64 - update p_memsz member of each PT_NOTE entry |
| 253 | struct list_head *vc_list) | 336 | * |
| 337 | * @ehdr_ptr: ELF header | ||
| 338 | * | ||
| 339 | * This function updates p_memsz member of each PT_NOTE entry in the | ||
| 340 | * program header table pointed to by @ehdr_ptr to real size of ELF | ||
| 341 | * note segment. | ||
| 342 | */ | ||
| 343 | static int __init update_note_header_size_elf64(const Elf64_Ehdr *ehdr_ptr) | ||
| 254 | { | 344 | { |
| 255 | int i, nr_ptnote=0, rc=0; | 345 | int i, rc=0; |
| 256 | char *tmp; | 346 | Elf64_Phdr *phdr_ptr; |
| 257 | Elf64_Ehdr *ehdr_ptr; | ||
| 258 | Elf64_Phdr phdr, *phdr_ptr; | ||
| 259 | Elf64_Nhdr *nhdr_ptr; | 347 | Elf64_Nhdr *nhdr_ptr; |
| 260 | u64 phdr_sz = 0, note_off; | ||
| 261 | 348 | ||
| 262 | ehdr_ptr = (Elf64_Ehdr *)elfptr; | 349 | phdr_ptr = (Elf64_Phdr *)(ehdr_ptr + 1); |
| 263 | phdr_ptr = (Elf64_Phdr*)(elfptr + sizeof(Elf64_Ehdr)); | ||
| 264 | for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) { | 350 | for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) { |
| 265 | int j; | ||
| 266 | void *notes_section; | 351 | void *notes_section; |
| 267 | struct vmcore *new; | ||
| 268 | u64 offset, max_sz, sz, real_sz = 0; | 352 | u64 offset, max_sz, sz, real_sz = 0; |
| 269 | if (phdr_ptr->p_type != PT_NOTE) | 353 | if (phdr_ptr->p_type != PT_NOTE) |
| 270 | continue; | 354 | continue; |
| 271 | nr_ptnote++; | ||
| 272 | max_sz = phdr_ptr->p_memsz; | 355 | max_sz = phdr_ptr->p_memsz; |
| 273 | offset = phdr_ptr->p_offset; | 356 | offset = phdr_ptr->p_offset; |
| 274 | notes_section = kmalloc(max_sz, GFP_KERNEL); | 357 | notes_section = kmalloc(max_sz, GFP_KERNEL); |
| @@ -280,7 +363,7 @@ static int __init merge_note_headers_elf64(char *elfptr, size_t *elfsz, | |||
| 280 | return rc; | 363 | return rc; |
| 281 | } | 364 | } |
| 282 | nhdr_ptr = notes_section; | 365 | nhdr_ptr = notes_section; |
| 283 | for (j = 0; j < max_sz; j += sz) { | 366 | while (real_sz < max_sz) { |
| 284 | if (nhdr_ptr->n_namesz == 0) | 367 | if (nhdr_ptr->n_namesz == 0) |
| 285 | break; | 368 | break; |
| 286 | sz = sizeof(Elf64_Nhdr) + | 369 | sz = sizeof(Elf64_Nhdr) + |
| @@ -289,26 +372,122 @@ static int __init merge_note_headers_elf64(char *elfptr, size_t *elfsz, | |||
| 289 | real_sz += sz; | 372 | real_sz += sz; |
| 290 | nhdr_ptr = (Elf64_Nhdr*)((char*)nhdr_ptr + sz); | 373 | nhdr_ptr = (Elf64_Nhdr*)((char*)nhdr_ptr + sz); |
| 291 | } | 374 | } |
| 292 | |||
| 293 | /* Add this contiguous chunk of notes section to vmcore list.*/ | ||
| 294 | new = get_new_element(); | ||
| 295 | if (!new) { | ||
| 296 | kfree(notes_section); | ||
| 297 | return -ENOMEM; | ||
| 298 | } | ||
| 299 | new->paddr = phdr_ptr->p_offset; | ||
| 300 | new->size = real_sz; | ||
| 301 | list_add_tail(&new->list, vc_list); | ||
| 302 | phdr_sz += real_sz; | ||
| 303 | kfree(notes_section); | 375 | kfree(notes_section); |
| 376 | phdr_ptr->p_memsz = real_sz; | ||
| 377 | } | ||
| 378 | |||
| 379 | return 0; | ||
| 380 | } | ||
| 381 | |||
| 382 | /** | ||
| 383 | * get_note_number_and_size_elf64 - get the number of PT_NOTE program | ||
| 384 | * headers and sum of real size of their ELF note segment headers and | ||
| 385 | * data. | ||
| 386 | * | ||
| 387 | * @ehdr_ptr: ELF header | ||
| 388 | * @nr_ptnote: buffer for the number of PT_NOTE program headers | ||
| 389 | * @sz_ptnote: buffer for size of unique PT_NOTE program header | ||
| 390 | * | ||
| 391 | * This function is used to merge multiple PT_NOTE program headers | ||
| 392 | * into a unique single one. The resulting unique entry will have | ||
| 393 | * @sz_ptnote in its phdr->p_mem. | ||
| 394 | * | ||
| 395 | * It is assumed that program headers with PT_NOTE type pointed to by | ||
| 396 | * @ehdr_ptr has already been updated by update_note_header_size_elf64 | ||
| 397 | * and each of PT_NOTE program headers has actual ELF note segment | ||
| 398 | * size in its p_memsz member. | ||
| 399 | */ | ||
| 400 | static int __init get_note_number_and_size_elf64(const Elf64_Ehdr *ehdr_ptr, | ||
| 401 | int *nr_ptnote, u64 *sz_ptnote) | ||
| 402 | { | ||
| 403 | int i; | ||
| 404 | Elf64_Phdr *phdr_ptr; | ||
| 405 | |||
| 406 | *nr_ptnote = *sz_ptnote = 0; | ||
| 407 | |||
| 408 | phdr_ptr = (Elf64_Phdr *)(ehdr_ptr + 1); | ||
| 409 | for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) { | ||
| 410 | if (phdr_ptr->p_type != PT_NOTE) | ||
| 411 | continue; | ||
| 412 | *nr_ptnote += 1; | ||
| 413 | *sz_ptnote += phdr_ptr->p_memsz; | ||
| 414 | } | ||
| 415 | |||
| 416 | return 0; | ||
| 417 | } | ||
| 418 | |||
| 419 | /** | ||
| 420 | * copy_notes_elf64 - copy ELF note segments in a given buffer | ||
| 421 | * | ||
| 422 | * @ehdr_ptr: ELF header | ||
| 423 | * @notes_buf: buffer into which ELF note segments are copied | ||
| 424 | * | ||
| 425 | * This function is used to copy ELF note segment in the 1st kernel | ||
| 426 | * into the buffer @notes_buf in the 2nd kernel. It is assumed that | ||
| 427 | * size of the buffer @notes_buf is equal to or larger than sum of the | ||
| 428 | * real ELF note segment headers and data. | ||
| 429 | * | ||
| 430 | * It is assumed that program headers with PT_NOTE type pointed to by | ||
| 431 | * @ehdr_ptr has already been updated by update_note_header_size_elf64 | ||
| 432 | * and each of PT_NOTE program headers has actual ELF note segment | ||
| 433 | * size in its p_memsz member. | ||
| 434 | */ | ||
| 435 | static int __init copy_notes_elf64(const Elf64_Ehdr *ehdr_ptr, char *notes_buf) | ||
| 436 | { | ||
| 437 | int i, rc=0; | ||
| 438 | Elf64_Phdr *phdr_ptr; | ||
| 439 | |||
| 440 | phdr_ptr = (Elf64_Phdr*)(ehdr_ptr + 1); | ||
| 441 | |||
| 442 | for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) { | ||
| 443 | u64 offset; | ||
| 444 | if (phdr_ptr->p_type != PT_NOTE) | ||
| 445 | continue; | ||
| 446 | offset = phdr_ptr->p_offset; | ||
| 447 | rc = read_from_oldmem(notes_buf, phdr_ptr->p_memsz, &offset, 0); | ||
| 448 | if (rc < 0) | ||
| 449 | return rc; | ||
| 450 | notes_buf += phdr_ptr->p_memsz; | ||
| 304 | } | 451 | } |
| 305 | 452 | ||
| 453 | return 0; | ||
| 454 | } | ||
| 455 | |||
| 456 | /* Merges all the PT_NOTE headers into one. */ | ||
| 457 | static int __init merge_note_headers_elf64(char *elfptr, size_t *elfsz, | ||
| 458 | char **notes_buf, size_t *notes_sz) | ||
| 459 | { | ||
| 460 | int i, nr_ptnote=0, rc=0; | ||
| 461 | char *tmp; | ||
| 462 | Elf64_Ehdr *ehdr_ptr; | ||
| 463 | Elf64_Phdr phdr; | ||
| 464 | u64 phdr_sz = 0, note_off; | ||
| 465 | |||
| 466 | ehdr_ptr = (Elf64_Ehdr *)elfptr; | ||
| 467 | |||
| 468 | rc = update_note_header_size_elf64(ehdr_ptr); | ||
| 469 | if (rc < 0) | ||
| 470 | return rc; | ||
| 471 | |||
| 472 | rc = get_note_number_and_size_elf64(ehdr_ptr, &nr_ptnote, &phdr_sz); | ||
| 473 | if (rc < 0) | ||
| 474 | return rc; | ||
| 475 | |||
| 476 | *notes_sz = roundup(phdr_sz, PAGE_SIZE); | ||
| 477 | *notes_buf = alloc_elfnotes_buf(*notes_sz); | ||
| 478 | if (!*notes_buf) | ||
| 479 | return -ENOMEM; | ||
| 480 | |||
| 481 | rc = copy_notes_elf64(ehdr_ptr, *notes_buf); | ||
| 482 | if (rc < 0) | ||
| 483 | return rc; | ||
| 484 | |||
| 306 | /* Prepare merged PT_NOTE program header. */ | 485 | /* Prepare merged PT_NOTE program header. */ |
| 307 | phdr.p_type = PT_NOTE; | 486 | phdr.p_type = PT_NOTE; |
| 308 | phdr.p_flags = 0; | 487 | phdr.p_flags = 0; |
| 309 | note_off = sizeof(Elf64_Ehdr) + | 488 | note_off = sizeof(Elf64_Ehdr) + |
| 310 | (ehdr_ptr->e_phnum - nr_ptnote +1) * sizeof(Elf64_Phdr); | 489 | (ehdr_ptr->e_phnum - nr_ptnote +1) * sizeof(Elf64_Phdr); |
| 311 | phdr.p_offset = note_off; | 490 | phdr.p_offset = roundup(note_off, PAGE_SIZE); |
| 312 | phdr.p_vaddr = phdr.p_paddr = 0; | 491 | phdr.p_vaddr = phdr.p_paddr = 0; |
| 313 | phdr.p_filesz = phdr.p_memsz = phdr_sz; | 492 | phdr.p_filesz = phdr.p_memsz = phdr_sz; |
| 314 | phdr.p_align = 0; | 493 | phdr.p_align = 0; |
| @@ -322,6 +501,8 @@ static int __init merge_note_headers_elf64(char *elfptr, size_t *elfsz, | |||
| 322 | i = (nr_ptnote - 1) * sizeof(Elf64_Phdr); | 501 | i = (nr_ptnote - 1) * sizeof(Elf64_Phdr); |
| 323 | *elfsz = *elfsz - i; | 502 | *elfsz = *elfsz - i; |
| 324 | memmove(tmp, tmp+i, ((*elfsz)-sizeof(Elf64_Ehdr)-sizeof(Elf64_Phdr))); | 503 | memmove(tmp, tmp+i, ((*elfsz)-sizeof(Elf64_Ehdr)-sizeof(Elf64_Phdr))); |
| 504 | memset(elfptr + *elfsz, 0, i); | ||
| 505 | *elfsz = roundup(*elfsz, PAGE_SIZE); | ||
| 325 | 506 | ||
| 326 | /* Modify e_phnum to reflect merged headers. */ | 507 | /* Modify e_phnum to reflect merged headers. */ |
| 327 | ehdr_ptr->e_phnum = ehdr_ptr->e_phnum - nr_ptnote + 1; | 508 | ehdr_ptr->e_phnum = ehdr_ptr->e_phnum - nr_ptnote + 1; |
| @@ -329,27 +510,27 @@ static int __init merge_note_headers_elf64(char *elfptr, size_t *elfsz, | |||
| 329 | return 0; | 510 | return 0; |
| 330 | } | 511 | } |
| 331 | 512 | ||
| 332 | /* Merges all the PT_NOTE headers into one. */ | 513 | /** |
| 333 | static int __init merge_note_headers_elf32(char *elfptr, size_t *elfsz, | 514 | * update_note_header_size_elf32 - update p_memsz member of each PT_NOTE entry |
| 334 | struct list_head *vc_list) | 515 | * |
| 516 | * @ehdr_ptr: ELF header | ||
| 517 | * | ||
| 518 | * This function updates p_memsz member of each PT_NOTE entry in the | ||
| 519 | * program header table pointed to by @ehdr_ptr to real size of ELF | ||
| 520 | * note segment. | ||
| 521 | */ | ||
| 522 | static int __init update_note_header_size_elf32(const Elf32_Ehdr *ehdr_ptr) | ||
| 335 | { | 523 | { |
| 336 | int i, nr_ptnote=0, rc=0; | 524 | int i, rc=0; |
| 337 | char *tmp; | 525 | Elf32_Phdr *phdr_ptr; |
| 338 | Elf32_Ehdr *ehdr_ptr; | ||
| 339 | Elf32_Phdr phdr, *phdr_ptr; | ||
| 340 | Elf32_Nhdr *nhdr_ptr; | 526 | Elf32_Nhdr *nhdr_ptr; |
| 341 | u64 phdr_sz = 0, note_off; | ||
| 342 | 527 | ||
| 343 | ehdr_ptr = (Elf32_Ehdr *)elfptr; | 528 | phdr_ptr = (Elf32_Phdr *)(ehdr_ptr + 1); |
| 344 | phdr_ptr = (Elf32_Phdr*)(elfptr + sizeof(Elf32_Ehdr)); | ||
| 345 | for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) { | 529 | for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) { |
| 346 | int j; | ||
| 347 | void *notes_section; | 530 | void *notes_section; |
| 348 | struct vmcore *new; | ||
| 349 | u64 offset, max_sz, sz, real_sz = 0; | 531 | u64 offset, max_sz, sz, real_sz = 0; |
| 350 | if (phdr_ptr->p_type != PT_NOTE) | 532 | if (phdr_ptr->p_type != PT_NOTE) |
| 351 | continue; | 533 | continue; |
| 352 | nr_ptnote++; | ||
| 353 | max_sz = phdr_ptr->p_memsz; | 534 | max_sz = phdr_ptr->p_memsz; |
| 354 | offset = phdr_ptr->p_offset; | 535 | offset = phdr_ptr->p_offset; |
| 355 | notes_section = kmalloc(max_sz, GFP_KERNEL); | 536 | notes_section = kmalloc(max_sz, GFP_KERNEL); |
| @@ -361,7 +542,7 @@ static int __init merge_note_headers_elf32(char *elfptr, size_t *elfsz, | |||
| 361 | return rc; | 542 | return rc; |
| 362 | } | 543 | } |
| 363 | nhdr_ptr = notes_section; | 544 | nhdr_ptr = notes_section; |
| 364 | for (j = 0; j < max_sz; j += sz) { | 545 | while (real_sz < max_sz) { |
| 365 | if (nhdr_ptr->n_namesz == 0) | 546 | if (nhdr_ptr->n_namesz == 0) |
| 366 | break; | 547 | break; |
| 367 | sz = sizeof(Elf32_Nhdr) + | 548 | sz = sizeof(Elf32_Nhdr) + |
| @@ -370,26 +551,122 @@ static int __init merge_note_headers_elf32(char *elfptr, size_t *elfsz, | |||
| 370 | real_sz += sz; | 551 | real_sz += sz; |
| 371 | nhdr_ptr = (Elf32_Nhdr*)((char*)nhdr_ptr + sz); | 552 | nhdr_ptr = (Elf32_Nhdr*)((char*)nhdr_ptr + sz); |
| 372 | } | 553 | } |
| 373 | |||
| 374 | /* Add this contiguous chunk of notes section to vmcore list.*/ | ||
| 375 | new = get_new_element(); | ||
| 376 | if (!new) { | ||
| 377 | kfree(notes_section); | ||
| 378 | return -ENOMEM; | ||
| 379 | } | ||
| 380 | new->paddr = phdr_ptr->p_offset; | ||
| 381 | new->size = real_sz; | ||
| 382 | list_add_tail(&new->list, vc_list); | ||
| 383 | phdr_sz += real_sz; | ||
| 384 | kfree(notes_section); | 554 | kfree(notes_section); |
| 555 | phdr_ptr->p_memsz = real_sz; | ||
| 556 | } | ||
| 557 | |||
| 558 | return 0; | ||
| 559 | } | ||
| 560 | |||
| 561 | /** | ||
| 562 | * get_note_number_and_size_elf32 - get the number of PT_NOTE program | ||
| 563 | * headers and sum of real size of their ELF note segment headers and | ||
| 564 | * data. | ||
| 565 | * | ||
| 566 | * @ehdr_ptr: ELF header | ||
| 567 | * @nr_ptnote: buffer for the number of PT_NOTE program headers | ||
| 568 | * @sz_ptnote: buffer for size of unique PT_NOTE program header | ||
| 569 | * | ||
| 570 | * This function is used to merge multiple PT_NOTE program headers | ||
| 571 | * into a unique single one. The resulting unique entry will have | ||
| 572 | * @sz_ptnote in its phdr->p_mem. | ||
| 573 | * | ||
| 574 | * It is assumed that program headers with PT_NOTE type pointed to by | ||
| 575 | * @ehdr_ptr has already been updated by update_note_header_size_elf32 | ||
| 576 | * and each of PT_NOTE program headers has actual ELF note segment | ||
| 577 | * size in its p_memsz member. | ||
| 578 | */ | ||
| 579 | static int __init get_note_number_and_size_elf32(const Elf32_Ehdr *ehdr_ptr, | ||
| 580 | int *nr_ptnote, u64 *sz_ptnote) | ||
| 581 | { | ||
| 582 | int i; | ||
| 583 | Elf32_Phdr *phdr_ptr; | ||
| 584 | |||
| 585 | *nr_ptnote = *sz_ptnote = 0; | ||
| 586 | |||
| 587 | phdr_ptr = (Elf32_Phdr *)(ehdr_ptr + 1); | ||
| 588 | for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) { | ||
| 589 | if (phdr_ptr->p_type != PT_NOTE) | ||
| 590 | continue; | ||
| 591 | *nr_ptnote += 1; | ||
| 592 | *sz_ptnote += phdr_ptr->p_memsz; | ||
| 593 | } | ||
| 594 | |||
| 595 | return 0; | ||
| 596 | } | ||
| 597 | |||
| 598 | /** | ||
| 599 | * copy_notes_elf32 - copy ELF note segments in a given buffer | ||
| 600 | * | ||
| 601 | * @ehdr_ptr: ELF header | ||
| 602 | * @notes_buf: buffer into which ELF note segments are copied | ||
| 603 | * | ||
| 604 | * This function is used to copy ELF note segment in the 1st kernel | ||
| 605 | * into the buffer @notes_buf in the 2nd kernel. It is assumed that | ||
| 606 | * size of the buffer @notes_buf is equal to or larger than sum of the | ||
| 607 | * real ELF note segment headers and data. | ||
| 608 | * | ||
| 609 | * It is assumed that program headers with PT_NOTE type pointed to by | ||
| 610 | * @ehdr_ptr has already been updated by update_note_header_size_elf32 | ||
| 611 | * and each of PT_NOTE program headers has actual ELF note segment | ||
| 612 | * size in its p_memsz member. | ||
| 613 | */ | ||
| 614 | static int __init copy_notes_elf32(const Elf32_Ehdr *ehdr_ptr, char *notes_buf) | ||
| 615 | { | ||
| 616 | int i, rc=0; | ||
| 617 | Elf32_Phdr *phdr_ptr; | ||
| 618 | |||
| 619 | phdr_ptr = (Elf32_Phdr*)(ehdr_ptr + 1); | ||
| 620 | |||
| 621 | for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) { | ||
| 622 | u64 offset; | ||
| 623 | if (phdr_ptr->p_type != PT_NOTE) | ||
| 624 | continue; | ||
| 625 | offset = phdr_ptr->p_offset; | ||
| 626 | rc = read_from_oldmem(notes_buf, phdr_ptr->p_memsz, &offset, 0); | ||
| 627 | if (rc < 0) | ||
| 628 | return rc; | ||
| 629 | notes_buf += phdr_ptr->p_memsz; | ||
| 385 | } | 630 | } |
| 386 | 631 | ||
| 632 | return 0; | ||
| 633 | } | ||
| 634 | |||
| 635 | /* Merges all the PT_NOTE headers into one. */ | ||
| 636 | static int __init merge_note_headers_elf32(char *elfptr, size_t *elfsz, | ||
| 637 | char **notes_buf, size_t *notes_sz) | ||
| 638 | { | ||
| 639 | int i, nr_ptnote=0, rc=0; | ||
| 640 | char *tmp; | ||
| 641 | Elf32_Ehdr *ehdr_ptr; | ||
| 642 | Elf32_Phdr phdr; | ||
| 643 | u64 phdr_sz = 0, note_off; | ||
| 644 | |||
| 645 | ehdr_ptr = (Elf32_Ehdr *)elfptr; | ||
| 646 | |||
| 647 | rc = update_note_header_size_elf32(ehdr_ptr); | ||
| 648 | if (rc < 0) | ||
| 649 | return rc; | ||
| 650 | |||
| 651 | rc = get_note_number_and_size_elf32(ehdr_ptr, &nr_ptnote, &phdr_sz); | ||
| 652 | if (rc < 0) | ||
| 653 | return rc; | ||
| 654 | |||
| 655 | *notes_sz = roundup(phdr_sz, PAGE_SIZE); | ||
| 656 | *notes_buf = alloc_elfnotes_buf(*notes_sz); | ||
| 657 | if (!*notes_buf) | ||
| 658 | return -ENOMEM; | ||
| 659 | |||
| 660 | rc = copy_notes_elf32(ehdr_ptr, *notes_buf); | ||
| 661 | if (rc < 0) | ||
| 662 | return rc; | ||
| 663 | |||
| 387 | /* Prepare merged PT_NOTE program header. */ | 664 | /* Prepare merged PT_NOTE program header. */ |
| 388 | phdr.p_type = PT_NOTE; | 665 | phdr.p_type = PT_NOTE; |
| 389 | phdr.p_flags = 0; | 666 | phdr.p_flags = 0; |
| 390 | note_off = sizeof(Elf32_Ehdr) + | 667 | note_off = sizeof(Elf32_Ehdr) + |
| 391 | (ehdr_ptr->e_phnum - nr_ptnote +1) * sizeof(Elf32_Phdr); | 668 | (ehdr_ptr->e_phnum - nr_ptnote +1) * sizeof(Elf32_Phdr); |
| 392 | phdr.p_offset = note_off; | 669 | phdr.p_offset = roundup(note_off, PAGE_SIZE); |
| 393 | phdr.p_vaddr = phdr.p_paddr = 0; | 670 | phdr.p_vaddr = phdr.p_paddr = 0; |
| 394 | phdr.p_filesz = phdr.p_memsz = phdr_sz; | 671 | phdr.p_filesz = phdr.p_memsz = phdr_sz; |
| 395 | phdr.p_align = 0; | 672 | phdr.p_align = 0; |
| @@ -403,6 +680,8 @@ static int __init merge_note_headers_elf32(char *elfptr, size_t *elfsz, | |||
| 403 | i = (nr_ptnote - 1) * sizeof(Elf32_Phdr); | 680 | i = (nr_ptnote - 1) * sizeof(Elf32_Phdr); |
| 404 | *elfsz = *elfsz - i; | 681 | *elfsz = *elfsz - i; |
| 405 | memmove(tmp, tmp+i, ((*elfsz)-sizeof(Elf32_Ehdr)-sizeof(Elf32_Phdr))); | 682 | memmove(tmp, tmp+i, ((*elfsz)-sizeof(Elf32_Ehdr)-sizeof(Elf32_Phdr))); |
| 683 | memset(elfptr + *elfsz, 0, i); | ||
| 684 | *elfsz = roundup(*elfsz, PAGE_SIZE); | ||
| 406 | 685 | ||
| 407 | /* Modify e_phnum to reflect merged headers. */ | 686 | /* Modify e_phnum to reflect merged headers. */ |
| 408 | ehdr_ptr->e_phnum = ehdr_ptr->e_phnum - nr_ptnote + 1; | 687 | ehdr_ptr->e_phnum = ehdr_ptr->e_phnum - nr_ptnote + 1; |
| @@ -414,6 +693,7 @@ static int __init merge_note_headers_elf32(char *elfptr, size_t *elfsz, | |||
| 414 | * the new offset fields of exported program headers. */ | 693 | * the new offset fields of exported program headers. */ |
| 415 | static int __init process_ptload_program_headers_elf64(char *elfptr, | 694 | static int __init process_ptload_program_headers_elf64(char *elfptr, |
| 416 | size_t elfsz, | 695 | size_t elfsz, |
| 696 | size_t elfnotes_sz, | ||
| 417 | struct list_head *vc_list) | 697 | struct list_head *vc_list) |
| 418 | { | 698 | { |
| 419 | int i; | 699 | int i; |
| @@ -425,32 +705,38 @@ static int __init process_ptload_program_headers_elf64(char *elfptr, | |||
| 425 | ehdr_ptr = (Elf64_Ehdr *)elfptr; | 705 | ehdr_ptr = (Elf64_Ehdr *)elfptr; |
| 426 | phdr_ptr = (Elf64_Phdr*)(elfptr + sizeof(Elf64_Ehdr)); /* PT_NOTE hdr */ | 706 | phdr_ptr = (Elf64_Phdr*)(elfptr + sizeof(Elf64_Ehdr)); /* PT_NOTE hdr */ |
| 427 | 707 | ||
| 428 | /* First program header is PT_NOTE header. */ | 708 | /* Skip Elf header, program headers and Elf note segment. */ |
| 429 | vmcore_off = sizeof(Elf64_Ehdr) + | 709 | vmcore_off = elfsz + elfnotes_sz; |
| 430 | (ehdr_ptr->e_phnum) * sizeof(Elf64_Phdr) + | ||
| 431 | phdr_ptr->p_memsz; /* Note sections */ | ||
| 432 | 710 | ||
| 433 | for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) { | 711 | for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) { |
| 712 | u64 paddr, start, end, size; | ||
| 713 | |||
| 434 | if (phdr_ptr->p_type != PT_LOAD) | 714 | if (phdr_ptr->p_type != PT_LOAD) |
| 435 | continue; | 715 | continue; |
| 436 | 716 | ||
| 717 | paddr = phdr_ptr->p_offset; | ||
| 718 | start = rounddown(paddr, PAGE_SIZE); | ||
| 719 | end = roundup(paddr + phdr_ptr->p_memsz, PAGE_SIZE); | ||
| 720 | size = end - start; | ||
| 721 | |||
| 437 | /* Add this contiguous chunk of memory to vmcore list.*/ | 722 | /* Add this contiguous chunk of memory to vmcore list.*/ |
| 438 | new = get_new_element(); | 723 | new = get_new_element(); |
| 439 | if (!new) | 724 | if (!new) |
| 440 | return -ENOMEM; | 725 | return -ENOMEM; |
| 441 | new->paddr = phdr_ptr->p_offset; | 726 | new->paddr = start; |
| 442 | new->size = phdr_ptr->p_memsz; | 727 | new->size = size; |
| 443 | list_add_tail(&new->list, vc_list); | 728 | list_add_tail(&new->list, vc_list); |
| 444 | 729 | ||
| 445 | /* Update the program header offset. */ | 730 | /* Update the program header offset. */ |
| 446 | phdr_ptr->p_offset = vmcore_off; | 731 | phdr_ptr->p_offset = vmcore_off + (paddr - start); |
| 447 | vmcore_off = vmcore_off + phdr_ptr->p_memsz; | 732 | vmcore_off = vmcore_off + size; |
| 448 | } | 733 | } |
| 449 | return 0; | 734 | return 0; |
| 450 | } | 735 | } |
| 451 | 736 | ||
| 452 | static int __init process_ptload_program_headers_elf32(char *elfptr, | 737 | static int __init process_ptload_program_headers_elf32(char *elfptr, |
| 453 | size_t elfsz, | 738 | size_t elfsz, |
| 739 | size_t elfnotes_sz, | ||
| 454 | struct list_head *vc_list) | 740 | struct list_head *vc_list) |
| 455 | { | 741 | { |
| 456 | int i; | 742 | int i; |
| @@ -462,43 +748,44 @@ static int __init process_ptload_program_headers_elf32(char *elfptr, | |||
| 462 | ehdr_ptr = (Elf32_Ehdr *)elfptr; | 748 | ehdr_ptr = (Elf32_Ehdr *)elfptr; |
| 463 | phdr_ptr = (Elf32_Phdr*)(elfptr + sizeof(Elf32_Ehdr)); /* PT_NOTE hdr */ | 749 | phdr_ptr = (Elf32_Phdr*)(elfptr + sizeof(Elf32_Ehdr)); /* PT_NOTE hdr */ |
| 464 | 750 | ||
| 465 | /* First program header is PT_NOTE header. */ | 751 | /* Skip Elf header, program headers and Elf note segment. */ |
| 466 | vmcore_off = sizeof(Elf32_Ehdr) + | 752 | vmcore_off = elfsz + elfnotes_sz; |
| 467 | (ehdr_ptr->e_phnum) * sizeof(Elf32_Phdr) + | ||
| 468 | phdr_ptr->p_memsz; /* Note sections */ | ||
| 469 | 753 | ||
| 470 | for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) { | 754 | for (i = 0; i < ehdr_ptr->e_phnum; i++, phdr_ptr++) { |
| 755 | u64 paddr, start, end, size; | ||
| 756 | |||
| 471 | if (phdr_ptr->p_type != PT_LOAD) | 757 | if (phdr_ptr->p_type != PT_LOAD) |
| 472 | continue; | 758 | continue; |
| 473 | 759 | ||
| 760 | paddr = phdr_ptr->p_offset; | ||
| 761 | start = rounddown(paddr, PAGE_SIZE); | ||
| 762 | end = roundup(paddr + phdr_ptr->p_memsz, PAGE_SIZE); | ||
| 763 | size = end - start; | ||
| 764 | |||
| 474 | /* Add this contiguous chunk of memory to vmcore list.*/ | 765 | /* Add this contiguous chunk of memory to vmcore list.*/ |
| 475 | new = get_new_element(); | 766 | new = get_new_element(); |
| 476 | if (!new) | 767 | if (!new) |
| 477 | return -ENOMEM; | 768 | return -ENOMEM; |
| 478 | new->paddr = phdr_ptr->p_offset; | 769 | new->paddr = start; |
| 479 | new->size = phdr_ptr->p_memsz; | 770 | new->size = size; |
| 480 | list_add_tail(&new->list, vc_list); | 771 | list_add_tail(&new->list, vc_list); |
| 481 | 772 | ||
| 482 | /* Update the program header offset */ | 773 | /* Update the program header offset */ |
| 483 | phdr_ptr->p_offset = vmcore_off; | 774 | phdr_ptr->p_offset = vmcore_off + (paddr - start); |
| 484 | vmcore_off = vmcore_off + phdr_ptr->p_memsz; | 775 | vmcore_off = vmcore_off + size; |
| 485 | } | 776 | } |
| 486 | return 0; | 777 | return 0; |
| 487 | } | 778 | } |
| 488 | 779 | ||
| 489 | /* Sets offset fields of vmcore elements. */ | 780 | /* Sets offset fields of vmcore elements. */ |
| 490 | static void __init set_vmcore_list_offsets_elf64(char *elfptr, | 781 | static void __init set_vmcore_list_offsets(size_t elfsz, size_t elfnotes_sz, |
| 491 | struct list_head *vc_list) | 782 | struct list_head *vc_list) |
| 492 | { | 783 | { |
| 493 | loff_t vmcore_off; | 784 | loff_t vmcore_off; |
| 494 | Elf64_Ehdr *ehdr_ptr; | ||
| 495 | struct vmcore *m; | 785 | struct vmcore *m; |
| 496 | 786 | ||
| 497 | ehdr_ptr = (Elf64_Ehdr *)elfptr; | 787 | /* Skip Elf header, program headers and Elf note segment. */ |
| 498 | 788 | vmcore_off = elfsz + elfnotes_sz; | |
| 499 | /* Skip Elf header and program headers. */ | ||
| 500 | vmcore_off = sizeof(Elf64_Ehdr) + | ||
| 501 | (ehdr_ptr->e_phnum) * sizeof(Elf64_Phdr); | ||
| 502 | 789 | ||
| 503 | list_for_each_entry(m, vc_list, list) { | 790 | list_for_each_entry(m, vc_list, list) { |
| 504 | m->offset = vmcore_off; | 791 | m->offset = vmcore_off; |
| @@ -506,24 +793,12 @@ static void __init set_vmcore_list_offsets_elf64(char *elfptr, | |||
| 506 | } | 793 | } |
| 507 | } | 794 | } |
| 508 | 795 | ||
| 509 | /* Sets offset fields of vmcore elements. */ | 796 | static void free_elfcorebuf(void) |
| 510 | static void __init set_vmcore_list_offsets_elf32(char *elfptr, | ||
| 511 | struct list_head *vc_list) | ||
| 512 | { | 797 | { |
| 513 | loff_t vmcore_off; | 798 | free_pages((unsigned long)elfcorebuf, get_order(elfcorebuf_sz_orig)); |
| 514 | Elf32_Ehdr *ehdr_ptr; | 799 | elfcorebuf = NULL; |
| 515 | struct vmcore *m; | 800 | vfree(elfnotes_buf); |
| 516 | 801 | elfnotes_buf = NULL; | |
| 517 | ehdr_ptr = (Elf32_Ehdr *)elfptr; | ||
| 518 | |||
| 519 | /* Skip Elf header and program headers. */ | ||
| 520 | vmcore_off = sizeof(Elf32_Ehdr) + | ||
| 521 | (ehdr_ptr->e_phnum) * sizeof(Elf32_Phdr); | ||
| 522 | |||
| 523 | list_for_each_entry(m, vc_list, list) { | ||
| 524 | m->offset = vmcore_off; | ||
| 525 | vmcore_off += m->size; | ||
| 526 | } | ||
| 527 | } | 802 | } |
| 528 | 803 | ||
| 529 | static int __init parse_crash_elf64_headers(void) | 804 | static int __init parse_crash_elf64_headers(void) |
| @@ -554,31 +829,32 @@ static int __init parse_crash_elf64_headers(void) | |||
| 554 | } | 829 | } |
| 555 | 830 | ||
| 556 | /* Read in all elf headers. */ | 831 | /* Read in all elf headers. */ |
| 557 | elfcorebuf_sz = sizeof(Elf64_Ehdr) + ehdr.e_phnum * sizeof(Elf64_Phdr); | 832 | elfcorebuf_sz_orig = sizeof(Elf64_Ehdr) + |
| 558 | elfcorebuf = kmalloc(elfcorebuf_sz, GFP_KERNEL); | 833 | ehdr.e_phnum * sizeof(Elf64_Phdr); |
| 834 | elfcorebuf_sz = elfcorebuf_sz_orig; | ||
| 835 | elfcorebuf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, | ||
| 836 | get_order(elfcorebuf_sz_orig)); | ||
| 559 | if (!elfcorebuf) | 837 | if (!elfcorebuf) |
| 560 | return -ENOMEM; | 838 | return -ENOMEM; |
| 561 | addr = elfcorehdr_addr; | 839 | addr = elfcorehdr_addr; |
| 562 | rc = read_from_oldmem(elfcorebuf, elfcorebuf_sz, &addr, 0); | 840 | rc = read_from_oldmem(elfcorebuf, elfcorebuf_sz_orig, &addr, 0); |
| 563 | if (rc < 0) { | 841 | if (rc < 0) |
| 564 | kfree(elfcorebuf); | 842 | goto fail; |
| 565 | return rc; | ||
| 566 | } | ||
| 567 | 843 | ||
| 568 | /* Merge all PT_NOTE headers into one. */ | 844 | /* Merge all PT_NOTE headers into one. */ |
| 569 | rc = merge_note_headers_elf64(elfcorebuf, &elfcorebuf_sz, &vmcore_list); | 845 | rc = merge_note_headers_elf64(elfcorebuf, &elfcorebuf_sz, |
| 570 | if (rc) { | 846 | &elfnotes_buf, &elfnotes_sz); |
| 571 | kfree(elfcorebuf); | 847 | if (rc) |
| 572 | return rc; | 848 | goto fail; |
| 573 | } | ||
| 574 | rc = process_ptload_program_headers_elf64(elfcorebuf, elfcorebuf_sz, | 849 | rc = process_ptload_program_headers_elf64(elfcorebuf, elfcorebuf_sz, |
| 575 | &vmcore_list); | 850 | elfnotes_sz, &vmcore_list); |
| 576 | if (rc) { | 851 | if (rc) |
| 577 | kfree(elfcorebuf); | 852 | goto fail; |
| 578 | return rc; | 853 | set_vmcore_list_offsets(elfcorebuf_sz, elfnotes_sz, &vmcore_list); |
| 579 | } | ||
| 580 | set_vmcore_list_offsets_elf64(elfcorebuf, &vmcore_list); | ||
| 581 | return 0; | 854 | return 0; |
| 855 | fail: | ||
| 856 | free_elfcorebuf(); | ||
| 857 | return rc; | ||
| 582 | } | 858 | } |
| 583 | 859 | ||
| 584 | static int __init parse_crash_elf32_headers(void) | 860 | static int __init parse_crash_elf32_headers(void) |
| @@ -609,31 +885,31 @@ static int __init parse_crash_elf32_headers(void) | |||
| 609 | } | 885 | } |
| 610 | 886 | ||
| 611 | /* Read in all elf headers. */ | 887 | /* Read in all elf headers. */ |
| 612 | elfcorebuf_sz = sizeof(Elf32_Ehdr) + ehdr.e_phnum * sizeof(Elf32_Phdr); | 888 | elfcorebuf_sz_orig = sizeof(Elf32_Ehdr) + ehdr.e_phnum * sizeof(Elf32_Phdr); |
| 613 | elfcorebuf = kmalloc(elfcorebuf_sz, GFP_KERNEL); | 889 | elfcorebuf_sz = elfcorebuf_sz_orig; |
| 890 | elfcorebuf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, | ||
| 891 | get_order(elfcorebuf_sz_orig)); | ||
| 614 | if (!elfcorebuf) | 892 | if (!elfcorebuf) |
| 615 | return -ENOMEM; | 893 | return -ENOMEM; |
| 616 | addr = elfcorehdr_addr; | 894 | addr = elfcorehdr_addr; |
| 617 | rc = read_from_oldmem(elfcorebuf, elfcorebuf_sz, &addr, 0); | 895 | rc = read_from_oldmem(elfcorebuf, elfcorebuf_sz_orig, &addr, 0); |
| 618 | if (rc < 0) { | 896 | if (rc < 0) |
| 619 | kfree(elfcorebuf); | 897 | goto fail; |
| 620 | return rc; | ||
| 621 | } | ||
| 622 | 898 | ||
| 623 | /* Merge all PT_NOTE headers into one. */ | 899 | /* Merge all PT_NOTE headers into one. */ |
| 624 | rc = merge_note_headers_elf32(elfcorebuf, &elfcorebuf_sz, &vmcore_list); | 900 | rc = merge_note_headers_elf32(elfcorebuf, &elfcorebuf_sz, |
| 625 | if (rc) { | 901 | &elfnotes_buf, &elfnotes_sz); |
| 626 | kfree(elfcorebuf); | 902 | if (rc) |
| 627 | return rc; | 903 | goto fail; |
| 628 | } | ||
| 629 | rc = process_ptload_program_headers_elf32(elfcorebuf, elfcorebuf_sz, | 904 | rc = process_ptload_program_headers_elf32(elfcorebuf, elfcorebuf_sz, |
| 630 | &vmcore_list); | 905 | elfnotes_sz, &vmcore_list); |
| 631 | if (rc) { | 906 | if (rc) |
| 632 | kfree(elfcorebuf); | 907 | goto fail; |
| 633 | return rc; | 908 | set_vmcore_list_offsets(elfcorebuf_sz, elfnotes_sz, &vmcore_list); |
| 634 | } | ||
| 635 | set_vmcore_list_offsets_elf32(elfcorebuf, &vmcore_list); | ||
| 636 | return 0; | 909 | return 0; |
| 910 | fail: | ||
| 911 | free_elfcorebuf(); | ||
| 912 | return rc; | ||
| 637 | } | 913 | } |
| 638 | 914 | ||
| 639 | static int __init parse_crash_elf_headers(void) | 915 | static int __init parse_crash_elf_headers(void) |
| @@ -655,20 +931,19 @@ static int __init parse_crash_elf_headers(void) | |||
| 655 | rc = parse_crash_elf64_headers(); | 931 | rc = parse_crash_elf64_headers(); |
| 656 | if (rc) | 932 | if (rc) |
| 657 | return rc; | 933 | return rc; |
| 658 | |||
| 659 | /* Determine vmcore size. */ | ||
| 660 | vmcore_size = get_vmcore_size_elf64(elfcorebuf); | ||
| 661 | } else if (e_ident[EI_CLASS] == ELFCLASS32) { | 934 | } else if (e_ident[EI_CLASS] == ELFCLASS32) { |
| 662 | rc = parse_crash_elf32_headers(); | 935 | rc = parse_crash_elf32_headers(); |
| 663 | if (rc) | 936 | if (rc) |
| 664 | return rc; | 937 | return rc; |
| 665 | |||
| 666 | /* Determine vmcore size. */ | ||
| 667 | vmcore_size = get_vmcore_size_elf32(elfcorebuf); | ||
| 668 | } else { | 938 | } else { |
| 669 | pr_warn("Warning: Core image elf header is not sane\n"); | 939 | pr_warn("Warning: Core image elf header is not sane\n"); |
| 670 | return -EINVAL; | 940 | return -EINVAL; |
| 671 | } | 941 | } |
| 942 | |||
| 943 | /* Determine vmcore size. */ | ||
| 944 | vmcore_size = get_vmcore_size(elfcorebuf_sz, elfnotes_sz, | ||
| 945 | &vmcore_list); | ||
| 946 | |||
| 672 | return 0; | 947 | return 0; |
| 673 | } | 948 | } |
| 674 | 949 | ||
| @@ -711,7 +986,6 @@ void vmcore_cleanup(void) | |||
| 711 | list_del(&m->list); | 986 | list_del(&m->list); |
| 712 | kfree(m); | 987 | kfree(m); |
| 713 | } | 988 | } |
| 714 | kfree(elfcorebuf); | 989 | free_elfcorebuf(); |
| 715 | elfcorebuf = NULL; | ||
| 716 | } | 990 | } |
| 717 | EXPORT_SYMBOL_GPL(vmcore_cleanup); | 991 | EXPORT_SYMBOL_GPL(vmcore_cleanup); |
