blob: 8f681ccfb83a1d6e21b5b9d707706c4796ca6977 [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,
Michael Holzheu60a0c682011-10-30 15:16:40 +01006 * Michael Holzheu <holzheu@linux.vnet.ibm.com>
Heiko Carstenscf13f0e2005-06-25 14:58:11 -07007 */
8
Heiko Carstenscf13f0e2005-06-25 14:58:11 -07009#include <linux/device.h>
10#include <linux/mm.h>
11#include <linux/kexec.h>
12#include <linux/delay.h>
Heiko Carstens2b67fc42007-02-05 21:16:47 +010013#include <linux/reboot.h>
Heiko Carstens69667272011-03-15 17:08:33 +010014#include <linux/ftrace.h>
Michael Holzheu3ab121a2012-03-11 11:59:32 -040015#include <linux/debug_locks.h>
Heiko Carstens340750c2024-02-05 21:32:13 +010016#include <asm/guarded_storage.h>
Heiko Carstensc28c07f2023-07-24 17:20:26 +020017#include <asm/pfault.h>
Heiko Carstensa386fba2006-02-11 17:56:01 -080018#include <asm/cio.h>
Heiko Carstensfd2527f2024-02-03 11:45:02 +010019#include <asm/fpu.h>
Heiko Carstensa386fba2006-02-11 17:56:01 -080020#include <asm/setup.h>
Heiko Carstensa386fba2006-02-11 17:56:01 -080021#include <asm/smp.h>
Heiko Carstens9c9c1762007-03-05 23:35:45 +010022#include <asm/ipl.h>
Michael Holzheu60a0c682011-10-30 15:16:40 +010023#include <asm/diag.h>
Heiko Carstens6b563d82012-09-14 14:11:32 +020024#include <asm/elf.h>
Michael Holzheu60a0c682011-10-30 15:16:40 +010025#include <asm/asm-offsets.h>
Heiko Carstens4e042af2016-05-31 09:14:00 +020026#include <asm/cacheflush.h>
Alexander Gordeev4df29d22022-07-20 08:22:01 +020027#include <asm/abs_lowcore.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>
Martin Schwidefsky916cda12016-01-26 14:10:34 +010031#include <asm/nmi.h>
Alexander Egorenkov2ba24342021-09-03 11:08:48 +020032#include <asm/sclp.h>
Heiko Carstenscf13f0e2005-06-25 14:58:11 -070033
Alexander Gordeev39218bc2023-04-03 08:44:38 +020034typedef void (*relocate_kernel_t)(unsigned long, unsigned long, unsigned long);
Alexander Gordeev82caf7a2023-04-03 08:44:39 +020035typedef int (*purgatory_t)(int);
Heiko Carstenscf13f0e2005-06-25 14:58:11 -070036
Tobias Klauser2efe55a2006-06-26 18:57:34 +020037extern const unsigned char relocate_kernel[];
38extern const unsigned long long relocate_kernel_len;
Heiko Carstenscf13f0e2005-06-25 14:58:11 -070039
Michael Holzheu60a0c682011-10-30 15:16:40 +010040#ifdef CONFIG_CRASH_DUMP
41
Michael Holzheu60a0c682011-10-30 15:16:40 +010042/*
Martin Schwidefsky1a36a392015-10-29 10:28:26 +010043 * Reset the system, copy boot CPU registers to absolute zero,
44 * and jump to the kdump image
Michael Holzheu60a0c682011-10-30 15:16:40 +010045 */
Alexander Gordeev82caf7a2023-04-03 08:44:39 +020046static void __do_machine_kdump(void *data)
Michael Holzheu60a0c682011-10-30 15:16:40 +010047{
Alexander Gordeev82caf7a2023-04-03 08:44:39 +020048 struct kimage *image = data;
49 purgatory_t purgatory;
Martin Schwidefsky1a36a392015-10-29 10:28:26 +010050 unsigned long prefix;
51
Alexander Gordeev82caf7a2023-04-03 08:44:39 +020052 purgatory = (purgatory_t)image->start;
53
Martin Schwidefsky1a36a392015-10-29 10:28:26 +010054 /* store_status() saved the prefix register to lowcore */
Sven Schnelle208da1d2024-06-10 13:45:25 +020055 prefix = (unsigned long)get_lowcore()->prefixreg_save_area;
Martin Schwidefsky1a36a392015-10-29 10:28:26 +010056
57 /* Now do the reset */
58 s390_reset_system();
59
60 /*
61 * Copy dump CPU store status info to absolute zero.
62 * This need to be done *after* s390_reset_system set the
63 * prefix register of this CPU to zero
64 */
Sven Schnelle97cee3d2024-07-22 15:41:27 +020065 memcpy(absolute_pointer(get_lowcore()->floating_pt_save_area),
Alexander Gordeev39218bc2023-04-03 08:44:38 +020066 phys_to_virt(prefix + __LC_FPREGS_SAVE_AREA), 512);
Michael Holzheu60a0c682011-10-30 15:16:40 +010067
Alexander Gordeev82caf7a2023-04-03 08:44:39 +020068 call_nodat(1, int, purgatory, int, 1);
Martin Schwidefsky1a36a392015-10-29 10:28:26 +010069
Alexander Gordeev82caf7a2023-04-03 08:44:39 +020070 /* Die if kdump returns */
Martin Schwidefsky98587c22019-04-30 12:33:45 +020071 disabled_wait();
Martin Schwidefsky1a36a392015-10-29 10:28:26 +010072}
73
74/*
75 * Start kdump: create a LGR log entry, store status of all CPUs and
76 * branch to __do_machine_kdump.
77 */
78static noinline void __machine_kdump(void *image)
79{
Martin Schwidefsky916cda12016-01-26 14:10:34 +010080 struct mcesa *mcesa;
Martin Schwidefskyad3bc0a2017-10-12 13:24:45 +020081 union ctlreg2 cr2_old, cr2_new;
Martin Schwidefsky1a36a392015-10-29 10:28:26 +010082 int this_cpu, cpu;
83
84 lgr_info_log();
85 /* Get status of the other CPUs */
86 this_cpu = smp_find_processor_id(stap());
87 for_each_online_cpu(cpu) {
88 if (cpu == this_cpu)
89 continue;
90 if (smp_store_status(cpu))
91 continue;
92 }
93 /* Store status of the boot CPU */
Sven Schnelle208da1d2024-06-10 13:45:25 +020094 mcesa = __va(get_lowcore()->mcesad & MCESA_ORIGIN_MASK);
Heiko Carstens18564752023-12-01 15:42:19 +010095 if (cpu_has_vx())
Martin Schwidefsky916cda12016-01-26 14:10:34 +010096 save_vx_regs((__vector128 *) mcesa->vector_save_area);
97 if (MACHINE_HAS_GS) {
Heiko Carstens527618a2023-09-11 21:40:04 +020098 local_ctl_store(2, &cr2_old.reg);
Martin Schwidefskyad3bc0a2017-10-12 13:24:45 +020099 cr2_new = cr2_old;
100 cr2_new.gse = 1;
Heiko Carstens527618a2023-09-11 21:40:04 +0200101 local_ctl_load(2, &cr2_new.reg);
Martin Schwidefsky916cda12016-01-26 14:10:34 +0100102 save_gs_cb((struct gs_cb *) mcesa->guarded_storage_save_area);
Heiko Carstens527618a2023-09-11 21:40:04 +0200103 local_ctl_load(2, &cr2_old.reg);
Martin Schwidefsky916cda12016-01-26 14:10:34 +0100104 }
Martin Schwidefsky1a36a392015-10-29 10:28:26 +0100105 /*
106 * To create a good backchain for this CPU in the dump store_status
107 * is passed the address of a function. The address is saved into
108 * the PSW save area of the boot CPU and the function is invoked as
109 * a tail call of store_status. The backchain in the dump will look
110 * like this:
111 * restart_int_handler -> __machine_kexec -> __do_machine_kdump
112 * The call to store_status() will not return.
113 */
114 store_status(__do_machine_kdump, image);
Michael Holzheu60a0c682011-10-30 15:16:40 +0100115}
116
Heiko Carstens2980ba62019-06-10 17:22:38 +0200117#endif /* CONFIG_CRASH_DUMP */
118
Martin Schwidefskyce3dc442017-09-12 16:37:33 +0200119/*
120 * Check if kdump checksums are valid: We call purgatory with parameter "0"
121 */
122static bool kdump_csum_valid(struct kimage *image)
123{
124#ifdef CONFIG_CRASH_DUMP
Alexander Gordeev2d1b21e2023-04-03 08:44:40 +0200125 purgatory_t purgatory = (purgatory_t)image->start;
Martin Schwidefskyce3dc442017-09-12 16:37:33 +0200126 int rc;
127
Alexander Gordeev2d1b21e2023-04-03 08:44:40 +0200128 rc = call_nodat(1, int, purgatory, int, 0);
Philipp Rudo7c3eaaa2017-10-17 12:28:08 +0200129 return rc == 0;
Michael Holzheu60a0c682011-10-30 15:16:40 +0100130#else
Philipp Rudo7c3eaaa2017-10-17 12:28:08 +0200131 return false;
Michael Holzheu60a0c682011-10-30 15:16:40 +0100132#endif
133}
134
Xunlei Pang7a0058e2016-05-23 16:24:22 -0700135#ifdef CONFIG_CRASH_DUMP
136
Heiko Carstens2d0af222016-05-31 09:13:59 +0200137void crash_free_reserved_phys_range(unsigned long begin, unsigned long end)
138{
139 unsigned long addr, size;
140
141 for (addr = begin; addr < end; addr += PAGE_SIZE)
142 free_reserved_page(pfn_to_page(addr >> PAGE_SHIFT));
143 size = begin - crashk_res.start;
144 if (size)
145 os_info_crashkernel_add(crashk_res.start, size);
146 else
147 os_info_crashkernel_add(0, 0);
148}
149
Heiko Carstens4e042af2016-05-31 09:14:00 +0200150static void crash_protect_pages(int protect)
Michael Holzheudab7a7b2011-10-30 15:16:44 +0100151{
Heiko Carstens4e042af2016-05-31 09:14:00 +0200152 unsigned long size;
Michael Holzheudab7a7b2011-10-30 15:16:44 +0100153
Heiko Carstens4e042af2016-05-31 09:14:00 +0200154 if (!crashk_res.end)
155 return;
156 size = resource_size(&crashk_res);
157 if (protect)
158 set_memory_ro(crashk_res.start, size >> PAGE_SHIFT);
Heiko Carstens2d0af222016-05-31 09:13:59 +0200159 else
Heiko Carstens4e042af2016-05-31 09:14:00 +0200160 set_memory_rw(crashk_res.start, size >> PAGE_SHIFT);
Michael Holzheudab7a7b2011-10-30 15:16:44 +0100161}
162
Xunlei Pang7a0058e2016-05-23 16:24:22 -0700163void arch_kexec_protect_crashkres(void)
Michael Holzheudab7a7b2011-10-30 15:16:44 +0100164{
Heiko Carstens4e042af2016-05-31 09:14:00 +0200165 crash_protect_pages(1);
Michael Holzheudab7a7b2011-10-30 15:16:44 +0100166}
167
Xunlei Pang7a0058e2016-05-23 16:24:22 -0700168void arch_kexec_unprotect_crashkres(void)
Michael Holzheudab7a7b2011-10-30 15:16:44 +0100169{
Heiko Carstens4e042af2016-05-31 09:14:00 +0200170 crash_protect_pages(0);
Michael Holzheudab7a7b2011-10-30 15:16:44 +0100171}
172
Xunlei Pang7a0058e2016-05-23 16:24:22 -0700173#endif
174
Michael Holzheudab7a7b2011-10-30 15:16:44 +0100175/*
Michael Holzheu60a0c682011-10-30 15:16:40 +0100176 * Give back memory to hypervisor before new kdump is loaded
177 */
178static int machine_kexec_prepare_kdump(void)
179{
180#ifdef CONFIG_CRASH_DUMP
181 if (MACHINE_IS_VM)
182 diag10_range(PFN_DOWN(crashk_res.start),
183 PFN_DOWN(crashk_res.end - crashk_res.start + 1));
184 return 0;
185#else
186 return -EINVAL;
187#endif
188}
189
Heiko Carstensc6b5b842006-12-04 15:40:33 +0100190int machine_kexec_prepare(struct kimage *image)
Heiko Carstenscf13f0e2005-06-25 14:58:11 -0700191{
Heiko Carstensc6b5b842006-12-04 15:40:33 +0100192 void *reboot_code_buffer;
Heiko Carstenscf13f0e2005-06-25 14:58:11 -0700193
Michael Holzheu60a0c682011-10-30 15:16:40 +0100194 if (image->type == KEXEC_TYPE_CRASH)
195 return machine_kexec_prepare_kdump();
196
Heiko Carstenscf13f0e2005-06-25 14:58:11 -0700197 /* We don't support anything but the default image type for now. */
198 if (image->type != KEXEC_TYPE_DEFAULT)
199 return -EINVAL;
200
201 /* Get the destination where the assembler code should be copied to.*/
Alexander Gordeev39218bc2023-04-03 08:44:38 +0200202 reboot_code_buffer = page_to_virt(image->control_code_page);
Heiko Carstenscf13f0e2005-06-25 14:58:11 -0700203
204 /* Then copy it */
Heiko Carstensc6b5b842006-12-04 15:40:33 +0100205 memcpy(reboot_code_buffer, relocate_kernel, relocate_kernel_len);
Heiko Carstenscf13f0e2005-06-25 14:58:11 -0700206 return 0;
207}
208
Heiko Carstensc6b5b842006-12-04 15:40:33 +0100209void machine_kexec_cleanup(struct kimage *image)
Heiko Carstenscf13f0e2005-06-25 14:58:11 -0700210{
211}
212
Heiko Carstensc6b5b842006-12-04 15:40:33 +0100213void machine_shutdown(void)
Heiko Carstenscf13f0e2005-06-25 14:58:11 -0700214{
Heiko Carstenscf13f0e2005-06-25 14:58:11 -0700215}
216
Heiko Carstens48a8ca02012-08-27 15:50:29 +0200217void machine_crash_shutdown(struct pt_regs *regs)
218{
Vasily Gorbik3b967842018-04-04 14:42:41 +0200219 set_os_info_reipl_block();
Heiko Carstens48a8ca02012-08-27 15:50:29 +0200220}
221
Michael Holzheu60a0c682011-10-30 15:16:40 +0100222/*
223 * Do normal kexec
224 */
225static void __do_machine_kexec(void *data)
Heiko Carstenscf13f0e2005-06-25 14:58:11 -0700226{
Alexander Gordeev39218bc2023-04-03 08:44:38 +0200227 unsigned long data_mover, entry, diag308_subcode;
Heiko Carstens2c2df112010-02-26 22:37:34 +0100228 struct kimage *image = data;
Heiko Carstenscf13f0e2005-06-25 14:58:11 -0700229
Alexander Gordeev39218bc2023-04-03 08:44:38 +0200230 data_mover = page_to_phys(image->control_code_page);
231 entry = virt_to_phys(&image->head);
Alexander Egorenkov2ba24342021-09-03 11:08:48 +0200232 diag308_subcode = DIAG308_CLEAR_RESET;
233 if (sclp.has_iplcc)
234 diag308_subcode |= DIAG308_FLAG_EI;
Alexander Gordeev7a04d492023-04-03 08:44:36 +0200235 s390_reset_system();
236
Alexander Gordeev82caf7a2023-04-03 08:44:39 +0200237 call_nodat(3, void, (relocate_kernel_t)data_mover,
238 unsigned long, entry,
239 unsigned long, image->start,
240 unsigned long, diag308_subcode);
Martin Schwidefsky1a36a392015-10-29 10:28:26 +0100241
242 /* Die if kexec returns */
Martin Schwidefsky98587c22019-04-30 12:33:45 +0200243 disabled_wait();
Heiko Carstenscf13f0e2005-06-25 14:58:11 -0700244}
Heiko Carstens2c2df112010-02-26 22:37:34 +0100245
Michael Holzheu60a0c682011-10-30 15:16:40 +0100246/*
247 * Reset system and call either kdump or normal kexec
248 */
249static void __machine_kexec(void *data)
250{
Michael Holzheu60a0c682011-10-30 15:16:40 +0100251 pfault_fini();
Michael Holzheu3ab121a2012-03-11 11:59:32 -0400252 tracing_off();
253 debug_locks_off();
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100254#ifdef CONFIG_CRASH_DUMP
Martin Schwidefsky1a36a392015-10-29 10:28:26 +0100255 if (((struct kimage *) data)->type == KEXEC_TYPE_CRASH)
256 __machine_kdump(data);
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100257#endif
Martin Schwidefsky1a36a392015-10-29 10:28:26 +0100258 __do_machine_kexec(data);
Michael Holzheu60a0c682011-10-30 15:16:40 +0100259}
260
261/*
262 * Do either kdump or normal kexec. In case of kdump we first ask
263 * purgatory, if kdump checksums are valid.
264 */
Heiko Carstens2c2df112010-02-26 22:37:34 +0100265void machine_kexec(struct kimage *image)
266{
Michael Holzheu60a0c682011-10-30 15:16:40 +0100267 if (image->type == KEXEC_TYPE_CRASH && !kdump_csum_valid(image))
268 return;
Heiko Carstens69667272011-03-15 17:08:33 +0100269 tracer_disable();
Heiko Carstens2c2df112010-02-26 22:37:34 +0100270 smp_send_stop();
Martin Schwidefsky8b646bd2012-03-11 11:59:26 -0400271 smp_call_ipl_cpu(__machine_kexec, image);
Heiko Carstens2c2df112010-02-26 22:37:34 +0100272}