Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1/*
2 * zcore module to export memory content and register sets for creating system
3 * dumps on SCSI disks (zfcpdump). The "zcore/mem" debugfs file shows the same
4 * dump format as s390 standalone dumps.
5 *
6 * For more information please refer to Documentation/s390/zfcpdump.txt
7 *
8 * Copyright IBM Corp. 2003,2007
9 * Author(s): Michael Holzheu
10 */
11
12#include <linux/init.h>
13#include <linux/miscdevice.h>
14#include <linux/utsname.h>
15#include <linux/debugfs.h>
16#include <asm/ipl.h>
17#include <asm/sclp.h>
18#include <asm/setup.h>
19#include <asm/sigp.h>
20#include <asm/uaccess.h>
21#include <asm/debug.h>
22#include <asm/processor.h>
23#include <asm/irqflags.h>
24#include "sclp.h"
25
26#define TRACE(x...) debug_sprintf_event(zcore_dbf, 1, x)
27#define MSG(x...) printk( KERN_ALERT x )
28#define ERROR_MSG(x...) printk ( KERN_ALERT "DUMP: " x )
29
30#define TO_USER 0
31#define TO_KERNEL 1
32
33enum arch_id {
34 ARCH_S390 = 0,
35 ARCH_S390X = 1,
36};
37
38/* dump system info */
39
40struct sys_info {
41 enum arch_id arch;
42 unsigned long sa_base;
43 u32 sa_size;
44 int cpu_map[NR_CPUS];
45 unsigned long mem_size;
46 union save_area lc_mask;
47};
48
49static struct sys_info sys_info;
50static struct debug_info *zcore_dbf;
51static int hsa_available;
52static struct dentry *zcore_dir;
53static struct dentry *zcore_file;
54
55/*
56 * Copy memory from HSA to kernel or user memory (not reentrant):
57 *
58 * @dest: Kernel or user buffer where memory should be copied to
59 * @src: Start address within HSA where data should be copied
60 * @count: Size of buffer, which should be copied
61 * @mode: Either TO_KERNEL or TO_USER
62 */
63static int memcpy_hsa(void *dest, unsigned long src, size_t count, int mode)
64{
65 int offs, blk_num;
66 static char buf[PAGE_SIZE] __attribute__((__aligned__(PAGE_SIZE)));
67
68 if (count == 0)
69 return 0;
70
71 /* copy first block */
72 offs = 0;
73 if ((src % PAGE_SIZE) != 0) {
74 blk_num = src / PAGE_SIZE + 2;
75 if (sclp_sdias_copy(buf, blk_num, 1)) {
76 TRACE("sclp_sdias_copy() failed\n");
77 return -EIO;
78 }
79 offs = min((PAGE_SIZE - (src % PAGE_SIZE)), count);
80 if (mode == TO_USER) {
81 if (copy_to_user((__force __user void*) dest,
82 buf + (src % PAGE_SIZE), offs))
83 return -EFAULT;
84 } else
85 memcpy(dest, buf + (src % PAGE_SIZE), offs);
86 }
87 if (offs == count)
88 goto out;
89
90 /* copy middle */
91 for (; (offs + PAGE_SIZE) <= count; offs += PAGE_SIZE) {
92 blk_num = (src + offs) / PAGE_SIZE + 2;
93 if (sclp_sdias_copy(buf, blk_num, 1)) {
94 TRACE("sclp_sdias_copy() failed\n");
95 return -EIO;
96 }
97 if (mode == TO_USER) {
98 if (copy_to_user((__force __user void*) dest + offs,
99 buf, PAGE_SIZE))
100 return -EFAULT;
101 } else
102 memcpy(dest + offs, buf, PAGE_SIZE);
103 }
104 if (offs == count)
105 goto out;
106
107 /* copy last block */
108 blk_num = (src + offs) / PAGE_SIZE + 2;
109 if (sclp_sdias_copy(buf, blk_num, 1)) {
110 TRACE("sclp_sdias_copy() failed\n");
111 return -EIO;
112 }
113 if (mode == TO_USER) {
114 if (copy_to_user((__force __user void*) dest + offs, buf,
115 PAGE_SIZE))
116 return -EFAULT;
117 } else
118 memcpy(dest + offs, buf, count - offs);
119out:
120 return 0;
121}
122
123static int memcpy_hsa_user(void __user *dest, unsigned long src, size_t count)
124{
125 return memcpy_hsa((void __force *) dest, src, count, TO_USER);
126}
127
128static int memcpy_hsa_kernel(void *dest, unsigned long src, size_t count)
129{
130 return memcpy_hsa(dest, src, count, TO_KERNEL);
131}
132
133static int memcpy_real(void *dest, unsigned long src, size_t count)
134{
135 unsigned long flags;
136 int rc = -EFAULT;
137 register unsigned long _dest asm("2") = (unsigned long) dest;
138 register unsigned long _len1 asm("3") = (unsigned long) count;
139 register unsigned long _src asm("4") = src;
140 register unsigned long _len2 asm("5") = (unsigned long) count;
141
142 if (count == 0)
143 return 0;
144 flags = __raw_local_irq_stnsm(0xf8); /* switch to real mode */
145 asm volatile (
146 "0: mvcle %1,%2,0x0\n"
147 "1: jo 0b\n"
148 " lhi %0,0x0\n"
149 "2:\n"
150 EX_TABLE(1b,2b)
151 : "+d" (rc)
152 : "d" (_dest), "d" (_src), "d" (_len1), "d" (_len2)
153 : "cc", "memory");
154 __raw_local_irq_ssm(flags);
155
156 return rc;
157}
158
159static int memcpy_real_user(__user void *dest, unsigned long src, size_t count)
160{
161 static char buf[4096];
162 int offs = 0, size;
163
164 while (offs < count) {
165 size = min(sizeof(buf), count - offs);
166 if (memcpy_real(buf, src + offs, size))
167 return -EFAULT;
168 if (copy_to_user(dest + offs, buf, size))
169 return -EFAULT;
170 offs += size;
171 }
172 return 0;
173}
174
175#ifdef __s390x__
176/*
177 * Convert s390x (64 bit) cpu info to s390 (32 bit) cpu info
178 */
179static void __init s390x_to_s390_regs(union save_area *out, union save_area *in,
180 int cpu)
181{
182 int i;
183
184 for (i = 0; i < 16; i++) {
185 out->s390.gp_regs[i] = in->s390x.gp_regs[i] & 0x00000000ffffffff;
186 out->s390.acc_regs[i] = in->s390x.acc_regs[i];
187 out->s390.ctrl_regs[i] =
188 in->s390x.ctrl_regs[i] & 0x00000000ffffffff;
189 }
190 /* locore for 31 bit has only space for fpregs 0,2,4,6 */
191 out->s390.fp_regs[0] = in->s390x.fp_regs[0];
192 out->s390.fp_regs[1] = in->s390x.fp_regs[2];
193 out->s390.fp_regs[2] = in->s390x.fp_regs[4];
194 out->s390.fp_regs[3] = in->s390x.fp_regs[6];
195 memcpy(&(out->s390.psw[0]), &(in->s390x.psw[0]), 4);
196 out->s390.psw[1] |= 0x8; /* set bit 12 */
197 memcpy(&(out->s390.psw[4]),&(in->s390x.psw[12]), 4);
198 out->s390.psw[4] |= 0x80; /* set (31bit) addressing bit */
199 out->s390.pref_reg = in->s390x.pref_reg;
200 out->s390.timer = in->s390x.timer;
201 out->s390.clk_cmp = in->s390x.clk_cmp;
202}
203
204static void __init s390x_to_s390_save_areas(void)
205{
206 int i = 1;
207 static union save_area tmp;
208
209 while (zfcpdump_save_areas[i]) {
210 s390x_to_s390_regs(&tmp, zfcpdump_save_areas[i], i);
211 memcpy(zfcpdump_save_areas[i], &tmp, sizeof(tmp));
212 i++;
213 }
214}
215
216#endif /* __s390x__ */
217
218static int __init init_cpu_info(enum arch_id arch)
219{
220 union save_area *sa;
221
222 /* get info for boot cpu from lowcore, stored in the HSA */
223
224 sa = kmalloc(sizeof(*sa), GFP_KERNEL);
225 if (!sa) {
226 ERROR_MSG("kmalloc failed: %s: %i\n",__FUNCTION__, __LINE__);
227 return -ENOMEM;
228 }
229 if (memcpy_hsa_kernel(sa, sys_info.sa_base, sys_info.sa_size) < 0) {
230 ERROR_MSG("could not copy from HSA\n");
231 kfree(sa);
232 return -EIO;
233 }
234 zfcpdump_save_areas[0] = sa;
235
236#ifdef __s390x__
237 /* convert s390x regs to s390, if we are dumping an s390 Linux */
238
239 if (arch == ARCH_S390)
240 s390x_to_s390_save_areas();
241#endif
242
243 return 0;
244}
245
246static DEFINE_MUTEX(zcore_mutex);
247
248#define DUMP_VERSION 0x3
249#define DUMP_MAGIC 0xa8190173618f23fdULL
250#define DUMP_ARCH_S390X 2
251#define DUMP_ARCH_S390 1
252#define HEADER_SIZE 4096
253
254/* dump header dumped according to s390 crash dump format */
255
256struct zcore_header {
257 u64 magic;
258 u32 version;
259 u32 header_size;
260 u32 dump_level;
261 u32 page_size;
262 u64 mem_size;
263 u64 mem_start;
264 u64 mem_end;
265 u32 num_pages;
266 u32 pad1;
267 u64 tod;
268 cpuid_t cpu_id;
269 u32 arch_id;
270 u32 build_arch;
271 char pad2[4016];
272} __attribute__((packed,__aligned__(16)));
273
274static struct zcore_header zcore_header = {
275 .magic = DUMP_MAGIC,
276 .version = DUMP_VERSION,
277 .header_size = 4096,
278 .dump_level = 0,
279 .page_size = PAGE_SIZE,
280 .mem_start = 0,
281#ifdef __s390x__
282 .build_arch = DUMP_ARCH_S390X,
283#else
284 .build_arch = DUMP_ARCH_S390,
285#endif
286};
287
288/*
289 * Copy lowcore info to buffer. Use map in order to copy only register parts.
290 *
291 * @buf: User buffer
292 * @sa: Pointer to save area
293 * @sa_off: Offset in save area to copy
294 * @len: Number of bytes to copy
295 */
296static int copy_lc(void __user *buf, void *sa, int sa_off, int len)
297{
298 int i;
299 char *lc_mask = (char*)&sys_info.lc_mask;
300
301 for (i = 0; i < len; i++) {
302 if (!lc_mask[i + sa_off])
303 continue;
304 if (copy_to_user(buf + i, sa + sa_off + i, 1))
305 return -EFAULT;
306 }
307 return 0;
308}
309
310/*
311 * Copy lowcores info to memory, if necessary
312 *
313 * @buf: User buffer
314 * @addr: Start address of buffer in dump memory
315 * @count: Size of buffer
316 */
317static int zcore_add_lc(char __user *buf, unsigned long start, size_t count)
318{
319 unsigned long end;
320 int i = 0;
321
322 if (count == 0)
323 return 0;
324
325 end = start + count;
326 while (zfcpdump_save_areas[i]) {
327 unsigned long cp_start, cp_end; /* copy range */
328 unsigned long sa_start, sa_end; /* save area range */
329 unsigned long prefix;
330 unsigned long sa_off, len, buf_off;
331
332 if (sys_info.arch == ARCH_S390)
333 prefix = zfcpdump_save_areas[i]->s390.pref_reg;
334 else
335 prefix = zfcpdump_save_areas[i]->s390x.pref_reg;
336
337 sa_start = prefix + sys_info.sa_base;
338 sa_end = prefix + sys_info.sa_base + sys_info.sa_size;
339
340 if ((end < sa_start) || (start > sa_end))
341 goto next;
342 cp_start = max(start, sa_start);
343 cp_end = min(end, sa_end);
344
345 buf_off = cp_start - start;
346 sa_off = cp_start - sa_start;
347 len = cp_end - cp_start;
348
349 TRACE("copy_lc for: %lx\n", start);
350 if (copy_lc(buf + buf_off, zfcpdump_save_areas[i], sa_off, len))
351 return -EFAULT;
352next:
353 i++;
354 }
355 return 0;
356}
357
358/*
359 * Read routine for zcore character device
360 * First 4K are dump header
361 * Next 32MB are HSA Memory
362 * Rest is read from absolute Memory
363 */
364static ssize_t zcore_read(struct file *file, char __user *buf, size_t count,
365 loff_t *ppos)
366{
367 unsigned long mem_start; /* Start address in memory */
368 size_t mem_offs; /* Offset in dump memory */
369 size_t hdr_count; /* Size of header part of output buffer */
370 size_t size;
371 int rc;
372
373 mutex_lock(&zcore_mutex);
374
375 if (*ppos > (sys_info.mem_size + HEADER_SIZE)) {
376 rc = -EINVAL;
377 goto fail;
378 }
379
380 count = min(count, (size_t) (sys_info.mem_size + HEADER_SIZE - *ppos));
381
382 /* Copy dump header */
383 if (*ppos < HEADER_SIZE) {
384 size = min(count, (size_t) (HEADER_SIZE - *ppos));
385 if (copy_to_user(buf, &zcore_header + *ppos, size)) {
386 rc = -EFAULT;
387 goto fail;
388 }
389 hdr_count = size;
390 mem_start = 0;
391 } else {
392 hdr_count = 0;
393 mem_start = *ppos - HEADER_SIZE;
394 }
395
396 mem_offs = 0;
397
398 /* Copy from HSA data */
399 if (*ppos < (ZFCPDUMP_HSA_SIZE + HEADER_SIZE)) {
400 size = min((count - hdr_count), (size_t) (ZFCPDUMP_HSA_SIZE
401 - mem_start));
402 rc = memcpy_hsa_user(buf + hdr_count, mem_start, size);
403 if (rc)
404 goto fail;
405
406 mem_offs += size;
407 }
408
409 /* Copy from real mem */
410 size = count - mem_offs - hdr_count;
411 rc = memcpy_real_user(buf + hdr_count + mem_offs, mem_start + mem_offs,
412 size);
413 if (rc)
414 goto fail;
415
416 /*
417 * Since s390 dump analysis tools like lcrash or crash
418 * expect register sets in the prefix pages of the cpus,
419 * we copy them into the read buffer, if necessary.
420 * buf + hdr_count: Start of memory part of output buffer
421 * mem_start: Start memory address to copy from
422 * count - hdr_count: Size of memory area to copy
423 */
424 if (zcore_add_lc(buf + hdr_count, mem_start, count - hdr_count)) {
425 rc = -EFAULT;
426 goto fail;
427 }
428 *ppos += count;
429fail:
430 mutex_unlock(&zcore_mutex);
431 return (rc < 0) ? rc : count;
432}
433
434static int zcore_open(struct inode *inode, struct file *filp)
435{
436 if (!hsa_available)
437 return -ENODATA;
438 else
439 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
440}
441
442static int zcore_release(struct inode *inode, struct file *filep)
443{
444 diag308(DIAG308_REL_HSA, NULL);
445 hsa_available = 0;
446 return 0;
447}
448
449static loff_t zcore_lseek(struct file *file, loff_t offset, int orig)
450{
451 loff_t rc;
452
453 mutex_lock(&zcore_mutex);
454 switch (orig) {
455 case 0:
456 file->f_pos = offset;
457 rc = file->f_pos;
458 break;
459 case 1:
460 file->f_pos += offset;
461 rc = file->f_pos;
462 break;
463 default:
464 rc = -EINVAL;
465 }
466 mutex_unlock(&zcore_mutex);
467 return rc;
468}
469
470static struct file_operations zcore_fops = {
471 .owner = THIS_MODULE,
472 .llseek = zcore_lseek,
473 .read = zcore_read,
474 .open = zcore_open,
475 .release = zcore_release,
476};
477
478
479static void __init set_s390_lc_mask(union save_area *map)
480{
481 memset(&map->s390.ext_save, 0xff, sizeof(map->s390.ext_save));
482 memset(&map->s390.timer, 0xff, sizeof(map->s390.timer));
483 memset(&map->s390.clk_cmp, 0xff, sizeof(map->s390.clk_cmp));
484 memset(&map->s390.psw, 0xff, sizeof(map->s390.psw));
485 memset(&map->s390.pref_reg, 0xff, sizeof(map->s390.pref_reg));
486 memset(&map->s390.acc_regs, 0xff, sizeof(map->s390.acc_regs));
487 memset(&map->s390.fp_regs, 0xff, sizeof(map->s390.fp_regs));
488 memset(&map->s390.gp_regs, 0xff, sizeof(map->s390.gp_regs));
489 memset(&map->s390.ctrl_regs, 0xff, sizeof(map->s390.ctrl_regs));
490}
491
492static void __init set_s390x_lc_mask(union save_area *map)
493{
494 memset(&map->s390x.fp_regs, 0xff, sizeof(map->s390x.fp_regs));
495 memset(&map->s390x.gp_regs, 0xff, sizeof(map->s390x.gp_regs));
496 memset(&map->s390x.psw, 0xff, sizeof(map->s390x.psw));
497 memset(&map->s390x.pref_reg, 0xff, sizeof(map->s390x.pref_reg));
498 memset(&map->s390x.fp_ctrl_reg, 0xff, sizeof(map->s390x.fp_ctrl_reg));
499 memset(&map->s390x.tod_reg, 0xff, sizeof(map->s390x.tod_reg));
500 memset(&map->s390x.timer, 0xff, sizeof(map->s390x.timer));
501 memset(&map->s390x.clk_cmp, 0xff, sizeof(map->s390x.clk_cmp));
502 memset(&map->s390x.acc_regs, 0xff, sizeof(map->s390x.acc_regs));
503 memset(&map->s390x.ctrl_regs, 0xff, sizeof(map->s390x.ctrl_regs));
504}
505
506/*
507 * Initialize dump globals for a given architecture
508 */
509static int __init sys_info_init(enum arch_id arch)
510{
511 switch (arch) {
512 case ARCH_S390X:
513 MSG("DETECTED 'S390X (64 bit) OS'\n");
514 sys_info.sa_base = SAVE_AREA_BASE_S390X;
515 sys_info.sa_size = sizeof(struct save_area_s390x);
516 set_s390x_lc_mask(&sys_info.lc_mask);
517 break;
518 case ARCH_S390:
519 MSG("DETECTED 'S390 (32 bit) OS'\n");
520 sys_info.sa_base = SAVE_AREA_BASE_S390;
521 sys_info.sa_size = sizeof(struct save_area_s390);
522 set_s390_lc_mask(&sys_info.lc_mask);
523 break;
524 default:
525 ERROR_MSG("unknown architecture 0x%x.\n",arch);
526 return -EINVAL;
527 }
528 sys_info.arch = arch;
529 if (init_cpu_info(arch)) {
530 ERROR_MSG("get cpu info failed\n");
531 return -ENOMEM;
532 }
533 sys_info.mem_size = real_memory_size;
534
535 return 0;
536}
537
538static int __init check_sdias(void)
539{
540 int rc, act_hsa_size;
541
542 rc = sclp_sdias_blk_count();
543 if (rc < 0) {
544 ERROR_MSG("Could not determine HSA size\n");
545 return rc;
546 }
547 act_hsa_size = (rc - 1) * PAGE_SIZE;
548 if (act_hsa_size < ZFCPDUMP_HSA_SIZE) {
549 ERROR_MSG("HSA size too small: %i\n", act_hsa_size);
550 return -EINVAL;
551 }
552 return 0;
553}
554
555static void __init zcore_header_init(int arch, struct zcore_header *hdr)
556{
557 if (arch == ARCH_S390X)
558 hdr->arch_id = DUMP_ARCH_S390X;
559 else
560 hdr->arch_id = DUMP_ARCH_S390;
561 hdr->mem_size = sys_info.mem_size;
562 hdr->mem_end = sys_info.mem_size;
563 hdr->num_pages = sys_info.mem_size / PAGE_SIZE;
564 hdr->tod = get_clock();
565 get_cpu_id(&hdr->cpu_id);
566}
567
568static int __init zcore_init(void)
569{
570 unsigned char arch;
571 int rc;
572
573 if (ipl_info.type != IPL_TYPE_FCP_DUMP)
574 return -ENODATA;
575
576 zcore_dbf = debug_register("zcore", 4, 1, 4 * sizeof(long));
577 debug_register_view(zcore_dbf, &debug_sprintf_view);
578 debug_set_level(zcore_dbf, 6);
579
580 TRACE("devno: %x\n", ipl_info.data.fcp.dev_id.devno);
581 TRACE("wwpn: %llx\n", (unsigned long long) ipl_info.data.fcp.wwpn);
582 TRACE("lun: %llx\n", (unsigned long long) ipl_info.data.fcp.lun);
583
584 rc = sclp_sdias_init();
585 if (rc)
586 goto fail;
587
588 rc = check_sdias();
589 if (rc) {
590 ERROR_MSG("Dump initialization failed\n");
591 goto fail;
592 }
593
594 rc = memcpy_hsa_kernel(&arch, __LC_AR_MODE_ID, 1);
595 if (rc) {
596 ERROR_MSG("sdial memcpy for arch id failed\n");
597 goto fail;
598 }
599
600#ifndef __s390x__
601 if (arch == ARCH_S390X) {
602 ERROR_MSG("32 bit dumper can't dump 64 bit system!\n");
603 rc = -EINVAL;
604 goto fail;
605 }
606#endif
607
608 rc = sys_info_init(arch);
609 if (rc) {
610 ERROR_MSG("arch init failed\n");
611 goto fail;
612 }
613
614 zcore_header_init(arch, &zcore_header);
615
616 zcore_dir = debugfs_create_dir("zcore" , NULL);
617 if (!zcore_dir) {
618 rc = -ENOMEM;
619 goto fail;
620 }
621 zcore_file = debugfs_create_file("mem", S_IRUSR, zcore_dir, NULL,
622 &zcore_fops);
623 if (!zcore_file) {
624 debugfs_remove(zcore_dir);
625 rc = -ENOMEM;
626 goto fail;
627 }
628 hsa_available = 1;
629 return 0;
630
631fail:
632 diag308(DIAG308_REL_HSA, NULL);
633 return rc;
634}
635
636static void __exit zcore_exit(void)
637{
638 debug_unregister(zcore_dbf);
639 sclp_sdias_exit();
640 diag308(DIAG308_REL_HSA, NULL);
641}
642
643MODULE_AUTHOR("Copyright IBM Corp. 2003,2007");
644MODULE_DESCRIPTION("zcore module for zfcpdump support");
645MODULE_LICENSE("GPL");
646
647subsys_initcall(zcore_init);
648module_exit(zcore_exit);