• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * OS info memory interface
4  *
5  * Copyright IBM Corp. 2012
6  * Author(s): Michael Holzheu <holzheu@linux.vnet.ibm.com>
7  */
8 
9 #define KMSG_COMPONENT "os_info"
10 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
11 
12 #include <linux/crash_dump.h>
13 #include <linux/kernel.h>
14 #include <linux/slab.h>
15 #include <asm/checksum.h>
16 #include <asm/lowcore.h>
17 #include <asm/os_info.h>
18 #include <asm/asm-offsets.h>
19 
20 /*
21  * OS info structure has to be page aligned
22  */
23 static struct os_info os_info __page_aligned_data;
24 
25 /*
26  * Compute checksum over OS info structure
27  */
os_info_csum(struct os_info * os_info)28 u32 os_info_csum(struct os_info *os_info)
29 {
30 	int size = sizeof(*os_info) - offsetof(struct os_info, version_major);
31 	return (__force u32)csum_partial(&os_info->version_major, size, 0);
32 }
33 
34 /*
35  * Add crashkernel info to OS info and update checksum
36  */
os_info_crashkernel_add(unsigned long base,unsigned long size)37 void os_info_crashkernel_add(unsigned long base, unsigned long size)
38 {
39 	os_info.crashkernel_addr = (u64)(unsigned long)base;
40 	os_info.crashkernel_size = (u64)(unsigned long)size;
41 	os_info.csum = os_info_csum(&os_info);
42 }
43 
44 /*
45  * Add OS info entry and update checksum
46  */
os_info_entry_add(int nr,void * ptr,u64 size)47 void os_info_entry_add(int nr, void *ptr, u64 size)
48 {
49 	os_info.entry[nr].addr = (u64)(unsigned long)ptr;
50 	os_info.entry[nr].size = size;
51 	os_info.entry[nr].csum = (__force u32)csum_partial(ptr, size, 0);
52 	os_info.csum = os_info_csum(&os_info);
53 }
54 
55 /*
56  * Initialize OS info struture and set lowcore pointer
57  */
os_info_init(void)58 void __init os_info_init(void)
59 {
60 	void *ptr = &os_info;
61 
62 	os_info.version_major = OS_INFO_VERSION_MAJOR;
63 	os_info.version_minor = OS_INFO_VERSION_MINOR;
64 	os_info.magic = OS_INFO_MAGIC;
65 	os_info.csum = os_info_csum(&os_info);
66 	mem_assign_absolute(S390_lowcore.os_info, (unsigned long) ptr);
67 }
68 
69 #ifdef CONFIG_CRASH_DUMP
70 
71 static struct os_info *os_info_old;
72 
73 /*
74  * Allocate and copy OS info entry from oldmem
75  */
os_info_old_alloc(int nr,int align)76 static void os_info_old_alloc(int nr, int align)
77 {
78 	unsigned long addr, size = 0;
79 	char *buf, *buf_align, *msg;
80 	u32 csum;
81 
82 	addr = os_info_old->entry[nr].addr;
83 	if (!addr) {
84 		msg = "not available";
85 		goto fail;
86 	}
87 	size = os_info_old->entry[nr].size;
88 	buf = kmalloc(size + align - 1, GFP_KERNEL);
89 	if (!buf) {
90 		msg = "alloc failed";
91 		goto fail;
92 	}
93 	buf_align = PTR_ALIGN(buf, align);
94 	if (copy_oldmem_kernel(buf_align, (void *) addr, size)) {
95 		msg = "copy failed";
96 		goto fail_free;
97 	}
98 	csum = (__force u32)csum_partial(buf_align, size, 0);
99 	if (csum != os_info_old->entry[nr].csum) {
100 		msg = "checksum failed";
101 		goto fail_free;
102 	}
103 	os_info_old->entry[nr].addr = (u64)(unsigned long)buf_align;
104 	msg = "copied";
105 	goto out;
106 fail_free:
107 	kfree(buf);
108 fail:
109 	os_info_old->entry[nr].addr = 0;
110 out:
111 	pr_info("entry %i: %s (addr=0x%lx size=%lu)\n",
112 		nr, msg, addr, size);
113 }
114 
115 /*
116  * Initialize os info and os info entries from oldmem
117  */
os_info_old_init(void)118 static void os_info_old_init(void)
119 {
120 	static int os_info_init;
121 	unsigned long addr;
122 
123 	if (os_info_init)
124 		return;
125 	if (!OLDMEM_BASE)
126 		goto fail;
127 	if (copy_oldmem_kernel(&addr, (void *)__LC_OS_INFO, sizeof(addr)))
128 		goto fail;
129 	if (addr == 0 || addr % PAGE_SIZE)
130 		goto fail;
131 	os_info_old = kzalloc(sizeof(*os_info_old), GFP_KERNEL);
132 	if (!os_info_old)
133 		goto fail;
134 	if (copy_oldmem_kernel(os_info_old, (void *) addr,
135 			       sizeof(*os_info_old)))
136 		goto fail_free;
137 	if (os_info_old->magic != OS_INFO_MAGIC)
138 		goto fail_free;
139 	if (os_info_old->csum != os_info_csum(os_info_old))
140 		goto fail_free;
141 	if (os_info_old->version_major > OS_INFO_VERSION_MAJOR)
142 		goto fail_free;
143 	os_info_old_alloc(OS_INFO_VMCOREINFO, 1);
144 	os_info_old_alloc(OS_INFO_REIPL_BLOCK, 1);
145 	pr_info("crashkernel: addr=0x%lx size=%lu\n",
146 		(unsigned long) os_info_old->crashkernel_addr,
147 		(unsigned long) os_info_old->crashkernel_size);
148 	os_info_init = 1;
149 	return;
150 fail_free:
151 	kfree(os_info_old);
152 fail:
153 	os_info_init = 1;
154 	os_info_old = NULL;
155 }
156 
157 /*
158  * Return pointer to os infor entry and its size
159  */
os_info_old_entry(int nr,unsigned long * size)160 void *os_info_old_entry(int nr, unsigned long *size)
161 {
162 	os_info_old_init();
163 
164 	if (!os_info_old)
165 		return NULL;
166 	if (!os_info_old->entry[nr].addr)
167 		return NULL;
168 	*size = (unsigned long) os_info_old->entry[nr].size;
169 	return (void *)(unsigned long)os_info_old->entry[nr].addr;
170 }
171 #endif
172