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> 164df29d2bSAlexander Gordeev #include <asm/abs_lowcore.h> 174857d4bbSMichael Holzheu #include <asm/os_info.h> 188572f525SAlexander Gordeev #include <asm/physmem_info.h> 19fba07cd4SAlexander Gordeev #include <asm/maccess.h> 20dc306186SAlexander Gordeev #include <asm/asm-offsets.h> 21a2269a66SAlexander Egorenkov #include <asm/ipl.h> 224857d4bbSMichael Holzheu 234857d4bbSMichael Holzheu /* 244857d4bbSMichael Holzheu * OS info structure has to be page aligned 254857d4bbSMichael Holzheu */ 264857d4bbSMichael Holzheu static struct os_info os_info __page_aligned_data; 274857d4bbSMichael Holzheu 284857d4bbSMichael Holzheu /* 294857d4bbSMichael Holzheu * Compute checksum over OS info structure 304857d4bbSMichael Holzheu */ 314857d4bbSMichael Holzheu u32 os_info_csum(struct os_info *os_info) 324857d4bbSMichael Holzheu { 334857d4bbSMichael Holzheu int size = sizeof(*os_info) - offsetof(struct os_info, version_major); 343a74f44dSHeiko Carstens return (__force u32)cksm(&os_info->version_major, size, 0); 354857d4bbSMichael Holzheu } 364857d4bbSMichael Holzheu 374857d4bbSMichael Holzheu /* 384857d4bbSMichael Holzheu * Add crashkernel info to OS info and update checksum 394857d4bbSMichael Holzheu */ 404857d4bbSMichael Holzheu void os_info_crashkernel_add(unsigned long base, unsigned long size) 414857d4bbSMichael Holzheu { 424857d4bbSMichael Holzheu os_info.crashkernel_addr = (u64)(unsigned long)base; 434857d4bbSMichael Holzheu os_info.crashkernel_size = (u64)(unsigned long)size; 444857d4bbSMichael Holzheu os_info.csum = os_info_csum(&os_info); 454857d4bbSMichael Holzheu } 464857d4bbSMichael Holzheu 474857d4bbSMichael Holzheu /* 4888702793SAlexander Gordeev * Add OS info data entry and update checksum 494857d4bbSMichael Holzheu */ 5088702793SAlexander Gordeev void os_info_entry_add_data(int nr, void *ptr, u64 size) 514857d4bbSMichael Holzheu { 529de209c7SAlexander Gordeev os_info.entry[nr].addr = __pa(ptr); 534857d4bbSMichael Holzheu os_info.entry[nr].size = size; 543a74f44dSHeiko Carstens os_info.entry[nr].csum = (__force u32)cksm(ptr, size, 0); 554857d4bbSMichael Holzheu os_info.csum = os_info_csum(&os_info); 564857d4bbSMichael Holzheu } 574857d4bbSMichael Holzheu 584857d4bbSMichael Holzheu /* 5988702793SAlexander Gordeev * Add OS info value entry and update checksum 6088702793SAlexander Gordeev */ 6188702793SAlexander Gordeev void os_info_entry_add_val(int nr, u64 value) 6288702793SAlexander Gordeev { 6388702793SAlexander Gordeev os_info.entry[nr].val = value; 6488702793SAlexander Gordeev os_info.entry[nr].size = 0; 6588702793SAlexander Gordeev os_info.entry[nr].csum = 0; 6688702793SAlexander Gordeev os_info.csum = os_info_csum(&os_info); 6788702793SAlexander Gordeev } 6888702793SAlexander Gordeev 6988702793SAlexander Gordeev /* 705671d971SBhaskar Chowdhury * Initialize OS info structure and set lowcore pointer 714857d4bbSMichael Holzheu */ 724857d4bbSMichael Holzheu void __init os_info_init(void) 734857d4bbSMichael Holzheu { 744df29d2bSAlexander Gordeev struct lowcore *abs_lc; 754857d4bbSMichael Holzheu 76*fe742c08SSven Schnelle BUILD_BUG_ON(sizeof(struct os_info) != PAGE_SIZE); 774857d4bbSMichael Holzheu os_info.version_major = OS_INFO_VERSION_MAJOR; 784857d4bbSMichael Holzheu os_info.version_minor = OS_INFO_VERSION_MINOR; 794857d4bbSMichael Holzheu os_info.magic = OS_INFO_MAGIC; 808572f525SAlexander Gordeev os_info_entry_add_val(OS_INFO_IDENTITY_BASE, __identity_base); 818572f525SAlexander Gordeev os_info_entry_add_val(OS_INFO_KASLR_OFFSET, kaslr_offset()); 828572f525SAlexander Gordeev os_info_entry_add_val(OS_INFO_KASLR_OFF_PHYS, __kaslr_offset_phys); 838572f525SAlexander Gordeev os_info_entry_add_val(OS_INFO_VMEMMAP, (unsigned long)vmemmap); 848572f525SAlexander Gordeev os_info_entry_add_val(OS_INFO_AMODE31_START, AMODE31_START); 858572f525SAlexander Gordeev os_info_entry_add_val(OS_INFO_AMODE31_END, AMODE31_END); 86f4cac27dSAlexander Gordeev os_info_entry_add_val(OS_INFO_IMAGE_START, (unsigned long)_stext); 87f4cac27dSAlexander Gordeev os_info_entry_add_val(OS_INFO_IMAGE_END, (unsigned long)_end); 88f4cac27dSAlexander Gordeev os_info_entry_add_val(OS_INFO_IMAGE_PHYS, __pa_symbol(_stext)); 894857d4bbSMichael Holzheu os_info.csum = os_info_csum(&os_info); 902154e0b3SAlexander Gordeev abs_lc = get_abs_lowcore(); 914df29d2bSAlexander Gordeev abs_lc->os_info = __pa(&os_info); 922154e0b3SAlexander Gordeev put_abs_lowcore(abs_lc); 934857d4bbSMichael Holzheu } 944857d4bbSMichael Holzheu 954857d4bbSMichael Holzheu #ifdef CONFIG_CRASH_DUMP 964857d4bbSMichael Holzheu 974857d4bbSMichael Holzheu static struct os_info *os_info_old; 984857d4bbSMichael Holzheu 994857d4bbSMichael Holzheu /* 1004857d4bbSMichael Holzheu * Allocate and copy OS info entry from oldmem 1014857d4bbSMichael Holzheu */ 1024857d4bbSMichael Holzheu static void os_info_old_alloc(int nr, int align) 1034857d4bbSMichael Holzheu { 1044857d4bbSMichael Holzheu unsigned long addr, size = 0; 1054857d4bbSMichael Holzheu char *buf, *buf_align, *msg; 1064857d4bbSMichael Holzheu u32 csum; 1074857d4bbSMichael Holzheu 1084857d4bbSMichael Holzheu addr = os_info_old->entry[nr].addr; 1094857d4bbSMichael Holzheu if (!addr) { 1104857d4bbSMichael Holzheu msg = "not available"; 1114857d4bbSMichael Holzheu goto fail; 1124857d4bbSMichael Holzheu } 1134857d4bbSMichael Holzheu size = os_info_old->entry[nr].size; 1144857d4bbSMichael Holzheu buf = kmalloc(size + align - 1, GFP_KERNEL); 1154857d4bbSMichael Holzheu if (!buf) { 1164857d4bbSMichael Holzheu msg = "alloc failed"; 1174857d4bbSMichael Holzheu goto fail; 1184857d4bbSMichael Holzheu } 1194857d4bbSMichael Holzheu buf_align = PTR_ALIGN(buf, align); 120303fd988SAlexander Gordeev if (copy_oldmem_kernel(buf_align, addr, size)) { 1214857d4bbSMichael Holzheu msg = "copy failed"; 1224857d4bbSMichael Holzheu goto fail_free; 1234857d4bbSMichael Holzheu } 1243a74f44dSHeiko Carstens csum = (__force u32)cksm(buf_align, size, 0); 1254857d4bbSMichael Holzheu if (csum != os_info_old->entry[nr].csum) { 1264857d4bbSMichael Holzheu msg = "checksum failed"; 1274857d4bbSMichael Holzheu goto fail_free; 1284857d4bbSMichael Holzheu } 1294857d4bbSMichael Holzheu os_info_old->entry[nr].addr = (u64)(unsigned long)buf_align; 1304857d4bbSMichael Holzheu msg = "copied"; 1314857d4bbSMichael Holzheu goto out; 1324857d4bbSMichael Holzheu fail_free: 1334857d4bbSMichael Holzheu kfree(buf); 1344857d4bbSMichael Holzheu fail: 1354857d4bbSMichael Holzheu os_info_old->entry[nr].addr = 0; 1364857d4bbSMichael Holzheu out: 1374857d4bbSMichael Holzheu pr_info("entry %i: %s (addr=0x%lx size=%lu)\n", 1384857d4bbSMichael Holzheu nr, msg, addr, size); 1394857d4bbSMichael Holzheu } 1404857d4bbSMichael Holzheu 1414857d4bbSMichael Holzheu /* 1424857d4bbSMichael Holzheu * Initialize os info and os info entries from oldmem 1434857d4bbSMichael Holzheu */ 1444857d4bbSMichael Holzheu static void os_info_old_init(void) 1454857d4bbSMichael Holzheu { 1464857d4bbSMichael Holzheu static int os_info_init; 1474857d4bbSMichael Holzheu unsigned long addr; 1484857d4bbSMichael Holzheu 1494857d4bbSMichael Holzheu if (os_info_init) 1504857d4bbSMichael Holzheu return; 151a2269a66SAlexander Egorenkov if (!oldmem_data.start && !is_ipl_type_dump()) 1524857d4bbSMichael Holzheu goto fail; 153303fd988SAlexander Gordeev if (copy_oldmem_kernel(&addr, __LC_OS_INFO, sizeof(addr))) 1544857d4bbSMichael Holzheu goto fail; 1554857d4bbSMichael Holzheu if (addr == 0 || addr % PAGE_SIZE) 1564857d4bbSMichael Holzheu goto fail; 1574857d4bbSMichael Holzheu os_info_old = kzalloc(sizeof(*os_info_old), GFP_KERNEL); 1584857d4bbSMichael Holzheu if (!os_info_old) 1594857d4bbSMichael Holzheu goto fail; 160303fd988SAlexander Gordeev if (copy_oldmem_kernel(os_info_old, addr, sizeof(*os_info_old))) 1614857d4bbSMichael Holzheu goto fail_free; 1624857d4bbSMichael Holzheu if (os_info_old->magic != OS_INFO_MAGIC) 1634857d4bbSMichael Holzheu goto fail_free; 1644857d4bbSMichael Holzheu if (os_info_old->csum != os_info_csum(os_info_old)) 1654857d4bbSMichael Holzheu goto fail_free; 1664857d4bbSMichael Holzheu if (os_info_old->version_major > OS_INFO_VERSION_MAJOR) 1674857d4bbSMichael Holzheu goto fail_free; 1684857d4bbSMichael Holzheu os_info_old_alloc(OS_INFO_VMCOREINFO, 1); 1694857d4bbSMichael Holzheu os_info_old_alloc(OS_INFO_REIPL_BLOCK, 1); 1704857d4bbSMichael Holzheu pr_info("crashkernel: addr=0x%lx size=%lu\n", 1714857d4bbSMichael Holzheu (unsigned long) os_info_old->crashkernel_addr, 1724857d4bbSMichael Holzheu (unsigned long) os_info_old->crashkernel_size); 1734857d4bbSMichael Holzheu os_info_init = 1; 1744857d4bbSMichael Holzheu return; 1754857d4bbSMichael Holzheu fail_free: 1764857d4bbSMichael Holzheu kfree(os_info_old); 1774857d4bbSMichael Holzheu fail: 1784857d4bbSMichael Holzheu os_info_init = 1; 1794857d4bbSMichael Holzheu os_info_old = NULL; 1804857d4bbSMichael Holzheu } 1814857d4bbSMichael Holzheu 1824857d4bbSMichael Holzheu /* 1834857d4bbSMichael Holzheu * Return pointer to os infor entry and its size 1844857d4bbSMichael Holzheu */ 1854857d4bbSMichael Holzheu void *os_info_old_entry(int nr, unsigned long *size) 1864857d4bbSMichael Holzheu { 1874857d4bbSMichael Holzheu os_info_old_init(); 1884857d4bbSMichael Holzheu 1894857d4bbSMichael Holzheu if (!os_info_old) 1904857d4bbSMichael Holzheu return NULL; 1914857d4bbSMichael Holzheu if (!os_info_old->entry[nr].addr) 1924857d4bbSMichael Holzheu return NULL; 1934857d4bbSMichael Holzheu *size = (unsigned long) os_info_old->entry[nr].size; 1944857d4bbSMichael Holzheu return (void *)(unsigned long)os_info_old->entry[nr].addr; 1954857d4bbSMichael Holzheu } 1964857d4bbSMichael Holzheu #endif 197