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/slab.h>
17 #include <linux/miscdevice.h>
18 #include <linux/debugfs.h>
19 #include <linux/module.h>
20 #include <linux/memblock.h>
22 #include <asm/asm-offsets.h>
25 #include <asm/setup.h>
26 #include <asm/uaccess.h>
27 #include <asm/debug.h>
28 #include <asm/processor.h>
29 #include <asm/irqflags.h>
30 #include <asm/checksum.h>
33 #define TRACE(x...) debug_sprintf_event(zcore_dbf, 1, x)
37 #define CHUNK_INFO_SIZE 34 /* 2 16-byte char, each followed by blank */
44 /* dump system info */
48 unsigned long sa_base;
51 unsigned long mem_size;
52 struct save_area lc_mask;
58 } __attribute__((packed));
60 static struct sys_info sys_info;
61 static struct debug_info *zcore_dbf;
62 static int hsa_available;
63 static struct dentry *zcore_dir;
64 static struct dentry *zcore_file;
65 static struct dentry *zcore_memmap_file;
66 static struct dentry *zcore_reipl_file;
67 static struct dentry *zcore_hsa_file;
68 static struct ipl_parameter_block *ipl_block;
71 * Copy memory from HSA to kernel or user memory (not reentrant):
73 * @dest: Kernel or user buffer where memory should be copied to
74 * @src: Start address within HSA where data should be copied
75 * @count: Size of buffer, which should be copied
76 * @mode: Either TO_KERNEL or TO_USER
78 int memcpy_hsa(void *dest, unsigned long src, size_t count, int mode)
81 static char buf[PAGE_SIZE] __attribute__((__aligned__(PAGE_SIZE)));
88 /* copy first block */
90 if ((src % PAGE_SIZE) != 0) {
91 blk_num = src / PAGE_SIZE + 2;
92 if (sclp_sdias_copy(buf, blk_num, 1)) {
93 TRACE("sclp_sdias_copy() failed\n");
96 offs = min((PAGE_SIZE - (src % PAGE_SIZE)), count);
97 if (mode == TO_USER) {
98 if (copy_to_user((__force __user void*) dest,
99 buf + (src % PAGE_SIZE), offs))
102 memcpy(dest, buf + (src % PAGE_SIZE), offs);
108 for (; (offs + PAGE_SIZE) <= count; offs += PAGE_SIZE) {
109 blk_num = (src + offs) / PAGE_SIZE + 2;
110 if (sclp_sdias_copy(buf, blk_num, 1)) {
111 TRACE("sclp_sdias_copy() failed\n");
114 if (mode == TO_USER) {
115 if (copy_to_user((__force __user void*) dest + offs,
119 memcpy(dest + offs, buf, PAGE_SIZE);
124 /* copy last block */
125 blk_num = (src + offs) / PAGE_SIZE + 2;
126 if (sclp_sdias_copy(buf, blk_num, 1)) {
127 TRACE("sclp_sdias_copy() failed\n");
130 if (mode == TO_USER) {
131 if (copy_to_user((__force __user void*) dest + offs, buf,
135 memcpy(dest + offs, buf, count - offs);
140 static int memcpy_hsa_user(void __user *dest, unsigned long src, size_t count)
142 return memcpy_hsa((void __force *) dest, src, count, TO_USER);
145 static int memcpy_hsa_kernel(void *dest, unsigned long src, size_t count)
147 return memcpy_hsa(dest, src, count, TO_KERNEL);
150 static int __init init_cpu_info(enum arch_id arch)
152 struct save_area *sa;
154 /* get info for boot cpu from lowcore, stored in the HSA */
156 sa = dump_save_area_create(0);
159 if (memcpy_hsa_kernel(sa, sys_info.sa_base, sys_info.sa_size) < 0) {
160 TRACE("could not copy from HSA\n");
167 static DEFINE_MUTEX(zcore_mutex);
169 #define DUMP_VERSION 0x5
170 #define DUMP_MAGIC 0xa8190173618f23fdULL
171 #define DUMP_ARCH_S390X 2
172 #define DUMP_ARCH_S390 1
173 #define HEADER_SIZE 4096
175 /* dump header dumped according to s390 crash dump format */
177 struct zcore_header {
197 u8 end_pad1[0x200-0x061];
199 u64 mvdump_zipl_time;
200 u8 end_pad2[0x800-0x210];
202 } __attribute__((packed,__aligned__(16)));
204 static struct zcore_header zcore_header = {
206 .version = DUMP_VERSION,
209 .page_size = PAGE_SIZE,
212 .build_arch = DUMP_ARCH_S390X,
214 .build_arch = DUMP_ARCH_S390,
219 * Copy lowcore info to buffer. Use map in order to copy only register parts.
222 * @sa: Pointer to save area
223 * @sa_off: Offset in save area to copy
224 * @len: Number of bytes to copy
226 static int copy_lc(void __user *buf, void *sa, int sa_off, int len)
229 char *lc_mask = (char*)&sys_info.lc_mask;
231 for (i = 0; i < len; i++) {
232 if (!lc_mask[i + sa_off])
234 if (copy_to_user(buf + i, sa + sa_off + i, 1))
241 * Copy lowcores info to memory, if necessary
244 * @addr: Start address of buffer in dump memory
245 * @count: Size of buffer
247 static int zcore_add_lc(char __user *buf, unsigned long start, size_t count)
256 for (i = 0; i < dump_save_areas.count; i++) {
257 unsigned long cp_start, cp_end; /* copy range */
258 unsigned long sa_start, sa_end; /* save area range */
259 unsigned long prefix;
260 unsigned long sa_off, len, buf_off;
261 struct save_area *save_area = dump_save_areas.areas[i];
263 prefix = save_area->pref_reg;
264 sa_start = prefix + sys_info.sa_base;
265 sa_end = prefix + sys_info.sa_base + sys_info.sa_size;
267 if ((end < sa_start) || (start > sa_end))
269 cp_start = max(start, sa_start);
270 cp_end = min(end, sa_end);
272 buf_off = cp_start - start;
273 sa_off = cp_start - sa_start;
274 len = cp_end - cp_start;
276 TRACE("copy_lc for: %lx\n", start);
277 if (copy_lc(buf + buf_off, save_area, sa_off, len))
286 static void release_hsa(void)
288 diag308(DIAG308_REL_HSA, NULL);
293 * Read routine for zcore character device
294 * First 4K are dump header
295 * Next 32MB are HSA Memory
296 * Rest is read from absolute Memory
298 static ssize_t zcore_read(struct file *file, char __user *buf, size_t count,
301 unsigned long mem_start; /* Start address in memory */
302 size_t mem_offs; /* Offset in dump memory */
303 size_t hdr_count; /* Size of header part of output buffer */
307 mutex_lock(&zcore_mutex);
309 if (*ppos > (sys_info.mem_size + HEADER_SIZE)) {
314 count = min(count, (size_t) (sys_info.mem_size + HEADER_SIZE - *ppos));
316 /* Copy dump header */
317 if (*ppos < HEADER_SIZE) {
318 size = min(count, (size_t) (HEADER_SIZE - *ppos));
319 if (copy_to_user(buf, &zcore_header + *ppos, size)) {
327 mem_start = *ppos - HEADER_SIZE;
332 /* Copy from HSA data */
333 if (*ppos < sclp_get_hsa_size() + HEADER_SIZE) {
334 size = min((count - hdr_count),
335 (size_t) (sclp_get_hsa_size() - mem_start));
336 rc = memcpy_hsa_user(buf + hdr_count, mem_start, size);
343 /* Copy from real mem */
344 size = count - mem_offs - hdr_count;
345 rc = copy_to_user_real(buf + hdr_count + mem_offs,
346 (void *) mem_start + mem_offs, size);
351 * Since s390 dump analysis tools like lcrash or crash
352 * expect register sets in the prefix pages of the cpus,
353 * we copy them into the read buffer, if necessary.
354 * buf + hdr_count: Start of memory part of output buffer
355 * mem_start: Start memory address to copy from
356 * count - hdr_count: Size of memory area to copy
358 if (zcore_add_lc(buf + hdr_count, mem_start, count - hdr_count)) {
364 mutex_unlock(&zcore_mutex);
365 return (rc < 0) ? rc : count;
368 static int zcore_open(struct inode *inode, struct file *filp)
373 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
376 static int zcore_release(struct inode *inode, struct file *filep)
383 static loff_t zcore_lseek(struct file *file, loff_t offset, int orig)
387 mutex_lock(&zcore_mutex);
390 file->f_pos = offset;
394 file->f_pos += offset;
400 mutex_unlock(&zcore_mutex);
404 static const struct file_operations zcore_fops = {
405 .owner = THIS_MODULE,
406 .llseek = zcore_lseek,
409 .release = zcore_release,
412 static ssize_t zcore_memmap_read(struct file *filp, char __user *buf,
413 size_t count, loff_t *ppos)
415 return simple_read_from_buffer(buf, count, ppos, filp->private_data,
416 memblock.memory.cnt * CHUNK_INFO_SIZE);
419 static int zcore_memmap_open(struct inode *inode, struct file *filp)
421 struct memblock_region *reg;
425 buf = kzalloc(memblock.memory.cnt * CHUNK_INFO_SIZE, GFP_KERNEL);
429 for_each_memblock(memory, reg) {
430 sprintf(buf + (i++ * CHUNK_INFO_SIZE), "%016llx %016llx ",
431 (unsigned long long) reg->base,
432 (unsigned long long) reg->size);
434 filp->private_data = buf;
435 return nonseekable_open(inode, filp);
438 static int zcore_memmap_release(struct inode *inode, struct file *filp)
440 kfree(filp->private_data);
444 static const struct file_operations zcore_memmap_fops = {
445 .owner = THIS_MODULE,
446 .read = zcore_memmap_read,
447 .open = zcore_memmap_open,
448 .release = zcore_memmap_release,
452 static ssize_t zcore_reipl_write(struct file *filp, const char __user *buf,
453 size_t count, loff_t *ppos)
456 diag308(DIAG308_SET, ipl_block);
457 diag308(DIAG308_IPL, NULL);
462 static int zcore_reipl_open(struct inode *inode, struct file *filp)
464 return nonseekable_open(inode, filp);
467 static int zcore_reipl_release(struct inode *inode, struct file *filp)
472 static const struct file_operations zcore_reipl_fops = {
473 .owner = THIS_MODULE,
474 .write = zcore_reipl_write,
475 .open = zcore_reipl_open,
476 .release = zcore_reipl_release,
480 static ssize_t zcore_hsa_read(struct file *filp, char __user *buf,
481 size_t count, loff_t *ppos)
486 snprintf(str, sizeof(str), "%lx\n", sclp_get_hsa_size());
488 snprintf(str, sizeof(str), "0\n");
489 return simple_read_from_buffer(buf, count, ppos, str, strlen(str));
492 static ssize_t zcore_hsa_write(struct file *filp, const char __user *buf,
493 size_t count, loff_t *ppos)
499 if (copy_from_user(&value, buf, 1))
507 static const struct file_operations zcore_hsa_fops = {
508 .owner = THIS_MODULE,
509 .write = zcore_hsa_write,
510 .read = zcore_hsa_read,
511 .open = nonseekable_open,
517 static void __init set_lc_mask(struct save_area *map)
519 memset(&map->ext_save, 0xff, sizeof(map->ext_save));
520 memset(&map->timer, 0xff, sizeof(map->timer));
521 memset(&map->clk_cmp, 0xff, sizeof(map->clk_cmp));
522 memset(&map->psw, 0xff, sizeof(map->psw));
523 memset(&map->pref_reg, 0xff, sizeof(map->pref_reg));
524 memset(&map->acc_regs, 0xff, sizeof(map->acc_regs));
525 memset(&map->fp_regs, 0xff, sizeof(map->fp_regs));
526 memset(&map->gp_regs, 0xff, sizeof(map->gp_regs));
527 memset(&map->ctrl_regs, 0xff, sizeof(map->ctrl_regs));
530 #else /* CONFIG_32BIT */
532 static void __init set_lc_mask(struct save_area *map)
534 memset(&map->fp_regs, 0xff, sizeof(map->fp_regs));
535 memset(&map->gp_regs, 0xff, sizeof(map->gp_regs));
536 memset(&map->psw, 0xff, sizeof(map->psw));
537 memset(&map->pref_reg, 0xff, sizeof(map->pref_reg));
538 memset(&map->fp_ctrl_reg, 0xff, sizeof(map->fp_ctrl_reg));
539 memset(&map->tod_reg, 0xff, sizeof(map->tod_reg));
540 memset(&map->timer, 0xff, sizeof(map->timer));
541 memset(&map->clk_cmp, 0xff, sizeof(map->clk_cmp));
542 memset(&map->acc_regs, 0xff, sizeof(map->acc_regs));
543 memset(&map->ctrl_regs, 0xff, sizeof(map->ctrl_regs));
546 #endif /* CONFIG_32BIT */
549 * Initialize dump globals for a given architecture
551 static int __init sys_info_init(enum arch_id arch, unsigned long mem_end)
557 pr_alert("DETECTED 'S390X (64 bit) OS'\n");
560 pr_alert("DETECTED 'S390 (32 bit) OS'\n");
563 pr_alert("0x%x is an unknown architecture.\n",arch);
566 sys_info.sa_base = SAVE_AREA_BASE;
567 sys_info.sa_size = sizeof(struct save_area);
568 sys_info.arch = arch;
569 set_lc_mask(&sys_info.lc_mask);
570 rc = init_cpu_info(arch);
573 sys_info.mem_size = mem_end;
578 static int __init check_sdias(void)
580 if (!sclp_get_hsa_size()) {
581 TRACE("Could not determine HSA size\n");
587 static int __init get_mem_info(unsigned long *mem, unsigned long *end)
589 struct memblock_region *reg;
591 for_each_memblock(memory, reg) {
593 *end = max_t(unsigned long, *end, reg->base + reg->size);
598 static void __init zcore_header_init(int arch, struct zcore_header *hdr,
599 unsigned long mem_size)
604 if (arch == ARCH_S390X)
605 hdr->arch_id = DUMP_ARCH_S390X;
607 hdr->arch_id = DUMP_ARCH_S390;
608 hdr->mem_size = mem_size;
609 hdr->rmem_size = mem_size;
610 hdr->mem_end = sys_info.mem_size;
611 hdr->num_pages = mem_size / PAGE_SIZE;
612 hdr->tod = get_tod_clock();
613 get_cpu_id(&hdr->cpu_id);
614 for (i = 0; i < dump_save_areas.count; i++) {
615 prefix = dump_save_areas.areas[i]->pref_reg;
619 hdr->lc_vec[hdr->cpu_cnt] = prefix;
625 * Provide IPL parameter information block from either HSA or memory
628 static int __init zcore_reipl_init(void)
630 struct ipib_info ipib_info;
633 rc = memcpy_hsa_kernel(&ipib_info, __LC_DUMP_REIPL, sizeof(ipib_info));
636 if (ipib_info.ipib == 0)
638 ipl_block = (void *) __get_free_page(GFP_KERNEL);
641 if (ipib_info.ipib < sclp_get_hsa_size())
642 rc = memcpy_hsa_kernel(ipl_block, ipib_info.ipib, PAGE_SIZE);
644 rc = memcpy_real(ipl_block, (void *) ipib_info.ipib, PAGE_SIZE);
645 if (rc || csum_partial(ipl_block, ipl_block->hdr.len, 0) !=
646 ipib_info.checksum) {
647 TRACE("Checksum does not match\n");
648 free_page((unsigned long) ipl_block);
654 static int __init zcore_init(void)
656 unsigned long mem_size, mem_end;
660 mem_size = mem_end = 0;
661 if (ipl_info.type != IPL_TYPE_FCP_DUMP)
666 zcore_dbf = debug_register("zcore", 4, 1, 4 * sizeof(long));
667 debug_register_view(zcore_dbf, &debug_sprintf_view);
668 debug_set_level(zcore_dbf, 6);
670 TRACE("devno: %x\n", ipl_info.data.fcp.dev_id.devno);
671 TRACE("wwpn: %llx\n", (unsigned long long) ipl_info.data.fcp.wwpn);
672 TRACE("lun: %llx\n", (unsigned long long) ipl_info.data.fcp.lun);
674 rc = sclp_sdias_init();
683 rc = memcpy_hsa_kernel(&arch, __LC_AR_MODE_ID, 1);
688 if (arch == ARCH_S390) {
689 pr_alert("The 64-bit dump tool cannot be used for a "
694 #else /* CONFIG_64BIT */
695 if (arch == ARCH_S390X) {
696 pr_alert("The 32-bit dump tool cannot be used for a "
701 #endif /* CONFIG_64BIT */
703 rc = get_mem_info(&mem_size, &mem_end);
707 rc = sys_info_init(arch, mem_end);
710 zcore_header_init(arch, &zcore_header, mem_size);
712 rc = zcore_reipl_init();
716 zcore_dir = debugfs_create_dir("zcore" , NULL);
721 zcore_file = debugfs_create_file("mem", S_IRUSR, zcore_dir, NULL,
727 zcore_memmap_file = debugfs_create_file("memmap", S_IRUSR, zcore_dir,
728 NULL, &zcore_memmap_fops);
729 if (!zcore_memmap_file) {
733 zcore_reipl_file = debugfs_create_file("reipl", S_IRUSR, zcore_dir,
734 NULL, &zcore_reipl_fops);
735 if (!zcore_reipl_file) {
737 goto fail_memmap_file;
739 zcore_hsa_file = debugfs_create_file("hsa", S_IRUSR|S_IWUSR, zcore_dir,
740 NULL, &zcore_hsa_fops);
741 if (!zcore_hsa_file) {
743 goto fail_reipl_file;
748 debugfs_remove(zcore_reipl_file);
750 debugfs_remove(zcore_memmap_file);
752 debugfs_remove(zcore_file);
754 debugfs_remove(zcore_dir);
756 diag308(DIAG308_REL_HSA, NULL);
760 static void __exit zcore_exit(void)
762 debug_unregister(zcore_dbf);
764 free_page((unsigned long) ipl_block);
765 debugfs_remove(zcore_hsa_file);
766 debugfs_remove(zcore_reipl_file);
767 debugfs_remove(zcore_memmap_file);
768 debugfs_remove(zcore_file);
769 debugfs_remove(zcore_dir);
770 diag308(DIAG308_REL_HSA, NULL);
773 MODULE_AUTHOR("Copyright IBM Corp. 2003,2008");
774 MODULE_DESCRIPTION("zcore module for zfcpdump support");
775 MODULE_LICENSE("GPL");
777 subsys_initcall(zcore_init);
778 module_exit(zcore_exit);