xref: /titanic_53/usr/src/uts/i86pc/os/machdep.c (revision 843e19887f64dde75055cf8842fc4db2171eff45)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5fea9cb91Slq150181  * Common Development and Distribution License (the "License").
6fea9cb91Slq150181  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21fea9cb91Slq150181 
227c478bd9Sstevel@tonic-gate /*
23ae115bc7Smrj  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
317c478bd9Sstevel@tonic-gate #include <sys/param.h>
32ddece0baSsethg #include <sys/segments.h>
337c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
347c478bd9Sstevel@tonic-gate #include <sys/signal.h>
357c478bd9Sstevel@tonic-gate #include <sys/systm.h>
367c478bd9Sstevel@tonic-gate #include <sys/user.h>
377c478bd9Sstevel@tonic-gate #include <sys/mman.h>
387c478bd9Sstevel@tonic-gate #include <sys/vm.h>
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #include <sys/disp.h>
417c478bd9Sstevel@tonic-gate #include <sys/class.h>
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #include <sys/proc.h>
447c478bd9Sstevel@tonic-gate #include <sys/buf.h>
457c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate #include <sys/reboot.h>
487c478bd9Sstevel@tonic-gate #include <sys/uadmin.h>
497c478bd9Sstevel@tonic-gate #include <sys/callb.h>
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate #include <sys/cred.h>
527c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
537c478bd9Sstevel@tonic-gate #include <sys/file.h>
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate #include <sys/procfs.h>
567c478bd9Sstevel@tonic-gate #include <sys/acct.h>
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
597c478bd9Sstevel@tonic-gate #include <sys/dnlc.h>
607c478bd9Sstevel@tonic-gate #include <sys/var.h>
617c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
627c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
637c478bd9Sstevel@tonic-gate #include <sys/debug.h>
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate #include <sys/dumphdr.h>
667c478bd9Sstevel@tonic-gate #include <sys/bootconf.h>
677c478bd9Sstevel@tonic-gate #include <sys/varargs.h>
687c478bd9Sstevel@tonic-gate #include <sys/promif.h>
697c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate #include <sys/consdev.h>
727c478bd9Sstevel@tonic-gate #include <sys/frame.h>
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
757c478bd9Sstevel@tonic-gate #include <sys/ddidmareq.h>
767c478bd9Sstevel@tonic-gate #include <sys/psw.h>
777c478bd9Sstevel@tonic-gate #include <sys/regset.h>
787c478bd9Sstevel@tonic-gate #include <sys/privregs.h>
797c478bd9Sstevel@tonic-gate #include <sys/clock.h>
807c478bd9Sstevel@tonic-gate #include <sys/tss.h>
817c478bd9Sstevel@tonic-gate #include <sys/cpu.h>
827c478bd9Sstevel@tonic-gate #include <sys/stack.h>
837c478bd9Sstevel@tonic-gate #include <sys/trap.h>
847c478bd9Sstevel@tonic-gate #include <sys/pic.h>
857c478bd9Sstevel@tonic-gate #include <vm/hat.h>
867c478bd9Sstevel@tonic-gate #include <vm/anon.h>
877c478bd9Sstevel@tonic-gate #include <vm/as.h>
887c478bd9Sstevel@tonic-gate #include <vm/page.h>
897c478bd9Sstevel@tonic-gate #include <vm/seg.h>
907c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h>
917c478bd9Sstevel@tonic-gate #include <vm/seg_map.h>
927c478bd9Sstevel@tonic-gate #include <vm/seg_vn.h>
937c478bd9Sstevel@tonic-gate #include <vm/seg_kp.h>
947c478bd9Sstevel@tonic-gate #include <vm/hat_i86.h>
957c478bd9Sstevel@tonic-gate #include <sys/swap.h>
967c478bd9Sstevel@tonic-gate #include <sys/thread.h>
977c478bd9Sstevel@tonic-gate #include <sys/sysconf.h>
987c478bd9Sstevel@tonic-gate #include <sys/vm_machparam.h>
997c478bd9Sstevel@tonic-gate #include <sys/archsystm.h>
1007c478bd9Sstevel@tonic-gate #include <sys/machsystm.h>
1017c478bd9Sstevel@tonic-gate #include <sys/machlock.h>
1027c478bd9Sstevel@tonic-gate #include <sys/x_call.h>
1037c478bd9Sstevel@tonic-gate #include <sys/instance.h>
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate #include <sys/time.h>
1067c478bd9Sstevel@tonic-gate #include <sys/smp_impldefs.h>
1077c478bd9Sstevel@tonic-gate #include <sys/psm_types.h>
1087c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
1097c478bd9Sstevel@tonic-gate #include <sys/panic.h>
1107c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
1117c478bd9Sstevel@tonic-gate #include <sys/dtrace.h>
1127c478bd9Sstevel@tonic-gate #include <sys/bl.h>
1137c478bd9Sstevel@tonic-gate #include <sys/nvpair.h>
1147c478bd9Sstevel@tonic-gate #include <sys/x86_archext.h>
1157c478bd9Sstevel@tonic-gate #include <sys/pool_pset.h>
1167c478bd9Sstevel@tonic-gate #include <sys/autoconf.h>
117ae115bc7Smrj #include <sys/mem.h>
118ae115bc7Smrj #include <sys/dumphdr.h>
119ae115bc7Smrj #include <sys/compress.h>
120*843e1988Sjohnlev #if defined(__xpv)
121*843e1988Sjohnlev #include <sys/hypervisor.h>
122*843e1988Sjohnlev #include <sys/xpv_panic.h>
123*843e1988Sjohnlev #endif
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate #ifdef	TRAPTRACE
1267c478bd9Sstevel@tonic-gate #include <sys/traptrace.h>
1277c478bd9Sstevel@tonic-gate #endif	/* TRAPTRACE */
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate #ifdef C2_AUDIT
1307c478bd9Sstevel@tonic-gate extern void audit_enterprom(int);
1317c478bd9Sstevel@tonic-gate extern void audit_exitprom(int);
1327c478bd9Sstevel@tonic-gate #endif
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate /*
1357c478bd9Sstevel@tonic-gate  * The panicbuf array is used to record messages and state:
1367c478bd9Sstevel@tonic-gate  */
1377c478bd9Sstevel@tonic-gate char panicbuf[PANICBUFSIZE];
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate /*
1407c478bd9Sstevel@tonic-gate  * maxphys - used during physio
1417c478bd9Sstevel@tonic-gate  * klustsize - used for klustering by swapfs and specfs
1427c478bd9Sstevel@tonic-gate  */
1437c478bd9Sstevel@tonic-gate int maxphys = 56 * 1024;    /* XXX See vm_subr.c - max b_count in physio */
1447c478bd9Sstevel@tonic-gate int klustsize = 56 * 1024;
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate caddr_t	p0_va;		/* Virtual address for accessing physical page 0 */
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate /*
1497c478bd9Sstevel@tonic-gate  * defined here, though unused on x86,
1507c478bd9Sstevel@tonic-gate  * to make kstat_fr.c happy.
1517c478bd9Sstevel@tonic-gate  */
1527c478bd9Sstevel@tonic-gate int vac;
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate void stop_other_cpus();
1557c478bd9Sstevel@tonic-gate void debug_enter(char *);
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate extern void pm_cfb_check_and_powerup(void);
1587c478bd9Sstevel@tonic-gate extern void pm_cfb_rele(void);
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate /*
1617c478bd9Sstevel@tonic-gate  * Machine dependent code to reboot.
1627c478bd9Sstevel@tonic-gate  * "mdep" is interpreted as a character pointer; if non-null, it is a pointer
1637c478bd9Sstevel@tonic-gate  * to a string to be used as the argument string when rebooting.
164edc40228Sachartre  *
165edc40228Sachartre  * "invoke_cb" is a boolean. It is set to true when mdboot() can safely
166edc40228Sachartre  * invoke CB_CL_MDBOOT callbacks before shutting the system down, i.e. when
167edc40228Sachartre  * we are in a normal shutdown sequence (interrupts are not blocked, the
168edc40228Sachartre  * system is not panic'ing or being suspended).
1697c478bd9Sstevel@tonic-gate  */
1707c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1717c478bd9Sstevel@tonic-gate void
172edc40228Sachartre mdboot(int cmd, int fcn, char *mdep, boolean_t invoke_cb)
1737c478bd9Sstevel@tonic-gate {
174*843e1988Sjohnlev #ifndef __xpv
1757c478bd9Sstevel@tonic-gate 	extern void mtrr_resync(void);
176*843e1988Sjohnlev #endif
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	if (!panicstr) {
1797c478bd9Sstevel@tonic-gate 		kpreempt_disable();
180d90554ebSdmick 		affinity_set(CPU_CURRENT);
1817c478bd9Sstevel@tonic-gate 	}
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	/*
1847c478bd9Sstevel@tonic-gate 	 * XXX - rconsvp is set to NULL to ensure that output messages
1857c478bd9Sstevel@tonic-gate 	 * are sent to the underlying "hardware" device using the
1867c478bd9Sstevel@tonic-gate 	 * monitor's printf routine since we are in the process of
1877c478bd9Sstevel@tonic-gate 	 * either rebooting or halting the machine.
1887c478bd9Sstevel@tonic-gate 	 */
1897c478bd9Sstevel@tonic-gate 	rconsvp = NULL;
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	/*
1927c478bd9Sstevel@tonic-gate 	 * Print the reboot message now, before pausing other cpus.
1937c478bd9Sstevel@tonic-gate 	 * There is a race condition in the printing support that
1947c478bd9Sstevel@tonic-gate 	 * can deadlock multiprocessor machines.
1957c478bd9Sstevel@tonic-gate 	 */
1967c478bd9Sstevel@tonic-gate 	if (!(fcn == AD_HALT || fcn == AD_POWEROFF))
1977c478bd9Sstevel@tonic-gate 		prom_printf("rebooting...\n");
1987c478bd9Sstevel@tonic-gate 
199*843e1988Sjohnlev 	if (IN_XPV_PANIC())
200*843e1988Sjohnlev 		reset();
201*843e1988Sjohnlev 
2027c478bd9Sstevel@tonic-gate 	/*
2037c478bd9Sstevel@tonic-gate 	 * We can't bring up the console from above lock level, so do it now
2047c478bd9Sstevel@tonic-gate 	 */
2057c478bd9Sstevel@tonic-gate 	pm_cfb_check_and_powerup();
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	/* make sure there are no more changes to the device tree */
2087c478bd9Sstevel@tonic-gate 	devtree_freeze();
2097c478bd9Sstevel@tonic-gate 
210edc40228Sachartre 	if (invoke_cb)
211edc40228Sachartre 		(void) callb_execute_class(CB_CL_MDBOOT, NULL);
212edc40228Sachartre 
2138b464eb8Smec 	/*
2148b464eb8Smec 	 * Clear any unresolved UEs from memory.
2158b464eb8Smec 	 */
2168b464eb8Smec 	page_retire_mdboot();
217db874c57Selowe 
218*843e1988Sjohnlev #if defined(__xpv)
219*843e1988Sjohnlev 	/*
220*843e1988Sjohnlev 	 * XXPV	Should probably think some more about how we deal
221*843e1988Sjohnlev 	 *	with panicing before it's really safe to panic.
222*843e1988Sjohnlev 	 *	On hypervisors, we reboot very quickly..  Perhaps panic
223*843e1988Sjohnlev 	 *	should only attempt to recover by rebooting if,
224*843e1988Sjohnlev 	 *	say, we were able to mount the root filesystem,
225*843e1988Sjohnlev 	 *	or if we successfully launched init(1m).
226*843e1988Sjohnlev 	 */
227*843e1988Sjohnlev 	if (panicstr && proc_init == NULL)
228*843e1988Sjohnlev 		(void) HYPERVISOR_shutdown(SHUTDOWN_poweroff);
229*843e1988Sjohnlev #endif
230*843e1988Sjohnlev 
2317c478bd9Sstevel@tonic-gate 	/*
2327c478bd9Sstevel@tonic-gate 	 * stop other cpus and raise our priority.  since there is only
2337c478bd9Sstevel@tonic-gate 	 * one active cpu after this, and our priority will be too high
2347c478bd9Sstevel@tonic-gate 	 * for us to be preempted, we're essentially single threaded
2357c478bd9Sstevel@tonic-gate 	 * from here on out.
2367c478bd9Sstevel@tonic-gate 	 */
2377c478bd9Sstevel@tonic-gate 	(void) spl6();
2387c478bd9Sstevel@tonic-gate 	if (!panicstr) {
2397c478bd9Sstevel@tonic-gate 		mutex_enter(&cpu_lock);
2407c478bd9Sstevel@tonic-gate 		pause_cpus(NULL);
2417c478bd9Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
2427c478bd9Sstevel@tonic-gate 	}
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	/*
2457c478bd9Sstevel@tonic-gate 	 * try and reset leaf devices.  reset_leaves() should only
2467c478bd9Sstevel@tonic-gate 	 * be called when there are no other threads that could be
2477c478bd9Sstevel@tonic-gate 	 * accessing devices
2487c478bd9Sstevel@tonic-gate 	 */
2497c478bd9Sstevel@tonic-gate 	reset_leaves();
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 	(void) spl8();
2527c478bd9Sstevel@tonic-gate 	(*psm_shutdownf)(cmd, fcn);
2537c478bd9Sstevel@tonic-gate 
254*843e1988Sjohnlev #ifndef __xpv
2557c478bd9Sstevel@tonic-gate 	mtrr_resync();
256*843e1988Sjohnlev #endif
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 	if (fcn == AD_HALT || fcn == AD_POWEROFF)
2597c478bd9Sstevel@tonic-gate 		halt((char *)NULL);
2607c478bd9Sstevel@tonic-gate 	else
2617c478bd9Sstevel@tonic-gate 		prom_reboot("");
2627c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
2637c478bd9Sstevel@tonic-gate }
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate /* mdpreboot - may be called prior to mdboot while root fs still mounted */
2667c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2677c478bd9Sstevel@tonic-gate void
2687c478bd9Sstevel@tonic-gate mdpreboot(int cmd, int fcn, char *mdep)
2697c478bd9Sstevel@tonic-gate {
2707c478bd9Sstevel@tonic-gate 	(*psm_preshutdownf)(cmd, fcn);
2717c478bd9Sstevel@tonic-gate }
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate void
2747c478bd9Sstevel@tonic-gate idle_other_cpus()
2757c478bd9Sstevel@tonic-gate {
2767c478bd9Sstevel@tonic-gate 	int cpuid = CPU->cpu_id;
2777c478bd9Sstevel@tonic-gate 	cpuset_t xcset;
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 	ASSERT(cpuid < NCPU);
2807c478bd9Sstevel@tonic-gate 	CPUSET_ALL_BUT(xcset, cpuid);
2817c478bd9Sstevel@tonic-gate 	xc_capture_cpus(xcset);
2827c478bd9Sstevel@tonic-gate }
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate void
2857c478bd9Sstevel@tonic-gate resume_other_cpus()
2867c478bd9Sstevel@tonic-gate {
2877c478bd9Sstevel@tonic-gate 	ASSERT(CPU->cpu_id < NCPU);
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 	xc_release_cpus();
2907c478bd9Sstevel@tonic-gate }
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate void
2937c478bd9Sstevel@tonic-gate stop_other_cpus()
2947c478bd9Sstevel@tonic-gate {
2957c478bd9Sstevel@tonic-gate 	int cpuid = CPU->cpu_id;
2967c478bd9Sstevel@tonic-gate 	cpuset_t xcset;
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	ASSERT(cpuid < NCPU);
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 	/*
301ae115bc7Smrj 	 * xc_trycall will attempt to make all other CPUs execute mach_cpu_halt,
3027c478bd9Sstevel@tonic-gate 	 * and will return immediately regardless of whether or not it was
3037c478bd9Sstevel@tonic-gate 	 * able to make them do it.
3047c478bd9Sstevel@tonic-gate 	 */
3057c478bd9Sstevel@tonic-gate 	CPUSET_ALL_BUT(xcset, cpuid);
306ae115bc7Smrj 	xc_trycall(NULL, NULL, NULL, xcset, (int (*)())mach_cpu_halt);
3077c478bd9Sstevel@tonic-gate }
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate /*
3107c478bd9Sstevel@tonic-gate  *	Machine dependent abort sequence handling
3117c478bd9Sstevel@tonic-gate  */
3127c478bd9Sstevel@tonic-gate void
3137c478bd9Sstevel@tonic-gate abort_sequence_enter(char *msg)
3147c478bd9Sstevel@tonic-gate {
3157c478bd9Sstevel@tonic-gate 	if (abort_enable == 0) {
3167c478bd9Sstevel@tonic-gate #ifdef C2_AUDIT
3177c478bd9Sstevel@tonic-gate 		if (audit_active)
3187c478bd9Sstevel@tonic-gate 			audit_enterprom(0);
3197c478bd9Sstevel@tonic-gate #endif /* C2_AUDIT */
3207c478bd9Sstevel@tonic-gate 		return;
3217c478bd9Sstevel@tonic-gate 	}
3227c478bd9Sstevel@tonic-gate #ifdef C2_AUDIT
3237c478bd9Sstevel@tonic-gate 	if (audit_active)
3247c478bd9Sstevel@tonic-gate 		audit_enterprom(1);
3257c478bd9Sstevel@tonic-gate #endif /* C2_AUDIT */
3267c478bd9Sstevel@tonic-gate 	debug_enter(msg);
3277c478bd9Sstevel@tonic-gate #ifdef C2_AUDIT
3287c478bd9Sstevel@tonic-gate 	if (audit_active)
3297c478bd9Sstevel@tonic-gate 		audit_exitprom(1);
3307c478bd9Sstevel@tonic-gate #endif /* C2_AUDIT */
3317c478bd9Sstevel@tonic-gate }
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate /*
3347c478bd9Sstevel@tonic-gate  * Enter debugger.  Called when the user types ctrl-alt-d or whenever
3357c478bd9Sstevel@tonic-gate  * code wants to enter the debugger and possibly resume later.
3367c478bd9Sstevel@tonic-gate  */
3377c478bd9Sstevel@tonic-gate void
3387c478bd9Sstevel@tonic-gate debug_enter(
3397c478bd9Sstevel@tonic-gate 	char	*msg)		/* message to print, possibly NULL */
3407c478bd9Sstevel@tonic-gate {
3417c478bd9Sstevel@tonic-gate 	if (dtrace_debugger_init != NULL)
3427c478bd9Sstevel@tonic-gate 		(*dtrace_debugger_init)();
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 	if (msg)
3457c478bd9Sstevel@tonic-gate 		prom_printf("%s\n", msg);
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 	if (boothowto & RB_DEBUG)
348ae115bc7Smrj 		kmdb_enter();
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 	if (dtrace_debugger_fini != NULL)
3517c478bd9Sstevel@tonic-gate 		(*dtrace_debugger_fini)();
3527c478bd9Sstevel@tonic-gate }
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate void
3557c478bd9Sstevel@tonic-gate reset(void)
3567c478bd9Sstevel@tonic-gate {
357*843e1988Sjohnlev #if !defined(__xpv)
3587c478bd9Sstevel@tonic-gate 	ushort_t *bios_memchk;
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 	/*
3617c478bd9Sstevel@tonic-gate 	 * Can't use psm_map_phys before the hat is initialized.
3627c478bd9Sstevel@tonic-gate 	 */
3637c478bd9Sstevel@tonic-gate 	if (khat_running) {
3647c478bd9Sstevel@tonic-gate 		bios_memchk = (ushort_t *)psm_map_phys(0x472,
3657c478bd9Sstevel@tonic-gate 		    sizeof (ushort_t), PROT_READ | PROT_WRITE);
3667c478bd9Sstevel@tonic-gate 		if (bios_memchk)
3677c478bd9Sstevel@tonic-gate 			*bios_memchk = 0x1234;	/* bios memory check disable */
3687c478bd9Sstevel@tonic-gate 	}
3697c478bd9Sstevel@tonic-gate 
370f2be5148Sszhou 	if (ddi_prop_exists(DDI_DEV_T_ANY, ddi_root_node(), 0, "efi-systab"))
371f2be5148Sszhou 		efi_reset();
3727c478bd9Sstevel@tonic-gate 	pc_reset();
373*843e1988Sjohnlev #else
374*843e1988Sjohnlev 	if (IN_XPV_PANIC())
375*843e1988Sjohnlev 		pc_reset();
376*843e1988Sjohnlev 	(void) HYPERVISOR_shutdown(SHUTDOWN_reboot);
377*843e1988Sjohnlev 	panic("HYPERVISOR_shutdown() failed");
378*843e1988Sjohnlev #endif
3797c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
3807c478bd9Sstevel@tonic-gate }
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate /*
3837c478bd9Sstevel@tonic-gate  * Halt the machine and return to the monitor
3847c478bd9Sstevel@tonic-gate  */
3857c478bd9Sstevel@tonic-gate void
3867c478bd9Sstevel@tonic-gate halt(char *s)
3877c478bd9Sstevel@tonic-gate {
3887c478bd9Sstevel@tonic-gate 	stop_other_cpus();	/* send stop signal to other CPUs */
3897c478bd9Sstevel@tonic-gate 	if (s)
3907c478bd9Sstevel@tonic-gate 		prom_printf("(%s) \n", s);
3917c478bd9Sstevel@tonic-gate 	prom_exit_to_mon();
3927c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
3937c478bd9Sstevel@tonic-gate }
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate /*
3967c478bd9Sstevel@tonic-gate  * Initiate interrupt redistribution.
3977c478bd9Sstevel@tonic-gate  */
3987c478bd9Sstevel@tonic-gate void
3997c478bd9Sstevel@tonic-gate i_ddi_intr_redist_all_cpus()
4007c478bd9Sstevel@tonic-gate {
4017c478bd9Sstevel@tonic-gate }
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate /*
4047c478bd9Sstevel@tonic-gate  * XXX These probably ought to live somewhere else
4057c478bd9Sstevel@tonic-gate  * XXX They are called from mem.c
4067c478bd9Sstevel@tonic-gate  */
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate /*
4097c478bd9Sstevel@tonic-gate  * Convert page frame number to an OBMEM page frame number
4107c478bd9Sstevel@tonic-gate  * (i.e. put in the type bits -- zero for this implementation)
4117c478bd9Sstevel@tonic-gate  */
4127c478bd9Sstevel@tonic-gate pfn_t
4137c478bd9Sstevel@tonic-gate impl_obmem_pfnum(pfn_t pf)
4147c478bd9Sstevel@tonic-gate {
4157c478bd9Sstevel@tonic-gate 	return (pf);
4167c478bd9Sstevel@tonic-gate }
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate #ifdef	NM_DEBUG
4197c478bd9Sstevel@tonic-gate int nmi_test = 0;	/* checked in intentry.s during clock int */
4207c478bd9Sstevel@tonic-gate int nmtest = -1;
4217c478bd9Sstevel@tonic-gate nmfunc1(arg, rp)
4227c478bd9Sstevel@tonic-gate int	arg;
4237c478bd9Sstevel@tonic-gate struct regs *rp;
4247c478bd9Sstevel@tonic-gate {
4257c478bd9Sstevel@tonic-gate 	printf("nmi called with arg = %x, regs = %x\n", arg, rp);
4267c478bd9Sstevel@tonic-gate 	nmtest += 50;
4277c478bd9Sstevel@tonic-gate 	if (arg == nmtest) {
4287c478bd9Sstevel@tonic-gate 		printf("ip = %x\n", rp->r_pc);
4297c478bd9Sstevel@tonic-gate 		return (1);
4307c478bd9Sstevel@tonic-gate 	}
4317c478bd9Sstevel@tonic-gate 	return (0);
4327c478bd9Sstevel@tonic-gate }
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate #endif
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate #include <sys/bootsvcs.h>
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate /* Hacked up initialization for initial kernel check out is HERE. */
4397c478bd9Sstevel@tonic-gate /* The basic steps are: */
4407c478bd9Sstevel@tonic-gate /*	kernel bootfuncs definition/initialization for KADB */
4417c478bd9Sstevel@tonic-gate /*	kadb bootfuncs pointer initialization */
4427c478bd9Sstevel@tonic-gate /*	putchar/getchar (interrupts disabled) */
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate /* kadb bootfuncs pointer initialization */
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate int
4477c478bd9Sstevel@tonic-gate sysp_getchar()
4487c478bd9Sstevel@tonic-gate {
4497c478bd9Sstevel@tonic-gate 	int i;
450ae115bc7Smrj 	ulong_t s;
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate 	if (cons_polledio == NULL) {
4537c478bd9Sstevel@tonic-gate 		/* Uh oh */
4547c478bd9Sstevel@tonic-gate 		prom_printf("getchar called with no console\n");
4557c478bd9Sstevel@tonic-gate 		for (;;)
4567c478bd9Sstevel@tonic-gate 			/* LOOP FOREVER */;
4577c478bd9Sstevel@tonic-gate 	}
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 	s = clear_int_flag();
4607c478bd9Sstevel@tonic-gate 	i = cons_polledio->cons_polledio_getchar(
4617c478bd9Sstevel@tonic-gate 	    cons_polledio->cons_polledio_argument);
4627c478bd9Sstevel@tonic-gate 	restore_int_flag(s);
4637c478bd9Sstevel@tonic-gate 	return (i);
4647c478bd9Sstevel@tonic-gate }
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate void
4677c478bd9Sstevel@tonic-gate sysp_putchar(int c)
4687c478bd9Sstevel@tonic-gate {
469ae115bc7Smrj 	ulong_t s;
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate 	/*
4727c478bd9Sstevel@tonic-gate 	 * We have no alternative but to drop the output on the floor.
4737c478bd9Sstevel@tonic-gate 	 */
474fea9cb91Slq150181 	if (cons_polledio == NULL ||
475fea9cb91Slq150181 	    cons_polledio->cons_polledio_putchar == NULL)
4767c478bd9Sstevel@tonic-gate 		return;
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate 	s = clear_int_flag();
4797c478bd9Sstevel@tonic-gate 	cons_polledio->cons_polledio_putchar(
4807c478bd9Sstevel@tonic-gate 	    cons_polledio->cons_polledio_argument, c);
4817c478bd9Sstevel@tonic-gate 	restore_int_flag(s);
4827c478bd9Sstevel@tonic-gate }
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate int
4857c478bd9Sstevel@tonic-gate sysp_ischar()
4867c478bd9Sstevel@tonic-gate {
4877c478bd9Sstevel@tonic-gate 	int i;
488ae115bc7Smrj 	ulong_t s;
4897c478bd9Sstevel@tonic-gate 
490fea9cb91Slq150181 	if (cons_polledio == NULL ||
491fea9cb91Slq150181 	    cons_polledio->cons_polledio_ischar == NULL)
4927c478bd9Sstevel@tonic-gate 		return (0);
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 	s = clear_int_flag();
4957c478bd9Sstevel@tonic-gate 	i = cons_polledio->cons_polledio_ischar(
4967c478bd9Sstevel@tonic-gate 	    cons_polledio->cons_polledio_argument);
4977c478bd9Sstevel@tonic-gate 	restore_int_flag(s);
4987c478bd9Sstevel@tonic-gate 	return (i);
4997c478bd9Sstevel@tonic-gate }
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate int
5027c478bd9Sstevel@tonic-gate goany(void)
5037c478bd9Sstevel@tonic-gate {
5047c478bd9Sstevel@tonic-gate 	prom_printf("Type any key to continue ");
5057c478bd9Sstevel@tonic-gate 	(void) prom_getchar();
5067c478bd9Sstevel@tonic-gate 	prom_printf("\n");
5077c478bd9Sstevel@tonic-gate 	return (1);
5087c478bd9Sstevel@tonic-gate }
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate static struct boot_syscalls kern_sysp = {
5117c478bd9Sstevel@tonic-gate 	sysp_getchar,	/*	unchar	(*getchar)();	7  */
5127c478bd9Sstevel@tonic-gate 	sysp_putchar,	/*	int	(*putchar)();	8  */
5137c478bd9Sstevel@tonic-gate 	sysp_ischar,	/*	int	(*ischar)();	9  */
5147c478bd9Sstevel@tonic-gate };
5157c478bd9Sstevel@tonic-gate 
516*843e1988Sjohnlev #if defined(__xpv)
517*843e1988Sjohnlev int using_kern_polledio;
518*843e1988Sjohnlev #endif
519*843e1988Sjohnlev 
5207c478bd9Sstevel@tonic-gate void
5217c478bd9Sstevel@tonic-gate kadb_uses_kernel()
5227c478bd9Sstevel@tonic-gate {
5237c478bd9Sstevel@tonic-gate 	/*
5247c478bd9Sstevel@tonic-gate 	 * This routine is now totally misnamed, since it does not in fact
5257c478bd9Sstevel@tonic-gate 	 * control kadb's I/O; it only controls the kernel's prom_* I/O.
5267c478bd9Sstevel@tonic-gate 	 */
5277c478bd9Sstevel@tonic-gate 	sysp = &kern_sysp;
528*843e1988Sjohnlev #if defined(__xpv)
529*843e1988Sjohnlev 	using_kern_polledio = 1;
530*843e1988Sjohnlev #endif
5317c478bd9Sstevel@tonic-gate }
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate /*
5347c478bd9Sstevel@tonic-gate  *	the interface to the outside world
5357c478bd9Sstevel@tonic-gate  */
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate /*
5387c478bd9Sstevel@tonic-gate  * poll_port -- wait for a register to achieve a
5397c478bd9Sstevel@tonic-gate  *		specific state.  Arguments are a mask of bits we care about,
5407c478bd9Sstevel@tonic-gate  *		and two sub-masks.  To return normally, all the bits in the
5417c478bd9Sstevel@tonic-gate  *		first sub-mask must be ON, all the bits in the second sub-
5427c478bd9Sstevel@tonic-gate  *		mask must be OFF.  If about seconds pass without the register
5437c478bd9Sstevel@tonic-gate  *		achieving the desired bit configuration, we return 1, else
5447c478bd9Sstevel@tonic-gate  *		0.
5457c478bd9Sstevel@tonic-gate  */
5467c478bd9Sstevel@tonic-gate int
5477c478bd9Sstevel@tonic-gate poll_port(ushort_t port, ushort_t mask, ushort_t onbits, ushort_t offbits)
5487c478bd9Sstevel@tonic-gate {
5497c478bd9Sstevel@tonic-gate 	int i;
5507c478bd9Sstevel@tonic-gate 	ushort_t maskval;
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 	for (i = 500000; i; i--) {
5537c478bd9Sstevel@tonic-gate 		maskval = inb(port) & mask;
5547c478bd9Sstevel@tonic-gate 		if (((maskval & onbits) == onbits) &&
5557c478bd9Sstevel@tonic-gate 		    ((maskval & offbits) == 0))
5567c478bd9Sstevel@tonic-gate 			return (0);
5577c478bd9Sstevel@tonic-gate 		drv_usecwait(10);
5587c478bd9Sstevel@tonic-gate 	}
5597c478bd9Sstevel@tonic-gate 	return (1);
5607c478bd9Sstevel@tonic-gate }
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate /*
5637c478bd9Sstevel@tonic-gate  * set_idle_cpu is called from idle() when a CPU becomes idle.
5647c478bd9Sstevel@tonic-gate  */
5657c478bd9Sstevel@tonic-gate /*LINTED: static unused */
5667c478bd9Sstevel@tonic-gate static uint_t last_idle_cpu;
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5697c478bd9Sstevel@tonic-gate void
5707c478bd9Sstevel@tonic-gate set_idle_cpu(int cpun)
5717c478bd9Sstevel@tonic-gate {
5727c478bd9Sstevel@tonic-gate 	last_idle_cpu = cpun;
5737c478bd9Sstevel@tonic-gate 	(*psm_set_idle_cpuf)(cpun);
5747c478bd9Sstevel@tonic-gate }
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate /*
5777c478bd9Sstevel@tonic-gate  * unset_idle_cpu is called from idle() when a CPU is no longer idle.
5787c478bd9Sstevel@tonic-gate  */
5797c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5807c478bd9Sstevel@tonic-gate void
5817c478bd9Sstevel@tonic-gate unset_idle_cpu(int cpun)
5827c478bd9Sstevel@tonic-gate {
5837c478bd9Sstevel@tonic-gate 	(*psm_unset_idle_cpuf)(cpun);
5847c478bd9Sstevel@tonic-gate }
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate /*
5877c478bd9Sstevel@tonic-gate  * This routine is almost correct now, but not quite.  It still needs the
5887c478bd9Sstevel@tonic-gate  * equivalent concept of "hres_last_tick", just like on the sparc side.
5897c478bd9Sstevel@tonic-gate  * The idea is to take a snapshot of the hi-res timer while doing the
5907c478bd9Sstevel@tonic-gate  * hrestime_adj updates under hres_lock in locore, so that the small
5917c478bd9Sstevel@tonic-gate  * interval between interrupt assertion and interrupt processing is
5927c478bd9Sstevel@tonic-gate  * accounted for correctly.  Once we have this, the code below should
5937c478bd9Sstevel@tonic-gate  * be modified to subtract off hres_last_tick rather than hrtime_base.
5947c478bd9Sstevel@tonic-gate  *
5957c478bd9Sstevel@tonic-gate  * I'd have done this myself, but I don't have source to all of the
5967c478bd9Sstevel@tonic-gate  * vendor-specific hi-res timer routines (grrr...).  The generic hook I
5977c478bd9Sstevel@tonic-gate  * need is something like "gethrtime_unlocked()", which would be just like
5987c478bd9Sstevel@tonic-gate  * gethrtime() but would assume that you're already holding CLOCK_LOCK().
5997c478bd9Sstevel@tonic-gate  * This is what the GET_HRTIME() macro is for on sparc (although it also
6007c478bd9Sstevel@tonic-gate  * serves the function of making time available without a function call
6017c478bd9Sstevel@tonic-gate  * so you don't take a register window overflow while traps are disabled).
6027c478bd9Sstevel@tonic-gate  */
6037c478bd9Sstevel@tonic-gate void
6047c478bd9Sstevel@tonic-gate pc_gethrestime(timestruc_t *tp)
6057c478bd9Sstevel@tonic-gate {
6067c478bd9Sstevel@tonic-gate 	int lock_prev;
6077c478bd9Sstevel@tonic-gate 	timestruc_t now;
6087c478bd9Sstevel@tonic-gate 	int nslt;		/* nsec since last tick */
6097c478bd9Sstevel@tonic-gate 	int adj;		/* amount of adjustment to apply */
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate loop:
6127c478bd9Sstevel@tonic-gate 	lock_prev = hres_lock;
6137c478bd9Sstevel@tonic-gate 	now = hrestime;
6147c478bd9Sstevel@tonic-gate 	nslt = (int)(gethrtime() - hres_last_tick);
6157c478bd9Sstevel@tonic-gate 	if (nslt < 0) {
6167c478bd9Sstevel@tonic-gate 		/*
6177c478bd9Sstevel@tonic-gate 		 * nslt < 0 means a tick came between sampling
6187c478bd9Sstevel@tonic-gate 		 * gethrtime() and hres_last_tick; restart the loop
6197c478bd9Sstevel@tonic-gate 		 */
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 		goto loop;
6227c478bd9Sstevel@tonic-gate 	}
6237c478bd9Sstevel@tonic-gate 	now.tv_nsec += nslt;
6247c478bd9Sstevel@tonic-gate 	if (hrestime_adj != 0) {
6257c478bd9Sstevel@tonic-gate 		if (hrestime_adj > 0) {
6267c478bd9Sstevel@tonic-gate 			adj = (nslt >> ADJ_SHIFT);
6277c478bd9Sstevel@tonic-gate 			if (adj > hrestime_adj)
6287c478bd9Sstevel@tonic-gate 				adj = (int)hrestime_adj;
6297c478bd9Sstevel@tonic-gate 		} else {
6307c478bd9Sstevel@tonic-gate 			adj = -(nslt >> ADJ_SHIFT);
6317c478bd9Sstevel@tonic-gate 			if (adj < hrestime_adj)
6327c478bd9Sstevel@tonic-gate 				adj = (int)hrestime_adj;
6337c478bd9Sstevel@tonic-gate 		}
6347c478bd9Sstevel@tonic-gate 		now.tv_nsec += adj;
6357c478bd9Sstevel@tonic-gate 	}
6367c478bd9Sstevel@tonic-gate 	while ((unsigned long)now.tv_nsec >= NANOSEC) {
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 		/*
6397c478bd9Sstevel@tonic-gate 		 * We might have a large adjustment or have been in the
6407c478bd9Sstevel@tonic-gate 		 * debugger for a long time; take care of (at most) four
6417c478bd9Sstevel@tonic-gate 		 * of those missed seconds (tv_nsec is 32 bits, so
6427c478bd9Sstevel@tonic-gate 		 * anything >4s will be wrapping around).  However,
6437c478bd9Sstevel@tonic-gate 		 * anything more than 2 seconds out of sync will trigger
6447c478bd9Sstevel@tonic-gate 		 * timedelta from clock() to go correct the time anyway,
6457c478bd9Sstevel@tonic-gate 		 * so do what we can, and let the big crowbar do the
6467c478bd9Sstevel@tonic-gate 		 * rest.  A similar correction while loop exists inside
6477c478bd9Sstevel@tonic-gate 		 * hres_tick(); in all cases we'd like tv_nsec to
6487c478bd9Sstevel@tonic-gate 		 * satisfy 0 <= tv_nsec < NANOSEC to avoid confusing
6497c478bd9Sstevel@tonic-gate 		 * user processes, but if tv_sec's a little behind for a
6507c478bd9Sstevel@tonic-gate 		 * little while, that's OK; time still monotonically
6517c478bd9Sstevel@tonic-gate 		 * increases.
6527c478bd9Sstevel@tonic-gate 		 */
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate 		now.tv_nsec -= NANOSEC;
6557c478bd9Sstevel@tonic-gate 		now.tv_sec++;
6567c478bd9Sstevel@tonic-gate 	}
6577c478bd9Sstevel@tonic-gate 	if ((hres_lock & ~1) != lock_prev)
6587c478bd9Sstevel@tonic-gate 		goto loop;
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 	*tp = now;
6617c478bd9Sstevel@tonic-gate }
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate void
6647c478bd9Sstevel@tonic-gate gethrestime_lasttick(timespec_t *tp)
6657c478bd9Sstevel@tonic-gate {
6667c478bd9Sstevel@tonic-gate 	int s;
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate 	s = hr_clock_lock();
6697c478bd9Sstevel@tonic-gate 	*tp = hrestime;
6707c478bd9Sstevel@tonic-gate 	hr_clock_unlock(s);
6717c478bd9Sstevel@tonic-gate }
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate time_t
6747c478bd9Sstevel@tonic-gate gethrestime_sec(void)
6757c478bd9Sstevel@tonic-gate {
6767c478bd9Sstevel@tonic-gate 	timestruc_t now;
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate 	gethrestime(&now);
6797c478bd9Sstevel@tonic-gate 	return (now.tv_sec);
6807c478bd9Sstevel@tonic-gate }
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate /*
6837c478bd9Sstevel@tonic-gate  * Initialize a kernel thread's stack
6847c478bd9Sstevel@tonic-gate  */
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate caddr_t
6877c478bd9Sstevel@tonic-gate thread_stk_init(caddr_t stk)
6887c478bd9Sstevel@tonic-gate {
6897c478bd9Sstevel@tonic-gate 	ASSERT(((uintptr_t)stk & (STACK_ALIGN - 1)) == 0);
6907c478bd9Sstevel@tonic-gate 	return (stk - SA(MINFRAME));
6917c478bd9Sstevel@tonic-gate }
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate /*
6947c478bd9Sstevel@tonic-gate  * Initialize lwp's kernel stack.
6957c478bd9Sstevel@tonic-gate  */
6967c478bd9Sstevel@tonic-gate 
6977c478bd9Sstevel@tonic-gate #ifdef TRAPTRACE
6987c478bd9Sstevel@tonic-gate /*
6997c478bd9Sstevel@tonic-gate  * There's a tricky interdependency here between use of sysenter and
7007c478bd9Sstevel@tonic-gate  * TRAPTRACE which needs recording to avoid future confusion (this is
7017c478bd9Sstevel@tonic-gate  * about the third time I've re-figured this out ..)
7027c478bd9Sstevel@tonic-gate  *
7037c478bd9Sstevel@tonic-gate  * Here's how debugging lcall works with TRAPTRACE.
7047c478bd9Sstevel@tonic-gate  *
7057c478bd9Sstevel@tonic-gate  * 1 We're in userland with a breakpoint on the lcall instruction.
7067c478bd9Sstevel@tonic-gate  * 2 We execute the instruction - the instruction pushes the userland
7077c478bd9Sstevel@tonic-gate  *   %ss, %esp, %efl, %cs, %eip on the stack and zips into the kernel
7087c478bd9Sstevel@tonic-gate  *   via the call gate.
7097c478bd9Sstevel@tonic-gate  * 3 The hardware raises a debug trap in kernel mode, the hardware
7107c478bd9Sstevel@tonic-gate  *   pushes %efl, %cs, %eip and gets to dbgtrap via the idt.
7117c478bd9Sstevel@tonic-gate  * 4 dbgtrap pushes the error code and trapno and calls cmntrap
7127c478bd9Sstevel@tonic-gate  * 5 cmntrap finishes building a trap frame
7137c478bd9Sstevel@tonic-gate  * 6 The TRACE_REGS macros in cmntrap copy a REGSIZE worth chunk
7147c478bd9Sstevel@tonic-gate  *   off the stack into the traptrace buffer.
7157c478bd9Sstevel@tonic-gate  *
7167c478bd9Sstevel@tonic-gate  * This means that the traptrace buffer contains the wrong values in
7177c478bd9Sstevel@tonic-gate  * %esp and %ss, but everything else in there is correct.
7187c478bd9Sstevel@tonic-gate  *
7197c478bd9Sstevel@tonic-gate  * Here's how debugging sysenter works with TRAPTRACE.
7207c478bd9Sstevel@tonic-gate  *
7217c478bd9Sstevel@tonic-gate  * a We're in userland with a breakpoint on the sysenter instruction.
7227c478bd9Sstevel@tonic-gate  * b We execute the instruction - the instruction pushes -nothing-
7237c478bd9Sstevel@tonic-gate  *   on the stack, but sets %cs, %eip, %ss, %esp to prearranged
7247c478bd9Sstevel@tonic-gate  *   values to take us to sys_sysenter, at the top of the lwp's
7257c478bd9Sstevel@tonic-gate  *   stack.
7267c478bd9Sstevel@tonic-gate  * c goto 3
7277c478bd9Sstevel@tonic-gate  *
7287c478bd9Sstevel@tonic-gate  * At this point, because we got into the kernel without the requisite
7297c478bd9Sstevel@tonic-gate  * five pushes on the stack, if we didn't make extra room, we'd
7307c478bd9Sstevel@tonic-gate  * end up with the TRACE_REGS macro fetching the saved %ss and %esp
7317c478bd9Sstevel@tonic-gate  * values from negative (unmapped) stack addresses -- which really bites.
7327c478bd9Sstevel@tonic-gate  * That's why we do the '-= 8' below.
7337c478bd9Sstevel@tonic-gate  *
7347c478bd9Sstevel@tonic-gate  * XXX	Note that reading "up" lwp0's stack works because t0 is declared
7357c478bd9Sstevel@tonic-gate  *	right next to t0stack in locore.s
7367c478bd9Sstevel@tonic-gate  */
7377c478bd9Sstevel@tonic-gate #endif
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate caddr_t
7407c478bd9Sstevel@tonic-gate lwp_stk_init(klwp_t *lwp, caddr_t stk)
7417c478bd9Sstevel@tonic-gate {
7427c478bd9Sstevel@tonic-gate 	caddr_t oldstk;
7437c478bd9Sstevel@tonic-gate 	struct pcb *pcb = &lwp->lwp_pcb;
7447c478bd9Sstevel@tonic-gate 
7457c478bd9Sstevel@tonic-gate 	oldstk = stk;
7467c478bd9Sstevel@tonic-gate 	stk -= SA(sizeof (struct regs) + SA(MINFRAME));
7477c478bd9Sstevel@tonic-gate #ifdef TRAPTRACE
7487c478bd9Sstevel@tonic-gate 	stk -= 2 * sizeof (greg_t); /* space for phony %ss:%sp (see above) */
7497c478bd9Sstevel@tonic-gate #endif
7507c478bd9Sstevel@tonic-gate 	stk = (caddr_t)((uintptr_t)stk & ~(STACK_ALIGN - 1ul));
7517c478bd9Sstevel@tonic-gate 	bzero(stk, oldstk - stk);
7527c478bd9Sstevel@tonic-gate 	lwp->lwp_regs = (void *)(stk + SA(MINFRAME));
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate 	/*
7557c478bd9Sstevel@tonic-gate 	 * Arrange that the virtualized %fs and %gs GDT descriptors
7567c478bd9Sstevel@tonic-gate 	 * have a well-defined initial state (present, ring 3
7577c478bd9Sstevel@tonic-gate 	 * and of type data).
7587c478bd9Sstevel@tonic-gate 	 */
7597c478bd9Sstevel@tonic-gate #if defined(__amd64)
7607c478bd9Sstevel@tonic-gate 	if (lwp_getdatamodel(lwp) == DATAMODEL_NATIVE)
7617c478bd9Sstevel@tonic-gate 		pcb->pcb_fsdesc = pcb->pcb_gsdesc = zero_udesc;
7627c478bd9Sstevel@tonic-gate 	else
7637c478bd9Sstevel@tonic-gate 		pcb->pcb_fsdesc = pcb->pcb_gsdesc = zero_u32desc;
7647c478bd9Sstevel@tonic-gate #elif defined(__i386)
7657c478bd9Sstevel@tonic-gate 	pcb->pcb_fsdesc = pcb->pcb_gsdesc = zero_udesc;
7667c478bd9Sstevel@tonic-gate #endif	/* __i386 */
7677c478bd9Sstevel@tonic-gate 	lwp_installctx(lwp);
7687c478bd9Sstevel@tonic-gate 	return (stk);
7697c478bd9Sstevel@tonic-gate }
7707c478bd9Sstevel@tonic-gate 
7717c478bd9Sstevel@tonic-gate /*ARGSUSED*/
7727c478bd9Sstevel@tonic-gate void
7737c478bd9Sstevel@tonic-gate lwp_stk_fini(klwp_t *lwp)
7747c478bd9Sstevel@tonic-gate {}
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate /*
777d90554ebSdmick  * If we're not the panic CPU, we wait in panic_idle for reboot.
7787c478bd9Sstevel@tonic-gate  */
7797c478bd9Sstevel@tonic-gate static void
7807c478bd9Sstevel@tonic-gate panic_idle(void)
7817c478bd9Sstevel@tonic-gate {
7827c478bd9Sstevel@tonic-gate 	splx(ipltospl(CLOCK_LEVEL));
7837c478bd9Sstevel@tonic-gate 	(void) setjmp(&curthread->t_pcb);
7847c478bd9Sstevel@tonic-gate 
785ae115bc7Smrj 	for (;;)
786ae115bc7Smrj 		;
7877c478bd9Sstevel@tonic-gate }
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate /*
7907c478bd9Sstevel@tonic-gate  * Stop the other CPUs by cross-calling them and forcing them to enter
7917c478bd9Sstevel@tonic-gate  * the panic_idle() loop above.
7927c478bd9Sstevel@tonic-gate  */
7937c478bd9Sstevel@tonic-gate /*ARGSUSED*/
7947c478bd9Sstevel@tonic-gate void
7957c478bd9Sstevel@tonic-gate panic_stopcpus(cpu_t *cp, kthread_t *t, int spl)
7967c478bd9Sstevel@tonic-gate {
7977c478bd9Sstevel@tonic-gate 	processorid_t i;
7987c478bd9Sstevel@tonic-gate 	cpuset_t xcset;
7997c478bd9Sstevel@tonic-gate 
800*843e1988Sjohnlev 	/*
801*843e1988Sjohnlev 	 * In the case of a Xen panic, the hypervisor has already stopped
802*843e1988Sjohnlev 	 * all of the CPUs.
803*843e1988Sjohnlev 	 */
804*843e1988Sjohnlev 	if (!IN_XPV_PANIC()) {
8057c478bd9Sstevel@tonic-gate 		(void) splzs();
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate 		CPUSET_ALL_BUT(xcset, cp->cpu_id);
8087c478bd9Sstevel@tonic-gate 		xc_trycall(NULL, NULL, NULL, xcset, (int (*)())panic_idle);
809*843e1988Sjohnlev 	}
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate 	for (i = 0; i < NCPU; i++) {
8127c478bd9Sstevel@tonic-gate 		if (i != cp->cpu_id && cpu[i] != NULL &&
8137c478bd9Sstevel@tonic-gate 		    (cpu[i]->cpu_flags & CPU_EXISTS))
8147c478bd9Sstevel@tonic-gate 			cpu[i]->cpu_flags |= CPU_QUIESCED;
8157c478bd9Sstevel@tonic-gate 	}
8167c478bd9Sstevel@tonic-gate }
8177c478bd9Sstevel@tonic-gate 
8187c478bd9Sstevel@tonic-gate /*
8197c478bd9Sstevel@tonic-gate  * Platform callback following each entry to panicsys().
8207c478bd9Sstevel@tonic-gate  */
8217c478bd9Sstevel@tonic-gate /*ARGSUSED*/
8227c478bd9Sstevel@tonic-gate void
8237c478bd9Sstevel@tonic-gate panic_enter_hw(int spl)
8247c478bd9Sstevel@tonic-gate {
8257c478bd9Sstevel@tonic-gate 	/* Nothing to do here */
8267c478bd9Sstevel@tonic-gate }
8277c478bd9Sstevel@tonic-gate 
8287c478bd9Sstevel@tonic-gate /*
8297c478bd9Sstevel@tonic-gate  * Platform-specific code to execute after panicstr is set: we invoke
8307c478bd9Sstevel@tonic-gate  * the PSM entry point to indicate that a panic has occurred.
8317c478bd9Sstevel@tonic-gate  */
8327c478bd9Sstevel@tonic-gate /*ARGSUSED*/
8337c478bd9Sstevel@tonic-gate void
8347c478bd9Sstevel@tonic-gate panic_quiesce_hw(panic_data_t *pdp)
8357c478bd9Sstevel@tonic-gate {
8367c478bd9Sstevel@tonic-gate 	psm_notifyf(PSM_PANIC_ENTER);
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate #ifdef	TRAPTRACE
8397c478bd9Sstevel@tonic-gate 	/*
8407c478bd9Sstevel@tonic-gate 	 * Turn off TRAPTRACE
8417c478bd9Sstevel@tonic-gate 	 */
8427c478bd9Sstevel@tonic-gate 	TRAPTRACE_FREEZE;
8437c478bd9Sstevel@tonic-gate #endif	/* TRAPTRACE */
8447c478bd9Sstevel@tonic-gate }
8457c478bd9Sstevel@tonic-gate 
8467c478bd9Sstevel@tonic-gate /*
8477c478bd9Sstevel@tonic-gate  * Platform callback prior to writing crash dump.
8487c478bd9Sstevel@tonic-gate  */
8497c478bd9Sstevel@tonic-gate /*ARGSUSED*/
8507c478bd9Sstevel@tonic-gate void
8517c478bd9Sstevel@tonic-gate panic_dump_hw(int spl)
8527c478bd9Sstevel@tonic-gate {
8537c478bd9Sstevel@tonic-gate 	/* Nothing to do here */
8547c478bd9Sstevel@tonic-gate }
8557c478bd9Sstevel@tonic-gate 
856*843e1988Sjohnlev void *
857*843e1988Sjohnlev plat_traceback(void *fpreg)
858*843e1988Sjohnlev {
859*843e1988Sjohnlev #ifdef __xpv
860*843e1988Sjohnlev 	if (IN_XPV_PANIC())
861*843e1988Sjohnlev 		return (xpv_traceback(fpreg));
862*843e1988Sjohnlev #endif
863*843e1988Sjohnlev 	return (fpreg);
864*843e1988Sjohnlev }
865*843e1988Sjohnlev 
8667c478bd9Sstevel@tonic-gate /*ARGSUSED*/
8677c478bd9Sstevel@tonic-gate void
8687c478bd9Sstevel@tonic-gate plat_tod_fault(enum tod_fault_type tod_bad)
869ae115bc7Smrj {}
8707c478bd9Sstevel@tonic-gate 
8717c478bd9Sstevel@tonic-gate /*ARGSUSED*/
8727c478bd9Sstevel@tonic-gate int
8737c478bd9Sstevel@tonic-gate blacklist(int cmd, const char *scheme, nvlist_t *fmri, const char *class)
8747c478bd9Sstevel@tonic-gate {
8757c478bd9Sstevel@tonic-gate 	return (ENOTSUP);
8767c478bd9Sstevel@tonic-gate }
8777c478bd9Sstevel@tonic-gate 
8787c478bd9Sstevel@tonic-gate /*
8797c478bd9Sstevel@tonic-gate  * The underlying console output routines are protected by raising IPL in case
8807c478bd9Sstevel@tonic-gate  * we are still calling into the early boot services.  Once we start calling
8817c478bd9Sstevel@tonic-gate  * the kernel console emulator, it will disable interrupts completely during
8827c478bd9Sstevel@tonic-gate  * character rendering (see sysp_putchar, for example).  Refer to the comments
8837c478bd9Sstevel@tonic-gate  * and code in common/os/console.c for more information on these callbacks.
8847c478bd9Sstevel@tonic-gate  */
8857c478bd9Sstevel@tonic-gate /*ARGSUSED*/
8867c478bd9Sstevel@tonic-gate int
8877c478bd9Sstevel@tonic-gate console_enter(int busy)
8887c478bd9Sstevel@tonic-gate {
8897c478bd9Sstevel@tonic-gate 	return (splzs());
8907c478bd9Sstevel@tonic-gate }
8917c478bd9Sstevel@tonic-gate 
8927c478bd9Sstevel@tonic-gate /*ARGSUSED*/
8937c478bd9Sstevel@tonic-gate void
8947c478bd9Sstevel@tonic-gate console_exit(int busy, int spl)
8957c478bd9Sstevel@tonic-gate {
8967c478bd9Sstevel@tonic-gate 	splx(spl);
8977c478bd9Sstevel@tonic-gate }
8987c478bd9Sstevel@tonic-gate 
8997c478bd9Sstevel@tonic-gate /*
9007c478bd9Sstevel@tonic-gate  * Allocate a region of virtual address space, unmapped.
9017c478bd9Sstevel@tonic-gate  * Stubbed out except on sparc, at least for now.
9027c478bd9Sstevel@tonic-gate  */
9037c478bd9Sstevel@tonic-gate /*ARGSUSED*/
9047c478bd9Sstevel@tonic-gate void *
9057c478bd9Sstevel@tonic-gate boot_virt_alloc(void *addr, size_t size)
9067c478bd9Sstevel@tonic-gate {
9077c478bd9Sstevel@tonic-gate 	return (addr);
9087c478bd9Sstevel@tonic-gate }
9097c478bd9Sstevel@tonic-gate 
9107c478bd9Sstevel@tonic-gate volatile unsigned long	tenmicrodata;
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate void
9137c478bd9Sstevel@tonic-gate tenmicrosec(void)
9147c478bd9Sstevel@tonic-gate {
915*843e1988Sjohnlev 	extern int gethrtime_hires;
9167c478bd9Sstevel@tonic-gate 
917*843e1988Sjohnlev 	if (gethrtime_hires) {
9187c478bd9Sstevel@tonic-gate 		hrtime_t start, end;
9197c478bd9Sstevel@tonic-gate 		start = end =  gethrtime();
9207c478bd9Sstevel@tonic-gate 		while ((end - start) < (10 * (NANOSEC / MICROSEC))) {
9217c478bd9Sstevel@tonic-gate 			SMT_PAUSE();
9227c478bd9Sstevel@tonic-gate 			end = gethrtime();
9237c478bd9Sstevel@tonic-gate 		}
9247c478bd9Sstevel@tonic-gate 	} else {
925*843e1988Sjohnlev #if defined(__xpv)
926*843e1988Sjohnlev 		hrtime_t newtime;
927*843e1988Sjohnlev 
928*843e1988Sjohnlev 		newtime = xpv_gethrtime() + 10000; /* now + 10 us */
929*843e1988Sjohnlev 		while (xpv_gethrtime() < newtime)
930*843e1988Sjohnlev 			SMT_PAUSE();
931*843e1988Sjohnlev #else	/* __xpv */
932ae115bc7Smrj 		int i;
933ae115bc7Smrj 
9347c478bd9Sstevel@tonic-gate 		/*
9357c478bd9Sstevel@tonic-gate 		 * Artificial loop to induce delay.
9367c478bd9Sstevel@tonic-gate 		 */
9377c478bd9Sstevel@tonic-gate 		for (i = 0; i < microdata; i++)
9387c478bd9Sstevel@tonic-gate 			tenmicrodata = microdata;
939*843e1988Sjohnlev #endif	/* __xpv */
9407c478bd9Sstevel@tonic-gate 	}
9417c478bd9Sstevel@tonic-gate }
942eda89462Sesolom 
943eda89462Sesolom /*
944eda89462Sesolom  * get_cpu_mstate() is passed an array of timestamps, NCMSTATES
945eda89462Sesolom  * long, and it fills in the array with the time spent on cpu in
946eda89462Sesolom  * each of the mstates, where time is returned in nsec.
947eda89462Sesolom  *
948eda89462Sesolom  * No guarantee is made that the returned values in times[] will
949eda89462Sesolom  * monotonically increase on sequential calls, although this will
950eda89462Sesolom  * be true in the long run. Any such guarantee must be handled by
951eda89462Sesolom  * the caller, if needed. This can happen if we fail to account
952eda89462Sesolom  * for elapsed time due to a generation counter conflict, yet we
953eda89462Sesolom  * did account for it on a prior call (see below).
954eda89462Sesolom  *
955eda89462Sesolom  * The complication is that the cpu in question may be updating
956eda89462Sesolom  * its microstate at the same time that we are reading it.
957eda89462Sesolom  * Because the microstate is only updated when the CPU's state
958eda89462Sesolom  * changes, the values in cpu_intracct[] can be indefinitely out
959eda89462Sesolom  * of date. To determine true current values, it is necessary to
960eda89462Sesolom  * compare the current time with cpu_mstate_start, and add the
961eda89462Sesolom  * difference to times[cpu_mstate].
962eda89462Sesolom  *
963eda89462Sesolom  * This can be a problem if those values are changing out from
964eda89462Sesolom  * under us. Because the code path in new_cpu_mstate() is
965eda89462Sesolom  * performance critical, we have not added a lock to it. Instead,
966eda89462Sesolom  * we have added a generation counter. Before beginning
967eda89462Sesolom  * modifications, the counter is set to 0. After modifications,
968eda89462Sesolom  * it is set to the old value plus one.
969eda89462Sesolom  *
970eda89462Sesolom  * get_cpu_mstate() will not consider the values of cpu_mstate
971eda89462Sesolom  * and cpu_mstate_start to be usable unless the value of
972eda89462Sesolom  * cpu_mstate_gen is both non-zero and unchanged, both before and
973eda89462Sesolom  * after reading the mstate information. Note that we must
974eda89462Sesolom  * protect against out-of-order loads around accesses to the
975eda89462Sesolom  * generation counter. Also, this is a best effort approach in
976eda89462Sesolom  * that we do not retry should the counter be found to have
977eda89462Sesolom  * changed.
978eda89462Sesolom  *
979eda89462Sesolom  * cpu_intracct[] is used to identify time spent in each CPU
980eda89462Sesolom  * mstate while handling interrupts. Such time should be reported
981eda89462Sesolom  * against system time, and so is subtracted out from its
982eda89462Sesolom  * corresponding cpu_acct[] time and added to
983eda89462Sesolom  * cpu_acct[CMS_SYSTEM].
984eda89462Sesolom  */
985eda89462Sesolom 
986eda89462Sesolom void
987eda89462Sesolom get_cpu_mstate(cpu_t *cpu, hrtime_t *times)
988eda89462Sesolom {
989eda89462Sesolom 	int i;
990eda89462Sesolom 	hrtime_t now, start;
991eda89462Sesolom 	uint16_t gen;
992eda89462Sesolom 	uint16_t state;
993eda89462Sesolom 	hrtime_t intracct[NCMSTATES];
994eda89462Sesolom 
995eda89462Sesolom 	/*
996eda89462Sesolom 	 * Load all volatile state under the protection of membar.
997eda89462Sesolom 	 * cpu_acct[cpu_mstate] must be loaded to avoid double counting
998eda89462Sesolom 	 * of (now - cpu_mstate_start) by a change in CPU mstate that
999eda89462Sesolom 	 * arrives after we make our last check of cpu_mstate_gen.
1000eda89462Sesolom 	 */
1001eda89462Sesolom 
1002eda89462Sesolom 	now = gethrtime_unscaled();
1003eda89462Sesolom 	gen = cpu->cpu_mstate_gen;
1004eda89462Sesolom 
1005eda89462Sesolom 	membar_consumer();	/* guarantee load ordering */
1006eda89462Sesolom 	start = cpu->cpu_mstate_start;
1007eda89462Sesolom 	state = cpu->cpu_mstate;
1008eda89462Sesolom 	for (i = 0; i < NCMSTATES; i++) {
1009eda89462Sesolom 		intracct[i] = cpu->cpu_intracct[i];
1010eda89462Sesolom 		times[i] = cpu->cpu_acct[i];
1011eda89462Sesolom 	}
1012eda89462Sesolom 	membar_consumer();	/* guarantee load ordering */
1013eda89462Sesolom 
1014eda89462Sesolom 	if (gen != 0 && gen == cpu->cpu_mstate_gen && now > start)
1015eda89462Sesolom 		times[state] += now - start;
1016eda89462Sesolom 
1017eda89462Sesolom 	for (i = 0; i < NCMSTATES; i++) {
1018eda89462Sesolom 		if (i == CMS_SYSTEM)
1019eda89462Sesolom 			continue;
1020eda89462Sesolom 		times[i] -= intracct[i];
1021eda89462Sesolom 		if (times[i] < 0) {
1022eda89462Sesolom 			intracct[i] += times[i];
1023eda89462Sesolom 			times[i] = 0;
1024eda89462Sesolom 		}
1025eda89462Sesolom 		times[CMS_SYSTEM] += intracct[i];
1026eda89462Sesolom 		scalehrtime(&times[i]);
1027eda89462Sesolom 	}
1028eda89462Sesolom 	scalehrtime(&times[CMS_SYSTEM]);
1029eda89462Sesolom }
1030ae115bc7Smrj 
1031ae115bc7Smrj 
1032ae115bc7Smrj /*
1033ae115bc7Smrj  * This is a version of the rdmsr instruction that allows
1034ae115bc7Smrj  * an error code to be returned in the case of failure.
1035ae115bc7Smrj  */
1036ae115bc7Smrj int
1037ae115bc7Smrj checked_rdmsr(uint_t msr, uint64_t *value)
1038ae115bc7Smrj {
1039ae115bc7Smrj 	if ((x86_feature & X86_MSR) == 0)
1040ae115bc7Smrj 		return (ENOTSUP);
1041ae115bc7Smrj 	*value = rdmsr(msr);
1042ae115bc7Smrj 	return (0);
1043ae115bc7Smrj }
1044ae115bc7Smrj 
1045ae115bc7Smrj /*
1046ae115bc7Smrj  * This is a version of the wrmsr instruction that allows
1047ae115bc7Smrj  * an error code to be returned in the case of failure.
1048ae115bc7Smrj  */
1049ae115bc7Smrj int
1050ae115bc7Smrj checked_wrmsr(uint_t msr, uint64_t value)
1051ae115bc7Smrj {
1052ae115bc7Smrj 	if ((x86_feature & X86_MSR) == 0)
1053ae115bc7Smrj 		return (ENOTSUP);
1054ae115bc7Smrj 	wrmsr(msr, value);
1055ae115bc7Smrj 	return (0);
1056ae115bc7Smrj }
1057ae115bc7Smrj 
1058ae115bc7Smrj /*
1059*843e1988Sjohnlev  * The mem driver's usual method of using hat_devload() to establish a
1060*843e1988Sjohnlev  * temporary mapping will not work for foreign pages mapped into this
1061*843e1988Sjohnlev  * domain or for the special hypervisor-provided pages.  For the foreign
1062*843e1988Sjohnlev  * pages, we often don't know which domain owns them, so we can't ask the
1063*843e1988Sjohnlev  * hypervisor to set up a new mapping.  For the other pages, we don't have
1064*843e1988Sjohnlev  * a pfn, so we can't create a new PTE.  For these special cases, we do a
1065*843e1988Sjohnlev  * direct uiomove() from the existing kernel virtual address.
1066ae115bc7Smrj  */
1067ae115bc7Smrj /*ARGSUSED*/
1068ae115bc7Smrj int
1069*843e1988Sjohnlev plat_mem_do_mmio(struct uio *uio, enum uio_rw rw)
1070ae115bc7Smrj {
1071*843e1988Sjohnlev #if defined(__xpv)
1072*843e1988Sjohnlev 	void *va = (void *)(uintptr_t)uio->uio_loffset;
1073*843e1988Sjohnlev 	off_t pageoff = uio->uio_loffset & PAGEOFFSET;
1074*843e1988Sjohnlev 	size_t nbytes = MIN((size_t)(PAGESIZE - pageoff),
1075*843e1988Sjohnlev 	    (size_t)uio->uio_iov->iov_len);
1076*843e1988Sjohnlev 
1077*843e1988Sjohnlev 	if ((rw == UIO_READ &&
1078*843e1988Sjohnlev 	    (va == HYPERVISOR_shared_info || va == xen_info)) ||
1079*843e1988Sjohnlev 	    (pfn_is_foreign(hat_getpfnum(kas.a_hat, va))))
1080*843e1988Sjohnlev 		return (uiomove(va, nbytes, rw, uio));
1081*843e1988Sjohnlev #endif
1082*843e1988Sjohnlev 	return (ENOTSUP);
1083*843e1988Sjohnlev }
1084*843e1988Sjohnlev 
1085*843e1988Sjohnlev pgcnt_t
1086*843e1988Sjohnlev num_phys_pages()
1087*843e1988Sjohnlev {
1088*843e1988Sjohnlev 	pgcnt_t npages = 0;
1089*843e1988Sjohnlev 	struct memlist *mp;
1090*843e1988Sjohnlev 
1091*843e1988Sjohnlev #if defined(__xpv)
1092*843e1988Sjohnlev 	if (DOMAIN_IS_INITDOMAIN(xen_info)) {
1093*843e1988Sjohnlev 		xen_sysctl_t op;
1094*843e1988Sjohnlev 
1095*843e1988Sjohnlev 		op.cmd = XEN_SYSCTL_physinfo;
1096*843e1988Sjohnlev 		op.interface_version = XEN_SYSCTL_INTERFACE_VERSION;
1097*843e1988Sjohnlev 		if (HYPERVISOR_sysctl(&op) != 0)
1098*843e1988Sjohnlev 			panic("physinfo op refused");
1099*843e1988Sjohnlev 
1100*843e1988Sjohnlev 		return ((pgcnt_t)op.u.physinfo.total_pages);
1101*843e1988Sjohnlev 	}
1102*843e1988Sjohnlev #endif /* __xpv */
1103*843e1988Sjohnlev 
1104*843e1988Sjohnlev 	for (mp = phys_install; mp != NULL; mp = mp->next)
1105*843e1988Sjohnlev 		npages += mp->size >> PAGESHIFT;
1106*843e1988Sjohnlev 
1107*843e1988Sjohnlev 	return (npages);
1108ae115bc7Smrj }
1109ae115bc7Smrj 
1110ae115bc7Smrj int
1111ae115bc7Smrj dump_plat_addr()
1112ae115bc7Smrj {
1113*843e1988Sjohnlev #ifdef __xpv
1114*843e1988Sjohnlev 	pfn_t pfn = mmu_btop(xen_info->shared_info) | PFN_IS_FOREIGN_MFN;
1115*843e1988Sjohnlev 	mem_vtop_t mem_vtop;
1116*843e1988Sjohnlev 	int cnt;
1117*843e1988Sjohnlev 
1118*843e1988Sjohnlev 	/*
1119*843e1988Sjohnlev 	 * On the hypervisor, we want to dump the page with shared_info on it.
1120*843e1988Sjohnlev 	 */
1121*843e1988Sjohnlev 	if (!IN_XPV_PANIC()) {
1122*843e1988Sjohnlev 		mem_vtop.m_as = &kas;
1123*843e1988Sjohnlev 		mem_vtop.m_va = HYPERVISOR_shared_info;
1124*843e1988Sjohnlev 		mem_vtop.m_pfn = pfn;
1125*843e1988Sjohnlev 		dumpvp_write(&mem_vtop, sizeof (mem_vtop_t));
1126*843e1988Sjohnlev 		cnt = 1;
1127*843e1988Sjohnlev 	} else {
1128*843e1988Sjohnlev 		cnt = dump_xpv_addr();
1129*843e1988Sjohnlev 	}
1130*843e1988Sjohnlev 	return (cnt);
1131*843e1988Sjohnlev #else
1132ae115bc7Smrj 	return (0);
1133*843e1988Sjohnlev #endif
1134ae115bc7Smrj }
1135ae115bc7Smrj 
1136ae115bc7Smrj void
1137ae115bc7Smrj dump_plat_pfn()
1138ae115bc7Smrj {
1139*843e1988Sjohnlev #ifdef __xpv
1140*843e1988Sjohnlev 	pfn_t pfn = mmu_btop(xen_info->shared_info) | PFN_IS_FOREIGN_MFN;
1141*843e1988Sjohnlev 
1142*843e1988Sjohnlev 	if (!IN_XPV_PANIC())
1143*843e1988Sjohnlev 		dumpvp_write(&pfn, sizeof (pfn));
1144*843e1988Sjohnlev 	else
1145*843e1988Sjohnlev 		dump_xpv_pfn();
1146*843e1988Sjohnlev #endif
1147ae115bc7Smrj }
1148ae115bc7Smrj 
1149ae115bc7Smrj /*ARGSUSED*/
1150ae115bc7Smrj int
1151ae115bc7Smrj dump_plat_data(void *dump_cbuf)
1152ae115bc7Smrj {
1153*843e1988Sjohnlev #ifdef __xpv
1154*843e1988Sjohnlev 	uint32_t csize;
1155*843e1988Sjohnlev 	int cnt;
1156*843e1988Sjohnlev 
1157*843e1988Sjohnlev 	if (!IN_XPV_PANIC()) {
1158*843e1988Sjohnlev 		csize = (uint32_t)compress(HYPERVISOR_shared_info, dump_cbuf,
1159*843e1988Sjohnlev 		    PAGESIZE);
1160*843e1988Sjohnlev 		dumpvp_write(&csize, sizeof (uint32_t));
1161*843e1988Sjohnlev 		dumpvp_write(dump_cbuf, csize);
1162*843e1988Sjohnlev 		cnt = 1;
1163*843e1988Sjohnlev 	} else {
1164*843e1988Sjohnlev 		cnt = dump_xpv_data(dump_cbuf);
1165*843e1988Sjohnlev 	}
1166*843e1988Sjohnlev 	return (cnt);
1167*843e1988Sjohnlev #else
1168ae115bc7Smrj 	return (0);
1169*843e1988Sjohnlev #endif
1170ae115bc7Smrj }
1171ddece0baSsethg 
1172ddece0baSsethg /*
1173ddece0baSsethg  * Calculates a linear address, given the CS selector and PC values
1174ddece0baSsethg  * by looking up the %cs selector process's LDT or the CPU's GDT.
1175ddece0baSsethg  * proc->p_ldtlock must be held across this call.
1176ddece0baSsethg  */
1177ddece0baSsethg int
1178ddece0baSsethg linear_pc(struct regs *rp, proc_t *p, caddr_t *linearp)
1179ddece0baSsethg {
1180ddece0baSsethg 	user_desc_t	*descrp;
1181ddece0baSsethg 	caddr_t		baseaddr;
1182ddece0baSsethg 	uint16_t	idx = SELTOIDX(rp->r_cs);
1183ddece0baSsethg 
1184ddece0baSsethg 	ASSERT(rp->r_cs <= 0xFFFF);
1185ddece0baSsethg 	ASSERT(MUTEX_HELD(&p->p_ldtlock));
1186ddece0baSsethg 
1187ddece0baSsethg 	if (SELISLDT(rp->r_cs)) {
1188ddece0baSsethg 		/*
1189ddece0baSsethg 		 * Currently 64 bit processes cannot have private LDTs.
1190ddece0baSsethg 		 */
1191ddece0baSsethg 		ASSERT(p->p_model != DATAMODEL_LP64);
1192ddece0baSsethg 
1193ddece0baSsethg 		if (p->p_ldt == NULL)
1194ddece0baSsethg 			return (-1);
1195ddece0baSsethg 
1196ddece0baSsethg 		descrp = &p->p_ldt[idx];
1197ddece0baSsethg 		baseaddr = (caddr_t)(uintptr_t)USEGD_GETBASE(descrp);
1198ddece0baSsethg 
1199ddece0baSsethg 		/*
1200ddece0baSsethg 		 * Calculate the linear address (wraparound is not only ok,
1201ddece0baSsethg 		 * it's expected behavior).  The cast to uint32_t is because
1202ddece0baSsethg 		 * LDT selectors are only allowed in 32-bit processes.
1203ddece0baSsethg 		 */
1204ddece0baSsethg 		*linearp = (caddr_t)(uintptr_t)(uint32_t)((uintptr_t)baseaddr +
1205ddece0baSsethg 		    rp->r_pc);
1206ddece0baSsethg 	} else {
1207ddece0baSsethg #ifdef DEBUG
1208ddece0baSsethg 		descrp = &CPU->cpu_gdt[idx];
1209ddece0baSsethg 		baseaddr = (caddr_t)(uintptr_t)USEGD_GETBASE(descrp);
1210ddece0baSsethg 		/* GDT-based descriptors' base addresses should always be 0 */
1211ddece0baSsethg 		ASSERT(baseaddr == 0);
1212ddece0baSsethg #endif
1213ddece0baSsethg 		*linearp = (caddr_t)(uintptr_t)rp->r_pc;
1214ddece0baSsethg 	}
1215ddece0baSsethg 
1216ddece0baSsethg 	return (0);
1217ddece0baSsethg }
1218ddece0baSsethg 
1219ddece0baSsethg /*
1220ddece0baSsethg  * The implementation of dtrace_linear_pc is similar to the that of
1221ddece0baSsethg  * linear_pc, above, but here we acquire p_ldtlock before accessing
1222ddece0baSsethg  * p_ldt.  This implementation is used by the pid provider; we prefix
1223ddece0baSsethg  * it with "dtrace_" to avoid inducing spurious tracing events.
1224ddece0baSsethg  */
1225ddece0baSsethg int
1226ddece0baSsethg dtrace_linear_pc(struct regs *rp, proc_t *p, caddr_t *linearp)
1227ddece0baSsethg {
1228ddece0baSsethg 	user_desc_t	*descrp;
1229ddece0baSsethg 	caddr_t		baseaddr;
1230ddece0baSsethg 	uint16_t	idx = SELTOIDX(rp->r_cs);
1231ddece0baSsethg 
1232ddece0baSsethg 	ASSERT(rp->r_cs <= 0xFFFF);
1233ddece0baSsethg 
1234ddece0baSsethg 	if (SELISLDT(rp->r_cs)) {
1235ddece0baSsethg 		/*
1236ddece0baSsethg 		 * Currently 64 bit processes cannot have private LDTs.
1237ddece0baSsethg 		 */
1238ddece0baSsethg 		ASSERT(p->p_model != DATAMODEL_LP64);
1239ddece0baSsethg 
1240ddece0baSsethg 		mutex_enter(&p->p_ldtlock);
1241ddece0baSsethg 		if (p->p_ldt == NULL) {
1242ddece0baSsethg 			mutex_exit(&p->p_ldtlock);
1243ddece0baSsethg 			return (-1);
1244ddece0baSsethg 		}
1245ddece0baSsethg 		descrp = &p->p_ldt[idx];
1246ddece0baSsethg 		baseaddr = (caddr_t)(uintptr_t)USEGD_GETBASE(descrp);
1247ddece0baSsethg 		mutex_exit(&p->p_ldtlock);
1248ddece0baSsethg 
1249ddece0baSsethg 		/*
1250ddece0baSsethg 		 * Calculate the linear address (wraparound is not only ok,
1251ddece0baSsethg 		 * it's expected behavior).  The cast to uint32_t is because
1252ddece0baSsethg 		 * LDT selectors are only allowed in 32-bit processes.
1253ddece0baSsethg 		 */
1254ddece0baSsethg 		*linearp = (caddr_t)(uintptr_t)(uint32_t)((uintptr_t)baseaddr +
1255ddece0baSsethg 		    rp->r_pc);
1256ddece0baSsethg 	} else {
1257ddece0baSsethg #ifdef DEBUG
1258ddece0baSsethg 		descrp = &CPU->cpu_gdt[idx];
1259ddece0baSsethg 		baseaddr = (caddr_t)(uintptr_t)USEGD_GETBASE(descrp);
1260ddece0baSsethg 		/* GDT-based descriptors' base addresses should always be 0 */
1261ddece0baSsethg 		ASSERT(baseaddr == 0);
1262ddece0baSsethg #endif
1263ddece0baSsethg 		*linearp = (caddr_t)(uintptr_t)rp->r_pc;
1264ddece0baSsethg 	}
1265ddece0baSsethg 
1266ddece0baSsethg 	return (0);
1267ddece0baSsethg }
1268