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,2007
9 * Author(s): Michael Holzheu
12 #include <linux/init.h>
13 #include <linux/miscdevice.h>
14 #include <linux/utsname.h>
15 #include <linux/debugfs.h>
18 #include <asm/setup.h>
20 #include <asm/uaccess.h>
21 #include <asm/debug.h>
22 #include <asm/processor.h>
23 #include <asm/irqflags.h>
25 #define TRACE(x...) debug_sprintf_event(zcore_dbf, 1, x)
26 #define MSG(x...) printk( KERN_ALERT x )
27 #define ERROR_MSG(x...) printk ( KERN_ALERT "DUMP: " x )
37 /* dump system info */
41 unsigned long sa_base
;
44 unsigned long mem_size
;
45 union save_area lc_mask
;
48 static struct sys_info sys_info
;
49 static struct debug_info
*zcore_dbf
;
50 static int hsa_available
;
51 static struct dentry
*zcore_dir
;
52 static struct dentry
*zcore_file
;
55 * Copy memory from HSA to kernel or user memory (not reentrant):
57 * @dest: Kernel or user buffer where memory should be copied to
58 * @src: Start address within HSA where data should be copied
59 * @count: Size of buffer, which should be copied
60 * @mode: Either TO_KERNEL or TO_USER
62 static int memcpy_hsa(void *dest
, unsigned long src
, size_t count
, int mode
)
65 static char buf
[PAGE_SIZE
] __attribute__((__aligned__(PAGE_SIZE
)));
70 /* copy first block */
72 if ((src
% PAGE_SIZE
) != 0) {
73 blk_num
= src
/ PAGE_SIZE
+ 2;
74 if (sclp_sdias_copy(buf
, blk_num
, 1)) {
75 TRACE("sclp_sdias_copy() failed\n");
78 offs
= min((PAGE_SIZE
- (src
% PAGE_SIZE
)), count
);
79 if (mode
== TO_USER
) {
80 if (copy_to_user((__force __user
void*) dest
,
81 buf
+ (src
% PAGE_SIZE
), offs
))
84 memcpy(dest
, buf
+ (src
% PAGE_SIZE
), offs
);
90 for (; (offs
+ PAGE_SIZE
) <= count
; offs
+= PAGE_SIZE
) {
91 blk_num
= (src
+ offs
) / PAGE_SIZE
+ 2;
92 if (sclp_sdias_copy(buf
, blk_num
, 1)) {
93 TRACE("sclp_sdias_copy() failed\n");
96 if (mode
== TO_USER
) {
97 if (copy_to_user((__force __user
void*) dest
+ offs
,
101 memcpy(dest
+ offs
, buf
, PAGE_SIZE
);
106 /* copy last block */
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
, buf
,
117 memcpy(dest
+ offs
, buf
, count
- offs
);
122 static int memcpy_hsa_user(void __user
*dest
, unsigned long src
, size_t count
)
124 return memcpy_hsa((void __force
*) dest
, src
, count
, TO_USER
);
127 static int memcpy_hsa_kernel(void *dest
, unsigned long src
, size_t count
)
129 return memcpy_hsa(dest
, src
, count
, TO_KERNEL
);
132 static int memcpy_real(void *dest
, unsigned long src
, size_t count
)
136 register unsigned long _dest
asm("2") = (unsigned long) dest
;
137 register unsigned long _len1
asm("3") = (unsigned long) count
;
138 register unsigned long _src
asm("4") = src
;
139 register unsigned long _len2
asm("5") = (unsigned long) count
;
143 flags
= __raw_local_irq_stnsm(0xf8); /* switch to real mode */
145 "0: mvcle %1,%2,0x0\n"
151 : "d" (_dest
), "d" (_src
), "d" (_len1
), "d" (_len2
)
153 __raw_local_irq_ssm(flags
);
158 static int memcpy_real_user(__user
void *dest
, unsigned long src
, size_t count
)
160 static char buf
[4096];
163 while (offs
< count
) {
164 size
= min(sizeof(buf
), count
- offs
);
165 if (memcpy_real(buf
, src
+ offs
, size
))
167 if (copy_to_user(dest
+ offs
, buf
, size
))
176 * Convert s390x (64 bit) cpu info to s390 (32 bit) cpu info
178 static void __init
s390x_to_s390_regs(union save_area
*out
, union save_area
*in
,
183 for (i
= 0; i
< 16; i
++) {
184 out
->s390
.gp_regs
[i
] = in
->s390x
.gp_regs
[i
] & 0x00000000ffffffff;
185 out
->s390
.acc_regs
[i
] = in
->s390x
.acc_regs
[i
];
186 out
->s390
.ctrl_regs
[i
] =
187 in
->s390x
.ctrl_regs
[i
] & 0x00000000ffffffff;
189 /* locore for 31 bit has only space for fpregs 0,2,4,6 */
190 out
->s390
.fp_regs
[0] = in
->s390x
.fp_regs
[0];
191 out
->s390
.fp_regs
[1] = in
->s390x
.fp_regs
[2];
192 out
->s390
.fp_regs
[2] = in
->s390x
.fp_regs
[4];
193 out
->s390
.fp_regs
[3] = in
->s390x
.fp_regs
[6];
194 memcpy(&(out
->s390
.psw
[0]), &(in
->s390x
.psw
[0]), 4);
195 out
->s390
.psw
[1] |= 0x8; /* set bit 12 */
196 memcpy(&(out
->s390
.psw
[4]),&(in
->s390x
.psw
[12]), 4);
197 out
->s390
.psw
[4] |= 0x80; /* set (31bit) addressing bit */
198 out
->s390
.pref_reg
= in
->s390x
.pref_reg
;
199 out
->s390
.timer
= in
->s390x
.timer
;
200 out
->s390
.clk_cmp
= in
->s390x
.clk_cmp
;
203 static void __init
s390x_to_s390_save_areas(void)
206 static union save_area tmp
;
208 while (zfcpdump_save_areas
[i
]) {
209 s390x_to_s390_regs(&tmp
, zfcpdump_save_areas
[i
], i
);
210 memcpy(zfcpdump_save_areas
[i
], &tmp
, sizeof(tmp
));
215 #endif /* __s390x__ */
217 static int __init
init_cpu_info(enum arch_id arch
)
221 /* get info for boot cpu from lowcore, stored in the HSA */
223 sa
= kmalloc(sizeof(*sa
), GFP_KERNEL
);
225 ERROR_MSG("kmalloc failed: %s: %i\n",__FUNCTION__
, __LINE__
);
228 if (memcpy_hsa_kernel(sa
, sys_info
.sa_base
, sys_info
.sa_size
) < 0) {
229 ERROR_MSG("could not copy from HSA\n");
233 zfcpdump_save_areas
[0] = sa
;
236 /* convert s390x regs to s390, if we are dumping an s390 Linux */
238 if (arch
== ARCH_S390
)
239 s390x_to_s390_save_areas();
245 static DEFINE_MUTEX(zcore_mutex
);
247 #define DUMP_VERSION 0x3
248 #define DUMP_MAGIC 0xa8190173618f23fdULL
249 #define DUMP_ARCH_S390X 2
250 #define DUMP_ARCH_S390 1
251 #define HEADER_SIZE 4096
253 /* dump header dumped according to s390 crash dump format */
255 struct zcore_header
{
271 } __attribute__((packed
,__aligned__(16)));
273 static struct zcore_header zcore_header
= {
275 .version
= DUMP_VERSION
,
278 .page_size
= PAGE_SIZE
,
281 .build_arch
= DUMP_ARCH_S390X
,
283 .build_arch
= DUMP_ARCH_S390
,
288 * Copy lowcore info to buffer. Use map in order to copy only register parts.
291 * @sa: Pointer to save area
292 * @sa_off: Offset in save area to copy
293 * @len: Number of bytes to copy
295 static int copy_lc(void __user
*buf
, void *sa
, int sa_off
, int len
)
298 char *lc_mask
= (char*)&sys_info
.lc_mask
;
300 for (i
= 0; i
< len
; i
++) {
301 if (!lc_mask
[i
+ sa_off
])
303 if (copy_to_user(buf
+ i
, sa
+ sa_off
+ i
, 1))
310 * Copy lowcores info to memory, if necessary
313 * @addr: Start address of buffer in dump memory
314 * @count: Size of buffer
316 static int zcore_add_lc(char __user
*buf
, unsigned long start
, size_t count
)
325 while (zfcpdump_save_areas
[i
]) {
326 unsigned long cp_start
, cp_end
; /* copy range */
327 unsigned long sa_start
, sa_end
; /* save area range */
328 unsigned long prefix
;
329 unsigned long sa_off
, len
, buf_off
;
331 if (sys_info
.arch
== ARCH_S390
)
332 prefix
= zfcpdump_save_areas
[i
]->s390
.pref_reg
;
334 prefix
= zfcpdump_save_areas
[i
]->s390x
.pref_reg
;
336 sa_start
= prefix
+ sys_info
.sa_base
;
337 sa_end
= prefix
+ sys_info
.sa_base
+ sys_info
.sa_size
;
339 if ((end
< sa_start
) || (start
> sa_end
))
341 cp_start
= max(start
, sa_start
);
342 cp_end
= min(end
, sa_end
);
344 buf_off
= cp_start
- start
;
345 sa_off
= cp_start
- sa_start
;
346 len
= cp_end
- cp_start
;
348 TRACE("copy_lc for: %lx\n", start
);
349 if (copy_lc(buf
+ buf_off
, zfcpdump_save_areas
[i
], sa_off
, len
))
358 * Read routine for zcore character device
359 * First 4K are dump header
360 * Next 32MB are HSA Memory
361 * Rest is read from absolute Memory
363 static ssize_t
zcore_read(struct file
*file
, char __user
*buf
, size_t count
,
366 unsigned long mem_start
; /* Start address in memory */
367 size_t mem_offs
; /* Offset in dump memory */
368 size_t hdr_count
; /* Size of header part of output buffer */
372 mutex_lock(&zcore_mutex
);
374 if (*ppos
> (sys_info
.mem_size
+ HEADER_SIZE
)) {
379 count
= min(count
, (size_t) (sys_info
.mem_size
+ HEADER_SIZE
- *ppos
));
381 /* Copy dump header */
382 if (*ppos
< HEADER_SIZE
) {
383 size
= min(count
, (size_t) (HEADER_SIZE
- *ppos
));
384 if (copy_to_user(buf
, &zcore_header
+ *ppos
, size
)) {
392 mem_start
= *ppos
- HEADER_SIZE
;
397 /* Copy from HSA data */
398 if (*ppos
< (ZFCPDUMP_HSA_SIZE
+ HEADER_SIZE
)) {
399 size
= min((count
- hdr_count
), (size_t) (ZFCPDUMP_HSA_SIZE
401 rc
= memcpy_hsa_user(buf
+ hdr_count
, mem_start
, size
);
408 /* Copy from real mem */
409 size
= count
- mem_offs
- hdr_count
;
410 rc
= memcpy_real_user(buf
+ hdr_count
+ mem_offs
, mem_start
+ mem_offs
,
416 * Since s390 dump analysis tools like lcrash or crash
417 * expect register sets in the prefix pages of the cpus,
418 * we copy them into the read buffer, if necessary.
419 * buf + hdr_count: Start of memory part of output buffer
420 * mem_start: Start memory address to copy from
421 * count - hdr_count: Size of memory area to copy
423 if (zcore_add_lc(buf
+ hdr_count
, mem_start
, count
- hdr_count
)) {
429 mutex_unlock(&zcore_mutex
);
430 return (rc
< 0) ? rc
: count
;
433 static int zcore_open(struct inode
*inode
, struct file
*filp
)
438 return capable(CAP_SYS_RAWIO
) ? 0 : -EPERM
;
441 static int zcore_release(struct inode
*inode
, struct file
*filep
)
443 diag308(DIAG308_REL_HSA
, NULL
);
448 static loff_t
zcore_lseek(struct file
*file
, loff_t offset
, int orig
)
452 mutex_lock(&zcore_mutex
);
455 file
->f_pos
= offset
;
459 file
->f_pos
+= offset
;
465 mutex_unlock(&zcore_mutex
);
469 static struct file_operations zcore_fops
= {
470 .owner
= THIS_MODULE
,
471 .llseek
= zcore_lseek
,
474 .release
= zcore_release
,
478 static void __init
set_s390_lc_mask(union save_area
*map
)
480 memset(&map
->s390
.ext_save
, 0xff, sizeof(map
->s390
.ext_save
));
481 memset(&map
->s390
.timer
, 0xff, sizeof(map
->s390
.timer
));
482 memset(&map
->s390
.clk_cmp
, 0xff, sizeof(map
->s390
.clk_cmp
));
483 memset(&map
->s390
.psw
, 0xff, sizeof(map
->s390
.psw
));
484 memset(&map
->s390
.pref_reg
, 0xff, sizeof(map
->s390
.pref_reg
));
485 memset(&map
->s390
.acc_regs
, 0xff, sizeof(map
->s390
.acc_regs
));
486 memset(&map
->s390
.fp_regs
, 0xff, sizeof(map
->s390
.fp_regs
));
487 memset(&map
->s390
.gp_regs
, 0xff, sizeof(map
->s390
.gp_regs
));
488 memset(&map
->s390
.ctrl_regs
, 0xff, sizeof(map
->s390
.ctrl_regs
));
491 static void __init
set_s390x_lc_mask(union save_area
*map
)
493 memset(&map
->s390x
.fp_regs
, 0xff, sizeof(map
->s390x
.fp_regs
));
494 memset(&map
->s390x
.gp_regs
, 0xff, sizeof(map
->s390x
.gp_regs
));
495 memset(&map
->s390x
.psw
, 0xff, sizeof(map
->s390x
.psw
));
496 memset(&map
->s390x
.pref_reg
, 0xff, sizeof(map
->s390x
.pref_reg
));
497 memset(&map
->s390x
.fp_ctrl_reg
, 0xff, sizeof(map
->s390x
.fp_ctrl_reg
));
498 memset(&map
->s390x
.tod_reg
, 0xff, sizeof(map
->s390x
.tod_reg
));
499 memset(&map
->s390x
.timer
, 0xff, sizeof(map
->s390x
.timer
));
500 memset(&map
->s390x
.clk_cmp
, 0xff, sizeof(map
->s390x
.clk_cmp
));
501 memset(&map
->s390x
.acc_regs
, 0xff, sizeof(map
->s390x
.acc_regs
));
502 memset(&map
->s390x
.ctrl_regs
, 0xff, sizeof(map
->s390x
.ctrl_regs
));
506 * Initialize dump globals for a given architecture
508 static int __init
sys_info_init(enum arch_id arch
)
512 MSG("DETECTED 'S390X (64 bit) OS'\n");
513 sys_info
.sa_base
= SAVE_AREA_BASE_S390X
;
514 sys_info
.sa_size
= sizeof(struct save_area_s390x
);
515 set_s390x_lc_mask(&sys_info
.lc_mask
);
518 MSG("DETECTED 'S390 (32 bit) OS'\n");
519 sys_info
.sa_base
= SAVE_AREA_BASE_S390
;
520 sys_info
.sa_size
= sizeof(struct save_area_s390
);
521 set_s390_lc_mask(&sys_info
.lc_mask
);
524 ERROR_MSG("unknown architecture 0x%x.\n",arch
);
527 sys_info
.arch
= arch
;
528 if (init_cpu_info(arch
)) {
529 ERROR_MSG("get cpu info failed\n");
532 sys_info
.mem_size
= real_memory_size
;
537 static int __init
check_sdias(void)
539 int rc
, act_hsa_size
;
541 rc
= sclp_sdias_blk_count();
543 ERROR_MSG("Could not determine HSA size\n");
546 act_hsa_size
= (rc
- 1) * PAGE_SIZE
;
547 if (act_hsa_size
< ZFCPDUMP_HSA_SIZE
) {
548 ERROR_MSG("HSA size too small: %i\n", act_hsa_size
);
554 static void __init
zcore_header_init(int arch
, struct zcore_header
*hdr
)
556 if (arch
== ARCH_S390X
)
557 hdr
->arch_id
= DUMP_ARCH_S390X
;
559 hdr
->arch_id
= DUMP_ARCH_S390
;
560 hdr
->mem_size
= sys_info
.mem_size
;
561 hdr
->mem_end
= sys_info
.mem_size
;
562 hdr
->num_pages
= sys_info
.mem_size
/ PAGE_SIZE
;
563 hdr
->tod
= get_clock();
564 get_cpu_id(&hdr
->cpu_id
);
567 extern int sdias_init(void);
569 static int __init
zcore_init(void)
574 if (ipl_info
.type
!= IPL_TYPE_FCP_DUMP
)
577 zcore_dbf
= debug_register("zcore", 4, 1, 4 * sizeof(long));
578 debug_register_view(zcore_dbf
, &debug_sprintf_view
);
579 debug_set_level(zcore_dbf
, 6);
581 TRACE("devno: %x\n", ipl_info
.data
.fcp
.dev_id
.devno
);
582 TRACE("wwpn: %llx\n", (unsigned long long) ipl_info
.data
.fcp
.wwpn
);
583 TRACE("lun: %llx\n", (unsigned long long) ipl_info
.data
.fcp
.lun
);
591 ERROR_MSG("Dump initialization failed\n");
595 rc
= memcpy_hsa_kernel(&arch
, __LC_AR_MODE_ID
, 1);
597 ERROR_MSG("sdial memcpy for arch id failed\n");
602 if (arch
== ARCH_S390X
) {
603 ERROR_MSG("32 bit dumper can't dump 64 bit system!\n");
609 rc
= sys_info_init(arch
);
611 ERROR_MSG("arch init failed\n");
615 zcore_header_init(arch
, &zcore_header
);
617 zcore_dir
= debugfs_create_dir("zcore" , NULL
);
622 zcore_file
= debugfs_create_file("mem", S_IRUSR
, zcore_dir
, NULL
,
625 debugfs_remove(zcore_dir
);
633 diag308(DIAG308_REL_HSA
, NULL
);
637 extern void sdias_exit(void);
639 static void __exit
zcore_exit(void)
641 debug_unregister(zcore_dbf
);
643 diag308(DIAG308_REL_HSA
, NULL
);
646 MODULE_AUTHOR("Copyright IBM Corp. 2003,2007");
647 MODULE_DESCRIPTION("zcore module for zfcpdump support");
648 MODULE_LICENSE("GPL");
650 subsys_initcall(zcore_init
);
651 module_exit(zcore_exit
);