xref: /linux/arch/s390/kernel/machine_kexec.c (revision 0883c2c06fb5bcf5b9e008270827e63c09a88c1e)
1 /*
2  * Copyright IBM Corp. 2005, 2011
3  *
4  * Author(s): Rolf Adelsberger,
5  *	      Heiko Carstens <heiko.carstens@de.ibm.com>
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/suspend.h>
17 #include <asm/cio.h>
18 #include <asm/setup.h>
19 #include <asm/pgtable.h>
20 #include <asm/pgalloc.h>
21 #include <asm/smp.h>
22 #include <asm/reset.h>
23 #include <asm/ipl.h>
24 #include <asm/diag.h>
25 #include <asm/elf.h>
26 #include <asm/asm-offsets.h>
27 #include <asm/os_info.h>
28 #include <asm/switch_to.h>
29 
30 typedef void (*relocate_kernel_t)(kimage_entry_t *, unsigned long);
31 
32 extern const unsigned char relocate_kernel[];
33 extern const unsigned long long relocate_kernel_len;
34 
35 #ifdef CONFIG_CRASH_DUMP
36 
37 /*
38  * PM notifier callback for kdump
39  */
40 static int machine_kdump_pm_cb(struct notifier_block *nb, unsigned long action,
41 			       void *ptr)
42 {
43 	switch (action) {
44 	case PM_SUSPEND_PREPARE:
45 	case PM_HIBERNATION_PREPARE:
46 		if (kexec_crash_image)
47 			arch_kexec_unprotect_crashkres();
48 		break;
49 	case PM_POST_SUSPEND:
50 	case PM_POST_HIBERNATION:
51 		if (kexec_crash_image)
52 			arch_kexec_protect_crashkres();
53 		break;
54 	default:
55 		return NOTIFY_DONE;
56 	}
57 	return NOTIFY_OK;
58 }
59 
60 static int __init machine_kdump_pm_init(void)
61 {
62 	pm_notifier(machine_kdump_pm_cb, 0);
63 	/* Create initial mapping for crashkernel memory */
64 	arch_kexec_unprotect_crashkres();
65 	return 0;
66 }
67 arch_initcall(machine_kdump_pm_init);
68 
69 /*
70  * Reset the system, copy boot CPU registers to absolute zero,
71  * and jump to the kdump image
72  */
73 static void __do_machine_kdump(void *image)
74 {
75 	int (*start_kdump)(int);
76 	unsigned long prefix;
77 
78 	/* store_status() saved the prefix register to lowcore */
79 	prefix = (unsigned long) S390_lowcore.prefixreg_save_area;
80 
81 	/* Now do the reset  */
82 	s390_reset_system();
83 
84 	/*
85 	 * Copy dump CPU store status info to absolute zero.
86 	 * This need to be done *after* s390_reset_system set the
87 	 * prefix register of this CPU to zero
88 	 */
89 	memcpy((void *) __LC_FPREGS_SAVE_AREA,
90 	       (void *)(prefix + __LC_FPREGS_SAVE_AREA), 512);
91 
92 	__load_psw_mask(PSW_MASK_BASE | PSW_DEFAULT_KEY | PSW_MASK_EA | PSW_MASK_BA);
93 	start_kdump = (void *)((struct kimage *) image)->start;
94 	start_kdump(1);
95 
96 	/* Die if start_kdump returns */
97 	disabled_wait((unsigned long) __builtin_return_address(0));
98 }
99 
100 /*
101  * Start kdump: create a LGR log entry, store status of all CPUs and
102  * branch to __do_machine_kdump.
103  */
104 static noinline void __machine_kdump(void *image)
105 {
106 	int this_cpu, cpu;
107 
108 	lgr_info_log();
109 	/* Get status of the other CPUs */
110 	this_cpu = smp_find_processor_id(stap());
111 	for_each_online_cpu(cpu) {
112 		if (cpu == this_cpu)
113 			continue;
114 		if (smp_store_status(cpu))
115 			continue;
116 	}
117 	/* Store status of the boot CPU */
118 	if (MACHINE_HAS_VX)
119 		save_vx_regs((void *) &S390_lowcore.vector_save_area);
120 	/*
121 	 * To create a good backchain for this CPU in the dump store_status
122 	 * is passed the address of a function. The address is saved into
123 	 * the PSW save area of the boot CPU and the function is invoked as
124 	 * a tail call of store_status. The backchain in the dump will look
125 	 * like this:
126 	 *   restart_int_handler ->  __machine_kexec -> __do_machine_kdump
127 	 * The call to store_status() will not return.
128 	 */
129 	store_status(__do_machine_kdump, image);
130 }
131 #endif
132 
133 /*
134  * Check if kdump checksums are valid: We call purgatory with parameter "0"
135  */
136 static int kdump_csum_valid(struct kimage *image)
137 {
138 #ifdef CONFIG_CRASH_DUMP
139 	int (*start_kdump)(int) = (void *)image->start;
140 	int rc;
141 
142 	__arch_local_irq_stnsm(0xfb); /* disable DAT */
143 	rc = start_kdump(0);
144 	__arch_local_irq_stosm(0x04); /* enable DAT */
145 	return rc ? 0 : -EINVAL;
146 #else
147 	return -EINVAL;
148 #endif
149 }
150 
151 #ifdef CONFIG_CRASH_DUMP
152 
153 /*
154  * Map or unmap crashkernel memory
155  */
156 static void crash_map_pages(int enable)
157 {
158 	unsigned long size = resource_size(&crashk_res);
159 
160 	BUG_ON(crashk_res.start % KEXEC_CRASH_MEM_ALIGN ||
161 	       size % KEXEC_CRASH_MEM_ALIGN);
162 	if (enable)
163 		vmem_add_mapping(crashk_res.start, size);
164 	else {
165 		vmem_remove_mapping(crashk_res.start, size);
166 		if (size)
167 			os_info_crashkernel_add(crashk_res.start, size);
168 		else
169 			os_info_crashkernel_add(0, 0);
170 	}
171 }
172 
173 /*
174  * Unmap crashkernel memory
175  */
176 void arch_kexec_protect_crashkres(void)
177 {
178 	if (crashk_res.end)
179 		crash_map_pages(0);
180 }
181 
182 /*
183  * Map crashkernel memory
184  */
185 void arch_kexec_unprotect_crashkres(void)
186 {
187 	if (crashk_res.end)
188 		crash_map_pages(1);
189 }
190 
191 #endif
192 
193 /*
194  * Give back memory to hypervisor before new kdump is loaded
195  */
196 static int machine_kexec_prepare_kdump(void)
197 {
198 #ifdef CONFIG_CRASH_DUMP
199 	if (MACHINE_IS_VM)
200 		diag10_range(PFN_DOWN(crashk_res.start),
201 			     PFN_DOWN(crashk_res.end - crashk_res.start + 1));
202 	return 0;
203 #else
204 	return -EINVAL;
205 #endif
206 }
207 
208 int machine_kexec_prepare(struct kimage *image)
209 {
210 	void *reboot_code_buffer;
211 
212 	/* Can't replace kernel image since it is read-only. */
213 	if (ipl_flags & IPL_NSS_VALID)
214 		return -EOPNOTSUPP;
215 
216 	if (image->type == KEXEC_TYPE_CRASH)
217 		return machine_kexec_prepare_kdump();
218 
219 	/* We don't support anything but the default image type for now. */
220 	if (image->type != KEXEC_TYPE_DEFAULT)
221 		return -EINVAL;
222 
223 	/* Get the destination where the assembler code should be copied to.*/
224 	reboot_code_buffer = (void *) page_to_phys(image->control_code_page);
225 
226 	/* Then copy it */
227 	memcpy(reboot_code_buffer, relocate_kernel, relocate_kernel_len);
228 	return 0;
229 }
230 
231 void machine_kexec_cleanup(struct kimage *image)
232 {
233 }
234 
235 void arch_crash_save_vmcoreinfo(void)
236 {
237 	VMCOREINFO_SYMBOL(lowcore_ptr);
238 	VMCOREINFO_SYMBOL(high_memory);
239 	VMCOREINFO_LENGTH(lowcore_ptr, NR_CPUS);
240 }
241 
242 void machine_shutdown(void)
243 {
244 }
245 
246 void machine_crash_shutdown(struct pt_regs *regs)
247 {
248 }
249 
250 /*
251  * Do normal kexec
252  */
253 static void __do_machine_kexec(void *data)
254 {
255 	relocate_kernel_t data_mover;
256 	struct kimage *image = data;
257 
258 	s390_reset_system();
259 	data_mover = (relocate_kernel_t) page_to_phys(image->control_code_page);
260 
261 	/* Call the moving routine */
262 	(*data_mover)(&image->head, image->start);
263 
264 	/* Die if kexec returns */
265 	disabled_wait((unsigned long) __builtin_return_address(0));
266 }
267 
268 /*
269  * Reset system and call either kdump or normal kexec
270  */
271 static void __machine_kexec(void *data)
272 {
273 	__arch_local_irq_stosm(0x04); /* enable DAT */
274 	pfault_fini();
275 	tracing_off();
276 	debug_locks_off();
277 #ifdef CONFIG_CRASH_DUMP
278 	if (((struct kimage *) data)->type == KEXEC_TYPE_CRASH)
279 		__machine_kdump(data);
280 #endif
281 	__do_machine_kexec(data);
282 }
283 
284 /*
285  * Do either kdump or normal kexec. In case of kdump we first ask
286  * purgatory, if kdump checksums are valid.
287  */
288 void machine_kexec(struct kimage *image)
289 {
290 	if (image->type == KEXEC_TYPE_CRASH && !kdump_csum_valid(image))
291 		return;
292 	tracer_disable();
293 	smp_send_stop();
294 	smp_call_ipl_cpu(__machine_kexec, image);
295 }
296