xref: /onnv-gate/usr/src/uts/sun4/os/cpu_states.c (revision 3446)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52036Swentaoy  * Common Development and Distribution License (the "License").
62036Swentaoy  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*3446Smrj  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/reboot.h>
290Sstevel@tonic-gate #include <sys/systm.h>
300Sstevel@tonic-gate #include <sys/archsystm.h>
310Sstevel@tonic-gate #include <sys/machsystm.h>
320Sstevel@tonic-gate #include <sys/promif.h>
330Sstevel@tonic-gate #include <sys/promimpl.h>
340Sstevel@tonic-gate #include <sys/prom_plat.h>
350Sstevel@tonic-gate #include <sys/cpu_sgnblk_defs.h>
360Sstevel@tonic-gate #include <sys/ivintr.h>
370Sstevel@tonic-gate #include <sys/kdi.h>
38*3446Smrj #include <sys/kdi_machimpl.h>
390Sstevel@tonic-gate #include <sys/callb.h>
402036Swentaoy #include <sys/wdt.h>
410Sstevel@tonic-gate 
420Sstevel@tonic-gate #ifdef	TRAPTRACE
430Sstevel@tonic-gate #include <sys/traptrace.h>
440Sstevel@tonic-gate #endif /* TRAPTRACE */
450Sstevel@tonic-gate 
460Sstevel@tonic-gate #ifdef C2_AUDIT
470Sstevel@tonic-gate extern void audit_enterprom();
480Sstevel@tonic-gate extern void audit_exitprom();
490Sstevel@tonic-gate #endif /* C2_AUDIT */
500Sstevel@tonic-gate 
510Sstevel@tonic-gate /*
520Sstevel@tonic-gate  * Platforms that use CPU signatures need to set cpu_sgn_func
530Sstevel@tonic-gate  * to point to a platform specific function.  This needs to
540Sstevel@tonic-gate  * be done in set_platform_defaults() within the platmod.
550Sstevel@tonic-gate  */
560Sstevel@tonic-gate void (*cpu_sgn_func)(ushort_t, uchar_t, uchar_t, int) = NULL;
570Sstevel@tonic-gate 
580Sstevel@tonic-gate /*
590Sstevel@tonic-gate  * abort_seq_handler required by sysctrl.
600Sstevel@tonic-gate  */
610Sstevel@tonic-gate void debug_enter(char *);
620Sstevel@tonic-gate void (*abort_seq_handler)(char *) = debug_enter;
630Sstevel@tonic-gate 
640Sstevel@tonic-gate /*
650Sstevel@tonic-gate  * Platform tunable to disable the h/w watchdog timer.
660Sstevel@tonic-gate  */
670Sstevel@tonic-gate extern void clear_watchdog_on_exit(void);
680Sstevel@tonic-gate 
690Sstevel@tonic-gate /*
700Sstevel@tonic-gate  * On sun4u platform, abort_sequence_enter() can be called at high PIL
710Sstevel@tonic-gate  * and we can't afford to acquire any adaptive mutex or use any
720Sstevel@tonic-gate  * condition variables as we are not allowed to sleep while running
730Sstevel@tonic-gate  * on interrupt stack. We work around this problem by posting a level
740Sstevel@tonic-gate  * 10 soft interrupt and then invoking the "abort_seq_handler" within
750Sstevel@tonic-gate  * that soft interrupt context.
760Sstevel@tonic-gate  *
770Sstevel@tonic-gate  * This has the side effect of not allowing us to drop into debugger
780Sstevel@tonic-gate  * when the kernel is stuck at high PIL (PIL > 10).  It's better to
790Sstevel@tonic-gate  * be able to break into a hung system even if it means crashing the
800Sstevel@tonic-gate  * system.  If a user presses L1-A more than once within a 15 seconds
810Sstevel@tonic-gate  * window, and the previous L1-A soft interrupt is still pending, then
820Sstevel@tonic-gate  * we directly invoke the abort_sequence_enter.
830Sstevel@tonic-gate  *
840Sstevel@tonic-gate  * Since the "msg" argument passed to abort_sequence_enter can refer
850Sstevel@tonic-gate  * to a message anywhere in memory, including stack, it's copied into
860Sstevel@tonic-gate  * abort_seq_msgbuf buffer for processing by the soft interrupt.
870Sstevel@tonic-gate  */
880Sstevel@tonic-gate 
890Sstevel@tonic-gate #define	ABORT_SEQ_MSGBUFSZ	256
900Sstevel@tonic-gate #define	FORCE_ABORT_SEQ_INTERVAL ((hrtime_t)15 * NANOSEC)
910Sstevel@tonic-gate 
920Sstevel@tonic-gate static kmutex_t	abort_seq_lock;
932973Sgovinda static uint64_t	abort_seq_inum;		/* abort seq softintr # */
940Sstevel@tonic-gate static hrtime_t	abort_seq_tstamp;	/* hrtime of last abort seq */
950Sstevel@tonic-gate static size_t	abort_seq_msglen;	/* abort seq message length */
960Sstevel@tonic-gate static char	abort_seq_msgbuf[ABORT_SEQ_MSGBUFSZ];
970Sstevel@tonic-gate 
980Sstevel@tonic-gate /*ARGSUSED0*/
990Sstevel@tonic-gate static uint_t
1000Sstevel@tonic-gate abort_seq_softintr(caddr_t arg)
1010Sstevel@tonic-gate {
1020Sstevel@tonic-gate 	char	*msg;
1030Sstevel@tonic-gate 	char	msgbuf[ABORT_SEQ_MSGBUFSZ];
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate 	mutex_enter(&abort_seq_lock);
1060Sstevel@tonic-gate 	if (abort_enable != 0 && abort_seq_tstamp != 0LL) {
1070Sstevel@tonic-gate 		if (abort_seq_msglen > 0) {
1080Sstevel@tonic-gate 			bcopy(abort_seq_msgbuf, msgbuf, abort_seq_msglen);
1090Sstevel@tonic-gate 			msg = msgbuf;
1100Sstevel@tonic-gate 		} else
1110Sstevel@tonic-gate 			msg = NULL;
1120Sstevel@tonic-gate 		abort_seq_tstamp = 0LL;
1130Sstevel@tonic-gate 		mutex_exit(&abort_seq_lock);
1140Sstevel@tonic-gate #ifdef C2_AUDIT
1150Sstevel@tonic-gate 		if (audit_active)
1160Sstevel@tonic-gate 			audit_enterprom(1);
1170Sstevel@tonic-gate #endif /* C2_AUDIT */
1180Sstevel@tonic-gate 		(*abort_seq_handler)(msg);
1190Sstevel@tonic-gate #ifdef C2_AUDIT
1200Sstevel@tonic-gate 		if (audit_active)
1210Sstevel@tonic-gate 			audit_exitprom(1);
1220Sstevel@tonic-gate #endif /* C2_AUDIT */
1230Sstevel@tonic-gate 	} else {
1240Sstevel@tonic-gate 		mutex_exit(&abort_seq_lock);
1250Sstevel@tonic-gate #ifdef C2_AUDIT
1260Sstevel@tonic-gate 		if (audit_active)
1270Sstevel@tonic-gate 			audit_enterprom(0);
1280Sstevel@tonic-gate #endif /* C2_AUDIT */
1290Sstevel@tonic-gate 	}
1300Sstevel@tonic-gate 	return (1);
1310Sstevel@tonic-gate }
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate void
1340Sstevel@tonic-gate abort_sequence_init(void)
1350Sstevel@tonic-gate {
1360Sstevel@tonic-gate 	mutex_init(&abort_seq_lock, NULL, MUTEX_SPIN, (void *)PIL_12);
1370Sstevel@tonic-gate 	abort_seq_tstamp = 0LL;
1380Sstevel@tonic-gate 	if (abort_seq_inum == 0)
1390Sstevel@tonic-gate 		abort_seq_inum = add_softintr(LOCK_LEVEL,
1402973Sgovinda 		    (softintrfunc)abort_seq_softintr, NULL, SOFTINT_ST);
1410Sstevel@tonic-gate }
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate /*
1440Sstevel@tonic-gate  *	Machine dependent abort sequence handling
1450Sstevel@tonic-gate  */
1460Sstevel@tonic-gate void
1470Sstevel@tonic-gate abort_sequence_enter(char *msg)
1480Sstevel@tonic-gate {
1490Sstevel@tonic-gate 	int		s, on_intr;
1500Sstevel@tonic-gate 	size_t		msglen;
1510Sstevel@tonic-gate 	hrtime_t	tstamp;
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 	if (abort_enable != 0) {
1540Sstevel@tonic-gate 		s = splhi();
1550Sstevel@tonic-gate 		on_intr = CPU_ON_INTR(CPU) || (spltoipl(s) > LOCK_LEVEL);
1560Sstevel@tonic-gate 		splx(s);
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 		tstamp = gethrtime();
1590Sstevel@tonic-gate 		mutex_enter(&abort_seq_lock);
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 		/*
1620Sstevel@tonic-gate 		 * If we are on an interrupt stack and/or running at
1630Sstevel@tonic-gate 		 * PIL > LOCK_LEVEL, then we post a softint and invoke
1640Sstevel@tonic-gate 		 * abort_seq_handler from there as we can't afford to
1650Sstevel@tonic-gate 		 * acquire any adaptive mutex here. However, if we
1660Sstevel@tonic-gate 		 * already have a pending softint, which was posted
1670Sstevel@tonic-gate 		 * within FORCE_ABORT_SEQ_INTERVAL duration, then we
1680Sstevel@tonic-gate 		 * bypass softint approach as our softint may be blocked
1690Sstevel@tonic-gate 		 * and the user really wants to drop into the debugger.
1700Sstevel@tonic-gate 		 */
1710Sstevel@tonic-gate 		if (on_intr && abort_seq_inum != 0 &&
1720Sstevel@tonic-gate 		    (abort_seq_tstamp == 0LL || tstamp >
1730Sstevel@tonic-gate 		    (abort_seq_tstamp + FORCE_ABORT_SEQ_INTERVAL))) {
1740Sstevel@tonic-gate 			abort_seq_tstamp = tstamp;
1750Sstevel@tonic-gate 			if (msg != NULL) {
1760Sstevel@tonic-gate 				msglen = strlen(msg);
1770Sstevel@tonic-gate 				if (msglen >= ABORT_SEQ_MSGBUFSZ)
1780Sstevel@tonic-gate 					msglen = ABORT_SEQ_MSGBUFSZ - 1;
1790Sstevel@tonic-gate 				bcopy(msg, abort_seq_msgbuf, msglen);
1800Sstevel@tonic-gate 				abort_seq_msgbuf[msglen] = '\0';
1810Sstevel@tonic-gate 				abort_seq_msglen = msglen + 1;
1820Sstevel@tonic-gate 			} else
1830Sstevel@tonic-gate 				abort_seq_msglen = 0;
1840Sstevel@tonic-gate 			mutex_exit(&abort_seq_lock);
1850Sstevel@tonic-gate 			setsoftint(abort_seq_inum);
1860Sstevel@tonic-gate 		} else {
1870Sstevel@tonic-gate 			/*
1880Sstevel@tonic-gate 			 * Ignore any pending abort sequence softint
1890Sstevel@tonic-gate 			 * as we are invoking the abort_seq_handler
1900Sstevel@tonic-gate 			 * here.
1910Sstevel@tonic-gate 			 */
1920Sstevel@tonic-gate 			abort_seq_tstamp = 0LL;
1930Sstevel@tonic-gate 			mutex_exit(&abort_seq_lock);
1940Sstevel@tonic-gate #ifdef C2_AUDIT
1950Sstevel@tonic-gate 		if (!on_intr && audit_active)
1960Sstevel@tonic-gate 			audit_enterprom(1);
1970Sstevel@tonic-gate #endif /* C2_AUDIT */
1980Sstevel@tonic-gate 			(*abort_seq_handler)(msg);
1990Sstevel@tonic-gate #ifdef C2_AUDIT
2000Sstevel@tonic-gate 		if (!on_intr && audit_active)
2010Sstevel@tonic-gate 			audit_exitprom(1);
2020Sstevel@tonic-gate #endif /* C2_AUDIT */
2030Sstevel@tonic-gate 		}
2040Sstevel@tonic-gate 	} else {
2050Sstevel@tonic-gate #ifdef C2_AUDIT
2060Sstevel@tonic-gate 		if (audit_active)
2070Sstevel@tonic-gate 			audit_enterprom(0);
2080Sstevel@tonic-gate #endif /* C2_AUDIT */
2090Sstevel@tonic-gate 	}
2100Sstevel@tonic-gate }
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate /*
2130Sstevel@tonic-gate  * Enter debugger.  Called when the user types L1-A or break or whenever
2140Sstevel@tonic-gate  * code wants to enter the debugger and possibly resume later.
2150Sstevel@tonic-gate  * If the debugger isn't present, enter the PROM monitor.
2160Sstevel@tonic-gate  *
2170Sstevel@tonic-gate  * If console is a framebuffer which is powered off, it will be powered up
2180Sstevel@tonic-gate  * before jumping to the debugger.  If we are called above lock level, a
2190Sstevel@tonic-gate  * softint is triggered to reenter this code and allow the fb to be powered
2200Sstevel@tonic-gate  * up as in the less than lock level case.  If this code is entered at greater
2210Sstevel@tonic-gate  * than lock level and the fb is not already powered up, the msg argument
2220Sstevel@tonic-gate  * will not be displayed.
2230Sstevel@tonic-gate  */
2240Sstevel@tonic-gate void
2250Sstevel@tonic-gate debug_enter(char *msg)
2260Sstevel@tonic-gate {
2270Sstevel@tonic-gate 	label_t old_pcb;
2280Sstevel@tonic-gate 	int s;
2290Sstevel@tonic-gate 	extern void pm_cfb_powerup(void);
2300Sstevel@tonic-gate 	extern void pm_cfb_rele(void);
2310Sstevel@tonic-gate 	extern void pm_cfb_trigger(void);
2320Sstevel@tonic-gate 	extern int pm_cfb_check_and_hold(void);
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 	/*
2350Sstevel@tonic-gate 	 * For platforms that use CPU signatures, update the signature
2360Sstevel@tonic-gate 	 * to indicate that we are entering the debugger if we are in
2370Sstevel@tonic-gate 	 * the middle of a panic flow.
2380Sstevel@tonic-gate 	 */
2390Sstevel@tonic-gate 	if (panicstr)
2400Sstevel@tonic-gate 		CPU_SIGNATURE(OS_SIG, SIGST_EXIT, SIGSUBST_DEBUG, -1);
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 	if (!panicstr)
2430Sstevel@tonic-gate 		(void) callb_execute_class(CB_CL_ENTER_DEBUGGER, 0);
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 	if (pm_cfb_check_and_hold())
2460Sstevel@tonic-gate 		if (getpil() > LOCK_LEVEL) {
2470Sstevel@tonic-gate 			pm_cfb_trigger();
2480Sstevel@tonic-gate 			return;
2490Sstevel@tonic-gate 		} else
2500Sstevel@tonic-gate 			pm_cfb_powerup();
2510Sstevel@tonic-gate 	if (msg)
2520Sstevel@tonic-gate 		prom_printf("%s\n", msg);
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 	clear_watchdog_on_exit();
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate 	if ((s = getpil()) < ipltospl(12))
2570Sstevel@tonic-gate 		s = splzs();
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 	old_pcb = curthread->t_pcb;
2600Sstevel@tonic-gate 	(void) setjmp(&curthread->t_pcb);
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 	if (boothowto & RB_DEBUG)
263*3446Smrj 		kmdb_enter();
2640Sstevel@tonic-gate 	else
2650Sstevel@tonic-gate 		prom_enter_mon();
2660Sstevel@tonic-gate 
2672036Swentaoy 	restore_watchdog_on_entry();
2682036Swentaoy 
2690Sstevel@tonic-gate 	curthread->t_pcb = old_pcb;
2700Sstevel@tonic-gate 	splx(s);
2710Sstevel@tonic-gate 	pm_cfb_rele();
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate 	if (!panicstr)
2740Sstevel@tonic-gate 		(void) callb_execute_class(CB_CL_ENTER_DEBUGGER, 1);
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate 	if (panicstr)
2770Sstevel@tonic-gate 		CPU_SIGNATURE(OS_SIG, SIGST_EXIT, SIGSUBST_PANIC_CONT, -1);
2780Sstevel@tonic-gate }
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate /*
2810Sstevel@tonic-gate  * Halt the machine and return to the monitor
2820Sstevel@tonic-gate  */
2830Sstevel@tonic-gate void
2840Sstevel@tonic-gate halt(char *s)
2850Sstevel@tonic-gate {
2860Sstevel@tonic-gate 	flush_windows();
2870Sstevel@tonic-gate 	stop_other_cpus();		/* send stop signal to other CPUs */
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate 	if (s)
2900Sstevel@tonic-gate 		prom_printf("(%s) ", s);
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 	/*
2930Sstevel@tonic-gate 	 * For Platforms that use CPU signatures, we
2940Sstevel@tonic-gate 	 * need to set the signature block to OS and
2950Sstevel@tonic-gate 	 * the state to exiting for all the processors.
2960Sstevel@tonic-gate 	 */
2970Sstevel@tonic-gate 	CPU_SIGNATURE(OS_SIG, SIGST_EXIT, SIGSUBST_HALT, -1);
2980Sstevel@tonic-gate 	prom_exit_to_mon();
2990Sstevel@tonic-gate 	/*NOTREACHED*/
3000Sstevel@tonic-gate }
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate /*
3030Sstevel@tonic-gate  * Halt the machine and power off the system.
3040Sstevel@tonic-gate  */
3050Sstevel@tonic-gate void
3060Sstevel@tonic-gate power_down(const char *s)
3070Sstevel@tonic-gate {
3080Sstevel@tonic-gate 	flush_windows();
3090Sstevel@tonic-gate 	stop_other_cpus();		/* send stop signal to other CPUs */
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate 	if (s != NULL)
3120Sstevel@tonic-gate 		prom_printf("(%s) ", s);
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 	/*
3150Sstevel@tonic-gate 	 * For platforms that use CPU signatures, we need to set up the
3160Sstevel@tonic-gate 	 * signature blocks to indicate that we have an environmental
3170Sstevel@tonic-gate 	 * interrupt request to power down, and then exit to the prom monitor.
3180Sstevel@tonic-gate 	 */
3190Sstevel@tonic-gate 	CPU_SIGNATURE(OS_SIG, SIGST_EXIT, SIGSUBST_ENVIRON, -1);
3200Sstevel@tonic-gate 	prom_power_off();
3210Sstevel@tonic-gate 	/*
3220Sstevel@tonic-gate 	 * If here is reached, for some reason prom's power-off command failed.
3230Sstevel@tonic-gate 	 * Prom should have already printed out error messages. Exit to
3240Sstevel@tonic-gate 	 * firmware.
3250Sstevel@tonic-gate 	 */
3260Sstevel@tonic-gate 	prom_exit_to_mon();
3270Sstevel@tonic-gate 	/*NOTREACHED*/
3280Sstevel@tonic-gate }
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate void
3310Sstevel@tonic-gate do_shutdown(void)
3320Sstevel@tonic-gate {
3330Sstevel@tonic-gate 	proc_t *initpp;
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate 	/*
3360Sstevel@tonic-gate 	 * If we're still booting and init(1) isn't set up yet, simply halt.
3370Sstevel@tonic-gate 	 */
3380Sstevel@tonic-gate 	mutex_enter(&pidlock);
3390Sstevel@tonic-gate 	initpp = prfind(P_INITPID);
3400Sstevel@tonic-gate 	mutex_exit(&pidlock);
3410Sstevel@tonic-gate 	if (initpp == NULL) {
3420Sstevel@tonic-gate 		extern void halt(char *);
3430Sstevel@tonic-gate 		prom_power_off();
3440Sstevel@tonic-gate 		halt("Power off the System");	/* just in case */
3450Sstevel@tonic-gate 	}
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 	/*
3480Sstevel@tonic-gate 	 * else, graceful shutdown with inittab and all getting involved
3490Sstevel@tonic-gate 	 */
3500Sstevel@tonic-gate 	psignal(initpp, SIGPWR);
3510Sstevel@tonic-gate }
352