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 <asm/asm-offsets.h>
23 #include <asm/setup.h>
24 #include <asm/uaccess.h>
25 #include <asm/debug.h>
26 #include <asm/processor.h>
27 #include <asm/irqflags.h>
28 #include <asm/checksum.h>
31 #define TRACE(x...) debug_sprintf_event(zcore_dbf, 1, x)
35 #define CHUNK_INFO_SIZE 34 /* 2 16-byte char, each followed by blank */
42 /* dump system info */
46 unsigned long sa_base
;
49 unsigned long mem_size
;
50 struct save_area lc_mask
;
56 } __attribute__((packed
));
58 static struct sys_info sys_info
;
59 static struct debug_info
*zcore_dbf
;
60 static int hsa_available
;
61 static struct dentry
*zcore_dir
;
62 static struct dentry
*zcore_file
;
63 static struct dentry
*zcore_memmap_file
;
64 static struct dentry
*zcore_reipl_file
;
65 static struct dentry
*zcore_hsa_file
;
66 static struct ipl_parameter_block
*ipl_block
;
69 * Copy memory from HSA to kernel or user memory (not reentrant):
71 * @dest: Kernel or user buffer where memory should be copied to
72 * @src: Start address within HSA where data should be copied
73 * @count: Size of buffer, which should be copied
74 * @mode: Either TO_KERNEL or TO_USER
76 int memcpy_hsa(void *dest
, unsigned long src
, size_t count
, int mode
)
79 static char buf
[PAGE_SIZE
] __attribute__((__aligned__(PAGE_SIZE
)));
86 /* copy first block */
88 if ((src
% PAGE_SIZE
) != 0) {
89 blk_num
= src
/ PAGE_SIZE
+ 2;
90 if (sclp_sdias_copy(buf
, blk_num
, 1)) {
91 TRACE("sclp_sdias_copy() failed\n");
94 offs
= min((PAGE_SIZE
- (src
% PAGE_SIZE
)), count
);
95 if (mode
== TO_USER
) {
96 if (copy_to_user((__force __user
void*) dest
,
97 buf
+ (src
% PAGE_SIZE
), offs
))
100 memcpy(dest
, buf
+ (src
% PAGE_SIZE
), offs
);
106 for (; (offs
+ PAGE_SIZE
) <= count
; offs
+= PAGE_SIZE
) {
107 blk_num
= (src
+ offs
) / PAGE_SIZE
+ 2;
108 if (sclp_sdias_copy(buf
, blk_num
, 1)) {
109 TRACE("sclp_sdias_copy() failed\n");
112 if (mode
== TO_USER
) {
113 if (copy_to_user((__force __user
void*) dest
+ offs
,
117 memcpy(dest
+ offs
, buf
, PAGE_SIZE
);
122 /* copy last block */
123 blk_num
= (src
+ offs
) / PAGE_SIZE
+ 2;
124 if (sclp_sdias_copy(buf
, blk_num
, 1)) {
125 TRACE("sclp_sdias_copy() failed\n");
128 if (mode
== TO_USER
) {
129 if (copy_to_user((__force __user
void*) dest
+ offs
, buf
,
133 memcpy(dest
+ offs
, buf
, count
- offs
);
138 static int memcpy_hsa_user(void __user
*dest
, unsigned long src
, size_t count
)
140 return memcpy_hsa((void __force
*) dest
, src
, count
, TO_USER
);
143 static int memcpy_hsa_kernel(void *dest
, unsigned long src
, size_t count
)
145 return memcpy_hsa(dest
, src
, count
, TO_KERNEL
);
148 static int __init
init_cpu_info(enum arch_id arch
)
150 struct save_area
*sa
;
152 /* get info for boot cpu from lowcore, stored in the HSA */
154 sa
= dump_save_area_create(0);
157 if (memcpy_hsa_kernel(sa
, sys_info
.sa_base
, sys_info
.sa_size
) < 0) {
158 TRACE("could not copy from HSA\n");
165 static DEFINE_MUTEX(zcore_mutex
);
167 #define DUMP_VERSION 0x5
168 #define DUMP_MAGIC 0xa8190173618f23fdULL
169 #define DUMP_ARCH_S390X 2
170 #define DUMP_ARCH_S390 1
171 #define HEADER_SIZE 4096
173 /* dump header dumped according to s390 crash dump format */
175 struct zcore_header
{
195 u8 end_pad1
[0x200-0x061];
197 u64 mvdump_zipl_time
;
198 u8 end_pad2
[0x800-0x210];
200 } __attribute__((packed
,__aligned__(16)));
202 static struct zcore_header zcore_header
= {
204 .version
= DUMP_VERSION
,
207 .page_size
= PAGE_SIZE
,
210 .build_arch
= DUMP_ARCH_S390X
,
212 .build_arch
= DUMP_ARCH_S390
,
217 * Copy lowcore info to buffer. Use map in order to copy only register parts.
220 * @sa: Pointer to save area
221 * @sa_off: Offset in save area to copy
222 * @len: Number of bytes to copy
224 static int copy_lc(void __user
*buf
, void *sa
, int sa_off
, int len
)
227 char *lc_mask
= (char*)&sys_info
.lc_mask
;
229 for (i
= 0; i
< len
; i
++) {
230 if (!lc_mask
[i
+ sa_off
])
232 if (copy_to_user(buf
+ i
, sa
+ sa_off
+ i
, 1))
239 * Copy lowcores info to memory, if necessary
242 * @addr: Start address of buffer in dump memory
243 * @count: Size of buffer
245 static int zcore_add_lc(char __user
*buf
, unsigned long start
, size_t count
)
254 for (i
= 0; i
< dump_save_areas
.count
; i
++) {
255 unsigned long cp_start
, cp_end
; /* copy range */
256 unsigned long sa_start
, sa_end
; /* save area range */
257 unsigned long prefix
;
258 unsigned long sa_off
, len
, buf_off
;
259 struct save_area
*save_area
= dump_save_areas
.areas
[i
];
261 prefix
= save_area
->pref_reg
;
262 sa_start
= prefix
+ sys_info
.sa_base
;
263 sa_end
= prefix
+ sys_info
.sa_base
+ sys_info
.sa_size
;
265 if ((end
< sa_start
) || (start
> sa_end
))
267 cp_start
= max(start
, sa_start
);
268 cp_end
= min(end
, sa_end
);
270 buf_off
= cp_start
- start
;
271 sa_off
= cp_start
- sa_start
;
272 len
= cp_end
- cp_start
;
274 TRACE("copy_lc for: %lx\n", start
);
275 if (copy_lc(buf
+ buf_off
, save_area
, sa_off
, len
))
284 static void release_hsa(void)
286 diag308(DIAG308_REL_HSA
, NULL
);
291 * Read routine for zcore character device
292 * First 4K are dump header
293 * Next 32MB are HSA Memory
294 * Rest is read from absolute Memory
296 static ssize_t
zcore_read(struct file
*file
, char __user
*buf
, size_t count
,
299 unsigned long mem_start
; /* Start address in memory */
300 size_t mem_offs
; /* Offset in dump memory */
301 size_t hdr_count
; /* Size of header part of output buffer */
305 mutex_lock(&zcore_mutex
);
307 if (*ppos
> (sys_info
.mem_size
+ HEADER_SIZE
)) {
312 count
= min(count
, (size_t) (sys_info
.mem_size
+ HEADER_SIZE
- *ppos
));
314 /* Copy dump header */
315 if (*ppos
< HEADER_SIZE
) {
316 size
= min(count
, (size_t) (HEADER_SIZE
- *ppos
));
317 if (copy_to_user(buf
, &zcore_header
+ *ppos
, size
)) {
325 mem_start
= *ppos
- HEADER_SIZE
;
330 /* Copy from HSA data */
331 if (*ppos
< sclp_get_hsa_size() + HEADER_SIZE
) {
332 size
= min((count
- hdr_count
),
333 (size_t) (sclp_get_hsa_size() - mem_start
));
334 rc
= memcpy_hsa_user(buf
+ hdr_count
, mem_start
, size
);
341 /* Copy from real mem */
342 size
= count
- mem_offs
- hdr_count
;
343 rc
= copy_to_user_real(buf
+ hdr_count
+ mem_offs
,
344 (void *) mem_start
+ mem_offs
, size
);
349 * Since s390 dump analysis tools like lcrash or crash
350 * expect register sets in the prefix pages of the cpus,
351 * we copy them into the read buffer, if necessary.
352 * buf + hdr_count: Start of memory part of output buffer
353 * mem_start: Start memory address to copy from
354 * count - hdr_count: Size of memory area to copy
356 if (zcore_add_lc(buf
+ hdr_count
, mem_start
, count
- hdr_count
)) {
362 mutex_unlock(&zcore_mutex
);
363 return (rc
< 0) ? rc
: count
;
366 static int zcore_open(struct inode
*inode
, struct file
*filp
)
371 return capable(CAP_SYS_RAWIO
) ? 0 : -EPERM
;
374 static int zcore_release(struct inode
*inode
, struct file
*filep
)
381 static loff_t
zcore_lseek(struct file
*file
, loff_t offset
, int orig
)
385 mutex_lock(&zcore_mutex
);
388 file
->f_pos
= offset
;
392 file
->f_pos
+= offset
;
398 mutex_unlock(&zcore_mutex
);
402 static const struct file_operations zcore_fops
= {
403 .owner
= THIS_MODULE
,
404 .llseek
= zcore_lseek
,
407 .release
= zcore_release
,
410 static ssize_t
zcore_memmap_read(struct file
*filp
, char __user
*buf
,
411 size_t count
, loff_t
*ppos
)
413 return simple_read_from_buffer(buf
, count
, ppos
, filp
->private_data
,
414 MEMORY_CHUNKS
* CHUNK_INFO_SIZE
);
417 static int zcore_memmap_open(struct inode
*inode
, struct file
*filp
)
421 struct mem_chunk
*chunk_array
;
423 chunk_array
= kzalloc(MEMORY_CHUNKS
* sizeof(struct mem_chunk
),
427 detect_memory_layout(chunk_array
, 0);
428 buf
= kzalloc(MEMORY_CHUNKS
* CHUNK_INFO_SIZE
, GFP_KERNEL
);
433 for (i
= 0; i
< MEMORY_CHUNKS
; i
++) {
434 sprintf(buf
+ (i
* CHUNK_INFO_SIZE
), "%016llx %016llx ",
435 (unsigned long long) chunk_array
[i
].addr
,
436 (unsigned long long) chunk_array
[i
].size
);
437 if (chunk_array
[i
].size
== 0)
441 filp
->private_data
= buf
;
442 return nonseekable_open(inode
, filp
);
445 static int zcore_memmap_release(struct inode
*inode
, struct file
*filp
)
447 kfree(filp
->private_data
);
451 static const struct file_operations zcore_memmap_fops
= {
452 .owner
= THIS_MODULE
,
453 .read
= zcore_memmap_read
,
454 .open
= zcore_memmap_open
,
455 .release
= zcore_memmap_release
,
459 static ssize_t
zcore_reipl_write(struct file
*filp
, const char __user
*buf
,
460 size_t count
, loff_t
*ppos
)
463 diag308(DIAG308_SET
, ipl_block
);
464 diag308(DIAG308_IPL
, NULL
);
469 static int zcore_reipl_open(struct inode
*inode
, struct file
*filp
)
471 return nonseekable_open(inode
, filp
);
474 static int zcore_reipl_release(struct inode
*inode
, struct file
*filp
)
479 static const struct file_operations zcore_reipl_fops
= {
480 .owner
= THIS_MODULE
,
481 .write
= zcore_reipl_write
,
482 .open
= zcore_reipl_open
,
483 .release
= zcore_reipl_release
,
487 static ssize_t
zcore_hsa_read(struct file
*filp
, char __user
*buf
,
488 size_t count
, loff_t
*ppos
)
493 snprintf(str
, sizeof(str
), "%lx\n", sclp_get_hsa_size());
495 snprintf(str
, sizeof(str
), "0\n");
496 return simple_read_from_buffer(buf
, count
, ppos
, str
, strlen(str
));
499 static ssize_t
zcore_hsa_write(struct file
*filp
, const char __user
*buf
,
500 size_t count
, loff_t
*ppos
)
506 if (copy_from_user(&value
, buf
, 1))
514 static const struct file_operations zcore_hsa_fops
= {
515 .owner
= THIS_MODULE
,
516 .write
= zcore_hsa_write
,
517 .read
= zcore_hsa_read
,
518 .open
= nonseekable_open
,
524 static void __init
set_lc_mask(struct save_area
*map
)
526 memset(&map
->ext_save
, 0xff, sizeof(map
->ext_save
));
527 memset(&map
->timer
, 0xff, sizeof(map
->timer
));
528 memset(&map
->clk_cmp
, 0xff, sizeof(map
->clk_cmp
));
529 memset(&map
->psw
, 0xff, sizeof(map
->psw
));
530 memset(&map
->pref_reg
, 0xff, sizeof(map
->pref_reg
));
531 memset(&map
->acc_regs
, 0xff, sizeof(map
->acc_regs
));
532 memset(&map
->fp_regs
, 0xff, sizeof(map
->fp_regs
));
533 memset(&map
->gp_regs
, 0xff, sizeof(map
->gp_regs
));
534 memset(&map
->ctrl_regs
, 0xff, sizeof(map
->ctrl_regs
));
537 #else /* CONFIG_32BIT */
539 static void __init
set_lc_mask(struct save_area
*map
)
541 memset(&map
->fp_regs
, 0xff, sizeof(map
->fp_regs
));
542 memset(&map
->gp_regs
, 0xff, sizeof(map
->gp_regs
));
543 memset(&map
->psw
, 0xff, sizeof(map
->psw
));
544 memset(&map
->pref_reg
, 0xff, sizeof(map
->pref_reg
));
545 memset(&map
->fp_ctrl_reg
, 0xff, sizeof(map
->fp_ctrl_reg
));
546 memset(&map
->tod_reg
, 0xff, sizeof(map
->tod_reg
));
547 memset(&map
->timer
, 0xff, sizeof(map
->timer
));
548 memset(&map
->clk_cmp
, 0xff, sizeof(map
->clk_cmp
));
549 memset(&map
->acc_regs
, 0xff, sizeof(map
->acc_regs
));
550 memset(&map
->ctrl_regs
, 0xff, sizeof(map
->ctrl_regs
));
553 #endif /* CONFIG_32BIT */
556 * Initialize dump globals for a given architecture
558 static int __init
sys_info_init(enum arch_id arch
, unsigned long mem_end
)
564 pr_alert("DETECTED 'S390X (64 bit) OS'\n");
567 pr_alert("DETECTED 'S390 (32 bit) OS'\n");
570 pr_alert("0x%x is an unknown architecture.\n",arch
);
573 sys_info
.sa_base
= SAVE_AREA_BASE
;
574 sys_info
.sa_size
= sizeof(struct save_area
);
575 sys_info
.arch
= arch
;
576 set_lc_mask(&sys_info
.lc_mask
);
577 rc
= init_cpu_info(arch
);
580 sys_info
.mem_size
= mem_end
;
585 static int __init
check_sdias(void)
587 if (!sclp_get_hsa_size()) {
588 TRACE("Could not determine HSA size\n");
594 static int __init
get_mem_info(unsigned long *mem
, unsigned long *end
)
597 struct mem_chunk
*chunk_array
;
599 chunk_array
= kzalloc(MEMORY_CHUNKS
* sizeof(struct mem_chunk
),
603 detect_memory_layout(chunk_array
, 0);
604 for (i
= 0; i
< MEMORY_CHUNKS
; i
++) {
605 if (chunk_array
[i
].size
== 0)
607 *mem
+= chunk_array
[i
].size
;
608 *end
= max(*end
, chunk_array
[i
].addr
+ chunk_array
[i
].size
);
614 static void __init
zcore_header_init(int arch
, struct zcore_header
*hdr
,
615 unsigned long mem_size
)
620 if (arch
== ARCH_S390X
)
621 hdr
->arch_id
= DUMP_ARCH_S390X
;
623 hdr
->arch_id
= DUMP_ARCH_S390
;
624 hdr
->mem_size
= mem_size
;
625 hdr
->rmem_size
= mem_size
;
626 hdr
->mem_end
= sys_info
.mem_size
;
627 hdr
->num_pages
= mem_size
/ PAGE_SIZE
;
628 hdr
->tod
= get_tod_clock();
629 get_cpu_id(&hdr
->cpu_id
);
630 for (i
= 0; i
< dump_save_areas
.count
; i
++) {
631 prefix
= dump_save_areas
.areas
[i
]->pref_reg
;
635 hdr
->lc_vec
[hdr
->cpu_cnt
] = prefix
;
641 * Provide IPL parameter information block from either HSA or memory
644 static int __init
zcore_reipl_init(void)
646 struct ipib_info ipib_info
;
649 rc
= memcpy_hsa_kernel(&ipib_info
, __LC_DUMP_REIPL
, sizeof(ipib_info
));
652 if (ipib_info
.ipib
== 0)
654 ipl_block
= (void *) __get_free_page(GFP_KERNEL
);
657 if (ipib_info
.ipib
< sclp_get_hsa_size())
658 rc
= memcpy_hsa_kernel(ipl_block
, ipib_info
.ipib
, PAGE_SIZE
);
660 rc
= memcpy_real(ipl_block
, (void *) ipib_info
.ipib
, PAGE_SIZE
);
661 if (rc
|| csum_partial(ipl_block
, ipl_block
->hdr
.len
, 0) !=
662 ipib_info
.checksum
) {
663 TRACE("Checksum does not match\n");
664 free_page((unsigned long) ipl_block
);
670 static int __init
zcore_init(void)
672 unsigned long mem_size
, mem_end
;
676 mem_size
= mem_end
= 0;
677 if (ipl_info
.type
!= IPL_TYPE_FCP_DUMP
)
682 zcore_dbf
= debug_register("zcore", 4, 1, 4 * sizeof(long));
683 debug_register_view(zcore_dbf
, &debug_sprintf_view
);
684 debug_set_level(zcore_dbf
, 6);
686 TRACE("devno: %x\n", ipl_info
.data
.fcp
.dev_id
.devno
);
687 TRACE("wwpn: %llx\n", (unsigned long long) ipl_info
.data
.fcp
.wwpn
);
688 TRACE("lun: %llx\n", (unsigned long long) ipl_info
.data
.fcp
.lun
);
690 rc
= sclp_sdias_init();
699 rc
= memcpy_hsa_kernel(&arch
, __LC_AR_MODE_ID
, 1);
704 if (arch
== ARCH_S390
) {
705 pr_alert("The 64-bit dump tool cannot be used for a "
710 #else /* CONFIG_64BIT */
711 if (arch
== ARCH_S390X
) {
712 pr_alert("The 32-bit dump tool cannot be used for a "
717 #endif /* CONFIG_64BIT */
719 rc
= get_mem_info(&mem_size
, &mem_end
);
723 rc
= sys_info_init(arch
, mem_end
);
726 zcore_header_init(arch
, &zcore_header
, mem_size
);
728 rc
= zcore_reipl_init();
732 zcore_dir
= debugfs_create_dir("zcore" , NULL
);
737 zcore_file
= debugfs_create_file("mem", S_IRUSR
, zcore_dir
, NULL
,
743 zcore_memmap_file
= debugfs_create_file("memmap", S_IRUSR
, zcore_dir
,
744 NULL
, &zcore_memmap_fops
);
745 if (!zcore_memmap_file
) {
749 zcore_reipl_file
= debugfs_create_file("reipl", S_IRUSR
, zcore_dir
,
750 NULL
, &zcore_reipl_fops
);
751 if (!zcore_reipl_file
) {
753 goto fail_memmap_file
;
755 zcore_hsa_file
= debugfs_create_file("hsa", S_IRUSR
|S_IWUSR
, zcore_dir
,
756 NULL
, &zcore_hsa_fops
);
757 if (!zcore_hsa_file
) {
759 goto fail_reipl_file
;
764 debugfs_remove(zcore_reipl_file
);
766 debugfs_remove(zcore_memmap_file
);
768 debugfs_remove(zcore_file
);
770 debugfs_remove(zcore_dir
);
772 diag308(DIAG308_REL_HSA
, NULL
);
776 static void __exit
zcore_exit(void)
778 debug_unregister(zcore_dbf
);
780 free_page((unsigned long) ipl_block
);
781 debugfs_remove(zcore_hsa_file
);
782 debugfs_remove(zcore_reipl_file
);
783 debugfs_remove(zcore_memmap_file
);
784 debugfs_remove(zcore_file
);
785 debugfs_remove(zcore_dir
);
786 diag308(DIAG308_REL_HSA
, NULL
);
789 MODULE_AUTHOR("Copyright IBM Corp. 2003,2008");
790 MODULE_DESCRIPTION("zcore module for zfcpdump support");
791 MODULE_LICENSE("GPL");
793 subsys_initcall(zcore_init
);
794 module_exit(zcore_exit
);