xref: /openbmc/linux/drivers/s390/char/zcore.c (revision 9221b289)
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 #include <linux/panic_notifier.h>
19 #include <linux/reboot.h>
20 #include <linux/uio.h>
21 
22 #include <asm/asm-offsets.h>
23 #include <asm/ipl.h>
24 #include <asm/sclp.h>
25 #include <asm/setup.h>
26 #include <linux/uaccess.h>
27 #include <asm/debug.h>
28 #include <asm/processor.h>
29 #include <asm/irqflags.h>
30 #include <asm/checksum.h>
31 #include <asm/os_info.h>
32 #include <asm/switch_to.h>
33 #include "sclp.h"
34 
35 #define TRACE(x...) debug_sprintf_event(zcore_dbf, 1, x)
36 
37 enum arch_id {
38 	ARCH_S390	= 0,
39 	ARCH_S390X	= 1,
40 };
41 
42 struct ipib_info {
43 	unsigned long	ipib;
44 	u32		checksum;
45 }  __attribute__((packed));
46 
47 static struct debug_info *zcore_dbf;
48 static int hsa_available;
49 static struct dentry *zcore_dir;
50 static struct dentry *zcore_reipl_file;
51 static struct dentry *zcore_hsa_file;
52 static struct ipl_parameter_block *zcore_ipl_block;
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, WRITE, &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 		diag308(DIAG308_LOAD_CLEAR, NULL);
142 	}
143 	return count;
144 }
145 
146 static int zcore_reipl_open(struct inode *inode, struct file *filp)
147 {
148 	return stream_open(inode, filp);
149 }
150 
151 static int zcore_reipl_release(struct inode *inode, struct file *filp)
152 {
153 	return 0;
154 }
155 
156 static const struct file_operations zcore_reipl_fops = {
157 	.owner		= THIS_MODULE,
158 	.write		= zcore_reipl_write,
159 	.open		= zcore_reipl_open,
160 	.release	= zcore_reipl_release,
161 	.llseek		= no_llseek,
162 };
163 
164 static ssize_t zcore_hsa_read(struct file *filp, char __user *buf,
165 			      size_t count, loff_t *ppos)
166 {
167 	static char str[18];
168 
169 	if (hsa_available)
170 		snprintf(str, sizeof(str), "%lx\n", sclp.hsa_size);
171 	else
172 		snprintf(str, sizeof(str), "0\n");
173 	return simple_read_from_buffer(buf, count, ppos, str, strlen(str));
174 }
175 
176 static ssize_t zcore_hsa_write(struct file *filp, const char __user *buf,
177 			       size_t count, loff_t *ppos)
178 {
179 	char value;
180 
181 	if (*ppos != 0)
182 		return -EPIPE;
183 	if (copy_from_user(&value, buf, 1))
184 		return -EFAULT;
185 	if (value != '0')
186 		return -EINVAL;
187 	release_hsa();
188 	return count;
189 }
190 
191 static const struct file_operations zcore_hsa_fops = {
192 	.owner		= THIS_MODULE,
193 	.write		= zcore_hsa_write,
194 	.read		= zcore_hsa_read,
195 	.open		= nonseekable_open,
196 	.llseek		= no_llseek,
197 };
198 
199 static int __init check_sdias(void)
200 {
201 	if (!sclp.hsa_size) {
202 		TRACE("Could not determine HSA size\n");
203 		return -ENODEV;
204 	}
205 	return 0;
206 }
207 
208 /*
209  * Provide IPL parameter information block from either HSA or memory
210  * for future reipl
211  */
212 static int __init zcore_reipl_init(void)
213 {
214 	struct ipib_info ipib_info;
215 	int rc;
216 
217 	rc = memcpy_hsa_kernel(&ipib_info, __LC_DUMP_REIPL, sizeof(ipib_info));
218 	if (rc)
219 		return rc;
220 	if (ipib_info.ipib == 0)
221 		return 0;
222 	zcore_ipl_block = (void *) __get_free_page(GFP_KERNEL);
223 	if (!zcore_ipl_block)
224 		return -ENOMEM;
225 	if (ipib_info.ipib < sclp.hsa_size)
226 		rc = memcpy_hsa_kernel(zcore_ipl_block, ipib_info.ipib,
227 				       PAGE_SIZE);
228 	else
229 		rc = memcpy_real(zcore_ipl_block, ipib_info.ipib, PAGE_SIZE);
230 	if (rc || (__force u32)csum_partial(zcore_ipl_block, zcore_ipl_block->hdr.len, 0) !=
231 	    ipib_info.checksum) {
232 		TRACE("Checksum does not match\n");
233 		free_page((unsigned long) zcore_ipl_block);
234 		zcore_ipl_block = NULL;
235 	}
236 	return 0;
237 }
238 
239 static int zcore_reboot_and_on_panic_handler(struct notifier_block *self,
240 					     unsigned long	   event,
241 					     void		   *data)
242 {
243 	if (hsa_available)
244 		release_hsa();
245 
246 	return NOTIFY_OK;
247 }
248 
249 static struct notifier_block zcore_reboot_notifier = {
250 	.notifier_call	= zcore_reboot_and_on_panic_handler,
251 	/* we need to be notified before reipl and kdump */
252 	.priority	= INT_MAX,
253 };
254 
255 static struct notifier_block zcore_on_panic_notifier = {
256 	.notifier_call	= zcore_reboot_and_on_panic_handler,
257 	/* we need to be notified before reipl and kdump */
258 	.priority	= INT_MAX,
259 };
260 
261 static int __init zcore_init(void)
262 {
263 	unsigned char arch;
264 	int rc;
265 
266 	if (!is_ipl_type_dump())
267 		return -ENODATA;
268 	if (oldmem_data.start)
269 		return -ENODATA;
270 
271 	zcore_dbf = debug_register("zcore", 4, 1, 4 * sizeof(long));
272 	debug_register_view(zcore_dbf, &debug_sprintf_view);
273 	debug_set_level(zcore_dbf, 6);
274 
275 	if (ipl_info.type == IPL_TYPE_FCP_DUMP) {
276 		TRACE("type:   fcp\n");
277 		TRACE("devno:  %x\n", ipl_info.data.fcp.dev_id.devno);
278 		TRACE("wwpn:   %llx\n", (unsigned long long) ipl_info.data.fcp.wwpn);
279 		TRACE("lun:    %llx\n", (unsigned long long) ipl_info.data.fcp.lun);
280 	} else if (ipl_info.type == IPL_TYPE_NVME_DUMP) {
281 		TRACE("type:   nvme\n");
282 		TRACE("fid:    %x\n", ipl_info.data.nvme.fid);
283 		TRACE("nsid:   %x\n", ipl_info.data.nvme.nsid);
284 	}
285 
286 	rc = sclp_sdias_init();
287 	if (rc)
288 		goto fail;
289 
290 	rc = check_sdias();
291 	if (rc)
292 		goto fail;
293 	hsa_available = 1;
294 
295 	rc = memcpy_hsa_kernel(&arch, __LC_AR_MODE_ID, 1);
296 	if (rc)
297 		goto fail;
298 
299 	if (arch == ARCH_S390) {
300 		pr_alert("The 64-bit dump tool cannot be used for a "
301 			 "32-bit system\n");
302 		rc = -EINVAL;
303 		goto fail;
304 	}
305 
306 	pr_alert("The dump process started for a 64-bit operating system\n");
307 	rc = init_cpu_info();
308 	if (rc)
309 		goto fail;
310 
311 	rc = zcore_reipl_init();
312 	if (rc)
313 		goto fail;
314 
315 	zcore_dir = debugfs_create_dir("zcore" , NULL);
316 	zcore_reipl_file = debugfs_create_file("reipl", S_IRUSR, zcore_dir,
317 						NULL, &zcore_reipl_fops);
318 	zcore_hsa_file = debugfs_create_file("hsa", S_IRUSR|S_IWUSR, zcore_dir,
319 					     NULL, &zcore_hsa_fops);
320 
321 	register_reboot_notifier(&zcore_reboot_notifier);
322 	atomic_notifier_chain_register(&panic_notifier_list, &zcore_on_panic_notifier);
323 
324 	return 0;
325 fail:
326 	diag308(DIAG308_REL_HSA, NULL);
327 	return rc;
328 }
329 subsys_initcall(zcore_init);
330