xref: /illumos-gate/usr/src/uts/common/os/panic.c (revision 3114379f81d5ab88054ea9e72c8874984ea8c263)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
23  */
24 
25 /*
26  * Copyright (c) 2011, Joyent, Inc. All rights reserved.
27  * Copyright (c) 2016 by Delphix. All rights reserved.
28  */
29 
30 /*
31  * When the operating system detects that it is in an invalid state, a panic
32  * is initiated in order to minimize potential damage to user data and to
33  * facilitate debugging.  There are three major tasks to be performed in
34  * a system panic: recording information about the panic in memory (and thus
35  * making it part of the crash dump), synchronizing the file systems to
36  * preserve user file data, and generating the crash dump.  We define the
37  * system to be in one of four states with respect to the panic code:
38  *
39  * CALM    - the state of the system prior to any thread initiating a panic
40  *
41  * QUIESCE - the state of the system when the first thread to initiate
42  *           a system panic records information about the cause of the panic
43  *           and renders the system quiescent by stopping other processors
44  *
45  * SYNC    - the state of the system when we synchronize the file systems
46  * DUMP    - the state when we generate the crash dump.
47  *
48  * The transitions between these states are irreversible: once we begin
49  * panicking, we only make one attempt to perform the actions associated with
50  * each state.
51  *
52  * The panic code itself must be re-entrant because actions taken during any
53  * state may lead to another system panic.  Additionally, any Solaris
54  * thread may initiate a panic at any time, and so we must have synchronization
55  * between threads which attempt to initiate a state transition simultaneously.
56  * The panic code makes use of a special locking primitive, a trigger, to
57  * perform this synchronization.  A trigger is simply a word which is set
58  * atomically and can only be set once.  We declare three triggers, one for
59  * each transition between the four states.  When a thread enters the panic
60  * code it attempts to set each trigger; if it fails it moves on to the
61  * next trigger.  A special case is the first trigger: if two threads race
62  * to perform the transition to QUIESCE, the losing thread may execute before
63  * the winner has a chance to stop its CPU.  To solve this problem, we have
64  * the loser look ahead to see if any other triggers are set; if not, it
65  * presumes a panic is underway and simply spins.  Unfortunately, since we
66  * are panicking, it is not possible to know this with absolute certainty.
67  *
68  * There are two common reasons for re-entering the panic code once a panic
69  * has been initiated: (1) after we debug_enter() at the end of QUIESCE,
70  * the operator may type "sync" instead of "go", and the PROM's sync callback
71  * routine will invoke panic(); (2) if the clock routine decides that sync
72  * or dump is not making progress, it will invoke panic() to force a timeout.
73  * The design assumes that a third possibility, another thread causing an
74  * unrelated panic while sync or dump is still underway, is extremely unlikely.
75  * If this situation occurs, we may end up triggering dump while sync is
76  * still in progress.  This third case is considered extremely unlikely because
77  * all other CPUs are stopped and low-level interrupts have been blocked.
78  *
79  * The panic code is entered via a call directly to the vpanic() function,
80  * or its varargs wrappers panic() and cmn_err(9F).  The vpanic routine
81  * is implemented in assembly language to record the current machine
82  * registers, attempt to set the trigger for the QUIESCE state, and
83  * if successful, switch stacks on to the panic_stack before calling into
84  * the common panicsys() routine.  The first thread to initiate a panic
85  * is allowed to make use of the reserved panic_stack so that executing
86  * the panic code itself does not overwrite valuable data on that thread's
87  * stack *ahead* of the current stack pointer.  This data will be preserved
88  * in the crash dump and may prove invaluable in determining what this
89  * thread has previously been doing.  The first thread, saved in panic_thread,
90  * is also responsible for stopping the other CPUs as quickly as possible,
91  * and then setting the various panic_* variables.  Most important among
92  * these is panicstr, which allows threads to subsequently bypass held
93  * locks so that we can proceed without ever blocking.  We must stop the
94  * other CPUs *prior* to setting panicstr in case threads running there are
95  * currently spinning to acquire a lock; we want that state to be preserved.
96  * Every thread which initiates a panic has its T_PANIC flag set so we can
97  * identify all such threads in the crash dump.
98  *
99  * The panic_thread is also allowed to make use of the special memory buffer
100  * panicbuf, which on machines with appropriate hardware is preserved across
101  * reboots.  We allow the panic_thread to store its register set and panic
102  * message in this buffer, so even if we fail to obtain a crash dump we will
103  * be able to examine the machine after reboot and determine some of the
104  * state at the time of the panic.  If we do get a dump, the panic buffer
105  * data is structured so that a debugger can easily consume the information
106  * therein (see <sys/panic.h>).
107  *
108  * Each platform or architecture is required to implement the functions
109  * panic_savetrap() to record trap-specific information to panicbuf,
110  * panic_saveregs() to record a register set to panicbuf, panic_stopcpus()
111  * to halt all CPUs but the panicking CPU, panic_quiesce_hw() to perform
112  * miscellaneous platform-specific tasks *after* panicstr is set,
113  * panic_showtrap() to print trap-specific information to the console,
114  * and panic_dump_hw() to perform platform tasks prior to calling dumpsys().
115  *
116  * A Note on Word Formation, courtesy of the Oxford Guide to English Usage:
117  *
118  * Words ending in -c interpose k before suffixes which otherwise would
119  * indicate a soft c, and thus the verb and adjective forms of 'panic' are
120  * spelled "panicked", "panicking", and "panicky" respectively.  Use of
121  * the ill-conceived "panicing" and "panic'd" is discouraged.
122  */
123 
124 #include <sys/types.h>
125 #include <sys/varargs.h>
126 #include <sys/sysmacros.h>
127 #include <sys/cmn_err.h>
128 #include <sys/cpuvar.h>
129 #include <sys/thread.h>
130 #include <sys/t_lock.h>
131 #include <sys/cred.h>
132 #include <sys/systm.h>
133 #include <sys/archsystm.h>
134 #include <sys/uadmin.h>
135 #include <sys/callb.h>
136 #include <sys/vfs.h>
137 #include <sys/log.h>
138 #include <sys/disp.h>
139 #include <sys/param.h>
140 #include <sys/dumphdr.h>
141 #include <sys/ftrace.h>
142 #include <sys/reboot.h>
143 #include <sys/debug.h>
144 #include <sys/stack.h>
145 #include <sys/spl.h>
146 #include <sys/errorq.h>
147 #include <sys/panic.h>
148 #include <sys/fm/util.h>
149 #include <sys/clock_impl.h>
150 #include <sys/sunddi.h>
151 
152 /*
153  * Panic variables which are set once during the QUIESCE state by the
154  * first thread to initiate a panic.  These are examined by post-mortem
155  * debugging tools; the inconsistent use of 'panic' versus 'panic_' in
156  * the variable naming is historical and allows legacy tools to work.
157  */
158 #pragma align STACK_ALIGN(panic_stack)
159 char panic_stack[PANICSTKSIZE];		/* reserved stack for panic_thread */
160 kthread_t *panic_thread;		/* first thread to call panicsys() */
161 cpu_t panic_cpu;			/* cpu from first call to panicsys() */
162 label_t panic_regs;			/* setjmp label from panic_thread */
163 label_t panic_pcb;			/* t_pcb at time of panic */
164 struct regs *panic_reg;			/* regs struct from first panicsys() */
165 char *volatile panicstr;		/* format string to first panicsys() */
166 va_list panicargs;			/* arguments to first panicsys() */
167 clock_t panic_lbolt;			/* lbolt at time of panic */
168 int64_t panic_lbolt64;			/* lbolt64 at time of panic */
169 hrtime_t panic_hrtime;			/* hrtime at time of panic */
170 timespec_t panic_hrestime;		/* hrestime at time of panic */
171 int panic_ipl;				/* ipl on panic_cpu at time of panic */
172 ushort_t panic_schedflag;		/* t_schedflag for panic_thread */
173 cpu_t *panic_bound_cpu;			/* t_bound_cpu for panic_thread */
174 char panic_preempt;			/* t_preempt for panic_thread */
175 dev_info_t *panic_dip;			/* from dev_err(dip, CE_PANIC, ...) */
176 
177 /*
178  * Panic variables which can be set via /etc/system or patched while
179  * the system is in operation.  Again, the stupid names are historic.
180  */
181 char *panic_bootstr = NULL;		/* mdboot string to use after panic */
182 int panic_bootfcn = AD_BOOT;		/* mdboot function to use after panic */
183 int halt_on_panic = 0;			/* halt after dump instead of reboot? */
184 int nopanicdebug = 0;			/* reboot instead of call debugger? */
185 int in_sync = 0;			/* skip vfs_syncall() and just dump? */
186 
187 /*
188  * The do_polled_io flag is set by the panic code to inform the SCSI subsystem
189  * to use polled mode instead of interrupt-driven i/o.
190  */
191 int do_polled_io = 0;
192 
193 /*
194  * The panic_forced flag is set by the uadmin A_DUMP code to inform the
195  * panic subsystem that it should not attempt an initial debug_enter.
196  */
197 int panic_forced = 0;
198 
199 /*
200  * Triggers for panic state transitions:
201  */
202 int panic_quiesce;			/* trigger for CALM    -> QUIESCE */
203 int panic_dump;				/* trigger for QUIESCE    -> DUMP */
204 
205 /*
206  * Variable signifying quiesce(9E) is in progress.
207  */
208 volatile int quiesce_active = 0;
209 
210 void
211 panicsys(const char *format, va_list alist, struct regs *rp, int on_panic_stack)
212 {
213 	int s = spl8();
214 	kthread_t *t = curthread;
215 	cpu_t *cp = CPU;
216 
217 	caddr_t intr_stack = NULL;
218 	volatile uint_t intr_actv;
219 
220 	ushort_t schedflag = t->t_schedflag;
221 	cpu_t *bound_cpu = t->t_bound_cpu;
222 	char preempt = t->t_preempt;
223 	label_t pcb = t->t_pcb;
224 
225 	(void) setjmp(&t->t_pcb);
226 	t->t_flag |= T_PANIC;
227 
228 	t->t_schedflag |= TS_DONT_SWAP;
229 	t->t_bound_cpu = cp;
230 	t->t_preempt++;
231 
232 	panic_enter_hw(s);
233 
234 	/*
235 	 * If we're on the interrupt stack and an interrupt thread is available
236 	 * in this CPU's pool, preserve the interrupt stack by detaching an
237 	 * interrupt thread and making its stack the intr_stack.
238 	 */
239 	if (CPU_ON_INTR(cp) && cp->cpu_intr_thread != NULL) {
240 		kthread_t *it = cp->cpu_intr_thread;
241 
242 		intr_stack = cp->cpu_intr_stack;
243 		intr_actv = cp->cpu_intr_actv;
244 
245 		cp->cpu_intr_stack = thread_stk_init(it->t_stk);
246 		cp->cpu_intr_thread = it->t_link;
247 
248 		/*
249 		 * Clear only the high level bits of cpu_intr_actv.
250 		 * We want to indicate that high-level interrupts are
251 		 * not active without destroying the low-level interrupt
252 		 * information stored there.
253 		 */
254 		cp->cpu_intr_actv &= ((1 << (LOCK_LEVEL + 1)) - 1);
255 	}
256 
257 	/*
258 	 * Record one-time panic information and quiesce the other CPUs.
259 	 * Then print out the panic message and stack trace.
260 	 */
261 	if (on_panic_stack) {
262 		panic_data_t *pdp = (panic_data_t *)panicbuf;
263 
264 		pdp->pd_version = PANICBUFVERS;
265 		pdp->pd_msgoff = sizeof (panic_data_t) - sizeof (panic_nv_t);
266 
267 		(void) strncpy(pdp->pd_uuid, dump_get_uuid(),
268 		    sizeof (pdp->pd_uuid));
269 
270 		if (t->t_panic_trap != NULL)
271 			panic_savetrap(pdp, t->t_panic_trap);
272 		else
273 			panic_saveregs(pdp, rp);
274 
275 		(void) vsnprintf(&panicbuf[pdp->pd_msgoff],
276 		    PANICBUFSIZE - pdp->pd_msgoff, format, alist);
277 
278 		/*
279 		 * Call into the platform code to stop the other CPUs.
280 		 * We currently have all interrupts blocked, and expect that
281 		 * the platform code will lower ipl only as far as needed to
282 		 * perform cross-calls, and will acquire as *few* locks as is
283 		 * possible -- panicstr is not set so we can still deadlock.
284 		 */
285 		panic_stopcpus(cp, t, s);
286 
287 		panicstr = (char *)format;
288 		va_copy(panicargs, alist);
289 		panic_lbolt = LBOLT_NO_ACCOUNT;
290 		panic_lbolt64 = LBOLT_NO_ACCOUNT64;
291 		panic_hrestime = hrestime;
292 		panic_hrtime = gethrtime_waitfree();
293 		panic_thread = t;
294 		panic_regs = t->t_pcb;
295 		panic_reg = rp;
296 		panic_cpu = *cp;
297 		panic_ipl = spltoipl(s);
298 		panic_schedflag = schedflag;
299 		panic_bound_cpu = bound_cpu;
300 		panic_preempt = preempt;
301 		panic_pcb = pcb;
302 
303 		if (intr_stack != NULL) {
304 			panic_cpu.cpu_intr_stack = intr_stack;
305 			panic_cpu.cpu_intr_actv = intr_actv;
306 		}
307 
308 		/*
309 		 * Lower ipl to 10 to keep clock() from running, but allow
310 		 * keyboard interrupts to enter the debugger.  These callbacks
311 		 * are executed with panicstr set so they can bypass locks.
312 		 */
313 		splx(ipltospl(CLOCK_LEVEL));
314 		panic_quiesce_hw(pdp);
315 		(void) FTRACE_STOP();
316 		(void) callb_execute_class(CB_CL_PANIC, 0);
317 
318 		if (log_intrq != NULL)
319 			log_flushq(log_intrq);
320 
321 		/*
322 		 * If log_consq has been initialized and syslogd has started,
323 		 * print any messages in log_consq that haven't been consumed.
324 		 */
325 		if (log_consq != NULL && log_consq != log_backlogq)
326 			log_printq(log_consq);
327 
328 		fm_banner();
329 
330 #if defined(__x86)
331 		/*
332 		 * A hypervisor panic originates outside of Solaris, so we
333 		 * don't want to prepend the panic message with misleading
334 		 * pointers from within Solaris.
335 		 */
336 		if (!IN_XPV_PANIC())
337 #endif
338 			printf("\n\rpanic[cpu%d]/thread=%p: ", cp->cpu_id,
339 			    (void *)t);
340 		if (panic_dip != NULL) {
341 			printf("%s%d: ", ddi_driver_name(panic_dip),
342 			    ddi_get_instance(panic_dip));
343 		}
344 		vprintf(format, alist);
345 		printf("\n\n");
346 
347 		if (t->t_panic_trap != NULL) {
348 			panic_showtrap(t->t_panic_trap);
349 			printf("\n");
350 		}
351 
352 		traceregs(rp);
353 		printf("\n");
354 
355 		if (((boothowto & RB_DEBUG) || obpdebug) &&
356 		    !nopanicdebug && !panic_forced) {
357 			if (dumpvp != NULL) {
358 				debug_enter("panic: entering debugger "
359 				    "(continue to save dump)");
360 			} else {
361 				debug_enter("panic: entering debugger "
362 				    "(no dump device, continue to reboot)");
363 			}
364 		}
365 
366 	} else if (panic_dump != 0 || panicstr != NULL) {
367 		printf("\n\rpanic[cpu%d]/thread=%p: ", cp->cpu_id, (void *)t);
368 		vprintf(format, alist);
369 		printf("\n");
370 	} else
371 		goto spin;
372 
373 	/*
374 	 * Prior to performing dump, we make sure that do_polled_io is
375 	 * set, but we'll leave ipl at 10; deadman(), a CY_HIGH_LEVEL cyclic,
376 	 * will re-enter panic if we are not making progress with dump.
377 	 */
378 	/*
379 	 * Take the crash dump.  If the dump trigger is already set, try to
380 	 * enter the debugger again before rebooting the system.
381 	 */
382 	if (panic_trigger(&panic_dump)) {
383 		panic_dump_hw(s);
384 		splx(ipltospl(CLOCK_LEVEL));
385 		errorq_panic();
386 		do_polled_io = 1;
387 		dumpsys();
388 	} else if (((boothowto & RB_DEBUG) || obpdebug) && !nopanicdebug) {
389 		debug_enter("panic: entering debugger (continue to reboot)");
390 	} else
391 		printf("dump aborted: please record the above information!\n");
392 
393 	if (halt_on_panic)
394 		mdboot(A_REBOOT, AD_HALT, NULL, B_FALSE);
395 	else
396 		mdboot(A_REBOOT, panic_bootfcn, panic_bootstr, B_FALSE);
397 spin:
398 	/*
399 	 * Restore ipl to at most CLOCK_LEVEL so we don't end up spinning
400 	 * and unable to jump into the debugger.
401 	 */
402 	splx(MIN(s, ipltospl(CLOCK_LEVEL)));
403 	for (;;)
404 		;
405 }
406 
407 void
408 panic(const char *format, ...)
409 {
410 	va_list alist;
411 
412 	va_start(alist, format);
413 	vpanic(format, alist);
414 	va_end(alist);
415 }
416