xref: /titanic_52/usr/src/uts/sun4/os/machdep.c (revision eda89462804e5700afce98b28174fa96082df280)
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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2005 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/kstat.h>
317c478bd9Sstevel@tonic-gate #include <sys/param.h>
327c478bd9Sstevel@tonic-gate #include <sys/stack.h>
337c478bd9Sstevel@tonic-gate #include <sys/regset.h>
347c478bd9Sstevel@tonic-gate #include <sys/thread.h>
357c478bd9Sstevel@tonic-gate #include <sys/proc.h>
367c478bd9Sstevel@tonic-gate #include <sys/procfs_isa.h>
377c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
387c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
397c478bd9Sstevel@tonic-gate #include <sys/systm.h>
407c478bd9Sstevel@tonic-gate #include <sys/machpcb.h>
417c478bd9Sstevel@tonic-gate #include <sys/machasi.h>
427c478bd9Sstevel@tonic-gate #include <sys/vis.h>
437c478bd9Sstevel@tonic-gate #include <sys/fpu/fpusystm.h>
447c478bd9Sstevel@tonic-gate #include <sys/cpu_module.h>
457c478bd9Sstevel@tonic-gate #include <sys/privregs.h>
467c478bd9Sstevel@tonic-gate #include <sys/archsystm.h>
477c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
487c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
497c478bd9Sstevel@tonic-gate #include <sys/time.h>
507c478bd9Sstevel@tonic-gate #include <sys/clock.h>
517c478bd9Sstevel@tonic-gate #include <sys/chip.h>
527c478bd9Sstevel@tonic-gate #include <sys/cmp.h>
537c478bd9Sstevel@tonic-gate #include <sys/platform_module.h>
547c478bd9Sstevel@tonic-gate #include <sys/bl.h>
557c478bd9Sstevel@tonic-gate #include <sys/nvpair.h>
567c478bd9Sstevel@tonic-gate #include <sys/kdi_impl.h>
577c478bd9Sstevel@tonic-gate #include <sys/machsystm.h>
587c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
597c478bd9Sstevel@tonic-gate #include <sys/promif.h>
607c478bd9Sstevel@tonic-gate #include <sys/pool_pset.h>
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate int maxphys = MMU_PAGESIZE * 16;	/* 128k */
637c478bd9Sstevel@tonic-gate int klustsize = MMU_PAGESIZE * 16;	/* 128k */
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate /*
667c478bd9Sstevel@tonic-gate  * Initialize kernel thread's stack.
677c478bd9Sstevel@tonic-gate  */
687c478bd9Sstevel@tonic-gate caddr_t
697c478bd9Sstevel@tonic-gate thread_stk_init(caddr_t stk)
707c478bd9Sstevel@tonic-gate {
717c478bd9Sstevel@tonic-gate 	kfpu_t *fp;
727c478bd9Sstevel@tonic-gate 	ulong_t align;
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 	/* allocate extra space for floating point state */
757c478bd9Sstevel@tonic-gate 	stk -= SA(sizeof (kfpu_t) + GSR_SIZE);
767c478bd9Sstevel@tonic-gate 	align = (uintptr_t)stk & 0x3f;
777c478bd9Sstevel@tonic-gate 	stk -= align;		/* force v9_fpu to be 16 byte aligned */
787c478bd9Sstevel@tonic-gate 	fp = (kfpu_t *)stk;
797c478bd9Sstevel@tonic-gate 	fp->fpu_fprs = 0;
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate 	stk -= SA(MINFRAME);
827c478bd9Sstevel@tonic-gate 	return (stk);
837c478bd9Sstevel@tonic-gate }
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate /*
867c478bd9Sstevel@tonic-gate  * Initialize lwp's kernel stack.
877c478bd9Sstevel@tonic-gate  * Note that now that the floating point register save area (kfpu_t)
887c478bd9Sstevel@tonic-gate  * has been broken out from machpcb and aligned on a 64 byte boundary so that
897c478bd9Sstevel@tonic-gate  * we can do block load/stores to/from it, there are a couple of potential
907c478bd9Sstevel@tonic-gate  * optimizations to save stack space. 1. The floating point register save
917c478bd9Sstevel@tonic-gate  * area could be aligned on a 16 byte boundary, and the floating point code
927c478bd9Sstevel@tonic-gate  * changed to (a) check the alignment and (b) use different save/restore
937c478bd9Sstevel@tonic-gate  * macros depending upon the alignment. 2. The lwp_stk_init code below
947c478bd9Sstevel@tonic-gate  * could be changed to calculate if less space would be wasted if machpcb
957c478bd9Sstevel@tonic-gate  * was first instead of second. However there is a REGOFF macro used in
967c478bd9Sstevel@tonic-gate  * locore, syscall_trap, machdep and mlsetup that assumes that the saved
977c478bd9Sstevel@tonic-gate  * register area is a fixed distance from the %sp, and would have to be
987c478bd9Sstevel@tonic-gate  * changed to a pointer or something...JJ said later.
997c478bd9Sstevel@tonic-gate  */
1007c478bd9Sstevel@tonic-gate caddr_t
1017c478bd9Sstevel@tonic-gate lwp_stk_init(klwp_t *lwp, caddr_t stk)
1027c478bd9Sstevel@tonic-gate {
1037c478bd9Sstevel@tonic-gate 	struct machpcb *mpcb;
1047c478bd9Sstevel@tonic-gate 	kfpu_t *fp;
1057c478bd9Sstevel@tonic-gate 	uintptr_t aln;
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	stk -= SA(sizeof (kfpu_t) + GSR_SIZE);
1087c478bd9Sstevel@tonic-gate 	aln = (uintptr_t)stk & 0x3F;
1097c478bd9Sstevel@tonic-gate 	stk -= aln;
1107c478bd9Sstevel@tonic-gate 	fp = (kfpu_t *)stk;
1117c478bd9Sstevel@tonic-gate 	stk -= SA(sizeof (struct machpcb));
1127c478bd9Sstevel@tonic-gate 	mpcb = (struct machpcb *)stk;
1137c478bd9Sstevel@tonic-gate 	bzero(mpcb, sizeof (struct machpcb));
1147c478bd9Sstevel@tonic-gate 	bzero(fp, sizeof (kfpu_t) + GSR_SIZE);
1157c478bd9Sstevel@tonic-gate 	lwp->lwp_regs = (void *)&mpcb->mpcb_regs;
1167c478bd9Sstevel@tonic-gate 	lwp->lwp_fpu = (void *)fp;
1177c478bd9Sstevel@tonic-gate 	mpcb->mpcb_fpu = fp;
1187c478bd9Sstevel@tonic-gate 	mpcb->mpcb_fpu->fpu_q = mpcb->mpcb_fpu_q;
1197c478bd9Sstevel@tonic-gate 	mpcb->mpcb_thread = lwp->lwp_thread;
1207c478bd9Sstevel@tonic-gate 	mpcb->mpcb_wbcnt = 0;
1217c478bd9Sstevel@tonic-gate 	if (lwp->lwp_procp->p_model == DATAMODEL_ILP32) {
1227c478bd9Sstevel@tonic-gate 		mpcb->mpcb_wstate = WSTATE_USER32;
1237c478bd9Sstevel@tonic-gate 		mpcb->mpcb_wbuf = kmem_alloc(MAXWIN * sizeof (struct rwindow32),
1247c478bd9Sstevel@tonic-gate 		    KM_SLEEP);
1257c478bd9Sstevel@tonic-gate 	} else {
1267c478bd9Sstevel@tonic-gate 		mpcb->mpcb_wstate = WSTATE_USER64;
1277c478bd9Sstevel@tonic-gate 		mpcb->mpcb_wbuf = kmem_alloc(MAXWIN * sizeof (struct rwindow64),
1287c478bd9Sstevel@tonic-gate 		    KM_SLEEP);
1297c478bd9Sstevel@tonic-gate 	}
1307c478bd9Sstevel@tonic-gate 	ASSERT(((uintptr_t)mpcb->mpcb_wbuf & 7) == 0);
1317c478bd9Sstevel@tonic-gate 	mpcb->mpcb_wbuf_pa = va_to_pa(mpcb->mpcb_wbuf);
1327c478bd9Sstevel@tonic-gate 	mpcb->mpcb_pa = va_to_pa(mpcb);
1337c478bd9Sstevel@tonic-gate 	return (stk);
1347c478bd9Sstevel@tonic-gate }
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate void
1377c478bd9Sstevel@tonic-gate lwp_stk_fini(klwp_t *lwp)
1387c478bd9Sstevel@tonic-gate {
1397c478bd9Sstevel@tonic-gate 	struct machpcb *mpcb = lwptompcb(lwp);
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 	/*
1427c478bd9Sstevel@tonic-gate 	 * there might be windows still in the wbuf due to unmapped
1437c478bd9Sstevel@tonic-gate 	 * stack, misaligned stack pointer, etc.  We just free it.
1447c478bd9Sstevel@tonic-gate 	 */
1457c478bd9Sstevel@tonic-gate 	mpcb->mpcb_wbcnt = 0;
1467c478bd9Sstevel@tonic-gate 	if (mpcb->mpcb_wstate == WSTATE_USER32)
1477c478bd9Sstevel@tonic-gate 		kmem_free(mpcb->mpcb_wbuf, MAXWIN * sizeof (struct rwindow32));
1487c478bd9Sstevel@tonic-gate 	else
1497c478bd9Sstevel@tonic-gate 		kmem_free(mpcb->mpcb_wbuf, MAXWIN * sizeof (struct rwindow64));
1507c478bd9Sstevel@tonic-gate 	mpcb->mpcb_wbuf = NULL;
1517c478bd9Sstevel@tonic-gate 	mpcb->mpcb_wbuf_pa = -1;
1527c478bd9Sstevel@tonic-gate }
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate /*
1567c478bd9Sstevel@tonic-gate  * Copy regs from parent to child.
1577c478bd9Sstevel@tonic-gate  */
1587c478bd9Sstevel@tonic-gate void
1597c478bd9Sstevel@tonic-gate lwp_forkregs(klwp_t *lwp, klwp_t *clwp)
1607c478bd9Sstevel@tonic-gate {
1617c478bd9Sstevel@tonic-gate 	kthread_t *t, *pt = lwptot(lwp);
1627c478bd9Sstevel@tonic-gate 	struct machpcb *mpcb = lwptompcb(clwp);
1637c478bd9Sstevel@tonic-gate 	struct machpcb *pmpcb = lwptompcb(lwp);
1647c478bd9Sstevel@tonic-gate 	kfpu_t *fp, *pfp = lwptofpu(lwp);
1657c478bd9Sstevel@tonic-gate 	caddr_t wbuf;
1667c478bd9Sstevel@tonic-gate 	uint_t wstate;
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	t = mpcb->mpcb_thread;
1697c478bd9Sstevel@tonic-gate 	/*
1707c478bd9Sstevel@tonic-gate 	 * remember child's fp and wbuf since they will get erased during
1717c478bd9Sstevel@tonic-gate 	 * the bcopy.
1727c478bd9Sstevel@tonic-gate 	 */
1737c478bd9Sstevel@tonic-gate 	fp = mpcb->mpcb_fpu;
1747c478bd9Sstevel@tonic-gate 	wbuf = mpcb->mpcb_wbuf;
1757c478bd9Sstevel@tonic-gate 	wstate = mpcb->mpcb_wstate;
1767c478bd9Sstevel@tonic-gate 	/*
1777c478bd9Sstevel@tonic-gate 	 * Don't copy mpcb_frame since we hand-crafted it
1787c478bd9Sstevel@tonic-gate 	 * in thread_load().
1797c478bd9Sstevel@tonic-gate 	 */
1807c478bd9Sstevel@tonic-gate 	bcopy(lwp->lwp_regs, clwp->lwp_regs, sizeof (struct machpcb) - REGOFF);
1817c478bd9Sstevel@tonic-gate 	mpcb->mpcb_thread = t;
1827c478bd9Sstevel@tonic-gate 	mpcb->mpcb_fpu = fp;
1837c478bd9Sstevel@tonic-gate 	fp->fpu_q = mpcb->mpcb_fpu_q;
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	/*
1867c478bd9Sstevel@tonic-gate 	 * It is theoretically possibly for the lwp's wstate to
1877c478bd9Sstevel@tonic-gate 	 * be different from its value assigned in lwp_stk_init,
1887c478bd9Sstevel@tonic-gate 	 * since lwp_stk_init assumed the data model of the process.
1897c478bd9Sstevel@tonic-gate 	 * Here, we took on the data model of the cloned lwp.
1907c478bd9Sstevel@tonic-gate 	 */
1917c478bd9Sstevel@tonic-gate 	if (mpcb->mpcb_wstate != wstate) {
1927c478bd9Sstevel@tonic-gate 		size_t osize, size;
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 		if (wstate == WSTATE_USER32) {
1957c478bd9Sstevel@tonic-gate 			osize = MAXWIN * sizeof (struct rwindow32);
1967c478bd9Sstevel@tonic-gate 			size = MAXWIN * sizeof (struct rwindow64);
1977c478bd9Sstevel@tonic-gate 			wstate = WSTATE_USER64;
1987c478bd9Sstevel@tonic-gate 		} else {
1997c478bd9Sstevel@tonic-gate 			osize = MAXWIN * sizeof (struct rwindow64);
2007c478bd9Sstevel@tonic-gate 			size = MAXWIN * sizeof (struct rwindow32);
2017c478bd9Sstevel@tonic-gate 			wstate = WSTATE_USER32;
2027c478bd9Sstevel@tonic-gate 		}
2037c478bd9Sstevel@tonic-gate 		kmem_free(wbuf, osize);
2047c478bd9Sstevel@tonic-gate 		wbuf = kmem_alloc(size, KM_SLEEP);
2057c478bd9Sstevel@tonic-gate 	}
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	mpcb->mpcb_pa = va_to_pa(mpcb);
2087c478bd9Sstevel@tonic-gate 	mpcb->mpcb_wbuf = wbuf;
2097c478bd9Sstevel@tonic-gate 	mpcb->mpcb_wbuf_pa = va_to_pa(wbuf);
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	ASSERT(mpcb->mpcb_wstate == wstate);
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 	if (mpcb->mpcb_wbcnt != 0) {
2147c478bd9Sstevel@tonic-gate 		bcopy(pmpcb->mpcb_wbuf, mpcb->mpcb_wbuf,
2157c478bd9Sstevel@tonic-gate 		    mpcb->mpcb_wbcnt * ((mpcb->mpcb_wstate == WSTATE_USER32) ?
2167c478bd9Sstevel@tonic-gate 		    sizeof (struct rwindow32) : sizeof (struct rwindow64)));
2177c478bd9Sstevel@tonic-gate 	}
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	if (pt == curthread)
2207c478bd9Sstevel@tonic-gate 		pfp->fpu_fprs = _fp_read_fprs();
2217c478bd9Sstevel@tonic-gate 	if ((pfp->fpu_en) || (pfp->fpu_fprs & FPRS_FEF)) {
2227c478bd9Sstevel@tonic-gate 		if (pt == curthread && fpu_exists) {
2237c478bd9Sstevel@tonic-gate 			save_gsr(clwp->lwp_fpu);
2247c478bd9Sstevel@tonic-gate 		} else {
2257c478bd9Sstevel@tonic-gate 			uint64_t gsr;
2267c478bd9Sstevel@tonic-gate 			gsr = get_gsr(lwp->lwp_fpu);
2277c478bd9Sstevel@tonic-gate 			set_gsr(gsr, clwp->lwp_fpu);
2287c478bd9Sstevel@tonic-gate 		}
2297c478bd9Sstevel@tonic-gate 		fp_fork(lwp, clwp);
2307c478bd9Sstevel@tonic-gate 	}
2317c478bd9Sstevel@tonic-gate }
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate /*
2347c478bd9Sstevel@tonic-gate  * Free lwp fpu regs.
2357c478bd9Sstevel@tonic-gate  */
2367c478bd9Sstevel@tonic-gate void
2377c478bd9Sstevel@tonic-gate lwp_freeregs(klwp_t *lwp, int isexec)
2387c478bd9Sstevel@tonic-gate {
2397c478bd9Sstevel@tonic-gate 	kfpu_t *fp = lwptofpu(lwp);
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	if (lwptot(lwp) == curthread)
2427c478bd9Sstevel@tonic-gate 		fp->fpu_fprs = _fp_read_fprs();
2437c478bd9Sstevel@tonic-gate 	if ((fp->fpu_en) || (fp->fpu_fprs & FPRS_FEF))
2447c478bd9Sstevel@tonic-gate 		fp_free(fp, isexec);
2457c478bd9Sstevel@tonic-gate }
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate /*
2487c478bd9Sstevel@tonic-gate  * fill in the extra register state area specified with the
2497c478bd9Sstevel@tonic-gate  * specified lwp's platform-dependent non-floating-point extra
2507c478bd9Sstevel@tonic-gate  * register state information
2517c478bd9Sstevel@tonic-gate  */
2527c478bd9Sstevel@tonic-gate /* ARGSUSED */
2537c478bd9Sstevel@tonic-gate void
2547c478bd9Sstevel@tonic-gate xregs_getgfiller(klwp_id_t lwp, caddr_t xrp)
2557c478bd9Sstevel@tonic-gate {
2567c478bd9Sstevel@tonic-gate 	/* for sun4u nothing to do here, added for symmetry */
2577c478bd9Sstevel@tonic-gate }
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate /*
2607c478bd9Sstevel@tonic-gate  * fill in the extra register state area specified with the specified lwp's
2617c478bd9Sstevel@tonic-gate  * platform-dependent floating-point extra register state information.
2627c478bd9Sstevel@tonic-gate  * NOTE:  'lwp' might not correspond to 'curthread' since this is
2637c478bd9Sstevel@tonic-gate  * called from code in /proc to get the registers of another lwp.
2647c478bd9Sstevel@tonic-gate  */
2657c478bd9Sstevel@tonic-gate void
2667c478bd9Sstevel@tonic-gate xregs_getfpfiller(klwp_id_t lwp, caddr_t xrp)
2677c478bd9Sstevel@tonic-gate {
2687c478bd9Sstevel@tonic-gate 	prxregset_t *xregs = (prxregset_t *)xrp;
2697c478bd9Sstevel@tonic-gate 	kfpu_t *fp = lwptofpu(lwp);
2707c478bd9Sstevel@tonic-gate 	uint32_t fprs = (FPRS_FEF|FPRS_DU|FPRS_DL);
2717c478bd9Sstevel@tonic-gate 	uint64_t gsr;
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	/*
2747c478bd9Sstevel@tonic-gate 	 * fp_fksave() does not flush the GSR register into
2757c478bd9Sstevel@tonic-gate 	 * the lwp area, so do it now
2767c478bd9Sstevel@tonic-gate 	 */
2777c478bd9Sstevel@tonic-gate 	kpreempt_disable();
2787c478bd9Sstevel@tonic-gate 	if (ttolwp(curthread) == lwp && fpu_exists) {
2797c478bd9Sstevel@tonic-gate 		fp->fpu_fprs = _fp_read_fprs();
2807c478bd9Sstevel@tonic-gate 		if ((fp->fpu_fprs & FPRS_FEF) != FPRS_FEF) {
2817c478bd9Sstevel@tonic-gate 			_fp_write_fprs(fprs);
2827c478bd9Sstevel@tonic-gate 			fp->fpu_fprs = (V9_FPU_FPRS_TYPE)fprs;
2837c478bd9Sstevel@tonic-gate 		}
2847c478bd9Sstevel@tonic-gate 		save_gsr(fp);
2857c478bd9Sstevel@tonic-gate 	}
2867c478bd9Sstevel@tonic-gate 	gsr = get_gsr(fp);
2877c478bd9Sstevel@tonic-gate 	kpreempt_enable();
2887c478bd9Sstevel@tonic-gate 	PRXREG_GSR(xregs) = gsr;
2897c478bd9Sstevel@tonic-gate }
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate /*
2927c478bd9Sstevel@tonic-gate  * set the specified lwp's platform-dependent non-floating-point
2937c478bd9Sstevel@tonic-gate  * extra register state based on the specified input
2947c478bd9Sstevel@tonic-gate  */
2957c478bd9Sstevel@tonic-gate /* ARGSUSED */
2967c478bd9Sstevel@tonic-gate void
2977c478bd9Sstevel@tonic-gate xregs_setgfiller(klwp_id_t lwp, caddr_t xrp)
2987c478bd9Sstevel@tonic-gate {
2997c478bd9Sstevel@tonic-gate 	/* for sun4u nothing to do here, added for symmetry */
3007c478bd9Sstevel@tonic-gate }
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate /*
3037c478bd9Sstevel@tonic-gate  * set the specified lwp's platform-dependent floating-point
3047c478bd9Sstevel@tonic-gate  * extra register state based on the specified input
3057c478bd9Sstevel@tonic-gate  */
3067c478bd9Sstevel@tonic-gate void
3077c478bd9Sstevel@tonic-gate xregs_setfpfiller(klwp_id_t lwp, caddr_t xrp)
3087c478bd9Sstevel@tonic-gate {
3097c478bd9Sstevel@tonic-gate 	prxregset_t *xregs = (prxregset_t *)xrp;
3107c478bd9Sstevel@tonic-gate 	kfpu_t *fp = lwptofpu(lwp);
3117c478bd9Sstevel@tonic-gate 	uint32_t fprs = (FPRS_FEF|FPRS_DU|FPRS_DL);
3127c478bd9Sstevel@tonic-gate 	uint64_t gsr = PRXREG_GSR(xregs);
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	kpreempt_disable();
3157c478bd9Sstevel@tonic-gate 	set_gsr(gsr, lwptofpu(lwp));
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 	if ((lwp == ttolwp(curthread)) && fpu_exists) {
3187c478bd9Sstevel@tonic-gate 		fp->fpu_fprs = _fp_read_fprs();
3197c478bd9Sstevel@tonic-gate 		if ((fp->fpu_fprs & FPRS_FEF) != FPRS_FEF) {
3207c478bd9Sstevel@tonic-gate 			_fp_write_fprs(fprs);
3217c478bd9Sstevel@tonic-gate 			fp->fpu_fprs = (V9_FPU_FPRS_TYPE)fprs;
3227c478bd9Sstevel@tonic-gate 		}
3237c478bd9Sstevel@tonic-gate 		restore_gsr(lwptofpu(lwp));
3247c478bd9Sstevel@tonic-gate 	}
3257c478bd9Sstevel@tonic-gate 	kpreempt_enable();
3267c478bd9Sstevel@tonic-gate }
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate /*
3297c478bd9Sstevel@tonic-gate  * fill in the sun4u asrs, ie, the lwp's platform-dependent
3307c478bd9Sstevel@tonic-gate  * non-floating-point extra register state information
3317c478bd9Sstevel@tonic-gate  */
3327c478bd9Sstevel@tonic-gate /* ARGSUSED */
3337c478bd9Sstevel@tonic-gate void
3347c478bd9Sstevel@tonic-gate getasrs(klwp_t *lwp, asrset_t asr)
3357c478bd9Sstevel@tonic-gate {
3367c478bd9Sstevel@tonic-gate 	/* for sun4u nothing to do here, added for symmetry */
3377c478bd9Sstevel@tonic-gate }
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate /*
3407c478bd9Sstevel@tonic-gate  * fill in the sun4u asrs, ie, the lwp's platform-dependent
3417c478bd9Sstevel@tonic-gate  * floating-point extra register state information
3427c478bd9Sstevel@tonic-gate  */
3437c478bd9Sstevel@tonic-gate void
3447c478bd9Sstevel@tonic-gate getfpasrs(klwp_t *lwp, asrset_t asr)
3457c478bd9Sstevel@tonic-gate {
3467c478bd9Sstevel@tonic-gate 	kfpu_t *fp = lwptofpu(lwp);
3477c478bd9Sstevel@tonic-gate 	uint32_t fprs = (FPRS_FEF|FPRS_DU|FPRS_DL);
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 	kpreempt_disable();
3507c478bd9Sstevel@tonic-gate 	if (ttolwp(curthread) == lwp)
3517c478bd9Sstevel@tonic-gate 		fp->fpu_fprs = _fp_read_fprs();
3527c478bd9Sstevel@tonic-gate 	if ((fp->fpu_en) || (fp->fpu_fprs & FPRS_FEF)) {
3537c478bd9Sstevel@tonic-gate 		if (fpu_exists && ttolwp(curthread) == lwp) {
3547c478bd9Sstevel@tonic-gate 			if ((fp->fpu_fprs & FPRS_FEF) != FPRS_FEF) {
3557c478bd9Sstevel@tonic-gate 				_fp_write_fprs(fprs);
3567c478bd9Sstevel@tonic-gate 				fp->fpu_fprs = (V9_FPU_FPRS_TYPE)fprs;
3577c478bd9Sstevel@tonic-gate 			}
3587c478bd9Sstevel@tonic-gate 			save_gsr(fp);
3597c478bd9Sstevel@tonic-gate 		}
3607c478bd9Sstevel@tonic-gate 		asr[ASR_GSR] = (int64_t)get_gsr(fp);
3617c478bd9Sstevel@tonic-gate 	}
3627c478bd9Sstevel@tonic-gate 	kpreempt_enable();
3637c478bd9Sstevel@tonic-gate }
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate /*
3667c478bd9Sstevel@tonic-gate  * set the sun4u asrs, ie, the lwp's platform-dependent
3677c478bd9Sstevel@tonic-gate  * non-floating-point extra register state information
3687c478bd9Sstevel@tonic-gate  */
3697c478bd9Sstevel@tonic-gate /* ARGSUSED */
3707c478bd9Sstevel@tonic-gate void
3717c478bd9Sstevel@tonic-gate setasrs(klwp_t *lwp, asrset_t asr)
3727c478bd9Sstevel@tonic-gate {
3737c478bd9Sstevel@tonic-gate 	/* for sun4u nothing to do here, added for symmetry */
3747c478bd9Sstevel@tonic-gate }
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate void
3777c478bd9Sstevel@tonic-gate setfpasrs(klwp_t *lwp, asrset_t asr)
3787c478bd9Sstevel@tonic-gate {
3797c478bd9Sstevel@tonic-gate 	kfpu_t *fp = lwptofpu(lwp);
3807c478bd9Sstevel@tonic-gate 	uint32_t fprs = (FPRS_FEF|FPRS_DU|FPRS_DL);
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	kpreempt_disable();
3837c478bd9Sstevel@tonic-gate 	if (ttolwp(curthread) == lwp)
3847c478bd9Sstevel@tonic-gate 		fp->fpu_fprs = _fp_read_fprs();
3857c478bd9Sstevel@tonic-gate 	if ((fp->fpu_en) || (fp->fpu_fprs & FPRS_FEF)) {
3867c478bd9Sstevel@tonic-gate 		set_gsr(asr[ASR_GSR], fp);
3877c478bd9Sstevel@tonic-gate 		if (fpu_exists && ttolwp(curthread) == lwp) {
3887c478bd9Sstevel@tonic-gate 			if ((fp->fpu_fprs & FPRS_FEF) != FPRS_FEF) {
3897c478bd9Sstevel@tonic-gate 				_fp_write_fprs(fprs);
3907c478bd9Sstevel@tonic-gate 				fp->fpu_fprs = (V9_FPU_FPRS_TYPE)fprs;
3917c478bd9Sstevel@tonic-gate 			}
3927c478bd9Sstevel@tonic-gate 			restore_gsr(fp);
3937c478bd9Sstevel@tonic-gate 		}
3947c478bd9Sstevel@tonic-gate 	}
3957c478bd9Sstevel@tonic-gate 	kpreempt_enable();
3967c478bd9Sstevel@tonic-gate }
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate /*
3997c478bd9Sstevel@tonic-gate  * Create interrupt kstats for this CPU.
4007c478bd9Sstevel@tonic-gate  */
4017c478bd9Sstevel@tonic-gate void
4027c478bd9Sstevel@tonic-gate cpu_create_intrstat(cpu_t *cp)
4037c478bd9Sstevel@tonic-gate {
4047c478bd9Sstevel@tonic-gate 	int		i;
4057c478bd9Sstevel@tonic-gate 	kstat_t		*intr_ksp;
4067c478bd9Sstevel@tonic-gate 	kstat_named_t	*knp;
4077c478bd9Sstevel@tonic-gate 	char		name[KSTAT_STRLEN];
4087c478bd9Sstevel@tonic-gate 	zoneid_t	zoneid;
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 	if (pool_pset_enabled())
4137c478bd9Sstevel@tonic-gate 		zoneid = GLOBAL_ZONEID;
4147c478bd9Sstevel@tonic-gate 	else
4157c478bd9Sstevel@tonic-gate 		zoneid = ALL_ZONES;
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 	intr_ksp = kstat_create_zone("cpu", cp->cpu_id, "intrstat", "misc",
4187c478bd9Sstevel@tonic-gate 	    KSTAT_TYPE_NAMED, PIL_MAX * 2, NULL, zoneid);
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 	/*
4217c478bd9Sstevel@tonic-gate 	 * Initialize each PIL's named kstat
4227c478bd9Sstevel@tonic-gate 	 */
4237c478bd9Sstevel@tonic-gate 	if (intr_ksp != NULL) {
4247c478bd9Sstevel@tonic-gate 		intr_ksp->ks_update = cpu_kstat_intrstat_update;
4257c478bd9Sstevel@tonic-gate 		knp = (kstat_named_t *)intr_ksp->ks_data;
4267c478bd9Sstevel@tonic-gate 		intr_ksp->ks_private = cp;
4277c478bd9Sstevel@tonic-gate 		for (i = 0; i < PIL_MAX; i++) {
4287c478bd9Sstevel@tonic-gate 			(void) snprintf(name, KSTAT_STRLEN, "level-%d-time",
4297c478bd9Sstevel@tonic-gate 			    i + 1);
4307c478bd9Sstevel@tonic-gate 			kstat_named_init(&knp[i * 2], name, KSTAT_DATA_UINT64);
4317c478bd9Sstevel@tonic-gate 			(void) snprintf(name, KSTAT_STRLEN, "level-%d-count",
4327c478bd9Sstevel@tonic-gate 			    i + 1);
4337c478bd9Sstevel@tonic-gate 			kstat_named_init(&knp[(i * 2) + 1], name,
4347c478bd9Sstevel@tonic-gate 			    KSTAT_DATA_UINT64);
4357c478bd9Sstevel@tonic-gate 		}
4367c478bd9Sstevel@tonic-gate 		kstat_install(intr_ksp);
4377c478bd9Sstevel@tonic-gate 	}
4387c478bd9Sstevel@tonic-gate }
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate /*
4417c478bd9Sstevel@tonic-gate  * Delete interrupt kstats for this CPU.
4427c478bd9Sstevel@tonic-gate  */
4437c478bd9Sstevel@tonic-gate void
4447c478bd9Sstevel@tonic-gate cpu_delete_intrstat(cpu_t *cp)
4457c478bd9Sstevel@tonic-gate {
4467c478bd9Sstevel@tonic-gate 	kstat_delete_byname_zone("cpu", cp->cpu_id, "intrstat", ALL_ZONES);
4477c478bd9Sstevel@tonic-gate }
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate /*
4507c478bd9Sstevel@tonic-gate  * Convert interrupt statistics from CPU ticks to nanoseconds and
4517c478bd9Sstevel@tonic-gate  * update kstat.
4527c478bd9Sstevel@tonic-gate  */
4537c478bd9Sstevel@tonic-gate int
4547c478bd9Sstevel@tonic-gate cpu_kstat_intrstat_update(kstat_t *ksp, int rw)
4557c478bd9Sstevel@tonic-gate {
4567c478bd9Sstevel@tonic-gate 	kstat_named_t	*knp = ksp->ks_data;
4577c478bd9Sstevel@tonic-gate 	cpu_t		*cpup = (cpu_t *)ksp->ks_private;
4587c478bd9Sstevel@tonic-gate 	int		i;
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 	if (rw == KSTAT_WRITE)
4617c478bd9Sstevel@tonic-gate 		return (EACCES);
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 	/*
4647c478bd9Sstevel@tonic-gate 	 * We use separate passes to copy and convert the statistics to
4657c478bd9Sstevel@tonic-gate 	 * nanoseconds. This assures that the snapshot of the data is as
4667c478bd9Sstevel@tonic-gate 	 * self-consistent as possible.
4677c478bd9Sstevel@tonic-gate 	 */
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 	for (i = 0; i < PIL_MAX; i++) {
4707c478bd9Sstevel@tonic-gate 		knp[i * 2].value.ui64 = cpup->cpu_m.intrstat[i + 1][0];
4717c478bd9Sstevel@tonic-gate 		knp[(i * 2) + 1].value.ui64 = cpup->cpu_stats.sys.intr[i];
4727c478bd9Sstevel@tonic-gate 	}
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate 	for (i = 0; i < PIL_MAX; i++) {
4757c478bd9Sstevel@tonic-gate 		knp[i * 2].value.ui64 =
4767c478bd9Sstevel@tonic-gate 		    (uint64_t)tick2ns((hrtime_t)knp[i * 2].value.ui64,
4777c478bd9Sstevel@tonic-gate 			cpup->cpu_id);
4787c478bd9Sstevel@tonic-gate 	}
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 	return (0);
4817c478bd9Sstevel@tonic-gate }
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate /*
4847c478bd9Sstevel@tonic-gate  * Called by common/os/cpu.c for psrinfo(1m) kstats
4857c478bd9Sstevel@tonic-gate  */
4867c478bd9Sstevel@tonic-gate char *
4877c478bd9Sstevel@tonic-gate cpu_fru_fmri(cpu_t *cp)
4887c478bd9Sstevel@tonic-gate {
4897c478bd9Sstevel@tonic-gate 	return (cpunodes[cp->cpu_id].fru_fmri);
4907c478bd9Sstevel@tonic-gate }
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate /*
4937c478bd9Sstevel@tonic-gate  * An interrupt thread is ending a time slice, so compute the interval it
4947c478bd9Sstevel@tonic-gate  * ran for and update the statistic for its PIL.
4957c478bd9Sstevel@tonic-gate  */
4967c478bd9Sstevel@tonic-gate void
4977c478bd9Sstevel@tonic-gate cpu_intr_swtch_enter(kthread_id_t t)
4987c478bd9Sstevel@tonic-gate {
4997c478bd9Sstevel@tonic-gate 	uint64_t	interval;
5007c478bd9Sstevel@tonic-gate 	uint64_t	start;
501*eda89462Sesolom 	cpu_t		*cpu;
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate 	ASSERT((t->t_flag & T_INTR_THREAD) != 0);
5047c478bd9Sstevel@tonic-gate 	ASSERT(t->t_pil > 0 && t->t_pil <= LOCK_LEVEL);
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 	/*
5077c478bd9Sstevel@tonic-gate 	 * We could be here with a zero timestamp. This could happen if:
5087c478bd9Sstevel@tonic-gate 	 * an interrupt thread which no longer has a pinned thread underneath
5097c478bd9Sstevel@tonic-gate 	 * it (i.e. it blocked at some point in its past) has finished running
5107c478bd9Sstevel@tonic-gate 	 * its handler. intr_thread() updated the interrupt statistic for its
5117c478bd9Sstevel@tonic-gate 	 * PIL and zeroed its timestamp. Since there was no pinned thread to
5127c478bd9Sstevel@tonic-gate 	 * return to, swtch() gets called and we end up here.
5137c478bd9Sstevel@tonic-gate 	 *
5147c478bd9Sstevel@tonic-gate 	 * It can also happen if an interrupt thread in intr_thread() calls
5157c478bd9Sstevel@tonic-gate 	 * preempt. It will have already taken care of updating stats. In
5167c478bd9Sstevel@tonic-gate 	 * this event, the interrupt thread will be runnable.
5177c478bd9Sstevel@tonic-gate 	 */
5187c478bd9Sstevel@tonic-gate 	if (t->t_intr_start) {
5197c478bd9Sstevel@tonic-gate 		do {
5207c478bd9Sstevel@tonic-gate 			start = t->t_intr_start;
5217c478bd9Sstevel@tonic-gate 			interval = gettick_counter() - start;
5227c478bd9Sstevel@tonic-gate 		} while (cas64(&t->t_intr_start, start, 0) != start);
523*eda89462Sesolom 		cpu = CPU;
524*eda89462Sesolom 		if (cpu->cpu_m.divisor > 1)
525*eda89462Sesolom 			interval *= cpu->cpu_m.divisor;
526*eda89462Sesolom 		cpu->cpu_m.intrstat[t->t_pil][0] += interval;
527*eda89462Sesolom 
528*eda89462Sesolom 		atomic_add_64((uint64_t *)&cpu->cpu_intracct[cpu->cpu_mstate],
529*eda89462Sesolom 		    interval);
5307c478bd9Sstevel@tonic-gate 	} else
5317c478bd9Sstevel@tonic-gate 		ASSERT(t->t_intr == NULL || t->t_state == TS_RUN);
5327c478bd9Sstevel@tonic-gate }
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate /*
5367c478bd9Sstevel@tonic-gate  * An interrupt thread is returning from swtch(). Place a starting timestamp
5377c478bd9Sstevel@tonic-gate  * in its thread structure.
5387c478bd9Sstevel@tonic-gate  */
5397c478bd9Sstevel@tonic-gate void
5407c478bd9Sstevel@tonic-gate cpu_intr_swtch_exit(kthread_id_t t)
5417c478bd9Sstevel@tonic-gate {
5427c478bd9Sstevel@tonic-gate 	uint64_t ts;
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 	ASSERT((t->t_flag & T_INTR_THREAD) != 0);
5457c478bd9Sstevel@tonic-gate 	ASSERT(t->t_pil > 0 && t->t_pil <= LOCK_LEVEL);
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 	do {
5487c478bd9Sstevel@tonic-gate 		ts = t->t_intr_start;
5497c478bd9Sstevel@tonic-gate 	} while (cas64(&t->t_intr_start, ts, gettick_counter()) != ts);
5507c478bd9Sstevel@tonic-gate }
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate int
5547c478bd9Sstevel@tonic-gate blacklist(int cmd, const char *scheme, nvlist_t *fmri, const char *class)
5557c478bd9Sstevel@tonic-gate {
5567c478bd9Sstevel@tonic-gate 	if (&plat_blacklist)
5577c478bd9Sstevel@tonic-gate 		return (plat_blacklist(cmd, scheme, fmri, class));
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate 	return (ENOTSUP);
5607c478bd9Sstevel@tonic-gate }
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate int
5637c478bd9Sstevel@tonic-gate kdi_pread(caddr_t buf, size_t nbytes, uint64_t addr, size_t *ncopiedp)
5647c478bd9Sstevel@tonic-gate {
5657c478bd9Sstevel@tonic-gate 	extern void kdi_flush_caches(void);
5667c478bd9Sstevel@tonic-gate 	size_t nread = 0;
5677c478bd9Sstevel@tonic-gate 	uint32_t word;
5687c478bd9Sstevel@tonic-gate 	int slop, i;
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate 	kdi_flush_caches();
5717c478bd9Sstevel@tonic-gate 	membar_enter();
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 	/* We might not begin on a word boundary. */
5747c478bd9Sstevel@tonic-gate 	if ((slop = addr & 3) != 0) {
5757c478bd9Sstevel@tonic-gate 		word = ldphys(addr & ~3);
5767c478bd9Sstevel@tonic-gate 		for (i = slop; i < 4 && nbytes > 0; i++, nbytes--, nread++)
5777c478bd9Sstevel@tonic-gate 			*buf++ = ((uchar_t *)&word)[i];
5787c478bd9Sstevel@tonic-gate 		addr = roundup(addr, 4);
5797c478bd9Sstevel@tonic-gate 	}
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate 	while (nbytes > 0) {
5827c478bd9Sstevel@tonic-gate 		word = ldphys(addr);
5837c478bd9Sstevel@tonic-gate 		for (i = 0; i < 4 && nbytes > 0; i++, nbytes--, nread++, addr++)
5847c478bd9Sstevel@tonic-gate 			*buf++ = ((uchar_t *)&word)[i];
5857c478bd9Sstevel@tonic-gate 	}
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate 	kdi_flush_caches();
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 	*ncopiedp = nread;
5907c478bd9Sstevel@tonic-gate 	return (0);
5917c478bd9Sstevel@tonic-gate }
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate int
5947c478bd9Sstevel@tonic-gate kdi_pwrite(caddr_t buf, size_t nbytes, uint64_t addr, size_t *ncopiedp)
5957c478bd9Sstevel@tonic-gate {
5967c478bd9Sstevel@tonic-gate 	extern void kdi_flush_caches(void);
5977c478bd9Sstevel@tonic-gate 	size_t nwritten = 0;
5987c478bd9Sstevel@tonic-gate 	uint32_t word;
5997c478bd9Sstevel@tonic-gate 	int slop, i;
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 	kdi_flush_caches();
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 	/* We might not begin on a word boundary. */
6047c478bd9Sstevel@tonic-gate 	if ((slop = addr & 3) != 0) {
6057c478bd9Sstevel@tonic-gate 		word = ldphys(addr & ~3);
6067c478bd9Sstevel@tonic-gate 		for (i = slop; i < 4 && nbytes > 0; i++, nbytes--, nwritten++)
6077c478bd9Sstevel@tonic-gate 			((uchar_t *)&word)[i] = *buf++;
6087c478bd9Sstevel@tonic-gate 		stphys(addr & ~3, word);
6097c478bd9Sstevel@tonic-gate 		addr = roundup(addr, 4);
6107c478bd9Sstevel@tonic-gate 	}
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate 	while (nbytes > 3) {
6137c478bd9Sstevel@tonic-gate 		for (word = 0, i = 0; i < 4; i++, nbytes--, nwritten++)
6147c478bd9Sstevel@tonic-gate 			((uchar_t *)&word)[i] = *buf++;
6157c478bd9Sstevel@tonic-gate 		stphys(addr, word);
6167c478bd9Sstevel@tonic-gate 		addr += 4;
6177c478bd9Sstevel@tonic-gate 	}
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate 	/* We might not end with a whole word. */
6207c478bd9Sstevel@tonic-gate 	if (nbytes > 0) {
6217c478bd9Sstevel@tonic-gate 		word = ldphys(addr);
6227c478bd9Sstevel@tonic-gate 		for (i = 0; nbytes > 0; i++, nbytes--, nwritten++)
6237c478bd9Sstevel@tonic-gate 			((uchar_t *)&word)[i] = *buf++;
6247c478bd9Sstevel@tonic-gate 		stphys(addr, word);
6257c478bd9Sstevel@tonic-gate 	}
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate 	membar_enter();
6287c478bd9Sstevel@tonic-gate 	kdi_flush_caches();
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 	*ncopiedp = nwritten;
6317c478bd9Sstevel@tonic-gate 	return (0);
6327c478bd9Sstevel@tonic-gate }
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate static void
6357c478bd9Sstevel@tonic-gate kdi_kernpanic(struct regs *regs, uint_t tt)
6367c478bd9Sstevel@tonic-gate {
6377c478bd9Sstevel@tonic-gate 	sync_reg_buf = *regs;
6387c478bd9Sstevel@tonic-gate 	sync_tt = tt;
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate 	sync_handler();
6417c478bd9Sstevel@tonic-gate }
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate static void
6447c478bd9Sstevel@tonic-gate kdi_plat_call(void (*platfn)(void))
6457c478bd9Sstevel@tonic-gate {
6467c478bd9Sstevel@tonic-gate 	if (platfn != NULL) {
6477c478bd9Sstevel@tonic-gate 		prom_suspend_prepost();
6487c478bd9Sstevel@tonic-gate 		platfn();
6497c478bd9Sstevel@tonic-gate 		prom_resume_prepost();
6507c478bd9Sstevel@tonic-gate 	}
6517c478bd9Sstevel@tonic-gate }
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate void
6547c478bd9Sstevel@tonic-gate mach_kdi_init(kdi_t *kdi)
6557c478bd9Sstevel@tonic-gate {
6567c478bd9Sstevel@tonic-gate 	kdi->kdi_plat_call = kdi_plat_call;
6577c478bd9Sstevel@tonic-gate 	kdi->mkdi_cpu_index = kdi_cpu_index;
6587c478bd9Sstevel@tonic-gate 	kdi->mkdi_trap_vatotte = kdi_trap_vatotte;
6597c478bd9Sstevel@tonic-gate 	kdi->mkdi_kernpanic = kdi_kernpanic;
6607c478bd9Sstevel@tonic-gate }
661*eda89462Sesolom 
662*eda89462Sesolom 
663*eda89462Sesolom /*
664*eda89462Sesolom  * get_cpu_mstate() is passed an array of timestamps, NCMSTATES
665*eda89462Sesolom  * long, and it fills in the array with the time spent on cpu in
666*eda89462Sesolom  * each of the mstates, where time is returned in nsec.
667*eda89462Sesolom  *
668*eda89462Sesolom  * No guarantee is made that the returned values in times[] will
669*eda89462Sesolom  * monotonically increase on sequential calls, although this will
670*eda89462Sesolom  * be true in the long run. Any such guarantee must be handled by
671*eda89462Sesolom  * the caller, if needed. This can happen if we fail to account
672*eda89462Sesolom  * for elapsed time due to a generation counter conflict, yet we
673*eda89462Sesolom  * did account for it on a prior call (see below).
674*eda89462Sesolom  *
675*eda89462Sesolom  * The complication is that the cpu in question may be updating
676*eda89462Sesolom  * its microstate at the same time that we are reading it.
677*eda89462Sesolom  * Because the microstate is only updated when the CPU's state
678*eda89462Sesolom  * changes, the values in cpu_intracct[] can be indefinitely out
679*eda89462Sesolom  * of date. To determine true current values, it is necessary to
680*eda89462Sesolom  * compare the current time with cpu_mstate_start, and add the
681*eda89462Sesolom  * difference to times[cpu_mstate].
682*eda89462Sesolom  *
683*eda89462Sesolom  * This can be a problem if those values are changing out from
684*eda89462Sesolom  * under us. Because the code path in new_cpu_mstate() is
685*eda89462Sesolom  * performance critical, we have not added a lock to it. Instead,
686*eda89462Sesolom  * we have added a generation counter. Before beginning
687*eda89462Sesolom  * modifications, the counter is set to 0. After modifications,
688*eda89462Sesolom  * it is set to the old value plus one.
689*eda89462Sesolom  *
690*eda89462Sesolom  * get_cpu_mstate() will not consider the values of cpu_mstate
691*eda89462Sesolom  * and cpu_mstate_start to be usable unless the value of
692*eda89462Sesolom  * cpu_mstate_gen is both non-zero and unchanged, both before and
693*eda89462Sesolom  * after reading the mstate information. Note that we must
694*eda89462Sesolom  * protect against out-of-order loads around accesses to the
695*eda89462Sesolom  * generation counter. Also, this is a best effort approach in
696*eda89462Sesolom  * that we do not retry should the counter be found to have
697*eda89462Sesolom  * changed.
698*eda89462Sesolom  *
699*eda89462Sesolom  * cpu_intracct[] is used to identify time spent in each CPU
700*eda89462Sesolom  * mstate while handling interrupts. Such time should be reported
701*eda89462Sesolom  * against system time, and so is subtracted out from its
702*eda89462Sesolom  * corresponding cpu_acct[] time and added to
703*eda89462Sesolom  * cpu_acct[CMS_SYSTEM]. Additionally, intracct time is stored in
704*eda89462Sesolom  * %ticks, but acct time may be stored as %sticks, thus requiring
705*eda89462Sesolom  * different conversions before they can be compared.
706*eda89462Sesolom  */
707*eda89462Sesolom 
708*eda89462Sesolom void
709*eda89462Sesolom get_cpu_mstate(cpu_t *cpu, hrtime_t *times)
710*eda89462Sesolom {
711*eda89462Sesolom 	int i;
712*eda89462Sesolom 	hrtime_t now, start;
713*eda89462Sesolom 	uint16_t gen;
714*eda89462Sesolom 	uint16_t state;
715*eda89462Sesolom 	hrtime_t intracct[NCMSTATES];
716*eda89462Sesolom 
717*eda89462Sesolom 	/*
718*eda89462Sesolom 	 * Load all volatile state under the protection of membar.
719*eda89462Sesolom 	 * cpu_acct[cpu_mstate] must be loaded to avoid double counting
720*eda89462Sesolom 	 * of (now - cpu_mstate_start) by a change in CPU mstate that
721*eda89462Sesolom 	 * arrives after we make our last check of cpu_mstate_gen.
722*eda89462Sesolom 	 */
723*eda89462Sesolom 
724*eda89462Sesolom 	now = gethrtime_unscaled();
725*eda89462Sesolom 	gen = cpu->cpu_mstate_gen;
726*eda89462Sesolom 
727*eda89462Sesolom 	membar_consumer();	/* guarantee load ordering */
728*eda89462Sesolom 	start = cpu->cpu_mstate_start;
729*eda89462Sesolom 	state = cpu->cpu_mstate;
730*eda89462Sesolom 	for (i = 0; i < NCMSTATES; i++) {
731*eda89462Sesolom 		intracct[i] = cpu->cpu_intracct[i];
732*eda89462Sesolom 		times[i] = cpu->cpu_acct[i];
733*eda89462Sesolom 	}
734*eda89462Sesolom 	membar_consumer();	/* guarantee load ordering */
735*eda89462Sesolom 
736*eda89462Sesolom 	if (gen != 0 && gen == cpu->cpu_mstate_gen && now > start)
737*eda89462Sesolom 		times[state] += now - start;
738*eda89462Sesolom 
739*eda89462Sesolom 	for (i = 0; i < NCMSTATES; i++) {
740*eda89462Sesolom 		scalehrtime(&times[i]);
741*eda89462Sesolom 		intracct[i] = tick2ns((hrtime_t)intracct[i], cpu->cpu_id);
742*eda89462Sesolom 	}
743*eda89462Sesolom 
744*eda89462Sesolom 	for (i = 0; i < NCMSTATES; i++) {
745*eda89462Sesolom 		if (i == CMS_SYSTEM)
746*eda89462Sesolom 			continue;
747*eda89462Sesolom 		times[i] -= intracct[i];
748*eda89462Sesolom 		if (times[i] < 0) {
749*eda89462Sesolom 			intracct[i] += times[i];
750*eda89462Sesolom 			times[i] = 0;
751*eda89462Sesolom 		}
752*eda89462Sesolom 		times[CMS_SYSTEM] += intracct[i];
753*eda89462Sesolom 	}
754*eda89462Sesolom }
755