xref: /onnv-gate/usr/src/uts/i86pc/os/machdep.c (revision 12931:2acab560865a)
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 /*
2312825SJimmy.Vetayases@oracle.com  * Copyright (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate  */
2512004Sjiang.liu@intel.com /*
2612004Sjiang.liu@intel.com  * Copyright (c) 2010, Intel Corporation.
2712004Sjiang.liu@intel.com  * All rights reserved.
2812004Sjiang.liu@intel.com  */
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #include <sys/types.h>
310Sstevel@tonic-gate #include <sys/t_lock.h>
320Sstevel@tonic-gate #include <sys/param.h>
333939Ssethg #include <sys/segments.h>
340Sstevel@tonic-gate #include <sys/sysmacros.h>
350Sstevel@tonic-gate #include <sys/signal.h>
360Sstevel@tonic-gate #include <sys/systm.h>
370Sstevel@tonic-gate #include <sys/user.h>
380Sstevel@tonic-gate #include <sys/mman.h>
390Sstevel@tonic-gate #include <sys/vm.h>
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #include <sys/disp.h>
420Sstevel@tonic-gate #include <sys/class.h>
430Sstevel@tonic-gate 
440Sstevel@tonic-gate #include <sys/proc.h>
450Sstevel@tonic-gate #include <sys/buf.h>
460Sstevel@tonic-gate #include <sys/kmem.h>
470Sstevel@tonic-gate 
480Sstevel@tonic-gate #include <sys/reboot.h>
490Sstevel@tonic-gate #include <sys/uadmin.h>
500Sstevel@tonic-gate #include <sys/callb.h>
510Sstevel@tonic-gate 
520Sstevel@tonic-gate #include <sys/cred.h>
530Sstevel@tonic-gate #include <sys/vnode.h>
540Sstevel@tonic-gate #include <sys/file.h>
550Sstevel@tonic-gate 
560Sstevel@tonic-gate #include <sys/procfs.h>
570Sstevel@tonic-gate #include <sys/acct.h>
580Sstevel@tonic-gate 
590Sstevel@tonic-gate #include <sys/vfs.h>
600Sstevel@tonic-gate #include <sys/dnlc.h>
610Sstevel@tonic-gate #include <sys/var.h>
620Sstevel@tonic-gate #include <sys/cmn_err.h>
630Sstevel@tonic-gate #include <sys/utsname.h>
640Sstevel@tonic-gate #include <sys/debug.h>
650Sstevel@tonic-gate 
660Sstevel@tonic-gate #include <sys/dumphdr.h>
670Sstevel@tonic-gate #include <sys/bootconf.h>
680Sstevel@tonic-gate #include <sys/varargs.h>
690Sstevel@tonic-gate #include <sys/promif.h>
700Sstevel@tonic-gate #include <sys/modctl.h>
710Sstevel@tonic-gate 
720Sstevel@tonic-gate #include <sys/consdev.h>
730Sstevel@tonic-gate #include <sys/frame.h>
740Sstevel@tonic-gate 
750Sstevel@tonic-gate #include <sys/sunddi.h>
760Sstevel@tonic-gate #include <sys/ddidmareq.h>
770Sstevel@tonic-gate #include <sys/psw.h>
780Sstevel@tonic-gate #include <sys/regset.h>
790Sstevel@tonic-gate #include <sys/privregs.h>
800Sstevel@tonic-gate #include <sys/clock.h>
810Sstevel@tonic-gate #include <sys/tss.h>
820Sstevel@tonic-gate #include <sys/cpu.h>
830Sstevel@tonic-gate #include <sys/stack.h>
840Sstevel@tonic-gate #include <sys/trap.h>
850Sstevel@tonic-gate #include <sys/pic.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>
1183446Smrj #include <sys/mem.h>
1193446Smrj #include <sys/dumphdr.h>
1203446Smrj #include <sys/compress.h>
1217532SSean.Ye@Sun.COM #include <sys/cpu_module.h>
1225084Sjohnlev #if defined(__xpv)
1235084Sjohnlev #include <sys/hypervisor.h>
1245084Sjohnlev #include <sys/xpv_panic.h>
1255084Sjohnlev #endif
1260Sstevel@tonic-gate 
1277656SSherry.Moore@Sun.COM #include <sys/fastboot.h>
1287656SSherry.Moore@Sun.COM #include <sys/machelf.h>
1297656SSherry.Moore@Sun.COM #include <sys/kobj.h>
1307656SSherry.Moore@Sun.COM #include <sys/multiboot.h>
1317656SSherry.Moore@Sun.COM 
1320Sstevel@tonic-gate #ifdef	TRAPTRACE
1330Sstevel@tonic-gate #include <sys/traptrace.h>
1340Sstevel@tonic-gate #endif	/* TRAPTRACE */
1350Sstevel@tonic-gate 
13611861SMarek.Pospisil@Sun.COM #include <c2/audit.h>
13711066Srafael.vanoni@sun.com #include <sys/clock_impl.h>
13811066Srafael.vanoni@sun.com 
1390Sstevel@tonic-gate extern void audit_enterprom(int);
1400Sstevel@tonic-gate extern void audit_exitprom(int);
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate /*
14312683SJimmy.Vetayases@oracle.com  * Tunable to enable apix PSM; if set to 0, pcplusmp PSM will be used.
14412683SJimmy.Vetayases@oracle.com  */
14512683SJimmy.Vetayases@oracle.com int	apix_enable = 1;
14612683SJimmy.Vetayases@oracle.com 
14712683SJimmy.Vetayases@oracle.com int	apic_nvidia_io_max = 0;	/* no. of NVIDIA i/o apics */
14812683SJimmy.Vetayases@oracle.com 
14912683SJimmy.Vetayases@oracle.com /*
1506681Sjohnlev  * Occassionally the kernel knows better whether to power-off or reboot.
1516681Sjohnlev  */
1526681Sjohnlev int force_shutdown_method = AD_UNKNOWN;
1536681Sjohnlev 
1546681Sjohnlev /*
1550Sstevel@tonic-gate  * The panicbuf array is used to record messages and state:
1560Sstevel@tonic-gate  */
1570Sstevel@tonic-gate char panicbuf[PANICBUFSIZE];
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate /*
16012004Sjiang.liu@intel.com  * Flags to control Dynamic Reconfiguration features.
16112004Sjiang.liu@intel.com  */
16212004Sjiang.liu@intel.com uint64_t plat_dr_options;
16312004Sjiang.liu@intel.com 
16412004Sjiang.liu@intel.com /*
16512004Sjiang.liu@intel.com  * Maximum physical address for memory DR operations.
16612004Sjiang.liu@intel.com  */
16712004Sjiang.liu@intel.com uint64_t plat_dr_physmax;
16812004Sjiang.liu@intel.com 
16912004Sjiang.liu@intel.com /*
1700Sstevel@tonic-gate  * maxphys - used during physio
1710Sstevel@tonic-gate  * klustsize - used for klustering by swapfs and specfs
1720Sstevel@tonic-gate  */
1730Sstevel@tonic-gate int maxphys = 56 * 1024;    /* XXX See vm_subr.c - max b_count in physio */
1740Sstevel@tonic-gate int klustsize = 56 * 1024;
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate caddr_t	p0_va;		/* Virtual address for accessing physical page 0 */
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate /*
1790Sstevel@tonic-gate  * defined here, though unused on x86,
1800Sstevel@tonic-gate  * to make kstat_fr.c happy.
1810Sstevel@tonic-gate  */
1820Sstevel@tonic-gate int vac;
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate void debug_enter(char *);
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate extern void pm_cfb_check_and_powerup(void);
1870Sstevel@tonic-gate extern void pm_cfb_rele(void);
1880Sstevel@tonic-gate 
1897656SSherry.Moore@Sun.COM extern fastboot_info_t newkernel;
1907656SSherry.Moore@Sun.COM 
1910Sstevel@tonic-gate /*
1920Sstevel@tonic-gate  * Machine dependent code to reboot.
1930Sstevel@tonic-gate  * "mdep" is interpreted as a character pointer; if non-null, it is a pointer
1940Sstevel@tonic-gate  * to a string to be used as the argument string when rebooting.
195136Sachartre  *
196136Sachartre  * "invoke_cb" is a boolean. It is set to true when mdboot() can safely
197136Sachartre  * invoke CB_CL_MDBOOT callbacks before shutting the system down, i.e. when
198136Sachartre  * we are in a normal shutdown sequence (interrupts are not blocked, the
199136Sachartre  * system is not panic'ing or being suspended).
2000Sstevel@tonic-gate  */
2010Sstevel@tonic-gate /*ARGSUSED*/
2020Sstevel@tonic-gate void
mdboot(int cmd,int fcn,char * mdep,boolean_t invoke_cb)203136Sachartre mdboot(int cmd, int fcn, char *mdep, boolean_t invoke_cb)
2040Sstevel@tonic-gate {
2057656SSherry.Moore@Sun.COM 	processorid_t bootcpuid = 0;
2067863SSherry.Moore@Sun.COM 	static int is_first_quiesce = 1;
2077863SSherry.Moore@Sun.COM 	static int is_first_reset = 1;
2087863SSherry.Moore@Sun.COM 	int reset_status = 0;
2099160SSherry.Moore@Sun.COM 	static char fallback_str[] = "Falling back to regular reboot.\n";
2107656SSherry.Moore@Sun.COM 
2117656SSherry.Moore@Sun.COM 	if (fcn == AD_FASTREBOOT && !newkernel.fi_valid)
2127656SSherry.Moore@Sun.COM 		fcn = AD_BOOT;
2137656SSherry.Moore@Sun.COM 
2140Sstevel@tonic-gate 	if (!panicstr) {
2150Sstevel@tonic-gate 		kpreempt_disable();
2167656SSherry.Moore@Sun.COM 		if (fcn == AD_FASTREBOOT) {
2177656SSherry.Moore@Sun.COM 			mutex_enter(&cpu_lock);
2187656SSherry.Moore@Sun.COM 			if (CPU_ACTIVE(cpu_get(bootcpuid))) {
2197656SSherry.Moore@Sun.COM 				affinity_set(bootcpuid);
2207656SSherry.Moore@Sun.COM 			}
2217656SSherry.Moore@Sun.COM 			mutex_exit(&cpu_lock);
2227656SSherry.Moore@Sun.COM 		} else {
2237656SSherry.Moore@Sun.COM 			affinity_set(CPU_CURRENT);
2247656SSherry.Moore@Sun.COM 		}
2250Sstevel@tonic-gate 	}
2260Sstevel@tonic-gate 
2276681Sjohnlev 	if (force_shutdown_method != AD_UNKNOWN)
2286681Sjohnlev 		fcn = force_shutdown_method;
2296681Sjohnlev 
2300Sstevel@tonic-gate 	/*
2315630Sjbeck 	 * XXX - rconsvp is set to NULL to ensure that output messages
2325630Sjbeck 	 * are sent to the underlying "hardware" device using the
2335630Sjbeck 	 * monitor's printf routine since we are in the process of
2345630Sjbeck 	 * either rebooting or halting the machine.
2355630Sjbeck 	 */
2365630Sjbeck 	rconsvp = NULL;
2375630Sjbeck 
2385630Sjbeck 	/*
2390Sstevel@tonic-gate 	 * Print the reboot message now, before pausing other cpus.
2400Sstevel@tonic-gate 	 * There is a race condition in the printing support that
2410Sstevel@tonic-gate 	 * can deadlock multiprocessor machines.
2420Sstevel@tonic-gate 	 */
2430Sstevel@tonic-gate 	if (!(fcn == AD_HALT || fcn == AD_POWEROFF))
2440Sstevel@tonic-gate 		prom_printf("rebooting...\n");
2450Sstevel@tonic-gate 
2465084Sjohnlev 	if (IN_XPV_PANIC())
2475084Sjohnlev 		reset();
2485084Sjohnlev 
2490Sstevel@tonic-gate 	/*
2500Sstevel@tonic-gate 	 * We can't bring up the console from above lock level, so do it now
2510Sstevel@tonic-gate 	 */
2520Sstevel@tonic-gate 	pm_cfb_check_and_powerup();
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 	/* make sure there are no more changes to the device tree */
2550Sstevel@tonic-gate 	devtree_freeze();
2560Sstevel@tonic-gate 
257136Sachartre 	if (invoke_cb)
258136Sachartre 		(void) callb_execute_class(CB_CL_MDBOOT, NULL);
259136Sachartre 
2603253Smec 	/*
2613253Smec 	 * Clear any unresolved UEs from memory.
2623253Smec 	 */
2633253Smec 	page_retire_mdboot();
264917Selowe 
2655084Sjohnlev #if defined(__xpv)
2665084Sjohnlev 	/*
2675084Sjohnlev 	 * XXPV	Should probably think some more about how we deal
2685084Sjohnlev 	 *	with panicing before it's really safe to panic.
2695084Sjohnlev 	 *	On hypervisors, we reboot very quickly..  Perhaps panic
2705084Sjohnlev 	 *	should only attempt to recover by rebooting if,
2715084Sjohnlev 	 *	say, we were able to mount the root filesystem,
2725084Sjohnlev 	 *	or if we successfully launched init(1m).
2735084Sjohnlev 	 */
2745084Sjohnlev 	if (panicstr && proc_init == NULL)
2755084Sjohnlev 		(void) HYPERVISOR_shutdown(SHUTDOWN_poweroff);
2765084Sjohnlev #endif
2770Sstevel@tonic-gate 	/*
2780Sstevel@tonic-gate 	 * stop other cpus and raise our priority.  since there is only
2790Sstevel@tonic-gate 	 * one active cpu after this, and our priority will be too high
2800Sstevel@tonic-gate 	 * for us to be preempted, we're essentially single threaded
2810Sstevel@tonic-gate 	 * from here on out.
2820Sstevel@tonic-gate 	 */
2830Sstevel@tonic-gate 	(void) spl6();
2840Sstevel@tonic-gate 	if (!panicstr) {
2850Sstevel@tonic-gate 		mutex_enter(&cpu_lock);
2860Sstevel@tonic-gate 		pause_cpus(NULL);
2870Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
2880Sstevel@tonic-gate 	}
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate 	/*
29110916SSherry.Moore@Sun.COM 	 * If the system is panicking, the preloaded kernel is valid, and
29210916SSherry.Moore@Sun.COM 	 * fastreboot_onpanic has been set, and the system has been up for
29310916SSherry.Moore@Sun.COM 	 * longer than fastreboot_onpanic_uptime (default to 10 minutes),
29410916SSherry.Moore@Sun.COM 	 * choose Fast Reboot.
2959160SSherry.Moore@Sun.COM 	 */
2969160SSherry.Moore@Sun.COM 	if (fcn == AD_BOOT && panicstr && newkernel.fi_valid &&
29710916SSherry.Moore@Sun.COM 	    fastreboot_onpanic &&
29810916SSherry.Moore@Sun.COM 	    (panic_lbolt - lbolt_at_boot) > fastreboot_onpanic_uptime) {
2999160SSherry.Moore@Sun.COM 		fcn = AD_FASTREBOOT;
30010916SSherry.Moore@Sun.COM 	}
3019160SSherry.Moore@Sun.COM 
3029160SSherry.Moore@Sun.COM 	/*
3037656SSherry.Moore@Sun.COM 	 * Try to quiesce devices.
3040Sstevel@tonic-gate 	 */
3057863SSherry.Moore@Sun.COM 	if (is_first_quiesce) {
3067863SSherry.Moore@Sun.COM 		/*
3077863SSherry.Moore@Sun.COM 		 * Clear is_first_quiesce before calling quiesce_devices()
3087863SSherry.Moore@Sun.COM 		 * so that if quiesce_devices() causes panics, it will not
3097863SSherry.Moore@Sun.COM 		 * be invoked again.
3107863SSherry.Moore@Sun.COM 		 */
3117863SSherry.Moore@Sun.COM 		is_first_quiesce = 0;
3127656SSherry.Moore@Sun.COM 
3137656SSherry.Moore@Sun.COM 		quiesce_active = 1;
3147656SSherry.Moore@Sun.COM 		quiesce_devices(ddi_root_node(), &reset_status);
3157783SSherry.Moore@Sun.COM 		if (reset_status == -1) {
3167783SSherry.Moore@Sun.COM 			if (fcn == AD_FASTREBOOT && !force_fastreboot) {
3177783SSherry.Moore@Sun.COM 				prom_printf("Driver(s) not capable of fast "
3189160SSherry.Moore@Sun.COM 				    "reboot.\n");
3199160SSherry.Moore@Sun.COM 				prom_printf(fallback_str);
3207783SSherry.Moore@Sun.COM 				fastreboot_capable = 0;
3219160SSherry.Moore@Sun.COM 				fcn = AD_BOOT;
3227783SSherry.Moore@Sun.COM 			} else if (fcn != AD_FASTREBOOT)
3237783SSherry.Moore@Sun.COM 				fastreboot_capable = 0;
3247656SSherry.Moore@Sun.COM 		}
3257656SSherry.Moore@Sun.COM 		quiesce_active = 0;
3267656SSherry.Moore@Sun.COM 	}
3277656SSherry.Moore@Sun.COM 
3287656SSherry.Moore@Sun.COM 	/*
3297863SSherry.Moore@Sun.COM 	 * Try to reset devices. reset_leaves() should only be called
3307863SSherry.Moore@Sun.COM 	 * a) when there are no other threads that could be accessing devices,
3317863SSherry.Moore@Sun.COM 	 *    and
3327863SSherry.Moore@Sun.COM 	 * b) on a system that's not capable of fast reboot (fastreboot_capable
3337863SSherry.Moore@Sun.COM 	 *    being 0), or on a system where quiesce_devices() failed to
3347863SSherry.Moore@Sun.COM 	 *    complete (quiesce_active being 1).
3357656SSherry.Moore@Sun.COM 	 */
3367863SSherry.Moore@Sun.COM 	if (is_first_reset && (!fastreboot_capable || quiesce_active)) {
3377863SSherry.Moore@Sun.COM 		/*
3387863SSherry.Moore@Sun.COM 		 * Clear is_first_reset before calling reset_devices()
3397863SSherry.Moore@Sun.COM 		 * so that if reset_devices() causes panics, it will not
3407863SSherry.Moore@Sun.COM 		 * be invoked again.
3417863SSherry.Moore@Sun.COM 		 */
3427863SSherry.Moore@Sun.COM 		is_first_reset = 0;
3437656SSherry.Moore@Sun.COM 		reset_leaves();
3447863SSherry.Moore@Sun.COM 	}
3450Sstevel@tonic-gate 
3469160SSherry.Moore@Sun.COM 	/* Verify newkernel checksum */
3479160SSherry.Moore@Sun.COM 	if (fastreboot_capable && fcn == AD_FASTREBOOT &&
3489160SSherry.Moore@Sun.COM 	    fastboot_cksum_verify(&newkernel) != 0) {
3499160SSherry.Moore@Sun.COM 		fastreboot_capable = 0;
3509160SSherry.Moore@Sun.COM 		prom_printf("Fast reboot: checksum failed for the new "
3519160SSherry.Moore@Sun.COM 		    "kernel.\n");
3529160SSherry.Moore@Sun.COM 		prom_printf(fallback_str);
3539160SSherry.Moore@Sun.COM 	}
3549160SSherry.Moore@Sun.COM 
3550Sstevel@tonic-gate 	(void) spl8();
3560Sstevel@tonic-gate 
3579160SSherry.Moore@Sun.COM 	if (fastreboot_capable && fcn == AD_FASTREBOOT) {
3589160SSherry.Moore@Sun.COM 		/*
3599160SSherry.Moore@Sun.COM 		 * psm_shutdown is called within fast_reboot()
3609160SSherry.Moore@Sun.COM 		 */
3617656SSherry.Moore@Sun.COM 		fast_reboot();
3629160SSherry.Moore@Sun.COM 	} else {
3639160SSherry.Moore@Sun.COM 		(*psm_shutdownf)(cmd, fcn);
3649160SSherry.Moore@Sun.COM 
3659160SSherry.Moore@Sun.COM 		if (fcn == AD_HALT || fcn == AD_POWEROFF)
3669160SSherry.Moore@Sun.COM 			halt((char *)NULL);
3679160SSherry.Moore@Sun.COM 		else
3689160SSherry.Moore@Sun.COM 			prom_reboot("");
3699160SSherry.Moore@Sun.COM 	}
3700Sstevel@tonic-gate 	/*NOTREACHED*/
3710Sstevel@tonic-gate }
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate /* mdpreboot - may be called prior to mdboot while root fs still mounted */
3740Sstevel@tonic-gate /*ARGSUSED*/
3750Sstevel@tonic-gate void
mdpreboot(int cmd,int fcn,char * mdep)3760Sstevel@tonic-gate mdpreboot(int cmd, int fcn, char *mdep)
3770Sstevel@tonic-gate {
3787656SSherry.Moore@Sun.COM 	if (fcn == AD_FASTREBOOT && !fastreboot_capable) {
3797656SSherry.Moore@Sun.COM 		fcn = AD_BOOT;
3807656SSherry.Moore@Sun.COM #ifdef	__xpv
3819160SSherry.Moore@Sun.COM 		cmn_err(CE_WARN, "Fast reboot is not supported on xVM");
3827656SSherry.Moore@Sun.COM #else
3839160SSherry.Moore@Sun.COM 		cmn_err(CE_WARN,
38411686SKonstantin.Ananyev@Sun.COM 		    "Fast reboot is not supported on this platform%s",
38511686SKonstantin.Ananyev@Sun.COM 		    fastreboot_nosup_message());
3867656SSherry.Moore@Sun.COM #endif
3877656SSherry.Moore@Sun.COM 	}
3887656SSherry.Moore@Sun.COM 
3897656SSherry.Moore@Sun.COM 	if (fcn == AD_FASTREBOOT) {
3909160SSherry.Moore@Sun.COM 		fastboot_load_kernel(mdep);
3917656SSherry.Moore@Sun.COM 		if (!newkernel.fi_valid)
3927656SSherry.Moore@Sun.COM 			fcn = AD_BOOT;
3937656SSherry.Moore@Sun.COM 	}
3947656SSherry.Moore@Sun.COM 
3950Sstevel@tonic-gate 	(*psm_preshutdownf)(cmd, fcn);
3960Sstevel@tonic-gate }
3970Sstevel@tonic-gate 
3989489SJoe.Bonasera@sun.com static void
stop_other_cpus(void)3999489SJoe.Bonasera@sun.com stop_other_cpus(void)
4000Sstevel@tonic-gate {
4019489SJoe.Bonasera@sun.com 	ulong_t s = clear_int_flag(); /* fast way to keep CPU from changing */
4020Sstevel@tonic-gate 	cpuset_t xcset;
4030Sstevel@tonic-gate 
4049489SJoe.Bonasera@sun.com 	CPUSET_ALL_BUT(xcset, CPU->cpu_id);
4059489SJoe.Bonasera@sun.com 	xc_priority(0, 0, 0, CPUSET2BV(xcset), (xc_func_t)mach_cpu_halt);
4069489SJoe.Bonasera@sun.com 	restore_int_flag(s);
4070Sstevel@tonic-gate }
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate /*
4100Sstevel@tonic-gate  *	Machine dependent abort sequence handling
4110Sstevel@tonic-gate  */
4120Sstevel@tonic-gate void
abort_sequence_enter(char * msg)4130Sstevel@tonic-gate abort_sequence_enter(char *msg)
4140Sstevel@tonic-gate {
4150Sstevel@tonic-gate 	if (abort_enable == 0) {
41611861SMarek.Pospisil@Sun.COM 		if (AU_ZONE_AUDITING(GET_KCTX_GZ))
4170Sstevel@tonic-gate 			audit_enterprom(0);
4180Sstevel@tonic-gate 		return;
4190Sstevel@tonic-gate 	}
42011861SMarek.Pospisil@Sun.COM 	if (AU_ZONE_AUDITING(GET_KCTX_GZ))
4210Sstevel@tonic-gate 		audit_enterprom(1);
4220Sstevel@tonic-gate 	debug_enter(msg);
42311861SMarek.Pospisil@Sun.COM 	if (AU_ZONE_AUDITING(GET_KCTX_GZ))
4240Sstevel@tonic-gate 		audit_exitprom(1);
4250Sstevel@tonic-gate }
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate /*
4280Sstevel@tonic-gate  * Enter debugger.  Called when the user types ctrl-alt-d or whenever
4290Sstevel@tonic-gate  * code wants to enter the debugger and possibly resume later.
4300Sstevel@tonic-gate  */
4310Sstevel@tonic-gate void
debug_enter(char * msg)4320Sstevel@tonic-gate debug_enter(
4330Sstevel@tonic-gate 	char	*msg)		/* message to print, possibly NULL */
4340Sstevel@tonic-gate {
4350Sstevel@tonic-gate 	if (dtrace_debugger_init != NULL)
4360Sstevel@tonic-gate 		(*dtrace_debugger_init)();
4370Sstevel@tonic-gate 
4380Sstevel@tonic-gate 	if (msg)
4390Sstevel@tonic-gate 		prom_printf("%s\n", msg);
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate 	if (boothowto & RB_DEBUG)
4423446Smrj 		kmdb_enter();
4430Sstevel@tonic-gate 
4440Sstevel@tonic-gate 	if (dtrace_debugger_fini != NULL)
4450Sstevel@tonic-gate 		(*dtrace_debugger_fini)();
4460Sstevel@tonic-gate }
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate void
reset(void)4490Sstevel@tonic-gate reset(void)
4500Sstevel@tonic-gate {
45110457SSaurabh.Mishra@Sun.COM 	extern	void acpi_reset_system();
4525084Sjohnlev #if !defined(__xpv)
4530Sstevel@tonic-gate 	ushort_t *bios_memchk;
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate 	/*
45610457SSaurabh.Mishra@Sun.COM 	 * Can't use psm_map_phys or acpi_reset_system before the hat is
45710457SSaurabh.Mishra@Sun.COM 	 * initialized.
4580Sstevel@tonic-gate 	 */
4590Sstevel@tonic-gate 	if (khat_running) {
4600Sstevel@tonic-gate 		bios_memchk = (ushort_t *)psm_map_phys(0x472,
4610Sstevel@tonic-gate 		    sizeof (ushort_t), PROT_READ | PROT_WRITE);
4620Sstevel@tonic-gate 		if (bios_memchk)
4630Sstevel@tonic-gate 			*bios_memchk = 0x1234;	/* bios memory check disable */
46410457SSaurabh.Mishra@Sun.COM 
46510457SSaurabh.Mishra@Sun.COM 		if (options_dip != NULL &&
46610457SSaurabh.Mishra@Sun.COM 		    ddi_prop_exists(DDI_DEV_T_ANY, ddi_root_node(), 0,
46710457SSaurabh.Mishra@Sun.COM 		    "efi-systab")) {
46810457SSaurabh.Mishra@Sun.COM 			efi_reset();
46910457SSaurabh.Mishra@Sun.COM 		}
47010457SSaurabh.Mishra@Sun.COM 
47110457SSaurabh.Mishra@Sun.COM 		/*
47210457SSaurabh.Mishra@Sun.COM 		 * The problem with using stubs is that we can call
47310457SSaurabh.Mishra@Sun.COM 		 * acpi_reset_system only after the kernel is up and running.
47410457SSaurabh.Mishra@Sun.COM 		 *
47510457SSaurabh.Mishra@Sun.COM 		 * We should create a global state to keep track of how far
47610457SSaurabh.Mishra@Sun.COM 		 * up the kernel is but for the time being we will depend on
47710457SSaurabh.Mishra@Sun.COM 		 * bootops. bootops cleared in startup_end().
47810457SSaurabh.Mishra@Sun.COM 		 */
47910457SSaurabh.Mishra@Sun.COM 		if (bootops == NULL)
48010457SSaurabh.Mishra@Sun.COM 			acpi_reset_system();
4810Sstevel@tonic-gate 	}
4820Sstevel@tonic-gate 
4830Sstevel@tonic-gate 	pc_reset();
4845084Sjohnlev #else
48510457SSaurabh.Mishra@Sun.COM 	if (IN_XPV_PANIC()) {
48610457SSaurabh.Mishra@Sun.COM 		if (khat_running && bootops == NULL) {
48710457SSaurabh.Mishra@Sun.COM 			acpi_reset_system();
48810457SSaurabh.Mishra@Sun.COM 		}
48910457SSaurabh.Mishra@Sun.COM 
4905084Sjohnlev 		pc_reset();
49110457SSaurabh.Mishra@Sun.COM 	}
49210457SSaurabh.Mishra@Sun.COM 
4935084Sjohnlev 	(void) HYPERVISOR_shutdown(SHUTDOWN_reboot);
4945084Sjohnlev 	panic("HYPERVISOR_shutdown() failed");
4955084Sjohnlev #endif
4960Sstevel@tonic-gate 	/*NOTREACHED*/
4970Sstevel@tonic-gate }
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate /*
5000Sstevel@tonic-gate  * Halt the machine and return to the monitor
5010Sstevel@tonic-gate  */
5020Sstevel@tonic-gate void
halt(char * s)5030Sstevel@tonic-gate halt(char *s)
5040Sstevel@tonic-gate {
5050Sstevel@tonic-gate 	stop_other_cpus();	/* send stop signal to other CPUs */
5060Sstevel@tonic-gate 	if (s)
5070Sstevel@tonic-gate 		prom_printf("(%s) \n", s);
5080Sstevel@tonic-gate 	prom_exit_to_mon();
5090Sstevel@tonic-gate 	/*NOTREACHED*/
5100Sstevel@tonic-gate }
5110Sstevel@tonic-gate 
5120Sstevel@tonic-gate /*
5130Sstevel@tonic-gate  * Initiate interrupt redistribution.
5140Sstevel@tonic-gate  */
5150Sstevel@tonic-gate void
i_ddi_intr_redist_all_cpus()5160Sstevel@tonic-gate i_ddi_intr_redist_all_cpus()
5170Sstevel@tonic-gate {
5180Sstevel@tonic-gate }
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate /*
5210Sstevel@tonic-gate  * XXX These probably ought to live somewhere else
5220Sstevel@tonic-gate  * XXX They are called from mem.c
5230Sstevel@tonic-gate  */
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate /*
5260Sstevel@tonic-gate  * Convert page frame number to an OBMEM page frame number
5270Sstevel@tonic-gate  * (i.e. put in the type bits -- zero for this implementation)
5280Sstevel@tonic-gate  */
5290Sstevel@tonic-gate pfn_t
impl_obmem_pfnum(pfn_t pf)5300Sstevel@tonic-gate impl_obmem_pfnum(pfn_t pf)
5310Sstevel@tonic-gate {
5320Sstevel@tonic-gate 	return (pf);
5330Sstevel@tonic-gate }
5340Sstevel@tonic-gate 
5350Sstevel@tonic-gate #ifdef	NM_DEBUG
5360Sstevel@tonic-gate int nmi_test = 0;	/* checked in intentry.s during clock int */
5370Sstevel@tonic-gate int nmtest = -1;
nmfunc1(arg,rp)5380Sstevel@tonic-gate nmfunc1(arg, rp)
5390Sstevel@tonic-gate int	arg;
5400Sstevel@tonic-gate struct regs *rp;
5410Sstevel@tonic-gate {
5420Sstevel@tonic-gate 	printf("nmi called with arg = %x, regs = %x\n", arg, rp);
5430Sstevel@tonic-gate 	nmtest += 50;
5440Sstevel@tonic-gate 	if (arg == nmtest) {
5450Sstevel@tonic-gate 		printf("ip = %x\n", rp->r_pc);
5460Sstevel@tonic-gate 		return (1);
5470Sstevel@tonic-gate 	}
5480Sstevel@tonic-gate 	return (0);
5490Sstevel@tonic-gate }
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate #endif
5520Sstevel@tonic-gate 
5530Sstevel@tonic-gate #include <sys/bootsvcs.h>
5540Sstevel@tonic-gate 
5550Sstevel@tonic-gate /* Hacked up initialization for initial kernel check out is HERE. */
5560Sstevel@tonic-gate /* The basic steps are: */
5570Sstevel@tonic-gate /*	kernel bootfuncs definition/initialization for KADB */
5580Sstevel@tonic-gate /*	kadb bootfuncs pointer initialization */
5590Sstevel@tonic-gate /*	putchar/getchar (interrupts disabled) */
5600Sstevel@tonic-gate 
5610Sstevel@tonic-gate /* kadb bootfuncs pointer initialization */
5620Sstevel@tonic-gate 
5630Sstevel@tonic-gate int
sysp_getchar()5640Sstevel@tonic-gate sysp_getchar()
5650Sstevel@tonic-gate {
5660Sstevel@tonic-gate 	int i;
5673446Smrj 	ulong_t s;
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate 	if (cons_polledio == NULL) {
5700Sstevel@tonic-gate 		/* Uh oh */
5710Sstevel@tonic-gate 		prom_printf("getchar called with no console\n");
5720Sstevel@tonic-gate 		for (;;)
5730Sstevel@tonic-gate 			/* LOOP FOREVER */;
5740Sstevel@tonic-gate 	}
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate 	s = clear_int_flag();
5770Sstevel@tonic-gate 	i = cons_polledio->cons_polledio_getchar(
5785084Sjohnlev 	    cons_polledio->cons_polledio_argument);
5790Sstevel@tonic-gate 	restore_int_flag(s);
5800Sstevel@tonic-gate 	return (i);
5810Sstevel@tonic-gate }
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate void
sysp_putchar(int c)5840Sstevel@tonic-gate sysp_putchar(int c)
5850Sstevel@tonic-gate {
5863446Smrj 	ulong_t s;
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate 	/*
5890Sstevel@tonic-gate 	 * We have no alternative but to drop the output on the floor.
5900Sstevel@tonic-gate 	 */
5911253Slq150181 	if (cons_polledio == NULL ||
5921253Slq150181 	    cons_polledio->cons_polledio_putchar == NULL)
5930Sstevel@tonic-gate 		return;
5940Sstevel@tonic-gate 
5950Sstevel@tonic-gate 	s = clear_int_flag();
5960Sstevel@tonic-gate 	cons_polledio->cons_polledio_putchar(
5975084Sjohnlev 	    cons_polledio->cons_polledio_argument, c);
5980Sstevel@tonic-gate 	restore_int_flag(s);
5990Sstevel@tonic-gate }
6000Sstevel@tonic-gate 
6010Sstevel@tonic-gate int
sysp_ischar()6020Sstevel@tonic-gate sysp_ischar()
6030Sstevel@tonic-gate {
6040Sstevel@tonic-gate 	int i;
6053446Smrj 	ulong_t s;
6060Sstevel@tonic-gate 
6071253Slq150181 	if (cons_polledio == NULL ||
6081253Slq150181 	    cons_polledio->cons_polledio_ischar == NULL)
6090Sstevel@tonic-gate 		return (0);
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate 	s = clear_int_flag();
6120Sstevel@tonic-gate 	i = cons_polledio->cons_polledio_ischar(
6135084Sjohnlev 	    cons_polledio->cons_polledio_argument);
6140Sstevel@tonic-gate 	restore_int_flag(s);
6150Sstevel@tonic-gate 	return (i);
6160Sstevel@tonic-gate }
6170Sstevel@tonic-gate 
6180Sstevel@tonic-gate int
goany(void)6190Sstevel@tonic-gate goany(void)
6200Sstevel@tonic-gate {
6210Sstevel@tonic-gate 	prom_printf("Type any key to continue ");
6220Sstevel@tonic-gate 	(void) prom_getchar();
6230Sstevel@tonic-gate 	prom_printf("\n");
6240Sstevel@tonic-gate 	return (1);
6250Sstevel@tonic-gate }
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate static struct boot_syscalls kern_sysp = {
6280Sstevel@tonic-gate 	sysp_getchar,	/*	unchar	(*getchar)();	7  */
6290Sstevel@tonic-gate 	sysp_putchar,	/*	int	(*putchar)();	8  */
6300Sstevel@tonic-gate 	sysp_ischar,	/*	int	(*ischar)();	9  */
6310Sstevel@tonic-gate };
6320Sstevel@tonic-gate 
6335084Sjohnlev #if defined(__xpv)
6345084Sjohnlev int using_kern_polledio;
6355084Sjohnlev #endif
6365084Sjohnlev 
6370Sstevel@tonic-gate void
kadb_uses_kernel()6380Sstevel@tonic-gate kadb_uses_kernel()
6390Sstevel@tonic-gate {
6400Sstevel@tonic-gate 	/*
6410Sstevel@tonic-gate 	 * This routine is now totally misnamed, since it does not in fact
6420Sstevel@tonic-gate 	 * control kadb's I/O; it only controls the kernel's prom_* I/O.
6430Sstevel@tonic-gate 	 */
6440Sstevel@tonic-gate 	sysp = &kern_sysp;
6455084Sjohnlev #if defined(__xpv)
6465084Sjohnlev 	using_kern_polledio = 1;
6475084Sjohnlev #endif
6480Sstevel@tonic-gate }
6490Sstevel@tonic-gate 
6500Sstevel@tonic-gate /*
6510Sstevel@tonic-gate  *	the interface to the outside world
6520Sstevel@tonic-gate  */
6530Sstevel@tonic-gate 
6540Sstevel@tonic-gate /*
6550Sstevel@tonic-gate  * poll_port -- wait for a register to achieve a
6560Sstevel@tonic-gate  *		specific state.  Arguments are a mask of bits we care about,
6570Sstevel@tonic-gate  *		and two sub-masks.  To return normally, all the bits in the
6580Sstevel@tonic-gate  *		first sub-mask must be ON, all the bits in the second sub-
6590Sstevel@tonic-gate  *		mask must be OFF.  If about seconds pass without the register
6600Sstevel@tonic-gate  *		achieving the desired bit configuration, we return 1, else
6610Sstevel@tonic-gate  *		0.
6620Sstevel@tonic-gate  */
6630Sstevel@tonic-gate int
poll_port(ushort_t port,ushort_t mask,ushort_t onbits,ushort_t offbits)6640Sstevel@tonic-gate poll_port(ushort_t port, ushort_t mask, ushort_t onbits, ushort_t offbits)
6650Sstevel@tonic-gate {
6660Sstevel@tonic-gate 	int i;
6670Sstevel@tonic-gate 	ushort_t maskval;
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate 	for (i = 500000; i; i--) {
6700Sstevel@tonic-gate 		maskval = inb(port) & mask;
6710Sstevel@tonic-gate 		if (((maskval & onbits) == onbits) &&
6725084Sjohnlev 		    ((maskval & offbits) == 0))
6730Sstevel@tonic-gate 			return (0);
6740Sstevel@tonic-gate 		drv_usecwait(10);
6750Sstevel@tonic-gate 	}
6760Sstevel@tonic-gate 	return (1);
6770Sstevel@tonic-gate }
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate /*
6800Sstevel@tonic-gate  * set_idle_cpu is called from idle() when a CPU becomes idle.
6810Sstevel@tonic-gate  */
6820Sstevel@tonic-gate /*LINTED: static unused */
6830Sstevel@tonic-gate static uint_t last_idle_cpu;
6840Sstevel@tonic-gate 
6850Sstevel@tonic-gate /*ARGSUSED*/
6860Sstevel@tonic-gate void
set_idle_cpu(int cpun)6870Sstevel@tonic-gate set_idle_cpu(int cpun)
6880Sstevel@tonic-gate {
6890Sstevel@tonic-gate 	last_idle_cpu = cpun;
6900Sstevel@tonic-gate 	(*psm_set_idle_cpuf)(cpun);
6910Sstevel@tonic-gate }
6920Sstevel@tonic-gate 
6930Sstevel@tonic-gate /*
6940Sstevel@tonic-gate  * unset_idle_cpu is called from idle() when a CPU is no longer idle.
6950Sstevel@tonic-gate  */
6960Sstevel@tonic-gate /*ARGSUSED*/
6970Sstevel@tonic-gate void
unset_idle_cpu(int cpun)6980Sstevel@tonic-gate unset_idle_cpu(int cpun)
6990Sstevel@tonic-gate {
7000Sstevel@tonic-gate 	(*psm_unset_idle_cpuf)(cpun);
7010Sstevel@tonic-gate }
7020Sstevel@tonic-gate 
7030Sstevel@tonic-gate /*
7040Sstevel@tonic-gate  * This routine is almost correct now, but not quite.  It still needs the
7050Sstevel@tonic-gate  * equivalent concept of "hres_last_tick", just like on the sparc side.
7060Sstevel@tonic-gate  * The idea is to take a snapshot of the hi-res timer while doing the
7070Sstevel@tonic-gate  * hrestime_adj updates under hres_lock in locore, so that the small
7080Sstevel@tonic-gate  * interval between interrupt assertion and interrupt processing is
7090Sstevel@tonic-gate  * accounted for correctly.  Once we have this, the code below should
7100Sstevel@tonic-gate  * be modified to subtract off hres_last_tick rather than hrtime_base.
7110Sstevel@tonic-gate  *
7120Sstevel@tonic-gate  * I'd have done this myself, but I don't have source to all of the
7130Sstevel@tonic-gate  * vendor-specific hi-res timer routines (grrr...).  The generic hook I
7140Sstevel@tonic-gate  * need is something like "gethrtime_unlocked()", which would be just like
7150Sstevel@tonic-gate  * gethrtime() but would assume that you're already holding CLOCK_LOCK().
7160Sstevel@tonic-gate  * This is what the GET_HRTIME() macro is for on sparc (although it also
7170Sstevel@tonic-gate  * serves the function of making time available without a function call
7180Sstevel@tonic-gate  * so you don't take a register window overflow while traps are disabled).
7190Sstevel@tonic-gate  */
7200Sstevel@tonic-gate void
pc_gethrestime(timestruc_t * tp)7210Sstevel@tonic-gate pc_gethrestime(timestruc_t *tp)
7220Sstevel@tonic-gate {
7230Sstevel@tonic-gate 	int lock_prev;
7240Sstevel@tonic-gate 	timestruc_t now;
7250Sstevel@tonic-gate 	int nslt;		/* nsec since last tick */
7260Sstevel@tonic-gate 	int adj;		/* amount of adjustment to apply */
7270Sstevel@tonic-gate 
7280Sstevel@tonic-gate loop:
7290Sstevel@tonic-gate 	lock_prev = hres_lock;
7300Sstevel@tonic-gate 	now = hrestime;
7310Sstevel@tonic-gate 	nslt = (int)(gethrtime() - hres_last_tick);
7320Sstevel@tonic-gate 	if (nslt < 0) {
7330Sstevel@tonic-gate 		/*
7340Sstevel@tonic-gate 		 * nslt < 0 means a tick came between sampling
7350Sstevel@tonic-gate 		 * gethrtime() and hres_last_tick; restart the loop
7360Sstevel@tonic-gate 		 */
7370Sstevel@tonic-gate 
7380Sstevel@tonic-gate 		goto loop;
7390Sstevel@tonic-gate 	}
7400Sstevel@tonic-gate 	now.tv_nsec += nslt;
7410Sstevel@tonic-gate 	if (hrestime_adj != 0) {
7420Sstevel@tonic-gate 		if (hrestime_adj > 0) {
7430Sstevel@tonic-gate 			adj = (nslt >> ADJ_SHIFT);
7440Sstevel@tonic-gate 			if (adj > hrestime_adj)
7450Sstevel@tonic-gate 				adj = (int)hrestime_adj;
7460Sstevel@tonic-gate 		} else {
7470Sstevel@tonic-gate 			adj = -(nslt >> ADJ_SHIFT);
7480Sstevel@tonic-gate 			if (adj < hrestime_adj)
7490Sstevel@tonic-gate 				adj = (int)hrestime_adj;
7500Sstevel@tonic-gate 		}
7510Sstevel@tonic-gate 		now.tv_nsec += adj;
7520Sstevel@tonic-gate 	}
7530Sstevel@tonic-gate 	while ((unsigned long)now.tv_nsec >= NANOSEC) {
7540Sstevel@tonic-gate 
7550Sstevel@tonic-gate 		/*
7560Sstevel@tonic-gate 		 * We might have a large adjustment or have been in the
7570Sstevel@tonic-gate 		 * debugger for a long time; take care of (at most) four
7580Sstevel@tonic-gate 		 * of those missed seconds (tv_nsec is 32 bits, so
7590Sstevel@tonic-gate 		 * anything >4s will be wrapping around).  However,
7600Sstevel@tonic-gate 		 * anything more than 2 seconds out of sync will trigger
7610Sstevel@tonic-gate 		 * timedelta from clock() to go correct the time anyway,
7620Sstevel@tonic-gate 		 * so do what we can, and let the big crowbar do the
7630Sstevel@tonic-gate 		 * rest.  A similar correction while loop exists inside
7640Sstevel@tonic-gate 		 * hres_tick(); in all cases we'd like tv_nsec to
7650Sstevel@tonic-gate 		 * satisfy 0 <= tv_nsec < NANOSEC to avoid confusing
7660Sstevel@tonic-gate 		 * user processes, but if tv_sec's a little behind for a
7670Sstevel@tonic-gate 		 * little while, that's OK; time still monotonically
7680Sstevel@tonic-gate 		 * increases.
7690Sstevel@tonic-gate 		 */
7700Sstevel@tonic-gate 
7710Sstevel@tonic-gate 		now.tv_nsec -= NANOSEC;
7720Sstevel@tonic-gate 		now.tv_sec++;
7730Sstevel@tonic-gate 	}
7740Sstevel@tonic-gate 	if ((hres_lock & ~1) != lock_prev)
7750Sstevel@tonic-gate 		goto loop;
7760Sstevel@tonic-gate 
7770Sstevel@tonic-gate 	*tp = now;
7780Sstevel@tonic-gate }
7790Sstevel@tonic-gate 
7800Sstevel@tonic-gate void
gethrestime_lasttick(timespec_t * tp)7810Sstevel@tonic-gate gethrestime_lasttick(timespec_t *tp)
7820Sstevel@tonic-gate {
7830Sstevel@tonic-gate 	int s;
7840Sstevel@tonic-gate 
7850Sstevel@tonic-gate 	s = hr_clock_lock();
7860Sstevel@tonic-gate 	*tp = hrestime;
7870Sstevel@tonic-gate 	hr_clock_unlock(s);
7880Sstevel@tonic-gate }
7890Sstevel@tonic-gate 
7900Sstevel@tonic-gate time_t
gethrestime_sec(void)7910Sstevel@tonic-gate gethrestime_sec(void)
7920Sstevel@tonic-gate {
7930Sstevel@tonic-gate 	timestruc_t now;
7940Sstevel@tonic-gate 
7950Sstevel@tonic-gate 	gethrestime(&now);
7960Sstevel@tonic-gate 	return (now.tv_sec);
7970Sstevel@tonic-gate }
7980Sstevel@tonic-gate 
7990Sstevel@tonic-gate /*
8000Sstevel@tonic-gate  * Initialize a kernel thread's stack
8010Sstevel@tonic-gate  */
8020Sstevel@tonic-gate 
8030Sstevel@tonic-gate caddr_t
thread_stk_init(caddr_t stk)8040Sstevel@tonic-gate thread_stk_init(caddr_t stk)
8050Sstevel@tonic-gate {
8060Sstevel@tonic-gate 	ASSERT(((uintptr_t)stk & (STACK_ALIGN - 1)) == 0);
8070Sstevel@tonic-gate 	return (stk - SA(MINFRAME));
8080Sstevel@tonic-gate }
8090Sstevel@tonic-gate 
8100Sstevel@tonic-gate /*
8110Sstevel@tonic-gate  * Initialize lwp's kernel stack.
8120Sstevel@tonic-gate  */
8130Sstevel@tonic-gate 
8140Sstevel@tonic-gate #ifdef TRAPTRACE
8150Sstevel@tonic-gate /*
8160Sstevel@tonic-gate  * There's a tricky interdependency here between use of sysenter and
8170Sstevel@tonic-gate  * TRAPTRACE which needs recording to avoid future confusion (this is
8180Sstevel@tonic-gate  * about the third time I've re-figured this out ..)
8190Sstevel@tonic-gate  *
8200Sstevel@tonic-gate  * Here's how debugging lcall works with TRAPTRACE.
8210Sstevel@tonic-gate  *
8220Sstevel@tonic-gate  * 1 We're in userland with a breakpoint on the lcall instruction.
8230Sstevel@tonic-gate  * 2 We execute the instruction - the instruction pushes the userland
8240Sstevel@tonic-gate  *   %ss, %esp, %efl, %cs, %eip on the stack and zips into the kernel
8250Sstevel@tonic-gate  *   via the call gate.
8260Sstevel@tonic-gate  * 3 The hardware raises a debug trap in kernel mode, the hardware
8270Sstevel@tonic-gate  *   pushes %efl, %cs, %eip and gets to dbgtrap via the idt.
8280Sstevel@tonic-gate  * 4 dbgtrap pushes the error code and trapno and calls cmntrap
8290Sstevel@tonic-gate  * 5 cmntrap finishes building a trap frame
8300Sstevel@tonic-gate  * 6 The TRACE_REGS macros in cmntrap copy a REGSIZE worth chunk
8310Sstevel@tonic-gate  *   off the stack into the traptrace buffer.
8320Sstevel@tonic-gate  *
8330Sstevel@tonic-gate  * This means that the traptrace buffer contains the wrong values in
8340Sstevel@tonic-gate  * %esp and %ss, but everything else in there is correct.
8350Sstevel@tonic-gate  *
8360Sstevel@tonic-gate  * Here's how debugging sysenter works with TRAPTRACE.
8370Sstevel@tonic-gate  *
8380Sstevel@tonic-gate  * a We're in userland with a breakpoint on the sysenter instruction.
8390Sstevel@tonic-gate  * b We execute the instruction - the instruction pushes -nothing-
8400Sstevel@tonic-gate  *   on the stack, but sets %cs, %eip, %ss, %esp to prearranged
8410Sstevel@tonic-gate  *   values to take us to sys_sysenter, at the top of the lwp's
8420Sstevel@tonic-gate  *   stack.
8430Sstevel@tonic-gate  * c goto 3
8440Sstevel@tonic-gate  *
8450Sstevel@tonic-gate  * At this point, because we got into the kernel without the requisite
8460Sstevel@tonic-gate  * five pushes on the stack, if we didn't make extra room, we'd
8470Sstevel@tonic-gate  * end up with the TRACE_REGS macro fetching the saved %ss and %esp
8480Sstevel@tonic-gate  * values from negative (unmapped) stack addresses -- which really bites.
8490Sstevel@tonic-gate  * That's why we do the '-= 8' below.
8500Sstevel@tonic-gate  *
8510Sstevel@tonic-gate  * XXX	Note that reading "up" lwp0's stack works because t0 is declared
8520Sstevel@tonic-gate  *	right next to t0stack in locore.s
8530Sstevel@tonic-gate  */
8540Sstevel@tonic-gate #endif
8550Sstevel@tonic-gate 
8560Sstevel@tonic-gate caddr_t
lwp_stk_init(klwp_t * lwp,caddr_t stk)8570Sstevel@tonic-gate lwp_stk_init(klwp_t *lwp, caddr_t stk)
8580Sstevel@tonic-gate {
8590Sstevel@tonic-gate 	caddr_t oldstk;
8600Sstevel@tonic-gate 	struct pcb *pcb = &lwp->lwp_pcb;
8610Sstevel@tonic-gate 
8620Sstevel@tonic-gate 	oldstk = stk;
8630Sstevel@tonic-gate 	stk -= SA(sizeof (struct regs) + SA(MINFRAME));
8640Sstevel@tonic-gate #ifdef TRAPTRACE
8650Sstevel@tonic-gate 	stk -= 2 * sizeof (greg_t); /* space for phony %ss:%sp (see above) */
8660Sstevel@tonic-gate #endif
8670Sstevel@tonic-gate 	stk = (caddr_t)((uintptr_t)stk & ~(STACK_ALIGN - 1ul));
8680Sstevel@tonic-gate 	bzero(stk, oldstk - stk);
8690Sstevel@tonic-gate 	lwp->lwp_regs = (void *)(stk + SA(MINFRAME));
8700Sstevel@tonic-gate 
8710Sstevel@tonic-gate 	/*
8720Sstevel@tonic-gate 	 * Arrange that the virtualized %fs and %gs GDT descriptors
8730Sstevel@tonic-gate 	 * have a well-defined initial state (present, ring 3
8740Sstevel@tonic-gate 	 * and of type data).
8750Sstevel@tonic-gate 	 */
8760Sstevel@tonic-gate #if defined(__amd64)
8770Sstevel@tonic-gate 	if (lwp_getdatamodel(lwp) == DATAMODEL_NATIVE)
8780Sstevel@tonic-gate 		pcb->pcb_fsdesc = pcb->pcb_gsdesc = zero_udesc;
8790Sstevel@tonic-gate 	else
8800Sstevel@tonic-gate 		pcb->pcb_fsdesc = pcb->pcb_gsdesc = zero_u32desc;
8810Sstevel@tonic-gate #elif defined(__i386)
8820Sstevel@tonic-gate 	pcb->pcb_fsdesc = pcb->pcb_gsdesc = zero_udesc;
8830Sstevel@tonic-gate #endif	/* __i386 */
8840Sstevel@tonic-gate 	lwp_installctx(lwp);
8850Sstevel@tonic-gate 	return (stk);
8860Sstevel@tonic-gate }
8870Sstevel@tonic-gate 
8880Sstevel@tonic-gate /*ARGSUSED*/
8890Sstevel@tonic-gate void
lwp_stk_fini(klwp_t * lwp)8900Sstevel@tonic-gate lwp_stk_fini(klwp_t *lwp)
8910Sstevel@tonic-gate {}
8920Sstevel@tonic-gate 
8930Sstevel@tonic-gate /*
8941389Sdmick  * If we're not the panic CPU, we wait in panic_idle for reboot.
8950Sstevel@tonic-gate  */
8969489SJoe.Bonasera@sun.com void
panic_idle(void)8970Sstevel@tonic-gate panic_idle(void)
8980Sstevel@tonic-gate {
8990Sstevel@tonic-gate 	splx(ipltospl(CLOCK_LEVEL));
9000Sstevel@tonic-gate 	(void) setjmp(&curthread->t_pcb);
9010Sstevel@tonic-gate 
90210843SDave.Plauger@Sun.COM 	dumpsys_helper();
90310843SDave.Plauger@Sun.COM 
9049489SJoe.Bonasera@sun.com #ifndef __xpv
9059489SJoe.Bonasera@sun.com 	for (;;)
9069489SJoe.Bonasera@sun.com 		i86_halt();
9079489SJoe.Bonasera@sun.com #else
9083446Smrj 	for (;;)
9093446Smrj 		;
9109489SJoe.Bonasera@sun.com #endif
9110Sstevel@tonic-gate }
9120Sstevel@tonic-gate 
9130Sstevel@tonic-gate /*
9140Sstevel@tonic-gate  * Stop the other CPUs by cross-calling them and forcing them to enter
9150Sstevel@tonic-gate  * the panic_idle() loop above.
9160Sstevel@tonic-gate  */
9170Sstevel@tonic-gate /*ARGSUSED*/
9180Sstevel@tonic-gate void
panic_stopcpus(cpu_t * cp,kthread_t * t,int spl)9190Sstevel@tonic-gate panic_stopcpus(cpu_t *cp, kthread_t *t, int spl)
9200Sstevel@tonic-gate {
9210Sstevel@tonic-gate 	processorid_t i;
9220Sstevel@tonic-gate 	cpuset_t xcset;
9230Sstevel@tonic-gate 
9245084Sjohnlev 	/*
9255084Sjohnlev 	 * In the case of a Xen panic, the hypervisor has already stopped
9265084Sjohnlev 	 * all of the CPUs.
9275084Sjohnlev 	 */
9285084Sjohnlev 	if (!IN_XPV_PANIC()) {
9295084Sjohnlev 		(void) splzs();
9300Sstevel@tonic-gate 
9315084Sjohnlev 		CPUSET_ALL_BUT(xcset, cp->cpu_id);
9329489SJoe.Bonasera@sun.com 		xc_priority(0, 0, 0, CPUSET2BV(xcset), (xc_func_t)panic_idle);
9335084Sjohnlev 	}
9340Sstevel@tonic-gate 
9350Sstevel@tonic-gate 	for (i = 0; i < NCPU; i++) {
9360Sstevel@tonic-gate 		if (i != cp->cpu_id && cpu[i] != NULL &&
9370Sstevel@tonic-gate 		    (cpu[i]->cpu_flags & CPU_EXISTS))
9380Sstevel@tonic-gate 			cpu[i]->cpu_flags |= CPU_QUIESCED;
9390Sstevel@tonic-gate 	}
9400Sstevel@tonic-gate }
9410Sstevel@tonic-gate 
9420Sstevel@tonic-gate /*
9430Sstevel@tonic-gate  * Platform callback following each entry to panicsys().
9440Sstevel@tonic-gate  */
9450Sstevel@tonic-gate /*ARGSUSED*/
9460Sstevel@tonic-gate void
panic_enter_hw(int spl)9470Sstevel@tonic-gate panic_enter_hw(int spl)
9480Sstevel@tonic-gate {
9490Sstevel@tonic-gate 	/* Nothing to do here */
9500Sstevel@tonic-gate }
9510Sstevel@tonic-gate 
9520Sstevel@tonic-gate /*
9530Sstevel@tonic-gate  * Platform-specific code to execute after panicstr is set: we invoke
9540Sstevel@tonic-gate  * the PSM entry point to indicate that a panic has occurred.
9550Sstevel@tonic-gate  */
9560Sstevel@tonic-gate /*ARGSUSED*/
9570Sstevel@tonic-gate void
panic_quiesce_hw(panic_data_t * pdp)9580Sstevel@tonic-gate panic_quiesce_hw(panic_data_t *pdp)
9590Sstevel@tonic-gate {
9600Sstevel@tonic-gate 	psm_notifyf(PSM_PANIC_ENTER);
9610Sstevel@tonic-gate 
9627532SSean.Ye@Sun.COM 	cmi_panic_callback();
9637532SSean.Ye@Sun.COM 
9640Sstevel@tonic-gate #ifdef	TRAPTRACE
9650Sstevel@tonic-gate 	/*
9660Sstevel@tonic-gate 	 * Turn off TRAPTRACE
9670Sstevel@tonic-gate 	 */
9680Sstevel@tonic-gate 	TRAPTRACE_FREEZE;
9690Sstevel@tonic-gate #endif	/* TRAPTRACE */
9700Sstevel@tonic-gate }
9710Sstevel@tonic-gate 
9720Sstevel@tonic-gate /*
9730Sstevel@tonic-gate  * Platform callback prior to writing crash dump.
9740Sstevel@tonic-gate  */
9750Sstevel@tonic-gate /*ARGSUSED*/
9760Sstevel@tonic-gate void
panic_dump_hw(int spl)9770Sstevel@tonic-gate panic_dump_hw(int spl)
9780Sstevel@tonic-gate {
9790Sstevel@tonic-gate 	/* Nothing to do here */
9800Sstevel@tonic-gate }
9810Sstevel@tonic-gate 
9825084Sjohnlev void *
plat_traceback(void * fpreg)9835084Sjohnlev plat_traceback(void *fpreg)
9845084Sjohnlev {
9855084Sjohnlev #ifdef __xpv
9865084Sjohnlev 	if (IN_XPV_PANIC())
9875084Sjohnlev 		return (xpv_traceback(fpreg));
9885084Sjohnlev #endif
9895084Sjohnlev 	return (fpreg);
9905084Sjohnlev }
9915084Sjohnlev 
9920Sstevel@tonic-gate /*ARGSUSED*/
9930Sstevel@tonic-gate void
plat_tod_fault(enum tod_fault_type tod_bad)9940Sstevel@tonic-gate plat_tod_fault(enum tod_fault_type tod_bad)
9953446Smrj {}
9960Sstevel@tonic-gate 
9970Sstevel@tonic-gate /*ARGSUSED*/
9980Sstevel@tonic-gate int
blacklist(int cmd,const char * scheme,nvlist_t * fmri,const char * class)9990Sstevel@tonic-gate blacklist(int cmd, const char *scheme, nvlist_t *fmri, const char *class)
10000Sstevel@tonic-gate {
10010Sstevel@tonic-gate 	return (ENOTSUP);
10020Sstevel@tonic-gate }
10030Sstevel@tonic-gate 
10040Sstevel@tonic-gate /*
10050Sstevel@tonic-gate  * The underlying console output routines are protected by raising IPL in case
10060Sstevel@tonic-gate  * we are still calling into the early boot services.  Once we start calling
10070Sstevel@tonic-gate  * the kernel console emulator, it will disable interrupts completely during
10080Sstevel@tonic-gate  * character rendering (see sysp_putchar, for example).  Refer to the comments
10090Sstevel@tonic-gate  * and code in common/os/console.c for more information on these callbacks.
10100Sstevel@tonic-gate  */
10110Sstevel@tonic-gate /*ARGSUSED*/
10120Sstevel@tonic-gate int
console_enter(int busy)10130Sstevel@tonic-gate console_enter(int busy)
10140Sstevel@tonic-gate {
10150Sstevel@tonic-gate 	return (splzs());
10160Sstevel@tonic-gate }
10170Sstevel@tonic-gate 
10180Sstevel@tonic-gate /*ARGSUSED*/
10190Sstevel@tonic-gate void
console_exit(int busy,int spl)10200Sstevel@tonic-gate console_exit(int busy, int spl)
10210Sstevel@tonic-gate {
10220Sstevel@tonic-gate 	splx(spl);
10230Sstevel@tonic-gate }
10240Sstevel@tonic-gate 
10250Sstevel@tonic-gate /*
10260Sstevel@tonic-gate  * Allocate a region of virtual address space, unmapped.
10270Sstevel@tonic-gate  * Stubbed out except on sparc, at least for now.
10280Sstevel@tonic-gate  */
10290Sstevel@tonic-gate /*ARGSUSED*/
10300Sstevel@tonic-gate void *
boot_virt_alloc(void * addr,size_t size)10310Sstevel@tonic-gate boot_virt_alloc(void *addr, size_t size)
10320Sstevel@tonic-gate {
10330Sstevel@tonic-gate 	return (addr);
10340Sstevel@tonic-gate }
10350Sstevel@tonic-gate 
10360Sstevel@tonic-gate volatile unsigned long	tenmicrodata;
10370Sstevel@tonic-gate 
10380Sstevel@tonic-gate void
tenmicrosec(void)10390Sstevel@tonic-gate tenmicrosec(void)
10400Sstevel@tonic-gate {
10415084Sjohnlev 	extern int gethrtime_hires;
10420Sstevel@tonic-gate 
10435084Sjohnlev 	if (gethrtime_hires) {
10440Sstevel@tonic-gate 		hrtime_t start, end;
10450Sstevel@tonic-gate 		start = end =  gethrtime();
10460Sstevel@tonic-gate 		while ((end - start) < (10 * (NANOSEC / MICROSEC))) {
10470Sstevel@tonic-gate 			SMT_PAUSE();
10480Sstevel@tonic-gate 			end = gethrtime();
10490Sstevel@tonic-gate 		}
10500Sstevel@tonic-gate 	} else {
10515084Sjohnlev #if defined(__xpv)
10525084Sjohnlev 		hrtime_t newtime;
10535084Sjohnlev 
10545084Sjohnlev 		newtime = xpv_gethrtime() + 10000; /* now + 10 us */
10555084Sjohnlev 		while (xpv_gethrtime() < newtime)
10565084Sjohnlev 			SMT_PAUSE();
10575084Sjohnlev #else	/* __xpv */
10583446Smrj 		int i;
10593446Smrj 
10600Sstevel@tonic-gate 		/*
10610Sstevel@tonic-gate 		 * Artificial loop to induce delay.
10620Sstevel@tonic-gate 		 */
10630Sstevel@tonic-gate 		for (i = 0; i < microdata; i++)
10640Sstevel@tonic-gate 			tenmicrodata = microdata;
10655084Sjohnlev #endif	/* __xpv */
10660Sstevel@tonic-gate 	}
10670Sstevel@tonic-gate }
1068590Sesolom 
1069590Sesolom /*
1070590Sesolom  * get_cpu_mstate() is passed an array of timestamps, NCMSTATES
1071590Sesolom  * long, and it fills in the array with the time spent on cpu in
1072590Sesolom  * each of the mstates, where time is returned in nsec.
1073590Sesolom  *
1074590Sesolom  * No guarantee is made that the returned values in times[] will
1075590Sesolom  * monotonically increase on sequential calls, although this will
1076590Sesolom  * be true in the long run. Any such guarantee must be handled by
1077590Sesolom  * the caller, if needed. This can happen if we fail to account
1078590Sesolom  * for elapsed time due to a generation counter conflict, yet we
1079590Sesolom  * did account for it on a prior call (see below).
1080590Sesolom  *
1081590Sesolom  * The complication is that the cpu in question may be updating
1082590Sesolom  * its microstate at the same time that we are reading it.
1083590Sesolom  * Because the microstate is only updated when the CPU's state
1084590Sesolom  * changes, the values in cpu_intracct[] can be indefinitely out
1085590Sesolom  * of date. To determine true current values, it is necessary to
1086590Sesolom  * compare the current time with cpu_mstate_start, and add the
1087590Sesolom  * difference to times[cpu_mstate].
1088590Sesolom  *
1089590Sesolom  * This can be a problem if those values are changing out from
1090590Sesolom  * under us. Because the code path in new_cpu_mstate() is
1091590Sesolom  * performance critical, we have not added a lock to it. Instead,
1092590Sesolom  * we have added a generation counter. Before beginning
1093590Sesolom  * modifications, the counter is set to 0. After modifications,
1094590Sesolom  * it is set to the old value plus one.
1095590Sesolom  *
1096590Sesolom  * get_cpu_mstate() will not consider the values of cpu_mstate
1097590Sesolom  * and cpu_mstate_start to be usable unless the value of
1098590Sesolom  * cpu_mstate_gen is both non-zero and unchanged, both before and
1099590Sesolom  * after reading the mstate information. Note that we must
1100590Sesolom  * protect against out-of-order loads around accesses to the
1101590Sesolom  * generation counter. Also, this is a best effort approach in
1102590Sesolom  * that we do not retry should the counter be found to have
1103590Sesolom  * changed.
1104590Sesolom  *
1105590Sesolom  * cpu_intracct[] is used to identify time spent in each CPU
1106590Sesolom  * mstate while handling interrupts. Such time should be reported
1107590Sesolom  * against system time, and so is subtracted out from its
1108590Sesolom  * corresponding cpu_acct[] time and added to
1109590Sesolom  * cpu_acct[CMS_SYSTEM].
1110590Sesolom  */
1111590Sesolom 
1112590Sesolom void
get_cpu_mstate(cpu_t * cpu,hrtime_t * times)1113590Sesolom get_cpu_mstate(cpu_t *cpu, hrtime_t *times)
1114590Sesolom {
1115590Sesolom 	int i;
1116590Sesolom 	hrtime_t now, start;
1117590Sesolom 	uint16_t gen;
1118590Sesolom 	uint16_t state;
1119590Sesolom 	hrtime_t intracct[NCMSTATES];
1120590Sesolom 
1121590Sesolom 	/*
1122590Sesolom 	 * Load all volatile state under the protection of membar.
1123590Sesolom 	 * cpu_acct[cpu_mstate] must be loaded to avoid double counting
1124590Sesolom 	 * of (now - cpu_mstate_start) by a change in CPU mstate that
1125590Sesolom 	 * arrives after we make our last check of cpu_mstate_gen.
1126590Sesolom 	 */
1127590Sesolom 
1128590Sesolom 	now = gethrtime_unscaled();
1129590Sesolom 	gen = cpu->cpu_mstate_gen;
1130590Sesolom 
1131590Sesolom 	membar_consumer();	/* guarantee load ordering */
1132590Sesolom 	start = cpu->cpu_mstate_start;
1133590Sesolom 	state = cpu->cpu_mstate;
1134590Sesolom 	for (i = 0; i < NCMSTATES; i++) {
1135590Sesolom 		intracct[i] = cpu->cpu_intracct[i];
1136590Sesolom 		times[i] = cpu->cpu_acct[i];
1137590Sesolom 	}
1138590Sesolom 	membar_consumer();	/* guarantee load ordering */
1139590Sesolom 
1140590Sesolom 	if (gen != 0 && gen == cpu->cpu_mstate_gen && now > start)
1141590Sesolom 		times[state] += now - start;
1142590Sesolom 
1143590Sesolom 	for (i = 0; i < NCMSTATES; i++) {
1144590Sesolom 		if (i == CMS_SYSTEM)
1145590Sesolom 			continue;
1146590Sesolom 		times[i] -= intracct[i];
1147590Sesolom 		if (times[i] < 0) {
1148590Sesolom 			intracct[i] += times[i];
1149590Sesolom 			times[i] = 0;
1150590Sesolom 		}
1151590Sesolom 		times[CMS_SYSTEM] += intracct[i];
1152590Sesolom 		scalehrtime(&times[i]);
1153590Sesolom 	}
1154590Sesolom 	scalehrtime(&times[CMS_SYSTEM]);
1155590Sesolom }
11563446Smrj 
11573446Smrj /*
11583446Smrj  * This is a version of the rdmsr instruction that allows
11593446Smrj  * an error code to be returned in the case of failure.
11603446Smrj  */
11613446Smrj int
checked_rdmsr(uint_t msr,uint64_t * value)11623446Smrj checked_rdmsr(uint_t msr, uint64_t *value)
11633446Smrj {
116412826Skuriakose.kuruvilla@oracle.com 	if (!is_x86_feature(x86_featureset, X86FSET_MSR))
11653446Smrj 		return (ENOTSUP);
11663446Smrj 	*value = rdmsr(msr);
11673446Smrj 	return (0);
11683446Smrj }
11693446Smrj 
11703446Smrj /*
11713446Smrj  * This is a version of the wrmsr instruction that allows
11723446Smrj  * an error code to be returned in the case of failure.
11733446Smrj  */
11743446Smrj int
checked_wrmsr(uint_t msr,uint64_t value)11753446Smrj checked_wrmsr(uint_t msr, uint64_t value)
11763446Smrj {
117712826Skuriakose.kuruvilla@oracle.com 	if (!is_x86_feature(x86_featureset, X86FSET_MSR))
11783446Smrj 		return (ENOTSUP);
11793446Smrj 	wrmsr(msr, value);
11803446Smrj 	return (0);
11813446Smrj }
11823446Smrj 
11833446Smrj /*
11845084Sjohnlev  * The mem driver's usual method of using hat_devload() to establish a
11855084Sjohnlev  * temporary mapping will not work for foreign pages mapped into this
11865084Sjohnlev  * domain or for the special hypervisor-provided pages.  For the foreign
11875084Sjohnlev  * pages, we often don't know which domain owns them, so we can't ask the
11885084Sjohnlev  * hypervisor to set up a new mapping.  For the other pages, we don't have
11895084Sjohnlev  * a pfn, so we can't create a new PTE.  For these special cases, we do a
11905084Sjohnlev  * direct uiomove() from the existing kernel virtual address.
11913446Smrj  */
11923446Smrj /*ARGSUSED*/
11933446Smrj int
plat_mem_do_mmio(struct uio * uio,enum uio_rw rw)11945084Sjohnlev plat_mem_do_mmio(struct uio *uio, enum uio_rw rw)
11955084Sjohnlev {
11965084Sjohnlev #if defined(__xpv)
11975084Sjohnlev 	void *va = (void *)(uintptr_t)uio->uio_loffset;
11985084Sjohnlev 	off_t pageoff = uio->uio_loffset & PAGEOFFSET;
11995084Sjohnlev 	size_t nbytes = MIN((size_t)(PAGESIZE - pageoff),
12005084Sjohnlev 	    (size_t)uio->uio_iov->iov_len);
12015084Sjohnlev 
12025084Sjohnlev 	if ((rw == UIO_READ &&
12035084Sjohnlev 	    (va == HYPERVISOR_shared_info || va == xen_info)) ||
12045084Sjohnlev 	    (pfn_is_foreign(hat_getpfnum(kas.a_hat, va))))
12055084Sjohnlev 		return (uiomove(va, nbytes, rw, uio));
12065084Sjohnlev #endif
12075084Sjohnlev 	return (ENOTSUP);
12085084Sjohnlev }
12095084Sjohnlev 
12105084Sjohnlev pgcnt_t
num_phys_pages()12115084Sjohnlev num_phys_pages()
12123446Smrj {
12135084Sjohnlev 	pgcnt_t npages = 0;
12145084Sjohnlev 	struct memlist *mp;
12155084Sjohnlev 
12165084Sjohnlev #if defined(__xpv)
121710175SStuart.Maybee@Sun.COM 	if (DOMAIN_IS_INITDOMAIN(xen_info))
121810175SStuart.Maybee@Sun.COM 		return (xpv_nr_phys_pages());
12195084Sjohnlev #endif /* __xpv */
12205084Sjohnlev 
122111474SJonathan.Adams@Sun.COM 	for (mp = phys_install; mp != NULL; mp = mp->ml_next)
122211474SJonathan.Adams@Sun.COM 		npages += mp->ml_size >> PAGESHIFT;
12235084Sjohnlev 
12245084Sjohnlev 	return (npages);
12253446Smrj }
12263446Smrj 
122710843SDave.Plauger@Sun.COM /* cpu threshold for compressed dumps */
122810843SDave.Plauger@Sun.COM #ifdef _LP64
1229*12931SDave.Plauger@Sun.COM uint_t dump_plat_mincpu_default = DUMP_PLAT_X86_64_MINCPU;
123010843SDave.Plauger@Sun.COM #else
1231*12931SDave.Plauger@Sun.COM uint_t dump_plat_mincpu_default = DUMP_PLAT_X86_32_MINCPU;
123210843SDave.Plauger@Sun.COM #endif
123310843SDave.Plauger@Sun.COM 
12343446Smrj int
dump_plat_addr()12353446Smrj dump_plat_addr()
12363446Smrj {
12375084Sjohnlev #ifdef __xpv
12385084Sjohnlev 	pfn_t pfn = mmu_btop(xen_info->shared_info) | PFN_IS_FOREIGN_MFN;
12395084Sjohnlev 	mem_vtop_t mem_vtop;
12405084Sjohnlev 	int cnt;
12415084Sjohnlev 
12425084Sjohnlev 	/*
12435084Sjohnlev 	 * On the hypervisor, we want to dump the page with shared_info on it.
12445084Sjohnlev 	 */
12455084Sjohnlev 	if (!IN_XPV_PANIC()) {
12465084Sjohnlev 		mem_vtop.m_as = &kas;
12475084Sjohnlev 		mem_vtop.m_va = HYPERVISOR_shared_info;
12485084Sjohnlev 		mem_vtop.m_pfn = pfn;
12495084Sjohnlev 		dumpvp_write(&mem_vtop, sizeof (mem_vtop_t));
12505084Sjohnlev 		cnt = 1;
12515084Sjohnlev 	} else {
12525084Sjohnlev 		cnt = dump_xpv_addr();
12535084Sjohnlev 	}
12545084Sjohnlev 	return (cnt);
12555084Sjohnlev #else
12563446Smrj 	return (0);
12575084Sjohnlev #endif
12583446Smrj }
12593446Smrj 
12603446Smrj void
dump_plat_pfn()12613446Smrj dump_plat_pfn()
12623446Smrj {
12635084Sjohnlev #ifdef __xpv
12645084Sjohnlev 	pfn_t pfn = mmu_btop(xen_info->shared_info) | PFN_IS_FOREIGN_MFN;
12655084Sjohnlev 
12665084Sjohnlev 	if (!IN_XPV_PANIC())
12675084Sjohnlev 		dumpvp_write(&pfn, sizeof (pfn));
12685084Sjohnlev 	else
12695084Sjohnlev 		dump_xpv_pfn();
12705084Sjohnlev #endif
12713446Smrj }
12723446Smrj 
12733446Smrj /*ARGSUSED*/
12743446Smrj int
dump_plat_data(void * dump_cbuf)12753446Smrj dump_plat_data(void *dump_cbuf)
12763446Smrj {
12775084Sjohnlev #ifdef __xpv
12785084Sjohnlev 	uint32_t csize;
12795084Sjohnlev 	int cnt;
12805084Sjohnlev 
12815084Sjohnlev 	if (!IN_XPV_PANIC()) {
12825084Sjohnlev 		csize = (uint32_t)compress(HYPERVISOR_shared_info, dump_cbuf,
12835084Sjohnlev 		    PAGESIZE);
12845084Sjohnlev 		dumpvp_write(&csize, sizeof (uint32_t));
12855084Sjohnlev 		dumpvp_write(dump_cbuf, csize);
12865084Sjohnlev 		cnt = 1;
12875084Sjohnlev 	} else {
12885084Sjohnlev 		cnt = dump_xpv_data(dump_cbuf);
12895084Sjohnlev 	}
12905084Sjohnlev 	return (cnt);
12915084Sjohnlev #else
12923446Smrj 	return (0);
12935084Sjohnlev #endif
12943446Smrj }
12953939Ssethg 
12963939Ssethg /*
12973939Ssethg  * Calculates a linear address, given the CS selector and PC values
12983939Ssethg  * by looking up the %cs selector process's LDT or the CPU's GDT.
12993939Ssethg  * proc->p_ldtlock must be held across this call.
13003939Ssethg  */
13013939Ssethg int
linear_pc(struct regs * rp,proc_t * p,caddr_t * linearp)13023939Ssethg linear_pc(struct regs *rp, proc_t *p, caddr_t *linearp)
13033939Ssethg {
13043939Ssethg 	user_desc_t	*descrp;
13053939Ssethg 	caddr_t		baseaddr;
13063939Ssethg 	uint16_t	idx = SELTOIDX(rp->r_cs);
13073939Ssethg 
13083939Ssethg 	ASSERT(rp->r_cs <= 0xFFFF);
13093939Ssethg 	ASSERT(MUTEX_HELD(&p->p_ldtlock));
13103939Ssethg 
13113939Ssethg 	if (SELISLDT(rp->r_cs)) {
13123939Ssethg 		/*
13133939Ssethg 		 * Currently 64 bit processes cannot have private LDTs.
13143939Ssethg 		 */
13153939Ssethg 		ASSERT(p->p_model != DATAMODEL_LP64);
13163939Ssethg 
13173939Ssethg 		if (p->p_ldt == NULL)
13183939Ssethg 			return (-1);
13193939Ssethg 
13203939Ssethg 		descrp = &p->p_ldt[idx];
13213939Ssethg 		baseaddr = (caddr_t)(uintptr_t)USEGD_GETBASE(descrp);
13223939Ssethg 
13233939Ssethg 		/*
13243939Ssethg 		 * Calculate the linear address (wraparound is not only ok,
13253939Ssethg 		 * it's expected behavior).  The cast to uint32_t is because
13263939Ssethg 		 * LDT selectors are only allowed in 32-bit processes.
13273939Ssethg 		 */
13283939Ssethg 		*linearp = (caddr_t)(uintptr_t)(uint32_t)((uintptr_t)baseaddr +
13293939Ssethg 		    rp->r_pc);
13303939Ssethg 	} else {
13313939Ssethg #ifdef DEBUG
13323939Ssethg 		descrp = &CPU->cpu_gdt[idx];
13333939Ssethg 		baseaddr = (caddr_t)(uintptr_t)USEGD_GETBASE(descrp);
13343939Ssethg 		/* GDT-based descriptors' base addresses should always be 0 */
13353939Ssethg 		ASSERT(baseaddr == 0);
13363939Ssethg #endif
13373939Ssethg 		*linearp = (caddr_t)(uintptr_t)rp->r_pc;
13383939Ssethg 	}
13393939Ssethg 
13403939Ssethg 	return (0);
13413939Ssethg }
13423939Ssethg 
13433939Ssethg /*
13443939Ssethg  * The implementation of dtrace_linear_pc is similar to the that of
13453939Ssethg  * linear_pc, above, but here we acquire p_ldtlock before accessing
13463939Ssethg  * p_ldt.  This implementation is used by the pid provider; we prefix
13473939Ssethg  * it with "dtrace_" to avoid inducing spurious tracing events.
13483939Ssethg  */
13493939Ssethg int
dtrace_linear_pc(struct regs * rp,proc_t * p,caddr_t * linearp)13503939Ssethg dtrace_linear_pc(struct regs *rp, proc_t *p, caddr_t *linearp)
13513939Ssethg {
13523939Ssethg 	user_desc_t	*descrp;
13533939Ssethg 	caddr_t		baseaddr;
13543939Ssethg 	uint16_t	idx = SELTOIDX(rp->r_cs);
13553939Ssethg 
13563939Ssethg 	ASSERT(rp->r_cs <= 0xFFFF);
13573939Ssethg 
13583939Ssethg 	if (SELISLDT(rp->r_cs)) {
13593939Ssethg 		/*
13603939Ssethg 		 * Currently 64 bit processes cannot have private LDTs.
13613939Ssethg 		 */
13623939Ssethg 		ASSERT(p->p_model != DATAMODEL_LP64);
13633939Ssethg 
13643939Ssethg 		mutex_enter(&p->p_ldtlock);
13653939Ssethg 		if (p->p_ldt == NULL) {
13663939Ssethg 			mutex_exit(&p->p_ldtlock);
13673939Ssethg 			return (-1);
13683939Ssethg 		}
13693939Ssethg 		descrp = &p->p_ldt[idx];
13703939Ssethg 		baseaddr = (caddr_t)(uintptr_t)USEGD_GETBASE(descrp);
13713939Ssethg 		mutex_exit(&p->p_ldtlock);
13723939Ssethg 
13733939Ssethg 		/*
13743939Ssethg 		 * Calculate the linear address (wraparound is not only ok,
13753939Ssethg 		 * it's expected behavior).  The cast to uint32_t is because
13763939Ssethg 		 * LDT selectors are only allowed in 32-bit processes.
13773939Ssethg 		 */
13783939Ssethg 		*linearp = (caddr_t)(uintptr_t)(uint32_t)((uintptr_t)baseaddr +
13793939Ssethg 		    rp->r_pc);
13803939Ssethg 	} else {
13813939Ssethg #ifdef DEBUG
13823939Ssethg 		descrp = &CPU->cpu_gdt[idx];
13833939Ssethg 		baseaddr = (caddr_t)(uintptr_t)USEGD_GETBASE(descrp);
13843939Ssethg 		/* GDT-based descriptors' base addresses should always be 0 */
13853939Ssethg 		ASSERT(baseaddr == 0);
13863939Ssethg #endif
13873939Ssethg 		*linearp = (caddr_t)(uintptr_t)rp->r_pc;
13883939Ssethg 	}
13893939Ssethg 
13903939Ssethg 	return (0);
13913939Ssethg }
139211066Srafael.vanoni@sun.com 
139311066Srafael.vanoni@sun.com /*
139411066Srafael.vanoni@sun.com  * We need to post a soft interrupt to reprogram the lbolt cyclic when
139511066Srafael.vanoni@sun.com  * switching from event to cyclic driven lbolt. The following code adds
139611066Srafael.vanoni@sun.com  * and posts the softint for x86.
139711066Srafael.vanoni@sun.com  */
139811066Srafael.vanoni@sun.com static ddi_softint_hdl_impl_t lbolt_softint_hdl =
139911066Srafael.vanoni@sun.com 	{0, NULL, NULL, NULL, 0, NULL, NULL, NULL};
140011066Srafael.vanoni@sun.com 
140111066Srafael.vanoni@sun.com void
lbolt_softint_add(void)140211066Srafael.vanoni@sun.com lbolt_softint_add(void)
140311066Srafael.vanoni@sun.com {
140411066Srafael.vanoni@sun.com 	(void) add_avsoftintr((void *)&lbolt_softint_hdl, LOCK_LEVEL,
140511066Srafael.vanoni@sun.com 	    (avfunc)lbolt_ev_to_cyclic, "lbolt_ev_to_cyclic", NULL, NULL);
140611066Srafael.vanoni@sun.com }
140711066Srafael.vanoni@sun.com 
140811066Srafael.vanoni@sun.com void
lbolt_softint_post(void)140911066Srafael.vanoni@sun.com lbolt_softint_post(void)
141011066Srafael.vanoni@sun.com {
141111066Srafael.vanoni@sun.com 	(*setsoftint)(CBE_LOCK_PIL, lbolt_softint_hdl.ih_pending);
141211066Srafael.vanoni@sun.com }
141312004Sjiang.liu@intel.com 
141412004Sjiang.liu@intel.com boolean_t
plat_dr_check_capability(uint64_t features)141512004Sjiang.liu@intel.com plat_dr_check_capability(uint64_t features)
141612004Sjiang.liu@intel.com {
141712004Sjiang.liu@intel.com 	return ((plat_dr_options & features) == features);
141812004Sjiang.liu@intel.com }
141912004Sjiang.liu@intel.com 
142012004Sjiang.liu@intel.com boolean_t
plat_dr_support_cpu(void)142112004Sjiang.liu@intel.com plat_dr_support_cpu(void)
142212004Sjiang.liu@intel.com {
142312004Sjiang.liu@intel.com 	return (plat_dr_options & PLAT_DR_FEATURE_CPU);
142412004Sjiang.liu@intel.com }
142512004Sjiang.liu@intel.com 
142612004Sjiang.liu@intel.com boolean_t
plat_dr_support_memory(void)142712004Sjiang.liu@intel.com plat_dr_support_memory(void)
142812004Sjiang.liu@intel.com {
142912004Sjiang.liu@intel.com 	return (plat_dr_options & PLAT_DR_FEATURE_MEMORY);
143012004Sjiang.liu@intel.com }
143112004Sjiang.liu@intel.com 
143212004Sjiang.liu@intel.com void
plat_dr_enable_capability(uint64_t features)143312004Sjiang.liu@intel.com plat_dr_enable_capability(uint64_t features)
143412004Sjiang.liu@intel.com {
143512004Sjiang.liu@intel.com 	atomic_or_64(&plat_dr_options, features);
143612004Sjiang.liu@intel.com }
143712004Sjiang.liu@intel.com 
143812004Sjiang.liu@intel.com void
plat_dr_disable_capability(uint64_t features)143912004Sjiang.liu@intel.com plat_dr_disable_capability(uint64_t features)
144012004Sjiang.liu@intel.com {
144112004Sjiang.liu@intel.com 	atomic_and_64(&plat_dr_options, ~features);
144212004Sjiang.liu@intel.com }
1443