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