Linux Audio

Check our new training course

Loading...
v4.17
  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
 29static struct memblock_region oldmem_region;
 30
 31static struct memblock_type oldmem_type = {
 32	.cnt = 1,
 33	.max = 1,
 34	.total_size = 0,
 35	.regions = &oldmem_region,
 36	.name = "oldmem",
 37};
 38
 39struct 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
 55static LIST_HEAD(dump_save_areas);
 56
 57/*
 58 * Allocate a save area
 
 
 
 
 59 */
 60struct 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 */
 75struct 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 */
 83void __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 */
103void __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 */
117static 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 */
133int 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 */
177static 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 */
215ssize_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 */
237static 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 */
264static 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 */
286int 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 */
299static 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/*
310 * Initialize ELF note
311 */
312static void *nt_init_name(void *buf, Elf64_Word type, void *desc, int d_len,
313			  const char *name)
314{
315	Elf64_Nhdr *note;
316	u64 len;
317
318	note = (Elf64_Nhdr *)buf;
319	note->n_namesz = strlen(name) + 1;
320	note->n_descsz = d_len;
321	note->n_type = type;
322	len = sizeof(Elf64_Nhdr);
323
324	memcpy(buf + len, name, note->n_namesz);
325	len = roundup(len + note->n_namesz, 4);
326
327	memcpy(buf + len, desc, note->n_descsz);
328	len = roundup(len + note->n_descsz, 4);
329
330	return PTR_ADD(buf, len);
331}
332
333static inline void *nt_init(void *buf, Elf64_Word type, void *desc, int d_len)
334{
335	const char *note_name = "LINUX";
336
337	if (type == NT_PRPSINFO || type == NT_PRSTATUS || type == NT_PRFPREG)
338		note_name = KEXEC_CORE_NOTE_NAME;
339	return nt_init_name(buf, type, desc, d_len, note_name);
340}
341
342/*
343 * Fill ELF notes for one CPU with save area registers
344 */
345static void *fill_cpu_elf_notes(void *ptr, int cpu, struct save_area *sa)
346{
347	struct elf_prstatus nt_prstatus;
348	elf_fpregset_t nt_fpregset;
349
350	/* Prepare prstatus note */
351	memset(&nt_prstatus, 0, sizeof(nt_prstatus));
352	memcpy(&nt_prstatus.pr_reg.gprs, sa->gprs, sizeof(sa->gprs));
353	memcpy(&nt_prstatus.pr_reg.psw, sa->psw, sizeof(sa->psw));
354	memcpy(&nt_prstatus.pr_reg.acrs, sa->acrs, sizeof(sa->acrs));
355	nt_prstatus.pr_pid = cpu;
356	/* Prepare fpregset (floating point) note */
357	memset(&nt_fpregset, 0, sizeof(nt_fpregset));
358	memcpy(&nt_fpregset.fpc, &sa->fpc, sizeof(sa->fpc));
359	memcpy(&nt_fpregset.fprs, &sa->fprs, sizeof(sa->fprs));
360	/* Create ELF notes for the CPU */
361	ptr = nt_init(ptr, NT_PRSTATUS, &nt_prstatus, sizeof(nt_prstatus));
362	ptr = nt_init(ptr, NT_PRFPREG, &nt_fpregset, sizeof(nt_fpregset));
363	ptr = nt_init(ptr, NT_S390_TIMER, &sa->timer, sizeof(sa->timer));
364	ptr = nt_init(ptr, NT_S390_TODCMP, &sa->todcmp, sizeof(sa->todcmp));
365	ptr = nt_init(ptr, NT_S390_TODPREG, &sa->todpreg, sizeof(sa->todpreg));
366	ptr = nt_init(ptr, NT_S390_CTRS, &sa->ctrs, sizeof(sa->ctrs));
367	ptr = nt_init(ptr, NT_S390_PREFIX, &sa->prefix, sizeof(sa->prefix));
368	if (MACHINE_HAS_VX) {
369		ptr = nt_init(ptr, NT_S390_VXRS_HIGH,
370			      &sa->vxrs_high, sizeof(sa->vxrs_high));
371		ptr = nt_init(ptr, NT_S390_VXRS_LOW,
372			      &sa->vxrs_low, sizeof(sa->vxrs_low));
373	}
374	return ptr;
375}
376
377/*
378 * Initialize prpsinfo note (new kernel)
379 */
380static void *nt_prpsinfo(void *ptr)
381{
382	struct elf_prpsinfo prpsinfo;
383
384	memset(&prpsinfo, 0, sizeof(prpsinfo));
385	prpsinfo.pr_sname = 'R';
386	strcpy(prpsinfo.pr_fname, "vmlinux");
387	return nt_init(ptr, NT_PRPSINFO, &prpsinfo, sizeof(prpsinfo));
 
388}
389
390/*
391 * Get vmcoreinfo using lowcore->vmcore_info (new kernel)
392 */
393static void *get_vmcoreinfo_old(unsigned long *size)
394{
395	char nt_name[11], *vmcoreinfo;
396	Elf64_Nhdr note;
397	void *addr;
398
399	if (copy_oldmem_kernel(&addr, &S390_lowcore.vmcore_info, sizeof(addr)))
400		return NULL;
401	memset(nt_name, 0, sizeof(nt_name));
402	if (copy_oldmem_kernel(&note, addr, sizeof(note)))
403		return NULL;
404	if (copy_oldmem_kernel(nt_name, addr + sizeof(note),
405			       sizeof(nt_name) - 1))
406		return NULL;
407	if (strcmp(nt_name, "VMCOREINFO") != 0)
408		return NULL;
409	vmcoreinfo = kzalloc_panic(note.n_descsz);
410	if (copy_oldmem_kernel(vmcoreinfo, addr + 24, note.n_descsz))
411		return NULL;
412	*size = note.n_descsz;
413	return vmcoreinfo;
414}
415
416/*
417 * Initialize vmcoreinfo note (new kernel)
418 */
419static void *nt_vmcoreinfo(void *ptr)
420{
421	unsigned long size;
422	void *vmcoreinfo;
423
424	vmcoreinfo = os_info_old_entry(OS_INFO_VMCOREINFO, &size);
425	if (!vmcoreinfo)
426		vmcoreinfo = get_vmcoreinfo_old(&size);
427	if (!vmcoreinfo)
428		return ptr;
429	return nt_init_name(ptr, 0, vmcoreinfo, size, "VMCOREINFO");
430}
431
432/*
433 * Initialize final note (needed for /proc/vmcore code)
434 */
435static void *nt_final(void *ptr)
436{
437	Elf64_Nhdr *note;
438
439	note = (Elf64_Nhdr *) ptr;
440	note->n_namesz = 0;
441	note->n_descsz = 0;
442	note->n_type = 0;
443	return PTR_ADD(ptr, sizeof(Elf64_Nhdr));
444}
445
446/*
447 * Initialize ELF header (new kernel)
448 */
449static void *ehdr_init(Elf64_Ehdr *ehdr, int mem_chunk_cnt)
450{
451	memset(ehdr, 0, sizeof(*ehdr));
452	memcpy(ehdr->e_ident, ELFMAG, SELFMAG);
453	ehdr->e_ident[EI_CLASS] = ELFCLASS64;
454	ehdr->e_ident[EI_DATA] = ELFDATA2MSB;
455	ehdr->e_ident[EI_VERSION] = EV_CURRENT;
456	memset(ehdr->e_ident + EI_PAD, 0, EI_NIDENT - EI_PAD);
457	ehdr->e_type = ET_CORE;
458	ehdr->e_machine = EM_S390;
459	ehdr->e_version = EV_CURRENT;
460	ehdr->e_phoff = sizeof(Elf64_Ehdr);
461	ehdr->e_ehsize = sizeof(Elf64_Ehdr);
462	ehdr->e_phentsize = sizeof(Elf64_Phdr);
463	ehdr->e_phnum = mem_chunk_cnt + 1;
464	return ehdr + 1;
465}
466
467/*
468 * Return CPU count for ELF header (new kernel)
469 */
470static int get_cpu_cnt(void)
471{
472	struct save_area *sa;
473	int cpus = 0;
474
475	list_for_each_entry(sa, &dump_save_areas, list)
476		if (sa->prefix != 0)
477			cpus++;
 
 
478	return cpus;
479}
480
481/*
482 * Return memory chunk count for ELF header (new kernel)
483 */
484static int get_mem_chunk_cnt(void)
485{
486	int cnt = 0;
487	u64 idx;
488
489	for_each_mem_range(idx, &memblock.physmem, &oldmem_type, NUMA_NO_NODE,
490			   MEMBLOCK_NONE, NULL, NULL, NULL)
 
 
 
 
 
 
491		cnt++;
 
 
492	return cnt;
493}
494
495/*
 
 
 
 
 
 
 
 
496 * Initialize ELF loads (new kernel)
497 */
498static void loads_init(Elf64_Phdr *phdr, u64 loads_offset)
499{
500	phys_addr_t start, end;
501	u64 idx;
502
503	for_each_mem_range(idx, &memblock.physmem, &oldmem_type, NUMA_NO_NODE,
504			   MEMBLOCK_NONE, &start, &end, NULL) {
505		phdr->p_filesz = end - start;
 
 
 
 
 
 
 
506		phdr->p_type = PT_LOAD;
507		phdr->p_offset = start;
508		phdr->p_vaddr = start;
509		phdr->p_paddr = start;
510		phdr->p_memsz = end - start;
511		phdr->p_flags = PF_R | PF_W | PF_X;
512		phdr->p_align = PAGE_SIZE;
513		phdr++;
514	}
 
 
515}
516
517/*
518 * Initialize notes (new kernel)
519 */
520static void *notes_init(Elf64_Phdr *phdr, void *ptr, u64 notes_offset)
521{
522	struct save_area *sa;
523	void *ptr_start = ptr;
524	int cpu;
525
526	ptr = nt_prpsinfo(ptr);
527
528	cpu = 1;
529	list_for_each_entry(sa, &dump_save_areas, list)
530		if (sa->prefix != 0)
531			ptr = fill_cpu_elf_notes(ptr, cpu++, sa);
 
 
532	ptr = nt_vmcoreinfo(ptr);
533	ptr = nt_final(ptr);
534	memset(phdr, 0, sizeof(*phdr));
535	phdr->p_type = PT_NOTE;
536	phdr->p_offset = notes_offset;
537	phdr->p_filesz = (unsigned long) PTR_SUB(ptr, ptr_start);
538	phdr->p_memsz = phdr->p_filesz;
539	return ptr;
540}
541
542/*
543 * Create ELF core header (new kernel)
544 */
545int elfcorehdr_alloc(unsigned long long *addr, unsigned long long *size)
546{
547	Elf64_Phdr *phdr_notes, *phdr_loads;
548	int mem_chunk_cnt;
549	void *ptr, *hdr;
550	u32 alloc_size;
551	u64 hdr_off;
552
553	/* If we are not in kdump or zfcpdump mode return */
554	if (!OLDMEM_BASE && ipl_info.type != IPL_TYPE_FCP_DUMP)
555		return 0;
556	/* If we cannot get HSA size for zfcpdump return error */
557	if (ipl_info.type == IPL_TYPE_FCP_DUMP && !sclp.hsa_size)
558		return -ENODEV;
559
560	/* For kdump, exclude previous crashkernel memory */
561	if (OLDMEM_BASE) {
562		oldmem_region.base = OLDMEM_BASE;
563		oldmem_region.size = OLDMEM_SIZE;
564		oldmem_type.total_size = OLDMEM_SIZE;
565	}
566
567	mem_chunk_cnt = get_mem_chunk_cnt();
568
569	alloc_size = 0x1000 + get_cpu_cnt() * 0x4a0 +
570		mem_chunk_cnt * sizeof(Elf64_Phdr);
571	hdr = kzalloc_panic(alloc_size);
572	/* Init elf header */
573	ptr = ehdr_init(hdr, mem_chunk_cnt);
574	/* Init program headers */
575	phdr_notes = ptr;
576	ptr = PTR_ADD(ptr, sizeof(Elf64_Phdr));
577	phdr_loads = ptr;
578	ptr = PTR_ADD(ptr, sizeof(Elf64_Phdr) * mem_chunk_cnt);
579	/* Init notes */
580	hdr_off = PTR_DIFF(ptr, hdr);
581	ptr = notes_init(phdr_notes, ptr, ((unsigned long) hdr) + hdr_off);
582	/* Init loads */
583	hdr_off = PTR_DIFF(ptr, hdr);
584	loads_init(phdr_loads, hdr_off);
585	*addr = (unsigned long long) hdr;
586	*size = (unsigned long long) hdr_off;
587	BUG_ON(elfcorehdr_size > alloc_size);
588	return 0;
589}
590
591/*
592 * Free ELF core header (new kernel)
 
593 */
594void elfcorehdr_free(unsigned long long addr)
595{
596	kfree((void *)(unsigned long)addr);
597}
598
599/*
600 * Read from ELF header
601 */
602ssize_t elfcorehdr_read(char *buf, size_t count, u64 *ppos)
603{
604	void *src = (void *)(unsigned long)*ppos;
605
606	memcpy(buf, src, count);
607	*ppos += count;
608	return count;
609}
610
611/*
612 * Read from ELF notes data
613 */
614ssize_t elfcorehdr_read_notes(char *buf, size_t count, u64 *ppos)
615{
616	void *src = (void *)(unsigned long)*ppos;
617
618	memcpy(buf, src, count);
619	*ppos += count;
620	return count;
621}
v3.5.6
 
  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/ipl.h>
 17#include <asm/os_info.h>
 18
 19#define PTR_ADD(x, y) (((char *) (x)) + ((unsigned long) (y)))
 20#define PTR_SUB(x, y) (((char *) (x)) - ((unsigned long) (y)))
 21#define PTR_DIFF(x, y) ((unsigned long)(((char *) (x)) - ((unsigned long) (y))))
 22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 23/*
 24 * Copy one page from "oldmem"
 25 *
 26 * For the kdump reserved memory this functions performs a swap operation:
 27 *  - [OLDMEM_BASE - OLDMEM_BASE + OLDMEM_SIZE] is mapped to [0 - OLDMEM_SIZE].
 28 *  - [0 - OLDMEM_SIZE] is mapped to [OLDMEM_BASE - OLDMEM_BASE + OLDMEM_SIZE]
 29 */
 30ssize_t copy_oldmem_page(unsigned long pfn, char *buf,
 31			 size_t csize, unsigned long offset, int userbuf)
 32{
 33	unsigned long src;
 34
 35	if (!csize)
 36		return 0;
 37
 38	src = (pfn << PAGE_SHIFT) + offset;
 39	if (src < OLDMEM_SIZE)
 40		src += OLDMEM_BASE;
 41	else if (src > OLDMEM_BASE &&
 42		 src < OLDMEM_BASE + OLDMEM_SIZE)
 43		src -= OLDMEM_BASE;
 44	if (userbuf)
 45		copy_to_user_real((void __force __user *) buf, (void *) src,
 46				  csize);
 47	else
 48		memcpy_real(buf, (void *) src, csize);
 49	return csize;
 50}
 51
 52/*
 53 * Copy memory from old kernel
 54 */
 55int copy_from_oldmem(void *dest, void *src, size_t count)
 56{
 57	unsigned long copied = 0;
 58	int rc;
 59
 60	if ((unsigned long) src < OLDMEM_SIZE) {
 61		copied = min(count, OLDMEM_SIZE - (unsigned long) src);
 62		rc = memcpy_real(dest, src + OLDMEM_BASE, copied);
 63		if (rc)
 64			return rc;
 65	}
 66	return memcpy_real(dest + copied, src + copied, count - copied);
 67}
 68
 69/*
 70 * Alloc memory and panic in case of ENOMEM
 71 */
 72static void *kzalloc_panic(int len)
 73{
 74	void *rc;
 75
 76	rc = kzalloc(len, GFP_KERNEL);
 77	if (!rc)
 78		panic("s390 kdump kzalloc (%d) failed", len);
 79	return rc;
 
 
 
 
 
 
 
 80}
 81
 82/*
 83 * Get memory layout and create hole for oldmem
 84 */
 85static struct mem_chunk *get_memory_layout(void)
 86{
 87	struct mem_chunk *chunk_array;
 88
 89	chunk_array = kzalloc_panic(MEMORY_CHUNKS * sizeof(struct mem_chunk));
 90	detect_memory_layout(chunk_array);
 91	create_mem_hole(chunk_array, OLDMEM_BASE, OLDMEM_SIZE, CHUNK_CRASHK);
 92	return chunk_array;
 
 93}
 94
 95/*
 96 * Initialize ELF note
 97 */
 98static void *nt_init(void *buf, Elf64_Word type, void *desc, int d_len,
 99		     const char *name)
100{
101	Elf64_Nhdr *note;
102	u64 len;
103
104	note = (Elf64_Nhdr *)buf;
105	note->n_namesz = strlen(name) + 1;
106	note->n_descsz = d_len;
107	note->n_type = type;
108	len = sizeof(Elf64_Nhdr);
 
 
 
109
110	memcpy(buf + len, name, note->n_namesz);
111	len = roundup(len + note->n_namesz, 4);
 
 
 
 
 
 
112
113	memcpy(buf + len, desc, note->n_descsz);
114	len = roundup(len + note->n_descsz, 4);
115
116	return PTR_ADD(buf, len);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117}
118
119/*
120 * Initialize prstatus note
121 */
122static void *nt_prstatus(void *ptr, struct save_area *sa)
123{
124	struct elf_prstatus nt_prstatus;
125	static int cpu_nr = 1;
126
127	memset(&nt_prstatus, 0, sizeof(nt_prstatus));
128	memcpy(&nt_prstatus.pr_reg.gprs, sa->gp_regs, sizeof(sa->gp_regs));
129	memcpy(&nt_prstatus.pr_reg.psw, sa->psw, sizeof(sa->psw));
130	memcpy(&nt_prstatus.pr_reg.acrs, sa->acc_regs, sizeof(sa->acc_regs));
131	nt_prstatus.pr_pid = cpu_nr;
132	cpu_nr++;
133
134	return nt_init(ptr, NT_PRSTATUS, &nt_prstatus, sizeof(nt_prstatus),
135			 "CORE");
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
136}
137
138/*
139 * Initialize fpregset (floating point) note
140 */
141static void *nt_fpregset(void *ptr, struct save_area *sa)
 
142{
143	elf_fpregset_t nt_fpregset;
 
144
145	memset(&nt_fpregset, 0, sizeof(nt_fpregset));
146	memcpy(&nt_fpregset.fpc, &sa->fp_ctrl_reg, sizeof(sa->fp_ctrl_reg));
147	memcpy(&nt_fpregset.fprs, &sa->fp_regs, sizeof(sa->fp_regs));
148
149	return nt_init(ptr, NT_PRFPREG, &nt_fpregset, sizeof(nt_fpregset),
150		       "CORE");
 
 
151}
152
153/*
154 * Initialize timer note
 
 
 
155 */
156static void *nt_s390_timer(void *ptr, struct save_area *sa)
 
 
157{
158	return nt_init(ptr, NT_S390_TIMER, &sa->timer, sizeof(sa->timer),
159			 KEXEC_CORE_NOTE_NAME);
 
 
 
 
 
 
 
 
 
 
 
 
 
160}
161
162/*
163 * Initialize TOD clock comparator note
 
 
 
164 */
165static void *nt_s390_tod_cmp(void *ptr, struct save_area *sa)
166{
167	return nt_init(ptr, NT_S390_TODCMP, &sa->clk_cmp,
168		       sizeof(sa->clk_cmp), KEXEC_CORE_NOTE_NAME);
 
 
 
 
 
 
 
 
 
 
 
 
 
169}
170
171/*
172 * Initialize TOD programmable register note
173 */
174static void *nt_s390_tod_preg(void *ptr, struct save_area *sa)
 
175{
176	return nt_init(ptr, NT_S390_TODPREG, &sa->tod_reg,
177		       sizeof(sa->tod_reg), KEXEC_CORE_NOTE_NAME);
 
 
 
178}
179
180/*
181 * Initialize control register note
182 */
183static void *nt_s390_ctrs(void *ptr, struct save_area *sa)
184{
185	return nt_init(ptr, NT_S390_CTRS, &sa->ctrl_regs,
186		       sizeof(sa->ctrl_regs), KEXEC_CORE_NOTE_NAME);
 
 
 
 
187}
188
189/*
190 * Initialize prefix register note
191 */
192static void *nt_s390_prefix(void *ptr, struct save_area *sa)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
193{
194	return nt_init(ptr, NT_S390_PREFIX, &sa->pref_reg,
195			 sizeof(sa->pref_reg), KEXEC_CORE_NOTE_NAME);
 
 
 
196}
197
198/*
199 * Fill ELF notes for one CPU with save area registers
200 */
201void *fill_cpu_elf_notes(void *ptr, struct save_area *sa)
202{
203	ptr = nt_prstatus(ptr, sa);
204	ptr = nt_fpregset(ptr, sa);
205	ptr = nt_s390_timer(ptr, sa);
206	ptr = nt_s390_tod_cmp(ptr, sa);
207	ptr = nt_s390_tod_preg(ptr, sa);
208	ptr = nt_s390_ctrs(ptr, sa);
209	ptr = nt_s390_prefix(ptr, sa);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
210	return ptr;
211}
212
213/*
214 * Initialize prpsinfo note (new kernel)
215 */
216static void *nt_prpsinfo(void *ptr)
217{
218	struct elf_prpsinfo prpsinfo;
219
220	memset(&prpsinfo, 0, sizeof(prpsinfo));
221	prpsinfo.pr_sname = 'R';
222	strcpy(prpsinfo.pr_fname, "vmlinux");
223	return nt_init(ptr, NT_PRPSINFO, &prpsinfo, sizeof(prpsinfo),
224		       KEXEC_CORE_NOTE_NAME);
225}
226
227/*
228 * Get vmcoreinfo using lowcore->vmcore_info (new kernel)
229 */
230static void *get_vmcoreinfo_old(unsigned long *size)
231{
232	char nt_name[11], *vmcoreinfo;
233	Elf64_Nhdr note;
234	void *addr;
235
236	if (copy_from_oldmem(&addr, &S390_lowcore.vmcore_info, sizeof(addr)))
237		return NULL;
238	memset(nt_name, 0, sizeof(nt_name));
239	if (copy_from_oldmem(&note, addr, sizeof(note)))
240		return NULL;
241	if (copy_from_oldmem(nt_name, addr + sizeof(note), sizeof(nt_name) - 1))
 
242		return NULL;
243	if (strcmp(nt_name, "VMCOREINFO") != 0)
244		return NULL;
245	vmcoreinfo = kzalloc_panic(note.n_descsz);
246	if (copy_from_oldmem(vmcoreinfo, addr + 24, note.n_descsz))
247		return NULL;
248	*size = note.n_descsz;
249	return vmcoreinfo;
250}
251
252/*
253 * Initialize vmcoreinfo note (new kernel)
254 */
255static void *nt_vmcoreinfo(void *ptr)
256{
257	unsigned long size;
258	void *vmcoreinfo;
259
260	vmcoreinfo = os_info_old_entry(OS_INFO_VMCOREINFO, &size);
261	if (!vmcoreinfo)
262		vmcoreinfo = get_vmcoreinfo_old(&size);
263	if (!vmcoreinfo)
264		return ptr;
265	return nt_init(ptr, 0, vmcoreinfo, size, "VMCOREINFO");
 
 
 
 
 
 
 
 
 
 
 
 
 
 
266}
267
268/*
269 * Initialize ELF header (new kernel)
270 */
271static void *ehdr_init(Elf64_Ehdr *ehdr, int mem_chunk_cnt)
272{
273	memset(ehdr, 0, sizeof(*ehdr));
274	memcpy(ehdr->e_ident, ELFMAG, SELFMAG);
275	ehdr->e_ident[EI_CLASS] = ELFCLASS64;
276	ehdr->e_ident[EI_DATA] = ELFDATA2MSB;
277	ehdr->e_ident[EI_VERSION] = EV_CURRENT;
278	memset(ehdr->e_ident + EI_PAD, 0, EI_NIDENT - EI_PAD);
279	ehdr->e_type = ET_CORE;
280	ehdr->e_machine = EM_S390;
281	ehdr->e_version = EV_CURRENT;
282	ehdr->e_phoff = sizeof(Elf64_Ehdr);
283	ehdr->e_ehsize = sizeof(Elf64_Ehdr);
284	ehdr->e_phentsize = sizeof(Elf64_Phdr);
285	ehdr->e_phnum = mem_chunk_cnt + 1;
286	return ehdr + 1;
287}
288
289/*
290 * Return CPU count for ELF header (new kernel)
291 */
292static int get_cpu_cnt(void)
293{
294	int i, cpus = 0;
 
295
296	for (i = 0; zfcpdump_save_areas[i]; i++) {
297		if (zfcpdump_save_areas[i]->pref_reg == 0)
298			continue;
299		cpus++;
300	}
301	return cpus;
302}
303
304/*
305 * Return memory chunk count for ELF header (new kernel)
306 */
307static int get_mem_chunk_cnt(void)
308{
309	struct mem_chunk *chunk_array, *mem_chunk;
310	int i, cnt = 0;
311
312	chunk_array = get_memory_layout();
313	for (i = 0; i < MEMORY_CHUNKS; i++) {
314		mem_chunk = &chunk_array[i];
315		if (chunk_array[i].type != CHUNK_READ_WRITE &&
316		    chunk_array[i].type != CHUNK_READ_ONLY)
317			continue;
318		if (mem_chunk->size == 0)
319			continue;
320		cnt++;
321	}
322	kfree(chunk_array);
323	return cnt;
324}
325
326/*
327 * Relocate pointer in order to allow vmcore code access the data
328 */
329static inline unsigned long relocate(unsigned long addr)
330{
331	return OLDMEM_BASE + addr;
332}
333
334/*
335 * Initialize ELF loads (new kernel)
336 */
337static int loads_init(Elf64_Phdr *phdr, u64 loads_offset)
338{
339	struct mem_chunk *chunk_array, *mem_chunk;
340	int i;
341
342	chunk_array = get_memory_layout();
343	for (i = 0; i < MEMORY_CHUNKS; i++) {
344		mem_chunk = &chunk_array[i];
345		if (mem_chunk->size == 0)
346			break;
347		if (chunk_array[i].type != CHUNK_READ_WRITE &&
348		    chunk_array[i].type != CHUNK_READ_ONLY)
349			continue;
350		else
351			phdr->p_filesz = mem_chunk->size;
352		phdr->p_type = PT_LOAD;
353		phdr->p_offset = mem_chunk->addr;
354		phdr->p_vaddr = mem_chunk->addr;
355		phdr->p_paddr = mem_chunk->addr;
356		phdr->p_memsz = mem_chunk->size;
357		phdr->p_flags = PF_R | PF_W | PF_X;
358		phdr->p_align = PAGE_SIZE;
359		phdr++;
360	}
361	kfree(chunk_array);
362	return i;
363}
364
365/*
366 * Initialize notes (new kernel)
367 */
368static void *notes_init(Elf64_Phdr *phdr, void *ptr, u64 notes_offset)
369{
370	struct save_area *sa;
371	void *ptr_start = ptr;
372	int i;
373
374	ptr = nt_prpsinfo(ptr);
375
376	for (i = 0; zfcpdump_save_areas[i]; i++) {
377		sa = zfcpdump_save_areas[i];
378		if (sa->pref_reg == 0)
379			continue;
380		ptr = fill_cpu_elf_notes(ptr, sa);
381	}
382	ptr = nt_vmcoreinfo(ptr);
 
383	memset(phdr, 0, sizeof(*phdr));
384	phdr->p_type = PT_NOTE;
385	phdr->p_offset = relocate(notes_offset);
386	phdr->p_filesz = (unsigned long) PTR_SUB(ptr, ptr_start);
387	phdr->p_memsz = phdr->p_filesz;
388	return ptr;
389}
390
391/*
392 * Create ELF core header (new kernel)
393 */
394static void s390_elf_corehdr_create(char **elfcorebuf, size_t *elfcorebuf_sz)
395{
396	Elf64_Phdr *phdr_notes, *phdr_loads;
397	int mem_chunk_cnt;
398	void *ptr, *hdr;
399	u32 alloc_size;
400	u64 hdr_off;
401
 
 
 
 
 
 
 
 
 
 
 
 
 
 
402	mem_chunk_cnt = get_mem_chunk_cnt();
403
404	alloc_size = 0x1000 + get_cpu_cnt() * 0x300 +
405		mem_chunk_cnt * sizeof(Elf64_Phdr);
406	hdr = kzalloc_panic(alloc_size);
407	/* Init elf header */
408	ptr = ehdr_init(hdr, mem_chunk_cnt);
409	/* Init program headers */
410	phdr_notes = ptr;
411	ptr = PTR_ADD(ptr, sizeof(Elf64_Phdr));
412	phdr_loads = ptr;
413	ptr = PTR_ADD(ptr, sizeof(Elf64_Phdr) * mem_chunk_cnt);
414	/* Init notes */
415	hdr_off = PTR_DIFF(ptr, hdr);
416	ptr = notes_init(phdr_notes, ptr, ((unsigned long) hdr) + hdr_off);
417	/* Init loads */
418	hdr_off = PTR_DIFF(ptr, hdr);
419	loads_init(phdr_loads, ((unsigned long) hdr) + hdr_off);
420	*elfcorebuf_sz = hdr_off;
421	*elfcorebuf = (void *) relocate((unsigned long) hdr);
422	BUG_ON(*elfcorebuf_sz > alloc_size);
 
423}
424
425/*
426 * Create kdump ELF core header in new kernel, if it has not been passed via
427 * the "elfcorehdr" kernel parameter
428 */
429static int setup_kdump_elfcorehdr(void)
430{
431	size_t elfcorebuf_sz;
432	char *elfcorebuf;
433
434	if (!OLDMEM_BASE || is_kdump_kernel())
435		return -EINVAL;
436	s390_elf_corehdr_create(&elfcorebuf, &elfcorebuf_sz);
437	elfcorehdr_addr = (unsigned long long) elfcorebuf;
438	elfcorehdr_size = elfcorebuf_sz;
439	return 0;
 
 
 
 
440}
441
442subsys_initcall(setup_kdump_elfcorehdr);