1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <sys/reboot.h>
30*0Sstevel@tonic-gate #include <sys/systm.h>
31*0Sstevel@tonic-gate #include <sys/archsystm.h>
32*0Sstevel@tonic-gate #include <sys/machsystm.h>
33*0Sstevel@tonic-gate #include <sys/promif.h>
34*0Sstevel@tonic-gate #include <sys/promimpl.h>
35*0Sstevel@tonic-gate #include <sys/prom_plat.h>
36*0Sstevel@tonic-gate #include <sys/cpu_sgnblk_defs.h>
37*0Sstevel@tonic-gate #include <sys/ivintr.h>
38*0Sstevel@tonic-gate #include <sys/kdi.h>
39*0Sstevel@tonic-gate #include <sys/callb.h>
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate #ifdef	TRAPTRACE
42*0Sstevel@tonic-gate #include <sys/traptrace.h>
43*0Sstevel@tonic-gate #endif /* TRAPTRACE */
44*0Sstevel@tonic-gate 
45*0Sstevel@tonic-gate #ifdef C2_AUDIT
46*0Sstevel@tonic-gate extern void audit_enterprom();
47*0Sstevel@tonic-gate extern void audit_exitprom();
48*0Sstevel@tonic-gate #endif /* C2_AUDIT */
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate /*
51*0Sstevel@tonic-gate  * Platforms that use CPU signatures need to set cpu_sgn_func
52*0Sstevel@tonic-gate  * to point to a platform specific function.  This needs to
53*0Sstevel@tonic-gate  * be done in set_platform_defaults() within the platmod.
54*0Sstevel@tonic-gate  */
55*0Sstevel@tonic-gate void (*cpu_sgn_func)(ushort_t, uchar_t, uchar_t, int) = NULL;
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate /*
58*0Sstevel@tonic-gate  * abort_seq_handler required by sysctrl.
59*0Sstevel@tonic-gate  */
60*0Sstevel@tonic-gate void debug_enter(char *);
61*0Sstevel@tonic-gate void (*abort_seq_handler)(char *) = debug_enter;
62*0Sstevel@tonic-gate 
63*0Sstevel@tonic-gate /*
64*0Sstevel@tonic-gate  * Platform tunable to disable the h/w watchdog timer.
65*0Sstevel@tonic-gate  */
66*0Sstevel@tonic-gate int disable_watchdog_on_exit = 0;
67*0Sstevel@tonic-gate extern void clear_watchdog_on_exit(void);
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate /*
71*0Sstevel@tonic-gate  * On sun4u platform, abort_sequence_enter() can be called at high PIL
72*0Sstevel@tonic-gate  * and we can't afford to acquire any adaptive mutex or use any
73*0Sstevel@tonic-gate  * condition variables as we are not allowed to sleep while running
74*0Sstevel@tonic-gate  * on interrupt stack. We work around this problem by posting a level
75*0Sstevel@tonic-gate  * 10 soft interrupt and then invoking the "abort_seq_handler" within
76*0Sstevel@tonic-gate  * that soft interrupt context.
77*0Sstevel@tonic-gate  *
78*0Sstevel@tonic-gate  * This has the side effect of not allowing us to drop into debugger
79*0Sstevel@tonic-gate  * when the kernel is stuck at high PIL (PIL > 10).  It's better to
80*0Sstevel@tonic-gate  * be able to break into a hung system even if it means crashing the
81*0Sstevel@tonic-gate  * system.  If a user presses L1-A more than once within a 15 seconds
82*0Sstevel@tonic-gate  * window, and the previous L1-A soft interrupt is still pending, then
83*0Sstevel@tonic-gate  * we directly invoke the abort_sequence_enter.
84*0Sstevel@tonic-gate  *
85*0Sstevel@tonic-gate  * Since the "msg" argument passed to abort_sequence_enter can refer
86*0Sstevel@tonic-gate  * to a message anywhere in memory, including stack, it's copied into
87*0Sstevel@tonic-gate  * abort_seq_msgbuf buffer for processing by the soft interrupt.
88*0Sstevel@tonic-gate  */
89*0Sstevel@tonic-gate 
90*0Sstevel@tonic-gate #define	ABORT_SEQ_MSGBUFSZ	256
91*0Sstevel@tonic-gate #define	FORCE_ABORT_SEQ_INTERVAL ((hrtime_t)15 * NANOSEC)
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate static kmutex_t	abort_seq_lock;
94*0Sstevel@tonic-gate static uint_t	abort_seq_inum;		/* abort seq softintr # */
95*0Sstevel@tonic-gate static hrtime_t	abort_seq_tstamp;	/* hrtime of last abort seq */
96*0Sstevel@tonic-gate static size_t	abort_seq_msglen;	/* abort seq message length */
97*0Sstevel@tonic-gate static char	abort_seq_msgbuf[ABORT_SEQ_MSGBUFSZ];
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate /*ARGSUSED0*/
100*0Sstevel@tonic-gate static uint_t
101*0Sstevel@tonic-gate abort_seq_softintr(caddr_t arg)
102*0Sstevel@tonic-gate {
103*0Sstevel@tonic-gate 	char	*msg;
104*0Sstevel@tonic-gate 	char	msgbuf[ABORT_SEQ_MSGBUFSZ];
105*0Sstevel@tonic-gate 
106*0Sstevel@tonic-gate 	mutex_enter(&abort_seq_lock);
107*0Sstevel@tonic-gate 	if (abort_enable != 0 && abort_seq_tstamp != 0LL) {
108*0Sstevel@tonic-gate 		if (abort_seq_msglen > 0) {
109*0Sstevel@tonic-gate 			bcopy(abort_seq_msgbuf, msgbuf, abort_seq_msglen);
110*0Sstevel@tonic-gate 			msg = msgbuf;
111*0Sstevel@tonic-gate 		} else
112*0Sstevel@tonic-gate 			msg = NULL;
113*0Sstevel@tonic-gate 		abort_seq_tstamp = 0LL;
114*0Sstevel@tonic-gate 		mutex_exit(&abort_seq_lock);
115*0Sstevel@tonic-gate #ifdef C2_AUDIT
116*0Sstevel@tonic-gate 		if (audit_active)
117*0Sstevel@tonic-gate 			audit_enterprom(1);
118*0Sstevel@tonic-gate #endif /* C2_AUDIT */
119*0Sstevel@tonic-gate 		(*abort_seq_handler)(msg);
120*0Sstevel@tonic-gate #ifdef C2_AUDIT
121*0Sstevel@tonic-gate 		if (audit_active)
122*0Sstevel@tonic-gate 			audit_exitprom(1);
123*0Sstevel@tonic-gate #endif /* C2_AUDIT */
124*0Sstevel@tonic-gate 	} else {
125*0Sstevel@tonic-gate 		mutex_exit(&abort_seq_lock);
126*0Sstevel@tonic-gate #ifdef C2_AUDIT
127*0Sstevel@tonic-gate 		if (audit_active)
128*0Sstevel@tonic-gate 			audit_enterprom(0);
129*0Sstevel@tonic-gate #endif /* C2_AUDIT */
130*0Sstevel@tonic-gate 	}
131*0Sstevel@tonic-gate 	return (1);
132*0Sstevel@tonic-gate }
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate void
135*0Sstevel@tonic-gate abort_sequence_init(void)
136*0Sstevel@tonic-gate {
137*0Sstevel@tonic-gate 	mutex_init(&abort_seq_lock, NULL, MUTEX_SPIN, (void *)PIL_12);
138*0Sstevel@tonic-gate 	abort_seq_tstamp = 0LL;
139*0Sstevel@tonic-gate 	if (abort_seq_inum == 0)
140*0Sstevel@tonic-gate 		abort_seq_inum = add_softintr(LOCK_LEVEL,
141*0Sstevel@tonic-gate 		    (softintrfunc)abort_seq_softintr, NULL);
142*0Sstevel@tonic-gate }
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate /*
145*0Sstevel@tonic-gate  *	Machine dependent abort sequence handling
146*0Sstevel@tonic-gate  */
147*0Sstevel@tonic-gate void
148*0Sstevel@tonic-gate abort_sequence_enter(char *msg)
149*0Sstevel@tonic-gate {
150*0Sstevel@tonic-gate 	int		s, on_intr;
151*0Sstevel@tonic-gate 	size_t		msglen;
152*0Sstevel@tonic-gate 	hrtime_t	tstamp;
153*0Sstevel@tonic-gate 
154*0Sstevel@tonic-gate 	if (abort_enable != 0) {
155*0Sstevel@tonic-gate 		s = splhi();
156*0Sstevel@tonic-gate 		on_intr = CPU_ON_INTR(CPU) || (spltoipl(s) > LOCK_LEVEL);
157*0Sstevel@tonic-gate 		splx(s);
158*0Sstevel@tonic-gate 
159*0Sstevel@tonic-gate 		tstamp = gethrtime();
160*0Sstevel@tonic-gate 		mutex_enter(&abort_seq_lock);
161*0Sstevel@tonic-gate 
162*0Sstevel@tonic-gate 		/*
163*0Sstevel@tonic-gate 		 * If we are on an interrupt stack and/or running at
164*0Sstevel@tonic-gate 		 * PIL > LOCK_LEVEL, then we post a softint and invoke
165*0Sstevel@tonic-gate 		 * abort_seq_handler from there as we can't afford to
166*0Sstevel@tonic-gate 		 * acquire any adaptive mutex here. However, if we
167*0Sstevel@tonic-gate 		 * already have a pending softint, which was posted
168*0Sstevel@tonic-gate 		 * within FORCE_ABORT_SEQ_INTERVAL duration, then we
169*0Sstevel@tonic-gate 		 * bypass softint approach as our softint may be blocked
170*0Sstevel@tonic-gate 		 * and the user really wants to drop into the debugger.
171*0Sstevel@tonic-gate 		 */
172*0Sstevel@tonic-gate 		if (on_intr && abort_seq_inum != 0 &&
173*0Sstevel@tonic-gate 		    (abort_seq_tstamp == 0LL || tstamp >
174*0Sstevel@tonic-gate 		    (abort_seq_tstamp + FORCE_ABORT_SEQ_INTERVAL))) {
175*0Sstevel@tonic-gate 			abort_seq_tstamp = tstamp;
176*0Sstevel@tonic-gate 			if (msg != NULL) {
177*0Sstevel@tonic-gate 				msglen = strlen(msg);
178*0Sstevel@tonic-gate 				if (msglen >= ABORT_SEQ_MSGBUFSZ)
179*0Sstevel@tonic-gate 					msglen = ABORT_SEQ_MSGBUFSZ - 1;
180*0Sstevel@tonic-gate 				bcopy(msg, abort_seq_msgbuf, msglen);
181*0Sstevel@tonic-gate 				abort_seq_msgbuf[msglen] = '\0';
182*0Sstevel@tonic-gate 				abort_seq_msglen = msglen + 1;
183*0Sstevel@tonic-gate 			} else
184*0Sstevel@tonic-gate 				abort_seq_msglen = 0;
185*0Sstevel@tonic-gate 			mutex_exit(&abort_seq_lock);
186*0Sstevel@tonic-gate 			setsoftint(abort_seq_inum);
187*0Sstevel@tonic-gate 		} else {
188*0Sstevel@tonic-gate 			/*
189*0Sstevel@tonic-gate 			 * Ignore any pending abort sequence softint
190*0Sstevel@tonic-gate 			 * as we are invoking the abort_seq_handler
191*0Sstevel@tonic-gate 			 * here.
192*0Sstevel@tonic-gate 			 */
193*0Sstevel@tonic-gate 			abort_seq_tstamp = 0LL;
194*0Sstevel@tonic-gate 			mutex_exit(&abort_seq_lock);
195*0Sstevel@tonic-gate #ifdef C2_AUDIT
196*0Sstevel@tonic-gate 		if (!on_intr && audit_active)
197*0Sstevel@tonic-gate 			audit_enterprom(1);
198*0Sstevel@tonic-gate #endif /* C2_AUDIT */
199*0Sstevel@tonic-gate 			(*abort_seq_handler)(msg);
200*0Sstevel@tonic-gate #ifdef C2_AUDIT
201*0Sstevel@tonic-gate 		if (!on_intr && audit_active)
202*0Sstevel@tonic-gate 			audit_exitprom(1);
203*0Sstevel@tonic-gate #endif /* C2_AUDIT */
204*0Sstevel@tonic-gate 		}
205*0Sstevel@tonic-gate 	} else {
206*0Sstevel@tonic-gate #ifdef C2_AUDIT
207*0Sstevel@tonic-gate 		if (audit_active)
208*0Sstevel@tonic-gate 			audit_enterprom(0);
209*0Sstevel@tonic-gate #endif /* C2_AUDIT */
210*0Sstevel@tonic-gate 	}
211*0Sstevel@tonic-gate }
212*0Sstevel@tonic-gate 
213*0Sstevel@tonic-gate /*
214*0Sstevel@tonic-gate  * Enter debugger.  Called when the user types L1-A or break or whenever
215*0Sstevel@tonic-gate  * code wants to enter the debugger and possibly resume later.
216*0Sstevel@tonic-gate  * If the debugger isn't present, enter the PROM monitor.
217*0Sstevel@tonic-gate  *
218*0Sstevel@tonic-gate  * If console is a framebuffer which is powered off, it will be powered up
219*0Sstevel@tonic-gate  * before jumping to the debugger.  If we are called above lock level, a
220*0Sstevel@tonic-gate  * softint is triggered to reenter this code and allow the fb to be powered
221*0Sstevel@tonic-gate  * up as in the less than lock level case.  If this code is entered at greater
222*0Sstevel@tonic-gate  * than lock level and the fb is not already powered up, the msg argument
223*0Sstevel@tonic-gate  * will not be displayed.
224*0Sstevel@tonic-gate  */
225*0Sstevel@tonic-gate void
226*0Sstevel@tonic-gate debug_enter(char *msg)
227*0Sstevel@tonic-gate {
228*0Sstevel@tonic-gate 	label_t old_pcb;
229*0Sstevel@tonic-gate 	int s;
230*0Sstevel@tonic-gate 	extern void pm_cfb_powerup(void);
231*0Sstevel@tonic-gate 	extern void pm_cfb_rele(void);
232*0Sstevel@tonic-gate 	extern void pm_cfb_trigger(void);
233*0Sstevel@tonic-gate 	extern int pm_cfb_check_and_hold(void);
234*0Sstevel@tonic-gate 
235*0Sstevel@tonic-gate 	/*
236*0Sstevel@tonic-gate 	 * For platforms that use CPU signatures, update the signature
237*0Sstevel@tonic-gate 	 * to indicate that we are entering the debugger if we are in
238*0Sstevel@tonic-gate 	 * the middle of a panic flow.
239*0Sstevel@tonic-gate 	 */
240*0Sstevel@tonic-gate 	if (panicstr)
241*0Sstevel@tonic-gate 		CPU_SIGNATURE(OS_SIG, SIGST_EXIT, SIGSUBST_DEBUG, -1);
242*0Sstevel@tonic-gate 
243*0Sstevel@tonic-gate 	if (!panicstr)
244*0Sstevel@tonic-gate 		(void) callb_execute_class(CB_CL_ENTER_DEBUGGER, 0);
245*0Sstevel@tonic-gate 
246*0Sstevel@tonic-gate 	if (pm_cfb_check_and_hold())
247*0Sstevel@tonic-gate 		if (getpil() > LOCK_LEVEL) {
248*0Sstevel@tonic-gate 			pm_cfb_trigger();
249*0Sstevel@tonic-gate 			return;
250*0Sstevel@tonic-gate 		} else
251*0Sstevel@tonic-gate 			pm_cfb_powerup();
252*0Sstevel@tonic-gate 	if (msg)
253*0Sstevel@tonic-gate 		prom_printf("%s\n", msg);
254*0Sstevel@tonic-gate 
255*0Sstevel@tonic-gate 	clear_watchdog_on_exit();
256*0Sstevel@tonic-gate 
257*0Sstevel@tonic-gate 	if ((s = getpil()) < ipltospl(12))
258*0Sstevel@tonic-gate 		s = splzs();
259*0Sstevel@tonic-gate 
260*0Sstevel@tonic-gate 	old_pcb = curthread->t_pcb;
261*0Sstevel@tonic-gate 	(void) setjmp(&curthread->t_pcb);
262*0Sstevel@tonic-gate 
263*0Sstevel@tonic-gate 	if (boothowto & RB_DEBUG)
264*0Sstevel@tonic-gate 		kdi_dvec_enter();
265*0Sstevel@tonic-gate 	else
266*0Sstevel@tonic-gate 		prom_enter_mon();
267*0Sstevel@tonic-gate 
268*0Sstevel@tonic-gate 	curthread->t_pcb = old_pcb;
269*0Sstevel@tonic-gate 	splx(s);
270*0Sstevel@tonic-gate 	pm_cfb_rele();
271*0Sstevel@tonic-gate 
272*0Sstevel@tonic-gate 	if (!panicstr)
273*0Sstevel@tonic-gate 		(void) callb_execute_class(CB_CL_ENTER_DEBUGGER, 1);
274*0Sstevel@tonic-gate 
275*0Sstevel@tonic-gate 	if (panicstr)
276*0Sstevel@tonic-gate 		CPU_SIGNATURE(OS_SIG, SIGST_EXIT, SIGSUBST_PANIC_CONT, -1);
277*0Sstevel@tonic-gate }
278*0Sstevel@tonic-gate 
279*0Sstevel@tonic-gate /*
280*0Sstevel@tonic-gate  * Halt the machine and return to the monitor
281*0Sstevel@tonic-gate  */
282*0Sstevel@tonic-gate void
283*0Sstevel@tonic-gate halt(char *s)
284*0Sstevel@tonic-gate {
285*0Sstevel@tonic-gate 	flush_windows();
286*0Sstevel@tonic-gate 	stop_other_cpus();		/* send stop signal to other CPUs */
287*0Sstevel@tonic-gate 
288*0Sstevel@tonic-gate 	if (s)
289*0Sstevel@tonic-gate 		prom_printf("(%s) ", s);
290*0Sstevel@tonic-gate 
291*0Sstevel@tonic-gate 	/*
292*0Sstevel@tonic-gate 	 * For Platforms that use CPU signatures, we
293*0Sstevel@tonic-gate 	 * need to set the signature block to OS and
294*0Sstevel@tonic-gate 	 * the state to exiting for all the processors.
295*0Sstevel@tonic-gate 	 */
296*0Sstevel@tonic-gate 	CPU_SIGNATURE(OS_SIG, SIGST_EXIT, SIGSUBST_HALT, -1);
297*0Sstevel@tonic-gate 	prom_exit_to_mon();
298*0Sstevel@tonic-gate 	/*NOTREACHED*/
299*0Sstevel@tonic-gate }
300*0Sstevel@tonic-gate 
301*0Sstevel@tonic-gate /*
302*0Sstevel@tonic-gate  * Halt the machine and power off the system.
303*0Sstevel@tonic-gate  */
304*0Sstevel@tonic-gate void
305*0Sstevel@tonic-gate power_down(const char *s)
306*0Sstevel@tonic-gate {
307*0Sstevel@tonic-gate 	flush_windows();
308*0Sstevel@tonic-gate 	stop_other_cpus();		/* send stop signal to other CPUs */
309*0Sstevel@tonic-gate 
310*0Sstevel@tonic-gate 	if (s != NULL)
311*0Sstevel@tonic-gate 		prom_printf("(%s) ", s);
312*0Sstevel@tonic-gate 
313*0Sstevel@tonic-gate 	/*
314*0Sstevel@tonic-gate 	 * For platforms that use CPU signatures, we need to set up the
315*0Sstevel@tonic-gate 	 * signature blocks to indicate that we have an environmental
316*0Sstevel@tonic-gate 	 * interrupt request to power down, and then exit to the prom monitor.
317*0Sstevel@tonic-gate 	 */
318*0Sstevel@tonic-gate 	CPU_SIGNATURE(OS_SIG, SIGST_EXIT, SIGSUBST_ENVIRON, -1);
319*0Sstevel@tonic-gate 	prom_power_off();
320*0Sstevel@tonic-gate 	/*
321*0Sstevel@tonic-gate 	 * If here is reached, for some reason prom's power-off command failed.
322*0Sstevel@tonic-gate 	 * Prom should have already printed out error messages. Exit to
323*0Sstevel@tonic-gate 	 * firmware.
324*0Sstevel@tonic-gate 	 */
325*0Sstevel@tonic-gate 	prom_exit_to_mon();
326*0Sstevel@tonic-gate 	/*NOTREACHED*/
327*0Sstevel@tonic-gate }
328*0Sstevel@tonic-gate 
329*0Sstevel@tonic-gate void
330*0Sstevel@tonic-gate do_shutdown(void)
331*0Sstevel@tonic-gate {
332*0Sstevel@tonic-gate 	proc_t *initpp;
333*0Sstevel@tonic-gate 
334*0Sstevel@tonic-gate 	/*
335*0Sstevel@tonic-gate 	 * If we're still booting and init(1) isn't set up yet, simply halt.
336*0Sstevel@tonic-gate 	 */
337*0Sstevel@tonic-gate 	mutex_enter(&pidlock);
338*0Sstevel@tonic-gate 	initpp = prfind(P_INITPID);
339*0Sstevel@tonic-gate 	mutex_exit(&pidlock);
340*0Sstevel@tonic-gate 	if (initpp == NULL) {
341*0Sstevel@tonic-gate 		extern void halt(char *);
342*0Sstevel@tonic-gate 		prom_power_off();
343*0Sstevel@tonic-gate 		halt("Power off the System");	/* just in case */
344*0Sstevel@tonic-gate 	}
345*0Sstevel@tonic-gate 
346*0Sstevel@tonic-gate 	/*
347*0Sstevel@tonic-gate 	 * else, graceful shutdown with inittab and all getting involved
348*0Sstevel@tonic-gate 	 */
349*0Sstevel@tonic-gate 	psignal(initpp, SIGPWR);
350*0Sstevel@tonic-gate }
351