Merge branches 'work.misc' and 'work.dcache' of git://git.kernel.org/pub/scm/linux...
[sfrench/cifs-2.6.git] / arch / s390 / kernel / crash_dump.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * S390 kdump implementation
4  *
5  * Copyright IBM Corp. 2011
6  * Author(s): Michael Holzheu <holzheu@linux.vnet.ibm.com>
7  */
8
9 #include <linux/crash_dump.h>
10 #include <asm/lowcore.h>
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/mm.h>
14 #include <linux/gfp.h>
15 #include <linux/slab.h>
16 #include <linux/bootmem.h>
17 #include <linux/elf.h>
18 #include <asm/asm-offsets.h>
19 #include <linux/memblock.h>
20 #include <asm/os_info.h>
21 #include <asm/elf.h>
22 #include <asm/ipl.h>
23 #include <asm/sclp.h>
24
25 #define PTR_ADD(x, y) (((char *) (x)) + ((unsigned long) (y)))
26 #define PTR_SUB(x, y) (((char *) (x)) - ((unsigned long) (y)))
27 #define PTR_DIFF(x, y) ((unsigned long)(((char *) (x)) - ((unsigned long) (y))))
28
29 static struct memblock_region oldmem_region;
30
31 static struct memblock_type oldmem_type = {
32         .cnt = 1,
33         .max = 1,
34         .total_size = 0,
35         .regions = &oldmem_region,
36         .name = "oldmem",
37 };
38
39 struct save_area {
40         struct list_head list;
41         u64 psw[2];
42         u64 ctrs[16];
43         u64 gprs[16];
44         u32 acrs[16];
45         u64 fprs[16];
46         u32 fpc;
47         u32 prefix;
48         u64 todpreg;
49         u64 timer;
50         u64 todcmp;
51         u64 vxrs_low[16];
52         __vector128 vxrs_high[16];
53 };
54
55 static LIST_HEAD(dump_save_areas);
56
57 /*
58  * Allocate a save area
59  */
60 struct save_area * __init save_area_alloc(bool is_boot_cpu)
61 {
62         struct save_area *sa;
63
64         sa = (void *) memblock_alloc(sizeof(*sa), 8);
65         if (is_boot_cpu)
66                 list_add(&sa->list, &dump_save_areas);
67         else
68                 list_add_tail(&sa->list, &dump_save_areas);
69         return sa;
70 }
71
72 /*
73  * Return the address of the save area for the boot CPU
74  */
75 struct save_area * __init save_area_boot_cpu(void)
76 {
77         return list_first_entry_or_null(&dump_save_areas, struct save_area, list);
78 }
79
80 /*
81  * Copy CPU registers into the save area
82  */
83 void __init save_area_add_regs(struct save_area *sa, void *regs)
84 {
85         struct lowcore *lc;
86
87         lc = (struct lowcore *)(regs - __LC_FPREGS_SAVE_AREA);
88         memcpy(&sa->psw, &lc->psw_save_area, sizeof(sa->psw));
89         memcpy(&sa->ctrs, &lc->cregs_save_area, sizeof(sa->ctrs));
90         memcpy(&sa->gprs, &lc->gpregs_save_area, sizeof(sa->gprs));
91         memcpy(&sa->acrs, &lc->access_regs_save_area, sizeof(sa->acrs));
92         memcpy(&sa->fprs, &lc->floating_pt_save_area, sizeof(sa->fprs));
93         memcpy(&sa->fpc, &lc->fpt_creg_save_area, sizeof(sa->fpc));
94         memcpy(&sa->prefix, &lc->prefixreg_save_area, sizeof(sa->prefix));
95         memcpy(&sa->todpreg, &lc->tod_progreg_save_area, sizeof(sa->todpreg));
96         memcpy(&sa->timer, &lc->cpu_timer_save_area, sizeof(sa->timer));
97         memcpy(&sa->todcmp, &lc->clock_comp_save_area, sizeof(sa->todcmp));
98 }
99
100 /*
101  * Copy vector registers into the save area
102  */
103 void __init save_area_add_vxrs(struct save_area *sa, __vector128 *vxrs)
104 {
105         int i;
106
107         /* Copy lower halves of vector registers 0-15 */
108         for (i = 0; i < 16; i++)
109                 memcpy(&sa->vxrs_low[i], &vxrs[i].u[2], 8);
110         /* Copy vector registers 16-31 */
111         memcpy(sa->vxrs_high, vxrs + 16, 16 * sizeof(__vector128));
112 }
113
114 /*
115  * Return physical address for virtual address
116  */
117 static inline void *load_real_addr(void *addr)
118 {
119         unsigned long real_addr;
120
121         asm volatile(
122                    "    lra     %0,0(%1)\n"
123                    "    jz      0f\n"
124                    "    la      %0,0\n"
125                    "0:"
126                    : "=a" (real_addr) : "a" (addr) : "cc");
127         return (void *)real_addr;
128 }
129
130 /*
131  * Copy memory of the old, dumped system to a kernel space virtual address
132  */
133 int copy_oldmem_kernel(void *dst, void *src, size_t count)
134 {
135         unsigned long from, len;
136         void *ra;
137         int rc;
138
139         while (count) {
140                 from = __pa(src);
141                 if (!OLDMEM_BASE && from < sclp.hsa_size) {
142                         /* Copy from zfcpdump HSA area */
143                         len = min(count, sclp.hsa_size - from);
144                         rc = memcpy_hsa_kernel(dst, from, len);
145                         if (rc)
146                                 return rc;
147                 } else {
148                         /* Check for swapped kdump oldmem areas */
149                         if (OLDMEM_BASE && from - OLDMEM_BASE < OLDMEM_SIZE) {
150                                 from -= OLDMEM_BASE;
151                                 len = min(count, OLDMEM_SIZE - from);
152                         } else if (OLDMEM_BASE && from < OLDMEM_SIZE) {
153                                 len = min(count, OLDMEM_SIZE - from);
154                                 from += OLDMEM_BASE;
155                         } else {
156                                 len = count;
157                         }
158                         if (is_vmalloc_or_module_addr(dst)) {
159                                 ra = load_real_addr(dst);
160                                 len = min(PAGE_SIZE - offset_in_page(ra), len);
161                         } else {
162                                 ra = dst;
163                         }
164                         if (memcpy_real(ra, (void *) from, len))
165                                 return -EFAULT;
166                 }
167                 dst += len;
168                 src += len;
169                 count -= len;
170         }
171         return 0;
172 }
173
174 /*
175  * Copy memory of the old, dumped system to a user space virtual address
176  */
177 static int copy_oldmem_user(void __user *dst, void *src, size_t count)
178 {
179         unsigned long from, len;
180         int rc;
181
182         while (count) {
183                 from = __pa(src);
184                 if (!OLDMEM_BASE && from < sclp.hsa_size) {
185                         /* Copy from zfcpdump HSA area */
186                         len = min(count, sclp.hsa_size - from);
187                         rc = memcpy_hsa_user(dst, from, len);
188                         if (rc)
189                                 return rc;
190                 } else {
191                         /* Check for swapped kdump oldmem areas */
192                         if (OLDMEM_BASE && from - OLDMEM_BASE < OLDMEM_SIZE) {
193                                 from -= OLDMEM_BASE;
194                                 len = min(count, OLDMEM_SIZE - from);
195                         } else if (OLDMEM_BASE && from < OLDMEM_SIZE) {
196                                 len = min(count, OLDMEM_SIZE - from);
197                                 from += OLDMEM_BASE;
198                         } else {
199                                 len = count;
200                         }
201                         rc = copy_to_user_real(dst, (void *) from, count);
202                         if (rc)
203                                 return rc;
204                 }
205                 dst += len;
206                 src += len;
207                 count -= len;
208         }
209         return 0;
210 }
211
212 /*
213  * Copy one page from "oldmem"
214  */
215 ssize_t copy_oldmem_page(unsigned long pfn, char *buf, size_t csize,
216                          unsigned long offset, int userbuf)
217 {
218         void *src;
219         int rc;
220
221         if (!csize)
222                 return 0;
223         src = (void *) (pfn << PAGE_SHIFT) + offset;
224         if (userbuf)
225                 rc = copy_oldmem_user((void __force __user *) buf, src, csize);
226         else
227                 rc = copy_oldmem_kernel((void *) buf, src, csize);
228         return rc;
229 }
230
231 /*
232  * Remap "oldmem" for kdump
233  *
234  * For the kdump reserved memory this functions performs a swap operation:
235  * [0 - OLDMEM_SIZE] is mapped to [OLDMEM_BASE - OLDMEM_BASE + OLDMEM_SIZE]
236  */
237 static int remap_oldmem_pfn_range_kdump(struct vm_area_struct *vma,
238                                         unsigned long from, unsigned long pfn,
239                                         unsigned long size, pgprot_t prot)
240 {
241         unsigned long size_old;
242         int rc;
243
244         if (pfn < OLDMEM_SIZE >> PAGE_SHIFT) {
245                 size_old = min(size, OLDMEM_SIZE - (pfn << PAGE_SHIFT));
246                 rc = remap_pfn_range(vma, from,
247                                      pfn + (OLDMEM_BASE >> PAGE_SHIFT),
248                                      size_old, prot);
249                 if (rc || size == size_old)
250                         return rc;
251                 size -= size_old;
252                 from += size_old;
253                 pfn += size_old >> PAGE_SHIFT;
254         }
255         return remap_pfn_range(vma, from, pfn, size, prot);
256 }
257
258 /*
259  * Remap "oldmem" for zfcpdump
260  *
261  * We only map available memory above HSA size. Memory below HSA size
262  * is read on demand using the copy_oldmem_page() function.
263  */
264 static int remap_oldmem_pfn_range_zfcpdump(struct vm_area_struct *vma,
265                                            unsigned long from,
266                                            unsigned long pfn,
267                                            unsigned long size, pgprot_t prot)
268 {
269         unsigned long hsa_end = sclp.hsa_size;
270         unsigned long size_hsa;
271
272         if (pfn < hsa_end >> PAGE_SHIFT) {
273                 size_hsa = min(size, hsa_end - (pfn << PAGE_SHIFT));
274                 if (size == size_hsa)
275                         return 0;
276                 size -= size_hsa;
277                 from += size_hsa;
278                 pfn += size_hsa >> PAGE_SHIFT;
279         }
280         return remap_pfn_range(vma, from, pfn, size, prot);
281 }
282
283 /*
284  * Remap "oldmem" for kdump or zfcpdump
285  */
286 int remap_oldmem_pfn_range(struct vm_area_struct *vma, unsigned long from,
287                            unsigned long pfn, unsigned long size, pgprot_t prot)
288 {
289         if (OLDMEM_BASE)
290                 return remap_oldmem_pfn_range_kdump(vma, from, pfn, size, prot);
291         else
292                 return remap_oldmem_pfn_range_zfcpdump(vma, from, pfn, size,
293                                                        prot);
294 }
295
296 /*
297  * Alloc memory and panic in case of ENOMEM
298  */
299 static void *kzalloc_panic(int len)
300 {
301         void *rc;
302
303         rc = kzalloc(len, GFP_KERNEL);
304         if (!rc)
305                 panic("s390 kdump kzalloc (%d) failed", len);
306         return rc;
307 }
308
309 static const char *nt_name(Elf64_Word type)
310 {
311         const char *name = "LINUX";
312
313         if (type == NT_PRPSINFO || type == NT_PRSTATUS || type == NT_PRFPREG)
314                 name = KEXEC_CORE_NOTE_NAME;
315         return name;
316 }
317
318 /*
319  * Initialize ELF note
320  */
321 static void *nt_init_name(void *buf, Elf64_Word type, void *desc, int d_len,
322                           const char *name)
323 {
324         Elf64_Nhdr *note;
325         u64 len;
326
327         note = (Elf64_Nhdr *)buf;
328         note->n_namesz = strlen(name) + 1;
329         note->n_descsz = d_len;
330         note->n_type = type;
331         len = sizeof(Elf64_Nhdr);
332
333         memcpy(buf + len, name, note->n_namesz);
334         len = roundup(len + note->n_namesz, 4);
335
336         memcpy(buf + len, desc, note->n_descsz);
337         len = roundup(len + note->n_descsz, 4);
338
339         return PTR_ADD(buf, len);
340 }
341
342 static inline void *nt_init(void *buf, Elf64_Word type, void *desc, int d_len)
343 {
344         return nt_init_name(buf, type, desc, d_len, nt_name(type));
345 }
346
347 /*
348  * Calculate the size of ELF note
349  */
350 static size_t nt_size_name(int d_len, const char *name)
351 {
352         size_t size;
353
354         size = sizeof(Elf64_Nhdr);
355         size += roundup(strlen(name) + 1, 4);
356         size += roundup(d_len, 4);
357
358         return size;
359 }
360
361 static inline size_t nt_size(Elf64_Word type, int d_len)
362 {
363         return nt_size_name(d_len, nt_name(type));
364 }
365
366 /*
367  * Fill ELF notes for one CPU with save area registers
368  */
369 static void *fill_cpu_elf_notes(void *ptr, int cpu, struct save_area *sa)
370 {
371         struct elf_prstatus nt_prstatus;
372         elf_fpregset_t nt_fpregset;
373
374         /* Prepare prstatus note */
375         memset(&nt_prstatus, 0, sizeof(nt_prstatus));
376         memcpy(&nt_prstatus.pr_reg.gprs, sa->gprs, sizeof(sa->gprs));
377         memcpy(&nt_prstatus.pr_reg.psw, sa->psw, sizeof(sa->psw));
378         memcpy(&nt_prstatus.pr_reg.acrs, sa->acrs, sizeof(sa->acrs));
379         nt_prstatus.pr_pid = cpu;
380         /* Prepare fpregset (floating point) note */
381         memset(&nt_fpregset, 0, sizeof(nt_fpregset));
382         memcpy(&nt_fpregset.fpc, &sa->fpc, sizeof(sa->fpc));
383         memcpy(&nt_fpregset.fprs, &sa->fprs, sizeof(sa->fprs));
384         /* Create ELF notes for the CPU */
385         ptr = nt_init(ptr, NT_PRSTATUS, &nt_prstatus, sizeof(nt_prstatus));
386         ptr = nt_init(ptr, NT_PRFPREG, &nt_fpregset, sizeof(nt_fpregset));
387         ptr = nt_init(ptr, NT_S390_TIMER, &sa->timer, sizeof(sa->timer));
388         ptr = nt_init(ptr, NT_S390_TODCMP, &sa->todcmp, sizeof(sa->todcmp));
389         ptr = nt_init(ptr, NT_S390_TODPREG, &sa->todpreg, sizeof(sa->todpreg));
390         ptr = nt_init(ptr, NT_S390_CTRS, &sa->ctrs, sizeof(sa->ctrs));
391         ptr = nt_init(ptr, NT_S390_PREFIX, &sa->prefix, sizeof(sa->prefix));
392         if (MACHINE_HAS_VX) {
393                 ptr = nt_init(ptr, NT_S390_VXRS_HIGH,
394                               &sa->vxrs_high, sizeof(sa->vxrs_high));
395                 ptr = nt_init(ptr, NT_S390_VXRS_LOW,
396                               &sa->vxrs_low, sizeof(sa->vxrs_low));
397         }
398         return ptr;
399 }
400
401 /*
402  * Calculate size of ELF notes per cpu
403  */
404 static size_t get_cpu_elf_notes_size(void)
405 {
406         struct save_area *sa = NULL;
407         size_t size;
408
409         size =  nt_size(NT_PRSTATUS, sizeof(struct elf_prstatus));
410         size +=  nt_size(NT_PRFPREG, sizeof(elf_fpregset_t));
411         size +=  nt_size(NT_S390_TIMER, sizeof(sa->timer));
412         size +=  nt_size(NT_S390_TODCMP, sizeof(sa->todcmp));
413         size +=  nt_size(NT_S390_TODPREG, sizeof(sa->todpreg));
414         size +=  nt_size(NT_S390_CTRS, sizeof(sa->ctrs));
415         size +=  nt_size(NT_S390_PREFIX, sizeof(sa->prefix));
416         if (MACHINE_HAS_VX) {
417                 size += nt_size(NT_S390_VXRS_HIGH, sizeof(sa->vxrs_high));
418                 size += nt_size(NT_S390_VXRS_LOW, sizeof(sa->vxrs_low));
419         }
420
421         return size;
422 }
423
424 /*
425  * Initialize prpsinfo note (new kernel)
426  */
427 static void *nt_prpsinfo(void *ptr)
428 {
429         struct elf_prpsinfo prpsinfo;
430
431         memset(&prpsinfo, 0, sizeof(prpsinfo));
432         prpsinfo.pr_sname = 'R';
433         strcpy(prpsinfo.pr_fname, "vmlinux");
434         return nt_init(ptr, NT_PRPSINFO, &prpsinfo, sizeof(prpsinfo));
435 }
436
437 /*
438  * Get vmcoreinfo using lowcore->vmcore_info (new kernel)
439  */
440 static void *get_vmcoreinfo_old(unsigned long *size)
441 {
442         char nt_name[11], *vmcoreinfo;
443         Elf64_Nhdr note;
444         void *addr;
445
446         if (copy_oldmem_kernel(&addr, &S390_lowcore.vmcore_info, sizeof(addr)))
447                 return NULL;
448         memset(nt_name, 0, sizeof(nt_name));
449         if (copy_oldmem_kernel(&note, addr, sizeof(note)))
450                 return NULL;
451         if (copy_oldmem_kernel(nt_name, addr + sizeof(note),
452                                sizeof(nt_name) - 1))
453                 return NULL;
454         if (strcmp(nt_name, "VMCOREINFO") != 0)
455                 return NULL;
456         vmcoreinfo = kzalloc_panic(note.n_descsz);
457         if (copy_oldmem_kernel(vmcoreinfo, addr + 24, note.n_descsz))
458                 return NULL;
459         *size = note.n_descsz;
460         return vmcoreinfo;
461 }
462
463 /*
464  * Initialize vmcoreinfo note (new kernel)
465  */
466 static void *nt_vmcoreinfo(void *ptr)
467 {
468         unsigned long size;
469         void *vmcoreinfo;
470
471         vmcoreinfo = os_info_old_entry(OS_INFO_VMCOREINFO, &size);
472         if (!vmcoreinfo)
473                 vmcoreinfo = get_vmcoreinfo_old(&size);
474         if (!vmcoreinfo)
475                 return ptr;
476         return nt_init_name(ptr, 0, vmcoreinfo, size, "VMCOREINFO");
477 }
478
479 static size_t nt_vmcoreinfo_size(void)
480 {
481         const char *name = "VMCOREINFO";
482         char nt_name[11];
483         Elf64_Nhdr note;
484         void *addr;
485
486         if (copy_oldmem_kernel(&addr, &S390_lowcore.vmcore_info, sizeof(addr)))
487                 return 0;
488
489         if (copy_oldmem_kernel(&note, addr, sizeof(note)))
490                 return 0;
491
492         memset(nt_name, 0, sizeof(nt_name));
493         if (copy_oldmem_kernel(nt_name, addr + sizeof(note),
494                                sizeof(nt_name) - 1))
495                 return 0;
496
497         if (strcmp(nt_name, name) != 0)
498                 return 0;
499
500         return nt_size_name(note.n_descsz, name);
501 }
502
503 /*
504  * Initialize final note (needed for /proc/vmcore code)
505  */
506 static void *nt_final(void *ptr)
507 {
508         Elf64_Nhdr *note;
509
510         note = (Elf64_Nhdr *) ptr;
511         note->n_namesz = 0;
512         note->n_descsz = 0;
513         note->n_type = 0;
514         return PTR_ADD(ptr, sizeof(Elf64_Nhdr));
515 }
516
517 /*
518  * Initialize ELF header (new kernel)
519  */
520 static void *ehdr_init(Elf64_Ehdr *ehdr, int mem_chunk_cnt)
521 {
522         memset(ehdr, 0, sizeof(*ehdr));
523         memcpy(ehdr->e_ident, ELFMAG, SELFMAG);
524         ehdr->e_ident[EI_CLASS] = ELFCLASS64;
525         ehdr->e_ident[EI_DATA] = ELFDATA2MSB;
526         ehdr->e_ident[EI_VERSION] = EV_CURRENT;
527         memset(ehdr->e_ident + EI_PAD, 0, EI_NIDENT - EI_PAD);
528         ehdr->e_type = ET_CORE;
529         ehdr->e_machine = EM_S390;
530         ehdr->e_version = EV_CURRENT;
531         ehdr->e_phoff = sizeof(Elf64_Ehdr);
532         ehdr->e_ehsize = sizeof(Elf64_Ehdr);
533         ehdr->e_phentsize = sizeof(Elf64_Phdr);
534         ehdr->e_phnum = mem_chunk_cnt + 1;
535         return ehdr + 1;
536 }
537
538 /*
539  * Return CPU count for ELF header (new kernel)
540  */
541 static int get_cpu_cnt(void)
542 {
543         struct save_area *sa;
544         int cpus = 0;
545
546         list_for_each_entry(sa, &dump_save_areas, list)
547                 if (sa->prefix != 0)
548                         cpus++;
549         return cpus;
550 }
551
552 /*
553  * Return memory chunk count for ELF header (new kernel)
554  */
555 static int get_mem_chunk_cnt(void)
556 {
557         int cnt = 0;
558         u64 idx;
559
560         for_each_mem_range(idx, &memblock.physmem, &oldmem_type, NUMA_NO_NODE,
561                            MEMBLOCK_NONE, NULL, NULL, NULL)
562                 cnt++;
563         return cnt;
564 }
565
566 /*
567  * Initialize ELF loads (new kernel)
568  */
569 static void loads_init(Elf64_Phdr *phdr, u64 loads_offset)
570 {
571         phys_addr_t start, end;
572         u64 idx;
573
574         for_each_mem_range(idx, &memblock.physmem, &oldmem_type, NUMA_NO_NODE,
575                            MEMBLOCK_NONE, &start, &end, NULL) {
576                 phdr->p_filesz = end - start;
577                 phdr->p_type = PT_LOAD;
578                 phdr->p_offset = start;
579                 phdr->p_vaddr = start;
580                 phdr->p_paddr = start;
581                 phdr->p_memsz = end - start;
582                 phdr->p_flags = PF_R | PF_W | PF_X;
583                 phdr->p_align = PAGE_SIZE;
584                 phdr++;
585         }
586 }
587
588 /*
589  * Initialize notes (new kernel)
590  */
591 static void *notes_init(Elf64_Phdr *phdr, void *ptr, u64 notes_offset)
592 {
593         struct save_area *sa;
594         void *ptr_start = ptr;
595         int cpu;
596
597         ptr = nt_prpsinfo(ptr);
598
599         cpu = 1;
600         list_for_each_entry(sa, &dump_save_areas, list)
601                 if (sa->prefix != 0)
602                         ptr = fill_cpu_elf_notes(ptr, cpu++, sa);
603         ptr = nt_vmcoreinfo(ptr);
604         ptr = nt_final(ptr);
605         memset(phdr, 0, sizeof(*phdr));
606         phdr->p_type = PT_NOTE;
607         phdr->p_offset = notes_offset;
608         phdr->p_filesz = (unsigned long) PTR_SUB(ptr, ptr_start);
609         phdr->p_memsz = phdr->p_filesz;
610         return ptr;
611 }
612
613 static size_t get_elfcorehdr_size(int mem_chunk_cnt)
614 {
615         size_t size;
616
617         size = sizeof(Elf64_Ehdr);
618         /* PT_NOTES */
619         size += sizeof(Elf64_Phdr);
620         /* nt_prpsinfo */
621         size += nt_size(NT_PRPSINFO, sizeof(struct elf_prpsinfo));
622         /* regsets */
623         size += get_cpu_cnt() * get_cpu_elf_notes_size();
624         /* nt_vmcoreinfo */
625         size += nt_vmcoreinfo_size();
626         /* nt_final */
627         size += sizeof(Elf64_Nhdr);
628         /* PT_LOADS */
629         size += mem_chunk_cnt * sizeof(Elf64_Phdr);
630
631         return size;
632 }
633
634 /*
635  * Create ELF core header (new kernel)
636  */
637 int elfcorehdr_alloc(unsigned long long *addr, unsigned long long *size)
638 {
639         Elf64_Phdr *phdr_notes, *phdr_loads;
640         int mem_chunk_cnt;
641         void *ptr, *hdr;
642         u32 alloc_size;
643         u64 hdr_off;
644
645         /* If we are not in kdump or zfcpdump mode return */
646         if (!OLDMEM_BASE && ipl_info.type != IPL_TYPE_FCP_DUMP)
647                 return 0;
648         /* If we cannot get HSA size for zfcpdump return error */
649         if (ipl_info.type == IPL_TYPE_FCP_DUMP && !sclp.hsa_size)
650                 return -ENODEV;
651
652         /* For kdump, exclude previous crashkernel memory */
653         if (OLDMEM_BASE) {
654                 oldmem_region.base = OLDMEM_BASE;
655                 oldmem_region.size = OLDMEM_SIZE;
656                 oldmem_type.total_size = OLDMEM_SIZE;
657         }
658
659         mem_chunk_cnt = get_mem_chunk_cnt();
660
661         alloc_size = get_elfcorehdr_size(mem_chunk_cnt);
662
663         hdr = kzalloc_panic(alloc_size);
664         /* Init elf header */
665         ptr = ehdr_init(hdr, mem_chunk_cnt);
666         /* Init program headers */
667         phdr_notes = ptr;
668         ptr = PTR_ADD(ptr, sizeof(Elf64_Phdr));
669         phdr_loads = ptr;
670         ptr = PTR_ADD(ptr, sizeof(Elf64_Phdr) * mem_chunk_cnt);
671         /* Init notes */
672         hdr_off = PTR_DIFF(ptr, hdr);
673         ptr = notes_init(phdr_notes, ptr, ((unsigned long) hdr) + hdr_off);
674         /* Init loads */
675         hdr_off = PTR_DIFF(ptr, hdr);
676         loads_init(phdr_loads, hdr_off);
677         *addr = (unsigned long long) hdr;
678         *size = (unsigned long long) hdr_off;
679         BUG_ON(elfcorehdr_size > alloc_size);
680         return 0;
681 }
682
683 /*
684  * Free ELF core header (new kernel)
685  */
686 void elfcorehdr_free(unsigned long long addr)
687 {
688         kfree((void *)(unsigned long)addr);
689 }
690
691 /*
692  * Read from ELF header
693  */
694 ssize_t elfcorehdr_read(char *buf, size_t count, u64 *ppos)
695 {
696         void *src = (void *)(unsigned long)*ppos;
697
698         memcpy(buf, src, count);
699         *ppos += count;
700         return count;
701 }
702
703 /*
704  * Read from ELF notes data
705  */
706 ssize_t elfcorehdr_read_notes(char *buf, size_t count, u64 *ppos)
707 {
708         void *src = (void *)(unsigned long)*ppos;
709
710         memcpy(buf, src, count);
711         *ppos += count;
712         return count;
713 }