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