xref: /onnv-gate/usr/src/uts/i86pc/os/machdep.c (revision 10916:b5c9d492b177)
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 /*
239160SSherry.Moore@Sun.COM  * Copyright 2009 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 
1330Sstevel@tonic-gate extern void audit_enterprom(int);
1340Sstevel@tonic-gate extern void audit_exitprom(int);
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate /*
1376681Sjohnlev  * Occassionally the kernel knows better whether to power-off or reboot.
1386681Sjohnlev  */
1396681Sjohnlev int force_shutdown_method = AD_UNKNOWN;
1406681Sjohnlev 
1416681Sjohnlev /*
1420Sstevel@tonic-gate  * The panicbuf array is used to record messages and state:
1430Sstevel@tonic-gate  */
1440Sstevel@tonic-gate char panicbuf[PANICBUFSIZE];
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate /*
1470Sstevel@tonic-gate  * maxphys - used during physio
1480Sstevel@tonic-gate  * klustsize - used for klustering by swapfs and specfs
1490Sstevel@tonic-gate  */
1500Sstevel@tonic-gate int maxphys = 56 * 1024;    /* XXX See vm_subr.c - max b_count in physio */
1510Sstevel@tonic-gate int klustsize = 56 * 1024;
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate caddr_t	p0_va;		/* Virtual address for accessing physical page 0 */
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate /*
1560Sstevel@tonic-gate  * defined here, though unused on x86,
1570Sstevel@tonic-gate  * to make kstat_fr.c happy.
1580Sstevel@tonic-gate  */
1590Sstevel@tonic-gate int vac;
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate void debug_enter(char *);
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate extern void pm_cfb_check_and_powerup(void);
1640Sstevel@tonic-gate extern void pm_cfb_rele(void);
1650Sstevel@tonic-gate 
1667656SSherry.Moore@Sun.COM extern fastboot_info_t newkernel;
1677656SSherry.Moore@Sun.COM 
1680Sstevel@tonic-gate /*
1690Sstevel@tonic-gate  * Machine dependent code to reboot.
1700Sstevel@tonic-gate  * "mdep" is interpreted as a character pointer; if non-null, it is a pointer
1710Sstevel@tonic-gate  * to a string to be used as the argument string when rebooting.
172136Sachartre  *
173136Sachartre  * "invoke_cb" is a boolean. It is set to true when mdboot() can safely
174136Sachartre  * invoke CB_CL_MDBOOT callbacks before shutting the system down, i.e. when
175136Sachartre  * we are in a normal shutdown sequence (interrupts are not blocked, the
176136Sachartre  * system is not panic'ing or being suspended).
1770Sstevel@tonic-gate  */
1780Sstevel@tonic-gate /*ARGSUSED*/
1790Sstevel@tonic-gate void
180136Sachartre mdboot(int cmd, int fcn, char *mdep, boolean_t invoke_cb)
1810Sstevel@tonic-gate {
1827656SSherry.Moore@Sun.COM 	processorid_t bootcpuid = 0;
1837863SSherry.Moore@Sun.COM 	static int is_first_quiesce = 1;
1847863SSherry.Moore@Sun.COM 	static int is_first_reset = 1;
1857863SSherry.Moore@Sun.COM 	int reset_status = 0;
1869160SSherry.Moore@Sun.COM 	static char fallback_str[] = "Falling back to regular reboot.\n";
1877656SSherry.Moore@Sun.COM 
1887656SSherry.Moore@Sun.COM 	if (fcn == AD_FASTREBOOT && !newkernel.fi_valid)
1897656SSherry.Moore@Sun.COM 		fcn = AD_BOOT;
1907656SSherry.Moore@Sun.COM 
1910Sstevel@tonic-gate 	if (!panicstr) {
1920Sstevel@tonic-gate 		kpreempt_disable();
1937656SSherry.Moore@Sun.COM 		if (fcn == AD_FASTREBOOT) {
1947656SSherry.Moore@Sun.COM 			mutex_enter(&cpu_lock);
1957656SSherry.Moore@Sun.COM 			if (CPU_ACTIVE(cpu_get(bootcpuid))) {
1967656SSherry.Moore@Sun.COM 				affinity_set(bootcpuid);
1977656SSherry.Moore@Sun.COM 			}
1987656SSherry.Moore@Sun.COM 			mutex_exit(&cpu_lock);
1997656SSherry.Moore@Sun.COM 		} else {
2007656SSherry.Moore@Sun.COM 			affinity_set(CPU_CURRENT);
2017656SSherry.Moore@Sun.COM 		}
2020Sstevel@tonic-gate 	}
2030Sstevel@tonic-gate 
2046681Sjohnlev 	if (force_shutdown_method != AD_UNKNOWN)
2056681Sjohnlev 		fcn = force_shutdown_method;
2066681Sjohnlev 
2070Sstevel@tonic-gate 	/*
2085630Sjbeck 	 * XXX - rconsvp is set to NULL to ensure that output messages
2095630Sjbeck 	 * are sent to the underlying "hardware" device using the
2105630Sjbeck 	 * monitor's printf routine since we are in the process of
2115630Sjbeck 	 * either rebooting or halting the machine.
2125630Sjbeck 	 */
2135630Sjbeck 	rconsvp = NULL;
2145630Sjbeck 
2155630Sjbeck 	/*
2160Sstevel@tonic-gate 	 * Print the reboot message now, before pausing other cpus.
2170Sstevel@tonic-gate 	 * There is a race condition in the printing support that
2180Sstevel@tonic-gate 	 * can deadlock multiprocessor machines.
2190Sstevel@tonic-gate 	 */
2200Sstevel@tonic-gate 	if (!(fcn == AD_HALT || fcn == AD_POWEROFF))
2210Sstevel@tonic-gate 		prom_printf("rebooting...\n");
2220Sstevel@tonic-gate 
2235084Sjohnlev 	if (IN_XPV_PANIC())
2245084Sjohnlev 		reset();
2255084Sjohnlev 
2260Sstevel@tonic-gate 	/*
2270Sstevel@tonic-gate 	 * We can't bring up the console from above lock level, so do it now
2280Sstevel@tonic-gate 	 */
2290Sstevel@tonic-gate 	pm_cfb_check_and_powerup();
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 	/* make sure there are no more changes to the device tree */
2320Sstevel@tonic-gate 	devtree_freeze();
2330Sstevel@tonic-gate 
234136Sachartre 	if (invoke_cb)
235136Sachartre 		(void) callb_execute_class(CB_CL_MDBOOT, NULL);
236136Sachartre 
2373253Smec 	/*
2383253Smec 	 * Clear any unresolved UEs from memory.
2393253Smec 	 */
2403253Smec 	page_retire_mdboot();
241917Selowe 
2425084Sjohnlev #if defined(__xpv)
2435084Sjohnlev 	/*
2445084Sjohnlev 	 * XXPV	Should probably think some more about how we deal
2455084Sjohnlev 	 *	with panicing before it's really safe to panic.
2465084Sjohnlev 	 *	On hypervisors, we reboot very quickly..  Perhaps panic
2475084Sjohnlev 	 *	should only attempt to recover by rebooting if,
2485084Sjohnlev 	 *	say, we were able to mount the root filesystem,
2495084Sjohnlev 	 *	or if we successfully launched init(1m).
2505084Sjohnlev 	 */
2515084Sjohnlev 	if (panicstr && proc_init == NULL)
2525084Sjohnlev 		(void) HYPERVISOR_shutdown(SHUTDOWN_poweroff);
2535084Sjohnlev #endif
2540Sstevel@tonic-gate 	/*
2550Sstevel@tonic-gate 	 * stop other cpus and raise our priority.  since there is only
2560Sstevel@tonic-gate 	 * one active cpu after this, and our priority will be too high
2570Sstevel@tonic-gate 	 * for us to be preempted, we're essentially single threaded
2580Sstevel@tonic-gate 	 * from here on out.
2590Sstevel@tonic-gate 	 */
2600Sstevel@tonic-gate 	(void) spl6();
2610Sstevel@tonic-gate 	if (!panicstr) {
2620Sstevel@tonic-gate 		mutex_enter(&cpu_lock);
2630Sstevel@tonic-gate 		pause_cpus(NULL);
2640Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
2650Sstevel@tonic-gate 	}
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate 	/*
268*10916SSherry.Moore@Sun.COM 	 * If the system is panicking, the preloaded kernel is valid, and
269*10916SSherry.Moore@Sun.COM 	 * fastreboot_onpanic has been set, and the system has been up for
270*10916SSherry.Moore@Sun.COM 	 * longer than fastreboot_onpanic_uptime (default to 10 minutes),
271*10916SSherry.Moore@Sun.COM 	 * choose Fast Reboot.
2729160SSherry.Moore@Sun.COM 	 */
2739160SSherry.Moore@Sun.COM 	if (fcn == AD_BOOT && panicstr && newkernel.fi_valid &&
274*10916SSherry.Moore@Sun.COM 	    fastreboot_onpanic &&
275*10916SSherry.Moore@Sun.COM 	    (panic_lbolt - lbolt_at_boot) > fastreboot_onpanic_uptime) {
2769160SSherry.Moore@Sun.COM 		fcn = AD_FASTREBOOT;
277*10916SSherry.Moore@Sun.COM 	}
2789160SSherry.Moore@Sun.COM 
2799160SSherry.Moore@Sun.COM 	/*
2807656SSherry.Moore@Sun.COM 	 * Try to quiesce devices.
2810Sstevel@tonic-gate 	 */
2827863SSherry.Moore@Sun.COM 	if (is_first_quiesce) {
2837863SSherry.Moore@Sun.COM 		/*
2847863SSherry.Moore@Sun.COM 		 * Clear is_first_quiesce before calling quiesce_devices()
2857863SSherry.Moore@Sun.COM 		 * so that if quiesce_devices() causes panics, it will not
2867863SSherry.Moore@Sun.COM 		 * be invoked again.
2877863SSherry.Moore@Sun.COM 		 */
2887863SSherry.Moore@Sun.COM 		is_first_quiesce = 0;
2897656SSherry.Moore@Sun.COM 
2907656SSherry.Moore@Sun.COM 		quiesce_active = 1;
2917656SSherry.Moore@Sun.COM 		quiesce_devices(ddi_root_node(), &reset_status);
2927783SSherry.Moore@Sun.COM 		if (reset_status == -1) {
2937783SSherry.Moore@Sun.COM 			if (fcn == AD_FASTREBOOT && !force_fastreboot) {
2947783SSherry.Moore@Sun.COM 				prom_printf("Driver(s) not capable of fast "
2959160SSherry.Moore@Sun.COM 				    "reboot.\n");
2969160SSherry.Moore@Sun.COM 				prom_printf(fallback_str);
2977783SSherry.Moore@Sun.COM 				fastreboot_capable = 0;
2989160SSherry.Moore@Sun.COM 				fcn = AD_BOOT;
2997783SSherry.Moore@Sun.COM 			} else if (fcn != AD_FASTREBOOT)
3007783SSherry.Moore@Sun.COM 				fastreboot_capable = 0;
3017656SSherry.Moore@Sun.COM 		}
3027656SSherry.Moore@Sun.COM 		quiesce_active = 0;
3037656SSherry.Moore@Sun.COM 	}
3047656SSherry.Moore@Sun.COM 
3057656SSherry.Moore@Sun.COM 	/*
3067863SSherry.Moore@Sun.COM 	 * Try to reset devices. reset_leaves() should only be called
3077863SSherry.Moore@Sun.COM 	 * a) when there are no other threads that could be accessing devices,
3087863SSherry.Moore@Sun.COM 	 *    and
3097863SSherry.Moore@Sun.COM 	 * b) on a system that's not capable of fast reboot (fastreboot_capable
3107863SSherry.Moore@Sun.COM 	 *    being 0), or on a system where quiesce_devices() failed to
3117863SSherry.Moore@Sun.COM 	 *    complete (quiesce_active being 1).
3127656SSherry.Moore@Sun.COM 	 */
3137863SSherry.Moore@Sun.COM 	if (is_first_reset && (!fastreboot_capable || quiesce_active)) {
3147863SSherry.Moore@Sun.COM 		/*
3157863SSherry.Moore@Sun.COM 		 * Clear is_first_reset before calling reset_devices()
3167863SSherry.Moore@Sun.COM 		 * so that if reset_devices() causes panics, it will not
3177863SSherry.Moore@Sun.COM 		 * be invoked again.
3187863SSherry.Moore@Sun.COM 		 */
3197863SSherry.Moore@Sun.COM 		is_first_reset = 0;
3207656SSherry.Moore@Sun.COM 		reset_leaves();
3217863SSherry.Moore@Sun.COM 	}
3220Sstevel@tonic-gate 
3239160SSherry.Moore@Sun.COM 	/* Verify newkernel checksum */
3249160SSherry.Moore@Sun.COM 	if (fastreboot_capable && fcn == AD_FASTREBOOT &&
3259160SSherry.Moore@Sun.COM 	    fastboot_cksum_verify(&newkernel) != 0) {
3269160SSherry.Moore@Sun.COM 		fastreboot_capable = 0;
3279160SSherry.Moore@Sun.COM 		prom_printf("Fast reboot: checksum failed for the new "
3289160SSherry.Moore@Sun.COM 		    "kernel.\n");
3299160SSherry.Moore@Sun.COM 		prom_printf(fallback_str);
3309160SSherry.Moore@Sun.COM 	}
3319160SSherry.Moore@Sun.COM 
3320Sstevel@tonic-gate 	(void) spl8();
3330Sstevel@tonic-gate 
3349160SSherry.Moore@Sun.COM 	if (fastreboot_capable && fcn == AD_FASTREBOOT) {
3359160SSherry.Moore@Sun.COM 		/*
3369160SSherry.Moore@Sun.COM 		 * psm_shutdown is called within fast_reboot()
3379160SSherry.Moore@Sun.COM 		 */
3387656SSherry.Moore@Sun.COM 		fast_reboot();
3399160SSherry.Moore@Sun.COM 	} else {
3409160SSherry.Moore@Sun.COM 		(*psm_shutdownf)(cmd, fcn);
3419160SSherry.Moore@Sun.COM 
3429160SSherry.Moore@Sun.COM 		if (fcn == AD_HALT || fcn == AD_POWEROFF)
3439160SSherry.Moore@Sun.COM 			halt((char *)NULL);
3449160SSherry.Moore@Sun.COM 		else
3459160SSherry.Moore@Sun.COM 			prom_reboot("");
3469160SSherry.Moore@Sun.COM 	}
3470Sstevel@tonic-gate 	/*NOTREACHED*/
3480Sstevel@tonic-gate }
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate /* mdpreboot - may be called prior to mdboot while root fs still mounted */
3510Sstevel@tonic-gate /*ARGSUSED*/
3520Sstevel@tonic-gate void
3530Sstevel@tonic-gate mdpreboot(int cmd, int fcn, char *mdep)
3540Sstevel@tonic-gate {
3557656SSherry.Moore@Sun.COM 	if (fcn == AD_FASTREBOOT && !fastreboot_capable) {
3567656SSherry.Moore@Sun.COM 		fcn = AD_BOOT;
3577656SSherry.Moore@Sun.COM #ifdef	__xpv
3589160SSherry.Moore@Sun.COM 		cmn_err(CE_WARN, "Fast reboot is not supported on xVM");
3597656SSherry.Moore@Sun.COM #else
3609160SSherry.Moore@Sun.COM 		cmn_err(CE_WARN,
3619160SSherry.Moore@Sun.COM 		    "Fast reboot is not supported on this platform");
3627656SSherry.Moore@Sun.COM #endif
3637656SSherry.Moore@Sun.COM 	}
3647656SSherry.Moore@Sun.COM 
3657656SSherry.Moore@Sun.COM 	if (fcn == AD_FASTREBOOT) {
3669160SSherry.Moore@Sun.COM 		fastboot_load_kernel(mdep);
3677656SSherry.Moore@Sun.COM 		if (!newkernel.fi_valid)
3687656SSherry.Moore@Sun.COM 			fcn = AD_BOOT;
3697656SSherry.Moore@Sun.COM 	}
3707656SSherry.Moore@Sun.COM 
3710Sstevel@tonic-gate 	(*psm_preshutdownf)(cmd, fcn);
3720Sstevel@tonic-gate }
3730Sstevel@tonic-gate 
3749489SJoe.Bonasera@sun.com static void
3759489SJoe.Bonasera@sun.com stop_other_cpus(void)
3760Sstevel@tonic-gate {
3779489SJoe.Bonasera@sun.com 	ulong_t s = clear_int_flag(); /* fast way to keep CPU from changing */
3780Sstevel@tonic-gate 	cpuset_t xcset;
3790Sstevel@tonic-gate 
3809489SJoe.Bonasera@sun.com 	CPUSET_ALL_BUT(xcset, CPU->cpu_id);
3819489SJoe.Bonasera@sun.com 	xc_priority(0, 0, 0, CPUSET2BV(xcset), (xc_func_t)mach_cpu_halt);
3829489SJoe.Bonasera@sun.com 	restore_int_flag(s);
3830Sstevel@tonic-gate }
3840Sstevel@tonic-gate 
3850Sstevel@tonic-gate /*
3860Sstevel@tonic-gate  *	Machine dependent abort sequence handling
3870Sstevel@tonic-gate  */
3880Sstevel@tonic-gate void
3890Sstevel@tonic-gate abort_sequence_enter(char *msg)
3900Sstevel@tonic-gate {
3910Sstevel@tonic-gate 	if (abort_enable == 0) {
3920Sstevel@tonic-gate 		if (audit_active)
3930Sstevel@tonic-gate 			audit_enterprom(0);
3940Sstevel@tonic-gate 		return;
3950Sstevel@tonic-gate 	}
3960Sstevel@tonic-gate 	if (audit_active)
3970Sstevel@tonic-gate 		audit_enterprom(1);
3980Sstevel@tonic-gate 	debug_enter(msg);
3990Sstevel@tonic-gate 	if (audit_active)
4000Sstevel@tonic-gate 		audit_exitprom(1);
4010Sstevel@tonic-gate }
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate /*
4040Sstevel@tonic-gate  * Enter debugger.  Called when the user types ctrl-alt-d or whenever
4050Sstevel@tonic-gate  * code wants to enter the debugger and possibly resume later.
4060Sstevel@tonic-gate  */
4070Sstevel@tonic-gate void
4080Sstevel@tonic-gate debug_enter(
4090Sstevel@tonic-gate 	char	*msg)		/* message to print, possibly NULL */
4100Sstevel@tonic-gate {
4110Sstevel@tonic-gate 	if (dtrace_debugger_init != NULL)
4120Sstevel@tonic-gate 		(*dtrace_debugger_init)();
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate 	if (msg)
4150Sstevel@tonic-gate 		prom_printf("%s\n", msg);
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate 	if (boothowto & RB_DEBUG)
4183446Smrj 		kmdb_enter();
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate 	if (dtrace_debugger_fini != NULL)
4210Sstevel@tonic-gate 		(*dtrace_debugger_fini)();
4220Sstevel@tonic-gate }
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate void
4250Sstevel@tonic-gate reset(void)
4260Sstevel@tonic-gate {
42710457SSaurabh.Mishra@Sun.COM 	extern	void acpi_reset_system();
4285084Sjohnlev #if !defined(__xpv)
4290Sstevel@tonic-gate 	ushort_t *bios_memchk;
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 	/*
43210457SSaurabh.Mishra@Sun.COM 	 * Can't use psm_map_phys or acpi_reset_system before the hat is
43310457SSaurabh.Mishra@Sun.COM 	 * initialized.
4340Sstevel@tonic-gate 	 */
4350Sstevel@tonic-gate 	if (khat_running) {
4360Sstevel@tonic-gate 		bios_memchk = (ushort_t *)psm_map_phys(0x472,
4370Sstevel@tonic-gate 		    sizeof (ushort_t), PROT_READ | PROT_WRITE);
4380Sstevel@tonic-gate 		if (bios_memchk)
4390Sstevel@tonic-gate 			*bios_memchk = 0x1234;	/* bios memory check disable */
44010457SSaurabh.Mishra@Sun.COM 
44110457SSaurabh.Mishra@Sun.COM 		if (options_dip != NULL &&
44210457SSaurabh.Mishra@Sun.COM 		    ddi_prop_exists(DDI_DEV_T_ANY, ddi_root_node(), 0,
44310457SSaurabh.Mishra@Sun.COM 		    "efi-systab")) {
44410457SSaurabh.Mishra@Sun.COM 			efi_reset();
44510457SSaurabh.Mishra@Sun.COM 		}
44610457SSaurabh.Mishra@Sun.COM 
44710457SSaurabh.Mishra@Sun.COM 		/*
44810457SSaurabh.Mishra@Sun.COM 		 * The problem with using stubs is that we can call
44910457SSaurabh.Mishra@Sun.COM 		 * acpi_reset_system only after the kernel is up and running.
45010457SSaurabh.Mishra@Sun.COM 		 *
45110457SSaurabh.Mishra@Sun.COM 		 * We should create a global state to keep track of how far
45210457SSaurabh.Mishra@Sun.COM 		 * up the kernel is but for the time being we will depend on
45310457SSaurabh.Mishra@Sun.COM 		 * bootops. bootops cleared in startup_end().
45410457SSaurabh.Mishra@Sun.COM 		 */
45510457SSaurabh.Mishra@Sun.COM 		if (bootops == NULL)
45610457SSaurabh.Mishra@Sun.COM 			acpi_reset_system();
4570Sstevel@tonic-gate 	}
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate 	pc_reset();
4605084Sjohnlev #else
46110457SSaurabh.Mishra@Sun.COM 	if (IN_XPV_PANIC()) {
46210457SSaurabh.Mishra@Sun.COM 		if (khat_running && bootops == NULL) {
46310457SSaurabh.Mishra@Sun.COM 			acpi_reset_system();
46410457SSaurabh.Mishra@Sun.COM 		}
46510457SSaurabh.Mishra@Sun.COM 
4665084Sjohnlev 		pc_reset();
46710457SSaurabh.Mishra@Sun.COM 	}
46810457SSaurabh.Mishra@Sun.COM 
4695084Sjohnlev 	(void) HYPERVISOR_shutdown(SHUTDOWN_reboot);
4705084Sjohnlev 	panic("HYPERVISOR_shutdown() failed");
4715084Sjohnlev #endif
4720Sstevel@tonic-gate 	/*NOTREACHED*/
4730Sstevel@tonic-gate }
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate /*
4760Sstevel@tonic-gate  * Halt the machine and return to the monitor
4770Sstevel@tonic-gate  */
4780Sstevel@tonic-gate void
4790Sstevel@tonic-gate halt(char *s)
4800Sstevel@tonic-gate {
4810Sstevel@tonic-gate 	stop_other_cpus();	/* send stop signal to other CPUs */
4820Sstevel@tonic-gate 	if (s)
4830Sstevel@tonic-gate 		prom_printf("(%s) \n", s);
4840Sstevel@tonic-gate 	prom_exit_to_mon();
4850Sstevel@tonic-gate 	/*NOTREACHED*/
4860Sstevel@tonic-gate }
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate /*
4890Sstevel@tonic-gate  * Initiate interrupt redistribution.
4900Sstevel@tonic-gate  */
4910Sstevel@tonic-gate void
4920Sstevel@tonic-gate i_ddi_intr_redist_all_cpus()
4930Sstevel@tonic-gate {
4940Sstevel@tonic-gate }
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate /*
4970Sstevel@tonic-gate  * XXX These probably ought to live somewhere else
4980Sstevel@tonic-gate  * XXX They are called from mem.c
4990Sstevel@tonic-gate  */
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate /*
5020Sstevel@tonic-gate  * Convert page frame number to an OBMEM page frame number
5030Sstevel@tonic-gate  * (i.e. put in the type bits -- zero for this implementation)
5040Sstevel@tonic-gate  */
5050Sstevel@tonic-gate pfn_t
5060Sstevel@tonic-gate impl_obmem_pfnum(pfn_t pf)
5070Sstevel@tonic-gate {
5080Sstevel@tonic-gate 	return (pf);
5090Sstevel@tonic-gate }
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate #ifdef	NM_DEBUG
5120Sstevel@tonic-gate int nmi_test = 0;	/* checked in intentry.s during clock int */
5130Sstevel@tonic-gate int nmtest = -1;
5140Sstevel@tonic-gate nmfunc1(arg, rp)
5150Sstevel@tonic-gate int	arg;
5160Sstevel@tonic-gate struct regs *rp;
5170Sstevel@tonic-gate {
5180Sstevel@tonic-gate 	printf("nmi called with arg = %x, regs = %x\n", arg, rp);
5190Sstevel@tonic-gate 	nmtest += 50;
5200Sstevel@tonic-gate 	if (arg == nmtest) {
5210Sstevel@tonic-gate 		printf("ip = %x\n", rp->r_pc);
5220Sstevel@tonic-gate 		return (1);
5230Sstevel@tonic-gate 	}
5240Sstevel@tonic-gate 	return (0);
5250Sstevel@tonic-gate }
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate #endif
5280Sstevel@tonic-gate 
5290Sstevel@tonic-gate #include <sys/bootsvcs.h>
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate /* Hacked up initialization for initial kernel check out is HERE. */
5320Sstevel@tonic-gate /* The basic steps are: */
5330Sstevel@tonic-gate /*	kernel bootfuncs definition/initialization for KADB */
5340Sstevel@tonic-gate /*	kadb bootfuncs pointer initialization */
5350Sstevel@tonic-gate /*	putchar/getchar (interrupts disabled) */
5360Sstevel@tonic-gate 
5370Sstevel@tonic-gate /* kadb bootfuncs pointer initialization */
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate int
5400Sstevel@tonic-gate sysp_getchar()
5410Sstevel@tonic-gate {
5420Sstevel@tonic-gate 	int i;
5433446Smrj 	ulong_t s;
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate 	if (cons_polledio == NULL) {
5460Sstevel@tonic-gate 		/* Uh oh */
5470Sstevel@tonic-gate 		prom_printf("getchar called with no console\n");
5480Sstevel@tonic-gate 		for (;;)
5490Sstevel@tonic-gate 			/* LOOP FOREVER */;
5500Sstevel@tonic-gate 	}
5510Sstevel@tonic-gate 
5520Sstevel@tonic-gate 	s = clear_int_flag();
5530Sstevel@tonic-gate 	i = cons_polledio->cons_polledio_getchar(
5545084Sjohnlev 	    cons_polledio->cons_polledio_argument);
5550Sstevel@tonic-gate 	restore_int_flag(s);
5560Sstevel@tonic-gate 	return (i);
5570Sstevel@tonic-gate }
5580Sstevel@tonic-gate 
5590Sstevel@tonic-gate void
5600Sstevel@tonic-gate sysp_putchar(int c)
5610Sstevel@tonic-gate {
5623446Smrj 	ulong_t s;
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate 	/*
5650Sstevel@tonic-gate 	 * We have no alternative but to drop the output on the floor.
5660Sstevel@tonic-gate 	 */
5671253Slq150181 	if (cons_polledio == NULL ||
5681253Slq150181 	    cons_polledio->cons_polledio_putchar == NULL)
5690Sstevel@tonic-gate 		return;
5700Sstevel@tonic-gate 
5710Sstevel@tonic-gate 	s = clear_int_flag();
5720Sstevel@tonic-gate 	cons_polledio->cons_polledio_putchar(
5735084Sjohnlev 	    cons_polledio->cons_polledio_argument, c);
5740Sstevel@tonic-gate 	restore_int_flag(s);
5750Sstevel@tonic-gate }
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate int
5780Sstevel@tonic-gate sysp_ischar()
5790Sstevel@tonic-gate {
5800Sstevel@tonic-gate 	int i;
5813446Smrj 	ulong_t s;
5820Sstevel@tonic-gate 
5831253Slq150181 	if (cons_polledio == NULL ||
5841253Slq150181 	    cons_polledio->cons_polledio_ischar == NULL)
5850Sstevel@tonic-gate 		return (0);
5860Sstevel@tonic-gate 
5870Sstevel@tonic-gate 	s = clear_int_flag();
5880Sstevel@tonic-gate 	i = cons_polledio->cons_polledio_ischar(
5895084Sjohnlev 	    cons_polledio->cons_polledio_argument);
5900Sstevel@tonic-gate 	restore_int_flag(s);
5910Sstevel@tonic-gate 	return (i);
5920Sstevel@tonic-gate }
5930Sstevel@tonic-gate 
5940Sstevel@tonic-gate int
5950Sstevel@tonic-gate goany(void)
5960Sstevel@tonic-gate {
5970Sstevel@tonic-gate 	prom_printf("Type any key to continue ");
5980Sstevel@tonic-gate 	(void) prom_getchar();
5990Sstevel@tonic-gate 	prom_printf("\n");
6000Sstevel@tonic-gate 	return (1);
6010Sstevel@tonic-gate }
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate static struct boot_syscalls kern_sysp = {
6040Sstevel@tonic-gate 	sysp_getchar,	/*	unchar	(*getchar)();	7  */
6050Sstevel@tonic-gate 	sysp_putchar,	/*	int	(*putchar)();	8  */
6060Sstevel@tonic-gate 	sysp_ischar,	/*	int	(*ischar)();	9  */
6070Sstevel@tonic-gate };
6080Sstevel@tonic-gate 
6095084Sjohnlev #if defined(__xpv)
6105084Sjohnlev int using_kern_polledio;
6115084Sjohnlev #endif
6125084Sjohnlev 
6130Sstevel@tonic-gate void
6140Sstevel@tonic-gate kadb_uses_kernel()
6150Sstevel@tonic-gate {
6160Sstevel@tonic-gate 	/*
6170Sstevel@tonic-gate 	 * This routine is now totally misnamed, since it does not in fact
6180Sstevel@tonic-gate 	 * control kadb's I/O; it only controls the kernel's prom_* I/O.
6190Sstevel@tonic-gate 	 */
6200Sstevel@tonic-gate 	sysp = &kern_sysp;
6215084Sjohnlev #if defined(__xpv)
6225084Sjohnlev 	using_kern_polledio = 1;
6235084Sjohnlev #endif
6240Sstevel@tonic-gate }
6250Sstevel@tonic-gate 
6260Sstevel@tonic-gate /*
6270Sstevel@tonic-gate  *	the interface to the outside world
6280Sstevel@tonic-gate  */
6290Sstevel@tonic-gate 
6300Sstevel@tonic-gate /*
6310Sstevel@tonic-gate  * poll_port -- wait for a register to achieve a
6320Sstevel@tonic-gate  *		specific state.  Arguments are a mask of bits we care about,
6330Sstevel@tonic-gate  *		and two sub-masks.  To return normally, all the bits in the
6340Sstevel@tonic-gate  *		first sub-mask must be ON, all the bits in the second sub-
6350Sstevel@tonic-gate  *		mask must be OFF.  If about seconds pass without the register
6360Sstevel@tonic-gate  *		achieving the desired bit configuration, we return 1, else
6370Sstevel@tonic-gate  *		0.
6380Sstevel@tonic-gate  */
6390Sstevel@tonic-gate int
6400Sstevel@tonic-gate poll_port(ushort_t port, ushort_t mask, ushort_t onbits, ushort_t offbits)
6410Sstevel@tonic-gate {
6420Sstevel@tonic-gate 	int i;
6430Sstevel@tonic-gate 	ushort_t maskval;
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate 	for (i = 500000; i; i--) {
6460Sstevel@tonic-gate 		maskval = inb(port) & mask;
6470Sstevel@tonic-gate 		if (((maskval & onbits) == onbits) &&
6485084Sjohnlev 		    ((maskval & offbits) == 0))
6490Sstevel@tonic-gate 			return (0);
6500Sstevel@tonic-gate 		drv_usecwait(10);
6510Sstevel@tonic-gate 	}
6520Sstevel@tonic-gate 	return (1);
6530Sstevel@tonic-gate }
6540Sstevel@tonic-gate 
6550Sstevel@tonic-gate /*
6560Sstevel@tonic-gate  * set_idle_cpu is called from idle() when a CPU becomes idle.
6570Sstevel@tonic-gate  */
6580Sstevel@tonic-gate /*LINTED: static unused */
6590Sstevel@tonic-gate static uint_t last_idle_cpu;
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate /*ARGSUSED*/
6620Sstevel@tonic-gate void
6630Sstevel@tonic-gate set_idle_cpu(int cpun)
6640Sstevel@tonic-gate {
6650Sstevel@tonic-gate 	last_idle_cpu = cpun;
6660Sstevel@tonic-gate 	(*psm_set_idle_cpuf)(cpun);
6670Sstevel@tonic-gate }
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate /*
6700Sstevel@tonic-gate  * unset_idle_cpu is called from idle() when a CPU is no longer idle.
6710Sstevel@tonic-gate  */
6720Sstevel@tonic-gate /*ARGSUSED*/
6730Sstevel@tonic-gate void
6740Sstevel@tonic-gate unset_idle_cpu(int cpun)
6750Sstevel@tonic-gate {
6760Sstevel@tonic-gate 	(*psm_unset_idle_cpuf)(cpun);
6770Sstevel@tonic-gate }
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate /*
6800Sstevel@tonic-gate  * This routine is almost correct now, but not quite.  It still needs the
6810Sstevel@tonic-gate  * equivalent concept of "hres_last_tick", just like on the sparc side.
6820Sstevel@tonic-gate  * The idea is to take a snapshot of the hi-res timer while doing the
6830Sstevel@tonic-gate  * hrestime_adj updates under hres_lock in locore, so that the small
6840Sstevel@tonic-gate  * interval between interrupt assertion and interrupt processing is
6850Sstevel@tonic-gate  * accounted for correctly.  Once we have this, the code below should
6860Sstevel@tonic-gate  * be modified to subtract off hres_last_tick rather than hrtime_base.
6870Sstevel@tonic-gate  *
6880Sstevel@tonic-gate  * I'd have done this myself, but I don't have source to all of the
6890Sstevel@tonic-gate  * vendor-specific hi-res timer routines (grrr...).  The generic hook I
6900Sstevel@tonic-gate  * need is something like "gethrtime_unlocked()", which would be just like
6910Sstevel@tonic-gate  * gethrtime() but would assume that you're already holding CLOCK_LOCK().
6920Sstevel@tonic-gate  * This is what the GET_HRTIME() macro is for on sparc (although it also
6930Sstevel@tonic-gate  * serves the function of making time available without a function call
6940Sstevel@tonic-gate  * so you don't take a register window overflow while traps are disabled).
6950Sstevel@tonic-gate  */
6960Sstevel@tonic-gate void
6970Sstevel@tonic-gate pc_gethrestime(timestruc_t *tp)
6980Sstevel@tonic-gate {
6990Sstevel@tonic-gate 	int lock_prev;
7000Sstevel@tonic-gate 	timestruc_t now;
7010Sstevel@tonic-gate 	int nslt;		/* nsec since last tick */
7020Sstevel@tonic-gate 	int adj;		/* amount of adjustment to apply */
7030Sstevel@tonic-gate 
7040Sstevel@tonic-gate loop:
7050Sstevel@tonic-gate 	lock_prev = hres_lock;
7060Sstevel@tonic-gate 	now = hrestime;
7070Sstevel@tonic-gate 	nslt = (int)(gethrtime() - hres_last_tick);
7080Sstevel@tonic-gate 	if (nslt < 0) {
7090Sstevel@tonic-gate 		/*
7100Sstevel@tonic-gate 		 * nslt < 0 means a tick came between sampling
7110Sstevel@tonic-gate 		 * gethrtime() and hres_last_tick; restart the loop
7120Sstevel@tonic-gate 		 */
7130Sstevel@tonic-gate 
7140Sstevel@tonic-gate 		goto loop;
7150Sstevel@tonic-gate 	}
7160Sstevel@tonic-gate 	now.tv_nsec += nslt;
7170Sstevel@tonic-gate 	if (hrestime_adj != 0) {
7180Sstevel@tonic-gate 		if (hrestime_adj > 0) {
7190Sstevel@tonic-gate 			adj = (nslt >> ADJ_SHIFT);
7200Sstevel@tonic-gate 			if (adj > hrestime_adj)
7210Sstevel@tonic-gate 				adj = (int)hrestime_adj;
7220Sstevel@tonic-gate 		} else {
7230Sstevel@tonic-gate 			adj = -(nslt >> ADJ_SHIFT);
7240Sstevel@tonic-gate 			if (adj < hrestime_adj)
7250Sstevel@tonic-gate 				adj = (int)hrestime_adj;
7260Sstevel@tonic-gate 		}
7270Sstevel@tonic-gate 		now.tv_nsec += adj;
7280Sstevel@tonic-gate 	}
7290Sstevel@tonic-gate 	while ((unsigned long)now.tv_nsec >= NANOSEC) {
7300Sstevel@tonic-gate 
7310Sstevel@tonic-gate 		/*
7320Sstevel@tonic-gate 		 * We might have a large adjustment or have been in the
7330Sstevel@tonic-gate 		 * debugger for a long time; take care of (at most) four
7340Sstevel@tonic-gate 		 * of those missed seconds (tv_nsec is 32 bits, so
7350Sstevel@tonic-gate 		 * anything >4s will be wrapping around).  However,
7360Sstevel@tonic-gate 		 * anything more than 2 seconds out of sync will trigger
7370Sstevel@tonic-gate 		 * timedelta from clock() to go correct the time anyway,
7380Sstevel@tonic-gate 		 * so do what we can, and let the big crowbar do the
7390Sstevel@tonic-gate 		 * rest.  A similar correction while loop exists inside
7400Sstevel@tonic-gate 		 * hres_tick(); in all cases we'd like tv_nsec to
7410Sstevel@tonic-gate 		 * satisfy 0 <= tv_nsec < NANOSEC to avoid confusing
7420Sstevel@tonic-gate 		 * user processes, but if tv_sec's a little behind for a
7430Sstevel@tonic-gate 		 * little while, that's OK; time still monotonically
7440Sstevel@tonic-gate 		 * increases.
7450Sstevel@tonic-gate 		 */
7460Sstevel@tonic-gate 
7470Sstevel@tonic-gate 		now.tv_nsec -= NANOSEC;
7480Sstevel@tonic-gate 		now.tv_sec++;
7490Sstevel@tonic-gate 	}
7500Sstevel@tonic-gate 	if ((hres_lock & ~1) != lock_prev)
7510Sstevel@tonic-gate 		goto loop;
7520Sstevel@tonic-gate 
7530Sstevel@tonic-gate 	*tp = now;
7540Sstevel@tonic-gate }
7550Sstevel@tonic-gate 
7560Sstevel@tonic-gate void
7570Sstevel@tonic-gate gethrestime_lasttick(timespec_t *tp)
7580Sstevel@tonic-gate {
7590Sstevel@tonic-gate 	int s;
7600Sstevel@tonic-gate 
7610Sstevel@tonic-gate 	s = hr_clock_lock();
7620Sstevel@tonic-gate 	*tp = hrestime;
7630Sstevel@tonic-gate 	hr_clock_unlock(s);
7640Sstevel@tonic-gate }
7650Sstevel@tonic-gate 
7660Sstevel@tonic-gate time_t
7670Sstevel@tonic-gate gethrestime_sec(void)
7680Sstevel@tonic-gate {
7690Sstevel@tonic-gate 	timestruc_t now;
7700Sstevel@tonic-gate 
7710Sstevel@tonic-gate 	gethrestime(&now);
7720Sstevel@tonic-gate 	return (now.tv_sec);
7730Sstevel@tonic-gate }
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate /*
7760Sstevel@tonic-gate  * Initialize a kernel thread's stack
7770Sstevel@tonic-gate  */
7780Sstevel@tonic-gate 
7790Sstevel@tonic-gate caddr_t
7800Sstevel@tonic-gate thread_stk_init(caddr_t stk)
7810Sstevel@tonic-gate {
7820Sstevel@tonic-gate 	ASSERT(((uintptr_t)stk & (STACK_ALIGN - 1)) == 0);
7830Sstevel@tonic-gate 	return (stk - SA(MINFRAME));
7840Sstevel@tonic-gate }
7850Sstevel@tonic-gate 
7860Sstevel@tonic-gate /*
7870Sstevel@tonic-gate  * Initialize lwp's kernel stack.
7880Sstevel@tonic-gate  */
7890Sstevel@tonic-gate 
7900Sstevel@tonic-gate #ifdef TRAPTRACE
7910Sstevel@tonic-gate /*
7920Sstevel@tonic-gate  * There's a tricky interdependency here between use of sysenter and
7930Sstevel@tonic-gate  * TRAPTRACE which needs recording to avoid future confusion (this is
7940Sstevel@tonic-gate  * about the third time I've re-figured this out ..)
7950Sstevel@tonic-gate  *
7960Sstevel@tonic-gate  * Here's how debugging lcall works with TRAPTRACE.
7970Sstevel@tonic-gate  *
7980Sstevel@tonic-gate  * 1 We're in userland with a breakpoint on the lcall instruction.
7990Sstevel@tonic-gate  * 2 We execute the instruction - the instruction pushes the userland
8000Sstevel@tonic-gate  *   %ss, %esp, %efl, %cs, %eip on the stack and zips into the kernel
8010Sstevel@tonic-gate  *   via the call gate.
8020Sstevel@tonic-gate  * 3 The hardware raises a debug trap in kernel mode, the hardware
8030Sstevel@tonic-gate  *   pushes %efl, %cs, %eip and gets to dbgtrap via the idt.
8040Sstevel@tonic-gate  * 4 dbgtrap pushes the error code and trapno and calls cmntrap
8050Sstevel@tonic-gate  * 5 cmntrap finishes building a trap frame
8060Sstevel@tonic-gate  * 6 The TRACE_REGS macros in cmntrap copy a REGSIZE worth chunk
8070Sstevel@tonic-gate  *   off the stack into the traptrace buffer.
8080Sstevel@tonic-gate  *
8090Sstevel@tonic-gate  * This means that the traptrace buffer contains the wrong values in
8100Sstevel@tonic-gate  * %esp and %ss, but everything else in there is correct.
8110Sstevel@tonic-gate  *
8120Sstevel@tonic-gate  * Here's how debugging sysenter works with TRAPTRACE.
8130Sstevel@tonic-gate  *
8140Sstevel@tonic-gate  * a We're in userland with a breakpoint on the sysenter instruction.
8150Sstevel@tonic-gate  * b We execute the instruction - the instruction pushes -nothing-
8160Sstevel@tonic-gate  *   on the stack, but sets %cs, %eip, %ss, %esp to prearranged
8170Sstevel@tonic-gate  *   values to take us to sys_sysenter, at the top of the lwp's
8180Sstevel@tonic-gate  *   stack.
8190Sstevel@tonic-gate  * c goto 3
8200Sstevel@tonic-gate  *
8210Sstevel@tonic-gate  * At this point, because we got into the kernel without the requisite
8220Sstevel@tonic-gate  * five pushes on the stack, if we didn't make extra room, we'd
8230Sstevel@tonic-gate  * end up with the TRACE_REGS macro fetching the saved %ss and %esp
8240Sstevel@tonic-gate  * values from negative (unmapped) stack addresses -- which really bites.
8250Sstevel@tonic-gate  * That's why we do the '-= 8' below.
8260Sstevel@tonic-gate  *
8270Sstevel@tonic-gate  * XXX	Note that reading "up" lwp0's stack works because t0 is declared
8280Sstevel@tonic-gate  *	right next to t0stack in locore.s
8290Sstevel@tonic-gate  */
8300Sstevel@tonic-gate #endif
8310Sstevel@tonic-gate 
8320Sstevel@tonic-gate caddr_t
8330Sstevel@tonic-gate lwp_stk_init(klwp_t *lwp, caddr_t stk)
8340Sstevel@tonic-gate {
8350Sstevel@tonic-gate 	caddr_t oldstk;
8360Sstevel@tonic-gate 	struct pcb *pcb = &lwp->lwp_pcb;
8370Sstevel@tonic-gate 
8380Sstevel@tonic-gate 	oldstk = stk;
8390Sstevel@tonic-gate 	stk -= SA(sizeof (struct regs) + SA(MINFRAME));
8400Sstevel@tonic-gate #ifdef TRAPTRACE
8410Sstevel@tonic-gate 	stk -= 2 * sizeof (greg_t); /* space for phony %ss:%sp (see above) */
8420Sstevel@tonic-gate #endif
8430Sstevel@tonic-gate 	stk = (caddr_t)((uintptr_t)stk & ~(STACK_ALIGN - 1ul));
8440Sstevel@tonic-gate 	bzero(stk, oldstk - stk);
8450Sstevel@tonic-gate 	lwp->lwp_regs = (void *)(stk + SA(MINFRAME));
8460Sstevel@tonic-gate 
8470Sstevel@tonic-gate 	/*
8480Sstevel@tonic-gate 	 * Arrange that the virtualized %fs and %gs GDT descriptors
8490Sstevel@tonic-gate 	 * have a well-defined initial state (present, ring 3
8500Sstevel@tonic-gate 	 * and of type data).
8510Sstevel@tonic-gate 	 */
8520Sstevel@tonic-gate #if defined(__amd64)
8530Sstevel@tonic-gate 	if (lwp_getdatamodel(lwp) == DATAMODEL_NATIVE)
8540Sstevel@tonic-gate 		pcb->pcb_fsdesc = pcb->pcb_gsdesc = zero_udesc;
8550Sstevel@tonic-gate 	else
8560Sstevel@tonic-gate 		pcb->pcb_fsdesc = pcb->pcb_gsdesc = zero_u32desc;
8570Sstevel@tonic-gate #elif defined(__i386)
8580Sstevel@tonic-gate 	pcb->pcb_fsdesc = pcb->pcb_gsdesc = zero_udesc;
8590Sstevel@tonic-gate #endif	/* __i386 */
8600Sstevel@tonic-gate 	lwp_installctx(lwp);
8610Sstevel@tonic-gate 	return (stk);
8620Sstevel@tonic-gate }
8630Sstevel@tonic-gate 
8640Sstevel@tonic-gate /*ARGSUSED*/
8650Sstevel@tonic-gate void
8660Sstevel@tonic-gate lwp_stk_fini(klwp_t *lwp)
8670Sstevel@tonic-gate {}
8680Sstevel@tonic-gate 
8690Sstevel@tonic-gate /*
8701389Sdmick  * If we're not the panic CPU, we wait in panic_idle for reboot.
8710Sstevel@tonic-gate  */
8729489SJoe.Bonasera@sun.com void
8730Sstevel@tonic-gate panic_idle(void)
8740Sstevel@tonic-gate {
8750Sstevel@tonic-gate 	splx(ipltospl(CLOCK_LEVEL));
8760Sstevel@tonic-gate 	(void) setjmp(&curthread->t_pcb);
8770Sstevel@tonic-gate 
87810843SDave.Plauger@Sun.COM 	dumpsys_helper();
87910843SDave.Plauger@Sun.COM 
8809489SJoe.Bonasera@sun.com #ifndef __xpv
8819489SJoe.Bonasera@sun.com 	for (;;)
8829489SJoe.Bonasera@sun.com 		i86_halt();
8839489SJoe.Bonasera@sun.com #else
8843446Smrj 	for (;;)
8853446Smrj 		;
8869489SJoe.Bonasera@sun.com #endif
8870Sstevel@tonic-gate }
8880Sstevel@tonic-gate 
8890Sstevel@tonic-gate /*
8900Sstevel@tonic-gate  * Stop the other CPUs by cross-calling them and forcing them to enter
8910Sstevel@tonic-gate  * the panic_idle() loop above.
8920Sstevel@tonic-gate  */
8930Sstevel@tonic-gate /*ARGSUSED*/
8940Sstevel@tonic-gate void
8950Sstevel@tonic-gate panic_stopcpus(cpu_t *cp, kthread_t *t, int spl)
8960Sstevel@tonic-gate {
8970Sstevel@tonic-gate 	processorid_t i;
8980Sstevel@tonic-gate 	cpuset_t xcset;
8990Sstevel@tonic-gate 
9005084Sjohnlev 	/*
9015084Sjohnlev 	 * In the case of a Xen panic, the hypervisor has already stopped
9025084Sjohnlev 	 * all of the CPUs.
9035084Sjohnlev 	 */
9045084Sjohnlev 	if (!IN_XPV_PANIC()) {
9055084Sjohnlev 		(void) splzs();
9060Sstevel@tonic-gate 
9075084Sjohnlev 		CPUSET_ALL_BUT(xcset, cp->cpu_id);
9089489SJoe.Bonasera@sun.com 		xc_priority(0, 0, 0, CPUSET2BV(xcset), (xc_func_t)panic_idle);
9095084Sjohnlev 	}
9100Sstevel@tonic-gate 
9110Sstevel@tonic-gate 	for (i = 0; i < NCPU; i++) {
9120Sstevel@tonic-gate 		if (i != cp->cpu_id && cpu[i] != NULL &&
9130Sstevel@tonic-gate 		    (cpu[i]->cpu_flags & CPU_EXISTS))
9140Sstevel@tonic-gate 			cpu[i]->cpu_flags |= CPU_QUIESCED;
9150Sstevel@tonic-gate 	}
9160Sstevel@tonic-gate }
9170Sstevel@tonic-gate 
9180Sstevel@tonic-gate /*
9190Sstevel@tonic-gate  * Platform callback following each entry to panicsys().
9200Sstevel@tonic-gate  */
9210Sstevel@tonic-gate /*ARGSUSED*/
9220Sstevel@tonic-gate void
9230Sstevel@tonic-gate panic_enter_hw(int spl)
9240Sstevel@tonic-gate {
9250Sstevel@tonic-gate 	/* Nothing to do here */
9260Sstevel@tonic-gate }
9270Sstevel@tonic-gate 
9280Sstevel@tonic-gate /*
9290Sstevel@tonic-gate  * Platform-specific code to execute after panicstr is set: we invoke
9300Sstevel@tonic-gate  * the PSM entry point to indicate that a panic has occurred.
9310Sstevel@tonic-gate  */
9320Sstevel@tonic-gate /*ARGSUSED*/
9330Sstevel@tonic-gate void
9340Sstevel@tonic-gate panic_quiesce_hw(panic_data_t *pdp)
9350Sstevel@tonic-gate {
9360Sstevel@tonic-gate 	psm_notifyf(PSM_PANIC_ENTER);
9370Sstevel@tonic-gate 
9387532SSean.Ye@Sun.COM 	cmi_panic_callback();
9397532SSean.Ye@Sun.COM 
9400Sstevel@tonic-gate #ifdef	TRAPTRACE
9410Sstevel@tonic-gate 	/*
9420Sstevel@tonic-gate 	 * Turn off TRAPTRACE
9430Sstevel@tonic-gate 	 */
9440Sstevel@tonic-gate 	TRAPTRACE_FREEZE;
9450Sstevel@tonic-gate #endif	/* TRAPTRACE */
9460Sstevel@tonic-gate }
9470Sstevel@tonic-gate 
9480Sstevel@tonic-gate /*
9490Sstevel@tonic-gate  * Platform callback prior to writing crash dump.
9500Sstevel@tonic-gate  */
9510Sstevel@tonic-gate /*ARGSUSED*/
9520Sstevel@tonic-gate void
9530Sstevel@tonic-gate panic_dump_hw(int spl)
9540Sstevel@tonic-gate {
9550Sstevel@tonic-gate 	/* Nothing to do here */
9560Sstevel@tonic-gate }
9570Sstevel@tonic-gate 
9585084Sjohnlev void *
9595084Sjohnlev plat_traceback(void *fpreg)
9605084Sjohnlev {
9615084Sjohnlev #ifdef __xpv
9625084Sjohnlev 	if (IN_XPV_PANIC())
9635084Sjohnlev 		return (xpv_traceback(fpreg));
9645084Sjohnlev #endif
9655084Sjohnlev 	return (fpreg);
9665084Sjohnlev }
9675084Sjohnlev 
9680Sstevel@tonic-gate /*ARGSUSED*/
9690Sstevel@tonic-gate void
9700Sstevel@tonic-gate plat_tod_fault(enum tod_fault_type tod_bad)
9713446Smrj {}
9720Sstevel@tonic-gate 
9730Sstevel@tonic-gate /*ARGSUSED*/
9740Sstevel@tonic-gate int
9750Sstevel@tonic-gate blacklist(int cmd, const char *scheme, nvlist_t *fmri, const char *class)
9760Sstevel@tonic-gate {
9770Sstevel@tonic-gate 	return (ENOTSUP);
9780Sstevel@tonic-gate }
9790Sstevel@tonic-gate 
9800Sstevel@tonic-gate /*
9810Sstevel@tonic-gate  * The underlying console output routines are protected by raising IPL in case
9820Sstevel@tonic-gate  * we are still calling into the early boot services.  Once we start calling
9830Sstevel@tonic-gate  * the kernel console emulator, it will disable interrupts completely during
9840Sstevel@tonic-gate  * character rendering (see sysp_putchar, for example).  Refer to the comments
9850Sstevel@tonic-gate  * and code in common/os/console.c for more information on these callbacks.
9860Sstevel@tonic-gate  */
9870Sstevel@tonic-gate /*ARGSUSED*/
9880Sstevel@tonic-gate int
9890Sstevel@tonic-gate console_enter(int busy)
9900Sstevel@tonic-gate {
9910Sstevel@tonic-gate 	return (splzs());
9920Sstevel@tonic-gate }
9930Sstevel@tonic-gate 
9940Sstevel@tonic-gate /*ARGSUSED*/
9950Sstevel@tonic-gate void
9960Sstevel@tonic-gate console_exit(int busy, int spl)
9970Sstevel@tonic-gate {
9980Sstevel@tonic-gate 	splx(spl);
9990Sstevel@tonic-gate }
10000Sstevel@tonic-gate 
10010Sstevel@tonic-gate /*
10020Sstevel@tonic-gate  * Allocate a region of virtual address space, unmapped.
10030Sstevel@tonic-gate  * Stubbed out except on sparc, at least for now.
10040Sstevel@tonic-gate  */
10050Sstevel@tonic-gate /*ARGSUSED*/
10060Sstevel@tonic-gate void *
10070Sstevel@tonic-gate boot_virt_alloc(void *addr, size_t size)
10080Sstevel@tonic-gate {
10090Sstevel@tonic-gate 	return (addr);
10100Sstevel@tonic-gate }
10110Sstevel@tonic-gate 
10120Sstevel@tonic-gate volatile unsigned long	tenmicrodata;
10130Sstevel@tonic-gate 
10140Sstevel@tonic-gate void
10150Sstevel@tonic-gate tenmicrosec(void)
10160Sstevel@tonic-gate {
10175084Sjohnlev 	extern int gethrtime_hires;
10180Sstevel@tonic-gate 
10195084Sjohnlev 	if (gethrtime_hires) {
10200Sstevel@tonic-gate 		hrtime_t start, end;
10210Sstevel@tonic-gate 		start = end =  gethrtime();
10220Sstevel@tonic-gate 		while ((end - start) < (10 * (NANOSEC / MICROSEC))) {
10230Sstevel@tonic-gate 			SMT_PAUSE();
10240Sstevel@tonic-gate 			end = gethrtime();
10250Sstevel@tonic-gate 		}
10260Sstevel@tonic-gate 	} else {
10275084Sjohnlev #if defined(__xpv)
10285084Sjohnlev 		hrtime_t newtime;
10295084Sjohnlev 
10305084Sjohnlev 		newtime = xpv_gethrtime() + 10000; /* now + 10 us */
10315084Sjohnlev 		while (xpv_gethrtime() < newtime)
10325084Sjohnlev 			SMT_PAUSE();
10335084Sjohnlev #else	/* __xpv */
10343446Smrj 		int i;
10353446Smrj 
10360Sstevel@tonic-gate 		/*
10370Sstevel@tonic-gate 		 * Artificial loop to induce delay.
10380Sstevel@tonic-gate 		 */
10390Sstevel@tonic-gate 		for (i = 0; i < microdata; i++)
10400Sstevel@tonic-gate 			tenmicrodata = microdata;
10415084Sjohnlev #endif	/* __xpv */
10420Sstevel@tonic-gate 	}
10430Sstevel@tonic-gate }
1044590Sesolom 
1045590Sesolom /*
1046590Sesolom  * get_cpu_mstate() is passed an array of timestamps, NCMSTATES
1047590Sesolom  * long, and it fills in the array with the time spent on cpu in
1048590Sesolom  * each of the mstates, where time is returned in nsec.
1049590Sesolom  *
1050590Sesolom  * No guarantee is made that the returned values in times[] will
1051590Sesolom  * monotonically increase on sequential calls, although this will
1052590Sesolom  * be true in the long run. Any such guarantee must be handled by
1053590Sesolom  * the caller, if needed. This can happen if we fail to account
1054590Sesolom  * for elapsed time due to a generation counter conflict, yet we
1055590Sesolom  * did account for it on a prior call (see below).
1056590Sesolom  *
1057590Sesolom  * The complication is that the cpu in question may be updating
1058590Sesolom  * its microstate at the same time that we are reading it.
1059590Sesolom  * Because the microstate is only updated when the CPU's state
1060590Sesolom  * changes, the values in cpu_intracct[] can be indefinitely out
1061590Sesolom  * of date. To determine true current values, it is necessary to
1062590Sesolom  * compare the current time with cpu_mstate_start, and add the
1063590Sesolom  * difference to times[cpu_mstate].
1064590Sesolom  *
1065590Sesolom  * This can be a problem if those values are changing out from
1066590Sesolom  * under us. Because the code path in new_cpu_mstate() is
1067590Sesolom  * performance critical, we have not added a lock to it. Instead,
1068590Sesolom  * we have added a generation counter. Before beginning
1069590Sesolom  * modifications, the counter is set to 0. After modifications,
1070590Sesolom  * it is set to the old value plus one.
1071590Sesolom  *
1072590Sesolom  * get_cpu_mstate() will not consider the values of cpu_mstate
1073590Sesolom  * and cpu_mstate_start to be usable unless the value of
1074590Sesolom  * cpu_mstate_gen is both non-zero and unchanged, both before and
1075590Sesolom  * after reading the mstate information. Note that we must
1076590Sesolom  * protect against out-of-order loads around accesses to the
1077590Sesolom  * generation counter. Also, this is a best effort approach in
1078590Sesolom  * that we do not retry should the counter be found to have
1079590Sesolom  * changed.
1080590Sesolom  *
1081590Sesolom  * cpu_intracct[] is used to identify time spent in each CPU
1082590Sesolom  * mstate while handling interrupts. Such time should be reported
1083590Sesolom  * against system time, and so is subtracted out from its
1084590Sesolom  * corresponding cpu_acct[] time and added to
1085590Sesolom  * cpu_acct[CMS_SYSTEM].
1086590Sesolom  */
1087590Sesolom 
1088590Sesolom void
1089590Sesolom get_cpu_mstate(cpu_t *cpu, hrtime_t *times)
1090590Sesolom {
1091590Sesolom 	int i;
1092590Sesolom 	hrtime_t now, start;
1093590Sesolom 	uint16_t gen;
1094590Sesolom 	uint16_t state;
1095590Sesolom 	hrtime_t intracct[NCMSTATES];
1096590Sesolom 
1097590Sesolom 	/*
1098590Sesolom 	 * Load all volatile state under the protection of membar.
1099590Sesolom 	 * cpu_acct[cpu_mstate] must be loaded to avoid double counting
1100590Sesolom 	 * of (now - cpu_mstate_start) by a change in CPU mstate that
1101590Sesolom 	 * arrives after we make our last check of cpu_mstate_gen.
1102590Sesolom 	 */
1103590Sesolom 
1104590Sesolom 	now = gethrtime_unscaled();
1105590Sesolom 	gen = cpu->cpu_mstate_gen;
1106590Sesolom 
1107590Sesolom 	membar_consumer();	/* guarantee load ordering */
1108590Sesolom 	start = cpu->cpu_mstate_start;
1109590Sesolom 	state = cpu->cpu_mstate;
1110590Sesolom 	for (i = 0; i < NCMSTATES; i++) {
1111590Sesolom 		intracct[i] = cpu->cpu_intracct[i];
1112590Sesolom 		times[i] = cpu->cpu_acct[i];
1113590Sesolom 	}
1114590Sesolom 	membar_consumer();	/* guarantee load ordering */
1115590Sesolom 
1116590Sesolom 	if (gen != 0 && gen == cpu->cpu_mstate_gen && now > start)
1117590Sesolom 		times[state] += now - start;
1118590Sesolom 
1119590Sesolom 	for (i = 0; i < NCMSTATES; i++) {
1120590Sesolom 		if (i == CMS_SYSTEM)
1121590Sesolom 			continue;
1122590Sesolom 		times[i] -= intracct[i];
1123590Sesolom 		if (times[i] < 0) {
1124590Sesolom 			intracct[i] += times[i];
1125590Sesolom 			times[i] = 0;
1126590Sesolom 		}
1127590Sesolom 		times[CMS_SYSTEM] += intracct[i];
1128590Sesolom 		scalehrtime(&times[i]);
1129590Sesolom 	}
1130590Sesolom 	scalehrtime(&times[CMS_SYSTEM]);
1131590Sesolom }
11323446Smrj 
11333446Smrj /*
11343446Smrj  * This is a version of the rdmsr instruction that allows
11353446Smrj  * an error code to be returned in the case of failure.
11363446Smrj  */
11373446Smrj int
11383446Smrj checked_rdmsr(uint_t msr, uint64_t *value)
11393446Smrj {
11403446Smrj 	if ((x86_feature & X86_MSR) == 0)
11413446Smrj 		return (ENOTSUP);
11423446Smrj 	*value = rdmsr(msr);
11433446Smrj 	return (0);
11443446Smrj }
11453446Smrj 
11463446Smrj /*
11473446Smrj  * This is a version of the wrmsr instruction that allows
11483446Smrj  * an error code to be returned in the case of failure.
11493446Smrj  */
11503446Smrj int
11513446Smrj checked_wrmsr(uint_t msr, uint64_t value)
11523446Smrj {
11533446Smrj 	if ((x86_feature & X86_MSR) == 0)
11543446Smrj 		return (ENOTSUP);
11553446Smrj 	wrmsr(msr, value);
11563446Smrj 	return (0);
11573446Smrj }
11583446Smrj 
11593446Smrj /*
11605084Sjohnlev  * The mem driver's usual method of using hat_devload() to establish a
11615084Sjohnlev  * temporary mapping will not work for foreign pages mapped into this
11625084Sjohnlev  * domain or for the special hypervisor-provided pages.  For the foreign
11635084Sjohnlev  * pages, we often don't know which domain owns them, so we can't ask the
11645084Sjohnlev  * hypervisor to set up a new mapping.  For the other pages, we don't have
11655084Sjohnlev  * a pfn, so we can't create a new PTE.  For these special cases, we do a
11665084Sjohnlev  * direct uiomove() from the existing kernel virtual address.
11673446Smrj  */
11683446Smrj /*ARGSUSED*/
11693446Smrj int
11705084Sjohnlev plat_mem_do_mmio(struct uio *uio, enum uio_rw rw)
11715084Sjohnlev {
11725084Sjohnlev #if defined(__xpv)
11735084Sjohnlev 	void *va = (void *)(uintptr_t)uio->uio_loffset;
11745084Sjohnlev 	off_t pageoff = uio->uio_loffset & PAGEOFFSET;
11755084Sjohnlev 	size_t nbytes = MIN((size_t)(PAGESIZE - pageoff),
11765084Sjohnlev 	    (size_t)uio->uio_iov->iov_len);
11775084Sjohnlev 
11785084Sjohnlev 	if ((rw == UIO_READ &&
11795084Sjohnlev 	    (va == HYPERVISOR_shared_info || va == xen_info)) ||
11805084Sjohnlev 	    (pfn_is_foreign(hat_getpfnum(kas.a_hat, va))))
11815084Sjohnlev 		return (uiomove(va, nbytes, rw, uio));
11825084Sjohnlev #endif
11835084Sjohnlev 	return (ENOTSUP);
11845084Sjohnlev }
11855084Sjohnlev 
11865084Sjohnlev pgcnt_t
11875084Sjohnlev num_phys_pages()
11883446Smrj {
11895084Sjohnlev 	pgcnt_t npages = 0;
11905084Sjohnlev 	struct memlist *mp;
11915084Sjohnlev 
11925084Sjohnlev #if defined(__xpv)
119310175SStuart.Maybee@Sun.COM 	if (DOMAIN_IS_INITDOMAIN(xen_info))
119410175SStuart.Maybee@Sun.COM 		return (xpv_nr_phys_pages());
11955084Sjohnlev #endif /* __xpv */
11965084Sjohnlev 
11975084Sjohnlev 	for (mp = phys_install; mp != NULL; mp = mp->next)
11985084Sjohnlev 		npages += mp->size >> PAGESHIFT;
11995084Sjohnlev 
12005084Sjohnlev 	return (npages);
12013446Smrj }
12023446Smrj 
120310843SDave.Plauger@Sun.COM /* cpu threshold for compressed dumps */
120410843SDave.Plauger@Sun.COM #ifdef _LP64
120510843SDave.Plauger@Sun.COM uint_t dump_plat_mincpu = DUMP_PLAT_X86_64_MINCPU;
120610843SDave.Plauger@Sun.COM #else
120710843SDave.Plauger@Sun.COM uint_t dump_plat_mincpu = DUMP_PLAT_X86_32_MINCPU;
120810843SDave.Plauger@Sun.COM #endif
120910843SDave.Plauger@Sun.COM 
12103446Smrj int
12113446Smrj dump_plat_addr()
12123446Smrj {
12135084Sjohnlev #ifdef __xpv
12145084Sjohnlev 	pfn_t pfn = mmu_btop(xen_info->shared_info) | PFN_IS_FOREIGN_MFN;
12155084Sjohnlev 	mem_vtop_t mem_vtop;
12165084Sjohnlev 	int cnt;
12175084Sjohnlev 
12185084Sjohnlev 	/*
12195084Sjohnlev 	 * On the hypervisor, we want to dump the page with shared_info on it.
12205084Sjohnlev 	 */
12215084Sjohnlev 	if (!IN_XPV_PANIC()) {
12225084Sjohnlev 		mem_vtop.m_as = &kas;
12235084Sjohnlev 		mem_vtop.m_va = HYPERVISOR_shared_info;
12245084Sjohnlev 		mem_vtop.m_pfn = pfn;
12255084Sjohnlev 		dumpvp_write(&mem_vtop, sizeof (mem_vtop_t));
12265084Sjohnlev 		cnt = 1;
12275084Sjohnlev 	} else {
12285084Sjohnlev 		cnt = dump_xpv_addr();
12295084Sjohnlev 	}
12305084Sjohnlev 	return (cnt);
12315084Sjohnlev #else
12323446Smrj 	return (0);
12335084Sjohnlev #endif
12343446Smrj }
12353446Smrj 
12363446Smrj void
12373446Smrj dump_plat_pfn()
12383446Smrj {
12395084Sjohnlev #ifdef __xpv
12405084Sjohnlev 	pfn_t pfn = mmu_btop(xen_info->shared_info) | PFN_IS_FOREIGN_MFN;
12415084Sjohnlev 
12425084Sjohnlev 	if (!IN_XPV_PANIC())
12435084Sjohnlev 		dumpvp_write(&pfn, sizeof (pfn));
12445084Sjohnlev 	else
12455084Sjohnlev 		dump_xpv_pfn();
12465084Sjohnlev #endif
12473446Smrj }
12483446Smrj 
12493446Smrj /*ARGSUSED*/
12503446Smrj int
12513446Smrj dump_plat_data(void *dump_cbuf)
12523446Smrj {
12535084Sjohnlev #ifdef __xpv
12545084Sjohnlev 	uint32_t csize;
12555084Sjohnlev 	int cnt;
12565084Sjohnlev 
12575084Sjohnlev 	if (!IN_XPV_PANIC()) {
12585084Sjohnlev 		csize = (uint32_t)compress(HYPERVISOR_shared_info, dump_cbuf,
12595084Sjohnlev 		    PAGESIZE);
12605084Sjohnlev 		dumpvp_write(&csize, sizeof (uint32_t));
12615084Sjohnlev 		dumpvp_write(dump_cbuf, csize);
12625084Sjohnlev 		cnt = 1;
12635084Sjohnlev 	} else {
12645084Sjohnlev 		cnt = dump_xpv_data(dump_cbuf);
12655084Sjohnlev 	}
12665084Sjohnlev 	return (cnt);
12675084Sjohnlev #else
12683446Smrj 	return (0);
12695084Sjohnlev #endif
12703446Smrj }
12713939Ssethg 
12723939Ssethg /*
12733939Ssethg  * Calculates a linear address, given the CS selector and PC values
12743939Ssethg  * by looking up the %cs selector process's LDT or the CPU's GDT.
12753939Ssethg  * proc->p_ldtlock must be held across this call.
12763939Ssethg  */
12773939Ssethg int
12783939Ssethg linear_pc(struct regs *rp, proc_t *p, caddr_t *linearp)
12793939Ssethg {
12803939Ssethg 	user_desc_t	*descrp;
12813939Ssethg 	caddr_t		baseaddr;
12823939Ssethg 	uint16_t	idx = SELTOIDX(rp->r_cs);
12833939Ssethg 
12843939Ssethg 	ASSERT(rp->r_cs <= 0xFFFF);
12853939Ssethg 	ASSERT(MUTEX_HELD(&p->p_ldtlock));
12863939Ssethg 
12873939Ssethg 	if (SELISLDT(rp->r_cs)) {
12883939Ssethg 		/*
12893939Ssethg 		 * Currently 64 bit processes cannot have private LDTs.
12903939Ssethg 		 */
12913939Ssethg 		ASSERT(p->p_model != DATAMODEL_LP64);
12923939Ssethg 
12933939Ssethg 		if (p->p_ldt == NULL)
12943939Ssethg 			return (-1);
12953939Ssethg 
12963939Ssethg 		descrp = &p->p_ldt[idx];
12973939Ssethg 		baseaddr = (caddr_t)(uintptr_t)USEGD_GETBASE(descrp);
12983939Ssethg 
12993939Ssethg 		/*
13003939Ssethg 		 * Calculate the linear address (wraparound is not only ok,
13013939Ssethg 		 * it's expected behavior).  The cast to uint32_t is because
13023939Ssethg 		 * LDT selectors are only allowed in 32-bit processes.
13033939Ssethg 		 */
13043939Ssethg 		*linearp = (caddr_t)(uintptr_t)(uint32_t)((uintptr_t)baseaddr +
13053939Ssethg 		    rp->r_pc);
13063939Ssethg 	} else {
13073939Ssethg #ifdef DEBUG
13083939Ssethg 		descrp = &CPU->cpu_gdt[idx];
13093939Ssethg 		baseaddr = (caddr_t)(uintptr_t)USEGD_GETBASE(descrp);
13103939Ssethg 		/* GDT-based descriptors' base addresses should always be 0 */
13113939Ssethg 		ASSERT(baseaddr == 0);
13123939Ssethg #endif
13133939Ssethg 		*linearp = (caddr_t)(uintptr_t)rp->r_pc;
13143939Ssethg 	}
13153939Ssethg 
13163939Ssethg 	return (0);
13173939Ssethg }
13183939Ssethg 
13193939Ssethg /*
13203939Ssethg  * The implementation of dtrace_linear_pc is similar to the that of
13213939Ssethg  * linear_pc, above, but here we acquire p_ldtlock before accessing
13223939Ssethg  * p_ldt.  This implementation is used by the pid provider; we prefix
13233939Ssethg  * it with "dtrace_" to avoid inducing spurious tracing events.
13243939Ssethg  */
13253939Ssethg int
13263939Ssethg dtrace_linear_pc(struct regs *rp, proc_t *p, caddr_t *linearp)
13273939Ssethg {
13283939Ssethg 	user_desc_t	*descrp;
13293939Ssethg 	caddr_t		baseaddr;
13303939Ssethg 	uint16_t	idx = SELTOIDX(rp->r_cs);
13313939Ssethg 
13323939Ssethg 	ASSERT(rp->r_cs <= 0xFFFF);
13333939Ssethg 
13343939Ssethg 	if (SELISLDT(rp->r_cs)) {
13353939Ssethg 		/*
13363939Ssethg 		 * Currently 64 bit processes cannot have private LDTs.
13373939Ssethg 		 */
13383939Ssethg 		ASSERT(p->p_model != DATAMODEL_LP64);
13393939Ssethg 
13403939Ssethg 		mutex_enter(&p->p_ldtlock);
13413939Ssethg 		if (p->p_ldt == NULL) {
13423939Ssethg 			mutex_exit(&p->p_ldtlock);
13433939Ssethg 			return (-1);
13443939Ssethg 		}
13453939Ssethg 		descrp = &p->p_ldt[idx];
13463939Ssethg 		baseaddr = (caddr_t)(uintptr_t)USEGD_GETBASE(descrp);
13473939Ssethg 		mutex_exit(&p->p_ldtlock);
13483939Ssethg 
13493939Ssethg 		/*
13503939Ssethg 		 * Calculate the linear address (wraparound is not only ok,
13513939Ssethg 		 * it's expected behavior).  The cast to uint32_t is because
13523939Ssethg 		 * LDT selectors are only allowed in 32-bit processes.
13533939Ssethg 		 */
13543939Ssethg 		*linearp = (caddr_t)(uintptr_t)(uint32_t)((uintptr_t)baseaddr +
13553939Ssethg 		    rp->r_pc);
13563939Ssethg 	} else {
13573939Ssethg #ifdef DEBUG
13583939Ssethg 		descrp = &CPU->cpu_gdt[idx];
13593939Ssethg 		baseaddr = (caddr_t)(uintptr_t)USEGD_GETBASE(descrp);
13603939Ssethg 		/* GDT-based descriptors' base addresses should always be 0 */
13613939Ssethg 		ASSERT(baseaddr == 0);
13623939Ssethg #endif
13633939Ssethg 		*linearp = (caddr_t)(uintptr_t)rp->r_pc;
13643939Ssethg 	}
13653939Ssethg 
13663939Ssethg 	return (0);
13673939Ssethg }
1368