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