1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * OS info memory interface
4 *
5 * Copyright IBM Corp. 2012
6 * Author(s): Michael Holzheu <[email protected]>
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/abs_lowcore.h>
17 #include <asm/os_info.h>
18 #include <asm/physmem_info.h>
19 #include <asm/maccess.h>
20 #include <asm/asm-offsets.h>
21 #include <asm/sections.h>
22 #include <asm/ipl.h>
23
24 /*
25 * OS info structure has to be page aligned
26 */
27 static struct os_info os_info __page_aligned_data;
28
29 /*
30 * Compute checksum over OS info structure
31 */
os_info_csum(struct os_info * os_info)32 u32 os_info_csum(struct os_info *os_info)
33 {
34 int size = sizeof(*os_info) - offsetof(struct os_info, version_major);
35 return (__force u32)cksm(&os_info->version_major, size, 0);
36 }
37
38 /*
39 * Add crashkernel info to OS info and update checksum
40 */
os_info_crashkernel_add(unsigned long base,unsigned long size)41 void os_info_crashkernel_add(unsigned long base, unsigned long size)
42 {
43 os_info.crashkernel_addr = (u64)(unsigned long)base;
44 os_info.crashkernel_size = (u64)(unsigned long)size;
45 os_info.csum = os_info_csum(&os_info);
46 }
47
48 /*
49 * Add OS info data entry and update checksum
50 */
os_info_entry_add_data(int nr,void * ptr,u64 size)51 void os_info_entry_add_data(int nr, void *ptr, u64 size)
52 {
53 os_info.entry[nr].addr = __pa(ptr);
54 os_info.entry[nr].size = size;
55 os_info.entry[nr].csum = (__force u32)cksm(ptr, size, 0);
56 os_info.csum = os_info_csum(&os_info);
57 }
58
59 /*
60 * Add OS info value entry and update checksum
61 */
os_info_entry_add_val(int nr,u64 value)62 void os_info_entry_add_val(int nr, u64 value)
63 {
64 os_info.entry[nr].val = value;
65 os_info.entry[nr].size = 0;
66 os_info.entry[nr].csum = 0;
67 os_info.csum = os_info_csum(&os_info);
68 }
69
70 /*
71 * Initialize OS info structure and set lowcore pointer
72 */
os_info_init(void)73 void __init os_info_init(void)
74 {
75 struct lowcore *abs_lc;
76
77 BUILD_BUG_ON(sizeof(struct os_info) != PAGE_SIZE);
78 os_info.version_major = OS_INFO_VERSION_MAJOR;
79 os_info.version_minor = OS_INFO_VERSION_MINOR;
80 os_info.magic = OS_INFO_MAGIC;
81 os_info_entry_add_val(OS_INFO_IDENTITY_BASE, __identity_base);
82 os_info_entry_add_val(OS_INFO_KASLR_OFFSET, kaslr_offset());
83 os_info_entry_add_val(OS_INFO_KASLR_OFF_PHYS, __kaslr_offset_phys);
84 os_info_entry_add_val(OS_INFO_VMEMMAP, (unsigned long)vmemmap);
85 os_info_entry_add_val(OS_INFO_AMODE31_START, AMODE31_START);
86 os_info_entry_add_val(OS_INFO_AMODE31_END, AMODE31_END);
87 os_info_entry_add_val(OS_INFO_IMAGE_START, (unsigned long)_stext);
88 os_info_entry_add_val(OS_INFO_IMAGE_END, (unsigned long)_end);
89 os_info_entry_add_val(OS_INFO_IMAGE_PHYS, __pa_symbol(_stext));
90 os_info.csum = os_info_csum(&os_info);
91 abs_lc = get_abs_lowcore();
92 abs_lc->os_info = __pa(&os_info);
93 put_abs_lowcore(abs_lc);
94 }
95
96 #ifdef CONFIG_CRASH_DUMP
97
98 static struct os_info *os_info_old;
99
100 /*
101 * Allocate and copy OS info entry from oldmem
102 */
os_info_old_alloc(int nr,int align)103 static void os_info_old_alloc(int nr, int align)
104 {
105 unsigned long addr, size = 0;
106 char *buf, *buf_align, *msg;
107 u32 csum;
108
109 addr = os_info_old->entry[nr].addr;
110 if (!addr) {
111 msg = "not available";
112 goto fail;
113 }
114 size = os_info_old->entry[nr].size;
115 buf = kmalloc(size + align - 1, GFP_KERNEL);
116 if (!buf) {
117 msg = "alloc failed";
118 goto fail;
119 }
120 buf_align = PTR_ALIGN(buf, align);
121 if (copy_oldmem_kernel(buf_align, addr, size)) {
122 msg = "copy failed";
123 goto fail_free;
124 }
125 csum = (__force u32)cksm(buf_align, size, 0);
126 if (csum != os_info_old->entry[nr].csum) {
127 msg = "checksum failed";
128 goto fail_free;
129 }
130 os_info_old->entry[nr].addr = (u64)(unsigned long)buf_align;
131 msg = "copied";
132 goto out;
133 fail_free:
134 kfree(buf);
135 fail:
136 os_info_old->entry[nr].addr = 0;
137 out:
138 pr_info("entry %i: %s (addr=0x%lx size=%lu)\n",
139 nr, msg, addr, size);
140 }
141
142 /*
143 * Initialize os info and os info entries from oldmem
144 */
os_info_old_init(void)145 static void os_info_old_init(void)
146 {
147 static int os_info_init;
148 unsigned long addr;
149
150 if (os_info_init)
151 return;
152 if (!oldmem_data.start && !is_ipl_type_dump())
153 goto fail;
154 if (copy_oldmem_kernel(&addr, __LC_OS_INFO, sizeof(addr)))
155 goto fail;
156 if (addr == 0 || addr % PAGE_SIZE)
157 goto fail;
158 os_info_old = kzalloc(sizeof(*os_info_old), GFP_KERNEL);
159 if (!os_info_old)
160 goto fail;
161 if (copy_oldmem_kernel(os_info_old, addr, sizeof(*os_info_old)))
162 goto fail_free;
163 if (os_info_old->magic != OS_INFO_MAGIC)
164 goto fail_free;
165 if (os_info_old->csum != os_info_csum(os_info_old))
166 goto fail_free;
167 if (os_info_old->version_major > OS_INFO_VERSION_MAJOR)
168 goto fail_free;
169 os_info_old_alloc(OS_INFO_VMCOREINFO, 1);
170 os_info_old_alloc(OS_INFO_REIPL_BLOCK, 1);
171 pr_info("crashkernel: addr=0x%lx size=%lu\n",
172 (unsigned long) os_info_old->crashkernel_addr,
173 (unsigned long) os_info_old->crashkernel_size);
174 os_info_init = 1;
175 return;
176 fail_free:
177 kfree(os_info_old);
178 fail:
179 os_info_init = 1;
180 os_info_old = NULL;
181 }
182
183 /*
184 * Return pointer to os info entry and its size
185 */
os_info_old_entry(int nr,unsigned long * size)186 void *os_info_old_entry(int nr, unsigned long *size)
187 {
188 os_info_old_init();
189
190 if (!os_info_old)
191 return NULL;
192 if (!os_info_old->entry[nr].addr)
193 return NULL;
194 *size = (unsigned long) os_info_old->entry[nr].size;
195 return (void *)(unsigned long)os_info_old->entry[nr].addr;
196 }
197 #endif
198