GNU Linux-libre 4.19.286-gnu1
[releases.git] / drivers / s390 / char / zcore.c
1 // SPDX-License-Identifier: GPL-1.0+
2 /*
3  * zcore module to export memory content and register sets for creating system
4  * dumps on SCSI disks (zfcpdump). The "zcore/mem" debugfs file shows the same
5  * dump format as s390 standalone dumps.
6  *
7  * For more information please refer to Documentation/s390/zfcpdump.txt
8  *
9  * Copyright IBM Corp. 2003, 2008
10  * Author(s): Michael Holzheu
11  */
12
13 #define KMSG_COMPONENT "zdump"
14 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
15
16 #include <linux/init.h>
17 #include <linux/slab.h>
18 #include <linux/debugfs.h>
19 #include <linux/memblock.h>
20
21 #include <asm/asm-offsets.h>
22 #include <asm/ipl.h>
23 #include <asm/sclp.h>
24 #include <asm/setup.h>
25 #include <linux/uaccess.h>
26 #include <asm/debug.h>
27 #include <asm/processor.h>
28 #include <asm/irqflags.h>
29 #include <asm/checksum.h>
30 #include <asm/os_info.h>
31 #include <asm/switch_to.h>
32 #include "sclp.h"
33
34 #define TRACE(x...) debug_sprintf_event(zcore_dbf, 1, x)
35
36 #define CHUNK_INFO_SIZE 34 /* 2 16-byte char, each followed by blank */
37
38 enum arch_id {
39         ARCH_S390       = 0,
40         ARCH_S390X      = 1,
41 };
42
43 struct ipib_info {
44         unsigned long   ipib;
45         u32             checksum;
46 }  __attribute__((packed));
47
48 static struct debug_info *zcore_dbf;
49 static int hsa_available;
50 static struct dentry *zcore_dir;
51 static struct dentry *zcore_memmap_file;
52 static struct dentry *zcore_reipl_file;
53 static struct dentry *zcore_hsa_file;
54 static struct ipl_parameter_block *ipl_block;
55
56 static DEFINE_MUTEX(hsa_buf_mutex);
57 static char hsa_buf[PAGE_SIZE] __aligned(PAGE_SIZE);
58
59 /*
60  * Copy memory from HSA to user memory (not reentrant):
61  *
62  * @dest:  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  */
66 int memcpy_hsa_user(void __user *dest, unsigned long src, size_t count)
67 {
68         unsigned long offset, bytes;
69
70         if (!hsa_available)
71                 return -ENODATA;
72
73         mutex_lock(&hsa_buf_mutex);
74         while (count) {
75                 if (sclp_sdias_copy(hsa_buf, src / PAGE_SIZE + 2, 1)) {
76                         TRACE("sclp_sdias_copy() failed\n");
77                         mutex_unlock(&hsa_buf_mutex);
78                         return -EIO;
79                 }
80                 offset = src % PAGE_SIZE;
81                 bytes = min(PAGE_SIZE - offset, count);
82                 if (copy_to_user(dest, hsa_buf + offset, bytes)) {
83                         mutex_unlock(&hsa_buf_mutex);
84                         return -EFAULT;
85                 }
86                 src += bytes;
87                 dest += bytes;
88                 count -= bytes;
89         }
90         mutex_unlock(&hsa_buf_mutex);
91         return 0;
92 }
93
94 /*
95  * Copy memory from HSA to kernel memory (not reentrant):
96  *
97  * @dest:  Kernel or user buffer where memory should be copied to
98  * @src:   Start address within HSA where data should be copied
99  * @count: Size of buffer, which should be copied
100  */
101 int memcpy_hsa_kernel(void *dest, unsigned long src, size_t count)
102 {
103         unsigned long offset, bytes;
104
105         if (!hsa_available)
106                 return -ENODATA;
107
108         mutex_lock(&hsa_buf_mutex);
109         while (count) {
110                 if (sclp_sdias_copy(hsa_buf, src / PAGE_SIZE + 2, 1)) {
111                         TRACE("sclp_sdias_copy() failed\n");
112                         mutex_unlock(&hsa_buf_mutex);
113                         return -EIO;
114                 }
115                 offset = src % PAGE_SIZE;
116                 bytes = min(PAGE_SIZE - offset, count);
117                 memcpy(dest, hsa_buf + offset, bytes);
118                 src += bytes;
119                 dest += bytes;
120                 count -= bytes;
121         }
122         mutex_unlock(&hsa_buf_mutex);
123         return 0;
124 }
125
126 static int __init init_cpu_info(void)
127 {
128         struct save_area *sa;
129
130         /* get info for boot cpu from lowcore, stored in the HSA */
131         sa = save_area_boot_cpu();
132         if (!sa)
133                 return -ENOMEM;
134         if (memcpy_hsa_kernel(hsa_buf, __LC_FPREGS_SAVE_AREA, 512) < 0) {
135                 TRACE("could not copy from HSA\n");
136                 return -EIO;
137         }
138         save_area_add_regs(sa, hsa_buf); /* vx registers are saved in smp.c */
139         return 0;
140 }
141
142 /*
143  * Release the HSA
144  */
145 static void release_hsa(void)
146 {
147         diag308(DIAG308_REL_HSA, NULL);
148         hsa_available = 0;
149 }
150
151 static ssize_t zcore_memmap_read(struct file *filp, char __user *buf,
152                                  size_t count, loff_t *ppos)
153 {
154         return simple_read_from_buffer(buf, count, ppos, filp->private_data,
155                                        memblock.memory.cnt * CHUNK_INFO_SIZE);
156 }
157
158 static int zcore_memmap_open(struct inode *inode, struct file *filp)
159 {
160         struct memblock_region *reg;
161         char *buf;
162         int i = 0;
163
164         buf = kcalloc(memblock.memory.cnt, CHUNK_INFO_SIZE, GFP_KERNEL);
165         if (!buf) {
166                 return -ENOMEM;
167         }
168         for_each_memblock(memory, reg) {
169                 sprintf(buf + (i++ * CHUNK_INFO_SIZE), "%016llx %016llx ",
170                         (unsigned long long) reg->base,
171                         (unsigned long long) reg->size);
172         }
173         filp->private_data = buf;
174         return nonseekable_open(inode, filp);
175 }
176
177 static int zcore_memmap_release(struct inode *inode, struct file *filp)
178 {
179         kfree(filp->private_data);
180         return 0;
181 }
182
183 static const struct file_operations zcore_memmap_fops = {
184         .owner          = THIS_MODULE,
185         .read           = zcore_memmap_read,
186         .open           = zcore_memmap_open,
187         .release        = zcore_memmap_release,
188         .llseek         = no_llseek,
189 };
190
191 static ssize_t zcore_reipl_write(struct file *filp, const char __user *buf,
192                                  size_t count, loff_t *ppos)
193 {
194         if (ipl_block) {
195                 diag308(DIAG308_SET, ipl_block);
196                 diag308(DIAG308_LOAD_CLEAR, NULL);
197         }
198         return count;
199 }
200
201 static int zcore_reipl_open(struct inode *inode, struct file *filp)
202 {
203         return nonseekable_open(inode, filp);
204 }
205
206 static int zcore_reipl_release(struct inode *inode, struct file *filp)
207 {
208         return 0;
209 }
210
211 static const struct file_operations zcore_reipl_fops = {
212         .owner          = THIS_MODULE,
213         .write          = zcore_reipl_write,
214         .open           = zcore_reipl_open,
215         .release        = zcore_reipl_release,
216         .llseek         = no_llseek,
217 };
218
219 static ssize_t zcore_hsa_read(struct file *filp, char __user *buf,
220                               size_t count, loff_t *ppos)
221 {
222         static char str[18];
223
224         if (hsa_available)
225                 snprintf(str, sizeof(str), "%lx\n", sclp.hsa_size);
226         else
227                 snprintf(str, sizeof(str), "0\n");
228         return simple_read_from_buffer(buf, count, ppos, str, strlen(str));
229 }
230
231 static ssize_t zcore_hsa_write(struct file *filp, const char __user *buf,
232                                size_t count, loff_t *ppos)
233 {
234         char value;
235
236         if (*ppos != 0)
237                 return -EPIPE;
238         if (copy_from_user(&value, buf, 1))
239                 return -EFAULT;
240         if (value != '0')
241                 return -EINVAL;
242         release_hsa();
243         return count;
244 }
245
246 static const struct file_operations zcore_hsa_fops = {
247         .owner          = THIS_MODULE,
248         .write          = zcore_hsa_write,
249         .read           = zcore_hsa_read,
250         .open           = nonseekable_open,
251         .llseek         = no_llseek,
252 };
253
254 static int __init check_sdias(void)
255 {
256         if (!sclp.hsa_size) {
257                 TRACE("Could not determine HSA size\n");
258                 return -ENODEV;
259         }
260         return 0;
261 }
262
263 /*
264  * Provide IPL parameter information block from either HSA or memory
265  * for future reipl
266  */
267 static int __init zcore_reipl_init(void)
268 {
269         struct ipib_info ipib_info;
270         int rc;
271
272         rc = memcpy_hsa_kernel(&ipib_info, __LC_DUMP_REIPL, sizeof(ipib_info));
273         if (rc)
274                 return rc;
275         if (ipib_info.ipib == 0)
276                 return 0;
277         ipl_block = (void *) __get_free_page(GFP_KERNEL);
278         if (!ipl_block)
279                 return -ENOMEM;
280         if (ipib_info.ipib < sclp.hsa_size)
281                 rc = memcpy_hsa_kernel(ipl_block, ipib_info.ipib, PAGE_SIZE);
282         else
283                 rc = memcpy_real(ipl_block, (void *) ipib_info.ipib, PAGE_SIZE);
284         if (rc || (__force u32)csum_partial(ipl_block, ipl_block->hdr.len, 0) !=
285             ipib_info.checksum) {
286                 TRACE("Checksum does not match\n");
287                 free_page((unsigned long) ipl_block);
288                 ipl_block = NULL;
289         }
290         return 0;
291 }
292
293 static int __init zcore_init(void)
294 {
295         unsigned char arch;
296         int rc;
297
298         if (ipl_info.type != IPL_TYPE_FCP_DUMP)
299                 return -ENODATA;
300         if (OLDMEM_BASE)
301                 return -ENODATA;
302
303         zcore_dbf = debug_register("zcore", 4, 1, 4 * sizeof(long));
304         debug_register_view(zcore_dbf, &debug_sprintf_view);
305         debug_set_level(zcore_dbf, 6);
306
307         TRACE("devno:  %x\n", ipl_info.data.fcp.dev_id.devno);
308         TRACE("wwpn:   %llx\n", (unsigned long long) ipl_info.data.fcp.wwpn);
309         TRACE("lun:    %llx\n", (unsigned long long) ipl_info.data.fcp.lun);
310
311         rc = sclp_sdias_init();
312         if (rc)
313                 goto fail;
314
315         rc = check_sdias();
316         if (rc)
317                 goto fail;
318         hsa_available = 1;
319
320         rc = memcpy_hsa_kernel(&arch, __LC_AR_MODE_ID, 1);
321         if (rc)
322                 goto fail;
323
324         if (arch == ARCH_S390) {
325                 pr_alert("The 64-bit dump tool cannot be used for a "
326                          "32-bit system\n");
327                 rc = -EINVAL;
328                 goto fail;
329         }
330
331         pr_alert("The dump process started for a 64-bit operating system\n");
332         rc = init_cpu_info();
333         if (rc)
334                 goto fail;
335
336         rc = zcore_reipl_init();
337         if (rc)
338                 goto fail;
339
340         zcore_dir = debugfs_create_dir("zcore" , NULL);
341         if (!zcore_dir) {
342                 rc = -ENOMEM;
343                 goto fail;
344         }
345         zcore_memmap_file = debugfs_create_file("memmap", S_IRUSR, zcore_dir,
346                                                 NULL, &zcore_memmap_fops);
347         if (!zcore_memmap_file) {
348                 rc = -ENOMEM;
349                 goto fail_dir;
350         }
351         zcore_reipl_file = debugfs_create_file("reipl", S_IRUSR, zcore_dir,
352                                                 NULL, &zcore_reipl_fops);
353         if (!zcore_reipl_file) {
354                 rc = -ENOMEM;
355                 goto fail_memmap_file;
356         }
357         zcore_hsa_file = debugfs_create_file("hsa", S_IRUSR|S_IWUSR, zcore_dir,
358                                              NULL, &zcore_hsa_fops);
359         if (!zcore_hsa_file) {
360                 rc = -ENOMEM;
361                 goto fail_reipl_file;
362         }
363         return 0;
364
365 fail_reipl_file:
366         debugfs_remove(zcore_reipl_file);
367 fail_memmap_file:
368         debugfs_remove(zcore_memmap_file);
369 fail_dir:
370         debugfs_remove(zcore_dir);
371 fail:
372         diag308(DIAG308_REL_HSA, NULL);
373         return rc;
374 }
375 subsys_initcall(zcore_init);