blob: 2f3a742a71a5de6bd77919d03db9862ef60fed8d [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 */
99 disabled_wait((unsigned long) __builtin_return_address(0));
100}
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}
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100144#endif
Michael Holzheu60a0c682011-10-30 15:16:40 +0100145
Martin Schwidefskyce3dc442017-09-12 16:37:33 +0200146static unsigned long do_start_kdump(unsigned long addr)
Michael Holzheu60a0c682011-10-30 15:16:40 +0100147{
Martin Schwidefskyce3dc442017-09-12 16:37:33 +0200148 struct kimage *image = (struct kimage *) addr;
Michael Holzheu60a0c682011-10-30 15:16:40 +0100149 int (*start_kdump)(int) = (void *)image->start;
150 int rc;
151
152 __arch_local_irq_stnsm(0xfb); /* disable DAT */
153 rc = start_kdump(0);
154 __arch_local_irq_stosm(0x04); /* enable DAT */
Martin Schwidefskyce3dc442017-09-12 16:37:33 +0200155 return rc;
156}
157
158/*
159 * Check if kdump checksums are valid: We call purgatory with parameter "0"
160 */
161static bool kdump_csum_valid(struct kimage *image)
162{
163#ifdef CONFIG_CRASH_DUMP
164 int rc;
165
166 rc = CALL_ON_STACK(do_start_kdump, S390_lowcore.nodat_stack, 1, image);
Philipp Rudo7c3eaaa2017-10-17 12:28:08 +0200167 return rc == 0;
Michael Holzheu60a0c682011-10-30 15:16:40 +0100168#else
Philipp Rudo7c3eaaa2017-10-17 12:28:08 +0200169 return false;
Michael Holzheu60a0c682011-10-30 15:16:40 +0100170#endif
171}
172
Xunlei Pang7a0058e2016-05-23 16:24:22 -0700173#ifdef CONFIG_CRASH_DUMP
174
Heiko Carstens2d0af222016-05-31 09:13:59 +0200175void crash_free_reserved_phys_range(unsigned long begin, unsigned long end)
176{
177 unsigned long addr, size;
178
179 for (addr = begin; addr < end; addr += PAGE_SIZE)
180 free_reserved_page(pfn_to_page(addr >> PAGE_SHIFT));
181 size = begin - crashk_res.start;
182 if (size)
183 os_info_crashkernel_add(crashk_res.start, size);
184 else
185 os_info_crashkernel_add(0, 0);
186}
187
Heiko Carstens4e042af2016-05-31 09:14:00 +0200188static void crash_protect_pages(int protect)
Michael Holzheudab7a7b2011-10-30 15:16:44 +0100189{
Heiko Carstens4e042af2016-05-31 09:14:00 +0200190 unsigned long size;
Michael Holzheudab7a7b2011-10-30 15:16:44 +0100191
Heiko Carstens4e042af2016-05-31 09:14:00 +0200192 if (!crashk_res.end)
193 return;
194 size = resource_size(&crashk_res);
195 if (protect)
196 set_memory_ro(crashk_res.start, size >> PAGE_SHIFT);
Heiko Carstens2d0af222016-05-31 09:13:59 +0200197 else
Heiko Carstens4e042af2016-05-31 09:14:00 +0200198 set_memory_rw(crashk_res.start, size >> PAGE_SHIFT);
Michael Holzheudab7a7b2011-10-30 15:16:44 +0100199}
200
Xunlei Pang7a0058e2016-05-23 16:24:22 -0700201void arch_kexec_protect_crashkres(void)
Michael Holzheudab7a7b2011-10-30 15:16:44 +0100202{
Heiko Carstens4e042af2016-05-31 09:14:00 +0200203 crash_protect_pages(1);
Michael Holzheudab7a7b2011-10-30 15:16:44 +0100204}
205
Xunlei Pang7a0058e2016-05-23 16:24:22 -0700206void arch_kexec_unprotect_crashkres(void)
Michael Holzheudab7a7b2011-10-30 15:16:44 +0100207{
Heiko Carstens4e042af2016-05-31 09:14:00 +0200208 crash_protect_pages(0);
Michael Holzheudab7a7b2011-10-30 15:16:44 +0100209}
210
Xunlei Pang7a0058e2016-05-23 16:24:22 -0700211#endif
212
Michael Holzheudab7a7b2011-10-30 15:16:44 +0100213/*
Michael Holzheu60a0c682011-10-30 15:16:40 +0100214 * Give back memory to hypervisor before new kdump is loaded
215 */
216static int machine_kexec_prepare_kdump(void)
217{
218#ifdef CONFIG_CRASH_DUMP
219 if (MACHINE_IS_VM)
220 diag10_range(PFN_DOWN(crashk_res.start),
221 PFN_DOWN(crashk_res.end - crashk_res.start + 1));
222 return 0;
223#else
224 return -EINVAL;
225#endif
226}
227
Heiko Carstensc6b5b842006-12-04 15:40:33 +0100228int machine_kexec_prepare(struct kimage *image)
Heiko Carstenscf13f0e2005-06-25 14:58:11 -0700229{
Heiko Carstensc6b5b842006-12-04 15:40:33 +0100230 void *reboot_code_buffer;
Heiko Carstenscf13f0e2005-06-25 14:58:11 -0700231
Michael Holzheu60a0c682011-10-30 15:16:40 +0100232 if (image->type == KEXEC_TYPE_CRASH)
233 return machine_kexec_prepare_kdump();
234
Heiko Carstenscf13f0e2005-06-25 14:58:11 -0700235 /* We don't support anything but the default image type for now. */
236 if (image->type != KEXEC_TYPE_DEFAULT)
237 return -EINVAL;
238
239 /* Get the destination where the assembler code should be copied to.*/
Heiko Carstensc6b5b842006-12-04 15:40:33 +0100240 reboot_code_buffer = (void *) page_to_phys(image->control_code_page);
Heiko Carstenscf13f0e2005-06-25 14:58:11 -0700241
242 /* Then copy it */
Heiko Carstensc6b5b842006-12-04 15:40:33 +0100243 memcpy(reboot_code_buffer, relocate_kernel, relocate_kernel_len);
Heiko Carstenscf13f0e2005-06-25 14:58:11 -0700244 return 0;
245}
246
Heiko Carstensc6b5b842006-12-04 15:40:33 +0100247void machine_kexec_cleanup(struct kimage *image)
Heiko Carstenscf13f0e2005-06-25 14:58:11 -0700248{
249}
250
Michael Holzheu60a0c682011-10-30 15:16:40 +0100251void arch_crash_save_vmcoreinfo(void)
252{
253 VMCOREINFO_SYMBOL(lowcore_ptr);
Michael Holzheu7fe7a182011-12-27 11:27:24 +0100254 VMCOREINFO_SYMBOL(high_memory);
Michael Holzheu60a0c682011-10-30 15:16:40 +0100255 VMCOREINFO_LENGTH(lowcore_ptr, NR_CPUS);
Xunlei Pang203e9e42017-07-12 14:33:14 -0700256 mem_assign_absolute(S390_lowcore.vmcore_info, paddr_vmcoreinfo_note());
Gerald Schaefera80313f2019-02-03 21:37:20 +0100257 vmcoreinfo_append_str("SDMA=%lx\n", __sdma);
258 vmcoreinfo_append_str("EDMA=%lx\n", __edma);
Gerald Schaeferb2d24b92019-02-03 21:37:20 +0100259 vmcoreinfo_append_str("KERNELOFFSET=%lx\n", kaslr_offset());
Michael Holzheu60a0c682011-10-30 15:16:40 +0100260}
261
Heiko Carstensc6b5b842006-12-04 15:40:33 +0100262void machine_shutdown(void)
Heiko Carstenscf13f0e2005-06-25 14:58:11 -0700263{
Heiko Carstenscf13f0e2005-06-25 14:58:11 -0700264}
265
Heiko Carstens48a8ca02012-08-27 15:50:29 +0200266void machine_crash_shutdown(struct pt_regs *regs)
267{
Vasily Gorbik3b967842018-04-04 14:42:41 +0200268 set_os_info_reipl_block();
Heiko Carstens48a8ca02012-08-27 15:50:29 +0200269}
270
Michael Holzheu60a0c682011-10-30 15:16:40 +0100271/*
272 * Do normal kexec
273 */
274static void __do_machine_kexec(void *data)
Heiko Carstenscf13f0e2005-06-25 14:58:11 -0700275{
Heiko Carstenscf13f0e2005-06-25 14:58:11 -0700276 relocate_kernel_t data_mover;
Heiko Carstens2c2df112010-02-26 22:37:34 +0100277 struct kimage *image = data;
Heiko Carstenscf13f0e2005-06-25 14:58:11 -0700278
Martin Schwidefsky1a36a392015-10-29 10:28:26 +0100279 s390_reset_system();
Heiko Carstensc6b5b842006-12-04 15:40:33 +0100280 data_mover = (relocate_kernel_t) page_to_phys(image->control_code_page);
Heiko Carstenscf13f0e2005-06-25 14:58:11 -0700281
Heiko Carstensd0e810e2017-11-09 23:00:14 +0100282 __arch_local_irq_stnsm(0xfb); /* disable DAT - avoid no-execute */
Heiko Carstenscf13f0e2005-06-25 14:58:11 -0700283 /* Call the moving routine */
Heiko Carstensc6b5b842006-12-04 15:40:33 +0100284 (*data_mover)(&image->head, image->start);
Martin Schwidefsky1a36a392015-10-29 10:28:26 +0100285
286 /* Die if kexec returns */
287 disabled_wait((unsigned long) __builtin_return_address(0));
Heiko Carstenscf13f0e2005-06-25 14:58:11 -0700288}
Heiko Carstens2c2df112010-02-26 22:37:34 +0100289
Michael Holzheu60a0c682011-10-30 15:16:40 +0100290/*
291 * Reset system and call either kdump or normal kexec
292 */
293static void __machine_kexec(void *data)
294{
Michael Holzheufa7c0042012-05-21 11:30:30 +0200295 __arch_local_irq_stosm(0x04); /* enable DAT */
Michael Holzheu60a0c682011-10-30 15:16:40 +0100296 pfault_fini();
Michael Holzheu3ab121a2012-03-11 11:59:32 -0400297 tracing_off();
298 debug_locks_off();
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100299#ifdef CONFIG_CRASH_DUMP
Martin Schwidefsky1a36a392015-10-29 10:28:26 +0100300 if (((struct kimage *) data)->type == KEXEC_TYPE_CRASH)
301 __machine_kdump(data);
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100302#endif
Martin Schwidefsky1a36a392015-10-29 10:28:26 +0100303 __do_machine_kexec(data);
Michael Holzheu60a0c682011-10-30 15:16:40 +0100304}
305
306/*
307 * Do either kdump or normal kexec. In case of kdump we first ask
308 * purgatory, if kdump checksums are valid.
309 */
Heiko Carstens2c2df112010-02-26 22:37:34 +0100310void machine_kexec(struct kimage *image)
311{
Michael Holzheu60a0c682011-10-30 15:16:40 +0100312 if (image->type == KEXEC_TYPE_CRASH && !kdump_csum_valid(image))
313 return;
Heiko Carstens69667272011-03-15 17:08:33 +0100314 tracer_disable();
Heiko Carstens2c2df112010-02-26 22:37:34 +0100315 smp_send_stop();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400316 smp_call_ipl_cpu(__machine_kexec, image);
Heiko Carstens2c2df112010-02-26 22:37:34 +0100317}