xref: /onnv-gate/usr/src/uts/i86pc/os/machdep.c (revision 11861:a63258283f8f)
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 /*
2311474SJonathan.Adams@Sun.COM  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #include <sys/types.h>
280Sstevel@tonic-gate #include <sys/t_lock.h>
290Sstevel@tonic-gate #include <sys/param.h>
303939Ssethg #include <sys/segments.h>
310Sstevel@tonic-gate #include <sys/sysmacros.h>
320Sstevel@tonic-gate #include <sys/signal.h>
330Sstevel@tonic-gate #include <sys/systm.h>
340Sstevel@tonic-gate #include <sys/user.h>
350Sstevel@tonic-gate #include <sys/mman.h>
360Sstevel@tonic-gate #include <sys/vm.h>
370Sstevel@tonic-gate 
380Sstevel@tonic-gate #include <sys/disp.h>
390Sstevel@tonic-gate #include <sys/class.h>
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #include <sys/proc.h>
420Sstevel@tonic-gate #include <sys/buf.h>
430Sstevel@tonic-gate #include <sys/kmem.h>
440Sstevel@tonic-gate 
450Sstevel@tonic-gate #include <sys/reboot.h>
460Sstevel@tonic-gate #include <sys/uadmin.h>
470Sstevel@tonic-gate #include <sys/callb.h>
480Sstevel@tonic-gate 
490Sstevel@tonic-gate #include <sys/cred.h>
500Sstevel@tonic-gate #include <sys/vnode.h>
510Sstevel@tonic-gate #include <sys/file.h>
520Sstevel@tonic-gate 
530Sstevel@tonic-gate #include <sys/procfs.h>
540Sstevel@tonic-gate #include <sys/acct.h>
550Sstevel@tonic-gate 
560Sstevel@tonic-gate #include <sys/vfs.h>
570Sstevel@tonic-gate #include <sys/dnlc.h>
580Sstevel@tonic-gate #include <sys/var.h>
590Sstevel@tonic-gate #include <sys/cmn_err.h>
600Sstevel@tonic-gate #include <sys/utsname.h>
610Sstevel@tonic-gate #include <sys/debug.h>
620Sstevel@tonic-gate 
630Sstevel@tonic-gate #include <sys/dumphdr.h>
640Sstevel@tonic-gate #include <sys/bootconf.h>
650Sstevel@tonic-gate #include <sys/varargs.h>
660Sstevel@tonic-gate #include <sys/promif.h>
670Sstevel@tonic-gate #include <sys/modctl.h>
680Sstevel@tonic-gate 
690Sstevel@tonic-gate #include <sys/consdev.h>
700Sstevel@tonic-gate #include <sys/frame.h>
710Sstevel@tonic-gate 
720Sstevel@tonic-gate #include <sys/sunddi.h>
730Sstevel@tonic-gate #include <sys/ddidmareq.h>
740Sstevel@tonic-gate #include <sys/psw.h>
750Sstevel@tonic-gate #include <sys/regset.h>
760Sstevel@tonic-gate #include <sys/privregs.h>
770Sstevel@tonic-gate #include <sys/clock.h>
780Sstevel@tonic-gate #include <sys/tss.h>
790Sstevel@tonic-gate #include <sys/cpu.h>
800Sstevel@tonic-gate #include <sys/stack.h>
810Sstevel@tonic-gate #include <sys/trap.h>
820Sstevel@tonic-gate #include <sys/pic.h>
830Sstevel@tonic-gate #include <vm/hat.h>
840Sstevel@tonic-gate #include <vm/anon.h>
850Sstevel@tonic-gate #include <vm/as.h>
860Sstevel@tonic-gate #include <vm/page.h>
870Sstevel@tonic-gate #include <vm/seg.h>
880Sstevel@tonic-gate #include <vm/seg_kmem.h>
890Sstevel@tonic-gate #include <vm/seg_map.h>
900Sstevel@tonic-gate #include <vm/seg_vn.h>
910Sstevel@tonic-gate #include <vm/seg_kp.h>
920Sstevel@tonic-gate #include <vm/hat_i86.h>
930Sstevel@tonic-gate #include <sys/swap.h>
940Sstevel@tonic-gate #include <sys/thread.h>
950Sstevel@tonic-gate #include <sys/sysconf.h>
960Sstevel@tonic-gate #include <sys/vm_machparam.h>
970Sstevel@tonic-gate #include <sys/archsystm.h>
980Sstevel@tonic-gate #include <sys/machsystm.h>
990Sstevel@tonic-gate #include <sys/machlock.h>
1000Sstevel@tonic-gate #include <sys/x_call.h>
1010Sstevel@tonic-gate #include <sys/instance.h>
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate #include <sys/time.h>
1040Sstevel@tonic-gate #include <sys/smp_impldefs.h>
1050Sstevel@tonic-gate #include <sys/psm_types.h>
1060Sstevel@tonic-gate #include <sys/atomic.h>
1070Sstevel@tonic-gate #include <sys/panic.h>
1080Sstevel@tonic-gate #include <sys/cpuvar.h>
1090Sstevel@tonic-gate #include <sys/dtrace.h>
1100Sstevel@tonic-gate #include <sys/bl.h>
1110Sstevel@tonic-gate #include <sys/nvpair.h>
1120Sstevel@tonic-gate #include <sys/x86_archext.h>
1130Sstevel@tonic-gate #include <sys/pool_pset.h>
1140Sstevel@tonic-gate #include <sys/autoconf.h>
1153446Smrj #include <sys/mem.h>
1163446Smrj #include <sys/dumphdr.h>
1173446Smrj #include <sys/compress.h>
1187532SSean.Ye@Sun.COM #include <sys/cpu_module.h>
1195084Sjohnlev #if defined(__xpv)
1205084Sjohnlev #include <sys/hypervisor.h>
1215084Sjohnlev #include <sys/xpv_panic.h>
1225084Sjohnlev #endif
1230Sstevel@tonic-gate 
1247656SSherry.Moore@Sun.COM #include <sys/fastboot.h>
1257656SSherry.Moore@Sun.COM #include <sys/machelf.h>
1267656SSherry.Moore@Sun.COM #include <sys/kobj.h>
1277656SSherry.Moore@Sun.COM #include <sys/multiboot.h>
1287656SSherry.Moore@Sun.COM 
1290Sstevel@tonic-gate #ifdef	TRAPTRACE
1300Sstevel@tonic-gate #include <sys/traptrace.h>
1310Sstevel@tonic-gate #endif	/* TRAPTRACE */
1320Sstevel@tonic-gate 
133*11861SMarek.Pospisil@Sun.COM #include <c2/audit.h>
13411066Srafael.vanoni@sun.com #include <sys/clock_impl.h>
13511066Srafael.vanoni@sun.com 
1360Sstevel@tonic-gate extern void audit_enterprom(int);
1370Sstevel@tonic-gate extern void audit_exitprom(int);
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate /*
1406681Sjohnlev  * Occassionally the kernel knows better whether to power-off or reboot.
1416681Sjohnlev  */
1426681Sjohnlev int force_shutdown_method = AD_UNKNOWN;
1436681Sjohnlev 
1446681Sjohnlev /*
1450Sstevel@tonic-gate  * The panicbuf array is used to record messages and state:
1460Sstevel@tonic-gate  */
1470Sstevel@tonic-gate char panicbuf[PANICBUFSIZE];
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate /*
1500Sstevel@tonic-gate  * maxphys - used during physio
1510Sstevel@tonic-gate  * klustsize - used for klustering by swapfs and specfs
1520Sstevel@tonic-gate  */
1530Sstevel@tonic-gate int maxphys = 56 * 1024;    /* XXX See vm_subr.c - max b_count in physio */
1540Sstevel@tonic-gate int klustsize = 56 * 1024;
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate caddr_t	p0_va;		/* Virtual address for accessing physical page 0 */
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate /*
1590Sstevel@tonic-gate  * defined here, though unused on x86,
1600Sstevel@tonic-gate  * to make kstat_fr.c happy.
1610Sstevel@tonic-gate  */
1620Sstevel@tonic-gate int vac;
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate void debug_enter(char *);
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate extern void pm_cfb_check_and_powerup(void);
1670Sstevel@tonic-gate extern void pm_cfb_rele(void);
1680Sstevel@tonic-gate 
1697656SSherry.Moore@Sun.COM extern fastboot_info_t newkernel;
1707656SSherry.Moore@Sun.COM 
1710Sstevel@tonic-gate /*
1720Sstevel@tonic-gate  * Machine dependent code to reboot.
1730Sstevel@tonic-gate  * "mdep" is interpreted as a character pointer; if non-null, it is a pointer
1740Sstevel@tonic-gate  * to a string to be used as the argument string when rebooting.
175136Sachartre  *
176136Sachartre  * "invoke_cb" is a boolean. It is set to true when mdboot() can safely
177136Sachartre  * invoke CB_CL_MDBOOT callbacks before shutting the system down, i.e. when
178136Sachartre  * we are in a normal shutdown sequence (interrupts are not blocked, the
179136Sachartre  * system is not panic'ing or being suspended).
1800Sstevel@tonic-gate  */
1810Sstevel@tonic-gate /*ARGSUSED*/
1820Sstevel@tonic-gate void
183136Sachartre mdboot(int cmd, int fcn, char *mdep, boolean_t invoke_cb)
1840Sstevel@tonic-gate {
1857656SSherry.Moore@Sun.COM 	processorid_t bootcpuid = 0;
1867863SSherry.Moore@Sun.COM 	static int is_first_quiesce = 1;
1877863SSherry.Moore@Sun.COM 	static int is_first_reset = 1;
1887863SSherry.Moore@Sun.COM 	int reset_status = 0;
1899160SSherry.Moore@Sun.COM 	static char fallback_str[] = "Falling back to regular reboot.\n";
1907656SSherry.Moore@Sun.COM 
1917656SSherry.Moore@Sun.COM 	if (fcn == AD_FASTREBOOT && !newkernel.fi_valid)
1927656SSherry.Moore@Sun.COM 		fcn = AD_BOOT;
1937656SSherry.Moore@Sun.COM 
1940Sstevel@tonic-gate 	if (!panicstr) {
1950Sstevel@tonic-gate 		kpreempt_disable();
1967656SSherry.Moore@Sun.COM 		if (fcn == AD_FASTREBOOT) {
1977656SSherry.Moore@Sun.COM 			mutex_enter(&cpu_lock);
1987656SSherry.Moore@Sun.COM 			if (CPU_ACTIVE(cpu_get(bootcpuid))) {
1997656SSherry.Moore@Sun.COM 				affinity_set(bootcpuid);
2007656SSherry.Moore@Sun.COM 			}
2017656SSherry.Moore@Sun.COM 			mutex_exit(&cpu_lock);
2027656SSherry.Moore@Sun.COM 		} else {
2037656SSherry.Moore@Sun.COM 			affinity_set(CPU_CURRENT);
2047656SSherry.Moore@Sun.COM 		}
2050Sstevel@tonic-gate 	}
2060Sstevel@tonic-gate 
2076681Sjohnlev 	if (force_shutdown_method != AD_UNKNOWN)
2086681Sjohnlev 		fcn = force_shutdown_method;
2096681Sjohnlev 
2100Sstevel@tonic-gate 	/*
2115630Sjbeck 	 * XXX - rconsvp is set to NULL to ensure that output messages
2125630Sjbeck 	 * are sent to the underlying "hardware" device using the
2135630Sjbeck 	 * monitor's printf routine since we are in the process of
2145630Sjbeck 	 * either rebooting or halting the machine.
2155630Sjbeck 	 */
2165630Sjbeck 	rconsvp = NULL;
2175630Sjbeck 
2185630Sjbeck 	/*
2190Sstevel@tonic-gate 	 * Print the reboot message now, before pausing other cpus.
2200Sstevel@tonic-gate 	 * There is a race condition in the printing support that
2210Sstevel@tonic-gate 	 * can deadlock multiprocessor machines.
2220Sstevel@tonic-gate 	 */
2230Sstevel@tonic-gate 	if (!(fcn == AD_HALT || fcn == AD_POWEROFF))
2240Sstevel@tonic-gate 		prom_printf("rebooting...\n");
2250Sstevel@tonic-gate 
2265084Sjohnlev 	if (IN_XPV_PANIC())
2275084Sjohnlev 		reset();
2285084Sjohnlev 
2290Sstevel@tonic-gate 	/*
2300Sstevel@tonic-gate 	 * We can't bring up the console from above lock level, so do it now
2310Sstevel@tonic-gate 	 */
2320Sstevel@tonic-gate 	pm_cfb_check_and_powerup();
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 	/* make sure there are no more changes to the device tree */
2350Sstevel@tonic-gate 	devtree_freeze();
2360Sstevel@tonic-gate 
237136Sachartre 	if (invoke_cb)
238136Sachartre 		(void) callb_execute_class(CB_CL_MDBOOT, NULL);
239136Sachartre 
2403253Smec 	/*
2413253Smec 	 * Clear any unresolved UEs from memory.
2423253Smec 	 */
2433253Smec 	page_retire_mdboot();
244917Selowe 
2455084Sjohnlev #if defined(__xpv)
2465084Sjohnlev 	/*
2475084Sjohnlev 	 * XXPV	Should probably think some more about how we deal
2485084Sjohnlev 	 *	with panicing before it's really safe to panic.
2495084Sjohnlev 	 *	On hypervisors, we reboot very quickly..  Perhaps panic
2505084Sjohnlev 	 *	should only attempt to recover by rebooting if,
2515084Sjohnlev 	 *	say, we were able to mount the root filesystem,
2525084Sjohnlev 	 *	or if we successfully launched init(1m).
2535084Sjohnlev 	 */
2545084Sjohnlev 	if (panicstr && proc_init == NULL)
2555084Sjohnlev 		(void) HYPERVISOR_shutdown(SHUTDOWN_poweroff);
2565084Sjohnlev #endif
2570Sstevel@tonic-gate 	/*
2580Sstevel@tonic-gate 	 * stop other cpus and raise our priority.  since there is only
2590Sstevel@tonic-gate 	 * one active cpu after this, and our priority will be too high
2600Sstevel@tonic-gate 	 * for us to be preempted, we're essentially single threaded
2610Sstevel@tonic-gate 	 * from here on out.
2620Sstevel@tonic-gate 	 */
2630Sstevel@tonic-gate 	(void) spl6();
2640Sstevel@tonic-gate 	if (!panicstr) {
2650Sstevel@tonic-gate 		mutex_enter(&cpu_lock);
2660Sstevel@tonic-gate 		pause_cpus(NULL);
2670Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
2680Sstevel@tonic-gate 	}
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate 	/*
27110916SSherry.Moore@Sun.COM 	 * If the system is panicking, the preloaded kernel is valid, and
27210916SSherry.Moore@Sun.COM 	 * fastreboot_onpanic has been set, and the system has been up for
27310916SSherry.Moore@Sun.COM 	 * longer than fastreboot_onpanic_uptime (default to 10 minutes),
27410916SSherry.Moore@Sun.COM 	 * choose Fast Reboot.
2759160SSherry.Moore@Sun.COM 	 */
2769160SSherry.Moore@Sun.COM 	if (fcn == AD_BOOT && panicstr && newkernel.fi_valid &&
27710916SSherry.Moore@Sun.COM 	    fastreboot_onpanic &&
27810916SSherry.Moore@Sun.COM 	    (panic_lbolt - lbolt_at_boot) > fastreboot_onpanic_uptime) {
2799160SSherry.Moore@Sun.COM 		fcn = AD_FASTREBOOT;
28010916SSherry.Moore@Sun.COM 	}
2819160SSherry.Moore@Sun.COM 
2829160SSherry.Moore@Sun.COM 	/*
2837656SSherry.Moore@Sun.COM 	 * Try to quiesce devices.
2840Sstevel@tonic-gate 	 */
2857863SSherry.Moore@Sun.COM 	if (is_first_quiesce) {
2867863SSherry.Moore@Sun.COM 		/*
2877863SSherry.Moore@Sun.COM 		 * Clear is_first_quiesce before calling quiesce_devices()
2887863SSherry.Moore@Sun.COM 		 * so that if quiesce_devices() causes panics, it will not
2897863SSherry.Moore@Sun.COM 		 * be invoked again.
2907863SSherry.Moore@Sun.COM 		 */
2917863SSherry.Moore@Sun.COM 		is_first_quiesce = 0;
2927656SSherry.Moore@Sun.COM 
2937656SSherry.Moore@Sun.COM 		quiesce_active = 1;
2947656SSherry.Moore@Sun.COM 		quiesce_devices(ddi_root_node(), &reset_status);
2957783SSherry.Moore@Sun.COM 		if (reset_status == -1) {
2967783SSherry.Moore@Sun.COM 			if (fcn == AD_FASTREBOOT && !force_fastreboot) {
2977783SSherry.Moore@Sun.COM 				prom_printf("Driver(s) not capable of fast "
2989160SSherry.Moore@Sun.COM 				    "reboot.\n");
2999160SSherry.Moore@Sun.COM 				prom_printf(fallback_str);
3007783SSherry.Moore@Sun.COM 				fastreboot_capable = 0;
3019160SSherry.Moore@Sun.COM 				fcn = AD_BOOT;
3027783SSherry.Moore@Sun.COM 			} else if (fcn != AD_FASTREBOOT)
3037783SSherry.Moore@Sun.COM 				fastreboot_capable = 0;
3047656SSherry.Moore@Sun.COM 		}
3057656SSherry.Moore@Sun.COM 		quiesce_active = 0;
3067656SSherry.Moore@Sun.COM 	}
3077656SSherry.Moore@Sun.COM 
3087656SSherry.Moore@Sun.COM 	/*
3097863SSherry.Moore@Sun.COM 	 * Try to reset devices. reset_leaves() should only be called
3107863SSherry.Moore@Sun.COM 	 * a) when there are no other threads that could be accessing devices,
3117863SSherry.Moore@Sun.COM 	 *    and
3127863SSherry.Moore@Sun.COM 	 * b) on a system that's not capable of fast reboot (fastreboot_capable
3137863SSherry.Moore@Sun.COM 	 *    being 0), or on a system where quiesce_devices() failed to
3147863SSherry.Moore@Sun.COM 	 *    complete (quiesce_active being 1).
3157656SSherry.Moore@Sun.COM 	 */
3167863SSherry.Moore@Sun.COM 	if (is_first_reset && (!fastreboot_capable || quiesce_active)) {
3177863SSherry.Moore@Sun.COM 		/*
3187863SSherry.Moore@Sun.COM 		 * Clear is_first_reset before calling reset_devices()
3197863SSherry.Moore@Sun.COM 		 * so that if reset_devices() causes panics, it will not
3207863SSherry.Moore@Sun.COM 		 * be invoked again.
3217863SSherry.Moore@Sun.COM 		 */
3227863SSherry.Moore@Sun.COM 		is_first_reset = 0;
3237656SSherry.Moore@Sun.COM 		reset_leaves();
3247863SSherry.Moore@Sun.COM 	}
3250Sstevel@tonic-gate 
3269160SSherry.Moore@Sun.COM 	/* Verify newkernel checksum */
3279160SSherry.Moore@Sun.COM 	if (fastreboot_capable && fcn == AD_FASTREBOOT &&
3289160SSherry.Moore@Sun.COM 	    fastboot_cksum_verify(&newkernel) != 0) {
3299160SSherry.Moore@Sun.COM 		fastreboot_capable = 0;
3309160SSherry.Moore@Sun.COM 		prom_printf("Fast reboot: checksum failed for the new "
3319160SSherry.Moore@Sun.COM 		    "kernel.\n");
3329160SSherry.Moore@Sun.COM 		prom_printf(fallback_str);
3339160SSherry.Moore@Sun.COM 	}
3349160SSherry.Moore@Sun.COM 
3350Sstevel@tonic-gate 	(void) spl8();
3360Sstevel@tonic-gate 
3379160SSherry.Moore@Sun.COM 	if (fastreboot_capable && fcn == AD_FASTREBOOT) {
3389160SSherry.Moore@Sun.COM 		/*
3399160SSherry.Moore@Sun.COM 		 * psm_shutdown is called within fast_reboot()
3409160SSherry.Moore@Sun.COM 		 */
3417656SSherry.Moore@Sun.COM 		fast_reboot();
3429160SSherry.Moore@Sun.COM 	} else {
3439160SSherry.Moore@Sun.COM 		(*psm_shutdownf)(cmd, fcn);
3449160SSherry.Moore@Sun.COM 
3459160SSherry.Moore@Sun.COM 		if (fcn == AD_HALT || fcn == AD_POWEROFF)
3469160SSherry.Moore@Sun.COM 			halt((char *)NULL);
3479160SSherry.Moore@Sun.COM 		else
3489160SSherry.Moore@Sun.COM 			prom_reboot("");
3499160SSherry.Moore@Sun.COM 	}
3500Sstevel@tonic-gate 	/*NOTREACHED*/
3510Sstevel@tonic-gate }
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate /* mdpreboot - may be called prior to mdboot while root fs still mounted */
3540Sstevel@tonic-gate /*ARGSUSED*/
3550Sstevel@tonic-gate void
3560Sstevel@tonic-gate mdpreboot(int cmd, int fcn, char *mdep)
3570Sstevel@tonic-gate {
3587656SSherry.Moore@Sun.COM 	if (fcn == AD_FASTREBOOT && !fastreboot_capable) {
3597656SSherry.Moore@Sun.COM 		fcn = AD_BOOT;
3607656SSherry.Moore@Sun.COM #ifdef	__xpv
3619160SSherry.Moore@Sun.COM 		cmn_err(CE_WARN, "Fast reboot is not supported on xVM");
3627656SSherry.Moore@Sun.COM #else
3639160SSherry.Moore@Sun.COM 		cmn_err(CE_WARN,
36411686SKonstantin.Ananyev@Sun.COM 		    "Fast reboot is not supported on this platform%s",
36511686SKonstantin.Ananyev@Sun.COM 		    fastreboot_nosup_message());
3667656SSherry.Moore@Sun.COM #endif
3677656SSherry.Moore@Sun.COM 	}
3687656SSherry.Moore@Sun.COM 
3697656SSherry.Moore@Sun.COM 	if (fcn == AD_FASTREBOOT) {
3709160SSherry.Moore@Sun.COM 		fastboot_load_kernel(mdep);
3717656SSherry.Moore@Sun.COM 		if (!newkernel.fi_valid)
3727656SSherry.Moore@Sun.COM 			fcn = AD_BOOT;
3737656SSherry.Moore@Sun.COM 	}
3747656SSherry.Moore@Sun.COM 
3750Sstevel@tonic-gate 	(*psm_preshutdownf)(cmd, fcn);
3760Sstevel@tonic-gate }
3770Sstevel@tonic-gate 
3789489SJoe.Bonasera@sun.com static void
3799489SJoe.Bonasera@sun.com stop_other_cpus(void)
3800Sstevel@tonic-gate {
3819489SJoe.Bonasera@sun.com 	ulong_t s = clear_int_flag(); /* fast way to keep CPU from changing */
3820Sstevel@tonic-gate 	cpuset_t xcset;
3830Sstevel@tonic-gate 
3849489SJoe.Bonasera@sun.com 	CPUSET_ALL_BUT(xcset, CPU->cpu_id);
3859489SJoe.Bonasera@sun.com 	xc_priority(0, 0, 0, CPUSET2BV(xcset), (xc_func_t)mach_cpu_halt);
3869489SJoe.Bonasera@sun.com 	restore_int_flag(s);
3870Sstevel@tonic-gate }
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate /*
3900Sstevel@tonic-gate  *	Machine dependent abort sequence handling
3910Sstevel@tonic-gate  */
3920Sstevel@tonic-gate void
3930Sstevel@tonic-gate abort_sequence_enter(char *msg)
3940Sstevel@tonic-gate {
3950Sstevel@tonic-gate 	if (abort_enable == 0) {
396*11861SMarek.Pospisil@Sun.COM 		if (AU_ZONE_AUDITING(GET_KCTX_GZ))
3970Sstevel@tonic-gate 			audit_enterprom(0);
3980Sstevel@tonic-gate 		return;
3990Sstevel@tonic-gate 	}
400*11861SMarek.Pospisil@Sun.COM 	if (AU_ZONE_AUDITING(GET_KCTX_GZ))
4010Sstevel@tonic-gate 		audit_enterprom(1);
4020Sstevel@tonic-gate 	debug_enter(msg);
403*11861SMarek.Pospisil@Sun.COM 	if (AU_ZONE_AUDITING(GET_KCTX_GZ))
4040Sstevel@tonic-gate 		audit_exitprom(1);
4050Sstevel@tonic-gate }
4060Sstevel@tonic-gate 
4070Sstevel@tonic-gate /*
4080Sstevel@tonic-gate  * Enter debugger.  Called when the user types ctrl-alt-d or whenever
4090Sstevel@tonic-gate  * code wants to enter the debugger and possibly resume later.
4100Sstevel@tonic-gate  */
4110Sstevel@tonic-gate void
4120Sstevel@tonic-gate debug_enter(
4130Sstevel@tonic-gate 	char	*msg)		/* message to print, possibly NULL */
4140Sstevel@tonic-gate {
4150Sstevel@tonic-gate 	if (dtrace_debugger_init != NULL)
4160Sstevel@tonic-gate 		(*dtrace_debugger_init)();
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate 	if (msg)
4190Sstevel@tonic-gate 		prom_printf("%s\n", msg);
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate 	if (boothowto & RB_DEBUG)
4223446Smrj 		kmdb_enter();
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate 	if (dtrace_debugger_fini != NULL)
4250Sstevel@tonic-gate 		(*dtrace_debugger_fini)();
4260Sstevel@tonic-gate }
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate void
4290Sstevel@tonic-gate reset(void)
4300Sstevel@tonic-gate {
43110457SSaurabh.Mishra@Sun.COM 	extern	void acpi_reset_system();
4325084Sjohnlev #if !defined(__xpv)
4330Sstevel@tonic-gate 	ushort_t *bios_memchk;
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate 	/*
43610457SSaurabh.Mishra@Sun.COM 	 * Can't use psm_map_phys or acpi_reset_system before the hat is
43710457SSaurabh.Mishra@Sun.COM 	 * initialized.
4380Sstevel@tonic-gate 	 */
4390Sstevel@tonic-gate 	if (khat_running) {
4400Sstevel@tonic-gate 		bios_memchk = (ushort_t *)psm_map_phys(0x472,
4410Sstevel@tonic-gate 		    sizeof (ushort_t), PROT_READ | PROT_WRITE);
4420Sstevel@tonic-gate 		if (bios_memchk)
4430Sstevel@tonic-gate 			*bios_memchk = 0x1234;	/* bios memory check disable */
44410457SSaurabh.Mishra@Sun.COM 
44510457SSaurabh.Mishra@Sun.COM 		if (options_dip != NULL &&
44610457SSaurabh.Mishra@Sun.COM 		    ddi_prop_exists(DDI_DEV_T_ANY, ddi_root_node(), 0,
44710457SSaurabh.Mishra@Sun.COM 		    "efi-systab")) {
44810457SSaurabh.Mishra@Sun.COM 			efi_reset();
44910457SSaurabh.Mishra@Sun.COM 		}
45010457SSaurabh.Mishra@Sun.COM 
45110457SSaurabh.Mishra@Sun.COM 		/*
45210457SSaurabh.Mishra@Sun.COM 		 * The problem with using stubs is that we can call
45310457SSaurabh.Mishra@Sun.COM 		 * acpi_reset_system only after the kernel is up and running.
45410457SSaurabh.Mishra@Sun.COM 		 *
45510457SSaurabh.Mishra@Sun.COM 		 * We should create a global state to keep track of how far
45610457SSaurabh.Mishra@Sun.COM 		 * up the kernel is but for the time being we will depend on
45710457SSaurabh.Mishra@Sun.COM 		 * bootops. bootops cleared in startup_end().
45810457SSaurabh.Mishra@Sun.COM 		 */
45910457SSaurabh.Mishra@Sun.COM 		if (bootops == NULL)
46010457SSaurabh.Mishra@Sun.COM 			acpi_reset_system();
4610Sstevel@tonic-gate 	}
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate 	pc_reset();
4645084Sjohnlev #else
46510457SSaurabh.Mishra@Sun.COM 	if (IN_XPV_PANIC()) {
46610457SSaurabh.Mishra@Sun.COM 		if (khat_running && bootops == NULL) {
46710457SSaurabh.Mishra@Sun.COM 			acpi_reset_system();
46810457SSaurabh.Mishra@Sun.COM 		}
46910457SSaurabh.Mishra@Sun.COM 
4705084Sjohnlev 		pc_reset();
47110457SSaurabh.Mishra@Sun.COM 	}
47210457SSaurabh.Mishra@Sun.COM 
4735084Sjohnlev 	(void) HYPERVISOR_shutdown(SHUTDOWN_reboot);
4745084Sjohnlev 	panic("HYPERVISOR_shutdown() failed");
4755084Sjohnlev #endif
4760Sstevel@tonic-gate 	/*NOTREACHED*/
4770Sstevel@tonic-gate }
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate /*
4800Sstevel@tonic-gate  * Halt the machine and return to the monitor
4810Sstevel@tonic-gate  */
4820Sstevel@tonic-gate void
4830Sstevel@tonic-gate halt(char *s)
4840Sstevel@tonic-gate {
4850Sstevel@tonic-gate 	stop_other_cpus();	/* send stop signal to other CPUs */
4860Sstevel@tonic-gate 	if (s)
4870Sstevel@tonic-gate 		prom_printf("(%s) \n", s);
4880Sstevel@tonic-gate 	prom_exit_to_mon();
4890Sstevel@tonic-gate 	/*NOTREACHED*/
4900Sstevel@tonic-gate }
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate /*
4930Sstevel@tonic-gate  * Initiate interrupt redistribution.
4940Sstevel@tonic-gate  */
4950Sstevel@tonic-gate void
4960Sstevel@tonic-gate i_ddi_intr_redist_all_cpus()
4970Sstevel@tonic-gate {
4980Sstevel@tonic-gate }
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate /*
5010Sstevel@tonic-gate  * XXX These probably ought to live somewhere else
5020Sstevel@tonic-gate  * XXX They are called from mem.c
5030Sstevel@tonic-gate  */
5040Sstevel@tonic-gate 
5050Sstevel@tonic-gate /*
5060Sstevel@tonic-gate  * Convert page frame number to an OBMEM page frame number
5070Sstevel@tonic-gate  * (i.e. put in the type bits -- zero for this implementation)
5080Sstevel@tonic-gate  */
5090Sstevel@tonic-gate pfn_t
5100Sstevel@tonic-gate impl_obmem_pfnum(pfn_t pf)
5110Sstevel@tonic-gate {
5120Sstevel@tonic-gate 	return (pf);
5130Sstevel@tonic-gate }
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate #ifdef	NM_DEBUG
5160Sstevel@tonic-gate int nmi_test = 0;	/* checked in intentry.s during clock int */
5170Sstevel@tonic-gate int nmtest = -1;
5180Sstevel@tonic-gate nmfunc1(arg, rp)
5190Sstevel@tonic-gate int	arg;
5200Sstevel@tonic-gate struct regs *rp;
5210Sstevel@tonic-gate {
5220Sstevel@tonic-gate 	printf("nmi called with arg = %x, regs = %x\n", arg, rp);
5230Sstevel@tonic-gate 	nmtest += 50;
5240Sstevel@tonic-gate 	if (arg == nmtest) {
5250Sstevel@tonic-gate 		printf("ip = %x\n", rp->r_pc);
5260Sstevel@tonic-gate 		return (1);
5270Sstevel@tonic-gate 	}
5280Sstevel@tonic-gate 	return (0);
5290Sstevel@tonic-gate }
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate #endif
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate #include <sys/bootsvcs.h>
5340Sstevel@tonic-gate 
5350Sstevel@tonic-gate /* Hacked up initialization for initial kernel check out is HERE. */
5360Sstevel@tonic-gate /* The basic steps are: */
5370Sstevel@tonic-gate /*	kernel bootfuncs definition/initialization for KADB */
5380Sstevel@tonic-gate /*	kadb bootfuncs pointer initialization */
5390Sstevel@tonic-gate /*	putchar/getchar (interrupts disabled) */
5400Sstevel@tonic-gate 
5410Sstevel@tonic-gate /* kadb bootfuncs pointer initialization */
5420Sstevel@tonic-gate 
5430Sstevel@tonic-gate int
5440Sstevel@tonic-gate sysp_getchar()
5450Sstevel@tonic-gate {
5460Sstevel@tonic-gate 	int i;
5473446Smrj 	ulong_t s;
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate 	if (cons_polledio == NULL) {
5500Sstevel@tonic-gate 		/* Uh oh */
5510Sstevel@tonic-gate 		prom_printf("getchar called with no console\n");
5520Sstevel@tonic-gate 		for (;;)
5530Sstevel@tonic-gate 			/* LOOP FOREVER */;
5540Sstevel@tonic-gate 	}
5550Sstevel@tonic-gate 
5560Sstevel@tonic-gate 	s = clear_int_flag();
5570Sstevel@tonic-gate 	i = cons_polledio->cons_polledio_getchar(
5585084Sjohnlev 	    cons_polledio->cons_polledio_argument);
5590Sstevel@tonic-gate 	restore_int_flag(s);
5600Sstevel@tonic-gate 	return (i);
5610Sstevel@tonic-gate }
5620Sstevel@tonic-gate 
5630Sstevel@tonic-gate void
5640Sstevel@tonic-gate sysp_putchar(int c)
5650Sstevel@tonic-gate {
5663446Smrj 	ulong_t s;
5670Sstevel@tonic-gate 
5680Sstevel@tonic-gate 	/*
5690Sstevel@tonic-gate 	 * We have no alternative but to drop the output on the floor.
5700Sstevel@tonic-gate 	 */
5711253Slq150181 	if (cons_polledio == NULL ||
5721253Slq150181 	    cons_polledio->cons_polledio_putchar == NULL)
5730Sstevel@tonic-gate 		return;
5740Sstevel@tonic-gate 
5750Sstevel@tonic-gate 	s = clear_int_flag();
5760Sstevel@tonic-gate 	cons_polledio->cons_polledio_putchar(
5775084Sjohnlev 	    cons_polledio->cons_polledio_argument, c);
5780Sstevel@tonic-gate 	restore_int_flag(s);
5790Sstevel@tonic-gate }
5800Sstevel@tonic-gate 
5810Sstevel@tonic-gate int
5820Sstevel@tonic-gate sysp_ischar()
5830Sstevel@tonic-gate {
5840Sstevel@tonic-gate 	int i;
5853446Smrj 	ulong_t s;
5860Sstevel@tonic-gate 
5871253Slq150181 	if (cons_polledio == NULL ||
5881253Slq150181 	    cons_polledio->cons_polledio_ischar == NULL)
5890Sstevel@tonic-gate 		return (0);
5900Sstevel@tonic-gate 
5910Sstevel@tonic-gate 	s = clear_int_flag();
5920Sstevel@tonic-gate 	i = cons_polledio->cons_polledio_ischar(
5935084Sjohnlev 	    cons_polledio->cons_polledio_argument);
5940Sstevel@tonic-gate 	restore_int_flag(s);
5950Sstevel@tonic-gate 	return (i);
5960Sstevel@tonic-gate }
5970Sstevel@tonic-gate 
5980Sstevel@tonic-gate int
5990Sstevel@tonic-gate goany(void)
6000Sstevel@tonic-gate {
6010Sstevel@tonic-gate 	prom_printf("Type any key to continue ");
6020Sstevel@tonic-gate 	(void) prom_getchar();
6030Sstevel@tonic-gate 	prom_printf("\n");
6040Sstevel@tonic-gate 	return (1);
6050Sstevel@tonic-gate }
6060Sstevel@tonic-gate 
6070Sstevel@tonic-gate static struct boot_syscalls kern_sysp = {
6080Sstevel@tonic-gate 	sysp_getchar,	/*	unchar	(*getchar)();	7  */
6090Sstevel@tonic-gate 	sysp_putchar,	/*	int	(*putchar)();	8  */
6100Sstevel@tonic-gate 	sysp_ischar,	/*	int	(*ischar)();	9  */
6110Sstevel@tonic-gate };
6120Sstevel@tonic-gate 
6135084Sjohnlev #if defined(__xpv)
6145084Sjohnlev int using_kern_polledio;
6155084Sjohnlev #endif
6165084Sjohnlev 
6170Sstevel@tonic-gate void
6180Sstevel@tonic-gate kadb_uses_kernel()
6190Sstevel@tonic-gate {
6200Sstevel@tonic-gate 	/*
6210Sstevel@tonic-gate 	 * This routine is now totally misnamed, since it does not in fact
6220Sstevel@tonic-gate 	 * control kadb's I/O; it only controls the kernel's prom_* I/O.
6230Sstevel@tonic-gate 	 */
6240Sstevel@tonic-gate 	sysp = &kern_sysp;
6255084Sjohnlev #if defined(__xpv)
6265084Sjohnlev 	using_kern_polledio = 1;
6275084Sjohnlev #endif
6280Sstevel@tonic-gate }
6290Sstevel@tonic-gate 
6300Sstevel@tonic-gate /*
6310Sstevel@tonic-gate  *	the interface to the outside world
6320Sstevel@tonic-gate  */
6330Sstevel@tonic-gate 
6340Sstevel@tonic-gate /*
6350Sstevel@tonic-gate  * poll_port -- wait for a register to achieve a
6360Sstevel@tonic-gate  *		specific state.  Arguments are a mask of bits we care about,
6370Sstevel@tonic-gate  *		and two sub-masks.  To return normally, all the bits in the
6380Sstevel@tonic-gate  *		first sub-mask must be ON, all the bits in the second sub-
6390Sstevel@tonic-gate  *		mask must be OFF.  If about seconds pass without the register
6400Sstevel@tonic-gate  *		achieving the desired bit configuration, we return 1, else
6410Sstevel@tonic-gate  *		0.
6420Sstevel@tonic-gate  */
6430Sstevel@tonic-gate int
6440Sstevel@tonic-gate poll_port(ushort_t port, ushort_t mask, ushort_t onbits, ushort_t offbits)
6450Sstevel@tonic-gate {
6460Sstevel@tonic-gate 	int i;
6470Sstevel@tonic-gate 	ushort_t maskval;
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate 	for (i = 500000; i; i--) {
6500Sstevel@tonic-gate 		maskval = inb(port) & mask;
6510Sstevel@tonic-gate 		if (((maskval & onbits) == onbits) &&
6525084Sjohnlev 		    ((maskval & offbits) == 0))
6530Sstevel@tonic-gate 			return (0);
6540Sstevel@tonic-gate 		drv_usecwait(10);
6550Sstevel@tonic-gate 	}
6560Sstevel@tonic-gate 	return (1);
6570Sstevel@tonic-gate }
6580Sstevel@tonic-gate 
6590Sstevel@tonic-gate /*
6600Sstevel@tonic-gate  * set_idle_cpu is called from idle() when a CPU becomes idle.
6610Sstevel@tonic-gate  */
6620Sstevel@tonic-gate /*LINTED: static unused */
6630Sstevel@tonic-gate static uint_t last_idle_cpu;
6640Sstevel@tonic-gate 
6650Sstevel@tonic-gate /*ARGSUSED*/
6660Sstevel@tonic-gate void
6670Sstevel@tonic-gate set_idle_cpu(int cpun)
6680Sstevel@tonic-gate {
6690Sstevel@tonic-gate 	last_idle_cpu = cpun;
6700Sstevel@tonic-gate 	(*psm_set_idle_cpuf)(cpun);
6710Sstevel@tonic-gate }
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate /*
6740Sstevel@tonic-gate  * unset_idle_cpu is called from idle() when a CPU is no longer idle.
6750Sstevel@tonic-gate  */
6760Sstevel@tonic-gate /*ARGSUSED*/
6770Sstevel@tonic-gate void
6780Sstevel@tonic-gate unset_idle_cpu(int cpun)
6790Sstevel@tonic-gate {
6800Sstevel@tonic-gate 	(*psm_unset_idle_cpuf)(cpun);
6810Sstevel@tonic-gate }
6820Sstevel@tonic-gate 
6830Sstevel@tonic-gate /*
6840Sstevel@tonic-gate  * This routine is almost correct now, but not quite.  It still needs the
6850Sstevel@tonic-gate  * equivalent concept of "hres_last_tick", just like on the sparc side.
6860Sstevel@tonic-gate  * The idea is to take a snapshot of the hi-res timer while doing the
6870Sstevel@tonic-gate  * hrestime_adj updates under hres_lock in locore, so that the small
6880Sstevel@tonic-gate  * interval between interrupt assertion and interrupt processing is
6890Sstevel@tonic-gate  * accounted for correctly.  Once we have this, the code below should
6900Sstevel@tonic-gate  * be modified to subtract off hres_last_tick rather than hrtime_base.
6910Sstevel@tonic-gate  *
6920Sstevel@tonic-gate  * I'd have done this myself, but I don't have source to all of the
6930Sstevel@tonic-gate  * vendor-specific hi-res timer routines (grrr...).  The generic hook I
6940Sstevel@tonic-gate  * need is something like "gethrtime_unlocked()", which would be just like
6950Sstevel@tonic-gate  * gethrtime() but would assume that you're already holding CLOCK_LOCK().
6960Sstevel@tonic-gate  * This is what the GET_HRTIME() macro is for on sparc (although it also
6970Sstevel@tonic-gate  * serves the function of making time available without a function call
6980Sstevel@tonic-gate  * so you don't take a register window overflow while traps are disabled).
6990Sstevel@tonic-gate  */
7000Sstevel@tonic-gate void
7010Sstevel@tonic-gate pc_gethrestime(timestruc_t *tp)
7020Sstevel@tonic-gate {
7030Sstevel@tonic-gate 	int lock_prev;
7040Sstevel@tonic-gate 	timestruc_t now;
7050Sstevel@tonic-gate 	int nslt;		/* nsec since last tick */
7060Sstevel@tonic-gate 	int adj;		/* amount of adjustment to apply */
7070Sstevel@tonic-gate 
7080Sstevel@tonic-gate loop:
7090Sstevel@tonic-gate 	lock_prev = hres_lock;
7100Sstevel@tonic-gate 	now = hrestime;
7110Sstevel@tonic-gate 	nslt = (int)(gethrtime() - hres_last_tick);
7120Sstevel@tonic-gate 	if (nslt < 0) {
7130Sstevel@tonic-gate 		/*
7140Sstevel@tonic-gate 		 * nslt < 0 means a tick came between sampling
7150Sstevel@tonic-gate 		 * gethrtime() and hres_last_tick; restart the loop
7160Sstevel@tonic-gate 		 */
7170Sstevel@tonic-gate 
7180Sstevel@tonic-gate 		goto loop;
7190Sstevel@tonic-gate 	}
7200Sstevel@tonic-gate 	now.tv_nsec += nslt;
7210Sstevel@tonic-gate 	if (hrestime_adj != 0) {
7220Sstevel@tonic-gate 		if (hrestime_adj > 0) {
7230Sstevel@tonic-gate 			adj = (nslt >> ADJ_SHIFT);
7240Sstevel@tonic-gate 			if (adj > hrestime_adj)
7250Sstevel@tonic-gate 				adj = (int)hrestime_adj;
7260Sstevel@tonic-gate 		} else {
7270Sstevel@tonic-gate 			adj = -(nslt >> ADJ_SHIFT);
7280Sstevel@tonic-gate 			if (adj < hrestime_adj)
7290Sstevel@tonic-gate 				adj = (int)hrestime_adj;
7300Sstevel@tonic-gate 		}
7310Sstevel@tonic-gate 		now.tv_nsec += adj;
7320Sstevel@tonic-gate 	}
7330Sstevel@tonic-gate 	while ((unsigned long)now.tv_nsec >= NANOSEC) {
7340Sstevel@tonic-gate 
7350Sstevel@tonic-gate 		/*
7360Sstevel@tonic-gate 		 * We might have a large adjustment or have been in the
7370Sstevel@tonic-gate 		 * debugger for a long time; take care of (at most) four
7380Sstevel@tonic-gate 		 * of those missed seconds (tv_nsec is 32 bits, so
7390Sstevel@tonic-gate 		 * anything >4s will be wrapping around).  However,
7400Sstevel@tonic-gate 		 * anything more than 2 seconds out of sync will trigger
7410Sstevel@tonic-gate 		 * timedelta from clock() to go correct the time anyway,
7420Sstevel@tonic-gate 		 * so do what we can, and let the big crowbar do the
7430Sstevel@tonic-gate 		 * rest.  A similar correction while loop exists inside
7440Sstevel@tonic-gate 		 * hres_tick(); in all cases we'd like tv_nsec to
7450Sstevel@tonic-gate 		 * satisfy 0 <= tv_nsec < NANOSEC to avoid confusing
7460Sstevel@tonic-gate 		 * user processes, but if tv_sec's a little behind for a
7470Sstevel@tonic-gate 		 * little while, that's OK; time still monotonically
7480Sstevel@tonic-gate 		 * increases.
7490Sstevel@tonic-gate 		 */
7500Sstevel@tonic-gate 
7510Sstevel@tonic-gate 		now.tv_nsec -= NANOSEC;
7520Sstevel@tonic-gate 		now.tv_sec++;
7530Sstevel@tonic-gate 	}
7540Sstevel@tonic-gate 	if ((hres_lock & ~1) != lock_prev)
7550Sstevel@tonic-gate 		goto loop;
7560Sstevel@tonic-gate 
7570Sstevel@tonic-gate 	*tp = now;
7580Sstevel@tonic-gate }
7590Sstevel@tonic-gate 
7600Sstevel@tonic-gate void
7610Sstevel@tonic-gate gethrestime_lasttick(timespec_t *tp)
7620Sstevel@tonic-gate {
7630Sstevel@tonic-gate 	int s;
7640Sstevel@tonic-gate 
7650Sstevel@tonic-gate 	s = hr_clock_lock();
7660Sstevel@tonic-gate 	*tp = hrestime;
7670Sstevel@tonic-gate 	hr_clock_unlock(s);
7680Sstevel@tonic-gate }
7690Sstevel@tonic-gate 
7700Sstevel@tonic-gate time_t
7710Sstevel@tonic-gate gethrestime_sec(void)
7720Sstevel@tonic-gate {
7730Sstevel@tonic-gate 	timestruc_t now;
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate 	gethrestime(&now);
7760Sstevel@tonic-gate 	return (now.tv_sec);
7770Sstevel@tonic-gate }
7780Sstevel@tonic-gate 
7790Sstevel@tonic-gate /*
7800Sstevel@tonic-gate  * Initialize a kernel thread's stack
7810Sstevel@tonic-gate  */
7820Sstevel@tonic-gate 
7830Sstevel@tonic-gate caddr_t
7840Sstevel@tonic-gate thread_stk_init(caddr_t stk)
7850Sstevel@tonic-gate {
7860Sstevel@tonic-gate 	ASSERT(((uintptr_t)stk & (STACK_ALIGN - 1)) == 0);
7870Sstevel@tonic-gate 	return (stk - SA(MINFRAME));
7880Sstevel@tonic-gate }
7890Sstevel@tonic-gate 
7900Sstevel@tonic-gate /*
7910Sstevel@tonic-gate  * Initialize lwp's kernel stack.
7920Sstevel@tonic-gate  */
7930Sstevel@tonic-gate 
7940Sstevel@tonic-gate #ifdef TRAPTRACE
7950Sstevel@tonic-gate /*
7960Sstevel@tonic-gate  * There's a tricky interdependency here between use of sysenter and
7970Sstevel@tonic-gate  * TRAPTRACE which needs recording to avoid future confusion (this is
7980Sstevel@tonic-gate  * about the third time I've re-figured this out ..)
7990Sstevel@tonic-gate  *
8000Sstevel@tonic-gate  * Here's how debugging lcall works with TRAPTRACE.
8010Sstevel@tonic-gate  *
8020Sstevel@tonic-gate  * 1 We're in userland with a breakpoint on the lcall instruction.
8030Sstevel@tonic-gate  * 2 We execute the instruction - the instruction pushes the userland
8040Sstevel@tonic-gate  *   %ss, %esp, %efl, %cs, %eip on the stack and zips into the kernel
8050Sstevel@tonic-gate  *   via the call gate.
8060Sstevel@tonic-gate  * 3 The hardware raises a debug trap in kernel mode, the hardware
8070Sstevel@tonic-gate  *   pushes %efl, %cs, %eip and gets to dbgtrap via the idt.
8080Sstevel@tonic-gate  * 4 dbgtrap pushes the error code and trapno and calls cmntrap
8090Sstevel@tonic-gate  * 5 cmntrap finishes building a trap frame
8100Sstevel@tonic-gate  * 6 The TRACE_REGS macros in cmntrap copy a REGSIZE worth chunk
8110Sstevel@tonic-gate  *   off the stack into the traptrace buffer.
8120Sstevel@tonic-gate  *
8130Sstevel@tonic-gate  * This means that the traptrace buffer contains the wrong values in
8140Sstevel@tonic-gate  * %esp and %ss, but everything else in there is correct.
8150Sstevel@tonic-gate  *
8160Sstevel@tonic-gate  * Here's how debugging sysenter works with TRAPTRACE.
8170Sstevel@tonic-gate  *
8180Sstevel@tonic-gate  * a We're in userland with a breakpoint on the sysenter instruction.
8190Sstevel@tonic-gate  * b We execute the instruction - the instruction pushes -nothing-
8200Sstevel@tonic-gate  *   on the stack, but sets %cs, %eip, %ss, %esp to prearranged
8210Sstevel@tonic-gate  *   values to take us to sys_sysenter, at the top of the lwp's
8220Sstevel@tonic-gate  *   stack.
8230Sstevel@tonic-gate  * c goto 3
8240Sstevel@tonic-gate  *
8250Sstevel@tonic-gate  * At this point, because we got into the kernel without the requisite
8260Sstevel@tonic-gate  * five pushes on the stack, if we didn't make extra room, we'd
8270Sstevel@tonic-gate  * end up with the TRACE_REGS macro fetching the saved %ss and %esp
8280Sstevel@tonic-gate  * values from negative (unmapped) stack addresses -- which really bites.
8290Sstevel@tonic-gate  * That's why we do the '-= 8' below.
8300Sstevel@tonic-gate  *
8310Sstevel@tonic-gate  * XXX	Note that reading "up" lwp0's stack works because t0 is declared
8320Sstevel@tonic-gate  *	right next to t0stack in locore.s
8330Sstevel@tonic-gate  */
8340Sstevel@tonic-gate #endif
8350Sstevel@tonic-gate 
8360Sstevel@tonic-gate caddr_t
8370Sstevel@tonic-gate lwp_stk_init(klwp_t *lwp, caddr_t stk)
8380Sstevel@tonic-gate {
8390Sstevel@tonic-gate 	caddr_t oldstk;
8400Sstevel@tonic-gate 	struct pcb *pcb = &lwp->lwp_pcb;
8410Sstevel@tonic-gate 
8420Sstevel@tonic-gate 	oldstk = stk;
8430Sstevel@tonic-gate 	stk -= SA(sizeof (struct regs) + SA(MINFRAME));
8440Sstevel@tonic-gate #ifdef TRAPTRACE
8450Sstevel@tonic-gate 	stk -= 2 * sizeof (greg_t); /* space for phony %ss:%sp (see above) */
8460Sstevel@tonic-gate #endif
8470Sstevel@tonic-gate 	stk = (caddr_t)((uintptr_t)stk & ~(STACK_ALIGN - 1ul));
8480Sstevel@tonic-gate 	bzero(stk, oldstk - stk);
8490Sstevel@tonic-gate 	lwp->lwp_regs = (void *)(stk + SA(MINFRAME));
8500Sstevel@tonic-gate 
8510Sstevel@tonic-gate 	/*
8520Sstevel@tonic-gate 	 * Arrange that the virtualized %fs and %gs GDT descriptors
8530Sstevel@tonic-gate 	 * have a well-defined initial state (present, ring 3
8540Sstevel@tonic-gate 	 * and of type data).
8550Sstevel@tonic-gate 	 */
8560Sstevel@tonic-gate #if defined(__amd64)
8570Sstevel@tonic-gate 	if (lwp_getdatamodel(lwp) == DATAMODEL_NATIVE)
8580Sstevel@tonic-gate 		pcb->pcb_fsdesc = pcb->pcb_gsdesc = zero_udesc;
8590Sstevel@tonic-gate 	else
8600Sstevel@tonic-gate 		pcb->pcb_fsdesc = pcb->pcb_gsdesc = zero_u32desc;
8610Sstevel@tonic-gate #elif defined(__i386)
8620Sstevel@tonic-gate 	pcb->pcb_fsdesc = pcb->pcb_gsdesc = zero_udesc;
8630Sstevel@tonic-gate #endif	/* __i386 */
8640Sstevel@tonic-gate 	lwp_installctx(lwp);
8650Sstevel@tonic-gate 	return (stk);
8660Sstevel@tonic-gate }
8670Sstevel@tonic-gate 
8680Sstevel@tonic-gate /*ARGSUSED*/
8690Sstevel@tonic-gate void
8700Sstevel@tonic-gate lwp_stk_fini(klwp_t *lwp)
8710Sstevel@tonic-gate {}
8720Sstevel@tonic-gate 
8730Sstevel@tonic-gate /*
8741389Sdmick  * If we're not the panic CPU, we wait in panic_idle for reboot.
8750Sstevel@tonic-gate  */
8769489SJoe.Bonasera@sun.com void
8770Sstevel@tonic-gate panic_idle(void)
8780Sstevel@tonic-gate {
8790Sstevel@tonic-gate 	splx(ipltospl(CLOCK_LEVEL));
8800Sstevel@tonic-gate 	(void) setjmp(&curthread->t_pcb);
8810Sstevel@tonic-gate 
88210843SDave.Plauger@Sun.COM 	dumpsys_helper();
88310843SDave.Plauger@Sun.COM 
8849489SJoe.Bonasera@sun.com #ifndef __xpv
8859489SJoe.Bonasera@sun.com 	for (;;)
8869489SJoe.Bonasera@sun.com 		i86_halt();
8879489SJoe.Bonasera@sun.com #else
8883446Smrj 	for (;;)
8893446Smrj 		;
8909489SJoe.Bonasera@sun.com #endif
8910Sstevel@tonic-gate }
8920Sstevel@tonic-gate 
8930Sstevel@tonic-gate /*
8940Sstevel@tonic-gate  * Stop the other CPUs by cross-calling them and forcing them to enter
8950Sstevel@tonic-gate  * the panic_idle() loop above.
8960Sstevel@tonic-gate  */
8970Sstevel@tonic-gate /*ARGSUSED*/
8980Sstevel@tonic-gate void
8990Sstevel@tonic-gate panic_stopcpus(cpu_t *cp, kthread_t *t, int spl)
9000Sstevel@tonic-gate {
9010Sstevel@tonic-gate 	processorid_t i;
9020Sstevel@tonic-gate 	cpuset_t xcset;
9030Sstevel@tonic-gate 
9045084Sjohnlev 	/*
9055084Sjohnlev 	 * In the case of a Xen panic, the hypervisor has already stopped
9065084Sjohnlev 	 * all of the CPUs.
9075084Sjohnlev 	 */
9085084Sjohnlev 	if (!IN_XPV_PANIC()) {
9095084Sjohnlev 		(void) splzs();
9100Sstevel@tonic-gate 
9115084Sjohnlev 		CPUSET_ALL_BUT(xcset, cp->cpu_id);
9129489SJoe.Bonasera@sun.com 		xc_priority(0, 0, 0, CPUSET2BV(xcset), (xc_func_t)panic_idle);
9135084Sjohnlev 	}
9140Sstevel@tonic-gate 
9150Sstevel@tonic-gate 	for (i = 0; i < NCPU; i++) {
9160Sstevel@tonic-gate 		if (i != cp->cpu_id && cpu[i] != NULL &&
9170Sstevel@tonic-gate 		    (cpu[i]->cpu_flags & CPU_EXISTS))
9180Sstevel@tonic-gate 			cpu[i]->cpu_flags |= CPU_QUIESCED;
9190Sstevel@tonic-gate 	}
9200Sstevel@tonic-gate }
9210Sstevel@tonic-gate 
9220Sstevel@tonic-gate /*
9230Sstevel@tonic-gate  * Platform callback following each entry to panicsys().
9240Sstevel@tonic-gate  */
9250Sstevel@tonic-gate /*ARGSUSED*/
9260Sstevel@tonic-gate void
9270Sstevel@tonic-gate panic_enter_hw(int spl)
9280Sstevel@tonic-gate {
9290Sstevel@tonic-gate 	/* Nothing to do here */
9300Sstevel@tonic-gate }
9310Sstevel@tonic-gate 
9320Sstevel@tonic-gate /*
9330Sstevel@tonic-gate  * Platform-specific code to execute after panicstr is set: we invoke
9340Sstevel@tonic-gate  * the PSM entry point to indicate that a panic has occurred.
9350Sstevel@tonic-gate  */
9360Sstevel@tonic-gate /*ARGSUSED*/
9370Sstevel@tonic-gate void
9380Sstevel@tonic-gate panic_quiesce_hw(panic_data_t *pdp)
9390Sstevel@tonic-gate {
9400Sstevel@tonic-gate 	psm_notifyf(PSM_PANIC_ENTER);
9410Sstevel@tonic-gate 
9427532SSean.Ye@Sun.COM 	cmi_panic_callback();
9437532SSean.Ye@Sun.COM 
9440Sstevel@tonic-gate #ifdef	TRAPTRACE
9450Sstevel@tonic-gate 	/*
9460Sstevel@tonic-gate 	 * Turn off TRAPTRACE
9470Sstevel@tonic-gate 	 */
9480Sstevel@tonic-gate 	TRAPTRACE_FREEZE;
9490Sstevel@tonic-gate #endif	/* TRAPTRACE */
9500Sstevel@tonic-gate }
9510Sstevel@tonic-gate 
9520Sstevel@tonic-gate /*
9530Sstevel@tonic-gate  * Platform callback prior to writing crash dump.
9540Sstevel@tonic-gate  */
9550Sstevel@tonic-gate /*ARGSUSED*/
9560Sstevel@tonic-gate void
9570Sstevel@tonic-gate panic_dump_hw(int spl)
9580Sstevel@tonic-gate {
9590Sstevel@tonic-gate 	/* Nothing to do here */
9600Sstevel@tonic-gate }
9610Sstevel@tonic-gate 
9625084Sjohnlev void *
9635084Sjohnlev plat_traceback(void *fpreg)
9645084Sjohnlev {
9655084Sjohnlev #ifdef __xpv
9665084Sjohnlev 	if (IN_XPV_PANIC())
9675084Sjohnlev 		return (xpv_traceback(fpreg));
9685084Sjohnlev #endif
9695084Sjohnlev 	return (fpreg);
9705084Sjohnlev }
9715084Sjohnlev 
9720Sstevel@tonic-gate /*ARGSUSED*/
9730Sstevel@tonic-gate void
9740Sstevel@tonic-gate plat_tod_fault(enum tod_fault_type tod_bad)
9753446Smrj {}
9760Sstevel@tonic-gate 
9770Sstevel@tonic-gate /*ARGSUSED*/
9780Sstevel@tonic-gate int
9790Sstevel@tonic-gate blacklist(int cmd, const char *scheme, nvlist_t *fmri, const char *class)
9800Sstevel@tonic-gate {
9810Sstevel@tonic-gate 	return (ENOTSUP);
9820Sstevel@tonic-gate }
9830Sstevel@tonic-gate 
9840Sstevel@tonic-gate /*
9850Sstevel@tonic-gate  * The underlying console output routines are protected by raising IPL in case
9860Sstevel@tonic-gate  * we are still calling into the early boot services.  Once we start calling
9870Sstevel@tonic-gate  * the kernel console emulator, it will disable interrupts completely during
9880Sstevel@tonic-gate  * character rendering (see sysp_putchar, for example).  Refer to the comments
9890Sstevel@tonic-gate  * and code in common/os/console.c for more information on these callbacks.
9900Sstevel@tonic-gate  */
9910Sstevel@tonic-gate /*ARGSUSED*/
9920Sstevel@tonic-gate int
9930Sstevel@tonic-gate console_enter(int busy)
9940Sstevel@tonic-gate {
9950Sstevel@tonic-gate 	return (splzs());
9960Sstevel@tonic-gate }
9970Sstevel@tonic-gate 
9980Sstevel@tonic-gate /*ARGSUSED*/
9990Sstevel@tonic-gate void
10000Sstevel@tonic-gate console_exit(int busy, int spl)
10010Sstevel@tonic-gate {
10020Sstevel@tonic-gate 	splx(spl);
10030Sstevel@tonic-gate }
10040Sstevel@tonic-gate 
10050Sstevel@tonic-gate /*
10060Sstevel@tonic-gate  * Allocate a region of virtual address space, unmapped.
10070Sstevel@tonic-gate  * Stubbed out except on sparc, at least for now.
10080Sstevel@tonic-gate  */
10090Sstevel@tonic-gate /*ARGSUSED*/
10100Sstevel@tonic-gate void *
10110Sstevel@tonic-gate boot_virt_alloc(void *addr, size_t size)
10120Sstevel@tonic-gate {
10130Sstevel@tonic-gate 	return (addr);
10140Sstevel@tonic-gate }
10150Sstevel@tonic-gate 
10160Sstevel@tonic-gate volatile unsigned long	tenmicrodata;
10170Sstevel@tonic-gate 
10180Sstevel@tonic-gate void
10190Sstevel@tonic-gate tenmicrosec(void)
10200Sstevel@tonic-gate {
10215084Sjohnlev 	extern int gethrtime_hires;
10220Sstevel@tonic-gate 
10235084Sjohnlev 	if (gethrtime_hires) {
10240Sstevel@tonic-gate 		hrtime_t start, end;
10250Sstevel@tonic-gate 		start = end =  gethrtime();
10260Sstevel@tonic-gate 		while ((end - start) < (10 * (NANOSEC / MICROSEC))) {
10270Sstevel@tonic-gate 			SMT_PAUSE();
10280Sstevel@tonic-gate 			end = gethrtime();
10290Sstevel@tonic-gate 		}
10300Sstevel@tonic-gate 	} else {
10315084Sjohnlev #if defined(__xpv)
10325084Sjohnlev 		hrtime_t newtime;
10335084Sjohnlev 
10345084Sjohnlev 		newtime = xpv_gethrtime() + 10000; /* now + 10 us */
10355084Sjohnlev 		while (xpv_gethrtime() < newtime)
10365084Sjohnlev 			SMT_PAUSE();
10375084Sjohnlev #else	/* __xpv */
10383446Smrj 		int i;
10393446Smrj 
10400Sstevel@tonic-gate 		/*
10410Sstevel@tonic-gate 		 * Artificial loop to induce delay.
10420Sstevel@tonic-gate 		 */
10430Sstevel@tonic-gate 		for (i = 0; i < microdata; i++)
10440Sstevel@tonic-gate 			tenmicrodata = microdata;
10455084Sjohnlev #endif	/* __xpv */
10460Sstevel@tonic-gate 	}
10470Sstevel@tonic-gate }
1048590Sesolom 
1049590Sesolom /*
1050590Sesolom  * get_cpu_mstate() is passed an array of timestamps, NCMSTATES
1051590Sesolom  * long, and it fills in the array with the time spent on cpu in
1052590Sesolom  * each of the mstates, where time is returned in nsec.
1053590Sesolom  *
1054590Sesolom  * No guarantee is made that the returned values in times[] will
1055590Sesolom  * monotonically increase on sequential calls, although this will
1056590Sesolom  * be true in the long run. Any such guarantee must be handled by
1057590Sesolom  * the caller, if needed. This can happen if we fail to account
1058590Sesolom  * for elapsed time due to a generation counter conflict, yet we
1059590Sesolom  * did account for it on a prior call (see below).
1060590Sesolom  *
1061590Sesolom  * The complication is that the cpu in question may be updating
1062590Sesolom  * its microstate at the same time that we are reading it.
1063590Sesolom  * Because the microstate is only updated when the CPU's state
1064590Sesolom  * changes, the values in cpu_intracct[] can be indefinitely out
1065590Sesolom  * of date. To determine true current values, it is necessary to
1066590Sesolom  * compare the current time with cpu_mstate_start, and add the
1067590Sesolom  * difference to times[cpu_mstate].
1068590Sesolom  *
1069590Sesolom  * This can be a problem if those values are changing out from
1070590Sesolom  * under us. Because the code path in new_cpu_mstate() is
1071590Sesolom  * performance critical, we have not added a lock to it. Instead,
1072590Sesolom  * we have added a generation counter. Before beginning
1073590Sesolom  * modifications, the counter is set to 0. After modifications,
1074590Sesolom  * it is set to the old value plus one.
1075590Sesolom  *
1076590Sesolom  * get_cpu_mstate() will not consider the values of cpu_mstate
1077590Sesolom  * and cpu_mstate_start to be usable unless the value of
1078590Sesolom  * cpu_mstate_gen is both non-zero and unchanged, both before and
1079590Sesolom  * after reading the mstate information. Note that we must
1080590Sesolom  * protect against out-of-order loads around accesses to the
1081590Sesolom  * generation counter. Also, this is a best effort approach in
1082590Sesolom  * that we do not retry should the counter be found to have
1083590Sesolom  * changed.
1084590Sesolom  *
1085590Sesolom  * cpu_intracct[] is used to identify time spent in each CPU
1086590Sesolom  * mstate while handling interrupts. Such time should be reported
1087590Sesolom  * against system time, and so is subtracted out from its
1088590Sesolom  * corresponding cpu_acct[] time and added to
1089590Sesolom  * cpu_acct[CMS_SYSTEM].
1090590Sesolom  */
1091590Sesolom 
1092590Sesolom void
1093590Sesolom get_cpu_mstate(cpu_t *cpu, hrtime_t *times)
1094590Sesolom {
1095590Sesolom 	int i;
1096590Sesolom 	hrtime_t now, start;
1097590Sesolom 	uint16_t gen;
1098590Sesolom 	uint16_t state;
1099590Sesolom 	hrtime_t intracct[NCMSTATES];
1100590Sesolom 
1101590Sesolom 	/*
1102590Sesolom 	 * Load all volatile state under the protection of membar.
1103590Sesolom 	 * cpu_acct[cpu_mstate] must be loaded to avoid double counting
1104590Sesolom 	 * of (now - cpu_mstate_start) by a change in CPU mstate that
1105590Sesolom 	 * arrives after we make our last check of cpu_mstate_gen.
1106590Sesolom 	 */
1107590Sesolom 
1108590Sesolom 	now = gethrtime_unscaled();
1109590Sesolom 	gen = cpu->cpu_mstate_gen;
1110590Sesolom 
1111590Sesolom 	membar_consumer();	/* guarantee load ordering */
1112590Sesolom 	start = cpu->cpu_mstate_start;
1113590Sesolom 	state = cpu->cpu_mstate;
1114590Sesolom 	for (i = 0; i < NCMSTATES; i++) {
1115590Sesolom 		intracct[i] = cpu->cpu_intracct[i];
1116590Sesolom 		times[i] = cpu->cpu_acct[i];
1117590Sesolom 	}
1118590Sesolom 	membar_consumer();	/* guarantee load ordering */
1119590Sesolom 
1120590Sesolom 	if (gen != 0 && gen == cpu->cpu_mstate_gen && now > start)
1121590Sesolom 		times[state] += now - start;
1122590Sesolom 
1123590Sesolom 	for (i = 0; i < NCMSTATES; i++) {
1124590Sesolom 		if (i == CMS_SYSTEM)
1125590Sesolom 			continue;
1126590Sesolom 		times[i] -= intracct[i];
1127590Sesolom 		if (times[i] < 0) {
1128590Sesolom 			intracct[i] += times[i];
1129590Sesolom 			times[i] = 0;
1130590Sesolom 		}
1131590Sesolom 		times[CMS_SYSTEM] += intracct[i];
1132590Sesolom 		scalehrtime(&times[i]);
1133590Sesolom 	}
1134590Sesolom 	scalehrtime(&times[CMS_SYSTEM]);
1135590Sesolom }
11363446Smrj 
11373446Smrj /*
11383446Smrj  * This is a version of the rdmsr instruction that allows
11393446Smrj  * an error code to be returned in the case of failure.
11403446Smrj  */
11413446Smrj int
11423446Smrj checked_rdmsr(uint_t msr, uint64_t *value)
11433446Smrj {
11443446Smrj 	if ((x86_feature & X86_MSR) == 0)
11453446Smrj 		return (ENOTSUP);
11463446Smrj 	*value = rdmsr(msr);
11473446Smrj 	return (0);
11483446Smrj }
11493446Smrj 
11503446Smrj /*
11513446Smrj  * This is a version of the wrmsr instruction that allows
11523446Smrj  * an error code to be returned in the case of failure.
11533446Smrj  */
11543446Smrj int
11553446Smrj checked_wrmsr(uint_t msr, uint64_t value)
11563446Smrj {
11573446Smrj 	if ((x86_feature & X86_MSR) == 0)
11583446Smrj 		return (ENOTSUP);
11593446Smrj 	wrmsr(msr, value);
11603446Smrj 	return (0);
11613446Smrj }
11623446Smrj 
11633446Smrj /*
11645084Sjohnlev  * The mem driver's usual method of using hat_devload() to establish a
11655084Sjohnlev  * temporary mapping will not work for foreign pages mapped into this
11665084Sjohnlev  * domain or for the special hypervisor-provided pages.  For the foreign
11675084Sjohnlev  * pages, we often don't know which domain owns them, so we can't ask the
11685084Sjohnlev  * hypervisor to set up a new mapping.  For the other pages, we don't have
11695084Sjohnlev  * a pfn, so we can't create a new PTE.  For these special cases, we do a
11705084Sjohnlev  * direct uiomove() from the existing kernel virtual address.
11713446Smrj  */
11723446Smrj /*ARGSUSED*/
11733446Smrj int
11745084Sjohnlev plat_mem_do_mmio(struct uio *uio, enum uio_rw rw)
11755084Sjohnlev {
11765084Sjohnlev #if defined(__xpv)
11775084Sjohnlev 	void *va = (void *)(uintptr_t)uio->uio_loffset;
11785084Sjohnlev 	off_t pageoff = uio->uio_loffset & PAGEOFFSET;
11795084Sjohnlev 	size_t nbytes = MIN((size_t)(PAGESIZE - pageoff),
11805084Sjohnlev 	    (size_t)uio->uio_iov->iov_len);
11815084Sjohnlev 
11825084Sjohnlev 	if ((rw == UIO_READ &&
11835084Sjohnlev 	    (va == HYPERVISOR_shared_info || va == xen_info)) ||
11845084Sjohnlev 	    (pfn_is_foreign(hat_getpfnum(kas.a_hat, va))))
11855084Sjohnlev 		return (uiomove(va, nbytes, rw, uio));
11865084Sjohnlev #endif
11875084Sjohnlev 	return (ENOTSUP);
11885084Sjohnlev }
11895084Sjohnlev 
11905084Sjohnlev pgcnt_t
11915084Sjohnlev num_phys_pages()
11923446Smrj {
11935084Sjohnlev 	pgcnt_t npages = 0;
11945084Sjohnlev 	struct memlist *mp;
11955084Sjohnlev 
11965084Sjohnlev #if defined(__xpv)
119710175SStuart.Maybee@Sun.COM 	if (DOMAIN_IS_INITDOMAIN(xen_info))
119810175SStuart.Maybee@Sun.COM 		return (xpv_nr_phys_pages());
11995084Sjohnlev #endif /* __xpv */
12005084Sjohnlev 
120111474SJonathan.Adams@Sun.COM 	for (mp = phys_install; mp != NULL; mp = mp->ml_next)
120211474SJonathan.Adams@Sun.COM 		npages += mp->ml_size >> PAGESHIFT;
12035084Sjohnlev 
12045084Sjohnlev 	return (npages);
12053446Smrj }
12063446Smrj 
120710843SDave.Plauger@Sun.COM /* cpu threshold for compressed dumps */
120810843SDave.Plauger@Sun.COM #ifdef _LP64
120910843SDave.Plauger@Sun.COM uint_t dump_plat_mincpu = DUMP_PLAT_X86_64_MINCPU;
121010843SDave.Plauger@Sun.COM #else
121110843SDave.Plauger@Sun.COM uint_t dump_plat_mincpu = DUMP_PLAT_X86_32_MINCPU;
121210843SDave.Plauger@Sun.COM #endif
121310843SDave.Plauger@Sun.COM 
12143446Smrj int
12153446Smrj dump_plat_addr()
12163446Smrj {
12175084Sjohnlev #ifdef __xpv
12185084Sjohnlev 	pfn_t pfn = mmu_btop(xen_info->shared_info) | PFN_IS_FOREIGN_MFN;
12195084Sjohnlev 	mem_vtop_t mem_vtop;
12205084Sjohnlev 	int cnt;
12215084Sjohnlev 
12225084Sjohnlev 	/*
12235084Sjohnlev 	 * On the hypervisor, we want to dump the page with shared_info on it.
12245084Sjohnlev 	 */
12255084Sjohnlev 	if (!IN_XPV_PANIC()) {
12265084Sjohnlev 		mem_vtop.m_as = &kas;
12275084Sjohnlev 		mem_vtop.m_va = HYPERVISOR_shared_info;
12285084Sjohnlev 		mem_vtop.m_pfn = pfn;
12295084Sjohnlev 		dumpvp_write(&mem_vtop, sizeof (mem_vtop_t));
12305084Sjohnlev 		cnt = 1;
12315084Sjohnlev 	} else {
12325084Sjohnlev 		cnt = dump_xpv_addr();
12335084Sjohnlev 	}
12345084Sjohnlev 	return (cnt);
12355084Sjohnlev #else
12363446Smrj 	return (0);
12375084Sjohnlev #endif
12383446Smrj }
12393446Smrj 
12403446Smrj void
12413446Smrj dump_plat_pfn()
12423446Smrj {
12435084Sjohnlev #ifdef __xpv
12445084Sjohnlev 	pfn_t pfn = mmu_btop(xen_info->shared_info) | PFN_IS_FOREIGN_MFN;
12455084Sjohnlev 
12465084Sjohnlev 	if (!IN_XPV_PANIC())
12475084Sjohnlev 		dumpvp_write(&pfn, sizeof (pfn));
12485084Sjohnlev 	else
12495084Sjohnlev 		dump_xpv_pfn();
12505084Sjohnlev #endif
12513446Smrj }
12523446Smrj 
12533446Smrj /*ARGSUSED*/
12543446Smrj int
12553446Smrj dump_plat_data(void *dump_cbuf)
12563446Smrj {
12575084Sjohnlev #ifdef __xpv
12585084Sjohnlev 	uint32_t csize;
12595084Sjohnlev 	int cnt;
12605084Sjohnlev 
12615084Sjohnlev 	if (!IN_XPV_PANIC()) {
12625084Sjohnlev 		csize = (uint32_t)compress(HYPERVISOR_shared_info, dump_cbuf,
12635084Sjohnlev 		    PAGESIZE);
12645084Sjohnlev 		dumpvp_write(&csize, sizeof (uint32_t));
12655084Sjohnlev 		dumpvp_write(dump_cbuf, csize);
12665084Sjohnlev 		cnt = 1;
12675084Sjohnlev 	} else {
12685084Sjohnlev 		cnt = dump_xpv_data(dump_cbuf);
12695084Sjohnlev 	}
12705084Sjohnlev 	return (cnt);
12715084Sjohnlev #else
12723446Smrj 	return (0);
12735084Sjohnlev #endif
12743446Smrj }
12753939Ssethg 
12763939Ssethg /*
12773939Ssethg  * Calculates a linear address, given the CS selector and PC values
12783939Ssethg  * by looking up the %cs selector process's LDT or the CPU's GDT.
12793939Ssethg  * proc->p_ldtlock must be held across this call.
12803939Ssethg  */
12813939Ssethg int
12823939Ssethg linear_pc(struct regs *rp, proc_t *p, caddr_t *linearp)
12833939Ssethg {
12843939Ssethg 	user_desc_t	*descrp;
12853939Ssethg 	caddr_t		baseaddr;
12863939Ssethg 	uint16_t	idx = SELTOIDX(rp->r_cs);
12873939Ssethg 
12883939Ssethg 	ASSERT(rp->r_cs <= 0xFFFF);
12893939Ssethg 	ASSERT(MUTEX_HELD(&p->p_ldtlock));
12903939Ssethg 
12913939Ssethg 	if (SELISLDT(rp->r_cs)) {
12923939Ssethg 		/*
12933939Ssethg 		 * Currently 64 bit processes cannot have private LDTs.
12943939Ssethg 		 */
12953939Ssethg 		ASSERT(p->p_model != DATAMODEL_LP64);
12963939Ssethg 
12973939Ssethg 		if (p->p_ldt == NULL)
12983939Ssethg 			return (-1);
12993939Ssethg 
13003939Ssethg 		descrp = &p->p_ldt[idx];
13013939Ssethg 		baseaddr = (caddr_t)(uintptr_t)USEGD_GETBASE(descrp);
13023939Ssethg 
13033939Ssethg 		/*
13043939Ssethg 		 * Calculate the linear address (wraparound is not only ok,
13053939Ssethg 		 * it's expected behavior).  The cast to uint32_t is because
13063939Ssethg 		 * LDT selectors are only allowed in 32-bit processes.
13073939Ssethg 		 */
13083939Ssethg 		*linearp = (caddr_t)(uintptr_t)(uint32_t)((uintptr_t)baseaddr +
13093939Ssethg 		    rp->r_pc);
13103939Ssethg 	} else {
13113939Ssethg #ifdef DEBUG
13123939Ssethg 		descrp = &CPU->cpu_gdt[idx];
13133939Ssethg 		baseaddr = (caddr_t)(uintptr_t)USEGD_GETBASE(descrp);
13143939Ssethg 		/* GDT-based descriptors' base addresses should always be 0 */
13153939Ssethg 		ASSERT(baseaddr == 0);
13163939Ssethg #endif
13173939Ssethg 		*linearp = (caddr_t)(uintptr_t)rp->r_pc;
13183939Ssethg 	}
13193939Ssethg 
13203939Ssethg 	return (0);
13213939Ssethg }
13223939Ssethg 
13233939Ssethg /*
13243939Ssethg  * The implementation of dtrace_linear_pc is similar to the that of
13253939Ssethg  * linear_pc, above, but here we acquire p_ldtlock before accessing
13263939Ssethg  * p_ldt.  This implementation is used by the pid provider; we prefix
13273939Ssethg  * it with "dtrace_" to avoid inducing spurious tracing events.
13283939Ssethg  */
13293939Ssethg int
13303939Ssethg dtrace_linear_pc(struct regs *rp, proc_t *p, caddr_t *linearp)
13313939Ssethg {
13323939Ssethg 	user_desc_t	*descrp;
13333939Ssethg 	caddr_t		baseaddr;
13343939Ssethg 	uint16_t	idx = SELTOIDX(rp->r_cs);
13353939Ssethg 
13363939Ssethg 	ASSERT(rp->r_cs <= 0xFFFF);
13373939Ssethg 
13383939Ssethg 	if (SELISLDT(rp->r_cs)) {
13393939Ssethg 		/*
13403939Ssethg 		 * Currently 64 bit processes cannot have private LDTs.
13413939Ssethg 		 */
13423939Ssethg 		ASSERT(p->p_model != DATAMODEL_LP64);
13433939Ssethg 
13443939Ssethg 		mutex_enter(&p->p_ldtlock);
13453939Ssethg 		if (p->p_ldt == NULL) {
13463939Ssethg 			mutex_exit(&p->p_ldtlock);
13473939Ssethg 			return (-1);
13483939Ssethg 		}
13493939Ssethg 		descrp = &p->p_ldt[idx];
13503939Ssethg 		baseaddr = (caddr_t)(uintptr_t)USEGD_GETBASE(descrp);
13513939Ssethg 		mutex_exit(&p->p_ldtlock);
13523939Ssethg 
13533939Ssethg 		/*
13543939Ssethg 		 * Calculate the linear address (wraparound is not only ok,
13553939Ssethg 		 * it's expected behavior).  The cast to uint32_t is because
13563939Ssethg 		 * LDT selectors are only allowed in 32-bit processes.
13573939Ssethg 		 */
13583939Ssethg 		*linearp = (caddr_t)(uintptr_t)(uint32_t)((uintptr_t)baseaddr +
13593939Ssethg 		    rp->r_pc);
13603939Ssethg 	} else {
13613939Ssethg #ifdef DEBUG
13623939Ssethg 		descrp = &CPU->cpu_gdt[idx];
13633939Ssethg 		baseaddr = (caddr_t)(uintptr_t)USEGD_GETBASE(descrp);
13643939Ssethg 		/* GDT-based descriptors' base addresses should always be 0 */
13653939Ssethg 		ASSERT(baseaddr == 0);
13663939Ssethg #endif
13673939Ssethg 		*linearp = (caddr_t)(uintptr_t)rp->r_pc;
13683939Ssethg 	}
13693939Ssethg 
13703939Ssethg 	return (0);
13713939Ssethg }
137211066Srafael.vanoni@sun.com 
137311066Srafael.vanoni@sun.com /*
137411066Srafael.vanoni@sun.com  * We need to post a soft interrupt to reprogram the lbolt cyclic when
137511066Srafael.vanoni@sun.com  * switching from event to cyclic driven lbolt. The following code adds
137611066Srafael.vanoni@sun.com  * and posts the softint for x86.
137711066Srafael.vanoni@sun.com  */
137811066Srafael.vanoni@sun.com static ddi_softint_hdl_impl_t lbolt_softint_hdl =
137911066Srafael.vanoni@sun.com 	{0, NULL, NULL, NULL, 0, NULL, NULL, NULL};
138011066Srafael.vanoni@sun.com 
138111066Srafael.vanoni@sun.com void
138211066Srafael.vanoni@sun.com lbolt_softint_add(void)
138311066Srafael.vanoni@sun.com {
138411066Srafael.vanoni@sun.com 	(void) add_avsoftintr((void *)&lbolt_softint_hdl, LOCK_LEVEL,
138511066Srafael.vanoni@sun.com 	    (avfunc)lbolt_ev_to_cyclic, "lbolt_ev_to_cyclic", NULL, NULL);
138611066Srafael.vanoni@sun.com }
138711066Srafael.vanoni@sun.com 
138811066Srafael.vanoni@sun.com void
138911066Srafael.vanoni@sun.com lbolt_softint_post(void)
139011066Srafael.vanoni@sun.com {
139111066Srafael.vanoni@sun.com 	(*setsoftint)(CBE_LOCK_PIL, lbolt_softint_hdl.ih_pending);
139211066Srafael.vanoni@sun.com }
1393