[S390] Use block_sigmask()
[linux-2.6-block.git] / arch / s390 / kernel / crash_dump.c
CommitLineData
60a0c68d
MH
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>
60a0c68d
MH
14#include <linux/bootmem.h>
15#include <linux/elf.h>
16#include <asm/ipl.h>
17
18#define PTR_ADD(x, y) (((char *) (x)) + ((unsigned long) (y)))
19#define PTR_SUB(x, y) (((char *) (x)) - ((unsigned long) (y)))
20#define PTR_DIFF(x, y) ((unsigned long)(((char *) (x)) - ((unsigned long) (y))))
21
22/*
23 * Copy one page from "oldmem"
24 *
25 * For the kdump reserved memory this functions performs a swap operation:
26 * - [OLDMEM_BASE - OLDMEM_BASE + OLDMEM_SIZE] is mapped to [0 - OLDMEM_SIZE].
27 * - [0 - OLDMEM_SIZE] is mapped to [OLDMEM_BASE - OLDMEM_BASE + OLDMEM_SIZE]
28 */
29ssize_t copy_oldmem_page(unsigned long pfn, char *buf,
30 size_t csize, unsigned long offset, int userbuf)
31{
32 unsigned long src;
60a0c68d
MH
33
34 if (!csize)
35 return 0;
36
37 src = (pfn << PAGE_SHIFT) + offset;
38 if (src < OLDMEM_SIZE)
39 src += OLDMEM_BASE;
40 else if (src > OLDMEM_BASE &&
41 src < OLDMEM_BASE + OLDMEM_SIZE)
42 src -= OLDMEM_BASE;
43 if (userbuf)
07ea815b
MH
44 copy_to_user_real((void __force __user *) buf, (void *) src,
45 csize);
60a0c68d 46 else
07ea815b
MH
47 memcpy_real(buf, (void *) src, csize);
48 return csize;
60a0c68d
MH
49}
50
51/*
52 * Copy memory from old kernel
53 */
54static int copy_from_oldmem(void *dest, void *src, size_t count)
55{
56 unsigned long copied = 0;
57 int rc;
58
59 if ((unsigned long) src < OLDMEM_SIZE) {
60 copied = min(count, OLDMEM_SIZE - (unsigned long) src);
61 rc = memcpy_real(dest, src + OLDMEM_BASE, copied);
62 if (rc)
63 return rc;
64 }
65 return memcpy_real(dest + copied, src + copied, count - copied);
66}
67
68/*
69 * Alloc memory and panic in case of ENOMEM
70 */
71static void *kzalloc_panic(int len)
72{
73 void *rc;
74
75 rc = kzalloc(len, GFP_KERNEL);
76 if (!rc)
77 panic("s390 kdump kzalloc (%d) failed", len);
78 return rc;
79}
80
81/*
82 * Get memory layout and create hole for oldmem
83 */
84static struct mem_chunk *get_memory_layout(void)
85{
86 struct mem_chunk *chunk_array;
87
88 chunk_array = kzalloc_panic(MEMORY_CHUNKS * sizeof(struct mem_chunk));
89 detect_memory_layout(chunk_array);
90 create_mem_hole(chunk_array, OLDMEM_BASE, OLDMEM_SIZE, CHUNK_CRASHK);
91 return chunk_array;
92}
93
94/*
95 * Initialize ELF note
96 */
97static void *nt_init(void *buf, Elf64_Word type, void *desc, int d_len,
98 const char *name)
99{
100 Elf64_Nhdr *note;
101 u64 len;
102
103 note = (Elf64_Nhdr *)buf;
104 note->n_namesz = strlen(name) + 1;
105 note->n_descsz = d_len;
106 note->n_type = type;
107 len = sizeof(Elf64_Nhdr);
108
109 memcpy(buf + len, name, note->n_namesz);
110 len = roundup(len + note->n_namesz, 4);
111
112 memcpy(buf + len, desc, note->n_descsz);
113 len = roundup(len + note->n_descsz, 4);
114
115 return PTR_ADD(buf, len);
116}
117
118/*
119 * Initialize prstatus note
120 */
121static void *nt_prstatus(void *ptr, struct save_area *sa)
122{
123 struct elf_prstatus nt_prstatus;
124 static int cpu_nr = 1;
125
126 memset(&nt_prstatus, 0, sizeof(nt_prstatus));
127 memcpy(&nt_prstatus.pr_reg.gprs, sa->gp_regs, sizeof(sa->gp_regs));
128 memcpy(&nt_prstatus.pr_reg.psw, sa->psw, sizeof(sa->psw));
129 memcpy(&nt_prstatus.pr_reg.acrs, sa->acc_regs, sizeof(sa->acc_regs));
130 nt_prstatus.pr_pid = cpu_nr;
131 cpu_nr++;
132
133 return nt_init(ptr, NT_PRSTATUS, &nt_prstatus, sizeof(nt_prstatus),
134 "CORE");
135}
136
137/*
138 * Initialize fpregset (floating point) note
139 */
140static void *nt_fpregset(void *ptr, struct save_area *sa)
141{
142 elf_fpregset_t nt_fpregset;
143
144 memset(&nt_fpregset, 0, sizeof(nt_fpregset));
145 memcpy(&nt_fpregset.fpc, &sa->fp_ctrl_reg, sizeof(sa->fp_ctrl_reg));
146 memcpy(&nt_fpregset.fprs, &sa->fp_regs, sizeof(sa->fp_regs));
147
148 return nt_init(ptr, NT_PRFPREG, &nt_fpregset, sizeof(nt_fpregset),
149 "CORE");
150}
151
152/*
153 * Initialize timer note
154 */
155static void *nt_s390_timer(void *ptr, struct save_area *sa)
156{
157 return nt_init(ptr, NT_S390_TIMER, &sa->timer, sizeof(sa->timer),
158 KEXEC_CORE_NOTE_NAME);
159}
160
161/*
162 * Initialize TOD clock comparator note
163 */
164static void *nt_s390_tod_cmp(void *ptr, struct save_area *sa)
165{
166 return nt_init(ptr, NT_S390_TODCMP, &sa->clk_cmp,
167 sizeof(sa->clk_cmp), KEXEC_CORE_NOTE_NAME);
168}
169
170/*
171 * Initialize TOD programmable register note
172 */
173static void *nt_s390_tod_preg(void *ptr, struct save_area *sa)
174{
175 return nt_init(ptr, NT_S390_TODPREG, &sa->tod_reg,
176 sizeof(sa->tod_reg), KEXEC_CORE_NOTE_NAME);
177}
178
179/*
180 * Initialize control register note
181 */
182static void *nt_s390_ctrs(void *ptr, struct save_area *sa)
183{
184 return nt_init(ptr, NT_S390_CTRS, &sa->ctrl_regs,
185 sizeof(sa->ctrl_regs), KEXEC_CORE_NOTE_NAME);
186}
187
188/*
189 * Initialize prefix register note
190 */
191static void *nt_s390_prefix(void *ptr, struct save_area *sa)
192{
193 return nt_init(ptr, NT_S390_PREFIX, &sa->pref_reg,
194 sizeof(sa->pref_reg), KEXEC_CORE_NOTE_NAME);
195}
196
197/*
198 * Fill ELF notes for one CPU with save area registers
199 */
200void *fill_cpu_elf_notes(void *ptr, struct save_area *sa)
201{
202 ptr = nt_prstatus(ptr, sa);
203 ptr = nt_fpregset(ptr, sa);
204 ptr = nt_s390_timer(ptr, sa);
205 ptr = nt_s390_tod_cmp(ptr, sa);
206 ptr = nt_s390_tod_preg(ptr, sa);
207 ptr = nt_s390_ctrs(ptr, sa);
208 ptr = nt_s390_prefix(ptr, sa);
209 return ptr;
210}
211
212/*
213 * Initialize prpsinfo note (new kernel)
214 */
215static void *nt_prpsinfo(void *ptr)
216{
217 struct elf_prpsinfo prpsinfo;
218
219 memset(&prpsinfo, 0, sizeof(prpsinfo));
220 prpsinfo.pr_sname = 'R';
221 strcpy(prpsinfo.pr_fname, "vmlinux");
222 return nt_init(ptr, NT_PRPSINFO, &prpsinfo, sizeof(prpsinfo),
223 KEXEC_CORE_NOTE_NAME);
224}
225
226/*
227 * Initialize vmcoreinfo note (new kernel)
228 */
229static void *nt_vmcoreinfo(void *ptr)
230{
231 char nt_name[11], *vmcoreinfo;
232 Elf64_Nhdr note;
233 void *addr;
234
235 if (copy_from_oldmem(&addr, &S390_lowcore.vmcore_info, sizeof(addr)))
236 return ptr;
237 memset(nt_name, 0, sizeof(nt_name));
238 if (copy_from_oldmem(&note, addr, sizeof(note)))
239 return ptr;
240 if (copy_from_oldmem(nt_name, addr + sizeof(note), sizeof(nt_name) - 1))
241 return ptr;
242 if (strcmp(nt_name, "VMCOREINFO") != 0)
243 return ptr;
244 vmcoreinfo = kzalloc_panic(note.n_descsz + 1);
245 if (copy_from_oldmem(vmcoreinfo, addr + 24, note.n_descsz))
246 return ptr;
247 vmcoreinfo[note.n_descsz + 1] = 0;
248 return nt_init(ptr, 0, vmcoreinfo, note.n_descsz, "VMCOREINFO");
249}
250
251/*
252 * Initialize ELF header (new kernel)
253 */
254static void *ehdr_init(Elf64_Ehdr *ehdr, int mem_chunk_cnt)
255{
256 memset(ehdr, 0, sizeof(*ehdr));
257 memcpy(ehdr->e_ident, ELFMAG, SELFMAG);
258 ehdr->e_ident[EI_CLASS] = ELFCLASS64;
259 ehdr->e_ident[EI_DATA] = ELFDATA2MSB;
260 ehdr->e_ident[EI_VERSION] = EV_CURRENT;
261 memset(ehdr->e_ident + EI_PAD, 0, EI_NIDENT - EI_PAD);
262 ehdr->e_type = ET_CORE;
263 ehdr->e_machine = EM_S390;
264 ehdr->e_version = EV_CURRENT;
265 ehdr->e_phoff = sizeof(Elf64_Ehdr);
266 ehdr->e_ehsize = sizeof(Elf64_Ehdr);
267 ehdr->e_phentsize = sizeof(Elf64_Phdr);
268 ehdr->e_phnum = mem_chunk_cnt + 1;
269 return ehdr + 1;
270}
271
272/*
273 * Return CPU count for ELF header (new kernel)
274 */
275static int get_cpu_cnt(void)
276{
277 int i, cpus = 0;
278
279 for (i = 0; zfcpdump_save_areas[i]; i++) {
280 if (zfcpdump_save_areas[i]->pref_reg == 0)
281 continue;
282 cpus++;
283 }
284 return cpus;
285}
286
287/*
288 * Return memory chunk count for ELF header (new kernel)
289 */
290static int get_mem_chunk_cnt(void)
291{
292 struct mem_chunk *chunk_array, *mem_chunk;
293 int i, cnt = 0;
294
295 chunk_array = get_memory_layout();
296 for (i = 0; i < MEMORY_CHUNKS; i++) {
297 mem_chunk = &chunk_array[i];
298 if (chunk_array[i].type != CHUNK_READ_WRITE &&
299 chunk_array[i].type != CHUNK_READ_ONLY)
300 continue;
301 if (mem_chunk->size == 0)
302 continue;
303 cnt++;
304 }
305 kfree(chunk_array);
306 return cnt;
307}
308
309/*
310 * Relocate pointer in order to allow vmcore code access the data
311 */
312static inline unsigned long relocate(unsigned long addr)
313{
314 return OLDMEM_BASE + addr;
315}
316
317/*
318 * Initialize ELF loads (new kernel)
319 */
320static int loads_init(Elf64_Phdr *phdr, u64 loads_offset)
321{
322 struct mem_chunk *chunk_array, *mem_chunk;
323 int i;
324
325 chunk_array = get_memory_layout();
326 for (i = 0; i < MEMORY_CHUNKS; i++) {
327 mem_chunk = &chunk_array[i];
328 if (mem_chunk->size == 0)
329 break;
330 if (chunk_array[i].type != CHUNK_READ_WRITE &&
331 chunk_array[i].type != CHUNK_READ_ONLY)
332 continue;
333 else
334 phdr->p_filesz = mem_chunk->size;
335 phdr->p_type = PT_LOAD;
336 phdr->p_offset = mem_chunk->addr;
337 phdr->p_vaddr = mem_chunk->addr;
338 phdr->p_paddr = mem_chunk->addr;
339 phdr->p_memsz = mem_chunk->size;
340 phdr->p_flags = PF_R | PF_W | PF_X;
341 phdr->p_align = PAGE_SIZE;
342 phdr++;
343 }
344 kfree(chunk_array);
345 return i;
346}
347
348/*
349 * Initialize notes (new kernel)
350 */
351static void *notes_init(Elf64_Phdr *phdr, void *ptr, u64 notes_offset)
352{
353 struct save_area *sa;
354 void *ptr_start = ptr;
355 int i;
356
357 ptr = nt_prpsinfo(ptr);
358
359 for (i = 0; zfcpdump_save_areas[i]; i++) {
360 sa = zfcpdump_save_areas[i];
361 if (sa->pref_reg == 0)
362 continue;
363 ptr = fill_cpu_elf_notes(ptr, sa);
364 }
365 ptr = nt_vmcoreinfo(ptr);
366 memset(phdr, 0, sizeof(*phdr));
367 phdr->p_type = PT_NOTE;
368 phdr->p_offset = relocate(notes_offset);
369 phdr->p_filesz = (unsigned long) PTR_SUB(ptr, ptr_start);
370 phdr->p_memsz = phdr->p_filesz;
371 return ptr;
372}
373
374/*
375 * Create ELF core header (new kernel)
376 */
377static void s390_elf_corehdr_create(char **elfcorebuf, size_t *elfcorebuf_sz)
378{
379 Elf64_Phdr *phdr_notes, *phdr_loads;
380 int mem_chunk_cnt;
381 void *ptr, *hdr;
382 u32 alloc_size;
383 u64 hdr_off;
384
385 mem_chunk_cnt = get_mem_chunk_cnt();
386
387 alloc_size = 0x1000 + get_cpu_cnt() * 0x300 +
388 mem_chunk_cnt * sizeof(Elf64_Phdr);
389 hdr = kzalloc_panic(alloc_size);
390 /* Init elf header */
391 ptr = ehdr_init(hdr, mem_chunk_cnt);
392 /* Init program headers */
393 phdr_notes = ptr;
394 ptr = PTR_ADD(ptr, sizeof(Elf64_Phdr));
395 phdr_loads = ptr;
396 ptr = PTR_ADD(ptr, sizeof(Elf64_Phdr) * mem_chunk_cnt);
397 /* Init notes */
398 hdr_off = PTR_DIFF(ptr, hdr);
399 ptr = notes_init(phdr_notes, ptr, ((unsigned long) hdr) + hdr_off);
400 /* Init loads */
401 hdr_off = PTR_DIFF(ptr, hdr);
402 loads_init(phdr_loads, ((unsigned long) hdr) + hdr_off);
403 *elfcorebuf_sz = hdr_off;
404 *elfcorebuf = (void *) relocate((unsigned long) hdr);
405 BUG_ON(*elfcorebuf_sz > alloc_size);
406}
407
408/*
409 * Create kdump ELF core header in new kernel, if it has not been passed via
410 * the "elfcorehdr" kernel parameter
411 */
412static int setup_kdump_elfcorehdr(void)
413{
414 size_t elfcorebuf_sz;
415 char *elfcorebuf;
416
417 if (!OLDMEM_BASE || is_kdump_kernel())
418 return -EINVAL;
419 s390_elf_corehdr_create(&elfcorebuf, &elfcorebuf_sz);
420 elfcorehdr_addr = (unsigned long long) elfcorebuf;
421 elfcorehdr_size = elfcorebuf_sz;
422 return 0;
423}
424
425subsys_initcall(setup_kdump_elfcorehdr);