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,2008
9 * Author(s): Michael Holzheu
12 #define KMSG_COMPONENT "zdump"
13 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
15 #include <linux/init.h>
16 #include <linux/miscdevice.h>
17 #include <linux/debugfs.h>
18 #include <asm/asm-offsets.h>
21 #include <asm/setup.h>
23 #include <asm/uaccess.h>
24 #include <asm/debug.h>
25 #include <asm/processor.h>
26 #include <asm/irqflags.h>
27 #include <asm/checksum.h>
30 #define TRACE(x...) debug_sprintf_event(zcore_dbf, 1, x)
34 #define CHUNK_INFO_SIZE 34 /* 2 16-byte char, each followed by blank */
41 /* dump system info */
45 unsigned long sa_base;
48 unsigned long mem_size;
49 struct save_area lc_mask;
55 } __attribute__((packed));
57 static struct sys_info sys_info;
58 static struct debug_info *zcore_dbf;
59 static int hsa_available;
60 static struct dentry *zcore_dir;
61 static struct dentry *zcore_file;
62 static struct dentry *zcore_memmap_file;
63 static struct dentry *zcore_reipl_file;
64 static struct ipl_parameter_block *ipl_block;
67 * Copy memory from HSA to kernel or user memory (not reentrant):
69 * @dest: Kernel or user buffer where memory should be copied to
70 * @src: Start address within HSA where data should be copied
71 * @count: Size of buffer, which should be copied
72 * @mode: Either TO_KERNEL or TO_USER
74 static int memcpy_hsa(void *dest, unsigned long src, size_t count, int mode)
77 static char buf[PAGE_SIZE] __attribute__((__aligned__(PAGE_SIZE)));
82 /* copy first block */
84 if ((src % PAGE_SIZE) != 0) {
85 blk_num = src / PAGE_SIZE + 2;
86 if (sclp_sdias_copy(buf, blk_num, 1)) {
87 TRACE("sclp_sdias_copy() failed\n");
90 offs = min((PAGE_SIZE - (src % PAGE_SIZE)), count);
91 if (mode == TO_USER) {
92 if (copy_to_user((__force __user void*) dest,
93 buf + (src % PAGE_SIZE), offs))
96 memcpy(dest, buf + (src % PAGE_SIZE), offs);
102 for (; (offs + PAGE_SIZE) <= count; offs += PAGE_SIZE) {
103 blk_num = (src + offs) / PAGE_SIZE + 2;
104 if (sclp_sdias_copy(buf, blk_num, 1)) {
105 TRACE("sclp_sdias_copy() failed\n");
108 if (mode == TO_USER) {
109 if (copy_to_user((__force __user void*) dest + offs,
113 memcpy(dest + offs, buf, PAGE_SIZE);
118 /* copy last block */
119 blk_num = (src + offs) / PAGE_SIZE + 2;
120 if (sclp_sdias_copy(buf, blk_num, 1)) {
121 TRACE("sclp_sdias_copy() failed\n");
124 if (mode == TO_USER) {
125 if (copy_to_user((__force __user void*) dest + offs, buf,
129 memcpy(dest + offs, buf, count - offs);
134 static int memcpy_hsa_user(void __user *dest, unsigned long src, size_t count)
136 return memcpy_hsa((void __force *) dest, src, count, TO_USER);
139 static int memcpy_hsa_kernel(void *dest, unsigned long src, size_t count)
141 return memcpy_hsa(dest, src, count, TO_KERNEL);
144 static int memcpy_real(void *dest, unsigned long src, size_t count)
148 register unsigned long _dest asm("2") = (unsigned long) dest;
149 register unsigned long _len1 asm("3") = (unsigned long) count;
150 register unsigned long _src asm("4") = src;
151 register unsigned long _len2 asm("5") = (unsigned long) count;
155 flags = __raw_local_irq_stnsm(0xf8UL); /* switch to real mode */
157 "0: mvcle %1,%2,0x0\n"
162 : "+d" (rc), "+d" (_dest), "+d" (_src), "+d" (_len1),
163 "+d" (_len2), "=m" (*((long*)dest))
164 : "m" (*((long*)src))
166 __raw_local_irq_ssm(flags);
171 static int memcpy_real_user(void __user *dest, unsigned long src, size_t count)
173 static char buf[4096];
176 while (offs < count) {
177 size = min(sizeof(buf), count - offs);
178 if (memcpy_real(buf, src + offs, size))
180 if (copy_to_user(dest + offs, buf, size))
187 static int __init init_cpu_info(enum arch_id arch)
189 struct save_area *sa;
191 /* get info for boot cpu from lowcore, stored in the HSA */
193 sa = kmalloc(sizeof(*sa), GFP_KERNEL);
196 if (memcpy_hsa_kernel(sa, sys_info.sa_base, sys_info.sa_size) < 0) {
197 TRACE("could not copy from HSA\n");
201 zfcpdump_save_areas[0] = sa;
205 static DEFINE_MUTEX(zcore_mutex);
207 #define DUMP_VERSION 0x3
208 #define DUMP_MAGIC 0xa8190173618f23fdULL
209 #define DUMP_ARCH_S390X 2
210 #define DUMP_ARCH_S390 1
211 #define HEADER_SIZE 4096
213 /* dump header dumped according to s390 crash dump format */
215 struct zcore_header {
233 } __attribute__((packed,__aligned__(16)));
235 static struct zcore_header zcore_header = {
237 .version = DUMP_VERSION,
240 .page_size = PAGE_SIZE,
243 .build_arch = DUMP_ARCH_S390X,
245 .build_arch = DUMP_ARCH_S390,
250 * Copy lowcore info to buffer. Use map in order to copy only register parts.
253 * @sa: Pointer to save area
254 * @sa_off: Offset in save area to copy
255 * @len: Number of bytes to copy
257 static int copy_lc(void __user *buf, void *sa, int sa_off, int len)
260 char *lc_mask = (char*)&sys_info.lc_mask;
262 for (i = 0; i < len; i++) {
263 if (!lc_mask[i + sa_off])
265 if (copy_to_user(buf + i, sa + sa_off + i, 1))
272 * Copy lowcores info to memory, if necessary
275 * @addr: Start address of buffer in dump memory
276 * @count: Size of buffer
278 static int zcore_add_lc(char __user *buf, unsigned long start, size_t count)
287 while (zfcpdump_save_areas[i]) {
288 unsigned long cp_start, cp_end; /* copy range */
289 unsigned long sa_start, sa_end; /* save area range */
290 unsigned long prefix;
291 unsigned long sa_off, len, buf_off;
293 prefix = zfcpdump_save_areas[i]->pref_reg;
294 sa_start = prefix + sys_info.sa_base;
295 sa_end = prefix + sys_info.sa_base + sys_info.sa_size;
297 if ((end < sa_start) || (start > sa_end))
299 cp_start = max(start, sa_start);
300 cp_end = min(end, sa_end);
302 buf_off = cp_start - start;
303 sa_off = cp_start - sa_start;
304 len = cp_end - cp_start;
306 TRACE("copy_lc for: %lx\n", start);
307 if (copy_lc(buf + buf_off, zfcpdump_save_areas[i], sa_off, len))
316 * Read routine for zcore character device
317 * First 4K are dump header
318 * Next 32MB are HSA Memory
319 * Rest is read from absolute Memory
321 static ssize_t zcore_read(struct file *file, char __user *buf, size_t count,
324 unsigned long mem_start; /* Start address in memory */
325 size_t mem_offs; /* Offset in dump memory */
326 size_t hdr_count; /* Size of header part of output buffer */
330 mutex_lock(&zcore_mutex);
332 if (*ppos > (sys_info.mem_size + HEADER_SIZE)) {
337 count = min(count, (size_t) (sys_info.mem_size + HEADER_SIZE - *ppos));
339 /* Copy dump header */
340 if (*ppos < HEADER_SIZE) {
341 size = min(count, (size_t) (HEADER_SIZE - *ppos));
342 if (copy_to_user(buf, &zcore_header + *ppos, size)) {
350 mem_start = *ppos - HEADER_SIZE;
355 /* Copy from HSA data */
356 if (*ppos < (ZFCPDUMP_HSA_SIZE + HEADER_SIZE)) {
357 size = min((count - hdr_count), (size_t) (ZFCPDUMP_HSA_SIZE
359 rc = memcpy_hsa_user(buf + hdr_count, mem_start, size);
366 /* Copy from real mem */
367 size = count - mem_offs - hdr_count;
368 rc = memcpy_real_user(buf + hdr_count + mem_offs, mem_start + mem_offs,
374 * Since s390 dump analysis tools like lcrash or crash
375 * expect register sets in the prefix pages of the cpus,
376 * we copy them into the read buffer, if necessary.
377 * buf + hdr_count: Start of memory part of output buffer
378 * mem_start: Start memory address to copy from
379 * count - hdr_count: Size of memory area to copy
381 if (zcore_add_lc(buf + hdr_count, mem_start, count - hdr_count)) {
387 mutex_unlock(&zcore_mutex);
388 return (rc < 0) ? rc : count;
391 static int zcore_open(struct inode *inode, struct file *filp)
396 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
399 static int zcore_release(struct inode *inode, struct file *filep)
401 diag308(DIAG308_REL_HSA, NULL);
406 static loff_t zcore_lseek(struct file *file, loff_t offset, int orig)
410 mutex_lock(&zcore_mutex);
413 file->f_pos = offset;
417 file->f_pos += offset;
423 mutex_unlock(&zcore_mutex);
427 static const struct file_operations zcore_fops = {
428 .owner = THIS_MODULE,
429 .llseek = zcore_lseek,
432 .release = zcore_release,
435 static ssize_t zcore_memmap_read(struct file *filp, char __user *buf,
436 size_t count, loff_t *ppos)
438 return simple_read_from_buffer(buf, count, ppos, filp->private_data,
439 MEMORY_CHUNKS * CHUNK_INFO_SIZE);
442 static int zcore_memmap_open(struct inode *inode, struct file *filp)
446 struct mem_chunk *chunk_array;
448 chunk_array = kzalloc(MEMORY_CHUNKS * sizeof(struct mem_chunk),
452 detect_memory_layout(chunk_array);
453 buf = kzalloc(MEMORY_CHUNKS * CHUNK_INFO_SIZE, GFP_KERNEL);
458 for (i = 0; i < MEMORY_CHUNKS; i++) {
459 sprintf(buf + (i * CHUNK_INFO_SIZE), "%016llx %016llx ",
460 (unsigned long long) chunk_array[i].addr,
461 (unsigned long long) chunk_array[i].size);
462 if (chunk_array[i].size == 0)
466 filp->private_data = buf;
470 static int zcore_memmap_release(struct inode *inode, struct file *filp)
472 kfree(filp->private_data);
476 static const struct file_operations zcore_memmap_fops = {
477 .owner = THIS_MODULE,
478 .read = zcore_memmap_read,
479 .open = zcore_memmap_open,
480 .release = zcore_memmap_release,
483 static ssize_t zcore_reipl_write(struct file *filp, const char __user *buf,
484 size_t count, loff_t *ppos)
487 diag308(DIAG308_SET, ipl_block);
488 diag308(DIAG308_IPL, NULL);
493 static int zcore_reipl_open(struct inode *inode, struct file *filp)
498 static int zcore_reipl_release(struct inode *inode, struct file *filp)
503 static const struct file_operations zcore_reipl_fops = {
504 .owner = THIS_MODULE,
505 .write = zcore_reipl_write,
506 .open = zcore_reipl_open,
507 .release = zcore_reipl_release,
512 static void __init set_lc_mask(struct save_area *map)
514 memset(&map->ext_save, 0xff, sizeof(map->ext_save));
515 memset(&map->timer, 0xff, sizeof(map->timer));
516 memset(&map->clk_cmp, 0xff, sizeof(map->clk_cmp));
517 memset(&map->psw, 0xff, sizeof(map->psw));
518 memset(&map->pref_reg, 0xff, sizeof(map->pref_reg));
519 memset(&map->acc_regs, 0xff, sizeof(map->acc_regs));
520 memset(&map->fp_regs, 0xff, sizeof(map->fp_regs));
521 memset(&map->gp_regs, 0xff, sizeof(map->gp_regs));
522 memset(&map->ctrl_regs, 0xff, sizeof(map->ctrl_regs));
525 #else /* CONFIG_32BIT */
527 static void __init set_lc_mask(struct save_area *map)
529 memset(&map->fp_regs, 0xff, sizeof(map->fp_regs));
530 memset(&map->gp_regs, 0xff, sizeof(map->gp_regs));
531 memset(&map->psw, 0xff, sizeof(map->psw));
532 memset(&map->pref_reg, 0xff, sizeof(map->pref_reg));
533 memset(&map->fp_ctrl_reg, 0xff, sizeof(map->fp_ctrl_reg));
534 memset(&map->tod_reg, 0xff, sizeof(map->tod_reg));
535 memset(&map->timer, 0xff, sizeof(map->timer));
536 memset(&map->clk_cmp, 0xff, sizeof(map->clk_cmp));
537 memset(&map->acc_regs, 0xff, sizeof(map->acc_regs));
538 memset(&map->ctrl_regs, 0xff, sizeof(map->ctrl_regs));
541 #endif /* CONFIG_32BIT */
544 * Initialize dump globals for a given architecture
546 static int __init sys_info_init(enum arch_id arch)
552 pr_alert("DETECTED 'S390X (64 bit) OS'\n");
555 pr_alert("DETECTED 'S390 (32 bit) OS'\n");
558 pr_alert("0x%x is an unknown architecture.\n",arch);
561 sys_info.sa_base = SAVE_AREA_BASE;
562 sys_info.sa_size = sizeof(struct save_area);
563 sys_info.arch = arch;
564 set_lc_mask(&sys_info.lc_mask);
565 rc = init_cpu_info(arch);
568 sys_info.mem_size = real_memory_size;
573 static int __init check_sdias(void)
575 int rc, act_hsa_size;
577 rc = sclp_sdias_blk_count();
579 TRACE("Could not determine HSA size\n");
582 act_hsa_size = (rc - 1) * PAGE_SIZE;
583 if (act_hsa_size < ZFCPDUMP_HSA_SIZE) {
584 TRACE("HSA size too small: %i\n", act_hsa_size);
590 static int __init get_mem_size(unsigned long *mem)
593 struct mem_chunk *chunk_array;
595 chunk_array = kzalloc(MEMORY_CHUNKS * sizeof(struct mem_chunk),
599 detect_memory_layout(chunk_array);
600 for (i = 0; i < MEMORY_CHUNKS; i++) {
601 if (chunk_array[i].size == 0)
603 *mem += chunk_array[i].size;
609 static int __init zcore_header_init(int arch, struct zcore_header *hdr)
612 unsigned long memory = 0;
614 if (arch == ARCH_S390X)
615 hdr->arch_id = DUMP_ARCH_S390X;
617 hdr->arch_id = DUMP_ARCH_S390;
618 rc = get_mem_size(&memory);
621 hdr->mem_size = memory;
622 hdr->rmem_size = memory;
623 hdr->mem_end = sys_info.mem_size;
624 hdr->num_pages = memory / PAGE_SIZE;
625 hdr->tod = get_clock();
626 get_cpu_id(&hdr->cpu_id);
631 * Provide IPL parameter information block from either HSA or memory
634 static int __init zcore_reipl_init(void)
636 struct ipib_info ipib_info;
639 rc = memcpy_hsa_kernel(&ipib_info, __LC_DUMP_REIPL, sizeof(ipib_info));
642 if (ipib_info.ipib == 0)
644 ipl_block = (void *) __get_free_page(GFP_KERNEL);
647 if (ipib_info.ipib < ZFCPDUMP_HSA_SIZE)
648 rc = memcpy_hsa_kernel(ipl_block, ipib_info.ipib, PAGE_SIZE);
650 rc = memcpy_real(ipl_block, ipib_info.ipib, PAGE_SIZE);
652 free_page((unsigned long) ipl_block);
655 if (csum_partial(ipl_block, ipl_block->hdr.len, 0) !=
656 ipib_info.checksum) {
657 TRACE("Checksum does not match\n");
658 free_page((unsigned long) ipl_block);
664 static int __init zcore_init(void)
669 if (ipl_info.type != IPL_TYPE_FCP_DUMP)
672 zcore_dbf = debug_register("zcore", 4, 1, 4 * sizeof(long));
673 debug_register_view(zcore_dbf, &debug_sprintf_view);
674 debug_set_level(zcore_dbf, 6);
676 TRACE("devno: %x\n", ipl_info.data.fcp.dev_id.devno);
677 TRACE("wwpn: %llx\n", (unsigned long long) ipl_info.data.fcp.wwpn);
678 TRACE("lun: %llx\n", (unsigned long long) ipl_info.data.fcp.lun);
680 rc = sclp_sdias_init();
688 rc = memcpy_hsa_kernel(&arch, __LC_AR_MODE_ID, 1);
693 if (arch == ARCH_S390) {
694 pr_alert("The 64-bit dump tool cannot be used for a "
699 #else /* CONFIG_64BIT */
700 if (arch == ARCH_S390X) {
701 pr_alert("The 32-bit dump tool cannot be used for a "
706 #endif /* CONFIG_64BIT */
708 rc = sys_info_init(arch);
712 rc = zcore_header_init(arch, &zcore_header);
716 rc = zcore_reipl_init();
720 zcore_dir = debugfs_create_dir("zcore" , NULL);
725 zcore_file = debugfs_create_file("mem", S_IRUSR, zcore_dir, NULL,
731 zcore_memmap_file = debugfs_create_file("memmap", S_IRUSR, zcore_dir,
732 NULL, &zcore_memmap_fops);
733 if (!zcore_memmap_file) {
737 zcore_reipl_file = debugfs_create_file("reipl", S_IRUSR, zcore_dir,
738 NULL, &zcore_reipl_fops);
739 if (!zcore_reipl_file) {
741 goto fail_memmap_file;
747 debugfs_remove(zcore_memmap_file);
749 debugfs_remove(zcore_file);
751 debugfs_remove(zcore_dir);
753 diag308(DIAG308_REL_HSA, NULL);
757 static void __exit zcore_exit(void)
759 debug_unregister(zcore_dbf);
761 free_page((unsigned long) ipl_block);
762 debugfs_remove(zcore_reipl_file);
763 debugfs_remove(zcore_memmap_file);
764 debugfs_remove(zcore_file);
765 debugfs_remove(zcore_dir);
766 diag308(DIAG308_REL_HSA, NULL);
769 MODULE_AUTHOR("Copyright IBM Corp. 2003,2008");
770 MODULE_DESCRIPTION("zcore module for zfcpdump support");
771 MODULE_LICENSE("GPL");
773 subsys_initcall(zcore_init);
774 module_exit(zcore_exit);