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