1 // SPDX-License-Identifier: GPL-1.0+
3 * zcore module to export memory content and register sets for creating system
4 * dumps on SCSI/NVMe disks (zfcp/nvme dump).
6 * For more information please refer to Documentation/arch/s390/zfcpdump.rst
8 * Copyright IBM Corp. 2003, 2008
9 * Author(s): Michael Holzheu
12 #define KMSG_COMPONENT "zdump"
13 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
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>
22 #include <asm/asm-offsets.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 <asm/maccess.h>
36 #define TRACE(x...) debug_sprintf_event(zcore_dbf, 1, x)
46 } __attribute__((packed));
48 static struct debug_info *zcore_dbf;
49 static int hsa_available;
50 static struct dentry *zcore_dir;
51 static struct dentry *zcore_reipl_file;
52 static struct dentry *zcore_hsa_file;
53 static struct ipl_parameter_block *zcore_ipl_block;
54 static unsigned long os_info_flags;
56 static DEFINE_MUTEX(hsa_buf_mutex);
57 static char hsa_buf[PAGE_SIZE] __aligned(PAGE_SIZE);
60 * Copy memory from HSA to iterator (not reentrant):
62 * @iter: Iterator 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
66 size_t memcpy_hsa_iter(struct iov_iter *iter, unsigned long src, size_t count)
68 size_t bytes, copied, res = 0;
74 mutex_lock(&hsa_buf_mutex);
76 if (sclp_sdias_copy(hsa_buf, src / PAGE_SIZE + 2, 1)) {
77 TRACE("sclp_sdias_copy() failed\n");
80 offset = src % PAGE_SIZE;
81 bytes = min(PAGE_SIZE - offset, count);
82 copied = copy_to_iter(hsa_buf + offset, bytes, iter);
89 mutex_unlock(&hsa_buf_mutex);
94 * Copy memory from HSA to kernel memory (not reentrant):
96 * @dest: Kernel or user buffer where memory should be copied to
97 * @src: Start address within HSA where data should be copied
98 * @count: Size of buffer, which should be copied
100 static inline int memcpy_hsa_kernel(void *dst, unsigned long src, size_t count)
102 struct iov_iter iter;
106 kvec.iov_len = count;
107 iov_iter_kvec(&iter, ITER_DEST, &kvec, 1, count);
108 if (memcpy_hsa_iter(&iter, src, count) < count)
113 static int __init init_cpu_info(void)
115 struct save_area *sa;
117 /* get info for boot cpu from lowcore, stored in the HSA */
118 sa = save_area_boot_cpu();
121 if (memcpy_hsa_kernel(hsa_buf, __LC_FPREGS_SAVE_AREA, 512) < 0) {
122 TRACE("could not copy from HSA\n");
125 save_area_add_regs(sa, hsa_buf); /* vx registers are saved in smp.c */
132 static void release_hsa(void)
134 diag308(DIAG308_REL_HSA, NULL);
138 static ssize_t zcore_reipl_write(struct file *filp, const char __user *buf,
139 size_t count, loff_t *ppos)
141 if (zcore_ipl_block) {
142 diag308(DIAG308_SET, zcore_ipl_block);
143 if (os_info_flags & OS_INFO_FLAG_REIPL_CLEAR)
144 diag308(DIAG308_LOAD_CLEAR, NULL);
145 /* Use special diag308 subcode for CCW normal ipl */
146 if (zcore_ipl_block->pb0_hdr.pbt == IPL_PBT_CCW)
147 diag308(DIAG308_LOAD_NORMAL_DUMP, NULL);
149 diag308(DIAG308_LOAD_NORMAL, NULL);
154 static int zcore_reipl_open(struct inode *inode, struct file *filp)
156 return stream_open(inode, filp);
159 static int zcore_reipl_release(struct inode *inode, struct file *filp)
164 static const struct file_operations zcore_reipl_fops = {
165 .owner = THIS_MODULE,
166 .write = zcore_reipl_write,
167 .open = zcore_reipl_open,
168 .release = zcore_reipl_release,
172 static ssize_t zcore_hsa_read(struct file *filp, char __user *buf,
173 size_t count, loff_t *ppos)
178 snprintf(str, sizeof(str), "%lx\n", sclp.hsa_size);
180 snprintf(str, sizeof(str), "0\n");
181 return simple_read_from_buffer(buf, count, ppos, str, strlen(str));
184 static ssize_t zcore_hsa_write(struct file *filp, const char __user *buf,
185 size_t count, loff_t *ppos)
191 if (copy_from_user(&value, buf, 1))
199 static const struct file_operations zcore_hsa_fops = {
200 .owner = THIS_MODULE,
201 .write = zcore_hsa_write,
202 .read = zcore_hsa_read,
203 .open = nonseekable_open,
207 static int __init check_sdias(void)
209 if (!sclp.hsa_size) {
210 TRACE("Could not determine HSA size\n");
217 * Provide IPL parameter information block from either HSA or memory
220 static int __init zcore_reipl_init(void)
222 struct os_info_entry *entry;
223 struct ipib_info ipib_info;
224 unsigned long os_info_addr;
225 struct os_info *os_info;
228 rc = memcpy_hsa_kernel(&ipib_info, __LC_DUMP_REIPL, sizeof(ipib_info));
231 if (ipib_info.ipib == 0)
233 zcore_ipl_block = (void *) __get_free_page(GFP_KERNEL);
234 if (!zcore_ipl_block)
236 if (ipib_info.ipib < sclp.hsa_size)
237 rc = memcpy_hsa_kernel(zcore_ipl_block, ipib_info.ipib,
240 rc = memcpy_real(zcore_ipl_block, ipib_info.ipib, PAGE_SIZE);
241 if (rc || (__force u32)csum_partial(zcore_ipl_block, zcore_ipl_block->hdr.len, 0) !=
242 ipib_info.checksum) {
243 TRACE("Checksum does not match\n");
244 free_page((unsigned long) zcore_ipl_block);
245 zcore_ipl_block = NULL;
248 * Read the bit-flags field from os_info flags entry.
249 * Return zero even for os_info read or entry checksum errors in order
250 * to continue dump processing, considering that os_info could be
251 * corrupted on the panicked system.
253 os_info = (void *)__get_free_page(GFP_KERNEL);
256 rc = memcpy_hsa_kernel(&os_info_addr, __LC_OS_INFO, sizeof(os_info_addr));
259 if (os_info_addr < sclp.hsa_size)
260 rc = memcpy_hsa_kernel(os_info, os_info_addr, PAGE_SIZE);
262 rc = memcpy_real(os_info, os_info_addr, PAGE_SIZE);
263 if (rc || os_info_csum(os_info) != os_info->csum)
265 entry = &os_info->entry[OS_INFO_FLAGS_ENTRY];
266 if (entry->addr && entry->size) {
267 if (entry->addr < sclp.hsa_size)
268 rc = memcpy_hsa_kernel(&os_info_flags, entry->addr, sizeof(os_info_flags));
270 rc = memcpy_real(&os_info_flags, entry->addr, sizeof(os_info_flags));
271 if (rc || (__force u32)csum_partial(&os_info_flags, entry->size, 0) != entry->csum)
275 free_page((unsigned long)os_info);
279 static int zcore_reboot_and_on_panic_handler(struct notifier_block *self,
289 static struct notifier_block zcore_reboot_notifier = {
290 .notifier_call = zcore_reboot_and_on_panic_handler,
291 /* we need to be notified before reipl and kdump */
295 static struct notifier_block zcore_on_panic_notifier = {
296 .notifier_call = zcore_reboot_and_on_panic_handler,
297 /* we need to be notified before reipl and kdump */
301 static int __init zcore_init(void)
306 if (!is_ipl_type_dump())
308 if (oldmem_data.start)
311 zcore_dbf = debug_register("zcore", 4, 1, 4 * sizeof(long));
312 debug_register_view(zcore_dbf, &debug_sprintf_view);
313 debug_set_level(zcore_dbf, 6);
315 if (ipl_info.type == IPL_TYPE_FCP_DUMP) {
316 TRACE("type: fcp\n");
317 TRACE("devno: %x\n", ipl_info.data.fcp.dev_id.devno);
318 TRACE("wwpn: %llx\n", (unsigned long long) ipl_info.data.fcp.wwpn);
319 TRACE("lun: %llx\n", (unsigned long long) ipl_info.data.fcp.lun);
320 } else if (ipl_info.type == IPL_TYPE_NVME_DUMP) {
321 TRACE("type: nvme\n");
322 TRACE("fid: %x\n", ipl_info.data.nvme.fid);
323 TRACE("nsid: %x\n", ipl_info.data.nvme.nsid);
324 } else if (ipl_info.type == IPL_TYPE_ECKD_DUMP) {
325 TRACE("type: eckd\n");
326 TRACE("devno: %x\n", ipl_info.data.eckd.dev_id.devno);
327 TRACE("ssid: %x\n", ipl_info.data.eckd.dev_id.ssid);
330 rc = sclp_sdias_init();
339 rc = memcpy_hsa_kernel(&arch, __LC_AR_MODE_ID, 1);
343 if (arch == ARCH_S390) {
344 pr_alert("The 64-bit dump tool cannot be used for a "
350 pr_alert("The dump process started for a 64-bit operating system\n");
351 rc = init_cpu_info();
355 rc = zcore_reipl_init();
359 zcore_dir = debugfs_create_dir("zcore" , NULL);
360 zcore_reipl_file = debugfs_create_file("reipl", S_IRUSR, zcore_dir,
361 NULL, &zcore_reipl_fops);
362 zcore_hsa_file = debugfs_create_file("hsa", S_IRUSR|S_IWUSR, zcore_dir,
363 NULL, &zcore_hsa_fops);
365 register_reboot_notifier(&zcore_reboot_notifier);
366 atomic_notifier_chain_register(&panic_notifier_list, &zcore_on_panic_notifier);
370 diag308(DIAG308_REL_HSA, NULL);
373 subsys_initcall(zcore_init);