xref: /linux/drivers/s390/char/zcore.c (revision 7fc2cd2e4b398c57c9cf961cfea05eadbf34c05c)
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/arch/s390/zfcpdump.rst
7  *
8  * Copyright IBM Corp. 2003, 2008
9  * Author(s): Michael Holzheu
10  */
11 
12 #define pr_fmt(fmt) "zdump: " fmt
13 
14 #include <linux/init.h>
15 #include <linux/slab.h>
16 #include <linux/debugfs.h>
17 #include <linux/panic_notifier.h>
18 #include <linux/reboot.h>
19 #include <linux/uio.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/maccess.h>
32 #include "sclp.h"
33 
34 #define TRACE(x...) debug_sprintf_event(zcore_dbf, 1, x)
35 
36 enum arch_id {
37 	ARCH_S390	= 0,
38 	ARCH_S390X	= 1,
39 };
40 
41 struct ipib_info {
42 	unsigned long	ipib;
43 	u32		checksum;
44 }  __attribute__((packed));
45 
46 static struct debug_info *zcore_dbf;
47 static int hsa_available;
48 static struct dentry *zcore_dir;
49 static struct dentry *zcore_reipl_file;
50 static struct dentry *zcore_hsa_file;
51 static struct ipl_parameter_block *zcore_ipl_block;
52 static unsigned long os_info_flags;
53 
54 static DEFINE_MUTEX(hsa_buf_mutex);
55 static char hsa_buf[PAGE_SIZE] __aligned(PAGE_SIZE);
56 
57 /*
58  * Copy memory from HSA to iterator (not reentrant):
59  *
60  * @iter:  Iterator 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  */
64 size_t memcpy_hsa_iter(struct iov_iter *iter, unsigned long src, size_t count)
65 {
66 	size_t bytes, copied, res = 0;
67 	unsigned long offset;
68 
69 	if (!hsa_available)
70 		return 0;
71 
72 	mutex_lock(&hsa_buf_mutex);
73 	while (count) {
74 		if (sclp_sdias_copy(hsa_buf, src / PAGE_SIZE + 2, 1)) {
75 			TRACE("sclp_sdias_copy() failed\n");
76 			break;
77 		}
78 		offset = src % PAGE_SIZE;
79 		bytes = min(PAGE_SIZE - offset, count);
80 		copied = copy_to_iter(hsa_buf + offset, bytes, iter);
81 		count -= copied;
82 		src += copied;
83 		res += copied;
84 		if (copied < bytes)
85 			break;
86 	}
87 	mutex_unlock(&hsa_buf_mutex);
88 	return res;
89 }
90 
91 /*
92  * Copy memory from HSA to kernel memory (not reentrant):
93  *
94  * @dest:  Kernel or user buffer where memory should be copied to
95  * @src:   Start address within HSA where data should be copied
96  * @count: Size of buffer, which should be copied
97  */
98 static inline int memcpy_hsa_kernel(void *dst, unsigned long src, size_t count)
99 {
100 	struct iov_iter iter;
101 	struct kvec kvec;
102 
103 	kvec.iov_base = dst;
104 	kvec.iov_len = count;
105 	iov_iter_kvec(&iter, ITER_DEST, &kvec, 1, count);
106 	if (memcpy_hsa_iter(&iter, src, count) < count)
107 		return -EIO;
108 	return 0;
109 }
110 
111 static int __init init_cpu_info(void)
112 {
113 	struct save_area *sa;
114 
115 	/* get info for boot cpu from lowcore, stored in the HSA */
116 	sa = save_area_boot_cpu();
117 	if (!sa)
118 		return -ENOMEM;
119 	if (memcpy_hsa_kernel(hsa_buf, __LC_FPREGS_SAVE_AREA, 512) < 0) {
120 		TRACE("could not copy from HSA\n");
121 		return -EIO;
122 	}
123 	save_area_add_regs(sa, hsa_buf); /* vx registers are saved in smp.c */
124 	return 0;
125 }
126 
127 /*
128  * Release the HSA
129  */
130 static void release_hsa(void)
131 {
132 	diag308(DIAG308_REL_HSA, NULL);
133 	hsa_available = 0;
134 }
135 
136 static ssize_t zcore_reipl_write(struct file *filp, const char __user *buf,
137 				 size_t count, loff_t *ppos)
138 {
139 	if (zcore_ipl_block) {
140 		diag308(DIAG308_SET, zcore_ipl_block);
141 		if (os_info_flags & OS_INFO_FLAG_REIPL_CLEAR)
142 			diag308(DIAG308_LOAD_CLEAR, NULL);
143 		/* Use special diag308 subcode for CCW normal ipl */
144 		if (zcore_ipl_block->pb0_hdr.pbt == IPL_PBT_CCW)
145 			diag308(DIAG308_LOAD_NORMAL_DUMP, NULL);
146 		else
147 			diag308(DIAG308_LOAD_NORMAL, NULL);
148 	}
149 	return count;
150 }
151 
152 static int zcore_reipl_open(struct inode *inode, struct file *filp)
153 {
154 	return stream_open(inode, filp);
155 }
156 
157 static int zcore_reipl_release(struct inode *inode, struct file *filp)
158 {
159 	return 0;
160 }
161 
162 static const struct file_operations zcore_reipl_fops = {
163 	.owner		= THIS_MODULE,
164 	.write		= zcore_reipl_write,
165 	.open		= zcore_reipl_open,
166 	.release	= zcore_reipl_release,
167 };
168 
169 static ssize_t zcore_hsa_read(struct file *filp, char __user *buf,
170 			      size_t count, loff_t *ppos)
171 {
172 	static char str[18];
173 
174 	if (hsa_available)
175 		snprintf(str, sizeof(str), "%lx\n", sclp.hsa_size);
176 	else
177 		snprintf(str, sizeof(str), "0\n");
178 	return simple_read_from_buffer(buf, count, ppos, str, strlen(str));
179 }
180 
181 static ssize_t zcore_hsa_write(struct file *filp, const char __user *buf,
182 			       size_t count, loff_t *ppos)
183 {
184 	char value;
185 
186 	if (*ppos != 0)
187 		return -EPIPE;
188 	if (copy_from_user(&value, buf, 1))
189 		return -EFAULT;
190 	if (value != '0')
191 		return -EINVAL;
192 	release_hsa();
193 	return count;
194 }
195 
196 static const struct file_operations zcore_hsa_fops = {
197 	.owner		= THIS_MODULE,
198 	.write		= zcore_hsa_write,
199 	.read		= zcore_hsa_read,
200 	.open		= nonseekable_open,
201 };
202 
203 static int __init check_sdias(void)
204 {
205 	if (!sclp.hsa_size) {
206 		TRACE("Could not determine HSA size\n");
207 		return -ENODEV;
208 	}
209 	return 0;
210 }
211 
212 /*
213  * Provide IPL parameter information block from either HSA or memory
214  * for future reipl
215  */
216 static int __init zcore_reipl_init(void)
217 {
218 	struct os_info_entry *entry;
219 	struct ipib_info ipib_info;
220 	unsigned long os_info_addr;
221 	struct os_info *os_info;
222 	int rc;
223 
224 	rc = memcpy_hsa_kernel(&ipib_info, __LC_DUMP_REIPL, sizeof(ipib_info));
225 	if (rc)
226 		return rc;
227 	if (ipib_info.ipib == 0)
228 		return 0;
229 	zcore_ipl_block = (void *) __get_free_page(GFP_KERNEL);
230 	if (!zcore_ipl_block)
231 		return -ENOMEM;
232 	if (ipib_info.ipib < sclp.hsa_size)
233 		rc = memcpy_hsa_kernel(zcore_ipl_block, ipib_info.ipib,
234 				       PAGE_SIZE);
235 	else
236 		rc = memcpy_real(zcore_ipl_block, ipib_info.ipib, PAGE_SIZE);
237 	if (rc || (__force u32)csum_partial(zcore_ipl_block, zcore_ipl_block->hdr.len, 0) !=
238 	    ipib_info.checksum) {
239 		TRACE("Checksum does not match\n");
240 		free_page((unsigned long) zcore_ipl_block);
241 		zcore_ipl_block = NULL;
242 	}
243 	/*
244 	 * Read the bit-flags field from os_info flags entry.
245 	 * Return zero even for os_info read or entry checksum errors in order
246 	 * to continue dump processing, considering that os_info could be
247 	 * corrupted on the panicked system.
248 	 */
249 	os_info = (void *)__get_free_page(GFP_KERNEL);
250 	if (!os_info)
251 		return -ENOMEM;
252 	rc = memcpy_hsa_kernel(&os_info_addr, __LC_OS_INFO, sizeof(os_info_addr));
253 	if (rc)
254 		goto out;
255 	if (os_info_addr < sclp.hsa_size)
256 		rc = memcpy_hsa_kernel(os_info, os_info_addr, PAGE_SIZE);
257 	else
258 		rc = memcpy_real(os_info, os_info_addr, PAGE_SIZE);
259 	if (rc || os_info_csum(os_info) != os_info->csum)
260 		goto out;
261 	entry = &os_info->entry[OS_INFO_FLAGS_ENTRY];
262 	if (entry->addr && entry->size) {
263 		if (entry->addr < sclp.hsa_size)
264 			rc = memcpy_hsa_kernel(&os_info_flags, entry->addr, sizeof(os_info_flags));
265 		else
266 			rc = memcpy_real(&os_info_flags, entry->addr, sizeof(os_info_flags));
267 		if (rc || (__force u32)csum_partial(&os_info_flags, entry->size, 0) != entry->csum)
268 			os_info_flags = 0;
269 	}
270 out:
271 	free_page((unsigned long)os_info);
272 	return 0;
273 }
274 
275 static int zcore_reboot_and_on_panic_handler(struct notifier_block *self,
276 					     unsigned long	   event,
277 					     void		   *data)
278 {
279 	if (hsa_available)
280 		release_hsa();
281 
282 	return NOTIFY_OK;
283 }
284 
285 static struct notifier_block zcore_reboot_notifier = {
286 	.notifier_call	= zcore_reboot_and_on_panic_handler,
287 	/* we need to be notified before reipl and kdump */
288 	.priority	= INT_MAX,
289 };
290 
291 static struct notifier_block zcore_on_panic_notifier = {
292 	.notifier_call	= zcore_reboot_and_on_panic_handler,
293 	/* we need to be notified before reipl and kdump */
294 	.priority	= INT_MAX,
295 };
296 
297 static int __init zcore_init(void)
298 {
299 	unsigned char arch;
300 	int rc;
301 
302 	if (!is_ipl_type_dump())
303 		return -ENODATA;
304 	if (oldmem_data.start)
305 		return -ENODATA;
306 
307 	zcore_dbf = debug_register("zcore", 4, 1, 4 * sizeof(long));
308 	debug_register_view(zcore_dbf, &debug_sprintf_view);
309 	debug_set_level(zcore_dbf, 6);
310 
311 	if (ipl_info.type == IPL_TYPE_FCP_DUMP) {
312 		TRACE("type:   fcp\n");
313 		TRACE("devno:  %x\n", ipl_info.data.fcp.dev_id.devno);
314 		TRACE("wwpn:   %llx\n", (unsigned long long) ipl_info.data.fcp.wwpn);
315 		TRACE("lun:    %llx\n", (unsigned long long) ipl_info.data.fcp.lun);
316 	} else if (ipl_info.type == IPL_TYPE_NVME_DUMP) {
317 		TRACE("type:   nvme\n");
318 		TRACE("fid:    %x\n", ipl_info.data.nvme.fid);
319 		TRACE("nsid:   %x\n", ipl_info.data.nvme.nsid);
320 	} else if (ipl_info.type == IPL_TYPE_ECKD_DUMP) {
321 		TRACE("type:   eckd\n");
322 		TRACE("devno:  %x\n", ipl_info.data.eckd.dev_id.devno);
323 		TRACE("ssid:   %x\n", ipl_info.data.eckd.dev_id.ssid);
324 	}
325 
326 	rc = sclp_sdias_init();
327 	if (rc)
328 		goto fail;
329 
330 	rc = check_sdias();
331 	if (rc)
332 		goto fail;
333 	hsa_available = 1;
334 
335 	rc = memcpy_hsa_kernel(&arch, __LC_AR_MODE_ID, 1);
336 	if (rc)
337 		goto fail;
338 
339 	if (arch == ARCH_S390) {
340 		pr_alert("The 64-bit dump tool cannot be used for a "
341 			 "32-bit system\n");
342 		rc = -EINVAL;
343 		goto fail;
344 	}
345 
346 	pr_alert("The dump process started for a 64-bit operating system\n");
347 	rc = init_cpu_info();
348 	if (rc)
349 		goto fail;
350 
351 	rc = zcore_reipl_init();
352 	if (rc)
353 		goto fail;
354 
355 	zcore_dir = debugfs_create_dir("zcore" , NULL);
356 	zcore_reipl_file = debugfs_create_file("reipl", S_IRUSR, zcore_dir,
357 						NULL, &zcore_reipl_fops);
358 	zcore_hsa_file = debugfs_create_file("hsa", S_IRUSR|S_IWUSR, zcore_dir,
359 					     NULL, &zcore_hsa_fops);
360 
361 	register_reboot_notifier(&zcore_reboot_notifier);
362 	atomic_notifier_chain_register(&panic_notifier_list, &zcore_on_panic_notifier);
363 
364 	return 0;
365 fail:
366 	diag308(DIAG308_REL_HSA, NULL);
367 	return rc;
368 }
369 subsys_initcall(zcore_init);
370