xref: /openbmc/linux/drivers/s390/char/zcore.c (revision 31e67366)
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/NVMe disks (zfcp/nvme dump).
5  *
6  * For more information please refer to Documentation/s390/zfcpdump.rst
7  *
8  * Copyright IBM Corp. 2003, 2008
9  * Author(s): Michael Holzheu
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/debugfs.h>
18 
19 #include <asm/asm-offsets.h>
20 #include <asm/ipl.h>
21 #include <asm/sclp.h>
22 #include <asm/setup.h>
23 #include <linux/uaccess.h>
24 #include <asm/debug.h>
25 #include <asm/processor.h>
26 #include <asm/irqflags.h>
27 #include <asm/checksum.h>
28 #include <asm/os_info.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 enum arch_id {
35 	ARCH_S390	= 0,
36 	ARCH_S390X	= 1,
37 };
38 
39 struct ipib_info {
40 	unsigned long	ipib;
41 	u32		checksum;
42 }  __attribute__((packed));
43 
44 static struct debug_info *zcore_dbf;
45 static int hsa_available;
46 static struct dentry *zcore_dir;
47 static struct dentry *zcore_reipl_file;
48 static struct dentry *zcore_hsa_file;
49 static struct ipl_parameter_block *zcore_ipl_block;
50 
51 static char hsa_buf[PAGE_SIZE] __aligned(PAGE_SIZE);
52 
53 /*
54  * Copy memory from HSA to user memory (not reentrant):
55  *
56  * @dest:  User buffer where memory should be copied to
57  * @src:   Start address within HSA where data should be copied
58  * @count: Size of buffer, which should be copied
59  */
60 int memcpy_hsa_user(void __user *dest, unsigned long src, size_t count)
61 {
62 	unsigned long offset, bytes;
63 
64 	if (!hsa_available)
65 		return -ENODATA;
66 
67 	while (count) {
68 		if (sclp_sdias_copy(hsa_buf, src / PAGE_SIZE + 2, 1)) {
69 			TRACE("sclp_sdias_copy() failed\n");
70 			return -EIO;
71 		}
72 		offset = src % PAGE_SIZE;
73 		bytes = min(PAGE_SIZE - offset, count);
74 		if (copy_to_user(dest, hsa_buf + offset, bytes))
75 			return -EFAULT;
76 		src += bytes;
77 		dest += bytes;
78 		count -= bytes;
79 	}
80 	return 0;
81 }
82 
83 /*
84  * Copy memory from HSA to kernel memory (not reentrant):
85  *
86  * @dest:  Kernel or user buffer where memory should be copied to
87  * @src:   Start address within HSA where data should be copied
88  * @count: Size of buffer, which should be copied
89  */
90 int memcpy_hsa_kernel(void *dest, unsigned long src, size_t count)
91 {
92 	unsigned long offset, bytes;
93 
94 	if (!hsa_available)
95 		return -ENODATA;
96 
97 	while (count) {
98 		if (sclp_sdias_copy(hsa_buf, src / PAGE_SIZE + 2, 1)) {
99 			TRACE("sclp_sdias_copy() failed\n");
100 			return -EIO;
101 		}
102 		offset = src % PAGE_SIZE;
103 		bytes = min(PAGE_SIZE - offset, count);
104 		memcpy(dest, hsa_buf + offset, bytes);
105 		src += bytes;
106 		dest += bytes;
107 		count -= bytes;
108 	}
109 	return 0;
110 }
111 
112 static int __init init_cpu_info(void)
113 {
114 	struct save_area *sa;
115 
116 	/* get info for boot cpu from lowcore, stored in the HSA */
117 	sa = save_area_boot_cpu();
118 	if (!sa)
119 		return -ENOMEM;
120 	if (memcpy_hsa_kernel(hsa_buf, __LC_FPREGS_SAVE_AREA, 512) < 0) {
121 		TRACE("could not copy from HSA\n");
122 		return -EIO;
123 	}
124 	save_area_add_regs(sa, hsa_buf); /* vx registers are saved in smp.c */
125 	return 0;
126 }
127 
128 /*
129  * Release the HSA
130  */
131 static void release_hsa(void)
132 {
133 	diag308(DIAG308_REL_HSA, NULL);
134 	hsa_available = 0;
135 }
136 
137 static ssize_t zcore_reipl_write(struct file *filp, const char __user *buf,
138 				 size_t count, loff_t *ppos)
139 {
140 	if (zcore_ipl_block) {
141 		diag308(DIAG308_SET, zcore_ipl_block);
142 		diag308(DIAG308_LOAD_CLEAR, NULL);
143 	}
144 	return count;
145 }
146 
147 static int zcore_reipl_open(struct inode *inode, struct file *filp)
148 {
149 	return stream_open(inode, filp);
150 }
151 
152 static int zcore_reipl_release(struct inode *inode, struct file *filp)
153 {
154 	return 0;
155 }
156 
157 static const struct file_operations zcore_reipl_fops = {
158 	.owner		= THIS_MODULE,
159 	.write		= zcore_reipl_write,
160 	.open		= zcore_reipl_open,
161 	.release	= zcore_reipl_release,
162 	.llseek		= no_llseek,
163 };
164 
165 static ssize_t zcore_hsa_read(struct file *filp, char __user *buf,
166 			      size_t count, loff_t *ppos)
167 {
168 	static char str[18];
169 
170 	if (hsa_available)
171 		snprintf(str, sizeof(str), "%lx\n", sclp.hsa_size);
172 	else
173 		snprintf(str, sizeof(str), "0\n");
174 	return simple_read_from_buffer(buf, count, ppos, str, strlen(str));
175 }
176 
177 static ssize_t zcore_hsa_write(struct file *filp, const char __user *buf,
178 			       size_t count, loff_t *ppos)
179 {
180 	char value;
181 
182 	if (*ppos != 0)
183 		return -EPIPE;
184 	if (copy_from_user(&value, buf, 1))
185 		return -EFAULT;
186 	if (value != '0')
187 		return -EINVAL;
188 	release_hsa();
189 	return count;
190 }
191 
192 static const struct file_operations zcore_hsa_fops = {
193 	.owner		= THIS_MODULE,
194 	.write		= zcore_hsa_write,
195 	.read		= zcore_hsa_read,
196 	.open		= nonseekable_open,
197 	.llseek		= no_llseek,
198 };
199 
200 static int __init check_sdias(void)
201 {
202 	if (!sclp.hsa_size) {
203 		TRACE("Could not determine HSA size\n");
204 		return -ENODEV;
205 	}
206 	return 0;
207 }
208 
209 /*
210  * Provide IPL parameter information block from either HSA or memory
211  * for future reipl
212  */
213 static int __init zcore_reipl_init(void)
214 {
215 	struct ipib_info ipib_info;
216 	int rc;
217 
218 	rc = memcpy_hsa_kernel(&ipib_info, __LC_DUMP_REIPL, sizeof(ipib_info));
219 	if (rc)
220 		return rc;
221 	if (ipib_info.ipib == 0)
222 		return 0;
223 	zcore_ipl_block = (void *) __get_free_page(GFP_KERNEL);
224 	if (!zcore_ipl_block)
225 		return -ENOMEM;
226 	if (ipib_info.ipib < sclp.hsa_size)
227 		rc = memcpy_hsa_kernel(zcore_ipl_block, ipib_info.ipib,
228 				       PAGE_SIZE);
229 	else
230 		rc = memcpy_real(zcore_ipl_block, (void *) ipib_info.ipib,
231 				 PAGE_SIZE);
232 	if (rc || (__force u32)csum_partial(zcore_ipl_block, zcore_ipl_block->hdr.len, 0) !=
233 	    ipib_info.checksum) {
234 		TRACE("Checksum does not match\n");
235 		free_page((unsigned long) zcore_ipl_block);
236 		zcore_ipl_block = NULL;
237 	}
238 	return 0;
239 }
240 
241 static int __init zcore_init(void)
242 {
243 	unsigned char arch;
244 	int rc;
245 
246 	if (!is_ipl_type_dump())
247 		return -ENODATA;
248 	if (OLDMEM_BASE)
249 		return -ENODATA;
250 
251 	zcore_dbf = debug_register("zcore", 4, 1, 4 * sizeof(long));
252 	debug_register_view(zcore_dbf, &debug_sprintf_view);
253 	debug_set_level(zcore_dbf, 6);
254 
255 	if (ipl_info.type == IPL_TYPE_FCP_DUMP) {
256 		TRACE("type:   fcp\n");
257 		TRACE("devno:  %x\n", ipl_info.data.fcp.dev_id.devno);
258 		TRACE("wwpn:   %llx\n", (unsigned long long) ipl_info.data.fcp.wwpn);
259 		TRACE("lun:    %llx\n", (unsigned long long) ipl_info.data.fcp.lun);
260 	} else if (ipl_info.type == IPL_TYPE_NVME_DUMP) {
261 		TRACE("type:   nvme\n");
262 		TRACE("fid:    %x\n", ipl_info.data.nvme.fid);
263 		TRACE("nsid:   %x\n", ipl_info.data.nvme.nsid);
264 	}
265 
266 	rc = sclp_sdias_init();
267 	if (rc)
268 		goto fail;
269 
270 	rc = check_sdias();
271 	if (rc)
272 		goto fail;
273 	hsa_available = 1;
274 
275 	rc = memcpy_hsa_kernel(&arch, __LC_AR_MODE_ID, 1);
276 	if (rc)
277 		goto fail;
278 
279 	if (arch == ARCH_S390) {
280 		pr_alert("The 64-bit dump tool cannot be used for a "
281 			 "32-bit system\n");
282 		rc = -EINVAL;
283 		goto fail;
284 	}
285 
286 	pr_alert("The dump process started for a 64-bit operating system\n");
287 	rc = init_cpu_info();
288 	if (rc)
289 		goto fail;
290 
291 	rc = zcore_reipl_init();
292 	if (rc)
293 		goto fail;
294 
295 	zcore_dir = debugfs_create_dir("zcore" , NULL);
296 	if (!zcore_dir) {
297 		rc = -ENOMEM;
298 		goto fail;
299 	}
300 	zcore_reipl_file = debugfs_create_file("reipl", S_IRUSR, zcore_dir,
301 						NULL, &zcore_reipl_fops);
302 	if (!zcore_reipl_file) {
303 		rc = -ENOMEM;
304 		goto fail_dir;
305 	}
306 	zcore_hsa_file = debugfs_create_file("hsa", S_IRUSR|S_IWUSR, zcore_dir,
307 					     NULL, &zcore_hsa_fops);
308 	if (!zcore_hsa_file) {
309 		rc = -ENOMEM;
310 		goto fail_reipl_file;
311 	}
312 	return 0;
313 
314 fail_reipl_file:
315 	debugfs_remove(zcore_reipl_file);
316 fail_dir:
317 	debugfs_remove(zcore_dir);
318 fail:
319 	diag308(DIAG308_REL_HSA, NULL);
320 	return rc;
321 }
322 subsys_initcall(zcore_init);
323