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  */
copy_oldmem_page(unsigned long pfn,char * buf,size_t csize,unsigned long offset,int userbuf)30 ssize_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  */
copy_from_oldmem(void * dest,void * src,size_t count)55 int 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  */
kzalloc_panic(int len)72 static 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  */
get_memory_layout(void)85 static 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  */
nt_init(void * buf,Elf64_Word type,void * desc,int d_len,const char * name)98 static 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  */
nt_prstatus(void * ptr,struct save_area * sa)122 static 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  */
nt_fpregset(void * ptr,struct save_area * sa)141 static 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  */
nt_s390_timer(void * ptr,struct save_area * sa)156 static 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  */
nt_s390_tod_cmp(void * ptr,struct save_area * sa)165 static 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  */
nt_s390_tod_preg(void * ptr,struct save_area * sa)174 static 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  */
nt_s390_ctrs(void * ptr,struct save_area * sa)183 static 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  */
nt_s390_prefix(void * ptr,struct save_area * sa)192 static 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  */
fill_cpu_elf_notes(void * ptr,struct save_area * sa)201 void *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  */
nt_prpsinfo(void * ptr)216 static 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  */
get_vmcoreinfo_old(unsigned long * size)230 static 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  */
nt_vmcoreinfo(void * ptr)255 static 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  */
ehdr_init(Elf64_Ehdr * ehdr,int mem_chunk_cnt)271 static 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  */
get_cpu_cnt(void)292 static 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  */
get_mem_chunk_cnt(void)307 static 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  */
relocate(unsigned long addr)329 static inline unsigned long relocate(unsigned long addr)
330 {
331 	return OLDMEM_BASE + addr;
332 }
333 
334 /*
335  * Initialize ELF loads (new kernel)
336  */
loads_init(Elf64_Phdr * phdr,u64 loads_offset)337 static 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  */
notes_init(Elf64_Phdr * phdr,void * ptr,u64 notes_offset)368 static 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  */
s390_elf_corehdr_create(char ** elfcorebuf,size_t * elfcorebuf_sz)394 static 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  */
setup_kdump_elfcorehdr(void)429 static 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 
442 subsys_initcall(setup_kdump_elfcorehdr);
443