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>
20 #include <asm/setup.h>
22 #include <asm/uaccess.h>
23 #include <asm/debug.h>
24 #include <asm/processor.h>
25 #include <asm/irqflags.h>
26 #include <asm/checksum.h>
29 #define TRACE(x...) debug_sprintf_event(zcore_dbf, 1, x)
33 #define CHUNK_INFO_SIZE 34 /* 2 16-byte char, each followed by blank */
40 /* dump system info */
44 unsigned long sa_base;
47 unsigned long mem_size;
48 union save_area lc_mask;
54 } __attribute__((packed));
56 static struct sys_info sys_info;
57 static struct debug_info *zcore_dbf;
58 static int hsa_available;
59 static struct dentry *zcore_dir;
60 static struct dentry *zcore_file;
61 static struct dentry *zcore_memmap_file;
62 static struct dentry *zcore_reipl_file;
63 static struct ipl_parameter_block *ipl_block;
66 * Copy memory from HSA to kernel or user memory (not reentrant):
68 * @dest: Kernel or user buffer where memory should be copied to
69 * @src: Start address within HSA where data should be copied
70 * @count: Size of buffer, which should be copied
71 * @mode: Either TO_KERNEL or TO_USER
73 static int memcpy_hsa(void *dest, unsigned long src, size_t count, int mode)
76 static char buf[PAGE_SIZE] __attribute__((__aligned__(PAGE_SIZE)));
81 /* copy first block */
83 if ((src % PAGE_SIZE) != 0) {
84 blk_num = src / PAGE_SIZE + 2;
85 if (sclp_sdias_copy(buf, blk_num, 1)) {
86 TRACE("sclp_sdias_copy() failed\n");
89 offs = min((PAGE_SIZE - (src % PAGE_SIZE)), count);
90 if (mode == TO_USER) {
91 if (copy_to_user((__force __user void*) dest,
92 buf + (src % PAGE_SIZE), offs))
95 memcpy(dest, buf + (src % PAGE_SIZE), offs);
101 for (; (offs + PAGE_SIZE) <= count; offs += PAGE_SIZE) {
102 blk_num = (src + offs) / PAGE_SIZE + 2;
103 if (sclp_sdias_copy(buf, blk_num, 1)) {
104 TRACE("sclp_sdias_copy() failed\n");
107 if (mode == TO_USER) {
108 if (copy_to_user((__force __user void*) dest + offs,
112 memcpy(dest + offs, buf, PAGE_SIZE);
117 /* copy last block */
118 blk_num = (src + offs) / PAGE_SIZE + 2;
119 if (sclp_sdias_copy(buf, blk_num, 1)) {
120 TRACE("sclp_sdias_copy() failed\n");
123 if (mode == TO_USER) {
124 if (copy_to_user((__force __user void*) dest + offs, buf,
128 memcpy(dest + offs, buf, count - offs);
133 static int memcpy_hsa_user(void __user *dest, unsigned long src, size_t count)
135 return memcpy_hsa((void __force *) dest, src, count, TO_USER);
138 static int memcpy_hsa_kernel(void *dest, unsigned long src, size_t count)
140 return memcpy_hsa(dest, src, count, TO_KERNEL);
143 static int memcpy_real(void *dest, unsigned long src, size_t count)
147 register unsigned long _dest asm("2") = (unsigned long) dest;
148 register unsigned long _len1 asm("3") = (unsigned long) count;
149 register unsigned long _src asm("4") = src;
150 register unsigned long _len2 asm("5") = (unsigned long) count;
154 flags = __raw_local_irq_stnsm(0xf8UL); /* switch to real mode */
156 "0: mvcle %1,%2,0x0\n"
161 : "+d" (rc), "+d" (_dest), "+d" (_src), "+d" (_len1),
162 "+d" (_len2), "=m" (*((long*)dest))
163 : "m" (*((long*)src))
165 __raw_local_irq_ssm(flags);
170 static int memcpy_real_user(void __user *dest, unsigned long src, size_t count)
172 static char buf[4096];
175 while (offs < count) {
176 size = min(sizeof(buf), count - offs);
177 if (memcpy_real(buf, src + offs, size))
179 if (copy_to_user(dest + offs, buf, size))
188 * Convert s390x (64 bit) cpu info to s390 (32 bit) cpu info
190 static void __init s390x_to_s390_regs(union save_area *out, union save_area *in,
195 for (i = 0; i < 16; i++) {
196 out->s390.gp_regs[i] = in->s390x.gp_regs[i] & 0x00000000ffffffff;
197 out->s390.acc_regs[i] = in->s390x.acc_regs[i];
198 out->s390.ctrl_regs[i] =
199 in->s390x.ctrl_regs[i] & 0x00000000ffffffff;
201 /* locore for 31 bit has only space for fpregs 0,2,4,6 */
202 out->s390.fp_regs[0] = in->s390x.fp_regs[0];
203 out->s390.fp_regs[1] = in->s390x.fp_regs[2];
204 out->s390.fp_regs[2] = in->s390x.fp_regs[4];
205 out->s390.fp_regs[3] = in->s390x.fp_regs[6];
206 memcpy(&(out->s390.psw[0]), &(in->s390x.psw[0]), 4);
207 out->s390.psw[1] |= 0x8; /* set bit 12 */
208 memcpy(&(out->s390.psw[4]),&(in->s390x.psw[12]), 4);
209 out->s390.psw[4] |= 0x80; /* set (31bit) addressing bit */
210 out->s390.pref_reg = in->s390x.pref_reg;
211 out->s390.timer = in->s390x.timer;
212 out->s390.clk_cmp = in->s390x.clk_cmp;
215 static void __init s390x_to_s390_save_areas(void)
218 static union save_area tmp;
220 while (zfcpdump_save_areas[i]) {
221 s390x_to_s390_regs(&tmp, zfcpdump_save_areas[i], i);
222 memcpy(zfcpdump_save_areas[i], &tmp, sizeof(tmp));
227 #endif /* __s390x__ */
229 static int __init init_cpu_info(enum arch_id arch)
233 /* get info for boot cpu from lowcore, stored in the HSA */
235 sa = kmalloc(sizeof(*sa), GFP_KERNEL);
238 if (memcpy_hsa_kernel(sa, sys_info.sa_base, sys_info.sa_size) < 0) {
239 TRACE("could not copy from HSA\n");
243 zfcpdump_save_areas[0] = sa;
246 /* convert s390x regs to s390, if we are dumping an s390 Linux */
248 if (arch == ARCH_S390)
249 s390x_to_s390_save_areas();
255 static DEFINE_MUTEX(zcore_mutex);
257 #define DUMP_VERSION 0x3
258 #define DUMP_MAGIC 0xa8190173618f23fdULL
259 #define DUMP_ARCH_S390X 2
260 #define DUMP_ARCH_S390 1
261 #define HEADER_SIZE 4096
263 /* dump header dumped according to s390 crash dump format */
265 struct zcore_header {
283 } __attribute__((packed,__aligned__(16)));
285 static struct zcore_header zcore_header = {
287 .version = DUMP_VERSION,
290 .page_size = PAGE_SIZE,
293 .build_arch = DUMP_ARCH_S390X,
295 .build_arch = DUMP_ARCH_S390,
300 * Copy lowcore info to buffer. Use map in order to copy only register parts.
303 * @sa: Pointer to save area
304 * @sa_off: Offset in save area to copy
305 * @len: Number of bytes to copy
307 static int copy_lc(void __user *buf, void *sa, int sa_off, int len)
310 char *lc_mask = (char*)&sys_info.lc_mask;
312 for (i = 0; i < len; i++) {
313 if (!lc_mask[i + sa_off])
315 if (copy_to_user(buf + i, sa + sa_off + i, 1))
322 * Copy lowcores info to memory, if necessary
325 * @addr: Start address of buffer in dump memory
326 * @count: Size of buffer
328 static int zcore_add_lc(char __user *buf, unsigned long start, size_t count)
337 while (zfcpdump_save_areas[i]) {
338 unsigned long cp_start, cp_end; /* copy range */
339 unsigned long sa_start, sa_end; /* save area range */
340 unsigned long prefix;
341 unsigned long sa_off, len, buf_off;
343 if (sys_info.arch == ARCH_S390)
344 prefix = zfcpdump_save_areas[i]->s390.pref_reg;
346 prefix = zfcpdump_save_areas[i]->s390x.pref_reg;
348 sa_start = prefix + sys_info.sa_base;
349 sa_end = prefix + sys_info.sa_base + sys_info.sa_size;
351 if ((end < sa_start) || (start > sa_end))
353 cp_start = max(start, sa_start);
354 cp_end = min(end, sa_end);
356 buf_off = cp_start - start;
357 sa_off = cp_start - sa_start;
358 len = cp_end - cp_start;
360 TRACE("copy_lc for: %lx\n", start);
361 if (copy_lc(buf + buf_off, zfcpdump_save_areas[i], sa_off, len))
370 * Read routine for zcore character device
371 * First 4K are dump header
372 * Next 32MB are HSA Memory
373 * Rest is read from absolute Memory
375 static ssize_t zcore_read(struct file *file, char __user *buf, size_t count,
378 unsigned long mem_start; /* Start address in memory */
379 size_t mem_offs; /* Offset in dump memory */
380 size_t hdr_count; /* Size of header part of output buffer */
384 mutex_lock(&zcore_mutex);
386 if (*ppos > (sys_info.mem_size + HEADER_SIZE)) {
391 count = min(count, (size_t) (sys_info.mem_size + HEADER_SIZE - *ppos));
393 /* Copy dump header */
394 if (*ppos < HEADER_SIZE) {
395 size = min(count, (size_t) (HEADER_SIZE - *ppos));
396 if (copy_to_user(buf, &zcore_header + *ppos, size)) {
404 mem_start = *ppos - HEADER_SIZE;
409 /* Copy from HSA data */
410 if (*ppos < (ZFCPDUMP_HSA_SIZE + HEADER_SIZE)) {
411 size = min((count - hdr_count), (size_t) (ZFCPDUMP_HSA_SIZE
413 rc = memcpy_hsa_user(buf + hdr_count, mem_start, size);
420 /* Copy from real mem */
421 size = count - mem_offs - hdr_count;
422 rc = memcpy_real_user(buf + hdr_count + mem_offs, mem_start + mem_offs,
428 * Since s390 dump analysis tools like lcrash or crash
429 * expect register sets in the prefix pages of the cpus,
430 * we copy them into the read buffer, if necessary.
431 * buf + hdr_count: Start of memory part of output buffer
432 * mem_start: Start memory address to copy from
433 * count - hdr_count: Size of memory area to copy
435 if (zcore_add_lc(buf + hdr_count, mem_start, count - hdr_count)) {
441 mutex_unlock(&zcore_mutex);
442 return (rc < 0) ? rc : count;
445 static int zcore_open(struct inode *inode, struct file *filp)
450 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
453 static int zcore_release(struct inode *inode, struct file *filep)
455 diag308(DIAG308_REL_HSA, NULL);
460 static loff_t zcore_lseek(struct file *file, loff_t offset, int orig)
464 mutex_lock(&zcore_mutex);
467 file->f_pos = offset;
471 file->f_pos += offset;
477 mutex_unlock(&zcore_mutex);
481 static const struct file_operations zcore_fops = {
482 .owner = THIS_MODULE,
483 .llseek = zcore_lseek,
486 .release = zcore_release,
489 static ssize_t zcore_memmap_read(struct file *filp, char __user *buf,
490 size_t count, loff_t *ppos)
492 return simple_read_from_buffer(buf, count, ppos, filp->private_data,
493 MEMORY_CHUNKS * CHUNK_INFO_SIZE);
496 static int zcore_memmap_open(struct inode *inode, struct file *filp)
500 struct mem_chunk *chunk_array;
502 chunk_array = kzalloc(MEMORY_CHUNKS * sizeof(struct mem_chunk),
506 detect_memory_layout(chunk_array);
507 buf = kzalloc(MEMORY_CHUNKS * CHUNK_INFO_SIZE, GFP_KERNEL);
512 for (i = 0; i < MEMORY_CHUNKS; i++) {
513 sprintf(buf + (i * CHUNK_INFO_SIZE), "%016llx %016llx ",
514 (unsigned long long) chunk_array[i].addr,
515 (unsigned long long) chunk_array[i].size);
516 if (chunk_array[i].size == 0)
520 filp->private_data = buf;
524 static int zcore_memmap_release(struct inode *inode, struct file *filp)
526 kfree(filp->private_data);
530 static const struct file_operations zcore_memmap_fops = {
531 .owner = THIS_MODULE,
532 .read = zcore_memmap_read,
533 .open = zcore_memmap_open,
534 .release = zcore_memmap_release,
537 static ssize_t zcore_reipl_write(struct file *filp, const char __user *buf,
538 size_t count, loff_t *ppos)
541 diag308(DIAG308_SET, ipl_block);
542 diag308(DIAG308_IPL, NULL);
547 static int zcore_reipl_open(struct inode *inode, struct file *filp)
552 static int zcore_reipl_release(struct inode *inode, struct file *filp)
557 static const struct file_operations zcore_reipl_fops = {
558 .owner = THIS_MODULE,
559 .write = zcore_reipl_write,
560 .open = zcore_reipl_open,
561 .release = zcore_reipl_release,
565 static void __init set_s390_lc_mask(union save_area *map)
567 memset(&map->s390.ext_save, 0xff, sizeof(map->s390.ext_save));
568 memset(&map->s390.timer, 0xff, sizeof(map->s390.timer));
569 memset(&map->s390.clk_cmp, 0xff, sizeof(map->s390.clk_cmp));
570 memset(&map->s390.psw, 0xff, sizeof(map->s390.psw));
571 memset(&map->s390.pref_reg, 0xff, sizeof(map->s390.pref_reg));
572 memset(&map->s390.acc_regs, 0xff, sizeof(map->s390.acc_regs));
573 memset(&map->s390.fp_regs, 0xff, sizeof(map->s390.fp_regs));
574 memset(&map->s390.gp_regs, 0xff, sizeof(map->s390.gp_regs));
575 memset(&map->s390.ctrl_regs, 0xff, sizeof(map->s390.ctrl_regs));
578 static void __init set_s390x_lc_mask(union save_area *map)
580 memset(&map->s390x.fp_regs, 0xff, sizeof(map->s390x.fp_regs));
581 memset(&map->s390x.gp_regs, 0xff, sizeof(map->s390x.gp_regs));
582 memset(&map->s390x.psw, 0xff, sizeof(map->s390x.psw));
583 memset(&map->s390x.pref_reg, 0xff, sizeof(map->s390x.pref_reg));
584 memset(&map->s390x.fp_ctrl_reg, 0xff, sizeof(map->s390x.fp_ctrl_reg));
585 memset(&map->s390x.tod_reg, 0xff, sizeof(map->s390x.tod_reg));
586 memset(&map->s390x.timer, 0xff, sizeof(map->s390x.timer));
587 memset(&map->s390x.clk_cmp, 0xff, sizeof(map->s390x.clk_cmp));
588 memset(&map->s390x.acc_regs, 0xff, sizeof(map->s390x.acc_regs));
589 memset(&map->s390x.ctrl_regs, 0xff, sizeof(map->s390x.ctrl_regs));
593 * Initialize dump globals for a given architecture
595 static int __init sys_info_init(enum arch_id arch)
601 pr_alert("DETECTED 'S390X (64 bit) OS'\n");
602 sys_info.sa_base = SAVE_AREA_BASE_S390X;
603 sys_info.sa_size = sizeof(struct save_area_s390x);
604 set_s390x_lc_mask(&sys_info.lc_mask);
607 pr_alert("DETECTED 'S390 (32 bit) OS'\n");
608 sys_info.sa_base = SAVE_AREA_BASE_S390;
609 sys_info.sa_size = sizeof(struct save_area_s390);
610 set_s390_lc_mask(&sys_info.lc_mask);
613 pr_alert("0x%x is an unknown architecture.\n",arch);
616 sys_info.arch = arch;
617 rc = init_cpu_info(arch);
620 sys_info.mem_size = real_memory_size;
625 static int __init check_sdias(void)
627 int rc, act_hsa_size;
629 rc = sclp_sdias_blk_count();
631 TRACE("Could not determine HSA size\n");
634 act_hsa_size = (rc - 1) * PAGE_SIZE;
635 if (act_hsa_size < ZFCPDUMP_HSA_SIZE) {
636 TRACE("HSA size too small: %i\n", act_hsa_size);
642 static int __init get_mem_size(unsigned long *mem)
645 struct mem_chunk *chunk_array;
647 chunk_array = kzalloc(MEMORY_CHUNKS * sizeof(struct mem_chunk),
651 detect_memory_layout(chunk_array);
652 for (i = 0; i < MEMORY_CHUNKS; i++) {
653 if (chunk_array[i].size == 0)
655 *mem += chunk_array[i].size;
661 static int __init zcore_header_init(int arch, struct zcore_header *hdr)
664 unsigned long memory = 0;
666 if (arch == ARCH_S390X)
667 hdr->arch_id = DUMP_ARCH_S390X;
669 hdr->arch_id = DUMP_ARCH_S390;
670 rc = get_mem_size(&memory);
673 hdr->mem_size = memory;
674 hdr->rmem_size = memory;
675 hdr->mem_end = sys_info.mem_size;
676 hdr->num_pages = memory / PAGE_SIZE;
677 hdr->tod = get_clock();
678 get_cpu_id(&hdr->cpu_id);
683 * Provide IPL parameter information block from either HSA or memory
686 static int __init zcore_reipl_init(void)
688 struct ipib_info ipib_info;
691 rc = memcpy_hsa_kernel(&ipib_info, __LC_DUMP_REIPL, sizeof(ipib_info));
694 if (ipib_info.ipib == 0)
696 ipl_block = (void *) __get_free_page(GFP_KERNEL);
699 if (ipib_info.ipib < ZFCPDUMP_HSA_SIZE)
700 rc = memcpy_hsa_kernel(ipl_block, ipib_info.ipib, PAGE_SIZE);
702 rc = memcpy_real(ipl_block, ipib_info.ipib, PAGE_SIZE);
704 free_page((unsigned long) ipl_block);
707 if (csum_partial(ipl_block, ipl_block->hdr.len, 0) !=
708 ipib_info.checksum) {
709 TRACE("Checksum does not match\n");
710 free_page((unsigned long) ipl_block);
716 static int __init zcore_init(void)
721 if (ipl_info.type != IPL_TYPE_FCP_DUMP)
724 zcore_dbf = debug_register("zcore", 4, 1, 4 * sizeof(long));
725 debug_register_view(zcore_dbf, &debug_sprintf_view);
726 debug_set_level(zcore_dbf, 6);
728 TRACE("devno: %x\n", ipl_info.data.fcp.dev_id.devno);
729 TRACE("wwpn: %llx\n", (unsigned long long) ipl_info.data.fcp.wwpn);
730 TRACE("lun: %llx\n", (unsigned long long) ipl_info.data.fcp.lun);
732 rc = sclp_sdias_init();
740 rc = memcpy_hsa_kernel(&arch, __LC_AR_MODE_ID, 1);
745 if (arch == ARCH_S390X) {
746 pr_alert("The 32-bit dump tool cannot be used for a "
753 rc = sys_info_init(arch);
757 rc = zcore_header_init(arch, &zcore_header);
761 rc = zcore_reipl_init();
765 zcore_dir = debugfs_create_dir("zcore" , NULL);
770 zcore_file = debugfs_create_file("mem", S_IRUSR, zcore_dir, NULL,
776 zcore_memmap_file = debugfs_create_file("memmap", S_IRUSR, zcore_dir,
777 NULL, &zcore_memmap_fops);
778 if (!zcore_memmap_file) {
782 zcore_reipl_file = debugfs_create_file("reipl", S_IRUSR, zcore_dir,
783 NULL, &zcore_reipl_fops);
784 if (!zcore_reipl_file) {
786 goto fail_memmap_file;
792 debugfs_remove(zcore_memmap_file);
794 debugfs_remove(zcore_file);
796 debugfs_remove(zcore_dir);
798 diag308(DIAG308_REL_HSA, NULL);
802 static void __exit zcore_exit(void)
804 debug_unregister(zcore_dbf);
806 free_page((unsigned long) ipl_block);
807 debugfs_remove(zcore_reipl_file);
808 debugfs_remove(zcore_memmap_file);
809 debugfs_remove(zcore_file);
810 debugfs_remove(zcore_dir);
811 diag308(DIAG308_REL_HSA, NULL);
814 MODULE_AUTHOR("Copyright IBM Corp. 2003,2008");
815 MODULE_DESCRIPTION("zcore module for zfcpdump support");
816 MODULE_LICENSE("GPL");
818 subsys_initcall(zcore_init);
819 module_exit(zcore_exit);