Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * OS info memory interface
0004  *
0005  * Copyright IBM Corp. 2012
0006  * Author(s): Michael Holzheu <holzheu@linux.vnet.ibm.com>
0007  */
0008 
0009 #define KMSG_COMPONENT "os_info"
0010 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
0011 
0012 #include <linux/crash_dump.h>
0013 #include <linux/kernel.h>
0014 #include <linux/slab.h>
0015 #include <asm/checksum.h>
0016 #include <asm/lowcore.h>
0017 #include <asm/os_info.h>
0018 #include <asm/asm-offsets.h>
0019 
0020 /*
0021  * OS info structure has to be page aligned
0022  */
0023 static struct os_info os_info __page_aligned_data;
0024 
0025 /*
0026  * Compute checksum over OS info structure
0027  */
0028 u32 os_info_csum(struct os_info *os_info)
0029 {
0030     int size = sizeof(*os_info) - offsetof(struct os_info, version_major);
0031     return (__force u32)csum_partial(&os_info->version_major, size, 0);
0032 }
0033 
0034 /*
0035  * Add crashkernel info to OS info and update checksum
0036  */
0037 void os_info_crashkernel_add(unsigned long base, unsigned long size)
0038 {
0039     os_info.crashkernel_addr = (u64)(unsigned long)base;
0040     os_info.crashkernel_size = (u64)(unsigned long)size;
0041     os_info.csum = os_info_csum(&os_info);
0042 }
0043 
0044 /*
0045  * Add OS info entry and update checksum
0046  */
0047 void os_info_entry_add(int nr, void *ptr, u64 size)
0048 {
0049     os_info.entry[nr].addr = __pa(ptr);
0050     os_info.entry[nr].size = size;
0051     os_info.entry[nr].csum = (__force u32)csum_partial(ptr, size, 0);
0052     os_info.csum = os_info_csum(&os_info);
0053 }
0054 
0055 /*
0056  * Initialize OS info structure and set lowcore pointer
0057  */
0058 void __init os_info_init(void)
0059 {
0060     void *ptr = &os_info;
0061 
0062     os_info.version_major = OS_INFO_VERSION_MAJOR;
0063     os_info.version_minor = OS_INFO_VERSION_MINOR;
0064     os_info.magic = OS_INFO_MAGIC;
0065     os_info.csum = os_info_csum(&os_info);
0066     put_abs_lowcore(os_info, __pa(ptr));
0067 }
0068 
0069 #ifdef CONFIG_CRASH_DUMP
0070 
0071 static struct os_info *os_info_old;
0072 
0073 /*
0074  * Allocate and copy OS info entry from oldmem
0075  */
0076 static void os_info_old_alloc(int nr, int align)
0077 {
0078     unsigned long addr, size = 0;
0079     char *buf, *buf_align, *msg;
0080     u32 csum;
0081 
0082     addr = os_info_old->entry[nr].addr;
0083     if (!addr) {
0084         msg = "not available";
0085         goto fail;
0086     }
0087     size = os_info_old->entry[nr].size;
0088     buf = kmalloc(size + align - 1, GFP_KERNEL);
0089     if (!buf) {
0090         msg = "alloc failed";
0091         goto fail;
0092     }
0093     buf_align = PTR_ALIGN(buf, align);
0094     if (copy_oldmem_kernel(buf_align, addr, size)) {
0095         msg = "copy failed";
0096         goto fail_free;
0097     }
0098     csum = (__force u32)csum_partial(buf_align, size, 0);
0099     if (csum != os_info_old->entry[nr].csum) {
0100         msg = "checksum failed";
0101         goto fail_free;
0102     }
0103     os_info_old->entry[nr].addr = (u64)(unsigned long)buf_align;
0104     msg = "copied";
0105     goto out;
0106 fail_free:
0107     kfree(buf);
0108 fail:
0109     os_info_old->entry[nr].addr = 0;
0110 out:
0111     pr_info("entry %i: %s (addr=0x%lx size=%lu)\n",
0112         nr, msg, addr, size);
0113 }
0114 
0115 /*
0116  * Initialize os info and os info entries from oldmem
0117  */
0118 static void os_info_old_init(void)
0119 {
0120     static int os_info_init;
0121     unsigned long addr;
0122 
0123     if (os_info_init)
0124         return;
0125     if (!oldmem_data.start)
0126         goto fail;
0127     if (copy_oldmem_kernel(&addr, __LC_OS_INFO, sizeof(addr)))
0128         goto fail;
0129     if (addr == 0 || addr % PAGE_SIZE)
0130         goto fail;
0131     os_info_old = kzalloc(sizeof(*os_info_old), GFP_KERNEL);
0132     if (!os_info_old)
0133         goto fail;
0134     if (copy_oldmem_kernel(os_info_old, addr, sizeof(*os_info_old)))
0135         goto fail_free;
0136     if (os_info_old->magic != OS_INFO_MAGIC)
0137         goto fail_free;
0138     if (os_info_old->csum != os_info_csum(os_info_old))
0139         goto fail_free;
0140     if (os_info_old->version_major > OS_INFO_VERSION_MAJOR)
0141         goto fail_free;
0142     os_info_old_alloc(OS_INFO_VMCOREINFO, 1);
0143     os_info_old_alloc(OS_INFO_REIPL_BLOCK, 1);
0144     pr_info("crashkernel: addr=0x%lx size=%lu\n",
0145         (unsigned long) os_info_old->crashkernel_addr,
0146         (unsigned long) os_info_old->crashkernel_size);
0147     os_info_init = 1;
0148     return;
0149 fail_free:
0150     kfree(os_info_old);
0151 fail:
0152     os_info_init = 1;
0153     os_info_old = NULL;
0154 }
0155 
0156 /*
0157  * Return pointer to os infor entry and its size
0158  */
0159 void *os_info_old_entry(int nr, unsigned long *size)
0160 {
0161     os_info_old_init();
0162 
0163     if (!os_info_old)
0164         return NULL;
0165     if (!os_info_old->entry[nr].addr)
0166         return NULL;
0167     *size = (unsigned long) os_info_old->entry[nr].size;
0168     return (void *)(unsigned long)os_info_old->entry[nr].addr;
0169 }
0170 #endif