blob: dcaadceaf6efcbc1887be39e6208dc9d40551a2d [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Heiko Carstenscf13f0e2005-06-25 14:58:11 -07002/*
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02003 * Copyright IBM Corp. 2005, 2011
Heiko Carstenscf13f0e2005-06-25 14:58:11 -07004 *
Heiko Carstensc6b5b842006-12-04 15:40:33 +01005 * Author(s): Rolf Adelsberger,
6 * Heiko Carstens <heiko.carstens@de.ibm.com>
Michael Holzheu60a0c682011-10-30 15:16:40 +01007 * Michael Holzheu <holzheu@linux.vnet.ibm.com>
Heiko Carstenscf13f0e2005-06-25 14:58:11 -07008 */
9
Heiko Carstenscf13f0e2005-06-25 14:58:11 -070010#include <linux/device.h>
11#include <linux/mm.h>
12#include <linux/kexec.h>
13#include <linux/delay.h>
Heiko Carstens2b67fc42007-02-05 21:16:47 +010014#include <linux/reboot.h>
Heiko Carstens69667272011-03-15 17:08:33 +010015#include <linux/ftrace.h>
Michael Holzheu3ab121a2012-03-11 11:59:32 -040016#include <linux/debug_locks.h>
Michael Holzheub66ac632013-04-04 19:49:53 +020017#include <linux/suspend.h>
Heiko Carstensa386fba2006-02-11 17:56:01 -080018#include <asm/cio.h>
19#include <asm/setup.h>
Heiko Carstenscf13f0e2005-06-25 14:58:11 -070020#include <asm/pgtable.h>
21#include <asm/pgalloc.h>
Heiko Carstensa386fba2006-02-11 17:56:01 -080022#include <asm/smp.h>
Heiko Carstens9c9c1762007-03-05 23:35:45 +010023#include <asm/ipl.h>
Michael Holzheu60a0c682011-10-30 15:16:40 +010024#include <asm/diag.h>
Heiko Carstens6b563d82012-09-14 14:11:32 +020025#include <asm/elf.h>
Michael Holzheu60a0c682011-10-30 15:16:40 +010026#include <asm/asm-offsets.h>
Heiko Carstens4e042af2016-05-31 09:14:00 +020027#include <asm/cacheflush.h>
Michael Holzheua9fbf1a2012-05-18 15:10:05 +020028#include <asm/os_info.h>
Laura Abbotte6c7c632017-05-08 15:58:08 -070029#include <asm/set_memory.h>
Martin Schwidefsky78c98f92019-01-28 08:33:08 +010030#include <asm/stacktrace.h>
Michael Holzheua62bc072014-10-06 17:57:43 +020031#include <asm/switch_to.h>
Martin Schwidefsky916cda12016-01-26 14:10:34 +010032#include <asm/nmi.h>
Heiko Carstenscf13f0e2005-06-25 14:58:11 -070033
Heiko Carstensc6b5b842006-12-04 15:40:33 +010034typedef void (*relocate_kernel_t)(kimage_entry_t *, unsigned long);
Heiko Carstenscf13f0e2005-06-25 14:58:11 -070035
Tobias Klauser2efe55a2006-06-26 18:57:34 +020036extern const unsigned char relocate_kernel[];
37extern const unsigned long long relocate_kernel_len;
Heiko Carstenscf13f0e2005-06-25 14:58:11 -070038
Michael Holzheu60a0c682011-10-30 15:16:40 +010039#ifdef CONFIG_CRASH_DUMP
40
Michael Holzheu60a0c682011-10-30 15:16:40 +010041/*
Michael Holzheub66ac632013-04-04 19:49:53 +020042 * PM notifier callback for kdump
43 */
44static int machine_kdump_pm_cb(struct notifier_block *nb, unsigned long action,
45 void *ptr)
46{
47 switch (action) {
48 case PM_SUSPEND_PREPARE:
49 case PM_HIBERNATION_PREPARE:
Xunlei Pang7a0058e2016-05-23 16:24:22 -070050 if (kexec_crash_image)
51 arch_kexec_unprotect_crashkres();
Michael Holzheub66ac632013-04-04 19:49:53 +020052 break;
53 case PM_POST_SUSPEND:
54 case PM_POST_HIBERNATION:
Xunlei Pang7a0058e2016-05-23 16:24:22 -070055 if (kexec_crash_image)
56 arch_kexec_protect_crashkres();
Michael Holzheub66ac632013-04-04 19:49:53 +020057 break;
58 default:
59 return NOTIFY_DONE;
60 }
61 return NOTIFY_OK;
62}
63
64static int __init machine_kdump_pm_init(void)
65{
66 pm_notifier(machine_kdump_pm_cb, 0);
67 return 0;
68}
69arch_initcall(machine_kdump_pm_init);
Michael Holzheu60a0c682011-10-30 15:16:40 +010070
71/*
Martin Schwidefsky1a36a392015-10-29 10:28:26 +010072 * Reset the system, copy boot CPU registers to absolute zero,
73 * and jump to the kdump image
Michael Holzheu60a0c682011-10-30 15:16:40 +010074 */
75static void __do_machine_kdump(void *image)
76{
Martin Schwidefsky1a36a392015-10-29 10:28:26 +010077 int (*start_kdump)(int);
78 unsigned long prefix;
79
80 /* store_status() saved the prefix register to lowcore */
81 prefix = (unsigned long) S390_lowcore.prefixreg_save_area;
82
83 /* Now do the reset */
84 s390_reset_system();
85
86 /*
87 * Copy dump CPU store status info to absolute zero.
88 * This need to be done *after* s390_reset_system set the
89 * prefix register of this CPU to zero
90 */
91 memcpy((void *) __LC_FPREGS_SAVE_AREA,
92 (void *)(prefix + __LC_FPREGS_SAVE_AREA), 512);
Michael Holzheu60a0c682011-10-30 15:16:40 +010093
Michael Holzheufa7c0042012-05-21 11:30:30 +020094 __load_psw_mask(PSW_MASK_BASE | PSW_DEFAULT_KEY | PSW_MASK_EA | PSW_MASK_BA);
Martin Schwidefsky1a36a392015-10-29 10:28:26 +010095 start_kdump = (void *)((struct kimage *) image)->start;
Michael Holzheu60a0c682011-10-30 15:16:40 +010096 start_kdump(1);
Martin Schwidefsky1a36a392015-10-29 10:28:26 +010097
98 /* Die if start_kdump returns */
Martin Schwidefsky98587c22019-04-30 12:33:45 +020099 disabled_wait();
Martin Schwidefsky1a36a392015-10-29 10:28:26 +0100100}
101
102/*
103 * Start kdump: create a LGR log entry, store status of all CPUs and
104 * branch to __do_machine_kdump.
105 */
106static noinline void __machine_kdump(void *image)
107{
Martin Schwidefsky916cda12016-01-26 14:10:34 +0100108 struct mcesa *mcesa;
Martin Schwidefskyad3bc0a2017-10-12 13:24:45 +0200109 union ctlreg2 cr2_old, cr2_new;
Martin Schwidefsky1a36a392015-10-29 10:28:26 +0100110 int this_cpu, cpu;
111
112 lgr_info_log();
113 /* Get status of the other CPUs */
114 this_cpu = smp_find_processor_id(stap());
115 for_each_online_cpu(cpu) {
116 if (cpu == this_cpu)
117 continue;
118 if (smp_store_status(cpu))
119 continue;
120 }
121 /* Store status of the boot CPU */
Martin Schwidefsky916cda12016-01-26 14:10:34 +0100122 mcesa = (struct mcesa *)(S390_lowcore.mcesad & MCESA_ORIGIN_MASK);
Martin Schwidefsky1a36a392015-10-29 10:28:26 +0100123 if (MACHINE_HAS_VX)
Martin Schwidefsky916cda12016-01-26 14:10:34 +0100124 save_vx_regs((__vector128 *) mcesa->vector_save_area);
125 if (MACHINE_HAS_GS) {
Martin Schwidefskyad3bc0a2017-10-12 13:24:45 +0200126 __ctl_store(cr2_old.val, 2, 2);
127 cr2_new = cr2_old;
128 cr2_new.gse = 1;
129 __ctl_load(cr2_new.val, 2, 2);
Martin Schwidefsky916cda12016-01-26 14:10:34 +0100130 save_gs_cb((struct gs_cb *) mcesa->guarded_storage_save_area);
Martin Schwidefskyad3bc0a2017-10-12 13:24:45 +0200131 __ctl_load(cr2_old.val, 2, 2);
Martin Schwidefsky916cda12016-01-26 14:10:34 +0100132 }
Martin Schwidefsky1a36a392015-10-29 10:28:26 +0100133 /*
134 * To create a good backchain for this CPU in the dump store_status
135 * is passed the address of a function. The address is saved into
136 * the PSW save area of the boot CPU and the function is invoked as
137 * a tail call of store_status. The backchain in the dump will look
138 * like this:
139 * restart_int_handler -> __machine_kexec -> __do_machine_kdump
140 * The call to store_status() will not return.
141 */
142 store_status(__do_machine_kdump, image);
Michael Holzheu60a0c682011-10-30 15:16:40 +0100143}
144
Martin Schwidefskyce3dc442017-09-12 16:37:33 +0200145static unsigned long do_start_kdump(unsigned long addr)
Michael Holzheu60a0c682011-10-30 15:16:40 +0100146{
Martin Schwidefskyce3dc442017-09-12 16:37:33 +0200147 struct kimage *image = (struct kimage *) addr;
Michael Holzheu60a0c682011-10-30 15:16:40 +0100148 int (*start_kdump)(int) = (void *)image->start;
149 int rc;
150
151 __arch_local_irq_stnsm(0xfb); /* disable DAT */
152 rc = start_kdump(0);
153 __arch_local_irq_stosm(0x04); /* enable DAT */
Martin Schwidefskyce3dc442017-09-12 16:37:33 +0200154 return rc;
155}
156
Heiko Carstens2980ba62019-06-10 17:22:38 +0200157#endif /* CONFIG_CRASH_DUMP */
158
Martin Schwidefskyce3dc442017-09-12 16:37:33 +0200159/*
160 * Check if kdump checksums are valid: We call purgatory with parameter "0"
161 */
162static bool kdump_csum_valid(struct kimage *image)
163{
164#ifdef CONFIG_CRASH_DUMP
165 int rc;
166
Vasily Gorbik7f28dad2019-11-22 12:19:16 +0100167 preempt_disable();
Martin Schwidefskyce3dc442017-09-12 16:37:33 +0200168 rc = CALL_ON_STACK(do_start_kdump, S390_lowcore.nodat_stack, 1, image);
Vasily Gorbik7f28dad2019-11-22 12:19:16 +0100169 preempt_enable();
Philipp Rudo7c3eaaa2017-10-17 12:28:08 +0200170 return rc == 0;
Michael Holzheu60a0c682011-10-30 15:16:40 +0100171#else
Philipp Rudo7c3eaaa2017-10-17 12:28:08 +0200172 return false;
Michael Holzheu60a0c682011-10-30 15:16:40 +0100173#endif
174}
175
Xunlei Pang7a0058e2016-05-23 16:24:22 -0700176#ifdef CONFIG_CRASH_DUMP
177
Heiko Carstens2d0af222016-05-31 09:13:59 +0200178void crash_free_reserved_phys_range(unsigned long begin, unsigned long end)
179{
180 unsigned long addr, size;
181
182 for (addr = begin; addr < end; addr += PAGE_SIZE)
183 free_reserved_page(pfn_to_page(addr >> PAGE_SHIFT));
184 size = begin - crashk_res.start;
185 if (size)
186 os_info_crashkernel_add(crashk_res.start, size);
187 else
188 os_info_crashkernel_add(0, 0);
189}
190
Heiko Carstens4e042af2016-05-31 09:14:00 +0200191static void crash_protect_pages(int protect)
Michael Holzheudab7a7b2011-10-30 15:16:44 +0100192{
Heiko Carstens4e042af2016-05-31 09:14:00 +0200193 unsigned long size;
Michael Holzheudab7a7b2011-10-30 15:16:44 +0100194
Heiko Carstens4e042af2016-05-31 09:14:00 +0200195 if (!crashk_res.end)
196 return;
197 size = resource_size(&crashk_res);
198 if (protect)
199 set_memory_ro(crashk_res.start, size >> PAGE_SHIFT);
Heiko Carstens2d0af222016-05-31 09:13:59 +0200200 else
Heiko Carstens4e042af2016-05-31 09:14:00 +0200201 set_memory_rw(crashk_res.start, size >> PAGE_SHIFT);
Michael Holzheudab7a7b2011-10-30 15:16:44 +0100202}
203
Xunlei Pang7a0058e2016-05-23 16:24:22 -0700204void arch_kexec_protect_crashkres(void)
Michael Holzheudab7a7b2011-10-30 15:16:44 +0100205{
Heiko Carstens4e042af2016-05-31 09:14:00 +0200206 crash_protect_pages(1);
Michael Holzheudab7a7b2011-10-30 15:16:44 +0100207}
208
Xunlei Pang7a0058e2016-05-23 16:24:22 -0700209void arch_kexec_unprotect_crashkres(void)
Michael Holzheudab7a7b2011-10-30 15:16:44 +0100210{
Heiko Carstens4e042af2016-05-31 09:14:00 +0200211 crash_protect_pages(0);
Michael Holzheudab7a7b2011-10-30 15:16:44 +0100212}
213
Xunlei Pang7a0058e2016-05-23 16:24:22 -0700214#endif
215
Michael Holzheudab7a7b2011-10-30 15:16:44 +0100216/*
Michael Holzheu60a0c682011-10-30 15:16:40 +0100217 * Give back memory to hypervisor before new kdump is loaded
218 */
219static int machine_kexec_prepare_kdump(void)
220{
221#ifdef CONFIG_CRASH_DUMP
222 if (MACHINE_IS_VM)
223 diag10_range(PFN_DOWN(crashk_res.start),
224 PFN_DOWN(crashk_res.end - crashk_res.start + 1));
225 return 0;
226#else
227 return -EINVAL;
228#endif
229}
230
Heiko Carstensc6b5b842006-12-04 15:40:33 +0100231int machine_kexec_prepare(struct kimage *image)
Heiko Carstenscf13f0e2005-06-25 14:58:11 -0700232{
Heiko Carstensc6b5b842006-12-04 15:40:33 +0100233 void *reboot_code_buffer;
Heiko Carstenscf13f0e2005-06-25 14:58:11 -0700234
Michael Holzheu60a0c682011-10-30 15:16:40 +0100235 if (image->type == KEXEC_TYPE_CRASH)
236 return machine_kexec_prepare_kdump();
237
Heiko Carstenscf13f0e2005-06-25 14:58:11 -0700238 /* We don't support anything but the default image type for now. */
239 if (image->type != KEXEC_TYPE_DEFAULT)
240 return -EINVAL;
241
242 /* Get the destination where the assembler code should be copied to.*/
Heiko Carstensc6b5b842006-12-04 15:40:33 +0100243 reboot_code_buffer = (void *) page_to_phys(image->control_code_page);
Heiko Carstenscf13f0e2005-06-25 14:58:11 -0700244
245 /* Then copy it */
Heiko Carstensc6b5b842006-12-04 15:40:33 +0100246 memcpy(reboot_code_buffer, relocate_kernel, relocate_kernel_len);
Heiko Carstenscf13f0e2005-06-25 14:58:11 -0700247 return 0;
248}
249
Heiko Carstensc6b5b842006-12-04 15:40:33 +0100250void machine_kexec_cleanup(struct kimage *image)
Heiko Carstenscf13f0e2005-06-25 14:58:11 -0700251{
252}
253
Michael Holzheu60a0c682011-10-30 15:16:40 +0100254void arch_crash_save_vmcoreinfo(void)
255{
256 VMCOREINFO_SYMBOL(lowcore_ptr);
Michael Holzheu7fe7a182011-12-27 11:27:24 +0100257 VMCOREINFO_SYMBOL(high_memory);
Michael Holzheu60a0c682011-10-30 15:16:40 +0100258 VMCOREINFO_LENGTH(lowcore_ptr, NR_CPUS);
Xunlei Pang203e9e42017-07-12 14:33:14 -0700259 mem_assign_absolute(S390_lowcore.vmcore_info, paddr_vmcoreinfo_note());
Gerald Schaefera80313f2019-02-03 21:37:20 +0100260 vmcoreinfo_append_str("SDMA=%lx\n", __sdma);
261 vmcoreinfo_append_str("EDMA=%lx\n", __edma);
Gerald Schaeferb2d24b92019-02-03 21:37:20 +0100262 vmcoreinfo_append_str("KERNELOFFSET=%lx\n", kaslr_offset());
Michael Holzheu60a0c682011-10-30 15:16:40 +0100263}
264
Heiko Carstensc6b5b842006-12-04 15:40:33 +0100265void machine_shutdown(void)
Heiko Carstenscf13f0e2005-06-25 14:58:11 -0700266{
Heiko Carstenscf13f0e2005-06-25 14:58:11 -0700267}
268
Heiko Carstens48a8ca02012-08-27 15:50:29 +0200269void machine_crash_shutdown(struct pt_regs *regs)
270{
Vasily Gorbik3b967842018-04-04 14:42:41 +0200271 set_os_info_reipl_block();
Heiko Carstens48a8ca02012-08-27 15:50:29 +0200272}
273
Michael Holzheu60a0c682011-10-30 15:16:40 +0100274/*
275 * Do normal kexec
276 */
277static void __do_machine_kexec(void *data)
Heiko Carstenscf13f0e2005-06-25 14:58:11 -0700278{
Heiko Carstenscf13f0e2005-06-25 14:58:11 -0700279 relocate_kernel_t data_mover;
Heiko Carstens2c2df112010-02-26 22:37:34 +0100280 struct kimage *image = data;
Heiko Carstenscf13f0e2005-06-25 14:58:11 -0700281
Martin Schwidefsky1a36a392015-10-29 10:28:26 +0100282 s390_reset_system();
Heiko Carstensc6b5b842006-12-04 15:40:33 +0100283 data_mover = (relocate_kernel_t) page_to_phys(image->control_code_page);
Heiko Carstenscf13f0e2005-06-25 14:58:11 -0700284
Heiko Carstensd0e810e2017-11-09 23:00:14 +0100285 __arch_local_irq_stnsm(0xfb); /* disable DAT - avoid no-execute */
Heiko Carstenscf13f0e2005-06-25 14:58:11 -0700286 /* Call the moving routine */
Heiko Carstensc6b5b842006-12-04 15:40:33 +0100287 (*data_mover)(&image->head, image->start);
Martin Schwidefsky1a36a392015-10-29 10:28:26 +0100288
289 /* Die if kexec returns */
Martin Schwidefsky98587c22019-04-30 12:33:45 +0200290 disabled_wait();
Heiko Carstenscf13f0e2005-06-25 14:58:11 -0700291}
Heiko Carstens2c2df112010-02-26 22:37:34 +0100292
Michael Holzheu60a0c682011-10-30 15:16:40 +0100293/*
294 * Reset system and call either kdump or normal kexec
295 */
296static void __machine_kexec(void *data)
297{
Michael Holzheufa7c0042012-05-21 11:30:30 +0200298 __arch_local_irq_stosm(0x04); /* enable DAT */
Michael Holzheu60a0c682011-10-30 15:16:40 +0100299 pfault_fini();
Michael Holzheu3ab121a2012-03-11 11:59:32 -0400300 tracing_off();
301 debug_locks_off();
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100302#ifdef CONFIG_CRASH_DUMP
Martin Schwidefsky1a36a392015-10-29 10:28:26 +0100303 if (((struct kimage *) data)->type == KEXEC_TYPE_CRASH)
304 __machine_kdump(data);
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100305#endif
Martin Schwidefsky1a36a392015-10-29 10:28:26 +0100306 __do_machine_kexec(data);
Michael Holzheu60a0c682011-10-30 15:16:40 +0100307}
308
309/*
310 * Do either kdump or normal kexec. In case of kdump we first ask
311 * purgatory, if kdump checksums are valid.
312 */
Heiko Carstens2c2df112010-02-26 22:37:34 +0100313void machine_kexec(struct kimage *image)
314{
Michael Holzheu60a0c682011-10-30 15:16:40 +0100315 if (image->type == KEXEC_TYPE_CRASH && !kdump_csum_valid(image))
316 return;
Heiko Carstens69667272011-03-15 17:08:33 +0100317 tracer_disable();
Heiko Carstens2c2df112010-02-26 22:37:34 +0100318 smp_send_stop();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400319 smp_call_ipl_cpu(__machine_kexec, image);
Heiko Carstens2c2df112010-02-26 22:37:34 +0100320}