xref: /linux/arch/s390/kernel/os_info.c (revision dc306186a130c6d9feb0aabc1c71b8ed1674a3bf)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
24857d4bbSMichael Holzheu /*
34857d4bbSMichael Holzheu  * OS info memory interface
44857d4bbSMichael Holzheu  *
54857d4bbSMichael Holzheu  * Copyright IBM Corp. 2012
64857d4bbSMichael Holzheu  * Author(s): Michael Holzheu <holzheu@linux.vnet.ibm.com>
74857d4bbSMichael Holzheu  */
84857d4bbSMichael Holzheu 
94857d4bbSMichael Holzheu #define KMSG_COMPONENT "os_info"
104857d4bbSMichael Holzheu #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
114857d4bbSMichael Holzheu 
124857d4bbSMichael Holzheu #include <linux/crash_dump.h>
134857d4bbSMichael Holzheu #include <linux/kernel.h>
140d01ff25SDavid Howells #include <linux/slab.h>
154857d4bbSMichael Holzheu #include <asm/checksum.h>
164857d4bbSMichael Holzheu #include <asm/lowcore.h>
174857d4bbSMichael Holzheu #include <asm/os_info.h>
18*dc306186SAlexander Gordeev #include <asm/asm-offsets.h>
194857d4bbSMichael Holzheu 
204857d4bbSMichael Holzheu /*
214857d4bbSMichael Holzheu  * OS info structure has to be page aligned
224857d4bbSMichael Holzheu  */
234857d4bbSMichael Holzheu static struct os_info os_info __page_aligned_data;
244857d4bbSMichael Holzheu 
254857d4bbSMichael Holzheu /*
264857d4bbSMichael Holzheu  * Compute checksum over OS info structure
274857d4bbSMichael Holzheu  */
284857d4bbSMichael Holzheu u32 os_info_csum(struct os_info *os_info)
294857d4bbSMichael Holzheu {
304857d4bbSMichael Holzheu 	int size = sizeof(*os_info) - offsetof(struct os_info, version_major);
3190b3baa2SHeiko Carstens 	return (__force u32)csum_partial(&os_info->version_major, size, 0);
324857d4bbSMichael Holzheu }
334857d4bbSMichael Holzheu 
344857d4bbSMichael Holzheu /*
354857d4bbSMichael Holzheu  * Add crashkernel info to OS info and update checksum
364857d4bbSMichael Holzheu  */
374857d4bbSMichael Holzheu void os_info_crashkernel_add(unsigned long base, unsigned long size)
384857d4bbSMichael Holzheu {
394857d4bbSMichael Holzheu 	os_info.crashkernel_addr = (u64)(unsigned long)base;
404857d4bbSMichael Holzheu 	os_info.crashkernel_size = (u64)(unsigned long)size;
414857d4bbSMichael Holzheu 	os_info.csum = os_info_csum(&os_info);
424857d4bbSMichael Holzheu }
434857d4bbSMichael Holzheu 
444857d4bbSMichael Holzheu /*
454857d4bbSMichael Holzheu  * Add OS info entry and update checksum
464857d4bbSMichael Holzheu  */
474857d4bbSMichael Holzheu void os_info_entry_add(int nr, void *ptr, u64 size)
484857d4bbSMichael Holzheu {
499de209c7SAlexander Gordeev 	os_info.entry[nr].addr = __pa(ptr);
504857d4bbSMichael Holzheu 	os_info.entry[nr].size = size;
5190b3baa2SHeiko Carstens 	os_info.entry[nr].csum = (__force u32)csum_partial(ptr, size, 0);
524857d4bbSMichael Holzheu 	os_info.csum = os_info_csum(&os_info);
534857d4bbSMichael Holzheu }
544857d4bbSMichael Holzheu 
554857d4bbSMichael Holzheu /*
565671d971SBhaskar Chowdhury  * Initialize OS info structure and set lowcore pointer
574857d4bbSMichael Holzheu  */
584857d4bbSMichael Holzheu void __init os_info_init(void)
594857d4bbSMichael Holzheu {
604857d4bbSMichael Holzheu 	void *ptr = &os_info;
614857d4bbSMichael Holzheu 
624857d4bbSMichael Holzheu 	os_info.version_major = OS_INFO_VERSION_MAJOR;
634857d4bbSMichael Holzheu 	os_info.version_minor = OS_INFO_VERSION_MINOR;
644857d4bbSMichael Holzheu 	os_info.magic = OS_INFO_MAGIC;
654857d4bbSMichael Holzheu 	os_info.csum = os_info_csum(&os_info);
669de209c7SAlexander Gordeev 	mem_assign_absolute(S390_lowcore.os_info, __pa(ptr));
674857d4bbSMichael Holzheu }
684857d4bbSMichael Holzheu 
694857d4bbSMichael Holzheu #ifdef CONFIG_CRASH_DUMP
704857d4bbSMichael Holzheu 
714857d4bbSMichael Holzheu static struct os_info *os_info_old;
724857d4bbSMichael Holzheu 
734857d4bbSMichael Holzheu /*
744857d4bbSMichael Holzheu  * Allocate and copy OS info entry from oldmem
754857d4bbSMichael Holzheu  */
764857d4bbSMichael Holzheu static void os_info_old_alloc(int nr, int align)
774857d4bbSMichael Holzheu {
784857d4bbSMichael Holzheu 	unsigned long addr, size = 0;
794857d4bbSMichael Holzheu 	char *buf, *buf_align, *msg;
804857d4bbSMichael Holzheu 	u32 csum;
814857d4bbSMichael Holzheu 
824857d4bbSMichael Holzheu 	addr = os_info_old->entry[nr].addr;
834857d4bbSMichael Holzheu 	if (!addr) {
844857d4bbSMichael Holzheu 		msg = "not available";
854857d4bbSMichael Holzheu 		goto fail;
864857d4bbSMichael Holzheu 	}
874857d4bbSMichael Holzheu 	size = os_info_old->entry[nr].size;
884857d4bbSMichael Holzheu 	buf = kmalloc(size + align - 1, GFP_KERNEL);
894857d4bbSMichael Holzheu 	if (!buf) {
904857d4bbSMichael Holzheu 		msg = "alloc failed";
914857d4bbSMichael Holzheu 		goto fail;
924857d4bbSMichael Holzheu 	}
934857d4bbSMichael Holzheu 	buf_align = PTR_ALIGN(buf, align);
94df9694c7SMartin Schwidefsky 	if (copy_oldmem_kernel(buf_align, (void *) addr, size)) {
954857d4bbSMichael Holzheu 		msg = "copy failed";
964857d4bbSMichael Holzheu 		goto fail_free;
974857d4bbSMichael Holzheu 	}
9890b3baa2SHeiko Carstens 	csum = (__force u32)csum_partial(buf_align, size, 0);
994857d4bbSMichael Holzheu 	if (csum != os_info_old->entry[nr].csum) {
1004857d4bbSMichael Holzheu 		msg = "checksum failed";
1014857d4bbSMichael Holzheu 		goto fail_free;
1024857d4bbSMichael Holzheu 	}
1034857d4bbSMichael Holzheu 	os_info_old->entry[nr].addr = (u64)(unsigned long)buf_align;
1044857d4bbSMichael Holzheu 	msg = "copied";
1054857d4bbSMichael Holzheu 	goto out;
1064857d4bbSMichael Holzheu fail_free:
1074857d4bbSMichael Holzheu 	kfree(buf);
1084857d4bbSMichael Holzheu fail:
1094857d4bbSMichael Holzheu 	os_info_old->entry[nr].addr = 0;
1104857d4bbSMichael Holzheu out:
1114857d4bbSMichael Holzheu 	pr_info("entry %i: %s (addr=0x%lx size=%lu)\n",
1124857d4bbSMichael Holzheu 		nr, msg, addr, size);
1134857d4bbSMichael Holzheu }
1144857d4bbSMichael Holzheu 
1154857d4bbSMichael Holzheu /*
1164857d4bbSMichael Holzheu  * Initialize os info and os info entries from oldmem
1174857d4bbSMichael Holzheu  */
1184857d4bbSMichael Holzheu static void os_info_old_init(void)
1194857d4bbSMichael Holzheu {
1204857d4bbSMichael Holzheu 	static int os_info_init;
1214857d4bbSMichael Holzheu 	unsigned long addr;
1224857d4bbSMichael Holzheu 
1234857d4bbSMichael Holzheu 	if (os_info_init)
1244857d4bbSMichael Holzheu 		return;
125e9e7870fSAlexander Egorenkov 	if (!oldmem_data.start)
1264857d4bbSMichael Holzheu 		goto fail;
127*dc306186SAlexander Gordeev 	if (copy_oldmem_kernel(&addr, (void *)__LC_OS_INFO, sizeof(addr)))
1284857d4bbSMichael Holzheu 		goto fail;
1294857d4bbSMichael Holzheu 	if (addr == 0 || addr % PAGE_SIZE)
1304857d4bbSMichael Holzheu 		goto fail;
1314857d4bbSMichael Holzheu 	os_info_old = kzalloc(sizeof(*os_info_old), GFP_KERNEL);
1324857d4bbSMichael Holzheu 	if (!os_info_old)
1334857d4bbSMichael Holzheu 		goto fail;
134df9694c7SMartin Schwidefsky 	if (copy_oldmem_kernel(os_info_old, (void *) addr,
135df9694c7SMartin Schwidefsky 			       sizeof(*os_info_old)))
1364857d4bbSMichael Holzheu 		goto fail_free;
1374857d4bbSMichael Holzheu 	if (os_info_old->magic != OS_INFO_MAGIC)
1384857d4bbSMichael Holzheu 		goto fail_free;
1394857d4bbSMichael Holzheu 	if (os_info_old->csum != os_info_csum(os_info_old))
1404857d4bbSMichael Holzheu 		goto fail_free;
1414857d4bbSMichael Holzheu 	if (os_info_old->version_major > OS_INFO_VERSION_MAJOR)
1424857d4bbSMichael Holzheu 		goto fail_free;
1434857d4bbSMichael Holzheu 	os_info_old_alloc(OS_INFO_VMCOREINFO, 1);
1444857d4bbSMichael Holzheu 	os_info_old_alloc(OS_INFO_REIPL_BLOCK, 1);
1454857d4bbSMichael Holzheu 	pr_info("crashkernel: addr=0x%lx size=%lu\n",
1464857d4bbSMichael Holzheu 		(unsigned long) os_info_old->crashkernel_addr,
1474857d4bbSMichael Holzheu 		(unsigned long) os_info_old->crashkernel_size);
1484857d4bbSMichael Holzheu 	os_info_init = 1;
1494857d4bbSMichael Holzheu 	return;
1504857d4bbSMichael Holzheu fail_free:
1514857d4bbSMichael Holzheu 	kfree(os_info_old);
1524857d4bbSMichael Holzheu fail:
1534857d4bbSMichael Holzheu 	os_info_init = 1;
1544857d4bbSMichael Holzheu 	os_info_old = NULL;
1554857d4bbSMichael Holzheu }
1564857d4bbSMichael Holzheu 
1574857d4bbSMichael Holzheu /*
1584857d4bbSMichael Holzheu  * Return pointer to os infor entry and its size
1594857d4bbSMichael Holzheu  */
1604857d4bbSMichael Holzheu void *os_info_old_entry(int nr, unsigned long *size)
1614857d4bbSMichael Holzheu {
1624857d4bbSMichael Holzheu 	os_info_old_init();
1634857d4bbSMichael Holzheu 
1644857d4bbSMichael Holzheu 	if (!os_info_old)
1654857d4bbSMichael Holzheu 		return NULL;
1664857d4bbSMichael Holzheu 	if (!os_info_old->entry[nr].addr)
1674857d4bbSMichael Holzheu 		return NULL;
1684857d4bbSMichael Holzheu 	*size = (unsigned long) os_info_old->entry[nr].size;
1694857d4bbSMichael Holzheu 	return (void *)(unsigned long)os_info_old->entry[nr].addr;
1704857d4bbSMichael Holzheu }
1714857d4bbSMichael Holzheu #endif
172