blob: 4fa455787a7756458a43891f7e9ba04dfb3504cf [file] [log] [blame]
Michael Holzheu411ed322007-04-27 16:01:49 +02001/*
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.
5 *
6 * For more information please refer to Documentation/s390/zfcpdump.txt
7 *
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02008 * Copyright IBM Corp. 2003, 2008
Michael Holzheu411ed322007-04-27 16:01:49 +02009 * Author(s): Michael Holzheu
10 */
11
Michael Holzheu17159dc62008-12-25 13:39:51 +010012#define KMSG_COMPONENT "zdump"
13#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
14
Michael Holzheu411ed322007-04-27 16:01:49 +020015#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Michael Holzheu411ed322007-04-27 16:01:49 +020017#include <linux/miscdevice.h>
Michael Holzheu411ed322007-04-27 16:01:49 +020018#include <linux/debugfs.h>
Heiko Carstens3948a102011-10-30 15:16:55 +010019#include <linux/module.h>
Philipp Hachtmann50be6342014-01-29 18:16:01 +010020#include <linux/memblock.h>
21
Heiko Carstenscbb870c2010-02-26 22:37:43 +010022#include <asm/asm-offsets.h>
Michael Holzheu411ed322007-04-27 16:01:49 +020023#include <asm/ipl.h>
24#include <asm/sclp.h>
25#include <asm/setup.h>
Michael Holzheu411ed322007-04-27 16:01:49 +020026#include <asm/uaccess.h>
27#include <asm/debug.h>
28#include <asm/processor.h>
29#include <asm/irqflags.h>
Frank Munzert159d1ff2009-03-26 15:24:45 +010030#include <asm/checksum.h>
Martin Schwidefskybbfed512015-10-15 11:14:19 +020031#include <asm/os_info.h>
Michael Holzheua62bc072014-10-06 17:57:43 +020032#include <asm/switch_to.h>
Heiko Carstens763968e2007-05-10 15:45:46 +020033#include "sclp.h"
Michael Holzheu411ed322007-04-27 16:01:49 +020034
35#define TRACE(x...) debug_sprintf_event(zcore_dbf, 1, x)
Michael Holzheu411ed322007-04-27 16:01:49 +020036
Michael Holzheu6f79d332013-09-11 14:24:54 -070037#define TO_USER 1
38#define TO_KERNEL 0
Frank Munzert12e0c952008-07-17 17:16:40 +020039#define CHUNK_INFO_SIZE 34 /* 2 16-byte char, each followed by blank */
Michael Holzheu411ed322007-04-27 16:01:49 +020040
41enum arch_id {
42 ARCH_S390 = 0,
43 ARCH_S390X = 1,
44};
45
Frank Munzert099b7652009-03-26 15:23:43 +010046struct ipib_info {
47 unsigned long ipib;
48 u32 checksum;
49} __attribute__((packed));
50
Michael Holzheu411ed322007-04-27 16:01:49 +020051static struct debug_info *zcore_dbf;
52static int hsa_available;
53static struct dentry *zcore_dir;
Frank Munzert12e0c952008-07-17 17:16:40 +020054static struct dentry *zcore_memmap_file;
Frank Munzert099b7652009-03-26 15:23:43 +010055static struct dentry *zcore_reipl_file;
Michael Holzheub4b3d122013-01-21 18:37:41 +010056static struct dentry *zcore_hsa_file;
Frank Munzert099b7652009-03-26 15:23:43 +010057static struct ipl_parameter_block *ipl_block;
Michael Holzheu411ed322007-04-27 16:01:49 +020058
59/*
60 * Copy memory from HSA to kernel or user memory (not reentrant):
61 *
62 * @dest: Kernel or user buffer where memory should be copied to
63 * @src: Start address within HSA where data should be copied
64 * @count: Size of buffer, which should be copied
65 * @mode: Either TO_KERNEL or TO_USER
66 */
Martin Schwidefskydf9694c2015-10-12 10:43:37 +020067static int memcpy_hsa(void *dest, unsigned long src, size_t count, int mode)
Michael Holzheu411ed322007-04-27 16:01:49 +020068{
69 int offs, blk_num;
70 static char buf[PAGE_SIZE] __attribute__((__aligned__(PAGE_SIZE)));
71
Michael Holzheub4b3d122013-01-21 18:37:41 +010072 if (!hsa_available)
73 return -ENODATA;
Michael Holzheu411ed322007-04-27 16:01:49 +020074 if (count == 0)
75 return 0;
76
77 /* copy first block */
78 offs = 0;
79 if ((src % PAGE_SIZE) != 0) {
80 blk_num = src / PAGE_SIZE + 2;
81 if (sclp_sdias_copy(buf, blk_num, 1)) {
82 TRACE("sclp_sdias_copy() failed\n");
83 return -EIO;
84 }
85 offs = min((PAGE_SIZE - (src % PAGE_SIZE)), count);
86 if (mode == TO_USER) {
87 if (copy_to_user((__force __user void*) dest,
88 buf + (src % PAGE_SIZE), offs))
89 return -EFAULT;
90 } else
91 memcpy(dest, buf + (src % PAGE_SIZE), offs);
92 }
93 if (offs == count)
94 goto out;
95
96 /* copy middle */
97 for (; (offs + PAGE_SIZE) <= count; offs += PAGE_SIZE) {
98 blk_num = (src + offs) / PAGE_SIZE + 2;
99 if (sclp_sdias_copy(buf, blk_num, 1)) {
100 TRACE("sclp_sdias_copy() failed\n");
101 return -EIO;
102 }
103 if (mode == TO_USER) {
104 if (copy_to_user((__force __user void*) dest + offs,
105 buf, PAGE_SIZE))
106 return -EFAULT;
107 } else
108 memcpy(dest + offs, buf, PAGE_SIZE);
109 }
110 if (offs == count)
111 goto out;
112
113 /* copy last block */
114 blk_num = (src + offs) / PAGE_SIZE + 2;
115 if (sclp_sdias_copy(buf, blk_num, 1)) {
116 TRACE("sclp_sdias_copy() failed\n");
117 return -EIO;
118 }
119 if (mode == TO_USER) {
120 if (copy_to_user((__force __user void*) dest + offs, buf,
Michael Holzheu241fd9b2013-04-19 18:03:02 +0200121 count - offs))
Michael Holzheu411ed322007-04-27 16:01:49 +0200122 return -EFAULT;
123 } else
124 memcpy(dest + offs, buf, count - offs);
125out:
126 return 0;
127}
128
Martin Schwidefskydf9694c2015-10-12 10:43:37 +0200129/*
130 * Copy memory from HSA to user memory (not reentrant):
131 *
132 * @dest: Kernel or user buffer where memory should be copied to
133 * @src: Start address within HSA where data should be copied
134 * @count: Size of buffer, which should be copied
135 */
136int memcpy_hsa_user(void __user *dest, unsigned long src, size_t count)
Michael Holzheu411ed322007-04-27 16:01:49 +0200137{
138 return memcpy_hsa((void __force *) dest, src, count, TO_USER);
139}
140
Martin Schwidefskydf9694c2015-10-12 10:43:37 +0200141/*
142 * Copy memory from HSA to kernel memory (not reentrant):
143 *
144 * @dest: Kernel or user buffer where memory should be copied to
145 * @src: Start address within HSA where data should be copied
146 * @count: Size of buffer, which should be copied
147 */
148int memcpy_hsa_kernel(void *dest, unsigned long src, size_t count)
Michael Holzheu411ed322007-04-27 16:01:49 +0200149{
150 return memcpy_hsa(dest, src, count, TO_KERNEL);
151}
152
Martin Schwidefskyffa52d02015-10-28 09:47:58 +0100153static int __init init_cpu_info(void)
Michael Holzheu411ed322007-04-27 16:01:49 +0200154{
Michael Holzheua62bc072014-10-06 17:57:43 +0200155 struct save_area_ext *sa_ext;
Michael Holzheu411ed322007-04-27 16:01:49 +0200156
157 /* get info for boot cpu from lowcore, stored in the HSA */
158
Michael Holzheu1592a8e2015-05-26 19:05:23 +0200159 sa_ext = dump_save_areas.areas[0];
Michael Holzheua62bc072014-10-06 17:57:43 +0200160 if (!sa_ext)
Michael Holzheu411ed322007-04-27 16:01:49 +0200161 return -ENOMEM;
Martin Schwidefskyffa52d02015-10-28 09:47:58 +0100162 if (memcpy_hsa_kernel(&sa_ext->sa, SAVE_AREA_BASE,
163 sizeof(struct save_area)) < 0) {
Michael Holzheu2a062ab2008-07-14 09:59:38 +0200164 TRACE("could not copy from HSA\n");
Michael Holzheu411ed322007-04-27 16:01:49 +0200165 return -EIO;
166 }
Martin Schwidefskyffa52d02015-10-28 09:47:58 +0100167 if (MACHINE_HAS_VX)
168 save_vx_regs_safe(sa_ext->vx_regs);
Michael Holzheu411ed322007-04-27 16:01:49 +0200169 return 0;
170}
171
172/*
Michael Holzheub4b3d122013-01-21 18:37:41 +0100173 * Release the HSA
174 */
175static void release_hsa(void)
176{
177 diag308(DIAG308_REL_HSA, NULL);
178 hsa_available = 0;
179}
180
Frank Munzert12e0c952008-07-17 17:16:40 +0200181static ssize_t zcore_memmap_read(struct file *filp, char __user *buf,
182 size_t count, loff_t *ppos)
183{
184 return simple_read_from_buffer(buf, count, ppos, filp->private_data,
Philipp Hachtmann50be6342014-01-29 18:16:01 +0100185 memblock.memory.cnt * CHUNK_INFO_SIZE);
Frank Munzert12e0c952008-07-17 17:16:40 +0200186}
187
188static int zcore_memmap_open(struct inode *inode, struct file *filp)
189{
Philipp Hachtmann50be6342014-01-29 18:16:01 +0100190 struct memblock_region *reg;
Frank Munzert12e0c952008-07-17 17:16:40 +0200191 char *buf;
Philipp Hachtmann50be6342014-01-29 18:16:01 +0100192 int i = 0;
Frank Munzert12e0c952008-07-17 17:16:40 +0200193
Philipp Hachtmann50be6342014-01-29 18:16:01 +0100194 buf = kzalloc(memblock.memory.cnt * CHUNK_INFO_SIZE, GFP_KERNEL);
Frank Munzert12e0c952008-07-17 17:16:40 +0200195 if (!buf) {
Frank Munzert12e0c952008-07-17 17:16:40 +0200196 return -ENOMEM;
197 }
Philipp Hachtmann50be6342014-01-29 18:16:01 +0100198 for_each_memblock(memory, reg) {
199 sprintf(buf + (i++ * CHUNK_INFO_SIZE), "%016llx %016llx ",
200 (unsigned long long) reg->base,
201 (unsigned long long) reg->size);
Frank Munzert12e0c952008-07-17 17:16:40 +0200202 }
Frank Munzert12e0c952008-07-17 17:16:40 +0200203 filp->private_data = buf;
Martin Schwidefsky58ea91c2010-05-17 10:00:07 +0200204 return nonseekable_open(inode, filp);
Frank Munzert12e0c952008-07-17 17:16:40 +0200205}
206
207static int zcore_memmap_release(struct inode *inode, struct file *filp)
208{
209 kfree(filp->private_data);
210 return 0;
211}
212
213static const struct file_operations zcore_memmap_fops = {
214 .owner = THIS_MODULE,
215 .read = zcore_memmap_read,
216 .open = zcore_memmap_open,
217 .release = zcore_memmap_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200218 .llseek = no_llseek,
Frank Munzert12e0c952008-07-17 17:16:40 +0200219};
220
Frank Munzert099b7652009-03-26 15:23:43 +0100221static ssize_t zcore_reipl_write(struct file *filp, const char __user *buf,
222 size_t count, loff_t *ppos)
223{
224 if (ipl_block) {
225 diag308(DIAG308_SET, ipl_block);
226 diag308(DIAG308_IPL, NULL);
227 }
228 return count;
229}
230
231static int zcore_reipl_open(struct inode *inode, struct file *filp)
232{
Martin Schwidefsky58ea91c2010-05-17 10:00:07 +0200233 return nonseekable_open(inode, filp);
Frank Munzert099b7652009-03-26 15:23:43 +0100234}
235
236static int zcore_reipl_release(struct inode *inode, struct file *filp)
237{
238 return 0;
239}
240
241static const struct file_operations zcore_reipl_fops = {
242 .owner = THIS_MODULE,
243 .write = zcore_reipl_write,
244 .open = zcore_reipl_open,
245 .release = zcore_reipl_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200246 .llseek = no_llseek,
Frank Munzert099b7652009-03-26 15:23:43 +0100247};
248
Michael Holzheub4b3d122013-01-21 18:37:41 +0100249static ssize_t zcore_hsa_read(struct file *filp, char __user *buf,
250 size_t count, loff_t *ppos)
251{
252 static char str[18];
253
254 if (hsa_available)
David Hildenbrand37c5f6c2015-05-06 13:18:59 +0200255 snprintf(str, sizeof(str), "%lx\n", sclp.hsa_size);
Michael Holzheub4b3d122013-01-21 18:37:41 +0100256 else
257 snprintf(str, sizeof(str), "0\n");
258 return simple_read_from_buffer(buf, count, ppos, str, strlen(str));
259}
260
261static ssize_t zcore_hsa_write(struct file *filp, const char __user *buf,
262 size_t count, loff_t *ppos)
263{
264 char value;
265
266 if (*ppos != 0)
267 return -EPIPE;
268 if (copy_from_user(&value, buf, 1))
269 return -EFAULT;
270 if (value != '0')
271 return -EINVAL;
272 release_hsa();
273 return count;
274}
275
276static const struct file_operations zcore_hsa_fops = {
277 .owner = THIS_MODULE,
278 .write = zcore_hsa_write,
279 .read = zcore_hsa_read,
280 .open = nonseekable_open,
281 .llseek = no_llseek,
282};
283
Michael Holzheu411ed322007-04-27 16:01:49 +0200284static int __init check_sdias(void)
285{
David Hildenbrand37c5f6c2015-05-06 13:18:59 +0200286 if (!sclp.hsa_size) {
Michael Holzheu2a062ab2008-07-14 09:59:38 +0200287 TRACE("Could not determine HSA size\n");
Michael Holzheue657d8f2013-11-13 10:38:27 +0100288 return -ENODEV;
Michael Holzheu411ed322007-04-27 16:01:49 +0200289 }
290 return 0;
291}
292
Frank Munzert099b7652009-03-26 15:23:43 +0100293/*
294 * Provide IPL parameter information block from either HSA or memory
295 * for future reipl
296 */
297static int __init zcore_reipl_init(void)
298{
299 struct ipib_info ipib_info;
300 int rc;
301
302 rc = memcpy_hsa_kernel(&ipib_info, __LC_DUMP_REIPL, sizeof(ipib_info));
303 if (rc)
304 return rc;
305 if (ipib_info.ipib == 0)
306 return 0;
307 ipl_block = (void *) __get_free_page(GFP_KERNEL);
308 if (!ipl_block)
309 return -ENOMEM;
David Hildenbrand37c5f6c2015-05-06 13:18:59 +0200310 if (ipib_info.ipib < sclp.hsa_size)
Frank Munzert099b7652009-03-26 15:23:43 +0100311 rc = memcpy_hsa_kernel(ipl_block, ipib_info.ipib, PAGE_SIZE);
312 else
Michael Holzheu92fe3132010-03-24 11:49:50 +0100313 rc = memcpy_real(ipl_block, (void *) ipib_info.ipib, PAGE_SIZE);
Michael Holzheu76ef9642010-04-22 17:17:07 +0200314 if (rc || csum_partial(ipl_block, ipl_block->hdr.len, 0) !=
Frank Munzert159d1ff2009-03-26 15:24:45 +0100315 ipib_info.checksum) {
Frank Munzert099b7652009-03-26 15:23:43 +0100316 TRACE("Checksum does not match\n");
317 free_page((unsigned long) ipl_block);
318 ipl_block = NULL;
319 }
320 return 0;
321}
322
Michael Holzheu411ed322007-04-27 16:01:49 +0200323static int __init zcore_init(void)
324{
325 unsigned char arch;
326 int rc;
327
328 if (ipl_info.type != IPL_TYPE_FCP_DUMP)
329 return -ENODATA;
Michael Holzheu3f25dc42011-11-14 11:19:05 +0100330 if (OLDMEM_BASE)
331 return -ENODATA;
Michael Holzheu411ed322007-04-27 16:01:49 +0200332
333 zcore_dbf = debug_register("zcore", 4, 1, 4 * sizeof(long));
334 debug_register_view(zcore_dbf, &debug_sprintf_view);
335 debug_set_level(zcore_dbf, 6);
336
337 TRACE("devno: %x\n", ipl_info.data.fcp.dev_id.devno);
338 TRACE("wwpn: %llx\n", (unsigned long long) ipl_info.data.fcp.wwpn);
339 TRACE("lun: %llx\n", (unsigned long long) ipl_info.data.fcp.lun);
340
Heiko Carstens763968e2007-05-10 15:45:46 +0200341 rc = sclp_sdias_init();
Michael Holzheu411ed322007-04-27 16:01:49 +0200342 if (rc)
343 goto fail;
344
345 rc = check_sdias();
Michael Holzheu2a062ab2008-07-14 09:59:38 +0200346 if (rc)
Michael Holzheu411ed322007-04-27 16:01:49 +0200347 goto fail;
Michael Holzheub4b3d122013-01-21 18:37:41 +0100348 hsa_available = 1;
Michael Holzheu411ed322007-04-27 16:01:49 +0200349
350 rc = memcpy_hsa_kernel(&arch, __LC_AR_MODE_ID, 1);
Michael Holzheu2a062ab2008-07-14 09:59:38 +0200351 if (rc)
Michael Holzheu411ed322007-04-27 16:01:49 +0200352 goto fail;
Michael Holzheu411ed322007-04-27 16:01:49 +0200353
Heiko Carstensf64ca212010-02-26 22:37:32 +0100354 if (arch == ARCH_S390) {
355 pr_alert("The 64-bit dump tool cannot be used for a "
356 "32-bit system\n");
357 rc = -EINVAL;
358 goto fail;
359 }
Michael Holzheu411ed322007-04-27 16:01:49 +0200360
Martin Schwidefskyffa52d02015-10-28 09:47:58 +0100361 pr_alert("DETECTED 'S390X (64 bit) OS'\n");
362 rc = init_cpu_info();
Michael Holzheu2a062ab2008-07-14 09:59:38 +0200363 if (rc)
Michael Holzheu411ed322007-04-27 16:01:49 +0200364 goto fail;
Michael Holzheu411ed322007-04-27 16:01:49 +0200365
Frank Munzert099b7652009-03-26 15:23:43 +0100366 rc = zcore_reipl_init();
367 if (rc)
368 goto fail;
369
Michael Holzheu411ed322007-04-27 16:01:49 +0200370 zcore_dir = debugfs_create_dir("zcore" , NULL);
371 if (!zcore_dir) {
372 rc = -ENOMEM;
373 goto fail;
374 }
Frank Munzert12e0c952008-07-17 17:16:40 +0200375 zcore_memmap_file = debugfs_create_file("memmap", S_IRUSR, zcore_dir,
376 NULL, &zcore_memmap_fops);
377 if (!zcore_memmap_file) {
378 rc = -ENOMEM;
Martin Schwidefskyffa52d02015-10-28 09:47:58 +0100379 goto fail_dir;
Michael Holzheu411ed322007-04-27 16:01:49 +0200380 }
Frank Munzert099b7652009-03-26 15:23:43 +0100381 zcore_reipl_file = debugfs_create_file("reipl", S_IRUSR, zcore_dir,
382 NULL, &zcore_reipl_fops);
383 if (!zcore_reipl_file) {
384 rc = -ENOMEM;
385 goto fail_memmap_file;
386 }
Michael Holzheub4b3d122013-01-21 18:37:41 +0100387 zcore_hsa_file = debugfs_create_file("hsa", S_IRUSR|S_IWUSR, zcore_dir,
388 NULL, &zcore_hsa_fops);
389 if (!zcore_hsa_file) {
390 rc = -ENOMEM;
391 goto fail_reipl_file;
392 }
Michael Holzheu411ed322007-04-27 16:01:49 +0200393 return 0;
394
Michael Holzheub4b3d122013-01-21 18:37:41 +0100395fail_reipl_file:
396 debugfs_remove(zcore_reipl_file);
Frank Munzert099b7652009-03-26 15:23:43 +0100397fail_memmap_file:
398 debugfs_remove(zcore_memmap_file);
Frank Munzert12e0c952008-07-17 17:16:40 +0200399fail_dir:
400 debugfs_remove(zcore_dir);
Michael Holzheu411ed322007-04-27 16:01:49 +0200401fail:
402 diag308(DIAG308_REL_HSA, NULL);
403 return rc;
404}
405
Michael Holzheu411ed322007-04-27 16:01:49 +0200406static void __exit zcore_exit(void)
407{
408 debug_unregister(zcore_dbf);
Heiko Carstens763968e2007-05-10 15:45:46 +0200409 sclp_sdias_exit();
Frank Munzert099b7652009-03-26 15:23:43 +0100410 free_page((unsigned long) ipl_block);
Michael Holzheub4b3d122013-01-21 18:37:41 +0100411 debugfs_remove(zcore_hsa_file);
Frank Munzert099b7652009-03-26 15:23:43 +0100412 debugfs_remove(zcore_reipl_file);
413 debugfs_remove(zcore_memmap_file);
Frank Munzert099b7652009-03-26 15:23:43 +0100414 debugfs_remove(zcore_dir);
Michael Holzheu411ed322007-04-27 16:01:49 +0200415 diag308(DIAG308_REL_HSA, NULL);
416}
417
Frank Munzert099b7652009-03-26 15:23:43 +0100418MODULE_AUTHOR("Copyright IBM Corp. 2003,2008");
Michael Holzheu411ed322007-04-27 16:01:49 +0200419MODULE_DESCRIPTION("zcore module for zfcpdump support");
420MODULE_LICENSE("GPL");
421
422subsys_initcall(zcore_init);
423module_exit(zcore_exit);