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 
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 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 
53 static DEFINE_MUTEX(hsa_buf_mutex);
54 static char hsa_buf[PAGE_SIZE] __aligned(PAGE_SIZE);
55 
56 /*
57  * Copy memory from HSA to user memory (not reentrant):
58  *
59  * @dest:  User buffer where memory should be copied to
60  * @src:   Start address within HSA where data should be copied
61  * @count: Size of buffer, which should be copied
62  */
memcpy_hsa_user(void __user * dest,unsigned long src,size_t count)63 int memcpy_hsa_user(void __user *dest, unsigned long src, size_t count)
64 {
65 	unsigned long offset, bytes;
66 
67 	if (!hsa_available)
68 		return -ENODATA;
69 
70 	mutex_lock(&hsa_buf_mutex);
71 	while (count) {
72 		if (sclp_sdias_copy(hsa_buf, src / PAGE_SIZE + 2, 1)) {
73 			TRACE("sclp_sdias_copy() failed\n");
74 			mutex_unlock(&hsa_buf_mutex);
75 			return -EIO;
76 		}
77 		offset = src % PAGE_SIZE;
78 		bytes = min(PAGE_SIZE - offset, count);
79 		if (copy_to_user(dest, hsa_buf + offset, bytes)) {
80 			mutex_unlock(&hsa_buf_mutex);
81 			return -EFAULT;
82 		}
83 		src += bytes;
84 		dest += bytes;
85 		count -= bytes;
86 	}
87 	mutex_unlock(&hsa_buf_mutex);
88 	return 0;
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  */
memcpy_hsa_kernel(void * dest,unsigned long src,size_t count)98 int memcpy_hsa_kernel(void *dest, unsigned long src, size_t count)
99 {
100 	unsigned long offset, bytes;
101 
102 	if (!hsa_available)
103 		return -ENODATA;
104 
105 	mutex_lock(&hsa_buf_mutex);
106 	while (count) {
107 		if (sclp_sdias_copy(hsa_buf, src / PAGE_SIZE + 2, 1)) {
108 			TRACE("sclp_sdias_copy() failed\n");
109 			mutex_unlock(&hsa_buf_mutex);
110 			return -EIO;
111 		}
112 		offset = src % PAGE_SIZE;
113 		bytes = min(PAGE_SIZE - offset, count);
114 		memcpy(dest, hsa_buf + offset, bytes);
115 		src += bytes;
116 		dest += bytes;
117 		count -= bytes;
118 	}
119 	mutex_unlock(&hsa_buf_mutex);
120 	return 0;
121 }
122 
init_cpu_info(void)123 static int __init init_cpu_info(void)
124 {
125 	struct save_area *sa;
126 
127 	/* get info for boot cpu from lowcore, stored in the HSA */
128 	sa = save_area_boot_cpu();
129 	if (!sa)
130 		return -ENOMEM;
131 	if (memcpy_hsa_kernel(hsa_buf, __LC_FPREGS_SAVE_AREA, 512) < 0) {
132 		TRACE("could not copy from HSA\n");
133 		return -EIO;
134 	}
135 	save_area_add_regs(sa, hsa_buf); /* vx registers are saved in smp.c */
136 	return 0;
137 }
138 
139 /*
140  * Release the HSA
141  */
release_hsa(void)142 static void release_hsa(void)
143 {
144 	diag308(DIAG308_REL_HSA, NULL);
145 	hsa_available = 0;
146 }
147 
zcore_reipl_write(struct file * filp,const char __user * buf,size_t count,loff_t * ppos)148 static ssize_t zcore_reipl_write(struct file *filp, const char __user *buf,
149 				 size_t count, loff_t *ppos)
150 {
151 	if (zcore_ipl_block) {
152 		diag308(DIAG308_SET, zcore_ipl_block);
153 		diag308(DIAG308_LOAD_CLEAR, NULL);
154 	}
155 	return count;
156 }
157 
zcore_reipl_open(struct inode * inode,struct file * filp)158 static int zcore_reipl_open(struct inode *inode, struct file *filp)
159 {
160 	return stream_open(inode, filp);
161 }
162 
zcore_reipl_release(struct inode * inode,struct file * filp)163 static int zcore_reipl_release(struct inode *inode, struct file *filp)
164 {
165 	return 0;
166 }
167 
168 static const struct file_operations zcore_reipl_fops = {
169 	.owner		= THIS_MODULE,
170 	.write		= zcore_reipl_write,
171 	.open		= zcore_reipl_open,
172 	.release	= zcore_reipl_release,
173 	.llseek		= no_llseek,
174 };
175 
zcore_hsa_read(struct file * filp,char __user * buf,size_t count,loff_t * ppos)176 static ssize_t zcore_hsa_read(struct file *filp, char __user *buf,
177 			      size_t count, loff_t *ppos)
178 {
179 	static char str[18];
180 
181 	if (hsa_available)
182 		snprintf(str, sizeof(str), "%lx\n", sclp.hsa_size);
183 	else
184 		snprintf(str, sizeof(str), "0\n");
185 	return simple_read_from_buffer(buf, count, ppos, str, strlen(str));
186 }
187 
zcore_hsa_write(struct file * filp,const char __user * buf,size_t count,loff_t * ppos)188 static ssize_t zcore_hsa_write(struct file *filp, const char __user *buf,
189 			       size_t count, loff_t *ppos)
190 {
191 	char value;
192 
193 	if (*ppos != 0)
194 		return -EPIPE;
195 	if (copy_from_user(&value, buf, 1))
196 		return -EFAULT;
197 	if (value != '0')
198 		return -EINVAL;
199 	release_hsa();
200 	return count;
201 }
202 
203 static const struct file_operations zcore_hsa_fops = {
204 	.owner		= THIS_MODULE,
205 	.write		= zcore_hsa_write,
206 	.read		= zcore_hsa_read,
207 	.open		= nonseekable_open,
208 	.llseek		= no_llseek,
209 };
210 
check_sdias(void)211 static int __init check_sdias(void)
212 {
213 	if (!sclp.hsa_size) {
214 		TRACE("Could not determine HSA size\n");
215 		return -ENODEV;
216 	}
217 	return 0;
218 }
219 
220 /*
221  * Provide IPL parameter information block from either HSA or memory
222  * for future reipl
223  */
zcore_reipl_init(void)224 static int __init zcore_reipl_init(void)
225 {
226 	struct ipib_info ipib_info;
227 	int rc;
228 
229 	rc = memcpy_hsa_kernel(&ipib_info, __LC_DUMP_REIPL, sizeof(ipib_info));
230 	if (rc)
231 		return rc;
232 	if (ipib_info.ipib == 0)
233 		return 0;
234 	zcore_ipl_block = (void *) __get_free_page(GFP_KERNEL);
235 	if (!zcore_ipl_block)
236 		return -ENOMEM;
237 	if (ipib_info.ipib < sclp.hsa_size)
238 		rc = memcpy_hsa_kernel(zcore_ipl_block, ipib_info.ipib,
239 				       PAGE_SIZE);
240 	else
241 		rc = memcpy_real(zcore_ipl_block, ipib_info.ipib, PAGE_SIZE);
242 	if (rc || (__force u32)csum_partial(zcore_ipl_block, zcore_ipl_block->hdr.len, 0) !=
243 	    ipib_info.checksum) {
244 		TRACE("Checksum does not match\n");
245 		free_page((unsigned long) zcore_ipl_block);
246 		zcore_ipl_block = NULL;
247 	}
248 	return 0;
249 }
250 
zcore_reboot_and_on_panic_handler(struct notifier_block * self,unsigned long event,void * data)251 static int zcore_reboot_and_on_panic_handler(struct notifier_block *self,
252 					     unsigned long	   event,
253 					     void		   *data)
254 {
255 	if (hsa_available)
256 		release_hsa();
257 
258 	return NOTIFY_OK;
259 }
260 
261 static struct notifier_block zcore_reboot_notifier = {
262 	.notifier_call	= zcore_reboot_and_on_panic_handler,
263 	/* we need to be notified before reipl and kdump */
264 	.priority	= INT_MAX,
265 };
266 
267 static struct notifier_block zcore_on_panic_notifier = {
268 	.notifier_call	= zcore_reboot_and_on_panic_handler,
269 	/* we need to be notified before reipl and kdump */
270 	.priority	= INT_MAX,
271 };
272 
zcore_init(void)273 static int __init zcore_init(void)
274 {
275 	unsigned char arch;
276 	int rc;
277 
278 	if (!is_ipl_type_dump())
279 		return -ENODATA;
280 	if (oldmem_data.start)
281 		return -ENODATA;
282 
283 	zcore_dbf = debug_register("zcore", 4, 1, 4 * sizeof(long));
284 	debug_register_view(zcore_dbf, &debug_sprintf_view);
285 	debug_set_level(zcore_dbf, 6);
286 
287 	if (ipl_info.type == IPL_TYPE_FCP_DUMP) {
288 		TRACE("type:   fcp\n");
289 		TRACE("devno:  %x\n", ipl_info.data.fcp.dev_id.devno);
290 		TRACE("wwpn:   %llx\n", (unsigned long long) ipl_info.data.fcp.wwpn);
291 		TRACE("lun:    %llx\n", (unsigned long long) ipl_info.data.fcp.lun);
292 	} else if (ipl_info.type == IPL_TYPE_NVME_DUMP) {
293 		TRACE("type:   nvme\n");
294 		TRACE("fid:    %x\n", ipl_info.data.nvme.fid);
295 		TRACE("nsid:   %x\n", ipl_info.data.nvme.nsid);
296 	}
297 
298 	rc = sclp_sdias_init();
299 	if (rc)
300 		goto fail;
301 
302 	rc = check_sdias();
303 	if (rc)
304 		goto fail;
305 	hsa_available = 1;
306 
307 	rc = memcpy_hsa_kernel(&arch, __LC_AR_MODE_ID, 1);
308 	if (rc)
309 		goto fail;
310 
311 	if (arch == ARCH_S390) {
312 		pr_alert("The 64-bit dump tool cannot be used for a "
313 			 "32-bit system\n");
314 		rc = -EINVAL;
315 		goto fail;
316 	}
317 
318 	pr_alert("The dump process started for a 64-bit operating system\n");
319 	rc = init_cpu_info();
320 	if (rc)
321 		goto fail;
322 
323 	rc = zcore_reipl_init();
324 	if (rc)
325 		goto fail;
326 
327 	zcore_dir = debugfs_create_dir("zcore" , NULL);
328 	zcore_reipl_file = debugfs_create_file("reipl", S_IRUSR, zcore_dir,
329 						NULL, &zcore_reipl_fops);
330 	zcore_hsa_file = debugfs_create_file("hsa", S_IRUSR|S_IWUSR, zcore_dir,
331 					     NULL, &zcore_hsa_fops);
332 
333 	register_reboot_notifier(&zcore_reboot_notifier);
334 	atomic_notifier_chain_register(&panic_notifier_list, &zcore_on_panic_notifier);
335 
336 	return 0;
337 fail:
338 	diag308(DIAG308_REL_HSA, NULL);
339 	return rc;
340 }
341 subsys_initcall(zcore_init);
342