m68k/atari/debug.c: possible cleanups
[linux-2.6/openmoko-kernel.git] / drivers / s390 / char / zcore.c
blob7fd84be1193132f53163a272eba02c3791775116
1 /*
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>
16 #include <asm/ipl.h>
17 #include <asm/sclp.h>
18 #include <asm/setup.h>
19 #include <asm/sigp.h>
20 #include <asm/uaccess.h>
21 #include <asm/debug.h>
22 #include <asm/processor.h>
23 #include <asm/irqflags.h>
24 #include "sclp.h"
26 #define TRACE(x...) debug_sprintf_event(zcore_dbf, 1, x)
27 #define MSG(x...) printk( KERN_ALERT x )
28 #define ERROR_MSG(x...) printk ( KERN_ALERT "DUMP: " x )
30 #define TO_USER 0
31 #define TO_KERNEL 1
32 #define CHUNK_INFO_SIZE 34 /* 2 16-byte char, each followed by blank */
34 enum arch_id {
35 ARCH_S390 = 0,
36 ARCH_S390X = 1,
39 /* dump system info */
41 struct sys_info {
42 enum arch_id arch;
43 unsigned long sa_base;
44 u32 sa_size;
45 int cpu_map[NR_CPUS];
46 unsigned long mem_size;
47 union save_area lc_mask;
50 static struct sys_info sys_info;
51 static struct debug_info *zcore_dbf;
52 static int hsa_available;
53 static struct dentry *zcore_dir;
54 static struct dentry *zcore_file;
55 static struct dentry *zcore_memmap_file;
58 * Copy memory from HSA to kernel or user memory (not reentrant):
60 * @dest: Kernel or user buffer where memory should be copied to
61 * @src: Start address within HSA where data should be copied
62 * @count: Size of buffer, which should be copied
63 * @mode: Either TO_KERNEL or TO_USER
65 static int memcpy_hsa(void *dest, unsigned long src, size_t count, int mode)
67 int offs, blk_num;
68 static char buf[PAGE_SIZE] __attribute__((__aligned__(PAGE_SIZE)));
70 if (count == 0)
71 return 0;
73 /* copy first block */
74 offs = 0;
75 if ((src % PAGE_SIZE) != 0) {
76 blk_num = src / PAGE_SIZE + 2;
77 if (sclp_sdias_copy(buf, blk_num, 1)) {
78 TRACE("sclp_sdias_copy() failed\n");
79 return -EIO;
81 offs = min((PAGE_SIZE - (src % PAGE_SIZE)), count);
82 if (mode == TO_USER) {
83 if (copy_to_user((__force __user void*) dest,
84 buf + (src % PAGE_SIZE), offs))
85 return -EFAULT;
86 } else
87 memcpy(dest, buf + (src % PAGE_SIZE), offs);
89 if (offs == count)
90 goto out;
92 /* copy middle */
93 for (; (offs + PAGE_SIZE) <= count; offs += PAGE_SIZE) {
94 blk_num = (src + offs) / PAGE_SIZE + 2;
95 if (sclp_sdias_copy(buf, blk_num, 1)) {
96 TRACE("sclp_sdias_copy() failed\n");
97 return -EIO;
99 if (mode == TO_USER) {
100 if (copy_to_user((__force __user void*) dest + offs,
101 buf, PAGE_SIZE))
102 return -EFAULT;
103 } else
104 memcpy(dest + offs, buf, PAGE_SIZE);
106 if (offs == count)
107 goto out;
109 /* copy last block */
110 blk_num = (src + offs) / PAGE_SIZE + 2;
111 if (sclp_sdias_copy(buf, blk_num, 1)) {
112 TRACE("sclp_sdias_copy() failed\n");
113 return -EIO;
115 if (mode == TO_USER) {
116 if (copy_to_user((__force __user void*) dest + offs, buf,
117 PAGE_SIZE))
118 return -EFAULT;
119 } else
120 memcpy(dest + offs, buf, count - offs);
121 out:
122 return 0;
125 static int memcpy_hsa_user(void __user *dest, unsigned long src, size_t count)
127 return memcpy_hsa((void __force *) dest, src, count, TO_USER);
130 static int memcpy_hsa_kernel(void *dest, unsigned long src, size_t count)
132 return memcpy_hsa(dest, src, count, TO_KERNEL);
135 static int memcpy_real(void *dest, unsigned long src, size_t count)
137 unsigned long flags;
138 int rc = -EFAULT;
139 register unsigned long _dest asm("2") = (unsigned long) dest;
140 register unsigned long _len1 asm("3") = (unsigned long) count;
141 register unsigned long _src asm("4") = src;
142 register unsigned long _len2 asm("5") = (unsigned long) count;
144 if (count == 0)
145 return 0;
146 flags = __raw_local_irq_stnsm(0xf8UL); /* switch to real mode */
147 asm volatile (
148 "0: mvcle %1,%2,0x0\n"
149 "1: jo 0b\n"
150 " lhi %0,0x0\n"
151 "2:\n"
152 EX_TABLE(1b,2b)
153 : "+d" (rc), "+d" (_dest), "+d" (_src), "+d" (_len1),
154 "+d" (_len2), "=m" (*((long*)dest))
155 : "m" (*((long*)src))
156 : "cc", "memory");
157 __raw_local_irq_ssm(flags);
159 return rc;
162 static int memcpy_real_user(void __user *dest, unsigned long src, size_t count)
164 static char buf[4096];
165 int offs = 0, size;
167 while (offs < count) {
168 size = min(sizeof(buf), count - offs);
169 if (memcpy_real(buf, src + offs, size))
170 return -EFAULT;
171 if (copy_to_user(dest + offs, buf, size))
172 return -EFAULT;
173 offs += size;
175 return 0;
178 #ifdef __s390x__
180 * Convert s390x (64 bit) cpu info to s390 (32 bit) cpu info
182 static void __init s390x_to_s390_regs(union save_area *out, union save_area *in,
183 int cpu)
185 int i;
187 for (i = 0; i < 16; i++) {
188 out->s390.gp_regs[i] = in->s390x.gp_regs[i] & 0x00000000ffffffff;
189 out->s390.acc_regs[i] = in->s390x.acc_regs[i];
190 out->s390.ctrl_regs[i] =
191 in->s390x.ctrl_regs[i] & 0x00000000ffffffff;
193 /* locore for 31 bit has only space for fpregs 0,2,4,6 */
194 out->s390.fp_regs[0] = in->s390x.fp_regs[0];
195 out->s390.fp_regs[1] = in->s390x.fp_regs[2];
196 out->s390.fp_regs[2] = in->s390x.fp_regs[4];
197 out->s390.fp_regs[3] = in->s390x.fp_regs[6];
198 memcpy(&(out->s390.psw[0]), &(in->s390x.psw[0]), 4);
199 out->s390.psw[1] |= 0x8; /* set bit 12 */
200 memcpy(&(out->s390.psw[4]),&(in->s390x.psw[12]), 4);
201 out->s390.psw[4] |= 0x80; /* set (31bit) addressing bit */
202 out->s390.pref_reg = in->s390x.pref_reg;
203 out->s390.timer = in->s390x.timer;
204 out->s390.clk_cmp = in->s390x.clk_cmp;
207 static void __init s390x_to_s390_save_areas(void)
209 int i = 1;
210 static union save_area tmp;
212 while (zfcpdump_save_areas[i]) {
213 s390x_to_s390_regs(&tmp, zfcpdump_save_areas[i], i);
214 memcpy(zfcpdump_save_areas[i], &tmp, sizeof(tmp));
215 i++;
219 #endif /* __s390x__ */
221 static int __init init_cpu_info(enum arch_id arch)
223 union save_area *sa;
225 /* get info for boot cpu from lowcore, stored in the HSA */
227 sa = kmalloc(sizeof(*sa), GFP_KERNEL);
228 if (!sa)
229 return -ENOMEM;
230 if (memcpy_hsa_kernel(sa, sys_info.sa_base, sys_info.sa_size) < 0) {
231 TRACE("could not copy from HSA\n");
232 kfree(sa);
233 return -EIO;
235 zfcpdump_save_areas[0] = sa;
237 #ifdef __s390x__
238 /* convert s390x regs to s390, if we are dumping an s390 Linux */
240 if (arch == ARCH_S390)
241 s390x_to_s390_save_areas();
242 #endif
244 return 0;
247 static DEFINE_MUTEX(zcore_mutex);
249 #define DUMP_VERSION 0x3
250 #define DUMP_MAGIC 0xa8190173618f23fdULL
251 #define DUMP_ARCH_S390X 2
252 #define DUMP_ARCH_S390 1
253 #define HEADER_SIZE 4096
255 /* dump header dumped according to s390 crash dump format */
257 struct zcore_header {
258 u64 magic;
259 u32 version;
260 u32 header_size;
261 u32 dump_level;
262 u32 page_size;
263 u64 mem_size;
264 u64 mem_start;
265 u64 mem_end;
266 u32 num_pages;
267 u32 pad1;
268 u64 tod;
269 cpuid_t cpu_id;
270 u32 arch_id;
271 u32 volnr;
272 u32 build_arch;
273 u64 rmem_size;
274 char pad2[4016];
275 } __attribute__((packed,__aligned__(16)));
277 static struct zcore_header zcore_header = {
278 .magic = DUMP_MAGIC,
279 .version = DUMP_VERSION,
280 .header_size = 4096,
281 .dump_level = 0,
282 .page_size = PAGE_SIZE,
283 .mem_start = 0,
284 #ifdef __s390x__
285 .build_arch = DUMP_ARCH_S390X,
286 #else
287 .build_arch = DUMP_ARCH_S390,
288 #endif
292 * Copy lowcore info to buffer. Use map in order to copy only register parts.
294 * @buf: User buffer
295 * @sa: Pointer to save area
296 * @sa_off: Offset in save area to copy
297 * @len: Number of bytes to copy
299 static int copy_lc(void __user *buf, void *sa, int sa_off, int len)
301 int i;
302 char *lc_mask = (char*)&sys_info.lc_mask;
304 for (i = 0; i < len; i++) {
305 if (!lc_mask[i + sa_off])
306 continue;
307 if (copy_to_user(buf + i, sa + sa_off + i, 1))
308 return -EFAULT;
310 return 0;
314 * Copy lowcores info to memory, if necessary
316 * @buf: User buffer
317 * @addr: Start address of buffer in dump memory
318 * @count: Size of buffer
320 static int zcore_add_lc(char __user *buf, unsigned long start, size_t count)
322 unsigned long end;
323 int i = 0;
325 if (count == 0)
326 return 0;
328 end = start + count;
329 while (zfcpdump_save_areas[i]) {
330 unsigned long cp_start, cp_end; /* copy range */
331 unsigned long sa_start, sa_end; /* save area range */
332 unsigned long prefix;
333 unsigned long sa_off, len, buf_off;
335 if (sys_info.arch == ARCH_S390)
336 prefix = zfcpdump_save_areas[i]->s390.pref_reg;
337 else
338 prefix = zfcpdump_save_areas[i]->s390x.pref_reg;
340 sa_start = prefix + sys_info.sa_base;
341 sa_end = prefix + sys_info.sa_base + sys_info.sa_size;
343 if ((end < sa_start) || (start > sa_end))
344 goto next;
345 cp_start = max(start, sa_start);
346 cp_end = min(end, sa_end);
348 buf_off = cp_start - start;
349 sa_off = cp_start - sa_start;
350 len = cp_end - cp_start;
352 TRACE("copy_lc for: %lx\n", start);
353 if (copy_lc(buf + buf_off, zfcpdump_save_areas[i], sa_off, len))
354 return -EFAULT;
355 next:
356 i++;
358 return 0;
362 * Read routine for zcore character device
363 * First 4K are dump header
364 * Next 32MB are HSA Memory
365 * Rest is read from absolute Memory
367 static ssize_t zcore_read(struct file *file, char __user *buf, size_t count,
368 loff_t *ppos)
370 unsigned long mem_start; /* Start address in memory */
371 size_t mem_offs; /* Offset in dump memory */
372 size_t hdr_count; /* Size of header part of output buffer */
373 size_t size;
374 int rc;
376 mutex_lock(&zcore_mutex);
378 if (*ppos > (sys_info.mem_size + HEADER_SIZE)) {
379 rc = -EINVAL;
380 goto fail;
383 count = min(count, (size_t) (sys_info.mem_size + HEADER_SIZE - *ppos));
385 /* Copy dump header */
386 if (*ppos < HEADER_SIZE) {
387 size = min(count, (size_t) (HEADER_SIZE - *ppos));
388 if (copy_to_user(buf, &zcore_header + *ppos, size)) {
389 rc = -EFAULT;
390 goto fail;
392 hdr_count = size;
393 mem_start = 0;
394 } else {
395 hdr_count = 0;
396 mem_start = *ppos - HEADER_SIZE;
399 mem_offs = 0;
401 /* Copy from HSA data */
402 if (*ppos < (ZFCPDUMP_HSA_SIZE + HEADER_SIZE)) {
403 size = min((count - hdr_count), (size_t) (ZFCPDUMP_HSA_SIZE
404 - mem_start));
405 rc = memcpy_hsa_user(buf + hdr_count, mem_start, size);
406 if (rc)
407 goto fail;
409 mem_offs += size;
412 /* Copy from real mem */
413 size = count - mem_offs - hdr_count;
414 rc = memcpy_real_user(buf + hdr_count + mem_offs, mem_start + mem_offs,
415 size);
416 if (rc)
417 goto fail;
420 * Since s390 dump analysis tools like lcrash or crash
421 * expect register sets in the prefix pages of the cpus,
422 * we copy them into the read buffer, if necessary.
423 * buf + hdr_count: Start of memory part of output buffer
424 * mem_start: Start memory address to copy from
425 * count - hdr_count: Size of memory area to copy
427 if (zcore_add_lc(buf + hdr_count, mem_start, count - hdr_count)) {
428 rc = -EFAULT;
429 goto fail;
431 *ppos += count;
432 fail:
433 mutex_unlock(&zcore_mutex);
434 return (rc < 0) ? rc : count;
437 static int zcore_open(struct inode *inode, struct file *filp)
439 if (!hsa_available)
440 return -ENODATA;
441 else
442 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
445 static int zcore_release(struct inode *inode, struct file *filep)
447 diag308(DIAG308_REL_HSA, NULL);
448 hsa_available = 0;
449 return 0;
452 static loff_t zcore_lseek(struct file *file, loff_t offset, int orig)
454 loff_t rc;
456 mutex_lock(&zcore_mutex);
457 switch (orig) {
458 case 0:
459 file->f_pos = offset;
460 rc = file->f_pos;
461 break;
462 case 1:
463 file->f_pos += offset;
464 rc = file->f_pos;
465 break;
466 default:
467 rc = -EINVAL;
469 mutex_unlock(&zcore_mutex);
470 return rc;
473 static const struct file_operations zcore_fops = {
474 .owner = THIS_MODULE,
475 .llseek = zcore_lseek,
476 .read = zcore_read,
477 .open = zcore_open,
478 .release = zcore_release,
481 static ssize_t zcore_memmap_read(struct file *filp, char __user *buf,
482 size_t count, loff_t *ppos)
484 return simple_read_from_buffer(buf, count, ppos, filp->private_data,
485 MEMORY_CHUNKS * CHUNK_INFO_SIZE);
488 static int zcore_memmap_open(struct inode *inode, struct file *filp)
490 int i;
491 char *buf;
492 struct mem_chunk *chunk_array;
494 chunk_array = kzalloc(MEMORY_CHUNKS * sizeof(struct mem_chunk),
495 GFP_KERNEL);
496 if (!chunk_array)
497 return -ENOMEM;
498 detect_memory_layout(chunk_array);
499 buf = kzalloc(MEMORY_CHUNKS * CHUNK_INFO_SIZE, GFP_KERNEL);
500 if (!buf) {
501 kfree(chunk_array);
502 return -ENOMEM;
504 for (i = 0; i < MEMORY_CHUNKS; i++) {
505 sprintf(buf + (i * CHUNK_INFO_SIZE), "%016llx %016llx ",
506 (unsigned long long) chunk_array[i].addr,
507 (unsigned long long) chunk_array[i].size);
508 if (chunk_array[i].size == 0)
509 break;
511 kfree(chunk_array);
512 filp->private_data = buf;
513 return 0;
516 static int zcore_memmap_release(struct inode *inode, struct file *filp)
518 kfree(filp->private_data);
519 return 0;
522 static const struct file_operations zcore_memmap_fops = {
523 .owner = THIS_MODULE,
524 .read = zcore_memmap_read,
525 .open = zcore_memmap_open,
526 .release = zcore_memmap_release,
530 static void __init set_s390_lc_mask(union save_area *map)
532 memset(&map->s390.ext_save, 0xff, sizeof(map->s390.ext_save));
533 memset(&map->s390.timer, 0xff, sizeof(map->s390.timer));
534 memset(&map->s390.clk_cmp, 0xff, sizeof(map->s390.clk_cmp));
535 memset(&map->s390.psw, 0xff, sizeof(map->s390.psw));
536 memset(&map->s390.pref_reg, 0xff, sizeof(map->s390.pref_reg));
537 memset(&map->s390.acc_regs, 0xff, sizeof(map->s390.acc_regs));
538 memset(&map->s390.fp_regs, 0xff, sizeof(map->s390.fp_regs));
539 memset(&map->s390.gp_regs, 0xff, sizeof(map->s390.gp_regs));
540 memset(&map->s390.ctrl_regs, 0xff, sizeof(map->s390.ctrl_regs));
543 static void __init set_s390x_lc_mask(union save_area *map)
545 memset(&map->s390x.fp_regs, 0xff, sizeof(map->s390x.fp_regs));
546 memset(&map->s390x.gp_regs, 0xff, sizeof(map->s390x.gp_regs));
547 memset(&map->s390x.psw, 0xff, sizeof(map->s390x.psw));
548 memset(&map->s390x.pref_reg, 0xff, sizeof(map->s390x.pref_reg));
549 memset(&map->s390x.fp_ctrl_reg, 0xff, sizeof(map->s390x.fp_ctrl_reg));
550 memset(&map->s390x.tod_reg, 0xff, sizeof(map->s390x.tod_reg));
551 memset(&map->s390x.timer, 0xff, sizeof(map->s390x.timer));
552 memset(&map->s390x.clk_cmp, 0xff, sizeof(map->s390x.clk_cmp));
553 memset(&map->s390x.acc_regs, 0xff, sizeof(map->s390x.acc_regs));
554 memset(&map->s390x.ctrl_regs, 0xff, sizeof(map->s390x.ctrl_regs));
558 * Initialize dump globals for a given architecture
560 static int __init sys_info_init(enum arch_id arch)
562 int rc;
564 switch (arch) {
565 case ARCH_S390X:
566 MSG("DETECTED 'S390X (64 bit) OS'\n");
567 sys_info.sa_base = SAVE_AREA_BASE_S390X;
568 sys_info.sa_size = sizeof(struct save_area_s390x);
569 set_s390x_lc_mask(&sys_info.lc_mask);
570 break;
571 case ARCH_S390:
572 MSG("DETECTED 'S390 (32 bit) OS'\n");
573 sys_info.sa_base = SAVE_AREA_BASE_S390;
574 sys_info.sa_size = sizeof(struct save_area_s390);
575 set_s390_lc_mask(&sys_info.lc_mask);
576 break;
577 default:
578 ERROR_MSG("unknown architecture 0x%x.\n",arch);
579 return -EINVAL;
581 sys_info.arch = arch;
582 rc = init_cpu_info(arch);
583 if (rc)
584 return rc;
585 sys_info.mem_size = real_memory_size;
587 return 0;
590 static int __init check_sdias(void)
592 int rc, act_hsa_size;
594 rc = sclp_sdias_blk_count();
595 if (rc < 0) {
596 TRACE("Could not determine HSA size\n");
597 return rc;
599 act_hsa_size = (rc - 1) * PAGE_SIZE;
600 if (act_hsa_size < ZFCPDUMP_HSA_SIZE) {
601 TRACE("HSA size too small: %i\n", act_hsa_size);
602 return -EINVAL;
604 return 0;
607 static int __init get_mem_size(unsigned long *mem)
609 int i;
610 struct mem_chunk *chunk_array;
612 chunk_array = kzalloc(MEMORY_CHUNKS * sizeof(struct mem_chunk),
613 GFP_KERNEL);
614 if (!chunk_array)
615 return -ENOMEM;
616 detect_memory_layout(chunk_array);
617 for (i = 0; i < MEMORY_CHUNKS; i++) {
618 if (chunk_array[i].size == 0)
619 break;
620 *mem += chunk_array[i].size;
622 kfree(chunk_array);
623 return 0;
626 static int __init zcore_header_init(int arch, struct zcore_header *hdr)
628 int rc;
629 unsigned long memory = 0;
631 if (arch == ARCH_S390X)
632 hdr->arch_id = DUMP_ARCH_S390X;
633 else
634 hdr->arch_id = DUMP_ARCH_S390;
635 rc = get_mem_size(&memory);
636 if (rc)
637 return rc;
638 hdr->mem_size = memory;
639 hdr->rmem_size = memory;
640 hdr->mem_end = sys_info.mem_size;
641 hdr->num_pages = memory / PAGE_SIZE;
642 hdr->tod = get_clock();
643 get_cpu_id(&hdr->cpu_id);
644 return 0;
647 static int __init zcore_init(void)
649 unsigned char arch;
650 int rc;
652 if (ipl_info.type != IPL_TYPE_FCP_DUMP)
653 return -ENODATA;
655 zcore_dbf = debug_register("zcore", 4, 1, 4 * sizeof(long));
656 debug_register_view(zcore_dbf, &debug_sprintf_view);
657 debug_set_level(zcore_dbf, 6);
659 TRACE("devno: %x\n", ipl_info.data.fcp.dev_id.devno);
660 TRACE("wwpn: %llx\n", (unsigned long long) ipl_info.data.fcp.wwpn);
661 TRACE("lun: %llx\n", (unsigned long long) ipl_info.data.fcp.lun);
663 rc = sclp_sdias_init();
664 if (rc)
665 goto fail;
667 rc = check_sdias();
668 if (rc)
669 goto fail;
671 rc = memcpy_hsa_kernel(&arch, __LC_AR_MODE_ID, 1);
672 if (rc)
673 goto fail;
675 #ifndef __s390x__
676 if (arch == ARCH_S390X) {
677 ERROR_MSG("32 bit dumper can't dump 64 bit system!\n");
678 rc = -EINVAL;
679 goto fail;
681 #endif
683 rc = sys_info_init(arch);
684 if (rc)
685 goto fail;
687 rc = zcore_header_init(arch, &zcore_header);
688 if (rc)
689 goto fail;
691 zcore_dir = debugfs_create_dir("zcore" , NULL);
692 if (!zcore_dir) {
693 rc = -ENOMEM;
694 goto fail;
696 zcore_file = debugfs_create_file("mem", S_IRUSR, zcore_dir, NULL,
697 &zcore_fops);
698 if (!zcore_file) {
699 rc = -ENOMEM;
700 goto fail_dir;
702 zcore_memmap_file = debugfs_create_file("memmap", S_IRUSR, zcore_dir,
703 NULL, &zcore_memmap_fops);
704 if (!zcore_memmap_file) {
705 rc = -ENOMEM;
706 goto fail_file;
708 hsa_available = 1;
709 return 0;
711 fail_file:
712 debugfs_remove(zcore_file);
713 fail_dir:
714 debugfs_remove(zcore_dir);
715 fail:
716 diag308(DIAG308_REL_HSA, NULL);
717 return rc;
720 static void __exit zcore_exit(void)
722 debug_unregister(zcore_dbf);
723 sclp_sdias_exit();
724 diag308(DIAG308_REL_HSA, NULL);
727 MODULE_AUTHOR("Copyright IBM Corp. 2003,2007");
728 MODULE_DESCRIPTION("zcore module for zfcpdump support");
729 MODULE_LICENSE("GPL");
731 subsys_initcall(zcore_init);
732 module_exit(zcore_exit);