1
2
3
4
5
6
7
8
9
10
11
12#define KMSG_COMPONENT "zdump"
13#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
14
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>
21#include <asm/ipl.h>
22#include <asm/sclp.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>
29#include <asm/switch_to.h>
30#include "sclp.h"
31
32#define TRACE(x...) debug_sprintf_event(zcore_dbf, 1, x)
33
34#define TO_USER 1
35#define TO_KERNEL 0
36#define CHUNK_INFO_SIZE 34
37
38enum arch_id {
39 ARCH_S390 = 0,
40 ARCH_S390X = 1,
41};
42
43
44
45struct sys_info {
46 enum arch_id arch;
47 unsigned long sa_base;
48 u32 sa_size;
49 int cpu_map[NR_CPUS];
50 unsigned long mem_size;
51 struct save_area lc_mask;
52};
53
54struct ipib_info {
55 unsigned long ipib;
56 u32 checksum;
57} __attribute__((packed));
58
59static struct sys_info sys_info;
60static struct debug_info *zcore_dbf;
61static int hsa_available;
62static struct dentry *zcore_dir;
63static struct dentry *zcore_file;
64static struct dentry *zcore_memmap_file;
65static struct dentry *zcore_reipl_file;
66static struct dentry *zcore_hsa_file;
67static struct ipl_parameter_block *ipl_block;
68
69
70
71
72
73
74
75
76
77int memcpy_hsa(void *dest, unsigned long src, size_t count, int mode)
78{
79 int offs, blk_num;
80 static char buf[PAGE_SIZE] __attribute__((__aligned__(PAGE_SIZE)));
81
82 if (!hsa_available)
83 return -ENODATA;
84 if (count == 0)
85 return 0;
86
87
88 offs = 0;
89 if ((src % PAGE_SIZE) != 0) {
90 blk_num = src / PAGE_SIZE + 2;
91 if (sclp_sdias_copy(buf, blk_num, 1)) {
92 TRACE("sclp_sdias_copy() failed\n");
93 return -EIO;
94 }
95 offs = min((PAGE_SIZE - (src % PAGE_SIZE)), count);
96 if (mode == TO_USER) {
97 if (copy_to_user((__force __user void*) dest,
98 buf + (src % PAGE_SIZE), offs))
99 return -EFAULT;
100 } else
101 memcpy(dest, buf + (src % PAGE_SIZE), offs);
102 }
103 if (offs == count)
104 goto out;
105
106
107 for (; (offs + PAGE_SIZE) <= count; offs += PAGE_SIZE) {
108 blk_num = (src + offs) / PAGE_SIZE + 2;
109 if (sclp_sdias_copy(buf, blk_num, 1)) {
110 TRACE("sclp_sdias_copy() failed\n");
111 return -EIO;
112 }
113 if (mode == TO_USER) {
114 if (copy_to_user((__force __user void*) dest + offs,
115 buf, PAGE_SIZE))
116 return -EFAULT;
117 } else
118 memcpy(dest + offs, buf, PAGE_SIZE);
119 }
120 if (offs == count)
121 goto out;
122
123
124 blk_num = (src + offs) / PAGE_SIZE + 2;
125 if (sclp_sdias_copy(buf, blk_num, 1)) {
126 TRACE("sclp_sdias_copy() failed\n");
127 return -EIO;
128 }
129 if (mode == TO_USER) {
130 if (copy_to_user((__force __user void*) dest + offs, buf,
131 count - offs))
132 return -EFAULT;
133 } else
134 memcpy(dest + offs, buf, count - offs);
135out:
136 return 0;
137}
138
139static int memcpy_hsa_user(void __user *dest, unsigned long src, size_t count)
140{
141 return memcpy_hsa((void __force *) dest, src, count, TO_USER);
142}
143
144static int memcpy_hsa_kernel(void *dest, unsigned long src, size_t count)
145{
146 return memcpy_hsa(dest, src, count, TO_KERNEL);
147}
148
149static int __init init_cpu_info(enum arch_id arch)
150{
151 struct save_area_ext *sa_ext;
152
153
154
155 sa_ext = dump_save_area_create(0);
156 if (!sa_ext)
157 return -ENOMEM;
158 if (memcpy_hsa_kernel(&sa_ext->sa, sys_info.sa_base,
159 sys_info.sa_size) < 0) {
160 TRACE("could not copy from HSA\n");
161 kfree(sa_ext);
162 return -EIO;
163 }
164 if (MACHINE_HAS_VX)
165 save_vx_regs_safe(sa_ext->vx_regs);
166 return 0;
167}
168
169static DEFINE_MUTEX(zcore_mutex);
170
171#define DUMP_VERSION 0x5
172#define DUMP_MAGIC 0xa8190173618f23fdULL
173#define DUMP_ARCH_S390X 2
174#define DUMP_ARCH_S390 1
175#define HEADER_SIZE 4096
176
177
178
179struct zcore_header {
180 u64 magic;
181 u32 version;
182 u32 header_size;
183 u32 dump_level;
184 u32 page_size;
185 u64 mem_size;
186 u64 mem_start;
187 u64 mem_end;
188 u32 num_pages;
189 u32 pad1;
190 u64 tod;
191 struct cpuid cpu_id;
192 u32 arch_id;
193 u32 volnr;
194 u32 build_arch;
195 u64 rmem_size;
196 u8 mvdump;
197 u16 cpu_cnt;
198 u16 real_cpu_cnt;
199 u8 end_pad1[0x200-0x061];
200 u64 mvdump_sign;
201 u64 mvdump_zipl_time;
202 u8 end_pad2[0x800-0x210];
203 u32 lc_vec[512];
204} __attribute__((packed,__aligned__(16)));
205
206static struct zcore_header zcore_header = {
207 .magic = DUMP_MAGIC,
208 .version = DUMP_VERSION,
209 .header_size = 4096,
210 .dump_level = 0,
211 .page_size = PAGE_SIZE,
212 .mem_start = 0,
213#ifdef CONFIG_64BIT
214 .build_arch = DUMP_ARCH_S390X,
215#else
216 .build_arch = DUMP_ARCH_S390,
217#endif
218};
219
220
221
222
223
224
225
226
227
228static int copy_lc(void __user *buf, void *sa, int sa_off, int len)
229{
230 int i;
231 char *lc_mask = (char*)&sys_info.lc_mask;
232
233 for (i = 0; i < len; i++) {
234 if (!lc_mask[i + sa_off])
235 continue;
236 if (copy_to_user(buf + i, sa + sa_off + i, 1))
237 return -EFAULT;
238 }
239 return 0;
240}
241
242
243
244
245
246
247
248
249static int zcore_add_lc(char __user *buf, unsigned long start, size_t count)
250{
251 unsigned long end;
252 int i;
253
254 if (count == 0)
255 return 0;
256
257 end = start + count;
258 for (i = 0; i < dump_save_areas.count; i++) {
259 unsigned long cp_start, cp_end;
260 unsigned long sa_start, sa_end;
261 unsigned long prefix;
262 unsigned long sa_off, len, buf_off;
263 struct save_area *save_area = &dump_save_areas.areas[i]->sa;
264
265 prefix = save_area->pref_reg;
266 sa_start = prefix + sys_info.sa_base;
267 sa_end = prefix + sys_info.sa_base + sys_info.sa_size;
268
269 if ((end < sa_start) || (start > sa_end))
270 continue;
271 cp_start = max(start, sa_start);
272 cp_end = min(end, sa_end);
273
274 buf_off = cp_start - start;
275 sa_off = cp_start - sa_start;
276 len = cp_end - cp_start;
277
278 TRACE("copy_lc for: %lx\n", start);
279 if (copy_lc(buf + buf_off, save_area, sa_off, len))
280 return -EFAULT;
281 }
282 return 0;
283}
284
285
286
287
288static void release_hsa(void)
289{
290 diag308(DIAG308_REL_HSA, NULL);
291 hsa_available = 0;
292}
293
294
295
296
297
298
299
300static ssize_t zcore_read(struct file *file, char __user *buf, size_t count,
301 loff_t *ppos)
302{
303 unsigned long mem_start;
304 size_t mem_offs;
305 size_t hdr_count;
306 size_t size;
307 int rc;
308
309 mutex_lock(&zcore_mutex);
310
311 if (*ppos > (sys_info.mem_size + HEADER_SIZE)) {
312 rc = -EINVAL;
313 goto fail;
314 }
315
316 count = min(count, (size_t) (sys_info.mem_size + HEADER_SIZE - *ppos));
317
318
319 if (*ppos < HEADER_SIZE) {
320 size = min(count, (size_t) (HEADER_SIZE - *ppos));
321 if (copy_to_user(buf, &zcore_header + *ppos, size)) {
322 rc = -EFAULT;
323 goto fail;
324 }
325 hdr_count = size;
326 mem_start = 0;
327 } else {
328 hdr_count = 0;
329 mem_start = *ppos - HEADER_SIZE;
330 }
331
332 mem_offs = 0;
333
334
335 if (*ppos < sclp_get_hsa_size() + HEADER_SIZE) {
336 size = min((count - hdr_count),
337 (size_t) (sclp_get_hsa_size() - mem_start));
338 rc = memcpy_hsa_user(buf + hdr_count, mem_start, size);
339 if (rc)
340 goto fail;
341
342 mem_offs += size;
343 }
344
345
346 size = count - mem_offs - hdr_count;
347 rc = copy_to_user_real(buf + hdr_count + mem_offs,
348 (void *) mem_start + mem_offs, size);
349 if (rc)
350 goto fail;
351
352
353
354
355
356
357
358
359
360 if (zcore_add_lc(buf + hdr_count, mem_start, count - hdr_count)) {
361 rc = -EFAULT;
362 goto fail;
363 }
364 *ppos += count;
365fail:
366 mutex_unlock(&zcore_mutex);
367 return (rc < 0) ? rc : count;
368}
369
370static int zcore_open(struct inode *inode, struct file *filp)
371{
372 if (!hsa_available)
373 return -ENODATA;
374 else
375 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
376}
377
378static int zcore_release(struct inode *inode, struct file *filep)
379{
380 if (hsa_available)
381 release_hsa();
382 return 0;
383}
384
385static loff_t zcore_lseek(struct file *file, loff_t offset, int orig)
386{
387 loff_t rc;
388
389 mutex_lock(&zcore_mutex);
390 rc = no_seek_end_llseek(file, offset, orig);
391 mutex_unlock(&zcore_mutex);
392 return rc;
393}
394
395static const struct file_operations zcore_fops = {
396 .owner = THIS_MODULE,
397 .llseek = zcore_lseek,
398 .read = zcore_read,
399 .open = zcore_open,
400 .release = zcore_release,
401};
402
403static ssize_t zcore_memmap_read(struct file *filp, char __user *buf,
404 size_t count, loff_t *ppos)
405{
406 return simple_read_from_buffer(buf, count, ppos, filp->private_data,
407 MEMORY_CHUNKS * CHUNK_INFO_SIZE);
408}
409
410static int zcore_memmap_open(struct inode *inode, struct file *filp)
411{
412 int i;
413 char *buf;
414 struct mem_chunk *chunk_array;
415
416 chunk_array = kzalloc(MEMORY_CHUNKS * sizeof(struct mem_chunk),
417 GFP_KERNEL);
418 if (!chunk_array)
419 return -ENOMEM;
420 detect_memory_layout(chunk_array, 0);
421 buf = kzalloc(MEMORY_CHUNKS * CHUNK_INFO_SIZE, GFP_KERNEL);
422 if (!buf) {
423 kfree(chunk_array);
424 return -ENOMEM;
425 }
426 for (i = 0; i < MEMORY_CHUNKS; i++) {
427 sprintf(buf + (i * CHUNK_INFO_SIZE), "%016llx %016llx ",
428 (unsigned long long) chunk_array[i].addr,
429 (unsigned long long) chunk_array[i].size);
430 if (chunk_array[i].size == 0)
431 break;
432 }
433 kfree(chunk_array);
434 filp->private_data = buf;
435 return nonseekable_open(inode, filp);
436}
437
438static int zcore_memmap_release(struct inode *inode, struct file *filp)
439{
440 kfree(filp->private_data);
441 return 0;
442}
443
444static const struct file_operations zcore_memmap_fops = {
445 .owner = THIS_MODULE,
446 .read = zcore_memmap_read,
447 .open = zcore_memmap_open,
448 .release = zcore_memmap_release,
449 .llseek = no_llseek,
450};
451
452static ssize_t zcore_reipl_write(struct file *filp, const char __user *buf,
453 size_t count, loff_t *ppos)
454{
455 if (ipl_block) {
456 diag308(DIAG308_SET, ipl_block);
457 diag308(DIAG308_IPL, NULL);
458 }
459 return count;
460}
461
462static int zcore_reipl_open(struct inode *inode, struct file *filp)
463{
464 return nonseekable_open(inode, filp);
465}
466
467static int zcore_reipl_release(struct inode *inode, struct file *filp)
468{
469 return 0;
470}
471
472static const struct file_operations zcore_reipl_fops = {
473 .owner = THIS_MODULE,
474 .write = zcore_reipl_write,
475 .open = zcore_reipl_open,
476 .release = zcore_reipl_release,
477 .llseek = no_llseek,
478};
479
480static ssize_t zcore_hsa_read(struct file *filp, char __user *buf,
481 size_t count, loff_t *ppos)
482{
483 static char str[18];
484
485 if (hsa_available)
486 snprintf(str, sizeof(str), "%lx\n", sclp_get_hsa_size());
487 else
488 snprintf(str, sizeof(str), "0\n");
489 return simple_read_from_buffer(buf, count, ppos, str, strlen(str));
490}
491
492static ssize_t zcore_hsa_write(struct file *filp, const char __user *buf,
493 size_t count, loff_t *ppos)
494{
495 char value;
496
497 if (*ppos != 0)
498 return -EPIPE;
499 if (copy_from_user(&value, buf, 1))
500 return -EFAULT;
501 if (value != '0')
502 return -EINVAL;
503 release_hsa();
504 return count;
505}
506
507static const struct file_operations zcore_hsa_fops = {
508 .owner = THIS_MODULE,
509 .write = zcore_hsa_write,
510 .read = zcore_hsa_read,
511 .open = nonseekable_open,
512 .llseek = no_llseek,
513};
514
515#ifdef CONFIG_32BIT
516
517static void __init set_lc_mask(struct save_area *map)
518{
519 memset(&map->ext_save, 0xff, sizeof(map->ext_save));
520 memset(&map->timer, 0xff, sizeof(map->timer));
521 memset(&map->clk_cmp, 0xff, sizeof(map->clk_cmp));
522 memset(&map->psw, 0xff, sizeof(map->psw));
523 memset(&map->pref_reg, 0xff, sizeof(map->pref_reg));
524 memset(&map->acc_regs, 0xff, sizeof(map->acc_regs));
525 memset(&map->fp_regs, 0xff, sizeof(map->fp_regs));
526 memset(&map->gp_regs, 0xff, sizeof(map->gp_regs));
527 memset(&map->ctrl_regs, 0xff, sizeof(map->ctrl_regs));
528}
529
530#else
531
532static void __init set_lc_mask(struct save_area *map)
533{
534 memset(&map->fp_regs, 0xff, sizeof(map->fp_regs));
535 memset(&map->gp_regs, 0xff, sizeof(map->gp_regs));
536 memset(&map->psw, 0xff, sizeof(map->psw));
537 memset(&map->pref_reg, 0xff, sizeof(map->pref_reg));
538 memset(&map->fp_ctrl_reg, 0xff, sizeof(map->fp_ctrl_reg));
539 memset(&map->tod_reg, 0xff, sizeof(map->tod_reg));
540 memset(&map->timer, 0xff, sizeof(map->timer));
541 memset(&map->clk_cmp, 0xff, sizeof(map->clk_cmp));
542 memset(&map->acc_regs, 0xff, sizeof(map->acc_regs));
543 memset(&map->ctrl_regs, 0xff, sizeof(map->ctrl_regs));
544}
545
546#endif
547
548
549
550
551static int __init sys_info_init(enum arch_id arch, unsigned long mem_end)
552{
553 int rc;
554
555 switch (arch) {
556 case ARCH_S390X:
557 pr_alert("DETECTED 'S390X (64 bit) OS'\n");
558 break;
559 case ARCH_S390:
560 pr_alert("DETECTED 'S390 (32 bit) OS'\n");
561 break;
562 default:
563 pr_alert("0x%x is an unknown architecture.\n",arch);
564 return -EINVAL;
565 }
566 sys_info.sa_base = SAVE_AREA_BASE;
567 sys_info.sa_size = sizeof(struct save_area);
568 sys_info.arch = arch;
569 set_lc_mask(&sys_info.lc_mask);
570 rc = init_cpu_info(arch);
571 if (rc)
572 return rc;
573 sys_info.mem_size = mem_end;
574
575 return 0;
576}
577
578static int __init check_sdias(void)
579{
580 if (!sclp_get_hsa_size()) {
581 TRACE("Could not determine HSA size\n");
582 return -ENODEV;
583 }
584 return 0;
585}
586
587static int __init get_mem_info(unsigned long *mem, unsigned long *end)
588{
589 int i;
590 struct mem_chunk *chunk_array;
591
592 chunk_array = kzalloc(MEMORY_CHUNKS * sizeof(struct mem_chunk),
593 GFP_KERNEL);
594 if (!chunk_array)
595 return -ENOMEM;
596 detect_memory_layout(chunk_array, 0);
597 for (i = 0; i < MEMORY_CHUNKS; i++) {
598 if (chunk_array[i].size == 0)
599 break;
600 *mem += chunk_array[i].size;
601 *end = max(*end, chunk_array[i].addr + chunk_array[i].size);
602 }
603 kfree(chunk_array);
604 return 0;
605}
606
607static void __init zcore_header_init(int arch, struct zcore_header *hdr,
608 unsigned long mem_size)
609{
610 u32 prefix;
611 int i;
612
613 if (arch == ARCH_S390X)
614 hdr->arch_id = DUMP_ARCH_S390X;
615 else
616 hdr->arch_id = DUMP_ARCH_S390;
617 hdr->mem_size = mem_size;
618 hdr->rmem_size = mem_size;
619 hdr->mem_end = sys_info.mem_size;
620 hdr->num_pages = mem_size / PAGE_SIZE;
621 hdr->tod = get_tod_clock();
622 get_cpu_id(&hdr->cpu_id);
623 for (i = 0; i < dump_save_areas.count; i++) {
624 prefix = dump_save_areas.areas[i]->sa.pref_reg;
625 hdr->real_cpu_cnt++;
626 if (!prefix)
627 continue;
628 hdr->lc_vec[hdr->cpu_cnt] = prefix;
629 hdr->cpu_cnt++;
630 }
631}
632
633
634
635
636
637static int __init zcore_reipl_init(void)
638{
639 struct ipib_info ipib_info;
640 int rc;
641
642 rc = memcpy_hsa_kernel(&ipib_info, __LC_DUMP_REIPL, sizeof(ipib_info));
643 if (rc)
644 return rc;
645 if (ipib_info.ipib == 0)
646 return 0;
647 ipl_block = (void *) __get_free_page(GFP_KERNEL);
648 if (!ipl_block)
649 return -ENOMEM;
650 if (ipib_info.ipib < sclp_get_hsa_size())
651 rc = memcpy_hsa_kernel(ipl_block, ipib_info.ipib, PAGE_SIZE);
652 else
653 rc = memcpy_real(ipl_block, (void *) ipib_info.ipib, PAGE_SIZE);
654 if (rc || csum_partial(ipl_block, ipl_block->hdr.len, 0) !=
655 ipib_info.checksum) {
656 TRACE("Checksum does not match\n");
657 free_page((unsigned long) ipl_block);
658 ipl_block = NULL;
659 }
660 return 0;
661}
662
663static int __init zcore_init(void)
664{
665 unsigned long mem_size, mem_end;
666 unsigned char arch;
667 int rc;
668
669 mem_size = mem_end = 0;
670 if (ipl_info.type != IPL_TYPE_FCP_DUMP)
671 return -ENODATA;
672 if (OLDMEM_BASE)
673 return -ENODATA;
674
675 zcore_dbf = debug_register("zcore", 4, 1, 4 * sizeof(long));
676 debug_register_view(zcore_dbf, &debug_sprintf_view);
677 debug_set_level(zcore_dbf, 6);
678
679 TRACE("devno: %x\n", ipl_info.data.fcp.dev_id.devno);
680 TRACE("wwpn: %llx\n", (unsigned long long) ipl_info.data.fcp.wwpn);
681 TRACE("lun: %llx\n", (unsigned long long) ipl_info.data.fcp.lun);
682
683 rc = sclp_sdias_init();
684 if (rc)
685 goto fail;
686
687 rc = check_sdias();
688 if (rc)
689 goto fail;
690 hsa_available = 1;
691
692 rc = memcpy_hsa_kernel(&arch, __LC_AR_MODE_ID, 1);
693 if (rc)
694 goto fail;
695
696#ifdef CONFIG_64BIT
697 if (arch == ARCH_S390) {
698 pr_alert("The 64-bit dump tool cannot be used for a "
699 "32-bit system\n");
700 rc = -EINVAL;
701 goto fail;
702 }
703#else
704 if (arch == ARCH_S390X) {
705 pr_alert("The 32-bit dump tool cannot be used for a "
706 "64-bit system\n");
707 rc = -EINVAL;
708 goto fail;
709 }
710#endif
711
712 rc = get_mem_info(&mem_size, &mem_end);
713 if (rc)
714 goto fail;
715
716 rc = sys_info_init(arch, mem_end);
717 if (rc)
718 goto fail;
719 zcore_header_init(arch, &zcore_header, mem_size);
720
721 rc = zcore_reipl_init();
722 if (rc)
723 goto fail;
724
725 zcore_dir = debugfs_create_dir("zcore" , NULL);
726 if (!zcore_dir) {
727 rc = -ENOMEM;
728 goto fail;
729 }
730 zcore_file = debugfs_create_file("mem", S_IRUSR, zcore_dir, NULL,
731 &zcore_fops);
732 if (!zcore_file) {
733 rc = -ENOMEM;
734 goto fail_dir;
735 }
736 zcore_memmap_file = debugfs_create_file("memmap", S_IRUSR, zcore_dir,
737 NULL, &zcore_memmap_fops);
738 if (!zcore_memmap_file) {
739 rc = -ENOMEM;
740 goto fail_file;
741 }
742 zcore_reipl_file = debugfs_create_file("reipl", S_IRUSR, zcore_dir,
743 NULL, &zcore_reipl_fops);
744 if (!zcore_reipl_file) {
745 rc = -ENOMEM;
746 goto fail_memmap_file;
747 }
748 zcore_hsa_file = debugfs_create_file("hsa", S_IRUSR|S_IWUSR, zcore_dir,
749 NULL, &zcore_hsa_fops);
750 if (!zcore_hsa_file) {
751 rc = -ENOMEM;
752 goto fail_reipl_file;
753 }
754 return 0;
755
756fail_reipl_file:
757 debugfs_remove(zcore_reipl_file);
758fail_memmap_file:
759 debugfs_remove(zcore_memmap_file);
760fail_file:
761 debugfs_remove(zcore_file);
762fail_dir:
763 debugfs_remove(zcore_dir);
764fail:
765 diag308(DIAG308_REL_HSA, NULL);
766 return rc;
767}
768
769static void __exit zcore_exit(void)
770{
771 debug_unregister(zcore_dbf);
772 sclp_sdias_exit();
773 free_page((unsigned long) ipl_block);
774 debugfs_remove(zcore_hsa_file);
775 debugfs_remove(zcore_reipl_file);
776 debugfs_remove(zcore_memmap_file);
777 debugfs_remove(zcore_file);
778 debugfs_remove(zcore_dir);
779 diag308(DIAG308_REL_HSA, NULL);
780}
781
782MODULE_AUTHOR("Copyright IBM Corp. 2003,2008");
783MODULE_DESCRIPTION("zcore module for zfcpdump support");
784MODULE_LICENSE("GPL");
785
786subsys_initcall(zcore_init);
787module_exit(zcore_exit);
788