xref: /onnv-gate/usr/src/uts/i86pc/os/machdep.c (revision 7863:4424fcacd119)
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 /*
236681Sjohnlev  * Copyright 2008 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 stop_other_cpus();
1620Sstevel@tonic-gate void debug_enter(char *);
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate extern void pm_cfb_check_and_powerup(void);
1650Sstevel@tonic-gate extern void pm_cfb_rele(void);
1660Sstevel@tonic-gate 
1677656SSherry.Moore@Sun.COM extern fastboot_info_t newkernel;
1687656SSherry.Moore@Sun.COM 
1697656SSherry.Moore@Sun.COM int quiesce_active = 0;
1707656SSherry.Moore@Sun.COM 
1710Sstevel@tonic-gate /*
1720Sstevel@tonic-gate  * Machine dependent code to reboot.
1730Sstevel@tonic-gate  * "mdep" is interpreted as a character pointer; if non-null, it is a pointer
1740Sstevel@tonic-gate  * to a string to be used as the argument string when rebooting.
175136Sachartre  *
176136Sachartre  * "invoke_cb" is a boolean. It is set to true when mdboot() can safely
177136Sachartre  * invoke CB_CL_MDBOOT callbacks before shutting the system down, i.e. when
178136Sachartre  * we are in a normal shutdown sequence (interrupts are not blocked, the
179136Sachartre  * system is not panic'ing or being suspended).
1800Sstevel@tonic-gate  */
1810Sstevel@tonic-gate /*ARGSUSED*/
1820Sstevel@tonic-gate void
183136Sachartre mdboot(int cmd, int fcn, char *mdep, boolean_t invoke_cb)
1840Sstevel@tonic-gate {
1857656SSherry.Moore@Sun.COM 	processorid_t bootcpuid = 0;
186*7863SSherry.Moore@Sun.COM 	static int is_first_quiesce = 1;
187*7863SSherry.Moore@Sun.COM 	static int is_first_reset = 1;
188*7863SSherry.Moore@Sun.COM 	int reset_status = 0;
1897656SSherry.Moore@Sun.COM 
1907656SSherry.Moore@Sun.COM 	if (fcn == AD_FASTREBOOT && !newkernel.fi_valid)
1917656SSherry.Moore@Sun.COM 		fcn = AD_BOOT;
1927656SSherry.Moore@Sun.COM 
1930Sstevel@tonic-gate 	if (!panicstr) {
1940Sstevel@tonic-gate 		kpreempt_disable();
1957656SSherry.Moore@Sun.COM 		if (fcn == AD_FASTREBOOT) {
1967656SSherry.Moore@Sun.COM 			mutex_enter(&cpu_lock);
1977656SSherry.Moore@Sun.COM 			if (CPU_ACTIVE(cpu_get(bootcpuid))) {
1987656SSherry.Moore@Sun.COM 				affinity_set(bootcpuid);
1997656SSherry.Moore@Sun.COM 			}
2007656SSherry.Moore@Sun.COM 			mutex_exit(&cpu_lock);
2017656SSherry.Moore@Sun.COM 		} else {
2027656SSherry.Moore@Sun.COM 			affinity_set(CPU_CURRENT);
2037656SSherry.Moore@Sun.COM 		}
2040Sstevel@tonic-gate 	}
2050Sstevel@tonic-gate 
2066681Sjohnlev 	if (force_shutdown_method != AD_UNKNOWN)
2076681Sjohnlev 		fcn = force_shutdown_method;
2086681Sjohnlev 
2090Sstevel@tonic-gate 	/*
2105630Sjbeck 	 * XXX - rconsvp is set to NULL to ensure that output messages
2115630Sjbeck 	 * are sent to the underlying "hardware" device using the
2125630Sjbeck 	 * monitor's printf routine since we are in the process of
2135630Sjbeck 	 * either rebooting or halting the machine.
2145630Sjbeck 	 */
2155630Sjbeck 	rconsvp = NULL;
2165630Sjbeck 
2175630Sjbeck 	/*
2180Sstevel@tonic-gate 	 * Print the reboot message now, before pausing other cpus.
2190Sstevel@tonic-gate 	 * There is a race condition in the printing support that
2200Sstevel@tonic-gate 	 * can deadlock multiprocessor machines.
2210Sstevel@tonic-gate 	 */
2220Sstevel@tonic-gate 	if (!(fcn == AD_HALT || fcn == AD_POWEROFF))
2230Sstevel@tonic-gate 		prom_printf("rebooting...\n");
2240Sstevel@tonic-gate 
2255084Sjohnlev 	if (IN_XPV_PANIC())
2265084Sjohnlev 		reset();
2275084Sjohnlev 
2280Sstevel@tonic-gate 	/*
2290Sstevel@tonic-gate 	 * We can't bring up the console from above lock level, so do it now
2300Sstevel@tonic-gate 	 */
2310Sstevel@tonic-gate 	pm_cfb_check_and_powerup();
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate 	/* make sure there are no more changes to the device tree */
2340Sstevel@tonic-gate 	devtree_freeze();
2350Sstevel@tonic-gate 
236136Sachartre 	if (invoke_cb)
237136Sachartre 		(void) callb_execute_class(CB_CL_MDBOOT, NULL);
238136Sachartre 
2393253Smec 	/*
2403253Smec 	 * Clear any unresolved UEs from memory.
2413253Smec 	 */
2423253Smec 	page_retire_mdboot();
243917Selowe 
2445084Sjohnlev #if defined(__xpv)
2455084Sjohnlev 	/*
2465084Sjohnlev 	 * XXPV	Should probably think some more about how we deal
2475084Sjohnlev 	 *	with panicing before it's really safe to panic.
2485084Sjohnlev 	 *	On hypervisors, we reboot very quickly..  Perhaps panic
2495084Sjohnlev 	 *	should only attempt to recover by rebooting if,
2505084Sjohnlev 	 *	say, we were able to mount the root filesystem,
2515084Sjohnlev 	 *	or if we successfully launched init(1m).
2525084Sjohnlev 	 */
2535084Sjohnlev 	if (panicstr && proc_init == NULL)
2545084Sjohnlev 		(void) HYPERVISOR_shutdown(SHUTDOWN_poweroff);
2555084Sjohnlev #endif
2560Sstevel@tonic-gate 	/*
2570Sstevel@tonic-gate 	 * stop other cpus and raise our priority.  since there is only
2580Sstevel@tonic-gate 	 * one active cpu after this, and our priority will be too high
2590Sstevel@tonic-gate 	 * for us to be preempted, we're essentially single threaded
2600Sstevel@tonic-gate 	 * from here on out.
2610Sstevel@tonic-gate 	 */
2620Sstevel@tonic-gate 	(void) spl6();
2630Sstevel@tonic-gate 	if (!panicstr) {
2640Sstevel@tonic-gate 		mutex_enter(&cpu_lock);
2650Sstevel@tonic-gate 		pause_cpus(NULL);
2660Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
2670Sstevel@tonic-gate 	}
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate 	/*
2707656SSherry.Moore@Sun.COM 	 * Try to quiesce devices.
2710Sstevel@tonic-gate 	 */
272*7863SSherry.Moore@Sun.COM 	if (is_first_quiesce) {
273*7863SSherry.Moore@Sun.COM 		/*
274*7863SSherry.Moore@Sun.COM 		 * Clear is_first_quiesce before calling quiesce_devices()
275*7863SSherry.Moore@Sun.COM 		 * so that if quiesce_devices() causes panics, it will not
276*7863SSherry.Moore@Sun.COM 		 * be invoked again.
277*7863SSherry.Moore@Sun.COM 		 */
278*7863SSherry.Moore@Sun.COM 		is_first_quiesce = 0;
2797656SSherry.Moore@Sun.COM 
2807656SSherry.Moore@Sun.COM 		quiesce_active = 1;
2817656SSherry.Moore@Sun.COM 		quiesce_devices(ddi_root_node(), &reset_status);
2827783SSherry.Moore@Sun.COM 		if (reset_status == -1) {
2837783SSherry.Moore@Sun.COM 			if (fcn == AD_FASTREBOOT && !force_fastreboot) {
2847783SSherry.Moore@Sun.COM 				prom_printf("Driver(s) not capable of fast "
2857783SSherry.Moore@Sun.COM 				    "reboot. Fall back to regular reboot.\n");
2867783SSherry.Moore@Sun.COM 				fastreboot_capable = 0;
2877783SSherry.Moore@Sun.COM 			} else if (fcn != AD_FASTREBOOT)
2887783SSherry.Moore@Sun.COM 				fastreboot_capable = 0;
2897656SSherry.Moore@Sun.COM 		}
2907656SSherry.Moore@Sun.COM 		quiesce_active = 0;
2917656SSherry.Moore@Sun.COM 	}
2927656SSherry.Moore@Sun.COM 
2937656SSherry.Moore@Sun.COM 	/*
294*7863SSherry.Moore@Sun.COM 	 * Try to reset devices. reset_leaves() should only be called
295*7863SSherry.Moore@Sun.COM 	 * a) when there are no other threads that could be accessing devices,
296*7863SSherry.Moore@Sun.COM 	 *    and
297*7863SSherry.Moore@Sun.COM 	 * b) on a system that's not capable of fast reboot (fastreboot_capable
298*7863SSherry.Moore@Sun.COM 	 *    being 0), or on a system where quiesce_devices() failed to
299*7863SSherry.Moore@Sun.COM 	 *    complete (quiesce_active being 1).
3007656SSherry.Moore@Sun.COM 	 */
301*7863SSherry.Moore@Sun.COM 	if (is_first_reset && (!fastreboot_capable || quiesce_active)) {
302*7863SSherry.Moore@Sun.COM 		/*
303*7863SSherry.Moore@Sun.COM 		 * Clear is_first_reset before calling reset_devices()
304*7863SSherry.Moore@Sun.COM 		 * so that if reset_devices() causes panics, it will not
305*7863SSherry.Moore@Sun.COM 		 * be invoked again.
306*7863SSherry.Moore@Sun.COM 		 */
307*7863SSherry.Moore@Sun.COM 		is_first_reset = 0;
3087656SSherry.Moore@Sun.COM 		reset_leaves();
309*7863SSherry.Moore@Sun.COM 	}
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate 	(void) spl8();
3120Sstevel@tonic-gate 	(*psm_shutdownf)(cmd, fcn);
3130Sstevel@tonic-gate 
3147656SSherry.Moore@Sun.COM 	if (fcn == AD_FASTREBOOT && !panicstr && fastreboot_capable)
3157656SSherry.Moore@Sun.COM 		fast_reboot();
3167656SSherry.Moore@Sun.COM 	else if (fcn == AD_HALT || fcn == AD_POWEROFF)
3170Sstevel@tonic-gate 		halt((char *)NULL);
3180Sstevel@tonic-gate 	else
3190Sstevel@tonic-gate 		prom_reboot("");
3200Sstevel@tonic-gate 	/*NOTREACHED*/
3210Sstevel@tonic-gate }
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate /* mdpreboot - may be called prior to mdboot while root fs still mounted */
3240Sstevel@tonic-gate /*ARGSUSED*/
3250Sstevel@tonic-gate void
3260Sstevel@tonic-gate mdpreboot(int cmd, int fcn, char *mdep)
3270Sstevel@tonic-gate {
3287656SSherry.Moore@Sun.COM 	if (fcn == AD_FASTREBOOT && !fastreboot_capable) {
3297656SSherry.Moore@Sun.COM 		fcn = AD_BOOT;
3307656SSherry.Moore@Sun.COM #ifdef	__xpv
3317656SSherry.Moore@Sun.COM 		cmn_err(CE_WARN, "Fast reboot not supported on xVM");
3327656SSherry.Moore@Sun.COM #else
3337656SSherry.Moore@Sun.COM 		cmn_err(CE_WARN, "Fast reboot not supported on this platform");
3347656SSherry.Moore@Sun.COM #endif
3357656SSherry.Moore@Sun.COM 	}
3367656SSherry.Moore@Sun.COM 
3377656SSherry.Moore@Sun.COM 	if (fcn == AD_FASTREBOOT) {
3387656SSherry.Moore@Sun.COM 		load_kernel(mdep);
3397656SSherry.Moore@Sun.COM 		if (!newkernel.fi_valid)
3407656SSherry.Moore@Sun.COM 			fcn = AD_BOOT;
3417656SSherry.Moore@Sun.COM 	}
3427656SSherry.Moore@Sun.COM 
3430Sstevel@tonic-gate 	(*psm_preshutdownf)(cmd, fcn);
3440Sstevel@tonic-gate }
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate void
3470Sstevel@tonic-gate idle_other_cpus()
3480Sstevel@tonic-gate {
3490Sstevel@tonic-gate 	int cpuid = CPU->cpu_id;
3500Sstevel@tonic-gate 	cpuset_t xcset;
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate 	ASSERT(cpuid < NCPU);
3530Sstevel@tonic-gate 	CPUSET_ALL_BUT(xcset, cpuid);
3540Sstevel@tonic-gate 	xc_capture_cpus(xcset);
3550Sstevel@tonic-gate }
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate void
3580Sstevel@tonic-gate resume_other_cpus()
3590Sstevel@tonic-gate {
3600Sstevel@tonic-gate 	ASSERT(CPU->cpu_id < NCPU);
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate 	xc_release_cpus();
3630Sstevel@tonic-gate }
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate void
3660Sstevel@tonic-gate stop_other_cpus()
3670Sstevel@tonic-gate {
3680Sstevel@tonic-gate 	int cpuid = CPU->cpu_id;
3690Sstevel@tonic-gate 	cpuset_t xcset;
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate 	ASSERT(cpuid < NCPU);
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate 	/*
3743446Smrj 	 * xc_trycall will attempt to make all other CPUs execute mach_cpu_halt,
3750Sstevel@tonic-gate 	 * and will return immediately regardless of whether or not it was
3760Sstevel@tonic-gate 	 * able to make them do it.
3770Sstevel@tonic-gate 	 */
3780Sstevel@tonic-gate 	CPUSET_ALL_BUT(xcset, cpuid);
3793446Smrj 	xc_trycall(NULL, NULL, NULL, xcset, (int (*)())mach_cpu_halt);
3800Sstevel@tonic-gate }
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate /*
3830Sstevel@tonic-gate  *	Machine dependent abort sequence handling
3840Sstevel@tonic-gate  */
3850Sstevel@tonic-gate void
3860Sstevel@tonic-gate abort_sequence_enter(char *msg)
3870Sstevel@tonic-gate {
3880Sstevel@tonic-gate 	if (abort_enable == 0) {
3890Sstevel@tonic-gate 		if (audit_active)
3900Sstevel@tonic-gate 			audit_enterprom(0);
3910Sstevel@tonic-gate 		return;
3920Sstevel@tonic-gate 	}
3930Sstevel@tonic-gate 	if (audit_active)
3940Sstevel@tonic-gate 		audit_enterprom(1);
3950Sstevel@tonic-gate 	debug_enter(msg);
3960Sstevel@tonic-gate 	if (audit_active)
3970Sstevel@tonic-gate 		audit_exitprom(1);
3980Sstevel@tonic-gate }
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate /*
4010Sstevel@tonic-gate  * Enter debugger.  Called when the user types ctrl-alt-d or whenever
4020Sstevel@tonic-gate  * code wants to enter the debugger and possibly resume later.
4030Sstevel@tonic-gate  */
4040Sstevel@tonic-gate void
4050Sstevel@tonic-gate debug_enter(
4060Sstevel@tonic-gate 	char	*msg)		/* message to print, possibly NULL */
4070Sstevel@tonic-gate {
4080Sstevel@tonic-gate 	if (dtrace_debugger_init != NULL)
4090Sstevel@tonic-gate 		(*dtrace_debugger_init)();
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate 	if (msg)
4120Sstevel@tonic-gate 		prom_printf("%s\n", msg);
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate 	if (boothowto & RB_DEBUG)
4153446Smrj 		kmdb_enter();
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate 	if (dtrace_debugger_fini != NULL)
4180Sstevel@tonic-gate 		(*dtrace_debugger_fini)();
4190Sstevel@tonic-gate }
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate void
4220Sstevel@tonic-gate reset(void)
4230Sstevel@tonic-gate {
4245084Sjohnlev #if !defined(__xpv)
4250Sstevel@tonic-gate 	ushort_t *bios_memchk;
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate 	/*
4280Sstevel@tonic-gate 	 * Can't use psm_map_phys before the hat is initialized.
4290Sstevel@tonic-gate 	 */
4300Sstevel@tonic-gate 	if (khat_running) {
4310Sstevel@tonic-gate 		bios_memchk = (ushort_t *)psm_map_phys(0x472,
4320Sstevel@tonic-gate 		    sizeof (ushort_t), PROT_READ | PROT_WRITE);
4330Sstevel@tonic-gate 		if (bios_memchk)
4340Sstevel@tonic-gate 			*bios_memchk = 0x1234;	/* bios memory check disable */
4350Sstevel@tonic-gate 	}
4360Sstevel@tonic-gate 
4372866Sszhou 	if (ddi_prop_exists(DDI_DEV_T_ANY, ddi_root_node(), 0, "efi-systab"))
4382866Sszhou 		efi_reset();
4390Sstevel@tonic-gate 	pc_reset();
4405084Sjohnlev #else
4415084Sjohnlev 	if (IN_XPV_PANIC())
4425084Sjohnlev 		pc_reset();
4435084Sjohnlev 	(void) HYPERVISOR_shutdown(SHUTDOWN_reboot);
4445084Sjohnlev 	panic("HYPERVISOR_shutdown() failed");
4455084Sjohnlev #endif
4460Sstevel@tonic-gate 	/*NOTREACHED*/
4470Sstevel@tonic-gate }
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate /*
4500Sstevel@tonic-gate  * Halt the machine and return to the monitor
4510Sstevel@tonic-gate  */
4520Sstevel@tonic-gate void
4530Sstevel@tonic-gate halt(char *s)
4540Sstevel@tonic-gate {
4550Sstevel@tonic-gate 	stop_other_cpus();	/* send stop signal to other CPUs */
4560Sstevel@tonic-gate 	if (s)
4570Sstevel@tonic-gate 		prom_printf("(%s) \n", s);
4580Sstevel@tonic-gate 	prom_exit_to_mon();
4590Sstevel@tonic-gate 	/*NOTREACHED*/
4600Sstevel@tonic-gate }
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate /*
4630Sstevel@tonic-gate  * Initiate interrupt redistribution.
4640Sstevel@tonic-gate  */
4650Sstevel@tonic-gate void
4660Sstevel@tonic-gate i_ddi_intr_redist_all_cpus()
4670Sstevel@tonic-gate {
4680Sstevel@tonic-gate }
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate /*
4710Sstevel@tonic-gate  * XXX These probably ought to live somewhere else
4720Sstevel@tonic-gate  * XXX They are called from mem.c
4730Sstevel@tonic-gate  */
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate /*
4760Sstevel@tonic-gate  * Convert page frame number to an OBMEM page frame number
4770Sstevel@tonic-gate  * (i.e. put in the type bits -- zero for this implementation)
4780Sstevel@tonic-gate  */
4790Sstevel@tonic-gate pfn_t
4800Sstevel@tonic-gate impl_obmem_pfnum(pfn_t pf)
4810Sstevel@tonic-gate {
4820Sstevel@tonic-gate 	return (pf);
4830Sstevel@tonic-gate }
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate #ifdef	NM_DEBUG
4860Sstevel@tonic-gate int nmi_test = 0;	/* checked in intentry.s during clock int */
4870Sstevel@tonic-gate int nmtest = -1;
4880Sstevel@tonic-gate nmfunc1(arg, rp)
4890Sstevel@tonic-gate int	arg;
4900Sstevel@tonic-gate struct regs *rp;
4910Sstevel@tonic-gate {
4920Sstevel@tonic-gate 	printf("nmi called with arg = %x, regs = %x\n", arg, rp);
4930Sstevel@tonic-gate 	nmtest += 50;
4940Sstevel@tonic-gate 	if (arg == nmtest) {
4950Sstevel@tonic-gate 		printf("ip = %x\n", rp->r_pc);
4960Sstevel@tonic-gate 		return (1);
4970Sstevel@tonic-gate 	}
4980Sstevel@tonic-gate 	return (0);
4990Sstevel@tonic-gate }
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate #endif
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate #include <sys/bootsvcs.h>
5040Sstevel@tonic-gate 
5050Sstevel@tonic-gate /* Hacked up initialization for initial kernel check out is HERE. */
5060Sstevel@tonic-gate /* The basic steps are: */
5070Sstevel@tonic-gate /*	kernel bootfuncs definition/initialization for KADB */
5080Sstevel@tonic-gate /*	kadb bootfuncs pointer initialization */
5090Sstevel@tonic-gate /*	putchar/getchar (interrupts disabled) */
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate /* kadb bootfuncs pointer initialization */
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate int
5140Sstevel@tonic-gate sysp_getchar()
5150Sstevel@tonic-gate {
5160Sstevel@tonic-gate 	int i;
5173446Smrj 	ulong_t s;
5180Sstevel@tonic-gate 
5190Sstevel@tonic-gate 	if (cons_polledio == NULL) {
5200Sstevel@tonic-gate 		/* Uh oh */
5210Sstevel@tonic-gate 		prom_printf("getchar called with no console\n");
5220Sstevel@tonic-gate 		for (;;)
5230Sstevel@tonic-gate 			/* LOOP FOREVER */;
5240Sstevel@tonic-gate 	}
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate 	s = clear_int_flag();
5270Sstevel@tonic-gate 	i = cons_polledio->cons_polledio_getchar(
5285084Sjohnlev 	    cons_polledio->cons_polledio_argument);
5290Sstevel@tonic-gate 	restore_int_flag(s);
5300Sstevel@tonic-gate 	return (i);
5310Sstevel@tonic-gate }
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate void
5340Sstevel@tonic-gate sysp_putchar(int c)
5350Sstevel@tonic-gate {
5363446Smrj 	ulong_t s;
5370Sstevel@tonic-gate 
5380Sstevel@tonic-gate 	/*
5390Sstevel@tonic-gate 	 * We have no alternative but to drop the output on the floor.
5400Sstevel@tonic-gate 	 */
5411253Slq150181 	if (cons_polledio == NULL ||
5421253Slq150181 	    cons_polledio->cons_polledio_putchar == NULL)
5430Sstevel@tonic-gate 		return;
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate 	s = clear_int_flag();
5460Sstevel@tonic-gate 	cons_polledio->cons_polledio_putchar(
5475084Sjohnlev 	    cons_polledio->cons_polledio_argument, c);
5480Sstevel@tonic-gate 	restore_int_flag(s);
5490Sstevel@tonic-gate }
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate int
5520Sstevel@tonic-gate sysp_ischar()
5530Sstevel@tonic-gate {
5540Sstevel@tonic-gate 	int i;
5553446Smrj 	ulong_t s;
5560Sstevel@tonic-gate 
5571253Slq150181 	if (cons_polledio == NULL ||
5581253Slq150181 	    cons_polledio->cons_polledio_ischar == NULL)
5590Sstevel@tonic-gate 		return (0);
5600Sstevel@tonic-gate 
5610Sstevel@tonic-gate 	s = clear_int_flag();
5620Sstevel@tonic-gate 	i = cons_polledio->cons_polledio_ischar(
5635084Sjohnlev 	    cons_polledio->cons_polledio_argument);
5640Sstevel@tonic-gate 	restore_int_flag(s);
5650Sstevel@tonic-gate 	return (i);
5660Sstevel@tonic-gate }
5670Sstevel@tonic-gate 
5680Sstevel@tonic-gate int
5690Sstevel@tonic-gate goany(void)
5700Sstevel@tonic-gate {
5710Sstevel@tonic-gate 	prom_printf("Type any key to continue ");
5720Sstevel@tonic-gate 	(void) prom_getchar();
5730Sstevel@tonic-gate 	prom_printf("\n");
5740Sstevel@tonic-gate 	return (1);
5750Sstevel@tonic-gate }
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate static struct boot_syscalls kern_sysp = {
5780Sstevel@tonic-gate 	sysp_getchar,	/*	unchar	(*getchar)();	7  */
5790Sstevel@tonic-gate 	sysp_putchar,	/*	int	(*putchar)();	8  */
5800Sstevel@tonic-gate 	sysp_ischar,	/*	int	(*ischar)();	9  */
5810Sstevel@tonic-gate };
5820Sstevel@tonic-gate 
5835084Sjohnlev #if defined(__xpv)
5845084Sjohnlev int using_kern_polledio;
5855084Sjohnlev #endif
5865084Sjohnlev 
5870Sstevel@tonic-gate void
5880Sstevel@tonic-gate kadb_uses_kernel()
5890Sstevel@tonic-gate {
5900Sstevel@tonic-gate 	/*
5910Sstevel@tonic-gate 	 * This routine is now totally misnamed, since it does not in fact
5920Sstevel@tonic-gate 	 * control kadb's I/O; it only controls the kernel's prom_* I/O.
5930Sstevel@tonic-gate 	 */
5940Sstevel@tonic-gate 	sysp = &kern_sysp;
5955084Sjohnlev #if defined(__xpv)
5965084Sjohnlev 	using_kern_polledio = 1;
5975084Sjohnlev #endif
5980Sstevel@tonic-gate }
5990Sstevel@tonic-gate 
6000Sstevel@tonic-gate /*
6010Sstevel@tonic-gate  *	the interface to the outside world
6020Sstevel@tonic-gate  */
6030Sstevel@tonic-gate 
6040Sstevel@tonic-gate /*
6050Sstevel@tonic-gate  * poll_port -- wait for a register to achieve a
6060Sstevel@tonic-gate  *		specific state.  Arguments are a mask of bits we care about,
6070Sstevel@tonic-gate  *		and two sub-masks.  To return normally, all the bits in the
6080Sstevel@tonic-gate  *		first sub-mask must be ON, all the bits in the second sub-
6090Sstevel@tonic-gate  *		mask must be OFF.  If about seconds pass without the register
6100Sstevel@tonic-gate  *		achieving the desired bit configuration, we return 1, else
6110Sstevel@tonic-gate  *		0.
6120Sstevel@tonic-gate  */
6130Sstevel@tonic-gate int
6140Sstevel@tonic-gate poll_port(ushort_t port, ushort_t mask, ushort_t onbits, ushort_t offbits)
6150Sstevel@tonic-gate {
6160Sstevel@tonic-gate 	int i;
6170Sstevel@tonic-gate 	ushort_t maskval;
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate 	for (i = 500000; i; i--) {
6200Sstevel@tonic-gate 		maskval = inb(port) & mask;
6210Sstevel@tonic-gate 		if (((maskval & onbits) == onbits) &&
6225084Sjohnlev 		    ((maskval & offbits) == 0))
6230Sstevel@tonic-gate 			return (0);
6240Sstevel@tonic-gate 		drv_usecwait(10);
6250Sstevel@tonic-gate 	}
6260Sstevel@tonic-gate 	return (1);
6270Sstevel@tonic-gate }
6280Sstevel@tonic-gate 
6290Sstevel@tonic-gate /*
6300Sstevel@tonic-gate  * set_idle_cpu is called from idle() when a CPU becomes idle.
6310Sstevel@tonic-gate  */
6320Sstevel@tonic-gate /*LINTED: static unused */
6330Sstevel@tonic-gate static uint_t last_idle_cpu;
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate /*ARGSUSED*/
6360Sstevel@tonic-gate void
6370Sstevel@tonic-gate set_idle_cpu(int cpun)
6380Sstevel@tonic-gate {
6390Sstevel@tonic-gate 	last_idle_cpu = cpun;
6400Sstevel@tonic-gate 	(*psm_set_idle_cpuf)(cpun);
6410Sstevel@tonic-gate }
6420Sstevel@tonic-gate 
6430Sstevel@tonic-gate /*
6440Sstevel@tonic-gate  * unset_idle_cpu is called from idle() when a CPU is no longer idle.
6450Sstevel@tonic-gate  */
6460Sstevel@tonic-gate /*ARGSUSED*/
6470Sstevel@tonic-gate void
6480Sstevel@tonic-gate unset_idle_cpu(int cpun)
6490Sstevel@tonic-gate {
6500Sstevel@tonic-gate 	(*psm_unset_idle_cpuf)(cpun);
6510Sstevel@tonic-gate }
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate /*
6540Sstevel@tonic-gate  * This routine is almost correct now, but not quite.  It still needs the
6550Sstevel@tonic-gate  * equivalent concept of "hres_last_tick", just like on the sparc side.
6560Sstevel@tonic-gate  * The idea is to take a snapshot of the hi-res timer while doing the
6570Sstevel@tonic-gate  * hrestime_adj updates under hres_lock in locore, so that the small
6580Sstevel@tonic-gate  * interval between interrupt assertion and interrupt processing is
6590Sstevel@tonic-gate  * accounted for correctly.  Once we have this, the code below should
6600Sstevel@tonic-gate  * be modified to subtract off hres_last_tick rather than hrtime_base.
6610Sstevel@tonic-gate  *
6620Sstevel@tonic-gate  * I'd have done this myself, but I don't have source to all of the
6630Sstevel@tonic-gate  * vendor-specific hi-res timer routines (grrr...).  The generic hook I
6640Sstevel@tonic-gate  * need is something like "gethrtime_unlocked()", which would be just like
6650Sstevel@tonic-gate  * gethrtime() but would assume that you're already holding CLOCK_LOCK().
6660Sstevel@tonic-gate  * This is what the GET_HRTIME() macro is for on sparc (although it also
6670Sstevel@tonic-gate  * serves the function of making time available without a function call
6680Sstevel@tonic-gate  * so you don't take a register window overflow while traps are disabled).
6690Sstevel@tonic-gate  */
6700Sstevel@tonic-gate void
6710Sstevel@tonic-gate pc_gethrestime(timestruc_t *tp)
6720Sstevel@tonic-gate {
6730Sstevel@tonic-gate 	int lock_prev;
6740Sstevel@tonic-gate 	timestruc_t now;
6750Sstevel@tonic-gate 	int nslt;		/* nsec since last tick */
6760Sstevel@tonic-gate 	int adj;		/* amount of adjustment to apply */
6770Sstevel@tonic-gate 
6780Sstevel@tonic-gate loop:
6790Sstevel@tonic-gate 	lock_prev = hres_lock;
6800Sstevel@tonic-gate 	now = hrestime;
6810Sstevel@tonic-gate 	nslt = (int)(gethrtime() - hres_last_tick);
6820Sstevel@tonic-gate 	if (nslt < 0) {
6830Sstevel@tonic-gate 		/*
6840Sstevel@tonic-gate 		 * nslt < 0 means a tick came between sampling
6850Sstevel@tonic-gate 		 * gethrtime() and hres_last_tick; restart the loop
6860Sstevel@tonic-gate 		 */
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate 		goto loop;
6890Sstevel@tonic-gate 	}
6900Sstevel@tonic-gate 	now.tv_nsec += nslt;
6910Sstevel@tonic-gate 	if (hrestime_adj != 0) {
6920Sstevel@tonic-gate 		if (hrestime_adj > 0) {
6930Sstevel@tonic-gate 			adj = (nslt >> ADJ_SHIFT);
6940Sstevel@tonic-gate 			if (adj > hrestime_adj)
6950Sstevel@tonic-gate 				adj = (int)hrestime_adj;
6960Sstevel@tonic-gate 		} else {
6970Sstevel@tonic-gate 			adj = -(nslt >> ADJ_SHIFT);
6980Sstevel@tonic-gate 			if (adj < hrestime_adj)
6990Sstevel@tonic-gate 				adj = (int)hrestime_adj;
7000Sstevel@tonic-gate 		}
7010Sstevel@tonic-gate 		now.tv_nsec += adj;
7020Sstevel@tonic-gate 	}
7030Sstevel@tonic-gate 	while ((unsigned long)now.tv_nsec >= NANOSEC) {
7040Sstevel@tonic-gate 
7050Sstevel@tonic-gate 		/*
7060Sstevel@tonic-gate 		 * We might have a large adjustment or have been in the
7070Sstevel@tonic-gate 		 * debugger for a long time; take care of (at most) four
7080Sstevel@tonic-gate 		 * of those missed seconds (tv_nsec is 32 bits, so
7090Sstevel@tonic-gate 		 * anything >4s will be wrapping around).  However,
7100Sstevel@tonic-gate 		 * anything more than 2 seconds out of sync will trigger
7110Sstevel@tonic-gate 		 * timedelta from clock() to go correct the time anyway,
7120Sstevel@tonic-gate 		 * so do what we can, and let the big crowbar do the
7130Sstevel@tonic-gate 		 * rest.  A similar correction while loop exists inside
7140Sstevel@tonic-gate 		 * hres_tick(); in all cases we'd like tv_nsec to
7150Sstevel@tonic-gate 		 * satisfy 0 <= tv_nsec < NANOSEC to avoid confusing
7160Sstevel@tonic-gate 		 * user processes, but if tv_sec's a little behind for a
7170Sstevel@tonic-gate 		 * little while, that's OK; time still monotonically
7180Sstevel@tonic-gate 		 * increases.
7190Sstevel@tonic-gate 		 */
7200Sstevel@tonic-gate 
7210Sstevel@tonic-gate 		now.tv_nsec -= NANOSEC;
7220Sstevel@tonic-gate 		now.tv_sec++;
7230Sstevel@tonic-gate 	}
7240Sstevel@tonic-gate 	if ((hres_lock & ~1) != lock_prev)
7250Sstevel@tonic-gate 		goto loop;
7260Sstevel@tonic-gate 
7270Sstevel@tonic-gate 	*tp = now;
7280Sstevel@tonic-gate }
7290Sstevel@tonic-gate 
7300Sstevel@tonic-gate void
7310Sstevel@tonic-gate gethrestime_lasttick(timespec_t *tp)
7320Sstevel@tonic-gate {
7330Sstevel@tonic-gate 	int s;
7340Sstevel@tonic-gate 
7350Sstevel@tonic-gate 	s = hr_clock_lock();
7360Sstevel@tonic-gate 	*tp = hrestime;
7370Sstevel@tonic-gate 	hr_clock_unlock(s);
7380Sstevel@tonic-gate }
7390Sstevel@tonic-gate 
7400Sstevel@tonic-gate time_t
7410Sstevel@tonic-gate gethrestime_sec(void)
7420Sstevel@tonic-gate {
7430Sstevel@tonic-gate 	timestruc_t now;
7440Sstevel@tonic-gate 
7450Sstevel@tonic-gate 	gethrestime(&now);
7460Sstevel@tonic-gate 	return (now.tv_sec);
7470Sstevel@tonic-gate }
7480Sstevel@tonic-gate 
7490Sstevel@tonic-gate /*
7500Sstevel@tonic-gate  * Initialize a kernel thread's stack
7510Sstevel@tonic-gate  */
7520Sstevel@tonic-gate 
7530Sstevel@tonic-gate caddr_t
7540Sstevel@tonic-gate thread_stk_init(caddr_t stk)
7550Sstevel@tonic-gate {
7560Sstevel@tonic-gate 	ASSERT(((uintptr_t)stk & (STACK_ALIGN - 1)) == 0);
7570Sstevel@tonic-gate 	return (stk - SA(MINFRAME));
7580Sstevel@tonic-gate }
7590Sstevel@tonic-gate 
7600Sstevel@tonic-gate /*
7610Sstevel@tonic-gate  * Initialize lwp's kernel stack.
7620Sstevel@tonic-gate  */
7630Sstevel@tonic-gate 
7640Sstevel@tonic-gate #ifdef TRAPTRACE
7650Sstevel@tonic-gate /*
7660Sstevel@tonic-gate  * There's a tricky interdependency here between use of sysenter and
7670Sstevel@tonic-gate  * TRAPTRACE which needs recording to avoid future confusion (this is
7680Sstevel@tonic-gate  * about the third time I've re-figured this out ..)
7690Sstevel@tonic-gate  *
7700Sstevel@tonic-gate  * Here's how debugging lcall works with TRAPTRACE.
7710Sstevel@tonic-gate  *
7720Sstevel@tonic-gate  * 1 We're in userland with a breakpoint on the lcall instruction.
7730Sstevel@tonic-gate  * 2 We execute the instruction - the instruction pushes the userland
7740Sstevel@tonic-gate  *   %ss, %esp, %efl, %cs, %eip on the stack and zips into the kernel
7750Sstevel@tonic-gate  *   via the call gate.
7760Sstevel@tonic-gate  * 3 The hardware raises a debug trap in kernel mode, the hardware
7770Sstevel@tonic-gate  *   pushes %efl, %cs, %eip and gets to dbgtrap via the idt.
7780Sstevel@tonic-gate  * 4 dbgtrap pushes the error code and trapno and calls cmntrap
7790Sstevel@tonic-gate  * 5 cmntrap finishes building a trap frame
7800Sstevel@tonic-gate  * 6 The TRACE_REGS macros in cmntrap copy a REGSIZE worth chunk
7810Sstevel@tonic-gate  *   off the stack into the traptrace buffer.
7820Sstevel@tonic-gate  *
7830Sstevel@tonic-gate  * This means that the traptrace buffer contains the wrong values in
7840Sstevel@tonic-gate  * %esp and %ss, but everything else in there is correct.
7850Sstevel@tonic-gate  *
7860Sstevel@tonic-gate  * Here's how debugging sysenter works with TRAPTRACE.
7870Sstevel@tonic-gate  *
7880Sstevel@tonic-gate  * a We're in userland with a breakpoint on the sysenter instruction.
7890Sstevel@tonic-gate  * b We execute the instruction - the instruction pushes -nothing-
7900Sstevel@tonic-gate  *   on the stack, but sets %cs, %eip, %ss, %esp to prearranged
7910Sstevel@tonic-gate  *   values to take us to sys_sysenter, at the top of the lwp's
7920Sstevel@tonic-gate  *   stack.
7930Sstevel@tonic-gate  * c goto 3
7940Sstevel@tonic-gate  *
7950Sstevel@tonic-gate  * At this point, because we got into the kernel without the requisite
7960Sstevel@tonic-gate  * five pushes on the stack, if we didn't make extra room, we'd
7970Sstevel@tonic-gate  * end up with the TRACE_REGS macro fetching the saved %ss and %esp
7980Sstevel@tonic-gate  * values from negative (unmapped) stack addresses -- which really bites.
7990Sstevel@tonic-gate  * That's why we do the '-= 8' below.
8000Sstevel@tonic-gate  *
8010Sstevel@tonic-gate  * XXX	Note that reading "up" lwp0's stack works because t0 is declared
8020Sstevel@tonic-gate  *	right next to t0stack in locore.s
8030Sstevel@tonic-gate  */
8040Sstevel@tonic-gate #endif
8050Sstevel@tonic-gate 
8060Sstevel@tonic-gate caddr_t
8070Sstevel@tonic-gate lwp_stk_init(klwp_t *lwp, caddr_t stk)
8080Sstevel@tonic-gate {
8090Sstevel@tonic-gate 	caddr_t oldstk;
8100Sstevel@tonic-gate 	struct pcb *pcb = &lwp->lwp_pcb;
8110Sstevel@tonic-gate 
8120Sstevel@tonic-gate 	oldstk = stk;
8130Sstevel@tonic-gate 	stk -= SA(sizeof (struct regs) + SA(MINFRAME));
8140Sstevel@tonic-gate #ifdef TRAPTRACE
8150Sstevel@tonic-gate 	stk -= 2 * sizeof (greg_t); /* space for phony %ss:%sp (see above) */
8160Sstevel@tonic-gate #endif
8170Sstevel@tonic-gate 	stk = (caddr_t)((uintptr_t)stk & ~(STACK_ALIGN - 1ul));
8180Sstevel@tonic-gate 	bzero(stk, oldstk - stk);
8190Sstevel@tonic-gate 	lwp->lwp_regs = (void *)(stk + SA(MINFRAME));
8200Sstevel@tonic-gate 
8210Sstevel@tonic-gate 	/*
8220Sstevel@tonic-gate 	 * Arrange that the virtualized %fs and %gs GDT descriptors
8230Sstevel@tonic-gate 	 * have a well-defined initial state (present, ring 3
8240Sstevel@tonic-gate 	 * and of type data).
8250Sstevel@tonic-gate 	 */
8260Sstevel@tonic-gate #if defined(__amd64)
8270Sstevel@tonic-gate 	if (lwp_getdatamodel(lwp) == DATAMODEL_NATIVE)
8280Sstevel@tonic-gate 		pcb->pcb_fsdesc = pcb->pcb_gsdesc = zero_udesc;
8290Sstevel@tonic-gate 	else
8300Sstevel@tonic-gate 		pcb->pcb_fsdesc = pcb->pcb_gsdesc = zero_u32desc;
8310Sstevel@tonic-gate #elif defined(__i386)
8320Sstevel@tonic-gate 	pcb->pcb_fsdesc = pcb->pcb_gsdesc = zero_udesc;
8330Sstevel@tonic-gate #endif	/* __i386 */
8340Sstevel@tonic-gate 	lwp_installctx(lwp);
8350Sstevel@tonic-gate 	return (stk);
8360Sstevel@tonic-gate }
8370Sstevel@tonic-gate 
8380Sstevel@tonic-gate /*ARGSUSED*/
8390Sstevel@tonic-gate void
8400Sstevel@tonic-gate lwp_stk_fini(klwp_t *lwp)
8410Sstevel@tonic-gate {}
8420Sstevel@tonic-gate 
8430Sstevel@tonic-gate /*
8441389Sdmick  * If we're not the panic CPU, we wait in panic_idle for reboot.
8450Sstevel@tonic-gate  */
8460Sstevel@tonic-gate static void
8470Sstevel@tonic-gate panic_idle(void)
8480Sstevel@tonic-gate {
8490Sstevel@tonic-gate 	splx(ipltospl(CLOCK_LEVEL));
8500Sstevel@tonic-gate 	(void) setjmp(&curthread->t_pcb);
8510Sstevel@tonic-gate 
8523446Smrj 	for (;;)
8533446Smrj 		;
8540Sstevel@tonic-gate }
8550Sstevel@tonic-gate 
8560Sstevel@tonic-gate /*
8570Sstevel@tonic-gate  * Stop the other CPUs by cross-calling them and forcing them to enter
8580Sstevel@tonic-gate  * the panic_idle() loop above.
8590Sstevel@tonic-gate  */
8600Sstevel@tonic-gate /*ARGSUSED*/
8610Sstevel@tonic-gate void
8620Sstevel@tonic-gate panic_stopcpus(cpu_t *cp, kthread_t *t, int spl)
8630Sstevel@tonic-gate {
8640Sstevel@tonic-gate 	processorid_t i;
8650Sstevel@tonic-gate 	cpuset_t xcset;
8660Sstevel@tonic-gate 
8675084Sjohnlev 	/*
8685084Sjohnlev 	 * In the case of a Xen panic, the hypervisor has already stopped
8695084Sjohnlev 	 * all of the CPUs.
8705084Sjohnlev 	 */
8715084Sjohnlev 	if (!IN_XPV_PANIC()) {
8725084Sjohnlev 		(void) splzs();
8730Sstevel@tonic-gate 
8745084Sjohnlev 		CPUSET_ALL_BUT(xcset, cp->cpu_id);
8755084Sjohnlev 		xc_trycall(NULL, NULL, NULL, xcset, (int (*)())panic_idle);
8765084Sjohnlev 	}
8770Sstevel@tonic-gate 
8780Sstevel@tonic-gate 	for (i = 0; i < NCPU; i++) {
8790Sstevel@tonic-gate 		if (i != cp->cpu_id && cpu[i] != NULL &&
8800Sstevel@tonic-gate 		    (cpu[i]->cpu_flags & CPU_EXISTS))
8810Sstevel@tonic-gate 			cpu[i]->cpu_flags |= CPU_QUIESCED;
8820Sstevel@tonic-gate 	}
8830Sstevel@tonic-gate }
8840Sstevel@tonic-gate 
8850Sstevel@tonic-gate /*
8860Sstevel@tonic-gate  * Platform callback following each entry to panicsys().
8870Sstevel@tonic-gate  */
8880Sstevel@tonic-gate /*ARGSUSED*/
8890Sstevel@tonic-gate void
8900Sstevel@tonic-gate panic_enter_hw(int spl)
8910Sstevel@tonic-gate {
8920Sstevel@tonic-gate 	/* Nothing to do here */
8930Sstevel@tonic-gate }
8940Sstevel@tonic-gate 
8950Sstevel@tonic-gate /*
8960Sstevel@tonic-gate  * Platform-specific code to execute after panicstr is set: we invoke
8970Sstevel@tonic-gate  * the PSM entry point to indicate that a panic has occurred.
8980Sstevel@tonic-gate  */
8990Sstevel@tonic-gate /*ARGSUSED*/
9000Sstevel@tonic-gate void
9010Sstevel@tonic-gate panic_quiesce_hw(panic_data_t *pdp)
9020Sstevel@tonic-gate {
9030Sstevel@tonic-gate 	psm_notifyf(PSM_PANIC_ENTER);
9040Sstevel@tonic-gate 
9057532SSean.Ye@Sun.COM 	cmi_panic_callback();
9067532SSean.Ye@Sun.COM 
9070Sstevel@tonic-gate #ifdef	TRAPTRACE
9080Sstevel@tonic-gate 	/*
9090Sstevel@tonic-gate 	 * Turn off TRAPTRACE
9100Sstevel@tonic-gate 	 */
9110Sstevel@tonic-gate 	TRAPTRACE_FREEZE;
9120Sstevel@tonic-gate #endif	/* TRAPTRACE */
9130Sstevel@tonic-gate }
9140Sstevel@tonic-gate 
9150Sstevel@tonic-gate /*
9160Sstevel@tonic-gate  * Platform callback prior to writing crash dump.
9170Sstevel@tonic-gate  */
9180Sstevel@tonic-gate /*ARGSUSED*/
9190Sstevel@tonic-gate void
9200Sstevel@tonic-gate panic_dump_hw(int spl)
9210Sstevel@tonic-gate {
9220Sstevel@tonic-gate 	/* Nothing to do here */
9230Sstevel@tonic-gate }
9240Sstevel@tonic-gate 
9255084Sjohnlev void *
9265084Sjohnlev plat_traceback(void *fpreg)
9275084Sjohnlev {
9285084Sjohnlev #ifdef __xpv
9295084Sjohnlev 	if (IN_XPV_PANIC())
9305084Sjohnlev 		return (xpv_traceback(fpreg));
9315084Sjohnlev #endif
9325084Sjohnlev 	return (fpreg);
9335084Sjohnlev }
9345084Sjohnlev 
9350Sstevel@tonic-gate /*ARGSUSED*/
9360Sstevel@tonic-gate void
9370Sstevel@tonic-gate plat_tod_fault(enum tod_fault_type tod_bad)
9383446Smrj {}
9390Sstevel@tonic-gate 
9400Sstevel@tonic-gate /*ARGSUSED*/
9410Sstevel@tonic-gate int
9420Sstevel@tonic-gate blacklist(int cmd, const char *scheme, nvlist_t *fmri, const char *class)
9430Sstevel@tonic-gate {
9440Sstevel@tonic-gate 	return (ENOTSUP);
9450Sstevel@tonic-gate }
9460Sstevel@tonic-gate 
9470Sstevel@tonic-gate /*
9480Sstevel@tonic-gate  * The underlying console output routines are protected by raising IPL in case
9490Sstevel@tonic-gate  * we are still calling into the early boot services.  Once we start calling
9500Sstevel@tonic-gate  * the kernel console emulator, it will disable interrupts completely during
9510Sstevel@tonic-gate  * character rendering (see sysp_putchar, for example).  Refer to the comments
9520Sstevel@tonic-gate  * and code in common/os/console.c for more information on these callbacks.
9530Sstevel@tonic-gate  */
9540Sstevel@tonic-gate /*ARGSUSED*/
9550Sstevel@tonic-gate int
9560Sstevel@tonic-gate console_enter(int busy)
9570Sstevel@tonic-gate {
9580Sstevel@tonic-gate 	return (splzs());
9590Sstevel@tonic-gate }
9600Sstevel@tonic-gate 
9610Sstevel@tonic-gate /*ARGSUSED*/
9620Sstevel@tonic-gate void
9630Sstevel@tonic-gate console_exit(int busy, int spl)
9640Sstevel@tonic-gate {
9650Sstevel@tonic-gate 	splx(spl);
9660Sstevel@tonic-gate }
9670Sstevel@tonic-gate 
9680Sstevel@tonic-gate /*
9690Sstevel@tonic-gate  * Allocate a region of virtual address space, unmapped.
9700Sstevel@tonic-gate  * Stubbed out except on sparc, at least for now.
9710Sstevel@tonic-gate  */
9720Sstevel@tonic-gate /*ARGSUSED*/
9730Sstevel@tonic-gate void *
9740Sstevel@tonic-gate boot_virt_alloc(void *addr, size_t size)
9750Sstevel@tonic-gate {
9760Sstevel@tonic-gate 	return (addr);
9770Sstevel@tonic-gate }
9780Sstevel@tonic-gate 
9790Sstevel@tonic-gate volatile unsigned long	tenmicrodata;
9800Sstevel@tonic-gate 
9810Sstevel@tonic-gate void
9820Sstevel@tonic-gate tenmicrosec(void)
9830Sstevel@tonic-gate {
9845084Sjohnlev 	extern int gethrtime_hires;
9850Sstevel@tonic-gate 
9865084Sjohnlev 	if (gethrtime_hires) {
9870Sstevel@tonic-gate 		hrtime_t start, end;
9880Sstevel@tonic-gate 		start = end =  gethrtime();
9890Sstevel@tonic-gate 		while ((end - start) < (10 * (NANOSEC / MICROSEC))) {
9900Sstevel@tonic-gate 			SMT_PAUSE();
9910Sstevel@tonic-gate 			end = gethrtime();
9920Sstevel@tonic-gate 		}
9930Sstevel@tonic-gate 	} else {
9945084Sjohnlev #if defined(__xpv)
9955084Sjohnlev 		hrtime_t newtime;
9965084Sjohnlev 
9975084Sjohnlev 		newtime = xpv_gethrtime() + 10000; /* now + 10 us */
9985084Sjohnlev 		while (xpv_gethrtime() < newtime)
9995084Sjohnlev 			SMT_PAUSE();
10005084Sjohnlev #else	/* __xpv */
10013446Smrj 		int i;
10023446Smrj 
10030Sstevel@tonic-gate 		/*
10040Sstevel@tonic-gate 		 * Artificial loop to induce delay.
10050Sstevel@tonic-gate 		 */
10060Sstevel@tonic-gate 		for (i = 0; i < microdata; i++)
10070Sstevel@tonic-gate 			tenmicrodata = microdata;
10085084Sjohnlev #endif	/* __xpv */
10090Sstevel@tonic-gate 	}
10100Sstevel@tonic-gate }
1011590Sesolom 
1012590Sesolom /*
1013590Sesolom  * get_cpu_mstate() is passed an array of timestamps, NCMSTATES
1014590Sesolom  * long, and it fills in the array with the time spent on cpu in
1015590Sesolom  * each of the mstates, where time is returned in nsec.
1016590Sesolom  *
1017590Sesolom  * No guarantee is made that the returned values in times[] will
1018590Sesolom  * monotonically increase on sequential calls, although this will
1019590Sesolom  * be true in the long run. Any such guarantee must be handled by
1020590Sesolom  * the caller, if needed. This can happen if we fail to account
1021590Sesolom  * for elapsed time due to a generation counter conflict, yet we
1022590Sesolom  * did account for it on a prior call (see below).
1023590Sesolom  *
1024590Sesolom  * The complication is that the cpu in question may be updating
1025590Sesolom  * its microstate at the same time that we are reading it.
1026590Sesolom  * Because the microstate is only updated when the CPU's state
1027590Sesolom  * changes, the values in cpu_intracct[] can be indefinitely out
1028590Sesolom  * of date. To determine true current values, it is necessary to
1029590Sesolom  * compare the current time with cpu_mstate_start, and add the
1030590Sesolom  * difference to times[cpu_mstate].
1031590Sesolom  *
1032590Sesolom  * This can be a problem if those values are changing out from
1033590Sesolom  * under us. Because the code path in new_cpu_mstate() is
1034590Sesolom  * performance critical, we have not added a lock to it. Instead,
1035590Sesolom  * we have added a generation counter. Before beginning
1036590Sesolom  * modifications, the counter is set to 0. After modifications,
1037590Sesolom  * it is set to the old value plus one.
1038590Sesolom  *
1039590Sesolom  * get_cpu_mstate() will not consider the values of cpu_mstate
1040590Sesolom  * and cpu_mstate_start to be usable unless the value of
1041590Sesolom  * cpu_mstate_gen is both non-zero and unchanged, both before and
1042590Sesolom  * after reading the mstate information. Note that we must
1043590Sesolom  * protect against out-of-order loads around accesses to the
1044590Sesolom  * generation counter. Also, this is a best effort approach in
1045590Sesolom  * that we do not retry should the counter be found to have
1046590Sesolom  * changed.
1047590Sesolom  *
1048590Sesolom  * cpu_intracct[] is used to identify time spent in each CPU
1049590Sesolom  * mstate while handling interrupts. Such time should be reported
1050590Sesolom  * against system time, and so is subtracted out from its
1051590Sesolom  * corresponding cpu_acct[] time and added to
1052590Sesolom  * cpu_acct[CMS_SYSTEM].
1053590Sesolom  */
1054590Sesolom 
1055590Sesolom void
1056590Sesolom get_cpu_mstate(cpu_t *cpu, hrtime_t *times)
1057590Sesolom {
1058590Sesolom 	int i;
1059590Sesolom 	hrtime_t now, start;
1060590Sesolom 	uint16_t gen;
1061590Sesolom 	uint16_t state;
1062590Sesolom 	hrtime_t intracct[NCMSTATES];
1063590Sesolom 
1064590Sesolom 	/*
1065590Sesolom 	 * Load all volatile state under the protection of membar.
1066590Sesolom 	 * cpu_acct[cpu_mstate] must be loaded to avoid double counting
1067590Sesolom 	 * of (now - cpu_mstate_start) by a change in CPU mstate that
1068590Sesolom 	 * arrives after we make our last check of cpu_mstate_gen.
1069590Sesolom 	 */
1070590Sesolom 
1071590Sesolom 	now = gethrtime_unscaled();
1072590Sesolom 	gen = cpu->cpu_mstate_gen;
1073590Sesolom 
1074590Sesolom 	membar_consumer();	/* guarantee load ordering */
1075590Sesolom 	start = cpu->cpu_mstate_start;
1076590Sesolom 	state = cpu->cpu_mstate;
1077590Sesolom 	for (i = 0; i < NCMSTATES; i++) {
1078590Sesolom 		intracct[i] = cpu->cpu_intracct[i];
1079590Sesolom 		times[i] = cpu->cpu_acct[i];
1080590Sesolom 	}
1081590Sesolom 	membar_consumer();	/* guarantee load ordering */
1082590Sesolom 
1083590Sesolom 	if (gen != 0 && gen == cpu->cpu_mstate_gen && now > start)
1084590Sesolom 		times[state] += now - start;
1085590Sesolom 
1086590Sesolom 	for (i = 0; i < NCMSTATES; i++) {
1087590Sesolom 		if (i == CMS_SYSTEM)
1088590Sesolom 			continue;
1089590Sesolom 		times[i] -= intracct[i];
1090590Sesolom 		if (times[i] < 0) {
1091590Sesolom 			intracct[i] += times[i];
1092590Sesolom 			times[i] = 0;
1093590Sesolom 		}
1094590Sesolom 		times[CMS_SYSTEM] += intracct[i];
1095590Sesolom 		scalehrtime(&times[i]);
1096590Sesolom 	}
1097590Sesolom 	scalehrtime(&times[CMS_SYSTEM]);
1098590Sesolom }
10993446Smrj 
11003446Smrj /*
11013446Smrj  * This is a version of the rdmsr instruction that allows
11023446Smrj  * an error code to be returned in the case of failure.
11033446Smrj  */
11043446Smrj int
11053446Smrj checked_rdmsr(uint_t msr, uint64_t *value)
11063446Smrj {
11073446Smrj 	if ((x86_feature & X86_MSR) == 0)
11083446Smrj 		return (ENOTSUP);
11093446Smrj 	*value = rdmsr(msr);
11103446Smrj 	return (0);
11113446Smrj }
11123446Smrj 
11133446Smrj /*
11143446Smrj  * This is a version of the wrmsr instruction that allows
11153446Smrj  * an error code to be returned in the case of failure.
11163446Smrj  */
11173446Smrj int
11183446Smrj checked_wrmsr(uint_t msr, uint64_t value)
11193446Smrj {
11203446Smrj 	if ((x86_feature & X86_MSR) == 0)
11213446Smrj 		return (ENOTSUP);
11223446Smrj 	wrmsr(msr, value);
11233446Smrj 	return (0);
11243446Smrj }
11253446Smrj 
11263446Smrj /*
11275084Sjohnlev  * The mem driver's usual method of using hat_devload() to establish a
11285084Sjohnlev  * temporary mapping will not work for foreign pages mapped into this
11295084Sjohnlev  * domain or for the special hypervisor-provided pages.  For the foreign
11305084Sjohnlev  * pages, we often don't know which domain owns them, so we can't ask the
11315084Sjohnlev  * hypervisor to set up a new mapping.  For the other pages, we don't have
11325084Sjohnlev  * a pfn, so we can't create a new PTE.  For these special cases, we do a
11335084Sjohnlev  * direct uiomove() from the existing kernel virtual address.
11343446Smrj  */
11353446Smrj /*ARGSUSED*/
11363446Smrj int
11375084Sjohnlev plat_mem_do_mmio(struct uio *uio, enum uio_rw rw)
11385084Sjohnlev {
11395084Sjohnlev #if defined(__xpv)
11405084Sjohnlev 	void *va = (void *)(uintptr_t)uio->uio_loffset;
11415084Sjohnlev 	off_t pageoff = uio->uio_loffset & PAGEOFFSET;
11425084Sjohnlev 	size_t nbytes = MIN((size_t)(PAGESIZE - pageoff),
11435084Sjohnlev 	    (size_t)uio->uio_iov->iov_len);
11445084Sjohnlev 
11455084Sjohnlev 	if ((rw == UIO_READ &&
11465084Sjohnlev 	    (va == HYPERVISOR_shared_info || va == xen_info)) ||
11475084Sjohnlev 	    (pfn_is_foreign(hat_getpfnum(kas.a_hat, va))))
11485084Sjohnlev 		return (uiomove(va, nbytes, rw, uio));
11495084Sjohnlev #endif
11505084Sjohnlev 	return (ENOTSUP);
11515084Sjohnlev }
11525084Sjohnlev 
11535084Sjohnlev pgcnt_t
11545084Sjohnlev num_phys_pages()
11553446Smrj {
11565084Sjohnlev 	pgcnt_t npages = 0;
11575084Sjohnlev 	struct memlist *mp;
11585084Sjohnlev 
11595084Sjohnlev #if defined(__xpv)
11605084Sjohnlev 	if (DOMAIN_IS_INITDOMAIN(xen_info)) {
11615084Sjohnlev 		xen_sysctl_t op;
11625084Sjohnlev 
11635084Sjohnlev 		op.cmd = XEN_SYSCTL_physinfo;
11645084Sjohnlev 		op.interface_version = XEN_SYSCTL_INTERFACE_VERSION;
11655084Sjohnlev 		if (HYPERVISOR_sysctl(&op) != 0)
11665084Sjohnlev 			panic("physinfo op refused");
11675084Sjohnlev 
11685084Sjohnlev 		return ((pgcnt_t)op.u.physinfo.total_pages);
11695084Sjohnlev 	}
11705084Sjohnlev #endif /* __xpv */
11715084Sjohnlev 
11725084Sjohnlev 	for (mp = phys_install; mp != NULL; mp = mp->next)
11735084Sjohnlev 		npages += mp->size >> PAGESHIFT;
11745084Sjohnlev 
11755084Sjohnlev 	return (npages);
11763446Smrj }
11773446Smrj 
11783446Smrj int
11793446Smrj dump_plat_addr()
11803446Smrj {
11815084Sjohnlev #ifdef __xpv
11825084Sjohnlev 	pfn_t pfn = mmu_btop(xen_info->shared_info) | PFN_IS_FOREIGN_MFN;
11835084Sjohnlev 	mem_vtop_t mem_vtop;
11845084Sjohnlev 	int cnt;
11855084Sjohnlev 
11865084Sjohnlev 	/*
11875084Sjohnlev 	 * On the hypervisor, we want to dump the page with shared_info on it.
11885084Sjohnlev 	 */
11895084Sjohnlev 	if (!IN_XPV_PANIC()) {
11905084Sjohnlev 		mem_vtop.m_as = &kas;
11915084Sjohnlev 		mem_vtop.m_va = HYPERVISOR_shared_info;
11925084Sjohnlev 		mem_vtop.m_pfn = pfn;
11935084Sjohnlev 		dumpvp_write(&mem_vtop, sizeof (mem_vtop_t));
11945084Sjohnlev 		cnt = 1;
11955084Sjohnlev 	} else {
11965084Sjohnlev 		cnt = dump_xpv_addr();
11975084Sjohnlev 	}
11985084Sjohnlev 	return (cnt);
11995084Sjohnlev #else
12003446Smrj 	return (0);
12015084Sjohnlev #endif
12023446Smrj }
12033446Smrj 
12043446Smrj void
12053446Smrj dump_plat_pfn()
12063446Smrj {
12075084Sjohnlev #ifdef __xpv
12085084Sjohnlev 	pfn_t pfn = mmu_btop(xen_info->shared_info) | PFN_IS_FOREIGN_MFN;
12095084Sjohnlev 
12105084Sjohnlev 	if (!IN_XPV_PANIC())
12115084Sjohnlev 		dumpvp_write(&pfn, sizeof (pfn));
12125084Sjohnlev 	else
12135084Sjohnlev 		dump_xpv_pfn();
12145084Sjohnlev #endif
12153446Smrj }
12163446Smrj 
12173446Smrj /*ARGSUSED*/
12183446Smrj int
12193446Smrj dump_plat_data(void *dump_cbuf)
12203446Smrj {
12215084Sjohnlev #ifdef __xpv
12225084Sjohnlev 	uint32_t csize;
12235084Sjohnlev 	int cnt;
12245084Sjohnlev 
12255084Sjohnlev 	if (!IN_XPV_PANIC()) {
12265084Sjohnlev 		csize = (uint32_t)compress(HYPERVISOR_shared_info, dump_cbuf,
12275084Sjohnlev 		    PAGESIZE);
12285084Sjohnlev 		dumpvp_write(&csize, sizeof (uint32_t));
12295084Sjohnlev 		dumpvp_write(dump_cbuf, csize);
12305084Sjohnlev 		cnt = 1;
12315084Sjohnlev 	} else {
12325084Sjohnlev 		cnt = dump_xpv_data(dump_cbuf);
12335084Sjohnlev 	}
12345084Sjohnlev 	return (cnt);
12355084Sjohnlev #else
12363446Smrj 	return (0);
12375084Sjohnlev #endif
12383446Smrj }
12393939Ssethg 
12403939Ssethg /*
12413939Ssethg  * Calculates a linear address, given the CS selector and PC values
12423939Ssethg  * by looking up the %cs selector process's LDT or the CPU's GDT.
12433939Ssethg  * proc->p_ldtlock must be held across this call.
12443939Ssethg  */
12453939Ssethg int
12463939Ssethg linear_pc(struct regs *rp, proc_t *p, caddr_t *linearp)
12473939Ssethg {
12483939Ssethg 	user_desc_t	*descrp;
12493939Ssethg 	caddr_t		baseaddr;
12503939Ssethg 	uint16_t	idx = SELTOIDX(rp->r_cs);
12513939Ssethg 
12523939Ssethg 	ASSERT(rp->r_cs <= 0xFFFF);
12533939Ssethg 	ASSERT(MUTEX_HELD(&p->p_ldtlock));
12543939Ssethg 
12553939Ssethg 	if (SELISLDT(rp->r_cs)) {
12563939Ssethg 		/*
12573939Ssethg 		 * Currently 64 bit processes cannot have private LDTs.
12583939Ssethg 		 */
12593939Ssethg 		ASSERT(p->p_model != DATAMODEL_LP64);
12603939Ssethg 
12613939Ssethg 		if (p->p_ldt == NULL)
12623939Ssethg 			return (-1);
12633939Ssethg 
12643939Ssethg 		descrp = &p->p_ldt[idx];
12653939Ssethg 		baseaddr = (caddr_t)(uintptr_t)USEGD_GETBASE(descrp);
12663939Ssethg 
12673939Ssethg 		/*
12683939Ssethg 		 * Calculate the linear address (wraparound is not only ok,
12693939Ssethg 		 * it's expected behavior).  The cast to uint32_t is because
12703939Ssethg 		 * LDT selectors are only allowed in 32-bit processes.
12713939Ssethg 		 */
12723939Ssethg 		*linearp = (caddr_t)(uintptr_t)(uint32_t)((uintptr_t)baseaddr +
12733939Ssethg 		    rp->r_pc);
12743939Ssethg 	} else {
12753939Ssethg #ifdef DEBUG
12763939Ssethg 		descrp = &CPU->cpu_gdt[idx];
12773939Ssethg 		baseaddr = (caddr_t)(uintptr_t)USEGD_GETBASE(descrp);
12783939Ssethg 		/* GDT-based descriptors' base addresses should always be 0 */
12793939Ssethg 		ASSERT(baseaddr == 0);
12803939Ssethg #endif
12813939Ssethg 		*linearp = (caddr_t)(uintptr_t)rp->r_pc;
12823939Ssethg 	}
12833939Ssethg 
12843939Ssethg 	return (0);
12853939Ssethg }
12863939Ssethg 
12873939Ssethg /*
12883939Ssethg  * The implementation of dtrace_linear_pc is similar to the that of
12893939Ssethg  * linear_pc, above, but here we acquire p_ldtlock before accessing
12903939Ssethg  * p_ldt.  This implementation is used by the pid provider; we prefix
12913939Ssethg  * it with "dtrace_" to avoid inducing spurious tracing events.
12923939Ssethg  */
12933939Ssethg int
12943939Ssethg dtrace_linear_pc(struct regs *rp, proc_t *p, caddr_t *linearp)
12953939Ssethg {
12963939Ssethg 	user_desc_t	*descrp;
12973939Ssethg 	caddr_t		baseaddr;
12983939Ssethg 	uint16_t	idx = SELTOIDX(rp->r_cs);
12993939Ssethg 
13003939Ssethg 	ASSERT(rp->r_cs <= 0xFFFF);
13013939Ssethg 
13023939Ssethg 	if (SELISLDT(rp->r_cs)) {
13033939Ssethg 		/*
13043939Ssethg 		 * Currently 64 bit processes cannot have private LDTs.
13053939Ssethg 		 */
13063939Ssethg 		ASSERT(p->p_model != DATAMODEL_LP64);
13073939Ssethg 
13083939Ssethg 		mutex_enter(&p->p_ldtlock);
13093939Ssethg 		if (p->p_ldt == NULL) {
13103939Ssethg 			mutex_exit(&p->p_ldtlock);
13113939Ssethg 			return (-1);
13123939Ssethg 		}
13133939Ssethg 		descrp = &p->p_ldt[idx];
13143939Ssethg 		baseaddr = (caddr_t)(uintptr_t)USEGD_GETBASE(descrp);
13153939Ssethg 		mutex_exit(&p->p_ldtlock);
13163939Ssethg 
13173939Ssethg 		/*
13183939Ssethg 		 * Calculate the linear address (wraparound is not only ok,
13193939Ssethg 		 * it's expected behavior).  The cast to uint32_t is because
13203939Ssethg 		 * LDT selectors are only allowed in 32-bit processes.
13213939Ssethg 		 */
13223939Ssethg 		*linearp = (caddr_t)(uintptr_t)(uint32_t)((uintptr_t)baseaddr +
13233939Ssethg 		    rp->r_pc);
13243939Ssethg 	} else {
13253939Ssethg #ifdef DEBUG
13263939Ssethg 		descrp = &CPU->cpu_gdt[idx];
13273939Ssethg 		baseaddr = (caddr_t)(uintptr_t)USEGD_GETBASE(descrp);
13283939Ssethg 		/* GDT-based descriptors' base addresses should always be 0 */
13293939Ssethg 		ASSERT(baseaddr == 0);
13303939Ssethg #endif
13313939Ssethg 		*linearp = (caddr_t)(uintptr_t)rp->r_pc;
13323939Ssethg 	}
13333939Ssethg 
13343939Ssethg 	return (0);
13353939Ssethg }
1336