blob: a16467b3825ecd433f0d2a5ff8f6f7cb544caef2 [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>
Heiko Carstensa386fba2006-02-11 17:56:01 -080017#include <asm/cio.h>
18#include <asm/setup.h>
Heiko Carstensa386fba2006-02-11 17:56:01 -080019#include <asm/smp.h>
Heiko Carstens9c9c1762007-03-05 23:35:45 +010020#include <asm/ipl.h>
Michael Holzheu60a0c682011-10-30 15:16:40 +010021#include <asm/diag.h>
Heiko Carstens6b563d82012-09-14 14:11:32 +020022#include <asm/elf.h>
Michael Holzheu60a0c682011-10-30 15:16:40 +010023#include <asm/asm-offsets.h>
Heiko Carstens4e042af2016-05-31 09:14:00 +020024#include <asm/cacheflush.h>
Michael Holzheua9fbf1a2012-05-18 15:10:05 +020025#include <asm/os_info.h>
Laura Abbotte6c7c632017-05-08 15:58:08 -070026#include <asm/set_memory.h>
Martin Schwidefsky78c98f92019-01-28 08:33:08 +010027#include <asm/stacktrace.h>
Michael Holzheua62bc072014-10-06 17:57:43 +020028#include <asm/switch_to.h>
Martin Schwidefsky916cda12016-01-26 14:10:34 +010029#include <asm/nmi.h>
Heiko Carstenscf13f0e2005-06-25 14:58:11 -070030
Heiko Carstensc6b5b842006-12-04 15:40:33 +010031typedef void (*relocate_kernel_t)(kimage_entry_t *, unsigned long);
Heiko Carstenscf13f0e2005-06-25 14:58:11 -070032
Tobias Klauser2efe55a2006-06-26 18:57:34 +020033extern const unsigned char relocate_kernel[];
34extern const unsigned long long relocate_kernel_len;
Heiko Carstenscf13f0e2005-06-25 14:58:11 -070035
Michael Holzheu60a0c682011-10-30 15:16:40 +010036#ifdef CONFIG_CRASH_DUMP
37
Michael Holzheu60a0c682011-10-30 15:16:40 +010038/*
Martin Schwidefsky1a36a392015-10-29 10:28:26 +010039 * Reset the system, copy boot CPU registers to absolute zero,
40 * and jump to the kdump image
Michael Holzheu60a0c682011-10-30 15:16:40 +010041 */
42static void __do_machine_kdump(void *image)
43{
Martin Schwidefsky1a36a392015-10-29 10:28:26 +010044 int (*start_kdump)(int);
45 unsigned long prefix;
46
47 /* store_status() saved the prefix register to lowcore */
48 prefix = (unsigned long) S390_lowcore.prefixreg_save_area;
49
50 /* Now do the reset */
51 s390_reset_system();
52
53 /*
54 * Copy dump CPU store status info to absolute zero.
55 * This need to be done *after* s390_reset_system set the
56 * prefix register of this CPU to zero
57 */
58 memcpy((void *) __LC_FPREGS_SAVE_AREA,
59 (void *)(prefix + __LC_FPREGS_SAVE_AREA), 512);
Michael Holzheu60a0c682011-10-30 15:16:40 +010060
Michael Holzheufa7c0042012-05-21 11:30:30 +020061 __load_psw_mask(PSW_MASK_BASE | PSW_DEFAULT_KEY | PSW_MASK_EA | PSW_MASK_BA);
Martin Schwidefsky1a36a392015-10-29 10:28:26 +010062 start_kdump = (void *)((struct kimage *) image)->start;
Michael Holzheu60a0c682011-10-30 15:16:40 +010063 start_kdump(1);
Martin Schwidefsky1a36a392015-10-29 10:28:26 +010064
65 /* Die if start_kdump returns */
Martin Schwidefsky98587c22019-04-30 12:33:45 +020066 disabled_wait();
Martin Schwidefsky1a36a392015-10-29 10:28:26 +010067}
68
69/*
70 * Start kdump: create a LGR log entry, store status of all CPUs and
71 * branch to __do_machine_kdump.
72 */
73static noinline void __machine_kdump(void *image)
74{
Martin Schwidefsky916cda12016-01-26 14:10:34 +010075 struct mcesa *mcesa;
Martin Schwidefskyad3bc0a2017-10-12 13:24:45 +020076 union ctlreg2 cr2_old, cr2_new;
Martin Schwidefsky1a36a392015-10-29 10:28:26 +010077 int this_cpu, cpu;
78
79 lgr_info_log();
80 /* Get status of the other CPUs */
81 this_cpu = smp_find_processor_id(stap());
82 for_each_online_cpu(cpu) {
83 if (cpu == this_cpu)
84 continue;
85 if (smp_store_status(cpu))
86 continue;
87 }
88 /* Store status of the boot CPU */
Heiko Carstens402ff5a2021-11-25 19:59:16 +010089 mcesa = __va(S390_lowcore.mcesad & MCESA_ORIGIN_MASK);
Martin Schwidefsky1a36a392015-10-29 10:28:26 +010090 if (MACHINE_HAS_VX)
Martin Schwidefsky916cda12016-01-26 14:10:34 +010091 save_vx_regs((__vector128 *) mcesa->vector_save_area);
92 if (MACHINE_HAS_GS) {
Martin Schwidefskyad3bc0a2017-10-12 13:24:45 +020093 __ctl_store(cr2_old.val, 2, 2);
94 cr2_new = cr2_old;
95 cr2_new.gse = 1;
96 __ctl_load(cr2_new.val, 2, 2);
Martin Schwidefsky916cda12016-01-26 14:10:34 +010097 save_gs_cb((struct gs_cb *) mcesa->guarded_storage_save_area);
Martin Schwidefskyad3bc0a2017-10-12 13:24:45 +020098 __ctl_load(cr2_old.val, 2, 2);
Martin Schwidefsky916cda12016-01-26 14:10:34 +010099 }
Martin Schwidefsky1a36a392015-10-29 10:28:26 +0100100 /*
101 * To create a good backchain for this CPU in the dump store_status
102 * is passed the address of a function. The address is saved into
103 * the PSW save area of the boot CPU and the function is invoked as
104 * a tail call of store_status. The backchain in the dump will look
105 * like this:
106 * restart_int_handler -> __machine_kexec -> __do_machine_kdump
107 * The call to store_status() will not return.
108 */
109 store_status(__do_machine_kdump, image);
Michael Holzheu60a0c682011-10-30 15:16:40 +0100110}
111
Martin Schwidefskyce3dc442017-09-12 16:37:33 +0200112static unsigned long do_start_kdump(unsigned long addr)
Michael Holzheu60a0c682011-10-30 15:16:40 +0100113{
Martin Schwidefskyce3dc442017-09-12 16:37:33 +0200114 struct kimage *image = (struct kimage *) addr;
Michael Holzheu60a0c682011-10-30 15:16:40 +0100115 int (*start_kdump)(int) = (void *)image->start;
116 int rc;
117
118 __arch_local_irq_stnsm(0xfb); /* disable DAT */
119 rc = start_kdump(0);
120 __arch_local_irq_stosm(0x04); /* enable DAT */
Martin Schwidefskyce3dc442017-09-12 16:37:33 +0200121 return rc;
122}
123
Heiko Carstens2980ba62019-06-10 17:22:38 +0200124#endif /* CONFIG_CRASH_DUMP */
125
Martin Schwidefskyce3dc442017-09-12 16:37:33 +0200126/*
127 * Check if kdump checksums are valid: We call purgatory with parameter "0"
128 */
129static bool kdump_csum_valid(struct kimage *image)
130{
131#ifdef CONFIG_CRASH_DUMP
132 int rc;
133
Vasily Gorbik7f28dad2019-11-22 12:19:16 +0100134 preempt_disable();
Heiko Carstens845370f2021-07-05 20:38:26 +0200135 rc = call_on_stack(1, S390_lowcore.nodat_stack, unsigned long, do_start_kdump,
136 unsigned long, (unsigned long)image);
Vasily Gorbik7f28dad2019-11-22 12:19:16 +0100137 preempt_enable();
Philipp Rudo7c3eaaa2017-10-17 12:28:08 +0200138 return rc == 0;
Michael Holzheu60a0c682011-10-30 15:16:40 +0100139#else
Philipp Rudo7c3eaaa2017-10-17 12:28:08 +0200140 return false;
Michael Holzheu60a0c682011-10-30 15:16:40 +0100141#endif
142}
143
Xunlei Pang7a0058e2016-05-23 16:24:22 -0700144#ifdef CONFIG_CRASH_DUMP
145
Heiko Carstens2d0af222016-05-31 09:13:59 +0200146void crash_free_reserved_phys_range(unsigned long begin, unsigned long end)
147{
148 unsigned long addr, size;
149
150 for (addr = begin; addr < end; addr += PAGE_SIZE)
151 free_reserved_page(pfn_to_page(addr >> PAGE_SHIFT));
152 size = begin - crashk_res.start;
153 if (size)
154 os_info_crashkernel_add(crashk_res.start, size);
155 else
156 os_info_crashkernel_add(0, 0);
157}
158
Heiko Carstens4e042af2016-05-31 09:14:00 +0200159static void crash_protect_pages(int protect)
Michael Holzheudab7a7b2011-10-30 15:16:44 +0100160{
Heiko Carstens4e042af2016-05-31 09:14:00 +0200161 unsigned long size;
Michael Holzheudab7a7b2011-10-30 15:16:44 +0100162
Heiko Carstens4e042af2016-05-31 09:14:00 +0200163 if (!crashk_res.end)
164 return;
165 size = resource_size(&crashk_res);
166 if (protect)
167 set_memory_ro(crashk_res.start, size >> PAGE_SHIFT);
Heiko Carstens2d0af222016-05-31 09:13:59 +0200168 else
Heiko Carstens4e042af2016-05-31 09:14:00 +0200169 set_memory_rw(crashk_res.start, size >> PAGE_SHIFT);
Michael Holzheudab7a7b2011-10-30 15:16:44 +0100170}
171
Xunlei Pang7a0058e2016-05-23 16:24:22 -0700172void arch_kexec_protect_crashkres(void)
Michael Holzheudab7a7b2011-10-30 15:16:44 +0100173{
Heiko Carstens4e042af2016-05-31 09:14:00 +0200174 crash_protect_pages(1);
Michael Holzheudab7a7b2011-10-30 15:16:44 +0100175}
176
Xunlei Pang7a0058e2016-05-23 16:24:22 -0700177void arch_kexec_unprotect_crashkres(void)
Michael Holzheudab7a7b2011-10-30 15:16:44 +0100178{
Heiko Carstens4e042af2016-05-31 09:14:00 +0200179 crash_protect_pages(0);
Michael Holzheudab7a7b2011-10-30 15:16:44 +0100180}
181
Xunlei Pang7a0058e2016-05-23 16:24:22 -0700182#endif
183
Michael Holzheudab7a7b2011-10-30 15:16:44 +0100184/*
Michael Holzheu60a0c682011-10-30 15:16:40 +0100185 * Give back memory to hypervisor before new kdump is loaded
186 */
187static int machine_kexec_prepare_kdump(void)
188{
189#ifdef CONFIG_CRASH_DUMP
190 if (MACHINE_IS_VM)
191 diag10_range(PFN_DOWN(crashk_res.start),
192 PFN_DOWN(crashk_res.end - crashk_res.start + 1));
193 return 0;
194#else
195 return -EINVAL;
196#endif
197}
198
Heiko Carstensc6b5b842006-12-04 15:40:33 +0100199int machine_kexec_prepare(struct kimage *image)
Heiko Carstenscf13f0e2005-06-25 14:58:11 -0700200{
Heiko Carstensc6b5b842006-12-04 15:40:33 +0100201 void *reboot_code_buffer;
Heiko Carstenscf13f0e2005-06-25 14:58:11 -0700202
Michael Holzheu60a0c682011-10-30 15:16:40 +0100203 if (image->type == KEXEC_TYPE_CRASH)
204 return machine_kexec_prepare_kdump();
205
Heiko Carstenscf13f0e2005-06-25 14:58:11 -0700206 /* We don't support anything but the default image type for now. */
207 if (image->type != KEXEC_TYPE_DEFAULT)
208 return -EINVAL;
209
210 /* Get the destination where the assembler code should be copied to.*/
Heiko Carstensc6b5b842006-12-04 15:40:33 +0100211 reboot_code_buffer = (void *) page_to_phys(image->control_code_page);
Heiko Carstenscf13f0e2005-06-25 14:58:11 -0700212
213 /* Then copy it */
Heiko Carstensc6b5b842006-12-04 15:40:33 +0100214 memcpy(reboot_code_buffer, relocate_kernel, relocate_kernel_len);
Heiko Carstenscf13f0e2005-06-25 14:58:11 -0700215 return 0;
216}
217
Heiko Carstensc6b5b842006-12-04 15:40:33 +0100218void machine_kexec_cleanup(struct kimage *image)
Heiko Carstenscf13f0e2005-06-25 14:58:11 -0700219{
220}
221
Michael Holzheu60a0c682011-10-30 15:16:40 +0100222void arch_crash_save_vmcoreinfo(void)
223{
224 VMCOREINFO_SYMBOL(lowcore_ptr);
Michael Holzheu7fe7a182011-12-27 11:27:24 +0100225 VMCOREINFO_SYMBOL(high_memory);
Michael Holzheu60a0c682011-10-30 15:16:40 +0100226 VMCOREINFO_LENGTH(lowcore_ptr, NR_CPUS);
Heiko Carstensc78d0c72021-08-04 13:40:31 +0200227 vmcoreinfo_append_str("SAMODE31=%lx\n", __samode31);
228 vmcoreinfo_append_str("EAMODE31=%lx\n", __eamode31);
Gerald Schaeferb2d24b92019-02-03 21:37:20 +0100229 vmcoreinfo_append_str("KERNELOFFSET=%lx\n", kaslr_offset());
Gerald Schaefera9f2f682019-11-19 12:30:53 +0100230 mem_assign_absolute(S390_lowcore.vmcore_info, paddr_vmcoreinfo_note());
Michael Holzheu60a0c682011-10-30 15:16:40 +0100231}
232
Heiko Carstensc6b5b842006-12-04 15:40:33 +0100233void machine_shutdown(void)
Heiko Carstenscf13f0e2005-06-25 14:58:11 -0700234{
Heiko Carstenscf13f0e2005-06-25 14:58:11 -0700235}
236
Heiko Carstens48a8ca02012-08-27 15:50:29 +0200237void machine_crash_shutdown(struct pt_regs *regs)
238{
Vasily Gorbik3b967842018-04-04 14:42:41 +0200239 set_os_info_reipl_block();
Heiko Carstens48a8ca02012-08-27 15:50:29 +0200240}
241
Michael Holzheu60a0c682011-10-30 15:16:40 +0100242/*
243 * Do normal kexec
244 */
245static void __do_machine_kexec(void *data)
Heiko Carstenscf13f0e2005-06-25 14:58:11 -0700246{
Heiko Carstenscf13f0e2005-06-25 14:58:11 -0700247 relocate_kernel_t data_mover;
Heiko Carstens2c2df112010-02-26 22:37:34 +0100248 struct kimage *image = data;
Heiko Carstenscf13f0e2005-06-25 14:58:11 -0700249
Martin Schwidefsky1a36a392015-10-29 10:28:26 +0100250 s390_reset_system();
Heiko Carstensc6b5b842006-12-04 15:40:33 +0100251 data_mover = (relocate_kernel_t) page_to_phys(image->control_code_page);
Heiko Carstenscf13f0e2005-06-25 14:58:11 -0700252
Heiko Carstensd0e810e2017-11-09 23:00:14 +0100253 __arch_local_irq_stnsm(0xfb); /* disable DAT - avoid no-execute */
Heiko Carstenscf13f0e2005-06-25 14:58:11 -0700254 /* Call the moving routine */
Heiko Carstensc6b5b842006-12-04 15:40:33 +0100255 (*data_mover)(&image->head, image->start);
Martin Schwidefsky1a36a392015-10-29 10:28:26 +0100256
257 /* Die if kexec returns */
Martin Schwidefsky98587c22019-04-30 12:33:45 +0200258 disabled_wait();
Heiko Carstenscf13f0e2005-06-25 14:58:11 -0700259}
Heiko Carstens2c2df112010-02-26 22:37:34 +0100260
Michael Holzheu60a0c682011-10-30 15:16:40 +0100261/*
262 * Reset system and call either kdump or normal kexec
263 */
264static void __machine_kexec(void *data)
265{
Michael Holzheu60a0c682011-10-30 15:16:40 +0100266 pfault_fini();
Michael Holzheu3ab121a2012-03-11 11:59:32 -0400267 tracing_off();
268 debug_locks_off();
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100269#ifdef CONFIG_CRASH_DUMP
Martin Schwidefsky1a36a392015-10-29 10:28:26 +0100270 if (((struct kimage *) data)->type == KEXEC_TYPE_CRASH)
271 __machine_kdump(data);
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100272#endif
Martin Schwidefsky1a36a392015-10-29 10:28:26 +0100273 __do_machine_kexec(data);
Michael Holzheu60a0c682011-10-30 15:16:40 +0100274}
275
276/*
277 * Do either kdump or normal kexec. In case of kdump we first ask
278 * purgatory, if kdump checksums are valid.
279 */
Heiko Carstens2c2df112010-02-26 22:37:34 +0100280void machine_kexec(struct kimage *image)
281{
Michael Holzheu60a0c682011-10-30 15:16:40 +0100282 if (image->type == KEXEC_TYPE_CRASH && !kdump_csum_valid(image))
283 return;
Heiko Carstens69667272011-03-15 17:08:33 +0100284 tracer_disable();
Heiko Carstens2c2df112010-02-26 22:37:34 +0100285 smp_send_stop();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400286 smp_call_ipl_cpu(__machine_kexec, image);
Heiko Carstens2c2df112010-02-26 22:37:34 +0100287}