1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright IBM Corp. 2005, 2011
4 *
5 * Author(s): Rolf Adelsberger,
6 * Michael Holzheu <holzheu@linux.vnet.ibm.com>
7 */
8
9 #include <linux/device.h>
10 #include <linux/mm.h>
11 #include <linux/kexec.h>
12 #include <linux/delay.h>
13 #include <linux/reboot.h>
14 #include <linux/ftrace.h>
15 #include <linux/debug_locks.h>
16 #include <linux/cpufeature.h>
17 #include <asm/guarded_storage.h>
18 #include <asm/machine.h>
19 #include <asm/pfault.h>
20 #include <asm/cio.h>
21 #include <asm/fpu.h>
22 #include <asm/setup.h>
23 #include <asm/smp.h>
24 #include <asm/ipl.h>
25 #include <asm/diag.h>
26 #include <asm/elf.h>
27 #include <asm/asm-offsets.h>
28 #include <asm/cacheflush.h>
29 #include <asm/abs_lowcore.h>
30 #include <asm/os_info.h>
31 #include <asm/set_memory.h>
32 #include <asm/stacktrace.h>
33 #include <asm/nmi.h>
34 #include <asm/sclp.h>
35
36 typedef void (*relocate_kernel_t)(unsigned long, unsigned long, unsigned long);
37 typedef int (*purgatory_t)(int);
38
39 extern const unsigned char relocate_kernel[];
40 extern const unsigned long long relocate_kernel_len;
41
42 #ifdef CONFIG_CRASH_DUMP
43
44 /*
45 * Reset the system, copy boot CPU registers to absolute zero,
46 * and jump to the kdump image
47 */
__do_machine_kdump(void * data)48 static void __do_machine_kdump(void *data)
49 {
50 struct kimage *image = data;
51 purgatory_t purgatory;
52 unsigned long prefix;
53
54 purgatory = (purgatory_t)image->start;
55
56 /* store_status() saved the prefix register to lowcore */
57 prefix = (unsigned long)get_lowcore()->prefixreg_save_area;
58
59 /* Now do the reset */
60 s390_reset_system();
61
62 /*
63 * Copy dump CPU store status info to absolute zero.
64 * This need to be done *after* s390_reset_system set the
65 * prefix register of this CPU to zero
66 */
67 memcpy(absolute_pointer(get_lowcore()->floating_pt_save_area),
68 phys_to_virt(prefix + __LC_FPREGS_SAVE_AREA), 512);
69
70 call_nodat(1, int, purgatory, int, 1);
71
72 /* Die if kdump returns */
73 disabled_wait();
74 }
75
76 /*
77 * Start kdump: create a LGR log entry, store status of all CPUs and
78 * branch to __do_machine_kdump.
79 */
__machine_kdump(void * image)80 static noinline void __machine_kdump(void *image)
81 {
82 struct mcesa *mcesa;
83 union ctlreg2 cr2_old, cr2_new;
84 int this_cpu, cpu;
85
86 lgr_info_log();
87 /* Get status of the other CPUs */
88 this_cpu = smp_find_processor_id(stap());
89 for_each_online_cpu(cpu) {
90 if (cpu == this_cpu)
91 continue;
92 if (smp_store_status(cpu))
93 continue;
94 }
95 /* Store status of the boot CPU */
96 mcesa = __va(get_lowcore()->mcesad & MCESA_ORIGIN_MASK);
97 if (cpu_has_vx())
98 save_vx_regs((__vector128 *) mcesa->vector_save_area);
99 if (cpu_has_gs()) {
100 local_ctl_store(2, &cr2_old.reg);
101 cr2_new = cr2_old;
102 cr2_new.gse = 1;
103 local_ctl_load(2, &cr2_new.reg);
104 save_gs_cb((struct gs_cb *) mcesa->guarded_storage_save_area);
105 local_ctl_load(2, &cr2_old.reg);
106 }
107 /*
108 * To create a good backchain for this CPU in the dump store_status
109 * is passed the address of a function. The address is saved into
110 * the PSW save area of the boot CPU and the function is invoked as
111 * a tail call of store_status. The backchain in the dump will look
112 * like this:
113 * restart_int_handler -> __machine_kexec -> __do_machine_kdump
114 * The call to store_status() will not return.
115 */
116 store_status(__do_machine_kdump, image);
117 }
118
119 #endif /* CONFIG_CRASH_DUMP */
120
121 /*
122 * Check if kdump checksums are valid: We call purgatory with parameter "0"
123 */
kdump_csum_valid(struct kimage * image)124 static bool kdump_csum_valid(struct kimage *image)
125 {
126 #ifdef CONFIG_CRASH_DUMP
127 purgatory_t purgatory = (purgatory_t)image->start;
128 int rc;
129
130 rc = call_nodat(1, int, purgatory, int, 0);
131 return rc == 0;
132 #else
133 return false;
134 #endif
135 }
136
137 #ifdef CONFIG_CRASH_DUMP
138
crash_free_reserved_phys_range(unsigned long begin,unsigned long end)139 void crash_free_reserved_phys_range(unsigned long begin, unsigned long end)
140 {
141 unsigned long addr, size;
142
143 for (addr = begin; addr < end; addr += PAGE_SIZE)
144 free_reserved_page(pfn_to_page(addr >> PAGE_SHIFT));
145 size = begin - crashk_res.start;
146 if (size)
147 os_info_crashkernel_add(crashk_res.start, size);
148 else
149 os_info_crashkernel_add(0, 0);
150 }
151
crash_protect_pages(int protect)152 static void crash_protect_pages(int protect)
153 {
154 unsigned long size;
155
156 if (!crashk_res.end)
157 return;
158 size = resource_size(&crashk_res);
159 if (protect)
160 set_memory_ro(crashk_res.start, size >> PAGE_SHIFT);
161 else
162 set_memory_rw(crashk_res.start, size >> PAGE_SHIFT);
163 }
164
arch_kexec_protect_crashkres(void)165 void arch_kexec_protect_crashkres(void)
166 {
167 crash_protect_pages(1);
168 }
169
arch_kexec_unprotect_crashkres(void)170 void arch_kexec_unprotect_crashkres(void)
171 {
172 crash_protect_pages(0);
173 }
174
175 #endif
176
177 /*
178 * Give back memory to hypervisor before new kdump is loaded
179 */
machine_kexec_prepare_kdump(void)180 static int machine_kexec_prepare_kdump(void)
181 {
182 #ifdef CONFIG_CRASH_DUMP
183 if (machine_is_vm())
184 diag10_range(PFN_DOWN(crashk_res.start),
185 PFN_DOWN(crashk_res.end - crashk_res.start + 1));
186 return 0;
187 #else
188 return -EINVAL;
189 #endif
190 }
191
machine_kexec_prepare(struct kimage * image)192 int machine_kexec_prepare(struct kimage *image)
193 {
194 void *reboot_code_buffer;
195
196 if (image->type == KEXEC_TYPE_CRASH)
197 return machine_kexec_prepare_kdump();
198
199 /* We don't support anything but the default image type for now. */
200 if (image->type != KEXEC_TYPE_DEFAULT)
201 return -EINVAL;
202
203 /* Get the destination where the assembler code should be copied to.*/
204 reboot_code_buffer = page_to_virt(image->control_code_page);
205
206 /* Then copy it */
207 memcpy(reboot_code_buffer, relocate_kernel, relocate_kernel_len);
208 return 0;
209 }
210
machine_kexec_cleanup(struct kimage * image)211 void machine_kexec_cleanup(struct kimage *image)
212 {
213 }
214
machine_shutdown(void)215 void machine_shutdown(void)
216 {
217 }
218
machine_crash_shutdown(struct pt_regs * regs)219 void machine_crash_shutdown(struct pt_regs *regs)
220 {
221 set_os_info_reipl_block();
222 }
223
224 /*
225 * Do normal kexec
226 */
__do_machine_kexec(void * data)227 static void __do_machine_kexec(void *data)
228 {
229 unsigned long data_mover, entry, diag308_subcode;
230 struct kimage *image = data;
231
232 data_mover = page_to_phys(image->control_code_page);
233 entry = virt_to_phys(&image->head);
234 diag308_subcode = DIAG308_CLEAR_RESET;
235 if (sclp.has_iplcc)
236 diag308_subcode |= DIAG308_FLAG_EI;
237 s390_reset_system();
238
239 call_nodat(3, void, (relocate_kernel_t)data_mover,
240 unsigned long, entry,
241 unsigned long, image->start,
242 unsigned long, diag308_subcode);
243
244 /* Die if kexec returns */
245 disabled_wait();
246 }
247
248 /*
249 * Reset system and call either kdump or normal kexec
250 */
__machine_kexec(void * data)251 static void __machine_kexec(void *data)
252 {
253 pfault_fini();
254 tracing_off();
255 debug_locks_off();
256 #ifdef CONFIG_CRASH_DUMP
257 if (((struct kimage *) data)->type == KEXEC_TYPE_CRASH)
258 __machine_kdump(data);
259 #endif
260 __do_machine_kexec(data);
261 }
262
263 /*
264 * Do either kdump or normal kexec. In case of kdump we first ask
265 * purgatory, if kdump checksums are valid.
266 */
machine_kexec(struct kimage * image)267 void machine_kexec(struct kimage *image)
268 {
269 if (image->type == KEXEC_TYPE_CRASH && !kdump_csum_valid(image))
270 return;
271 tracer_disable();
272 smp_send_stop();
273 smp_call_ipl_cpu(__machine_kexec, image);
274 }
275