xref: /onnv-gate/usr/src/uts/i86pc/os/machdep.c (revision 3253:c929f34b62c5)
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
51253Slq150181  * Common Development and Distribution License (the "License").
61253Slq150181  * 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  */
211253Slq150181 
220Sstevel@tonic-gate /*
231253Slq150181  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <sys/t_lock.h>
310Sstevel@tonic-gate #include <sys/param.h>
320Sstevel@tonic-gate #include <sys/sysmacros.h>
330Sstevel@tonic-gate #include <sys/signal.h>
340Sstevel@tonic-gate #include <sys/systm.h>
350Sstevel@tonic-gate #include <sys/user.h>
360Sstevel@tonic-gate #include <sys/mman.h>
370Sstevel@tonic-gate #include <sys/vm.h>
380Sstevel@tonic-gate 
390Sstevel@tonic-gate #include <sys/disp.h>
400Sstevel@tonic-gate #include <sys/class.h>
410Sstevel@tonic-gate 
420Sstevel@tonic-gate #include <sys/proc.h>
430Sstevel@tonic-gate #include <sys/buf.h>
440Sstevel@tonic-gate #include <sys/kmem.h>
450Sstevel@tonic-gate 
460Sstevel@tonic-gate #include <sys/reboot.h>
470Sstevel@tonic-gate #include <sys/uadmin.h>
480Sstevel@tonic-gate #include <sys/callb.h>
490Sstevel@tonic-gate 
500Sstevel@tonic-gate #include <sys/cred.h>
510Sstevel@tonic-gate #include <sys/vnode.h>
520Sstevel@tonic-gate #include <sys/file.h>
530Sstevel@tonic-gate 
540Sstevel@tonic-gate #include <sys/procfs.h>
550Sstevel@tonic-gate #include <sys/acct.h>
560Sstevel@tonic-gate 
570Sstevel@tonic-gate #include <sys/vfs.h>
580Sstevel@tonic-gate #include <sys/dnlc.h>
590Sstevel@tonic-gate #include <sys/var.h>
600Sstevel@tonic-gate #include <sys/cmn_err.h>
610Sstevel@tonic-gate #include <sys/utsname.h>
620Sstevel@tonic-gate #include <sys/debug.h>
630Sstevel@tonic-gate #include <sys/kdi_impl.h>
640Sstevel@tonic-gate 
650Sstevel@tonic-gate #include <sys/dumphdr.h>
660Sstevel@tonic-gate #include <sys/bootconf.h>
670Sstevel@tonic-gate #include <sys/varargs.h>
680Sstevel@tonic-gate #include <sys/promif.h>
690Sstevel@tonic-gate #include <sys/modctl.h>
700Sstevel@tonic-gate 
710Sstevel@tonic-gate #include <sys/consdev.h>
720Sstevel@tonic-gate #include <sys/frame.h>
730Sstevel@tonic-gate 
740Sstevel@tonic-gate #include <sys/sunddi.h>
750Sstevel@tonic-gate #include <sys/ddidmareq.h>
760Sstevel@tonic-gate #include <sys/psw.h>
770Sstevel@tonic-gate #include <sys/regset.h>
780Sstevel@tonic-gate #include <sys/privregs.h>
790Sstevel@tonic-gate #include <sys/clock.h>
800Sstevel@tonic-gate #include <sys/tss.h>
810Sstevel@tonic-gate #include <sys/cpu.h>
820Sstevel@tonic-gate #include <sys/stack.h>
830Sstevel@tonic-gate #include <sys/trap.h>
840Sstevel@tonic-gate #include <sys/pic.h>
850Sstevel@tonic-gate #include <sys/mmu.h>
860Sstevel@tonic-gate #include <vm/hat.h>
870Sstevel@tonic-gate #include <vm/anon.h>
880Sstevel@tonic-gate #include <vm/as.h>
890Sstevel@tonic-gate #include <vm/page.h>
900Sstevel@tonic-gate #include <vm/seg.h>
910Sstevel@tonic-gate #include <vm/seg_kmem.h>
920Sstevel@tonic-gate #include <vm/seg_map.h>
930Sstevel@tonic-gate #include <vm/seg_vn.h>
940Sstevel@tonic-gate #include <vm/seg_kp.h>
950Sstevel@tonic-gate #include <vm/hat_i86.h>
960Sstevel@tonic-gate #include <sys/swap.h>
970Sstevel@tonic-gate #include <sys/thread.h>
980Sstevel@tonic-gate #include <sys/sysconf.h>
990Sstevel@tonic-gate #include <sys/vm_machparam.h>
1000Sstevel@tonic-gate #include <sys/archsystm.h>
1010Sstevel@tonic-gate #include <sys/machsystm.h>
1020Sstevel@tonic-gate #include <sys/machlock.h>
1030Sstevel@tonic-gate #include <sys/x_call.h>
1040Sstevel@tonic-gate #include <sys/instance.h>
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate #include <sys/time.h>
1070Sstevel@tonic-gate #include <sys/smp_impldefs.h>
1080Sstevel@tonic-gate #include <sys/psm_types.h>
1090Sstevel@tonic-gate #include <sys/atomic.h>
1100Sstevel@tonic-gate #include <sys/panic.h>
1110Sstevel@tonic-gate #include <sys/cpuvar.h>
1120Sstevel@tonic-gate #include <sys/dtrace.h>
1130Sstevel@tonic-gate #include <sys/bl.h>
1140Sstevel@tonic-gate #include <sys/nvpair.h>
1150Sstevel@tonic-gate #include <sys/x86_archext.h>
1160Sstevel@tonic-gate #include <sys/pool_pset.h>
1170Sstevel@tonic-gate #include <sys/autoconf.h>
1180Sstevel@tonic-gate #include <sys/kdi.h>
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate #ifdef	TRAPTRACE
1210Sstevel@tonic-gate #include <sys/traptrace.h>
1220Sstevel@tonic-gate #endif	/* TRAPTRACE */
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate #ifdef C2_AUDIT
1250Sstevel@tonic-gate extern void audit_enterprom(int);
1260Sstevel@tonic-gate extern void audit_exitprom(int);
1270Sstevel@tonic-gate #endif
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate /*
1300Sstevel@tonic-gate  * The panicbuf array is used to record messages and state:
1310Sstevel@tonic-gate  */
1320Sstevel@tonic-gate char panicbuf[PANICBUFSIZE];
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate /*
1350Sstevel@tonic-gate  * maxphys - used during physio
1360Sstevel@tonic-gate  * klustsize - used for klustering by swapfs and specfs
1370Sstevel@tonic-gate  */
1380Sstevel@tonic-gate int maxphys = 56 * 1024;    /* XXX See vm_subr.c - max b_count in physio */
1390Sstevel@tonic-gate int klustsize = 56 * 1024;
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate caddr_t	p0_va;		/* Virtual address for accessing physical page 0 */
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate /*
1440Sstevel@tonic-gate  * defined here, though unused on x86,
1450Sstevel@tonic-gate  * to make kstat_fr.c happy.
1460Sstevel@tonic-gate  */
1470Sstevel@tonic-gate int vac;
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate void stop_other_cpus();
1500Sstevel@tonic-gate void debug_enter(char *);
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate extern void pm_cfb_check_and_powerup(void);
1530Sstevel@tonic-gate extern void pm_cfb_rele(void);
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate /*
1560Sstevel@tonic-gate  * Machine dependent code to reboot.
1570Sstevel@tonic-gate  * "mdep" is interpreted as a character pointer; if non-null, it is a pointer
1580Sstevel@tonic-gate  * to a string to be used as the argument string when rebooting.
159136Sachartre  *
160136Sachartre  * "invoke_cb" is a boolean. It is set to true when mdboot() can safely
161136Sachartre  * invoke CB_CL_MDBOOT callbacks before shutting the system down, i.e. when
162136Sachartre  * we are in a normal shutdown sequence (interrupts are not blocked, the
163136Sachartre  * system is not panic'ing or being suspended).
1640Sstevel@tonic-gate  */
1650Sstevel@tonic-gate /*ARGSUSED*/
1660Sstevel@tonic-gate void
167136Sachartre mdboot(int cmd, int fcn, char *mdep, boolean_t invoke_cb)
1680Sstevel@tonic-gate {
1690Sstevel@tonic-gate 	extern void mtrr_resync(void);
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 	if (!panicstr) {
1720Sstevel@tonic-gate 		kpreempt_disable();
1731389Sdmick 		affinity_set(CPU_CURRENT);
1740Sstevel@tonic-gate 	}
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate 	/*
1770Sstevel@tonic-gate 	 * XXX - rconsvp is set to NULL to ensure that output messages
1780Sstevel@tonic-gate 	 * are sent to the underlying "hardware" device using the
1790Sstevel@tonic-gate 	 * monitor's printf routine since we are in the process of
1800Sstevel@tonic-gate 	 * either rebooting or halting the machine.
1810Sstevel@tonic-gate 	 */
1820Sstevel@tonic-gate 	rconsvp = NULL;
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate 	/*
1850Sstevel@tonic-gate 	 * Print the reboot message now, before pausing other cpus.
1860Sstevel@tonic-gate 	 * There is a race condition in the printing support that
1870Sstevel@tonic-gate 	 * can deadlock multiprocessor machines.
1880Sstevel@tonic-gate 	 */
1890Sstevel@tonic-gate 	if (!(fcn == AD_HALT || fcn == AD_POWEROFF))
1900Sstevel@tonic-gate 		prom_printf("rebooting...\n");
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 	/*
1930Sstevel@tonic-gate 	 * We can't bring up the console from above lock level, so do it now
1940Sstevel@tonic-gate 	 */
1950Sstevel@tonic-gate 	pm_cfb_check_and_powerup();
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 	/* make sure there are no more changes to the device tree */
1980Sstevel@tonic-gate 	devtree_freeze();
1990Sstevel@tonic-gate 
200136Sachartre 	if (invoke_cb)
201136Sachartre 		(void) callb_execute_class(CB_CL_MDBOOT, NULL);
202136Sachartre 
203*3253Smec 	/*
204*3253Smec 	 * Clear any unresolved UEs from memory.
205*3253Smec 	 */
206*3253Smec 	page_retire_mdboot();
207917Selowe 
2080Sstevel@tonic-gate 	/*
2090Sstevel@tonic-gate 	 * stop other cpus and raise our priority.  since there is only
2100Sstevel@tonic-gate 	 * one active cpu after this, and our priority will be too high
2110Sstevel@tonic-gate 	 * for us to be preempted, we're essentially single threaded
2120Sstevel@tonic-gate 	 * from here on out.
2130Sstevel@tonic-gate 	 */
2140Sstevel@tonic-gate 	(void) spl6();
2150Sstevel@tonic-gate 	if (!panicstr) {
2160Sstevel@tonic-gate 		mutex_enter(&cpu_lock);
2170Sstevel@tonic-gate 		pause_cpus(NULL);
2180Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
2190Sstevel@tonic-gate 	}
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 	/*
2220Sstevel@tonic-gate 	 * try and reset leaf devices.  reset_leaves() should only
2230Sstevel@tonic-gate 	 * be called when there are no other threads that could be
2240Sstevel@tonic-gate 	 * accessing devices
2250Sstevel@tonic-gate 	 */
2260Sstevel@tonic-gate 	reset_leaves();
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 	(void) spl8();
2290Sstevel@tonic-gate 	(*psm_shutdownf)(cmd, fcn);
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 	mtrr_resync();
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate 	if (fcn == AD_HALT || fcn == AD_POWEROFF)
2340Sstevel@tonic-gate 		halt((char *)NULL);
2350Sstevel@tonic-gate 	else
2360Sstevel@tonic-gate 		prom_reboot("");
2370Sstevel@tonic-gate 	/*NOTREACHED*/
2380Sstevel@tonic-gate }
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate /* mdpreboot - may be called prior to mdboot while root fs still mounted */
2410Sstevel@tonic-gate /*ARGSUSED*/
2420Sstevel@tonic-gate void
2430Sstevel@tonic-gate mdpreboot(int cmd, int fcn, char *mdep)
2440Sstevel@tonic-gate {
2450Sstevel@tonic-gate 	(*psm_preshutdownf)(cmd, fcn);
2460Sstevel@tonic-gate }
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate void
2490Sstevel@tonic-gate idle_other_cpus()
2500Sstevel@tonic-gate {
2510Sstevel@tonic-gate 	int cpuid = CPU->cpu_id;
2520Sstevel@tonic-gate 	cpuset_t xcset;
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 	ASSERT(cpuid < NCPU);
2550Sstevel@tonic-gate 	CPUSET_ALL_BUT(xcset, cpuid);
2560Sstevel@tonic-gate 	xc_capture_cpus(xcset);
2570Sstevel@tonic-gate }
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate void
2600Sstevel@tonic-gate resume_other_cpus()
2610Sstevel@tonic-gate {
2620Sstevel@tonic-gate 	ASSERT(CPU->cpu_id < NCPU);
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 	xc_release_cpus();
2650Sstevel@tonic-gate }
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate extern void	mp_halt(char *);
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate void
2700Sstevel@tonic-gate stop_other_cpus()
2710Sstevel@tonic-gate {
2720Sstevel@tonic-gate 	int cpuid = CPU->cpu_id;
2730Sstevel@tonic-gate 	cpuset_t xcset;
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate 	ASSERT(cpuid < NCPU);
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 	/*
2780Sstevel@tonic-gate 	 * xc_trycall will attempt to make all other CPUs execute mp_halt,
2790Sstevel@tonic-gate 	 * and will return immediately regardless of whether or not it was
2800Sstevel@tonic-gate 	 * able to make them do it.
2810Sstevel@tonic-gate 	 */
2820Sstevel@tonic-gate 	CPUSET_ALL_BUT(xcset, cpuid);
2830Sstevel@tonic-gate 	xc_trycall(NULL, NULL, NULL, xcset, (int (*)())mp_halt);
2840Sstevel@tonic-gate }
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate /*
2870Sstevel@tonic-gate  *	Machine dependent abort sequence handling
2880Sstevel@tonic-gate  */
2890Sstevel@tonic-gate void
2900Sstevel@tonic-gate abort_sequence_enter(char *msg)
2910Sstevel@tonic-gate {
2920Sstevel@tonic-gate 	if (abort_enable == 0) {
2930Sstevel@tonic-gate #ifdef C2_AUDIT
2940Sstevel@tonic-gate 		if (audit_active)
2950Sstevel@tonic-gate 			audit_enterprom(0);
2960Sstevel@tonic-gate #endif /* C2_AUDIT */
2970Sstevel@tonic-gate 		return;
2980Sstevel@tonic-gate 	}
2990Sstevel@tonic-gate #ifdef C2_AUDIT
3000Sstevel@tonic-gate 	if (audit_active)
3010Sstevel@tonic-gate 		audit_enterprom(1);
3020Sstevel@tonic-gate #endif /* C2_AUDIT */
3030Sstevel@tonic-gate 	debug_enter(msg);
3040Sstevel@tonic-gate #ifdef C2_AUDIT
3050Sstevel@tonic-gate 	if (audit_active)
3060Sstevel@tonic-gate 		audit_exitprom(1);
3070Sstevel@tonic-gate #endif /* C2_AUDIT */
3080Sstevel@tonic-gate }
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate /*
3110Sstevel@tonic-gate  * Enter debugger.  Called when the user types ctrl-alt-d or whenever
3120Sstevel@tonic-gate  * code wants to enter the debugger and possibly resume later.
3130Sstevel@tonic-gate  */
3140Sstevel@tonic-gate void
3150Sstevel@tonic-gate debug_enter(
3160Sstevel@tonic-gate 	char	*msg)		/* message to print, possibly NULL */
3170Sstevel@tonic-gate {
3180Sstevel@tonic-gate 	if (dtrace_debugger_init != NULL)
3190Sstevel@tonic-gate 		(*dtrace_debugger_init)();
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate 	if (msg)
3220Sstevel@tonic-gate 		prom_printf("%s\n", msg);
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate 	if (boothowto & RB_DEBUG)
3250Sstevel@tonic-gate 		kdi_dvec_enter();
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 	if (dtrace_debugger_fini != NULL)
3280Sstevel@tonic-gate 		(*dtrace_debugger_fini)();
3290Sstevel@tonic-gate }
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate void
3320Sstevel@tonic-gate reset(void)
3330Sstevel@tonic-gate {
3340Sstevel@tonic-gate 	ushort_t *bios_memchk;
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate 	/*
3370Sstevel@tonic-gate 	 * Can't use psm_map_phys before the hat is initialized.
3380Sstevel@tonic-gate 	 */
3390Sstevel@tonic-gate 	if (khat_running) {
3400Sstevel@tonic-gate 		bios_memchk = (ushort_t *)psm_map_phys(0x472,
3410Sstevel@tonic-gate 		    sizeof (ushort_t), PROT_READ | PROT_WRITE);
3420Sstevel@tonic-gate 		if (bios_memchk)
3430Sstevel@tonic-gate 			*bios_memchk = 0x1234;	/* bios memory check disable */
3440Sstevel@tonic-gate 	}
3450Sstevel@tonic-gate 
3462866Sszhou 	if (ddi_prop_exists(DDI_DEV_T_ANY, ddi_root_node(), 0, "efi-systab"))
3472866Sszhou 		efi_reset();
3480Sstevel@tonic-gate 	pc_reset();
3490Sstevel@tonic-gate 	/*NOTREACHED*/
3500Sstevel@tonic-gate }
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate /*
3530Sstevel@tonic-gate  * Halt the machine and return to the monitor
3540Sstevel@tonic-gate  */
3550Sstevel@tonic-gate void
3560Sstevel@tonic-gate halt(char *s)
3570Sstevel@tonic-gate {
3580Sstevel@tonic-gate 	stop_other_cpus();	/* send stop signal to other CPUs */
3590Sstevel@tonic-gate 	if (s)
3600Sstevel@tonic-gate 		prom_printf("(%s) \n", s);
3610Sstevel@tonic-gate 	prom_exit_to_mon();
3620Sstevel@tonic-gate 	/*NOTREACHED*/
3630Sstevel@tonic-gate }
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate /*
3660Sstevel@tonic-gate  * Enter monitor.  Called via cross-call from stop_other_cpus().
3670Sstevel@tonic-gate  */
3680Sstevel@tonic-gate void
3690Sstevel@tonic-gate mp_halt(char *msg)
3700Sstevel@tonic-gate {
3710Sstevel@tonic-gate 	if (msg)
3720Sstevel@tonic-gate 		prom_printf("%s\n", msg);
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate 	/*CONSTANTCONDITION*/
3750Sstevel@tonic-gate 	while (1)
3760Sstevel@tonic-gate 		;
3770Sstevel@tonic-gate }
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate /*
3800Sstevel@tonic-gate  * Initiate interrupt redistribution.
3810Sstevel@tonic-gate  */
3820Sstevel@tonic-gate void
3830Sstevel@tonic-gate i_ddi_intr_redist_all_cpus()
3840Sstevel@tonic-gate {
3850Sstevel@tonic-gate }
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate /*
3880Sstevel@tonic-gate  * XXX These probably ought to live somewhere else
3890Sstevel@tonic-gate  * XXX They are called from mem.c
3900Sstevel@tonic-gate  */
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate /*
3930Sstevel@tonic-gate  * Convert page frame number to an OBMEM page frame number
3940Sstevel@tonic-gate  * (i.e. put in the type bits -- zero for this implementation)
3950Sstevel@tonic-gate  */
3960Sstevel@tonic-gate pfn_t
3970Sstevel@tonic-gate impl_obmem_pfnum(pfn_t pf)
3980Sstevel@tonic-gate {
3990Sstevel@tonic-gate 	return (pf);
4000Sstevel@tonic-gate }
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate #ifdef	NM_DEBUG
4030Sstevel@tonic-gate int nmi_test = 0;	/* checked in intentry.s during clock int */
4040Sstevel@tonic-gate int nmtest = -1;
4050Sstevel@tonic-gate nmfunc1(arg, rp)
4060Sstevel@tonic-gate int	arg;
4070Sstevel@tonic-gate struct regs *rp;
4080Sstevel@tonic-gate {
4090Sstevel@tonic-gate 	printf("nmi called with arg = %x, regs = %x\n", arg, rp);
4100Sstevel@tonic-gate 	nmtest += 50;
4110Sstevel@tonic-gate 	if (arg == nmtest) {
4120Sstevel@tonic-gate 		printf("ip = %x\n", rp->r_pc);
4130Sstevel@tonic-gate 		return (1);
4140Sstevel@tonic-gate 	}
4150Sstevel@tonic-gate 	return (0);
4160Sstevel@tonic-gate }
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate #endif
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate #include <sys/bootsvcs.h>
4210Sstevel@tonic-gate 
4220Sstevel@tonic-gate /* Hacked up initialization for initial kernel check out is HERE. */
4230Sstevel@tonic-gate /* The basic steps are: */
4240Sstevel@tonic-gate /*	kernel bootfuncs definition/initialization for KADB */
4250Sstevel@tonic-gate /*	kadb bootfuncs pointer initialization */
4260Sstevel@tonic-gate /*	putchar/getchar (interrupts disabled) */
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate /* kadb bootfuncs pointer initialization */
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate int
4310Sstevel@tonic-gate sysp_getchar()
4320Sstevel@tonic-gate {
4330Sstevel@tonic-gate 	int i;
4340Sstevel@tonic-gate 	int s;
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate 	if (cons_polledio == NULL) {
4370Sstevel@tonic-gate 		/* Uh oh */
4380Sstevel@tonic-gate 		prom_printf("getchar called with no console\n");
4390Sstevel@tonic-gate 		for (;;)
4400Sstevel@tonic-gate 			/* LOOP FOREVER */;
4410Sstevel@tonic-gate 	}
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate 	s = clear_int_flag();
4440Sstevel@tonic-gate 	i = cons_polledio->cons_polledio_getchar(
4450Sstevel@tonic-gate 		cons_polledio->cons_polledio_argument);
4460Sstevel@tonic-gate 	restore_int_flag(s);
4470Sstevel@tonic-gate 	return (i);
4480Sstevel@tonic-gate }
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate void
4510Sstevel@tonic-gate sysp_putchar(int c)
4520Sstevel@tonic-gate {
4530Sstevel@tonic-gate 	int s;
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate 	/*
4560Sstevel@tonic-gate 	 * We have no alternative but to drop the output on the floor.
4570Sstevel@tonic-gate 	 */
4581253Slq150181 	if (cons_polledio == NULL ||
4591253Slq150181 	    cons_polledio->cons_polledio_putchar == NULL)
4600Sstevel@tonic-gate 		return;
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate 	s = clear_int_flag();
4630Sstevel@tonic-gate 	cons_polledio->cons_polledio_putchar(
4640Sstevel@tonic-gate 		cons_polledio->cons_polledio_argument, c);
4650Sstevel@tonic-gate 	restore_int_flag(s);
4660Sstevel@tonic-gate }
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate int
4690Sstevel@tonic-gate sysp_ischar()
4700Sstevel@tonic-gate {
4710Sstevel@tonic-gate 	int i;
4720Sstevel@tonic-gate 	int s;
4730Sstevel@tonic-gate 
4741253Slq150181 	if (cons_polledio == NULL ||
4751253Slq150181 	    cons_polledio->cons_polledio_ischar == NULL)
4760Sstevel@tonic-gate 		return (0);
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate 	s = clear_int_flag();
4790Sstevel@tonic-gate 	i = cons_polledio->cons_polledio_ischar(
4800Sstevel@tonic-gate 		cons_polledio->cons_polledio_argument);
4810Sstevel@tonic-gate 	restore_int_flag(s);
4820Sstevel@tonic-gate 	return (i);
4830Sstevel@tonic-gate }
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate int
4860Sstevel@tonic-gate goany(void)
4870Sstevel@tonic-gate {
4880Sstevel@tonic-gate 	prom_printf("Type any key to continue ");
4890Sstevel@tonic-gate 	(void) prom_getchar();
4900Sstevel@tonic-gate 	prom_printf("\n");
4910Sstevel@tonic-gate 	return (1);
4920Sstevel@tonic-gate }
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate static struct boot_syscalls kern_sysp = {
4950Sstevel@tonic-gate 	sysp_getchar,	/*	unchar	(*getchar)();	7  */
4960Sstevel@tonic-gate 	sysp_putchar,	/*	int	(*putchar)();	8  */
4970Sstevel@tonic-gate 	sysp_ischar,	/*	int	(*ischar)();	9  */
4980Sstevel@tonic-gate };
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate void
5010Sstevel@tonic-gate kadb_uses_kernel()
5020Sstevel@tonic-gate {
5030Sstevel@tonic-gate 	/*
5040Sstevel@tonic-gate 	 * This routine is now totally misnamed, since it does not in fact
5050Sstevel@tonic-gate 	 * control kadb's I/O; it only controls the kernel's prom_* I/O.
5060Sstevel@tonic-gate 	 */
5070Sstevel@tonic-gate 	sysp = &kern_sysp;
5080Sstevel@tonic-gate }
5090Sstevel@tonic-gate 
5100Sstevel@tonic-gate /*
5110Sstevel@tonic-gate  *	the interface to the outside world
5120Sstevel@tonic-gate  */
5130Sstevel@tonic-gate 
5140Sstevel@tonic-gate /*
5150Sstevel@tonic-gate  * poll_port -- wait for a register to achieve a
5160Sstevel@tonic-gate  *		specific state.  Arguments are a mask of bits we care about,
5170Sstevel@tonic-gate  *		and two sub-masks.  To return normally, all the bits in the
5180Sstevel@tonic-gate  *		first sub-mask must be ON, all the bits in the second sub-
5190Sstevel@tonic-gate  *		mask must be OFF.  If about seconds pass without the register
5200Sstevel@tonic-gate  *		achieving the desired bit configuration, we return 1, else
5210Sstevel@tonic-gate  *		0.
5220Sstevel@tonic-gate  */
5230Sstevel@tonic-gate int
5240Sstevel@tonic-gate poll_port(ushort_t port, ushort_t mask, ushort_t onbits, ushort_t offbits)
5250Sstevel@tonic-gate {
5260Sstevel@tonic-gate 	int i;
5270Sstevel@tonic-gate 	ushort_t maskval;
5280Sstevel@tonic-gate 
5290Sstevel@tonic-gate 	for (i = 500000; i; i--) {
5300Sstevel@tonic-gate 		maskval = inb(port) & mask;
5310Sstevel@tonic-gate 		if (((maskval & onbits) == onbits) &&
5320Sstevel@tonic-gate 			((maskval & offbits) == 0))
5330Sstevel@tonic-gate 			return (0);
5340Sstevel@tonic-gate 		drv_usecwait(10);
5350Sstevel@tonic-gate 	}
5360Sstevel@tonic-gate 	return (1);
5370Sstevel@tonic-gate }
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate /*
5400Sstevel@tonic-gate  * set_idle_cpu is called from idle() when a CPU becomes idle.
5410Sstevel@tonic-gate  */
5420Sstevel@tonic-gate /*LINTED: static unused */
5430Sstevel@tonic-gate static uint_t last_idle_cpu;
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate /*ARGSUSED*/
5460Sstevel@tonic-gate void
5470Sstevel@tonic-gate set_idle_cpu(int cpun)
5480Sstevel@tonic-gate {
5490Sstevel@tonic-gate 	last_idle_cpu = cpun;
5500Sstevel@tonic-gate 	(*psm_set_idle_cpuf)(cpun);
5510Sstevel@tonic-gate }
5520Sstevel@tonic-gate 
5530Sstevel@tonic-gate /*
5540Sstevel@tonic-gate  * unset_idle_cpu is called from idle() when a CPU is no longer idle.
5550Sstevel@tonic-gate  */
5560Sstevel@tonic-gate /*ARGSUSED*/
5570Sstevel@tonic-gate void
5580Sstevel@tonic-gate unset_idle_cpu(int cpun)
5590Sstevel@tonic-gate {
5600Sstevel@tonic-gate 	(*psm_unset_idle_cpuf)(cpun);
5610Sstevel@tonic-gate }
5620Sstevel@tonic-gate 
5630Sstevel@tonic-gate /*
5640Sstevel@tonic-gate  * This routine is almost correct now, but not quite.  It still needs the
5650Sstevel@tonic-gate  * equivalent concept of "hres_last_tick", just like on the sparc side.
5660Sstevel@tonic-gate  * The idea is to take a snapshot of the hi-res timer while doing the
5670Sstevel@tonic-gate  * hrestime_adj updates under hres_lock in locore, so that the small
5680Sstevel@tonic-gate  * interval between interrupt assertion and interrupt processing is
5690Sstevel@tonic-gate  * accounted for correctly.  Once we have this, the code below should
5700Sstevel@tonic-gate  * be modified to subtract off hres_last_tick rather than hrtime_base.
5710Sstevel@tonic-gate  *
5720Sstevel@tonic-gate  * I'd have done this myself, but I don't have source to all of the
5730Sstevel@tonic-gate  * vendor-specific hi-res timer routines (grrr...).  The generic hook I
5740Sstevel@tonic-gate  * need is something like "gethrtime_unlocked()", which would be just like
5750Sstevel@tonic-gate  * gethrtime() but would assume that you're already holding CLOCK_LOCK().
5760Sstevel@tonic-gate  * This is what the GET_HRTIME() macro is for on sparc (although it also
5770Sstevel@tonic-gate  * serves the function of making time available without a function call
5780Sstevel@tonic-gate  * so you don't take a register window overflow while traps are disabled).
5790Sstevel@tonic-gate  */
5800Sstevel@tonic-gate void
5810Sstevel@tonic-gate pc_gethrestime(timestruc_t *tp)
5820Sstevel@tonic-gate {
5830Sstevel@tonic-gate 	int lock_prev;
5840Sstevel@tonic-gate 	timestruc_t now;
5850Sstevel@tonic-gate 	int nslt;		/* nsec since last tick */
5860Sstevel@tonic-gate 	int adj;		/* amount of adjustment to apply */
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate loop:
5890Sstevel@tonic-gate 	lock_prev = hres_lock;
5900Sstevel@tonic-gate 	now = hrestime;
5910Sstevel@tonic-gate 	nslt = (int)(gethrtime() - hres_last_tick);
5920Sstevel@tonic-gate 	if (nslt < 0) {
5930Sstevel@tonic-gate 		/*
5940Sstevel@tonic-gate 		 * nslt < 0 means a tick came between sampling
5950Sstevel@tonic-gate 		 * gethrtime() and hres_last_tick; restart the loop
5960Sstevel@tonic-gate 		 */
5970Sstevel@tonic-gate 
5980Sstevel@tonic-gate 		goto loop;
5990Sstevel@tonic-gate 	}
6000Sstevel@tonic-gate 	now.tv_nsec += nslt;
6010Sstevel@tonic-gate 	if (hrestime_adj != 0) {
6020Sstevel@tonic-gate 		if (hrestime_adj > 0) {
6030Sstevel@tonic-gate 			adj = (nslt >> ADJ_SHIFT);
6040Sstevel@tonic-gate 			if (adj > hrestime_adj)
6050Sstevel@tonic-gate 				adj = (int)hrestime_adj;
6060Sstevel@tonic-gate 		} else {
6070Sstevel@tonic-gate 			adj = -(nslt >> ADJ_SHIFT);
6080Sstevel@tonic-gate 			if (adj < hrestime_adj)
6090Sstevel@tonic-gate 				adj = (int)hrestime_adj;
6100Sstevel@tonic-gate 		}
6110Sstevel@tonic-gate 		now.tv_nsec += adj;
6120Sstevel@tonic-gate 	}
6130Sstevel@tonic-gate 	while ((unsigned long)now.tv_nsec >= NANOSEC) {
6140Sstevel@tonic-gate 
6150Sstevel@tonic-gate 		/*
6160Sstevel@tonic-gate 		 * We might have a large adjustment or have been in the
6170Sstevel@tonic-gate 		 * debugger for a long time; take care of (at most) four
6180Sstevel@tonic-gate 		 * of those missed seconds (tv_nsec is 32 bits, so
6190Sstevel@tonic-gate 		 * anything >4s will be wrapping around).  However,
6200Sstevel@tonic-gate 		 * anything more than 2 seconds out of sync will trigger
6210Sstevel@tonic-gate 		 * timedelta from clock() to go correct the time anyway,
6220Sstevel@tonic-gate 		 * so do what we can, and let the big crowbar do the
6230Sstevel@tonic-gate 		 * rest.  A similar correction while loop exists inside
6240Sstevel@tonic-gate 		 * hres_tick(); in all cases we'd like tv_nsec to
6250Sstevel@tonic-gate 		 * satisfy 0 <= tv_nsec < NANOSEC to avoid confusing
6260Sstevel@tonic-gate 		 * user processes, but if tv_sec's a little behind for a
6270Sstevel@tonic-gate 		 * little while, that's OK; time still monotonically
6280Sstevel@tonic-gate 		 * increases.
6290Sstevel@tonic-gate 		 */
6300Sstevel@tonic-gate 
6310Sstevel@tonic-gate 		now.tv_nsec -= NANOSEC;
6320Sstevel@tonic-gate 		now.tv_sec++;
6330Sstevel@tonic-gate 	}
6340Sstevel@tonic-gate 	if ((hres_lock & ~1) != lock_prev)
6350Sstevel@tonic-gate 		goto loop;
6360Sstevel@tonic-gate 
6370Sstevel@tonic-gate 	*tp = now;
6380Sstevel@tonic-gate }
6390Sstevel@tonic-gate 
6400Sstevel@tonic-gate void
6410Sstevel@tonic-gate gethrestime_lasttick(timespec_t *tp)
6420Sstevel@tonic-gate {
6430Sstevel@tonic-gate 	int s;
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate 	s = hr_clock_lock();
6460Sstevel@tonic-gate 	*tp = hrestime;
6470Sstevel@tonic-gate 	hr_clock_unlock(s);
6480Sstevel@tonic-gate }
6490Sstevel@tonic-gate 
6500Sstevel@tonic-gate time_t
6510Sstevel@tonic-gate gethrestime_sec(void)
6520Sstevel@tonic-gate {
6530Sstevel@tonic-gate 	timestruc_t now;
6540Sstevel@tonic-gate 
6550Sstevel@tonic-gate 	gethrestime(&now);
6560Sstevel@tonic-gate 	return (now.tv_sec);
6570Sstevel@tonic-gate }
6580Sstevel@tonic-gate 
6590Sstevel@tonic-gate /*
6600Sstevel@tonic-gate  * Initialize a kernel thread's stack
6610Sstevel@tonic-gate  */
6620Sstevel@tonic-gate 
6630Sstevel@tonic-gate caddr_t
6640Sstevel@tonic-gate thread_stk_init(caddr_t stk)
6650Sstevel@tonic-gate {
6660Sstevel@tonic-gate 	ASSERT(((uintptr_t)stk & (STACK_ALIGN - 1)) == 0);
6670Sstevel@tonic-gate 	return (stk - SA(MINFRAME));
6680Sstevel@tonic-gate }
6690Sstevel@tonic-gate 
6700Sstevel@tonic-gate /*
6710Sstevel@tonic-gate  * Initialize lwp's kernel stack.
6720Sstevel@tonic-gate  */
6730Sstevel@tonic-gate 
6740Sstevel@tonic-gate #ifdef TRAPTRACE
6750Sstevel@tonic-gate /*
6760Sstevel@tonic-gate  * There's a tricky interdependency here between use of sysenter and
6770Sstevel@tonic-gate  * TRAPTRACE which needs recording to avoid future confusion (this is
6780Sstevel@tonic-gate  * about the third time I've re-figured this out ..)
6790Sstevel@tonic-gate  *
6800Sstevel@tonic-gate  * Here's how debugging lcall works with TRAPTRACE.
6810Sstevel@tonic-gate  *
6820Sstevel@tonic-gate  * 1 We're in userland with a breakpoint on the lcall instruction.
6830Sstevel@tonic-gate  * 2 We execute the instruction - the instruction pushes the userland
6840Sstevel@tonic-gate  *   %ss, %esp, %efl, %cs, %eip on the stack and zips into the kernel
6850Sstevel@tonic-gate  *   via the call gate.
6860Sstevel@tonic-gate  * 3 The hardware raises a debug trap in kernel mode, the hardware
6870Sstevel@tonic-gate  *   pushes %efl, %cs, %eip and gets to dbgtrap via the idt.
6880Sstevel@tonic-gate  * 4 dbgtrap pushes the error code and trapno and calls cmntrap
6890Sstevel@tonic-gate  * 5 cmntrap finishes building a trap frame
6900Sstevel@tonic-gate  * 6 The TRACE_REGS macros in cmntrap copy a REGSIZE worth chunk
6910Sstevel@tonic-gate  *   off the stack into the traptrace buffer.
6920Sstevel@tonic-gate  *
6930Sstevel@tonic-gate  * This means that the traptrace buffer contains the wrong values in
6940Sstevel@tonic-gate  * %esp and %ss, but everything else in there is correct.
6950Sstevel@tonic-gate  *
6960Sstevel@tonic-gate  * Here's how debugging sysenter works with TRAPTRACE.
6970Sstevel@tonic-gate  *
6980Sstevel@tonic-gate  * a We're in userland with a breakpoint on the sysenter instruction.
6990Sstevel@tonic-gate  * b We execute the instruction - the instruction pushes -nothing-
7000Sstevel@tonic-gate  *   on the stack, but sets %cs, %eip, %ss, %esp to prearranged
7010Sstevel@tonic-gate  *   values to take us to sys_sysenter, at the top of the lwp's
7020Sstevel@tonic-gate  *   stack.
7030Sstevel@tonic-gate  * c goto 3
7040Sstevel@tonic-gate  *
7050Sstevel@tonic-gate  * At this point, because we got into the kernel without the requisite
7060Sstevel@tonic-gate  * five pushes on the stack, if we didn't make extra room, we'd
7070Sstevel@tonic-gate  * end up with the TRACE_REGS macro fetching the saved %ss and %esp
7080Sstevel@tonic-gate  * values from negative (unmapped) stack addresses -- which really bites.
7090Sstevel@tonic-gate  * That's why we do the '-= 8' below.
7100Sstevel@tonic-gate  *
7110Sstevel@tonic-gate  * XXX	Note that reading "up" lwp0's stack works because t0 is declared
7120Sstevel@tonic-gate  *	right next to t0stack in locore.s
7130Sstevel@tonic-gate  */
7140Sstevel@tonic-gate #endif
7150Sstevel@tonic-gate 
7160Sstevel@tonic-gate caddr_t
7170Sstevel@tonic-gate lwp_stk_init(klwp_t *lwp, caddr_t stk)
7180Sstevel@tonic-gate {
7190Sstevel@tonic-gate 	caddr_t oldstk;
7200Sstevel@tonic-gate 	struct pcb *pcb = &lwp->lwp_pcb;
7210Sstevel@tonic-gate 
7220Sstevel@tonic-gate 	oldstk = stk;
7230Sstevel@tonic-gate 	stk -= SA(sizeof (struct regs) + SA(MINFRAME));
7240Sstevel@tonic-gate #ifdef TRAPTRACE
7250Sstevel@tonic-gate 	stk -= 2 * sizeof (greg_t); /* space for phony %ss:%sp (see above) */
7260Sstevel@tonic-gate #endif
7270Sstevel@tonic-gate 	stk = (caddr_t)((uintptr_t)stk & ~(STACK_ALIGN - 1ul));
7280Sstevel@tonic-gate 	bzero(stk, oldstk - stk);
7290Sstevel@tonic-gate 	lwp->lwp_regs = (void *)(stk + SA(MINFRAME));
7300Sstevel@tonic-gate 
7310Sstevel@tonic-gate 	/*
7320Sstevel@tonic-gate 	 * Arrange that the virtualized %fs and %gs GDT descriptors
7330Sstevel@tonic-gate 	 * have a well-defined initial state (present, ring 3
7340Sstevel@tonic-gate 	 * and of type data).
7350Sstevel@tonic-gate 	 */
7360Sstevel@tonic-gate #if defined(__amd64)
7370Sstevel@tonic-gate 	if (lwp_getdatamodel(lwp) == DATAMODEL_NATIVE)
7380Sstevel@tonic-gate 		pcb->pcb_fsdesc = pcb->pcb_gsdesc = zero_udesc;
7390Sstevel@tonic-gate 	else
7400Sstevel@tonic-gate 		pcb->pcb_fsdesc = pcb->pcb_gsdesc = zero_u32desc;
7410Sstevel@tonic-gate #elif defined(__i386)
7420Sstevel@tonic-gate 	pcb->pcb_fsdesc = pcb->pcb_gsdesc = zero_udesc;
7430Sstevel@tonic-gate #endif	/* __i386 */
7440Sstevel@tonic-gate 	lwp_installctx(lwp);
7450Sstevel@tonic-gate 	return (stk);
7460Sstevel@tonic-gate }
7470Sstevel@tonic-gate 
7480Sstevel@tonic-gate /*ARGSUSED*/
7490Sstevel@tonic-gate void
7500Sstevel@tonic-gate lwp_stk_fini(klwp_t *lwp)
7510Sstevel@tonic-gate {}
7520Sstevel@tonic-gate 
7530Sstevel@tonic-gate /*
7541389Sdmick  * If we're not the panic CPU, we wait in panic_idle for reboot.
7550Sstevel@tonic-gate  */
7560Sstevel@tonic-gate static void
7570Sstevel@tonic-gate panic_idle(void)
7580Sstevel@tonic-gate {
7590Sstevel@tonic-gate 	splx(ipltospl(CLOCK_LEVEL));
7600Sstevel@tonic-gate 	(void) setjmp(&curthread->t_pcb);
7610Sstevel@tonic-gate 
7620Sstevel@tonic-gate 	for (;;);
7630Sstevel@tonic-gate }
7640Sstevel@tonic-gate 
7650Sstevel@tonic-gate /*
7660Sstevel@tonic-gate  * Stop the other CPUs by cross-calling them and forcing them to enter
7670Sstevel@tonic-gate  * the panic_idle() loop above.
7680Sstevel@tonic-gate  */
7690Sstevel@tonic-gate /*ARGSUSED*/
7700Sstevel@tonic-gate void
7710Sstevel@tonic-gate panic_stopcpus(cpu_t *cp, kthread_t *t, int spl)
7720Sstevel@tonic-gate {
7730Sstevel@tonic-gate 	processorid_t i;
7740Sstevel@tonic-gate 	cpuset_t xcset;
7750Sstevel@tonic-gate 
7760Sstevel@tonic-gate 	(void) splzs();
7770Sstevel@tonic-gate 
7780Sstevel@tonic-gate 	CPUSET_ALL_BUT(xcset, cp->cpu_id);
7790Sstevel@tonic-gate 	xc_trycall(NULL, NULL, NULL, xcset, (int (*)())panic_idle);
7800Sstevel@tonic-gate 
7810Sstevel@tonic-gate 	for (i = 0; i < NCPU; i++) {
7820Sstevel@tonic-gate 		if (i != cp->cpu_id && cpu[i] != NULL &&
7830Sstevel@tonic-gate 		    (cpu[i]->cpu_flags & CPU_EXISTS))
7840Sstevel@tonic-gate 			cpu[i]->cpu_flags |= CPU_QUIESCED;
7850Sstevel@tonic-gate 	}
7860Sstevel@tonic-gate }
7870Sstevel@tonic-gate 
7880Sstevel@tonic-gate /*
7890Sstevel@tonic-gate  * Platform callback following each entry to panicsys().
7900Sstevel@tonic-gate  */
7910Sstevel@tonic-gate /*ARGSUSED*/
7920Sstevel@tonic-gate void
7930Sstevel@tonic-gate panic_enter_hw(int spl)
7940Sstevel@tonic-gate {
7950Sstevel@tonic-gate 	/* Nothing to do here */
7960Sstevel@tonic-gate }
7970Sstevel@tonic-gate 
7980Sstevel@tonic-gate /*
7990Sstevel@tonic-gate  * Platform-specific code to execute after panicstr is set: we invoke
8000Sstevel@tonic-gate  * the PSM entry point to indicate that a panic has occurred.
8010Sstevel@tonic-gate  */
8020Sstevel@tonic-gate /*ARGSUSED*/
8030Sstevel@tonic-gate void
8040Sstevel@tonic-gate panic_quiesce_hw(panic_data_t *pdp)
8050Sstevel@tonic-gate {
8060Sstevel@tonic-gate 	psm_notifyf(PSM_PANIC_ENTER);
8070Sstevel@tonic-gate 
8080Sstevel@tonic-gate #ifdef	TRAPTRACE
8090Sstevel@tonic-gate 	/*
8100Sstevel@tonic-gate 	 * Turn off TRAPTRACE
8110Sstevel@tonic-gate 	 */
8120Sstevel@tonic-gate 	TRAPTRACE_FREEZE;
8130Sstevel@tonic-gate #endif	/* TRAPTRACE */
8140Sstevel@tonic-gate }
8150Sstevel@tonic-gate 
8160Sstevel@tonic-gate /*
8170Sstevel@tonic-gate  * Platform callback prior to writing crash dump.
8180Sstevel@tonic-gate  */
8190Sstevel@tonic-gate /*ARGSUSED*/
8200Sstevel@tonic-gate void
8210Sstevel@tonic-gate panic_dump_hw(int spl)
8220Sstevel@tonic-gate {
8230Sstevel@tonic-gate 	/* Nothing to do here */
8240Sstevel@tonic-gate }
8250Sstevel@tonic-gate 
8260Sstevel@tonic-gate /*ARGSUSED*/
8270Sstevel@tonic-gate void
8280Sstevel@tonic-gate plat_tod_fault(enum tod_fault_type tod_bad)
8290Sstevel@tonic-gate {
8300Sstevel@tonic-gate }
8310Sstevel@tonic-gate 
8320Sstevel@tonic-gate /*ARGSUSED*/
8330Sstevel@tonic-gate int
8340Sstevel@tonic-gate blacklist(int cmd, const char *scheme, nvlist_t *fmri, const char *class)
8350Sstevel@tonic-gate {
8360Sstevel@tonic-gate 	return (ENOTSUP);
8370Sstevel@tonic-gate }
8380Sstevel@tonic-gate 
8390Sstevel@tonic-gate /*
8400Sstevel@tonic-gate  * The underlying console output routines are protected by raising IPL in case
8410Sstevel@tonic-gate  * we are still calling into the early boot services.  Once we start calling
8420Sstevel@tonic-gate  * the kernel console emulator, it will disable interrupts completely during
8430Sstevel@tonic-gate  * character rendering (see sysp_putchar, for example).  Refer to the comments
8440Sstevel@tonic-gate  * and code in common/os/console.c for more information on these callbacks.
8450Sstevel@tonic-gate  */
8460Sstevel@tonic-gate /*ARGSUSED*/
8470Sstevel@tonic-gate int
8480Sstevel@tonic-gate console_enter(int busy)
8490Sstevel@tonic-gate {
8500Sstevel@tonic-gate 	return (splzs());
8510Sstevel@tonic-gate }
8520Sstevel@tonic-gate 
8530Sstevel@tonic-gate /*ARGSUSED*/
8540Sstevel@tonic-gate void
8550Sstevel@tonic-gate console_exit(int busy, int spl)
8560Sstevel@tonic-gate {
8570Sstevel@tonic-gate 	splx(spl);
8580Sstevel@tonic-gate }
8590Sstevel@tonic-gate 
8600Sstevel@tonic-gate /*
8610Sstevel@tonic-gate  * Allocate a region of virtual address space, unmapped.
8620Sstevel@tonic-gate  * Stubbed out except on sparc, at least for now.
8630Sstevel@tonic-gate  */
8640Sstevel@tonic-gate /*ARGSUSED*/
8650Sstevel@tonic-gate void *
8660Sstevel@tonic-gate boot_virt_alloc(void *addr, size_t size)
8670Sstevel@tonic-gate {
8680Sstevel@tonic-gate 	return (addr);
8690Sstevel@tonic-gate }
8700Sstevel@tonic-gate 
8710Sstevel@tonic-gate volatile unsigned long	tenmicrodata;
8720Sstevel@tonic-gate 
8730Sstevel@tonic-gate void
8740Sstevel@tonic-gate tenmicrosec(void)
8750Sstevel@tonic-gate {
8760Sstevel@tonic-gate 	extern int	tsc_gethrtime_initted;
8770Sstevel@tonic-gate 	int		i;
8780Sstevel@tonic-gate 
8790Sstevel@tonic-gate 	if (tsc_gethrtime_initted) {
8800Sstevel@tonic-gate 		hrtime_t start, end;
8810Sstevel@tonic-gate 		start = end =  gethrtime();
8820Sstevel@tonic-gate 		while ((end - start) < (10 * (NANOSEC / MICROSEC))) {
8830Sstevel@tonic-gate 			SMT_PAUSE();
8840Sstevel@tonic-gate 			end = gethrtime();
8850Sstevel@tonic-gate 		}
8860Sstevel@tonic-gate 	} else {
8870Sstevel@tonic-gate 		/*
8880Sstevel@tonic-gate 		 * Artificial loop to induce delay.
8890Sstevel@tonic-gate 		 */
8900Sstevel@tonic-gate 		for (i = 0; i < microdata; i++)
8910Sstevel@tonic-gate 			tenmicrodata = microdata;
8920Sstevel@tonic-gate 	}
8930Sstevel@tonic-gate }
894590Sesolom 
895590Sesolom /*
896590Sesolom  * get_cpu_mstate() is passed an array of timestamps, NCMSTATES
897590Sesolom  * long, and it fills in the array with the time spent on cpu in
898590Sesolom  * each of the mstates, where time is returned in nsec.
899590Sesolom  *
900590Sesolom  * No guarantee is made that the returned values in times[] will
901590Sesolom  * monotonically increase on sequential calls, although this will
902590Sesolom  * be true in the long run. Any such guarantee must be handled by
903590Sesolom  * the caller, if needed. This can happen if we fail to account
904590Sesolom  * for elapsed time due to a generation counter conflict, yet we
905590Sesolom  * did account for it on a prior call (see below).
906590Sesolom  *
907590Sesolom  * The complication is that the cpu in question may be updating
908590Sesolom  * its microstate at the same time that we are reading it.
909590Sesolom  * Because the microstate is only updated when the CPU's state
910590Sesolom  * changes, the values in cpu_intracct[] can be indefinitely out
911590Sesolom  * of date. To determine true current values, it is necessary to
912590Sesolom  * compare the current time with cpu_mstate_start, and add the
913590Sesolom  * difference to times[cpu_mstate].
914590Sesolom  *
915590Sesolom  * This can be a problem if those values are changing out from
916590Sesolom  * under us. Because the code path in new_cpu_mstate() is
917590Sesolom  * performance critical, we have not added a lock to it. Instead,
918590Sesolom  * we have added a generation counter. Before beginning
919590Sesolom  * modifications, the counter is set to 0. After modifications,
920590Sesolom  * it is set to the old value plus one.
921590Sesolom  *
922590Sesolom  * get_cpu_mstate() will not consider the values of cpu_mstate
923590Sesolom  * and cpu_mstate_start to be usable unless the value of
924590Sesolom  * cpu_mstate_gen is both non-zero and unchanged, both before and
925590Sesolom  * after reading the mstate information. Note that we must
926590Sesolom  * protect against out-of-order loads around accesses to the
927590Sesolom  * generation counter. Also, this is a best effort approach in
928590Sesolom  * that we do not retry should the counter be found to have
929590Sesolom  * changed.
930590Sesolom  *
931590Sesolom  * cpu_intracct[] is used to identify time spent in each CPU
932590Sesolom  * mstate while handling interrupts. Such time should be reported
933590Sesolom  * against system time, and so is subtracted out from its
934590Sesolom  * corresponding cpu_acct[] time and added to
935590Sesolom  * cpu_acct[CMS_SYSTEM].
936590Sesolom  */
937590Sesolom 
938590Sesolom void
939590Sesolom get_cpu_mstate(cpu_t *cpu, hrtime_t *times)
940590Sesolom {
941590Sesolom 	int i;
942590Sesolom 	hrtime_t now, start;
943590Sesolom 	uint16_t gen;
944590Sesolom 	uint16_t state;
945590Sesolom 	hrtime_t intracct[NCMSTATES];
946590Sesolom 
947590Sesolom 	/*
948590Sesolom 	 * Load all volatile state under the protection of membar.
949590Sesolom 	 * cpu_acct[cpu_mstate] must be loaded to avoid double counting
950590Sesolom 	 * of (now - cpu_mstate_start) by a change in CPU mstate that
951590Sesolom 	 * arrives after we make our last check of cpu_mstate_gen.
952590Sesolom 	 */
953590Sesolom 
954590Sesolom 	now = gethrtime_unscaled();
955590Sesolom 	gen = cpu->cpu_mstate_gen;
956590Sesolom 
957590Sesolom 	membar_consumer();	/* guarantee load ordering */
958590Sesolom 	start = cpu->cpu_mstate_start;
959590Sesolom 	state = cpu->cpu_mstate;
960590Sesolom 	for (i = 0; i < NCMSTATES; i++) {
961590Sesolom 		intracct[i] = cpu->cpu_intracct[i];
962590Sesolom 		times[i] = cpu->cpu_acct[i];
963590Sesolom 	}
964590Sesolom 	membar_consumer();	/* guarantee load ordering */
965590Sesolom 
966590Sesolom 	if (gen != 0 && gen == cpu->cpu_mstate_gen && now > start)
967590Sesolom 		times[state] += now - start;
968590Sesolom 
969590Sesolom 	for (i = 0; i < NCMSTATES; i++) {
970590Sesolom 		if (i == CMS_SYSTEM)
971590Sesolom 			continue;
972590Sesolom 		times[i] -= intracct[i];
973590Sesolom 		if (times[i] < 0) {
974590Sesolom 			intracct[i] += times[i];
975590Sesolom 			times[i] = 0;
976590Sesolom 		}
977590Sesolom 		times[CMS_SYSTEM] += intracct[i];
978590Sesolom 		scalehrtime(&times[i]);
979590Sesolom 	}
980590Sesolom 	scalehrtime(&times[CMS_SYSTEM]);
981590Sesolom }
982