xref: /openbmc/linux/arch/ia64/kernel/crash.c (revision 03860ef0)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * arch/ia64/kernel/crash.c
4  *
5  * Architecture specific (ia64) functions for kexec based crash dumps.
6  *
7  * Created by: Khalid Aziz <khalid.aziz@hp.com>
8  * Copyright (C) 2005 Hewlett-Packard Development Company, L.P.
9  * Copyright (C) 2005 Intel Corp	Zou Nan hai <nanhai.zou@intel.com>
10  *
11  */
12 #include <linux/smp.h>
13 #include <linux/delay.h>
14 #include <linux/crash_dump.h>
15 #include <linux/memblock.h>
16 #include <linux/kexec.h>
17 #include <linux/elfcore.h>
18 #include <linux/reboot.h>
19 #include <linux/sysctl.h>
20 #include <linux/init.h>
21 #include <linux/kdebug.h>
22 
23 #include <asm/mca.h>
24 
25 int kdump_status[NR_CPUS];
26 static atomic_t kdump_cpu_frozen;
27 atomic_t kdump_in_progress;
28 static int kdump_freeze_monarch;
29 static int kdump_on_init = 1;
30 static int kdump_on_fatal_mca = 1;
31 
32 extern void ia64_dump_cpu_regs(void *);
33 
34 static DEFINE_PER_CPU(struct elf_prstatus, elf_prstatus);
35 
36 void
crash_save_this_cpu(void)37 crash_save_this_cpu(void)
38 {
39 	void *buf;
40 	unsigned long cfm, sof, sol;
41 
42 	int cpu = smp_processor_id();
43 	struct elf_prstatus *prstatus = &per_cpu(elf_prstatus, cpu);
44 
45 	elf_greg_t *dst = (elf_greg_t *)&(prstatus->pr_reg);
46 	memset(prstatus, 0, sizeof(*prstatus));
47 	prstatus->common.pr_pid = current->pid;
48 
49 	ia64_dump_cpu_regs(dst);
50 	cfm = dst[43];
51 	sol = (cfm >> 7) & 0x7f;
52 	sof = cfm & 0x7f;
53 	dst[46] = (unsigned long)ia64_rse_skip_regs((unsigned long *)dst[46],
54 			sof - sol);
55 
56 	buf = (u64 *) per_cpu_ptr(crash_notes, cpu);
57 	if (!buf)
58 		return;
59 	buf = append_elf_note(buf, KEXEC_CORE_NOTE_NAME, NT_PRSTATUS, prstatus,
60 			sizeof(*prstatus));
61 	final_note(buf);
62 }
63 
64 #ifdef CONFIG_SMP
65 static int
kdump_wait_cpu_freeze(void)66 kdump_wait_cpu_freeze(void)
67 {
68 	int cpu_num = num_online_cpus() - 1;
69 	int timeout = 1000;
70 	while(timeout-- > 0) {
71 		if (atomic_read(&kdump_cpu_frozen) == cpu_num)
72 			return 0;
73 		udelay(1000);
74 	}
75 	return 1;
76 }
77 #endif
78 
79 void
machine_crash_shutdown(struct pt_regs * pt)80 machine_crash_shutdown(struct pt_regs *pt)
81 {
82 	/* This function is only called after the system
83 	 * has paniced or is otherwise in a critical state.
84 	 * The minimum amount of code to allow a kexec'd kernel
85 	 * to run successfully needs to happen here.
86 	 *
87 	 * In practice this means shooting down the other cpus in
88 	 * an SMP system.
89 	 */
90 	kexec_disable_iosapic();
91 #ifdef CONFIG_SMP
92 	/*
93 	 * If kdump_on_init is set and an INIT is asserted here, kdump will
94 	 * be started again via INIT monarch.
95 	 */
96 	local_irq_disable();
97 	ia64_set_psr_mc();	/* mask MCA/INIT */
98 	if (atomic_inc_return(&kdump_in_progress) != 1)
99 		unw_init_running(kdump_cpu_freeze, NULL);
100 
101 	/*
102 	 * Now this cpu is ready for kdump.
103 	 * Stop all others by IPI or INIT.  They could receive INIT from
104 	 * outside and might be INIT monarch, but only thing they have to
105 	 * do is falling into kdump_cpu_freeze().
106 	 *
107 	 * If an INIT is asserted here:
108 	 * - All receivers might be slaves, since some of cpus could already
109 	 *   be frozen and INIT might be masked on monarch.  In this case,
110 	 *   all slaves will be frozen soon since kdump_in_progress will let
111 	 *   them into DIE_INIT_SLAVE_LEAVE.
112 	 * - One might be a monarch, but INIT rendezvous will fail since
113 	 *   at least this cpu already have INIT masked so it never join
114 	 *   to the rendezvous.  In this case, all slaves and monarch will
115 	 *   be frozen soon with no wait since the INIT rendezvous is skipped
116 	 *   by kdump_in_progress.
117 	 */
118 	kdump_smp_send_stop();
119 	/* not all cpu response to IPI, send INIT to freeze them */
120 	if (kdump_wait_cpu_freeze()) {
121 		kdump_smp_send_init();
122 		/* wait again, don't go ahead if possible */
123 		kdump_wait_cpu_freeze();
124 	}
125 #endif
126 }
127 
128 static void
machine_kdump_on_init(void)129 machine_kdump_on_init(void)
130 {
131 	crash_save_vmcoreinfo();
132 	local_irq_disable();
133 	kexec_disable_iosapic();
134 	machine_kexec(ia64_kimage);
135 }
136 
137 void
kdump_cpu_freeze(struct unw_frame_info * info,void * arg)138 kdump_cpu_freeze(struct unw_frame_info *info, void *arg)
139 {
140 	int cpuid;
141 
142 	local_irq_disable();
143 	cpuid = smp_processor_id();
144 	crash_save_this_cpu();
145 	current->thread.ksp = (__u64)info->sw - 16;
146 
147 	ia64_set_psr_mc();	/* mask MCA/INIT and stop reentrance */
148 
149 	atomic_inc(&kdump_cpu_frozen);
150 	kdump_status[cpuid] = 1;
151 	mb();
152 	for (;;)
153 		cpu_relax();
154 }
155 
156 static int
kdump_init_notifier(struct notifier_block * self,unsigned long val,void * data)157 kdump_init_notifier(struct notifier_block *self, unsigned long val, void *data)
158 {
159 	struct ia64_mca_notify_die *nd;
160 	struct die_args *args = data;
161 
162 	if (atomic_read(&kdump_in_progress)) {
163 		switch (val) {
164 		case DIE_INIT_MONARCH_LEAVE:
165 			if (!kdump_freeze_monarch)
166 				break;
167 			fallthrough;
168 		case DIE_INIT_SLAVE_LEAVE:
169 		case DIE_INIT_MONARCH_ENTER:
170 		case DIE_MCA_RENDZVOUS_LEAVE:
171 			unw_init_running(kdump_cpu_freeze, NULL);
172 			break;
173 		}
174 	}
175 
176 	if (!kdump_on_init && !kdump_on_fatal_mca)
177 		return NOTIFY_DONE;
178 
179 	if (!ia64_kimage) {
180 		if (val == DIE_INIT_MONARCH_LEAVE)
181 			ia64_mca_printk(KERN_NOTICE
182 					"%s: kdump not configured\n",
183 					__func__);
184 		return NOTIFY_DONE;
185 	}
186 
187 	if (val != DIE_INIT_MONARCH_LEAVE &&
188 	    val != DIE_INIT_MONARCH_PROCESS &&
189 	    val != DIE_MCA_MONARCH_LEAVE)
190 		return NOTIFY_DONE;
191 
192 	nd = (struct ia64_mca_notify_die *)args->err;
193 
194 	switch (val) {
195 	case DIE_INIT_MONARCH_PROCESS:
196 		/* Reason code 1 means machine check rendezvous*/
197 		if (kdump_on_init && (nd->sos->rv_rc != 1)) {
198 			if (atomic_inc_return(&kdump_in_progress) != 1)
199 				kdump_freeze_monarch = 1;
200 		}
201 		break;
202 	case DIE_INIT_MONARCH_LEAVE:
203 		/* Reason code 1 means machine check rendezvous*/
204 		if (kdump_on_init && (nd->sos->rv_rc != 1))
205 			machine_kdump_on_init();
206 		break;
207 	case DIE_MCA_MONARCH_LEAVE:
208 		/* *(nd->data) indicate if MCA is recoverable */
209 		if (kdump_on_fatal_mca && !(*(nd->data))) {
210 			if (atomic_inc_return(&kdump_in_progress) == 1)
211 				machine_kdump_on_init();
212 			/* We got fatal MCA while kdump!? No way!! */
213 		}
214 		break;
215 	}
216 	return NOTIFY_DONE;
217 }
218 
219 #ifdef CONFIG_SYSCTL
220 static struct ctl_table kdump_ctl_table[] = {
221 	{
222 		.procname = "kdump_on_init",
223 		.data = &kdump_on_init,
224 		.maxlen = sizeof(int),
225 		.mode = 0644,
226 		.proc_handler = proc_dointvec,
227 	},
228 	{
229 		.procname = "kdump_on_fatal_mca",
230 		.data = &kdump_on_fatal_mca,
231 		.maxlen = sizeof(int),
232 		.mode = 0644,
233 		.proc_handler = proc_dointvec,
234 	},
235 	{ }
236 };
237 #endif
238 
239 static int
machine_crash_setup(void)240 machine_crash_setup(void)
241 {
242 	/* be notified before default_monarch_init_process */
243 	static struct notifier_block kdump_init_notifier_nb = {
244 		.notifier_call = kdump_init_notifier,
245 		.priority = 1,
246 	};
247 	int ret;
248 	if((ret = register_die_notifier(&kdump_init_notifier_nb)) != 0)
249 		return ret;
250 #ifdef CONFIG_SYSCTL
251 	register_sysctl("kernel", kdump_ctl_table);
252 #endif
253 	return 0;
254 }
255 
256 __initcall(machine_crash_setup);
257 
258