1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright IBM Corp. 2005, 2011
5 * Author(s): Rolf Adelsberger,
6 * Michael Holzheu <holzheu@linux.vnet.ibm.com>
9 #include <linux/device.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>
17 #include <asm/setup.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>
32 typedef void (*relocate_kernel_t)(unsigned long, unsigned long, unsigned long);
33 typedef int (*purgatory_t)(int);
35 extern const unsigned char relocate_kernel[];
36 extern const unsigned long long relocate_kernel_len;
38 #ifdef CONFIG_CRASH_DUMP
41 * Reset the system, copy boot CPU registers to absolute zero,
42 * and jump to the kdump image
44 static void __do_machine_kdump(void *data)
46 struct kimage *image = data;
47 purgatory_t purgatory;
50 purgatory = (purgatory_t)image->start;
52 /* store_status() saved the prefix register to lowcore */
53 prefix = (unsigned long) S390_lowcore.prefixreg_save_area;
55 /* Now do the reset */
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
63 memcpy(absolute_pointer(__LC_FPREGS_SAVE_AREA),
64 phys_to_virt(prefix + __LC_FPREGS_SAVE_AREA), 512);
66 call_nodat(1, int, purgatory, int, 1);
68 /* Die if kdump returns */
73 * Start kdump: create a LGR log entry, store status of all CPUs and
74 * branch to __do_machine_kdump.
76 static noinline void __machine_kdump(void *image)
79 union ctlreg2 cr2_old, cr2_new;
83 /* Get status of the other CPUs */
84 this_cpu = smp_find_processor_id(stap());
85 for_each_online_cpu(cpu) {
88 if (smp_store_status(cpu))
91 /* Store status of the boot CPU */
92 mcesa = __va(S390_lowcore.mcesad & MCESA_ORIGIN_MASK);
94 save_vx_regs((__vector128 *) mcesa->vector_save_area);
96 __ctl_store(cr2_old.val, 2, 2);
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);
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
109 * restart_int_handler -> __machine_kexec -> __do_machine_kdump
110 * The call to store_status() will not return.
112 store_status(__do_machine_kdump, image);
115 #endif /* CONFIG_CRASH_DUMP */
118 * Check if kdump checksums are valid: We call purgatory with parameter "0"
120 static bool kdump_csum_valid(struct kimage *image)
122 #ifdef CONFIG_CRASH_DUMP
123 purgatory_t purgatory = (purgatory_t)image->start;
126 rc = call_nodat(1, int, purgatory, int, 0);
133 #ifdef CONFIG_CRASH_DUMP
135 void crash_free_reserved_phys_range(unsigned long begin, unsigned long end)
137 unsigned long addr, size;
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;
143 os_info_crashkernel_add(crashk_res.start, size);
145 os_info_crashkernel_add(0, 0);
148 static void crash_protect_pages(int protect)
154 size = resource_size(&crashk_res);
156 set_memory_ro(crashk_res.start, size >> PAGE_SHIFT);
158 set_memory_rw(crashk_res.start, size >> PAGE_SHIFT);
161 void arch_kexec_protect_crashkres(void)
163 crash_protect_pages(1);
166 void arch_kexec_unprotect_crashkres(void)
168 crash_protect_pages(0);
174 * Give back memory to hypervisor before new kdump is loaded
176 static int machine_kexec_prepare_kdump(void)
178 #ifdef CONFIG_CRASH_DUMP
180 diag10_range(PFN_DOWN(crashk_res.start),
181 PFN_DOWN(crashk_res.end - crashk_res.start + 1));
188 int machine_kexec_prepare(struct kimage *image)
190 void *reboot_code_buffer;
192 if (image->type == KEXEC_TYPE_CRASH)
193 return machine_kexec_prepare_kdump();
195 /* We don't support anything but the default image type for now. */
196 if (image->type != KEXEC_TYPE_DEFAULT)
199 /* Get the destination where the assembler code should be copied to.*/
200 reboot_code_buffer = page_to_virt(image->control_code_page);
203 memcpy(reboot_code_buffer, relocate_kernel, relocate_kernel_len);
207 void machine_kexec_cleanup(struct kimage *image)
211 void arch_crash_save_vmcoreinfo(void)
213 struct lowcore *abs_lc;
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);
226 void machine_shutdown(void)
230 void machine_crash_shutdown(struct pt_regs *regs)
232 set_os_info_reipl_block();
238 static void __do_machine_kexec(void *data)
240 unsigned long data_mover, entry, diag308_subcode;
241 struct kimage *image = data;
243 data_mover = page_to_phys(image->control_code_page);
244 entry = virt_to_phys(&image->head);
245 diag308_subcode = DIAG308_CLEAR_RESET;
247 diag308_subcode |= DIAG308_FLAG_EI;
250 call_nodat(3, void, (relocate_kernel_t)data_mover,
251 unsigned long, entry,
252 unsigned long, image->start,
253 unsigned long, diag308_subcode);
255 /* Die if kexec returns */
260 * Reset system and call either kdump or normal kexec
262 static void __machine_kexec(void *data)
267 #ifdef CONFIG_CRASH_DUMP
268 if (((struct kimage *) data)->type == KEXEC_TYPE_CRASH)
269 __machine_kdump(data);
271 __do_machine_kexec(data);
275 * Do either kdump or normal kexec. In case of kdump we first ask
276 * purgatory, if kdump checksums are valid.
278 void machine_kexec(struct kimage *image)
280 if (image->type == KEXEC_TYPE_CRASH && !kdump_csum_valid(image))
284 smp_call_ipl_cpu(__machine_kexec, image);