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.
6 * For more information please refer to Documentation/s390/zfcpdump.txt
8 * Copyright IBM Corp. 2003,2007
9 * Author(s): Michael Holzheu
12 #include <linux/init.h>
13 #include <linux/miscdevice.h>
14 #include <linux/utsname.h>
15 #include <linux/debugfs.h>
18 #include <asm/setup.h>
20 #include <asm/uaccess.h>
21 #include <asm/debug.h>
22 #include <asm/processor.h>
23 #include <asm/irqflags.h>
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 )
38 /* dump system info */
42 unsigned long sa_base;
45 unsigned long mem_size;
46 union save_area lc_mask;
49 static struct sys_info sys_info;
50 static struct debug_info *zcore_dbf;
51 static int hsa_available;
52 static struct dentry *zcore_dir;
53 static struct dentry *zcore_file;
56 * Copy memory from HSA to kernel or user memory (not reentrant):
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
63 static int memcpy_hsa(void *dest, unsigned long src, size_t count, int mode)
66 static char buf[PAGE_SIZE] __attribute__((__aligned__(PAGE_SIZE)));
71 /* copy first block */
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");
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))
85 memcpy(dest, buf + (src % PAGE_SIZE), offs);
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");
97 if (mode == TO_USER) {
98 if (copy_to_user((__force __user void*) dest + offs,
102 memcpy(dest + offs, buf, PAGE_SIZE);
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");
113 if (mode == TO_USER) {
114 if (copy_to_user((__force __user void*) dest + offs, buf,
118 memcpy(dest + offs, buf, count - offs);
123 static int memcpy_hsa_user(void __user *dest, unsigned long src, size_t count)
125 return memcpy_hsa((void __force *) dest, src, count, TO_USER);
128 static int memcpy_hsa_kernel(void *dest, unsigned long src, size_t count)
130 return memcpy_hsa(dest, src, count, TO_KERNEL);
133 static int memcpy_real(void *dest, unsigned long src, size_t count)
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;
144 flags = __raw_local_irq_stnsm(0xf8UL); /* switch to real mode */
146 "0: mvcle %1,%2,0x0\n"
151 : "+d" (rc), "+d" (_dest), "+d" (_src), "+d" (_len1),
152 "+d" (_len2), "=m" (*((long*)dest))
153 : "m" (*((long*)src))
155 __raw_local_irq_ssm(flags);
160 static int memcpy_real_user(void __user *dest, unsigned long src, size_t count)
162 static char buf[4096];
165 while (offs < count) {
166 size = min(sizeof(buf), count - offs);
167 if (memcpy_real(buf, src + offs, size))
169 if (copy_to_user(dest + offs, buf, size))
178 * Convert s390x (64 bit) cpu info to s390 (32 bit) cpu info
180 static void __init s390x_to_s390_regs(union save_area *out, union save_area *in,
185 for (i = 0; i < 16; i++) {
186 out->s390.gp_regs[i] = in->s390x.gp_regs[i] & 0x00000000ffffffff;
187 out->s390.acc_regs[i] = in->s390x.acc_regs[i];
188 out->s390.ctrl_regs[i] =
189 in->s390x.ctrl_regs[i] & 0x00000000ffffffff;
191 /* locore for 31 bit has only space for fpregs 0,2,4,6 */
192 out->s390.fp_regs[0] = in->s390x.fp_regs[0];
193 out->s390.fp_regs[1] = in->s390x.fp_regs[2];
194 out->s390.fp_regs[2] = in->s390x.fp_regs[4];
195 out->s390.fp_regs[3] = in->s390x.fp_regs[6];
196 memcpy(&(out->s390.psw[0]), &(in->s390x.psw[0]), 4);
197 out->s390.psw[1] |= 0x8; /* set bit 12 */
198 memcpy(&(out->s390.psw[4]),&(in->s390x.psw[12]), 4);
199 out->s390.psw[4] |= 0x80; /* set (31bit) addressing bit */
200 out->s390.pref_reg = in->s390x.pref_reg;
201 out->s390.timer = in->s390x.timer;
202 out->s390.clk_cmp = in->s390x.clk_cmp;
205 static void __init s390x_to_s390_save_areas(void)
208 static union save_area tmp;
210 while (zfcpdump_save_areas[i]) {
211 s390x_to_s390_regs(&tmp, zfcpdump_save_areas[i], i);
212 memcpy(zfcpdump_save_areas[i], &tmp, sizeof(tmp));
217 #endif /* __s390x__ */
219 static int __init init_cpu_info(enum arch_id arch)
223 /* get info for boot cpu from lowcore, stored in the HSA */
225 sa = kmalloc(sizeof(*sa), GFP_KERNEL);
227 ERROR_MSG("kmalloc failed: %s: %i\n",__FUNCTION__, __LINE__);
230 if (memcpy_hsa_kernel(sa, sys_info.sa_base, sys_info.sa_size) < 0) {
231 ERROR_MSG("could not copy from HSA\n");
235 zfcpdump_save_areas[0] = sa;
238 /* convert s390x regs to s390, if we are dumping an s390 Linux */
240 if (arch == ARCH_S390)
241 s390x_to_s390_save_areas();
247 static DEFINE_MUTEX(zcore_mutex);
249 #define DUMP_VERSION 0x3
250 #define DUMP_MAGIC 0xa8190173618f23fdULL
251 #define DUMP_ARCH_S390X 2
252 #define DUMP_ARCH_S390 1
253 #define HEADER_SIZE 4096
255 /* dump header dumped according to s390 crash dump format */
257 struct zcore_header {
275 } __attribute__((packed,__aligned__(16)));
277 static struct zcore_header zcore_header = {
279 .version = DUMP_VERSION,
282 .page_size = PAGE_SIZE,
285 .build_arch = DUMP_ARCH_S390X,
287 .build_arch = DUMP_ARCH_S390,
292 * Copy lowcore info to buffer. Use map in order to copy only register parts.
295 * @sa: Pointer to save area
296 * @sa_off: Offset in save area to copy
297 * @len: Number of bytes to copy
299 static int copy_lc(void __user *buf, void *sa, int sa_off, int len)
302 char *lc_mask = (char*)&sys_info.lc_mask;
304 for (i = 0; i < len; i++) {
305 if (!lc_mask[i + sa_off])
307 if (copy_to_user(buf + i, sa + sa_off + i, 1))
314 * Copy lowcores info to memory, if necessary
317 * @addr: Start address of buffer in dump memory
318 * @count: Size of buffer
320 static int zcore_add_lc(char __user *buf, unsigned long start, size_t count)
329 while (zfcpdump_save_areas[i]) {
330 unsigned long cp_start, cp_end; /* copy range */
331 unsigned long sa_start, sa_end; /* save area range */
332 unsigned long prefix;
333 unsigned long sa_off, len, buf_off;
335 if (sys_info.arch == ARCH_S390)
336 prefix = zfcpdump_save_areas[i]->s390.pref_reg;
338 prefix = zfcpdump_save_areas[i]->s390x.pref_reg;
340 sa_start = prefix + sys_info.sa_base;
341 sa_end = prefix + sys_info.sa_base + sys_info.sa_size;
343 if ((end < sa_start) || (start > sa_end))
345 cp_start = max(start, sa_start);
346 cp_end = min(end, sa_end);
348 buf_off = cp_start - start;
349 sa_off = cp_start - sa_start;
350 len = cp_end - cp_start;
352 TRACE("copy_lc for: %lx\n", start);
353 if (copy_lc(buf + buf_off, zfcpdump_save_areas[i], sa_off, len))
362 * Read routine for zcore character device
363 * First 4K are dump header
364 * Next 32MB are HSA Memory
365 * Rest is read from absolute Memory
367 static ssize_t zcore_read(struct file *file, char __user *buf, size_t count,
370 unsigned long mem_start; /* Start address in memory */
371 size_t mem_offs; /* Offset in dump memory */
372 size_t hdr_count; /* Size of header part of output buffer */
376 mutex_lock(&zcore_mutex);
378 if (*ppos > (sys_info.mem_size + HEADER_SIZE)) {
383 count = min(count, (size_t) (sys_info.mem_size + HEADER_SIZE - *ppos));
385 /* Copy dump header */
386 if (*ppos < HEADER_SIZE) {
387 size = min(count, (size_t) (HEADER_SIZE - *ppos));
388 if (copy_to_user(buf, &zcore_header + *ppos, size)) {
396 mem_start = *ppos - HEADER_SIZE;
401 /* Copy from HSA data */
402 if (*ppos < (ZFCPDUMP_HSA_SIZE + HEADER_SIZE)) {
403 size = min((count - hdr_count), (size_t) (ZFCPDUMP_HSA_SIZE
405 rc = memcpy_hsa_user(buf + hdr_count, mem_start, size);
412 /* Copy from real mem */
413 size = count - mem_offs - hdr_count;
414 rc = memcpy_real_user(buf + hdr_count + mem_offs, mem_start + mem_offs,
420 * Since s390 dump analysis tools like lcrash or crash
421 * expect register sets in the prefix pages of the cpus,
422 * we copy them into the read buffer, if necessary.
423 * buf + hdr_count: Start of memory part of output buffer
424 * mem_start: Start memory address to copy from
425 * count - hdr_count: Size of memory area to copy
427 if (zcore_add_lc(buf + hdr_count, mem_start, count - hdr_count)) {
433 mutex_unlock(&zcore_mutex);
434 return (rc < 0) ? rc : count;
437 static int zcore_open(struct inode *inode, struct file *filp)
442 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
445 static int zcore_release(struct inode *inode, struct file *filep)
447 diag308(DIAG308_REL_HSA, NULL);
452 static loff_t zcore_lseek(struct file *file, loff_t offset, int orig)
456 mutex_lock(&zcore_mutex);
459 file->f_pos = offset;
463 file->f_pos += offset;
469 mutex_unlock(&zcore_mutex);
473 static struct file_operations zcore_fops = {
474 .owner = THIS_MODULE,
475 .llseek = zcore_lseek,
478 .release = zcore_release,
482 static void __init set_s390_lc_mask(union save_area *map)
484 memset(&map->s390.ext_save, 0xff, sizeof(map->s390.ext_save));
485 memset(&map->s390.timer, 0xff, sizeof(map->s390.timer));
486 memset(&map->s390.clk_cmp, 0xff, sizeof(map->s390.clk_cmp));
487 memset(&map->s390.psw, 0xff, sizeof(map->s390.psw));
488 memset(&map->s390.pref_reg, 0xff, sizeof(map->s390.pref_reg));
489 memset(&map->s390.acc_regs, 0xff, sizeof(map->s390.acc_regs));
490 memset(&map->s390.fp_regs, 0xff, sizeof(map->s390.fp_regs));
491 memset(&map->s390.gp_regs, 0xff, sizeof(map->s390.gp_regs));
492 memset(&map->s390.ctrl_regs, 0xff, sizeof(map->s390.ctrl_regs));
495 static void __init set_s390x_lc_mask(union save_area *map)
497 memset(&map->s390x.fp_regs, 0xff, sizeof(map->s390x.fp_regs));
498 memset(&map->s390x.gp_regs, 0xff, sizeof(map->s390x.gp_regs));
499 memset(&map->s390x.psw, 0xff, sizeof(map->s390x.psw));
500 memset(&map->s390x.pref_reg, 0xff, sizeof(map->s390x.pref_reg));
501 memset(&map->s390x.fp_ctrl_reg, 0xff, sizeof(map->s390x.fp_ctrl_reg));
502 memset(&map->s390x.tod_reg, 0xff, sizeof(map->s390x.tod_reg));
503 memset(&map->s390x.timer, 0xff, sizeof(map->s390x.timer));
504 memset(&map->s390x.clk_cmp, 0xff, sizeof(map->s390x.clk_cmp));
505 memset(&map->s390x.acc_regs, 0xff, sizeof(map->s390x.acc_regs));
506 memset(&map->s390x.ctrl_regs, 0xff, sizeof(map->s390x.ctrl_regs));
510 * Initialize dump globals for a given architecture
512 static int __init sys_info_init(enum arch_id arch)
516 MSG("DETECTED 'S390X (64 bit) OS'\n");
517 sys_info.sa_base = SAVE_AREA_BASE_S390X;
518 sys_info.sa_size = sizeof(struct save_area_s390x);
519 set_s390x_lc_mask(&sys_info.lc_mask);
522 MSG("DETECTED 'S390 (32 bit) OS'\n");
523 sys_info.sa_base = SAVE_AREA_BASE_S390;
524 sys_info.sa_size = sizeof(struct save_area_s390);
525 set_s390_lc_mask(&sys_info.lc_mask);
528 ERROR_MSG("unknown architecture 0x%x.\n",arch);
531 sys_info.arch = arch;
532 if (init_cpu_info(arch)) {
533 ERROR_MSG("get cpu info failed\n");
536 sys_info.mem_size = real_memory_size;
541 static int __init check_sdias(void)
543 int rc, act_hsa_size;
545 rc = sclp_sdias_blk_count();
547 ERROR_MSG("Could not determine HSA size\n");
550 act_hsa_size = (rc - 1) * PAGE_SIZE;
551 if (act_hsa_size < ZFCPDUMP_HSA_SIZE) {
552 ERROR_MSG("HSA size too small: %i\n", act_hsa_size);
558 static void __init zcore_header_init(int arch, struct zcore_header *hdr)
560 if (arch == ARCH_S390X)
561 hdr->arch_id = DUMP_ARCH_S390X;
563 hdr->arch_id = DUMP_ARCH_S390;
564 hdr->mem_size = sys_info.mem_size;
565 hdr->rmem_size = sys_info.mem_size;
566 hdr->mem_end = sys_info.mem_size;
567 hdr->num_pages = sys_info.mem_size / PAGE_SIZE;
568 hdr->tod = get_clock();
569 get_cpu_id(&hdr->cpu_id);
572 static int __init zcore_init(void)
577 if (ipl_info.type != IPL_TYPE_FCP_DUMP)
580 zcore_dbf = debug_register("zcore", 4, 1, 4 * sizeof(long));
581 debug_register_view(zcore_dbf, &debug_sprintf_view);
582 debug_set_level(zcore_dbf, 6);
584 TRACE("devno: %x\n", ipl_info.data.fcp.dev_id.devno);
585 TRACE("wwpn: %llx\n", (unsigned long long) ipl_info.data.fcp.wwpn);
586 TRACE("lun: %llx\n", (unsigned long long) ipl_info.data.fcp.lun);
588 rc = sclp_sdias_init();
594 ERROR_MSG("Dump initialization failed\n");
598 rc = memcpy_hsa_kernel(&arch, __LC_AR_MODE_ID, 1);
600 ERROR_MSG("sdial memcpy for arch id failed\n");
605 if (arch == ARCH_S390X) {
606 ERROR_MSG("32 bit dumper can't dump 64 bit system!\n");
612 rc = sys_info_init(arch);
614 ERROR_MSG("arch init failed\n");
618 zcore_header_init(arch, &zcore_header);
620 zcore_dir = debugfs_create_dir("zcore" , NULL);
625 zcore_file = debugfs_create_file("mem", S_IRUSR, zcore_dir, NULL,
628 debugfs_remove(zcore_dir);
636 diag308(DIAG308_REL_HSA, NULL);
640 static void __exit zcore_exit(void)
642 debug_unregister(zcore_dbf);
644 diag308(DIAG308_REL_HSA, NULL);
647 MODULE_AUTHOR("Copyright IBM Corp. 2003,2007");
648 MODULE_DESCRIPTION("zcore module for zfcpdump support");
649 MODULE_LICENSE("GPL");
651 subsys_initcall(zcore_init);
652 module_exit(zcore_exit);