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/utsname.h>
18 #include <linux/debugfs.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 union 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))
189 * Convert s390x (64 bit) cpu info to s390 (32 bit) cpu info
191 static void __init s390x_to_s390_regs(union save_area *out, union save_area *in,
196 for (i = 0; i < 16; i++) {
197 out->s390.gp_regs[i] = in->s390x.gp_regs[i] & 0x00000000ffffffff;
198 out->s390.acc_regs[i] = in->s390x.acc_regs[i];
199 out->s390.ctrl_regs[i] =
200 in->s390x.ctrl_regs[i] & 0x00000000ffffffff;
202 /* locore for 31 bit has only space for fpregs 0,2,4,6 */
203 out->s390.fp_regs[0] = in->s390x.fp_regs[0];
204 out->s390.fp_regs[1] = in->s390x.fp_regs[2];
205 out->s390.fp_regs[2] = in->s390x.fp_regs[4];
206 out->s390.fp_regs[3] = in->s390x.fp_regs[6];
207 memcpy(&(out->s390.psw[0]), &(in->s390x.psw[0]), 4);
208 out->s390.psw[1] |= 0x8; /* set bit 12 */
209 memcpy(&(out->s390.psw[4]),&(in->s390x.psw[12]), 4);
210 out->s390.psw[4] |= 0x80; /* set (31bit) addressing bit */
211 out->s390.pref_reg = in->s390x.pref_reg;
212 out->s390.timer = in->s390x.timer;
213 out->s390.clk_cmp = in->s390x.clk_cmp;
216 static void __init s390x_to_s390_save_areas(void)
219 static union save_area tmp;
221 while (zfcpdump_save_areas[i]) {
222 s390x_to_s390_regs(&tmp, zfcpdump_save_areas[i], i);
223 memcpy(zfcpdump_save_areas[i], &tmp, sizeof(tmp));
228 #endif /* __s390x__ */
230 static int __init init_cpu_info(enum arch_id arch)
234 /* get info for boot cpu from lowcore, stored in the HSA */
236 sa = kmalloc(sizeof(*sa), GFP_KERNEL);
239 if (memcpy_hsa_kernel(sa, sys_info.sa_base, sys_info.sa_size) < 0) {
240 TRACE("could not copy from HSA\n");
244 zfcpdump_save_areas[0] = sa;
247 /* convert s390x regs to s390, if we are dumping an s390 Linux */
249 if (arch == ARCH_S390)
250 s390x_to_s390_save_areas();
256 static DEFINE_MUTEX(zcore_mutex);
258 #define DUMP_VERSION 0x3
259 #define DUMP_MAGIC 0xa8190173618f23fdULL
260 #define DUMP_ARCH_S390X 2
261 #define DUMP_ARCH_S390 1
262 #define HEADER_SIZE 4096
264 /* dump header dumped according to s390 crash dump format */
266 struct zcore_header {
284 } __attribute__((packed,__aligned__(16)));
286 static struct zcore_header zcore_header = {
288 .version = DUMP_VERSION,
291 .page_size = PAGE_SIZE,
294 .build_arch = DUMP_ARCH_S390X,
296 .build_arch = DUMP_ARCH_S390,
301 * Copy lowcore info to buffer. Use map in order to copy only register parts.
304 * @sa: Pointer to save area
305 * @sa_off: Offset in save area to copy
306 * @len: Number of bytes to copy
308 static int copy_lc(void __user *buf, void *sa, int sa_off, int len)
311 char *lc_mask = (char*)&sys_info.lc_mask;
313 for (i = 0; i < len; i++) {
314 if (!lc_mask[i + sa_off])
316 if (copy_to_user(buf + i, sa + sa_off + i, 1))
323 * Copy lowcores info to memory, if necessary
326 * @addr: Start address of buffer in dump memory
327 * @count: Size of buffer
329 static int zcore_add_lc(char __user *buf, unsigned long start, size_t count)
338 while (zfcpdump_save_areas[i]) {
339 unsigned long cp_start, cp_end; /* copy range */
340 unsigned long sa_start, sa_end; /* save area range */
341 unsigned long prefix;
342 unsigned long sa_off, len, buf_off;
344 if (sys_info.arch == ARCH_S390)
345 prefix = zfcpdump_save_areas[i]->s390.pref_reg;
347 prefix = zfcpdump_save_areas[i]->s390x.pref_reg;
349 sa_start = prefix + sys_info.sa_base;
350 sa_end = prefix + sys_info.sa_base + sys_info.sa_size;
352 if ((end < sa_start) || (start > sa_end))
354 cp_start = max(start, sa_start);
355 cp_end = min(end, sa_end);
357 buf_off = cp_start - start;
358 sa_off = cp_start - sa_start;
359 len = cp_end - cp_start;
361 TRACE("copy_lc for: %lx\n", start);
362 if (copy_lc(buf + buf_off, zfcpdump_save_areas[i], sa_off, len))
371 * Read routine for zcore character device
372 * First 4K are dump header
373 * Next 32MB are HSA Memory
374 * Rest is read from absolute Memory
376 static ssize_t zcore_read(struct file *file, char __user *buf, size_t count,
379 unsigned long mem_start; /* Start address in memory */
380 size_t mem_offs; /* Offset in dump memory */
381 size_t hdr_count; /* Size of header part of output buffer */
385 mutex_lock(&zcore_mutex);
387 if (*ppos > (sys_info.mem_size + HEADER_SIZE)) {
392 count = min(count, (size_t) (sys_info.mem_size + HEADER_SIZE - *ppos));
394 /* Copy dump header */
395 if (*ppos < HEADER_SIZE) {
396 size = min(count, (size_t) (HEADER_SIZE - *ppos));
397 if (copy_to_user(buf, &zcore_header + *ppos, size)) {
405 mem_start = *ppos - HEADER_SIZE;
410 /* Copy from HSA data */
411 if (*ppos < (ZFCPDUMP_HSA_SIZE + HEADER_SIZE)) {
412 size = min((count - hdr_count), (size_t) (ZFCPDUMP_HSA_SIZE
414 rc = memcpy_hsa_user(buf + hdr_count, mem_start, size);
421 /* Copy from real mem */
422 size = count - mem_offs - hdr_count;
423 rc = memcpy_real_user(buf + hdr_count + mem_offs, mem_start + mem_offs,
429 * Since s390 dump analysis tools like lcrash or crash
430 * expect register sets in the prefix pages of the cpus,
431 * we copy them into the read buffer, if necessary.
432 * buf + hdr_count: Start of memory part of output buffer
433 * mem_start: Start memory address to copy from
434 * count - hdr_count: Size of memory area to copy
436 if (zcore_add_lc(buf + hdr_count, mem_start, count - hdr_count)) {
442 mutex_unlock(&zcore_mutex);
443 return (rc < 0) ? rc : count;
446 static int zcore_open(struct inode *inode, struct file *filp)
451 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
454 static int zcore_release(struct inode *inode, struct file *filep)
456 diag308(DIAG308_REL_HSA, NULL);
461 static loff_t zcore_lseek(struct file *file, loff_t offset, int orig)
465 mutex_lock(&zcore_mutex);
468 file->f_pos = offset;
472 file->f_pos += offset;
478 mutex_unlock(&zcore_mutex);
482 static const struct file_operations zcore_fops = {
483 .owner = THIS_MODULE,
484 .llseek = zcore_lseek,
487 .release = zcore_release,
490 static ssize_t zcore_memmap_read(struct file *filp, char __user *buf,
491 size_t count, loff_t *ppos)
493 return simple_read_from_buffer(buf, count, ppos, filp->private_data,
494 MEMORY_CHUNKS * CHUNK_INFO_SIZE);
497 static int zcore_memmap_open(struct inode *inode, struct file *filp)
501 struct mem_chunk *chunk_array;
503 chunk_array = kzalloc(MEMORY_CHUNKS * sizeof(struct mem_chunk),
507 detect_memory_layout(chunk_array);
508 buf = kzalloc(MEMORY_CHUNKS * CHUNK_INFO_SIZE, GFP_KERNEL);
513 for (i = 0; i < MEMORY_CHUNKS; i++) {
514 sprintf(buf + (i * CHUNK_INFO_SIZE), "%016llx %016llx ",
515 (unsigned long long) chunk_array[i].addr,
516 (unsigned long long) chunk_array[i].size);
517 if (chunk_array[i].size == 0)
521 filp->private_data = buf;
525 static int zcore_memmap_release(struct inode *inode, struct file *filp)
527 kfree(filp->private_data);
531 static const struct file_operations zcore_memmap_fops = {
532 .owner = THIS_MODULE,
533 .read = zcore_memmap_read,
534 .open = zcore_memmap_open,
535 .release = zcore_memmap_release,
538 static ssize_t zcore_reipl_write(struct file *filp, const char __user *buf,
539 size_t count, loff_t *ppos)
542 diag308(DIAG308_SET, ipl_block);
543 diag308(DIAG308_IPL, NULL);
548 static int zcore_reipl_open(struct inode *inode, struct file *filp)
553 static int zcore_reipl_release(struct inode *inode, struct file *filp)
558 static const struct file_operations zcore_reipl_fops = {
559 .owner = THIS_MODULE,
560 .write = zcore_reipl_write,
561 .open = zcore_reipl_open,
562 .release = zcore_reipl_release,
566 static void __init set_s390_lc_mask(union save_area *map)
568 memset(&map->s390.ext_save, 0xff, sizeof(map->s390.ext_save));
569 memset(&map->s390.timer, 0xff, sizeof(map->s390.timer));
570 memset(&map->s390.clk_cmp, 0xff, sizeof(map->s390.clk_cmp));
571 memset(&map->s390.psw, 0xff, sizeof(map->s390.psw));
572 memset(&map->s390.pref_reg, 0xff, sizeof(map->s390.pref_reg));
573 memset(&map->s390.acc_regs, 0xff, sizeof(map->s390.acc_regs));
574 memset(&map->s390.fp_regs, 0xff, sizeof(map->s390.fp_regs));
575 memset(&map->s390.gp_regs, 0xff, sizeof(map->s390.gp_regs));
576 memset(&map->s390.ctrl_regs, 0xff, sizeof(map->s390.ctrl_regs));
579 static void __init set_s390x_lc_mask(union save_area *map)
581 memset(&map->s390x.fp_regs, 0xff, sizeof(map->s390x.fp_regs));
582 memset(&map->s390x.gp_regs, 0xff, sizeof(map->s390x.gp_regs));
583 memset(&map->s390x.psw, 0xff, sizeof(map->s390x.psw));
584 memset(&map->s390x.pref_reg, 0xff, sizeof(map->s390x.pref_reg));
585 memset(&map->s390x.fp_ctrl_reg, 0xff, sizeof(map->s390x.fp_ctrl_reg));
586 memset(&map->s390x.tod_reg, 0xff, sizeof(map->s390x.tod_reg));
587 memset(&map->s390x.timer, 0xff, sizeof(map->s390x.timer));
588 memset(&map->s390x.clk_cmp, 0xff, sizeof(map->s390x.clk_cmp));
589 memset(&map->s390x.acc_regs, 0xff, sizeof(map->s390x.acc_regs));
590 memset(&map->s390x.ctrl_regs, 0xff, sizeof(map->s390x.ctrl_regs));
594 * Initialize dump globals for a given architecture
596 static int __init sys_info_init(enum arch_id arch)
602 pr_alert("DETECTED 'S390X (64 bit) OS'\n");
603 sys_info.sa_base = SAVE_AREA_BASE_S390X;
604 sys_info.sa_size = sizeof(struct save_area_s390x);
605 set_s390x_lc_mask(&sys_info.lc_mask);
608 pr_alert("DETECTED 'S390 (32 bit) OS'\n");
609 sys_info.sa_base = SAVE_AREA_BASE_S390;
610 sys_info.sa_size = sizeof(struct save_area_s390);
611 set_s390_lc_mask(&sys_info.lc_mask);
614 pr_alert("0x%x is an unknown architecture.\n",arch);
617 sys_info.arch = arch;
618 rc = init_cpu_info(arch);
621 sys_info.mem_size = real_memory_size;
626 static int __init check_sdias(void)
628 int rc, act_hsa_size;
630 rc = sclp_sdias_blk_count();
632 TRACE("Could not determine HSA size\n");
635 act_hsa_size = (rc - 1) * PAGE_SIZE;
636 if (act_hsa_size < ZFCPDUMP_HSA_SIZE) {
637 TRACE("HSA size too small: %i\n", act_hsa_size);
643 static int __init get_mem_size(unsigned long *mem)
646 struct mem_chunk *chunk_array;
648 chunk_array = kzalloc(MEMORY_CHUNKS * sizeof(struct mem_chunk),
652 detect_memory_layout(chunk_array);
653 for (i = 0; i < MEMORY_CHUNKS; i++) {
654 if (chunk_array[i].size == 0)
656 *mem += chunk_array[i].size;
662 static int __init zcore_header_init(int arch, struct zcore_header *hdr)
665 unsigned long memory = 0;
667 if (arch == ARCH_S390X)
668 hdr->arch_id = DUMP_ARCH_S390X;
670 hdr->arch_id = DUMP_ARCH_S390;
671 rc = get_mem_size(&memory);
674 hdr->mem_size = memory;
675 hdr->rmem_size = memory;
676 hdr->mem_end = sys_info.mem_size;
677 hdr->num_pages = memory / PAGE_SIZE;
678 hdr->tod = get_clock();
679 get_cpu_id(&hdr->cpu_id);
684 * Provide IPL parameter information block from either HSA or memory
687 static int __init zcore_reipl_init(void)
689 struct ipib_info ipib_info;
692 rc = memcpy_hsa_kernel(&ipib_info, __LC_DUMP_REIPL, sizeof(ipib_info));
695 if (ipib_info.ipib == 0)
697 ipl_block = (void *) __get_free_page(GFP_KERNEL);
700 if (ipib_info.ipib < ZFCPDUMP_HSA_SIZE)
701 rc = memcpy_hsa_kernel(ipl_block, ipib_info.ipib, PAGE_SIZE);
703 rc = memcpy_real(ipl_block, ipib_info.ipib, PAGE_SIZE);
705 free_page((unsigned long) ipl_block);
708 if (csum_partial(ipl_block, ipl_block->hdr.len, 0) !=
709 ipib_info.checksum) {
710 TRACE("Checksum does not match\n");
711 free_page((unsigned long) ipl_block);
717 static int __init zcore_init(void)
722 if (ipl_info.type != IPL_TYPE_FCP_DUMP)
725 zcore_dbf = debug_register("zcore", 4, 1, 4 * sizeof(long));
726 debug_register_view(zcore_dbf, &debug_sprintf_view);
727 debug_set_level(zcore_dbf, 6);
729 TRACE("devno: %x\n", ipl_info.data.fcp.dev_id.devno);
730 TRACE("wwpn: %llx\n", (unsigned long long) ipl_info.data.fcp.wwpn);
731 TRACE("lun: %llx\n", (unsigned long long) ipl_info.data.fcp.lun);
733 rc = sclp_sdias_init();
741 rc = memcpy_hsa_kernel(&arch, __LC_AR_MODE_ID, 1);
746 if (arch == ARCH_S390X) {
747 pr_alert("The 32-bit dump tool cannot be used for a "
754 rc = sys_info_init(arch);
758 rc = zcore_header_init(arch, &zcore_header);
762 rc = zcore_reipl_init();
766 zcore_dir = debugfs_create_dir("zcore" , NULL);
771 zcore_file = debugfs_create_file("mem", S_IRUSR, zcore_dir, NULL,
777 zcore_memmap_file = debugfs_create_file("memmap", S_IRUSR, zcore_dir,
778 NULL, &zcore_memmap_fops);
779 if (!zcore_memmap_file) {
783 zcore_reipl_file = debugfs_create_file("reipl", S_IRUSR, zcore_dir,
784 NULL, &zcore_reipl_fops);
785 if (!zcore_reipl_file) {
787 goto fail_memmap_file;
793 debugfs_remove(zcore_memmap_file);
795 debugfs_remove(zcore_file);
797 debugfs_remove(zcore_dir);
799 diag308(DIAG308_REL_HSA, NULL);
803 static void __exit zcore_exit(void)
805 debug_unregister(zcore_dbf);
807 free_page((unsigned long) ipl_block);
808 debugfs_remove(zcore_reipl_file);
809 debugfs_remove(zcore_memmap_file);
810 debugfs_remove(zcore_file);
811 debugfs_remove(zcore_dir);
812 diag308(DIAG308_REL_HSA, NULL);
815 MODULE_AUTHOR("Copyright IBM Corp. 2003,2008");
816 MODULE_DESCRIPTION("zcore module for zfcpdump support");
817 MODULE_LICENSE("GPL");
819 subsys_initcall(zcore_init);
820 module_exit(zcore_exit);