xref: /onnv-gate/usr/src/uts/sun4/os/machdep.c (revision 2224)
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
52005Selowe  * Common Development and Distribution License (the "License").
62005Selowe  * 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  */
210Sstevel@tonic-gate /*
222005Selowe  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/types.h>
290Sstevel@tonic-gate #include <sys/kstat.h>
300Sstevel@tonic-gate #include <sys/param.h>
310Sstevel@tonic-gate #include <sys/stack.h>
320Sstevel@tonic-gate #include <sys/regset.h>
330Sstevel@tonic-gate #include <sys/thread.h>
340Sstevel@tonic-gate #include <sys/proc.h>
350Sstevel@tonic-gate #include <sys/procfs_isa.h>
360Sstevel@tonic-gate #include <sys/kmem.h>
370Sstevel@tonic-gate #include <sys/cpuvar.h>
380Sstevel@tonic-gate #include <sys/systm.h>
390Sstevel@tonic-gate #include <sys/machpcb.h>
400Sstevel@tonic-gate #include <sys/machasi.h>
410Sstevel@tonic-gate #include <sys/vis.h>
420Sstevel@tonic-gate #include <sys/fpu/fpusystm.h>
430Sstevel@tonic-gate #include <sys/cpu_module.h>
440Sstevel@tonic-gate #include <sys/privregs.h>
450Sstevel@tonic-gate #include <sys/archsystm.h>
460Sstevel@tonic-gate #include <sys/atomic.h>
470Sstevel@tonic-gate #include <sys/cmn_err.h>
480Sstevel@tonic-gate #include <sys/time.h>
490Sstevel@tonic-gate #include <sys/clock.h>
500Sstevel@tonic-gate #include <sys/chip.h>
510Sstevel@tonic-gate #include <sys/cmp.h>
520Sstevel@tonic-gate #include <sys/platform_module.h>
530Sstevel@tonic-gate #include <sys/bl.h>
540Sstevel@tonic-gate #include <sys/nvpair.h>
550Sstevel@tonic-gate #include <sys/kdi_impl.h>
560Sstevel@tonic-gate #include <sys/machsystm.h>
570Sstevel@tonic-gate #include <sys/sysmacros.h>
580Sstevel@tonic-gate #include <sys/promif.h>
590Sstevel@tonic-gate #include <sys/pool_pset.h>
602005Selowe #include <vm/seg_kmem.h>
610Sstevel@tonic-gate 
620Sstevel@tonic-gate int maxphys = MMU_PAGESIZE * 16;	/* 128k */
630Sstevel@tonic-gate int klustsize = MMU_PAGESIZE * 16;	/* 128k */
640Sstevel@tonic-gate 
650Sstevel@tonic-gate /*
660Sstevel@tonic-gate  * Initialize kernel thread's stack.
670Sstevel@tonic-gate  */
680Sstevel@tonic-gate caddr_t
690Sstevel@tonic-gate thread_stk_init(caddr_t stk)
700Sstevel@tonic-gate {
710Sstevel@tonic-gate 	kfpu_t *fp;
720Sstevel@tonic-gate 	ulong_t align;
730Sstevel@tonic-gate 
740Sstevel@tonic-gate 	/* allocate extra space for floating point state */
750Sstevel@tonic-gate 	stk -= SA(sizeof (kfpu_t) + GSR_SIZE);
760Sstevel@tonic-gate 	align = (uintptr_t)stk & 0x3f;
770Sstevel@tonic-gate 	stk -= align;		/* force v9_fpu to be 16 byte aligned */
780Sstevel@tonic-gate 	fp = (kfpu_t *)stk;
790Sstevel@tonic-gate 	fp->fpu_fprs = 0;
800Sstevel@tonic-gate 
810Sstevel@tonic-gate 	stk -= SA(MINFRAME);
820Sstevel@tonic-gate 	return (stk);
830Sstevel@tonic-gate }
840Sstevel@tonic-gate 
852005Selowe #define	WIN32_SIZE	(MAXWIN * sizeof (struct rwindow32))
862005Selowe #define	WIN64_SIZE	(MAXWIN * sizeof (struct rwindow64))
872005Selowe 
882005Selowe kmem_cache_t	*wbuf32_cache;
892005Selowe kmem_cache_t	*wbuf64_cache;
902005Selowe 
912005Selowe void
922005Selowe lwp_stk_cache_init(void)
932005Selowe {
94*2224Selowe 	/*
95*2224Selowe 	 * Window buffers are allocated from the static arena
96*2224Selowe 	 * because they are accessed at TL>0. We also must use
97*2224Selowe 	 * KMC_NOHASH to prevent them from straddling page
98*2224Selowe 	 * boundaries as they are accessed by physical address.
99*2224Selowe 	 */
1002005Selowe 	wbuf32_cache = kmem_cache_create("wbuf32_cache", WIN32_SIZE,
101*2224Selowe 	    0, NULL, NULL, NULL, NULL, static_arena, KMC_NOHASH);
1022005Selowe 	wbuf64_cache = kmem_cache_create("wbuf64_cache", WIN64_SIZE,
103*2224Selowe 	    0, NULL, NULL, NULL, NULL, static_arena, KMC_NOHASH);
1042005Selowe }
1052005Selowe 
1060Sstevel@tonic-gate /*
1070Sstevel@tonic-gate  * Initialize lwp's kernel stack.
1080Sstevel@tonic-gate  * Note that now that the floating point register save area (kfpu_t)
1090Sstevel@tonic-gate  * has been broken out from machpcb and aligned on a 64 byte boundary so that
1100Sstevel@tonic-gate  * we can do block load/stores to/from it, there are a couple of potential
1110Sstevel@tonic-gate  * optimizations to save stack space. 1. The floating point register save
1120Sstevel@tonic-gate  * area could be aligned on a 16 byte boundary, and the floating point code
1130Sstevel@tonic-gate  * changed to (a) check the alignment and (b) use different save/restore
1140Sstevel@tonic-gate  * macros depending upon the alignment. 2. The lwp_stk_init code below
1150Sstevel@tonic-gate  * could be changed to calculate if less space would be wasted if machpcb
1160Sstevel@tonic-gate  * was first instead of second. However there is a REGOFF macro used in
1170Sstevel@tonic-gate  * locore, syscall_trap, machdep and mlsetup that assumes that the saved
1180Sstevel@tonic-gate  * register area is a fixed distance from the %sp, and would have to be
1190Sstevel@tonic-gate  * changed to a pointer or something...JJ said later.
1200Sstevel@tonic-gate  */
1210Sstevel@tonic-gate caddr_t
1220Sstevel@tonic-gate lwp_stk_init(klwp_t *lwp, caddr_t stk)
1230Sstevel@tonic-gate {
1240Sstevel@tonic-gate 	struct machpcb *mpcb;
1250Sstevel@tonic-gate 	kfpu_t *fp;
1260Sstevel@tonic-gate 	uintptr_t aln;
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate 	stk -= SA(sizeof (kfpu_t) + GSR_SIZE);
1290Sstevel@tonic-gate 	aln = (uintptr_t)stk & 0x3F;
1300Sstevel@tonic-gate 	stk -= aln;
1310Sstevel@tonic-gate 	fp = (kfpu_t *)stk;
1320Sstevel@tonic-gate 	stk -= SA(sizeof (struct machpcb));
1330Sstevel@tonic-gate 	mpcb = (struct machpcb *)stk;
1340Sstevel@tonic-gate 	bzero(mpcb, sizeof (struct machpcb));
1350Sstevel@tonic-gate 	bzero(fp, sizeof (kfpu_t) + GSR_SIZE);
1360Sstevel@tonic-gate 	lwp->lwp_regs = (void *)&mpcb->mpcb_regs;
1370Sstevel@tonic-gate 	lwp->lwp_fpu = (void *)fp;
1380Sstevel@tonic-gate 	mpcb->mpcb_fpu = fp;
1390Sstevel@tonic-gate 	mpcb->mpcb_fpu->fpu_q = mpcb->mpcb_fpu_q;
1400Sstevel@tonic-gate 	mpcb->mpcb_thread = lwp->lwp_thread;
1410Sstevel@tonic-gate 	mpcb->mpcb_wbcnt = 0;
1420Sstevel@tonic-gate 	if (lwp->lwp_procp->p_model == DATAMODEL_ILP32) {
1430Sstevel@tonic-gate 		mpcb->mpcb_wstate = WSTATE_USER32;
1442005Selowe 		mpcb->mpcb_wbuf = kmem_cache_alloc(wbuf32_cache, KM_SLEEP);
1450Sstevel@tonic-gate 	} else {
1460Sstevel@tonic-gate 		mpcb->mpcb_wstate = WSTATE_USER64;
1472005Selowe 		mpcb->mpcb_wbuf = kmem_cache_alloc(wbuf64_cache, KM_SLEEP);
1480Sstevel@tonic-gate 	}
1490Sstevel@tonic-gate 	ASSERT(((uintptr_t)mpcb->mpcb_wbuf & 7) == 0);
1500Sstevel@tonic-gate 	mpcb->mpcb_wbuf_pa = va_to_pa(mpcb->mpcb_wbuf);
1510Sstevel@tonic-gate 	mpcb->mpcb_pa = va_to_pa(mpcb);
1520Sstevel@tonic-gate 	return (stk);
1530Sstevel@tonic-gate }
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate void
1560Sstevel@tonic-gate lwp_stk_fini(klwp_t *lwp)
1570Sstevel@tonic-gate {
1580Sstevel@tonic-gate 	struct machpcb *mpcb = lwptompcb(lwp);
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 	/*
1610Sstevel@tonic-gate 	 * there might be windows still in the wbuf due to unmapped
1620Sstevel@tonic-gate 	 * stack, misaligned stack pointer, etc.  We just free it.
1630Sstevel@tonic-gate 	 */
1640Sstevel@tonic-gate 	mpcb->mpcb_wbcnt = 0;
1650Sstevel@tonic-gate 	if (mpcb->mpcb_wstate == WSTATE_USER32)
1662005Selowe 		kmem_cache_free(wbuf32_cache, mpcb->mpcb_wbuf);
1670Sstevel@tonic-gate 	else
1682005Selowe 		kmem_cache_free(wbuf64_cache, mpcb->mpcb_wbuf);
1690Sstevel@tonic-gate 	mpcb->mpcb_wbuf = NULL;
1700Sstevel@tonic-gate 	mpcb->mpcb_wbuf_pa = -1;
1710Sstevel@tonic-gate }
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate /*
1750Sstevel@tonic-gate  * Copy regs from parent to child.
1760Sstevel@tonic-gate  */
1770Sstevel@tonic-gate void
1780Sstevel@tonic-gate lwp_forkregs(klwp_t *lwp, klwp_t *clwp)
1790Sstevel@tonic-gate {
1800Sstevel@tonic-gate 	kthread_t *t, *pt = lwptot(lwp);
1810Sstevel@tonic-gate 	struct machpcb *mpcb = lwptompcb(clwp);
1820Sstevel@tonic-gate 	struct machpcb *pmpcb = lwptompcb(lwp);
1830Sstevel@tonic-gate 	kfpu_t *fp, *pfp = lwptofpu(lwp);
1840Sstevel@tonic-gate 	caddr_t wbuf;
1850Sstevel@tonic-gate 	uint_t wstate;
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 	t = mpcb->mpcb_thread;
1880Sstevel@tonic-gate 	/*
1890Sstevel@tonic-gate 	 * remember child's fp and wbuf since they will get erased during
1900Sstevel@tonic-gate 	 * the bcopy.
1910Sstevel@tonic-gate 	 */
1920Sstevel@tonic-gate 	fp = mpcb->mpcb_fpu;
1930Sstevel@tonic-gate 	wbuf = mpcb->mpcb_wbuf;
1940Sstevel@tonic-gate 	wstate = mpcb->mpcb_wstate;
1950Sstevel@tonic-gate 	/*
1960Sstevel@tonic-gate 	 * Don't copy mpcb_frame since we hand-crafted it
1970Sstevel@tonic-gate 	 * in thread_load().
1980Sstevel@tonic-gate 	 */
1990Sstevel@tonic-gate 	bcopy(lwp->lwp_regs, clwp->lwp_regs, sizeof (struct machpcb) - REGOFF);
2000Sstevel@tonic-gate 	mpcb->mpcb_thread = t;
2010Sstevel@tonic-gate 	mpcb->mpcb_fpu = fp;
2020Sstevel@tonic-gate 	fp->fpu_q = mpcb->mpcb_fpu_q;
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 	/*
2050Sstevel@tonic-gate 	 * It is theoretically possibly for the lwp's wstate to
2060Sstevel@tonic-gate 	 * be different from its value assigned in lwp_stk_init,
2070Sstevel@tonic-gate 	 * since lwp_stk_init assumed the data model of the process.
2080Sstevel@tonic-gate 	 * Here, we took on the data model of the cloned lwp.
2090Sstevel@tonic-gate 	 */
2100Sstevel@tonic-gate 	if (mpcb->mpcb_wstate != wstate) {
2110Sstevel@tonic-gate 		if (wstate == WSTATE_USER32) {
2122005Selowe 			kmem_cache_free(wbuf32_cache, wbuf);
2132005Selowe 			wbuf = kmem_cache_alloc(wbuf64_cache, KM_SLEEP);
2140Sstevel@tonic-gate 			wstate = WSTATE_USER64;
2150Sstevel@tonic-gate 		} else {
2162005Selowe 			kmem_cache_free(wbuf64_cache, wbuf);
2172005Selowe 			wbuf = kmem_cache_alloc(wbuf32_cache, KM_SLEEP);
2180Sstevel@tonic-gate 			wstate = WSTATE_USER32;
2190Sstevel@tonic-gate 		}
2200Sstevel@tonic-gate 	}
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 	mpcb->mpcb_pa = va_to_pa(mpcb);
2230Sstevel@tonic-gate 	mpcb->mpcb_wbuf = wbuf;
2240Sstevel@tonic-gate 	mpcb->mpcb_wbuf_pa = va_to_pa(wbuf);
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 	ASSERT(mpcb->mpcb_wstate == wstate);
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 	if (mpcb->mpcb_wbcnt != 0) {
2290Sstevel@tonic-gate 		bcopy(pmpcb->mpcb_wbuf, mpcb->mpcb_wbuf,
2300Sstevel@tonic-gate 		    mpcb->mpcb_wbcnt * ((mpcb->mpcb_wstate == WSTATE_USER32) ?
2310Sstevel@tonic-gate 		    sizeof (struct rwindow32) : sizeof (struct rwindow64)));
2320Sstevel@tonic-gate 	}
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 	if (pt == curthread)
2350Sstevel@tonic-gate 		pfp->fpu_fprs = _fp_read_fprs();
2360Sstevel@tonic-gate 	if ((pfp->fpu_en) || (pfp->fpu_fprs & FPRS_FEF)) {
2370Sstevel@tonic-gate 		if (pt == curthread && fpu_exists) {
2380Sstevel@tonic-gate 			save_gsr(clwp->lwp_fpu);
2390Sstevel@tonic-gate 		} else {
2400Sstevel@tonic-gate 			uint64_t gsr;
2410Sstevel@tonic-gate 			gsr = get_gsr(lwp->lwp_fpu);
2420Sstevel@tonic-gate 			set_gsr(gsr, clwp->lwp_fpu);
2430Sstevel@tonic-gate 		}
2440Sstevel@tonic-gate 		fp_fork(lwp, clwp);
2450Sstevel@tonic-gate 	}
2460Sstevel@tonic-gate }
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate /*
2490Sstevel@tonic-gate  * Free lwp fpu regs.
2500Sstevel@tonic-gate  */
2510Sstevel@tonic-gate void
2520Sstevel@tonic-gate lwp_freeregs(klwp_t *lwp, int isexec)
2530Sstevel@tonic-gate {
2540Sstevel@tonic-gate 	kfpu_t *fp = lwptofpu(lwp);
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate 	if (lwptot(lwp) == curthread)
2570Sstevel@tonic-gate 		fp->fpu_fprs = _fp_read_fprs();
2580Sstevel@tonic-gate 	if ((fp->fpu_en) || (fp->fpu_fprs & FPRS_FEF))
2590Sstevel@tonic-gate 		fp_free(fp, isexec);
2600Sstevel@tonic-gate }
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate /*
2630Sstevel@tonic-gate  * fill in the extra register state area specified with the
2640Sstevel@tonic-gate  * specified lwp's platform-dependent non-floating-point extra
2650Sstevel@tonic-gate  * register state information
2660Sstevel@tonic-gate  */
2670Sstevel@tonic-gate /* ARGSUSED */
2680Sstevel@tonic-gate void
2690Sstevel@tonic-gate xregs_getgfiller(klwp_id_t lwp, caddr_t xrp)
2700Sstevel@tonic-gate {
2710Sstevel@tonic-gate 	/* for sun4u nothing to do here, added for symmetry */
2720Sstevel@tonic-gate }
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate /*
2750Sstevel@tonic-gate  * fill in the extra register state area specified with the specified lwp's
2760Sstevel@tonic-gate  * platform-dependent floating-point extra register state information.
2770Sstevel@tonic-gate  * NOTE:  'lwp' might not correspond to 'curthread' since this is
2780Sstevel@tonic-gate  * called from code in /proc to get the registers of another lwp.
2790Sstevel@tonic-gate  */
2800Sstevel@tonic-gate void
2810Sstevel@tonic-gate xregs_getfpfiller(klwp_id_t lwp, caddr_t xrp)
2820Sstevel@tonic-gate {
2830Sstevel@tonic-gate 	prxregset_t *xregs = (prxregset_t *)xrp;
2840Sstevel@tonic-gate 	kfpu_t *fp = lwptofpu(lwp);
2850Sstevel@tonic-gate 	uint32_t fprs = (FPRS_FEF|FPRS_DU|FPRS_DL);
2860Sstevel@tonic-gate 	uint64_t gsr;
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate 	/*
2890Sstevel@tonic-gate 	 * fp_fksave() does not flush the GSR register into
2900Sstevel@tonic-gate 	 * the lwp area, so do it now
2910Sstevel@tonic-gate 	 */
2920Sstevel@tonic-gate 	kpreempt_disable();
2930Sstevel@tonic-gate 	if (ttolwp(curthread) == lwp && fpu_exists) {
2940Sstevel@tonic-gate 		fp->fpu_fprs = _fp_read_fprs();
2950Sstevel@tonic-gate 		if ((fp->fpu_fprs & FPRS_FEF) != FPRS_FEF) {
2960Sstevel@tonic-gate 			_fp_write_fprs(fprs);
2970Sstevel@tonic-gate 			fp->fpu_fprs = (V9_FPU_FPRS_TYPE)fprs;
2980Sstevel@tonic-gate 		}
2990Sstevel@tonic-gate 		save_gsr(fp);
3000Sstevel@tonic-gate 	}
3010Sstevel@tonic-gate 	gsr = get_gsr(fp);
3020Sstevel@tonic-gate 	kpreempt_enable();
3030Sstevel@tonic-gate 	PRXREG_GSR(xregs) = gsr;
3040Sstevel@tonic-gate }
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate /*
3070Sstevel@tonic-gate  * set the specified lwp's platform-dependent non-floating-point
3080Sstevel@tonic-gate  * extra register state based on the specified input
3090Sstevel@tonic-gate  */
3100Sstevel@tonic-gate /* ARGSUSED */
3110Sstevel@tonic-gate void
3120Sstevel@tonic-gate xregs_setgfiller(klwp_id_t lwp, caddr_t xrp)
3130Sstevel@tonic-gate {
3140Sstevel@tonic-gate 	/* for sun4u nothing to do here, added for symmetry */
3150Sstevel@tonic-gate }
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate /*
3180Sstevel@tonic-gate  * set the specified lwp's platform-dependent floating-point
3190Sstevel@tonic-gate  * extra register state based on the specified input
3200Sstevel@tonic-gate  */
3210Sstevel@tonic-gate void
3220Sstevel@tonic-gate xregs_setfpfiller(klwp_id_t lwp, caddr_t xrp)
3230Sstevel@tonic-gate {
3240Sstevel@tonic-gate 	prxregset_t *xregs = (prxregset_t *)xrp;
3250Sstevel@tonic-gate 	kfpu_t *fp = lwptofpu(lwp);
3260Sstevel@tonic-gate 	uint32_t fprs = (FPRS_FEF|FPRS_DU|FPRS_DL);
3270Sstevel@tonic-gate 	uint64_t gsr = PRXREG_GSR(xregs);
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate 	kpreempt_disable();
3300Sstevel@tonic-gate 	set_gsr(gsr, lwptofpu(lwp));
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 	if ((lwp == ttolwp(curthread)) && fpu_exists) {
3330Sstevel@tonic-gate 		fp->fpu_fprs = _fp_read_fprs();
3340Sstevel@tonic-gate 		if ((fp->fpu_fprs & FPRS_FEF) != FPRS_FEF) {
3350Sstevel@tonic-gate 			_fp_write_fprs(fprs);
3360Sstevel@tonic-gate 			fp->fpu_fprs = (V9_FPU_FPRS_TYPE)fprs;
3370Sstevel@tonic-gate 		}
3380Sstevel@tonic-gate 		restore_gsr(lwptofpu(lwp));
3390Sstevel@tonic-gate 	}
3400Sstevel@tonic-gate 	kpreempt_enable();
3410Sstevel@tonic-gate }
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate /*
3440Sstevel@tonic-gate  * fill in the sun4u asrs, ie, the lwp's platform-dependent
3450Sstevel@tonic-gate  * non-floating-point extra register state information
3460Sstevel@tonic-gate  */
3470Sstevel@tonic-gate /* ARGSUSED */
3480Sstevel@tonic-gate void
3490Sstevel@tonic-gate getasrs(klwp_t *lwp, asrset_t asr)
3500Sstevel@tonic-gate {
3510Sstevel@tonic-gate 	/* for sun4u nothing to do here, added for symmetry */
3520Sstevel@tonic-gate }
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate /*
3550Sstevel@tonic-gate  * fill in the sun4u asrs, ie, the lwp's platform-dependent
3560Sstevel@tonic-gate  * floating-point extra register state information
3570Sstevel@tonic-gate  */
3580Sstevel@tonic-gate void
3590Sstevel@tonic-gate getfpasrs(klwp_t *lwp, asrset_t asr)
3600Sstevel@tonic-gate {
3610Sstevel@tonic-gate 	kfpu_t *fp = lwptofpu(lwp);
3620Sstevel@tonic-gate 	uint32_t fprs = (FPRS_FEF|FPRS_DU|FPRS_DL);
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 	kpreempt_disable();
3650Sstevel@tonic-gate 	if (ttolwp(curthread) == lwp)
3660Sstevel@tonic-gate 		fp->fpu_fprs = _fp_read_fprs();
3670Sstevel@tonic-gate 	if ((fp->fpu_en) || (fp->fpu_fprs & FPRS_FEF)) {
3680Sstevel@tonic-gate 		if (fpu_exists && ttolwp(curthread) == lwp) {
3690Sstevel@tonic-gate 			if ((fp->fpu_fprs & FPRS_FEF) != FPRS_FEF) {
3700Sstevel@tonic-gate 				_fp_write_fprs(fprs);
3710Sstevel@tonic-gate 				fp->fpu_fprs = (V9_FPU_FPRS_TYPE)fprs;
3720Sstevel@tonic-gate 			}
3730Sstevel@tonic-gate 			save_gsr(fp);
3740Sstevel@tonic-gate 		}
3750Sstevel@tonic-gate 		asr[ASR_GSR] = (int64_t)get_gsr(fp);
3760Sstevel@tonic-gate 	}
3770Sstevel@tonic-gate 	kpreempt_enable();
3780Sstevel@tonic-gate }
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate /*
3810Sstevel@tonic-gate  * set the sun4u asrs, ie, the lwp's platform-dependent
3820Sstevel@tonic-gate  * non-floating-point extra register state information
3830Sstevel@tonic-gate  */
3840Sstevel@tonic-gate /* ARGSUSED */
3850Sstevel@tonic-gate void
3860Sstevel@tonic-gate setasrs(klwp_t *lwp, asrset_t asr)
3870Sstevel@tonic-gate {
3880Sstevel@tonic-gate 	/* for sun4u nothing to do here, added for symmetry */
3890Sstevel@tonic-gate }
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate void
3920Sstevel@tonic-gate setfpasrs(klwp_t *lwp, asrset_t asr)
3930Sstevel@tonic-gate {
3940Sstevel@tonic-gate 	kfpu_t *fp = lwptofpu(lwp);
3950Sstevel@tonic-gate 	uint32_t fprs = (FPRS_FEF|FPRS_DU|FPRS_DL);
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 	kpreempt_disable();
3980Sstevel@tonic-gate 	if (ttolwp(curthread) == lwp)
3990Sstevel@tonic-gate 		fp->fpu_fprs = _fp_read_fprs();
4000Sstevel@tonic-gate 	if ((fp->fpu_en) || (fp->fpu_fprs & FPRS_FEF)) {
4010Sstevel@tonic-gate 		set_gsr(asr[ASR_GSR], fp);
4020Sstevel@tonic-gate 		if (fpu_exists && ttolwp(curthread) == lwp) {
4030Sstevel@tonic-gate 			if ((fp->fpu_fprs & FPRS_FEF) != FPRS_FEF) {
4040Sstevel@tonic-gate 				_fp_write_fprs(fprs);
4050Sstevel@tonic-gate 				fp->fpu_fprs = (V9_FPU_FPRS_TYPE)fprs;
4060Sstevel@tonic-gate 			}
4070Sstevel@tonic-gate 			restore_gsr(fp);
4080Sstevel@tonic-gate 		}
4090Sstevel@tonic-gate 	}
4100Sstevel@tonic-gate 	kpreempt_enable();
4110Sstevel@tonic-gate }
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate /*
4140Sstevel@tonic-gate  * Create interrupt kstats for this CPU.
4150Sstevel@tonic-gate  */
4160Sstevel@tonic-gate void
4170Sstevel@tonic-gate cpu_create_intrstat(cpu_t *cp)
4180Sstevel@tonic-gate {
4190Sstevel@tonic-gate 	int		i;
4200Sstevel@tonic-gate 	kstat_t		*intr_ksp;
4210Sstevel@tonic-gate 	kstat_named_t	*knp;
4220Sstevel@tonic-gate 	char		name[KSTAT_STRLEN];
4230Sstevel@tonic-gate 	zoneid_t	zoneid;
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate 	if (pool_pset_enabled())
4280Sstevel@tonic-gate 		zoneid = GLOBAL_ZONEID;
4290Sstevel@tonic-gate 	else
4300Sstevel@tonic-gate 		zoneid = ALL_ZONES;
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate 	intr_ksp = kstat_create_zone("cpu", cp->cpu_id, "intrstat", "misc",
4330Sstevel@tonic-gate 	    KSTAT_TYPE_NAMED, PIL_MAX * 2, NULL, zoneid);
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate 	/*
4360Sstevel@tonic-gate 	 * Initialize each PIL's named kstat
4370Sstevel@tonic-gate 	 */
4380Sstevel@tonic-gate 	if (intr_ksp != NULL) {
4390Sstevel@tonic-gate 		intr_ksp->ks_update = cpu_kstat_intrstat_update;
4400Sstevel@tonic-gate 		knp = (kstat_named_t *)intr_ksp->ks_data;
4410Sstevel@tonic-gate 		intr_ksp->ks_private = cp;
4420Sstevel@tonic-gate 		for (i = 0; i < PIL_MAX; i++) {
4430Sstevel@tonic-gate 			(void) snprintf(name, KSTAT_STRLEN, "level-%d-time",
4440Sstevel@tonic-gate 			    i + 1);
4450Sstevel@tonic-gate 			kstat_named_init(&knp[i * 2], name, KSTAT_DATA_UINT64);
4460Sstevel@tonic-gate 			(void) snprintf(name, KSTAT_STRLEN, "level-%d-count",
4470Sstevel@tonic-gate 			    i + 1);
4480Sstevel@tonic-gate 			kstat_named_init(&knp[(i * 2) + 1], name,
4490Sstevel@tonic-gate 			    KSTAT_DATA_UINT64);
4500Sstevel@tonic-gate 		}
4510Sstevel@tonic-gate 		kstat_install(intr_ksp);
4520Sstevel@tonic-gate 	}
4530Sstevel@tonic-gate }
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate /*
4560Sstevel@tonic-gate  * Delete interrupt kstats for this CPU.
4570Sstevel@tonic-gate  */
4580Sstevel@tonic-gate void
4590Sstevel@tonic-gate cpu_delete_intrstat(cpu_t *cp)
4600Sstevel@tonic-gate {
4610Sstevel@tonic-gate 	kstat_delete_byname_zone("cpu", cp->cpu_id, "intrstat", ALL_ZONES);
4620Sstevel@tonic-gate }
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate /*
4650Sstevel@tonic-gate  * Convert interrupt statistics from CPU ticks to nanoseconds and
4660Sstevel@tonic-gate  * update kstat.
4670Sstevel@tonic-gate  */
4680Sstevel@tonic-gate int
4690Sstevel@tonic-gate cpu_kstat_intrstat_update(kstat_t *ksp, int rw)
4700Sstevel@tonic-gate {
4710Sstevel@tonic-gate 	kstat_named_t	*knp = ksp->ks_data;
4720Sstevel@tonic-gate 	cpu_t		*cpup = (cpu_t *)ksp->ks_private;
4730Sstevel@tonic-gate 	int		i;
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate 	if (rw == KSTAT_WRITE)
4760Sstevel@tonic-gate 		return (EACCES);
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate 	/*
4790Sstevel@tonic-gate 	 * We use separate passes to copy and convert the statistics to
4800Sstevel@tonic-gate 	 * nanoseconds. This assures that the snapshot of the data is as
4810Sstevel@tonic-gate 	 * self-consistent as possible.
4820Sstevel@tonic-gate 	 */
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate 	for (i = 0; i < PIL_MAX; i++) {
4850Sstevel@tonic-gate 		knp[i * 2].value.ui64 = cpup->cpu_m.intrstat[i + 1][0];
4860Sstevel@tonic-gate 		knp[(i * 2) + 1].value.ui64 = cpup->cpu_stats.sys.intr[i];
4870Sstevel@tonic-gate 	}
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate 	for (i = 0; i < PIL_MAX; i++) {
4900Sstevel@tonic-gate 		knp[i * 2].value.ui64 =
4910Sstevel@tonic-gate 		    (uint64_t)tick2ns((hrtime_t)knp[i * 2].value.ui64,
4920Sstevel@tonic-gate 			cpup->cpu_id);
4930Sstevel@tonic-gate 	}
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate 	return (0);
4960Sstevel@tonic-gate }
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate /*
4990Sstevel@tonic-gate  * Called by common/os/cpu.c for psrinfo(1m) kstats
5000Sstevel@tonic-gate  */
5010Sstevel@tonic-gate char *
5020Sstevel@tonic-gate cpu_fru_fmri(cpu_t *cp)
5030Sstevel@tonic-gate {
5040Sstevel@tonic-gate 	return (cpunodes[cp->cpu_id].fru_fmri);
5050Sstevel@tonic-gate }
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate /*
5080Sstevel@tonic-gate  * An interrupt thread is ending a time slice, so compute the interval it
5090Sstevel@tonic-gate  * ran for and update the statistic for its PIL.
5100Sstevel@tonic-gate  */
5110Sstevel@tonic-gate void
5120Sstevel@tonic-gate cpu_intr_swtch_enter(kthread_id_t t)
5130Sstevel@tonic-gate {
5140Sstevel@tonic-gate 	uint64_t	interval;
5150Sstevel@tonic-gate 	uint64_t	start;
516590Sesolom 	cpu_t		*cpu;
5170Sstevel@tonic-gate 
5180Sstevel@tonic-gate 	ASSERT((t->t_flag & T_INTR_THREAD) != 0);
5190Sstevel@tonic-gate 	ASSERT(t->t_pil > 0 && t->t_pil <= LOCK_LEVEL);
5200Sstevel@tonic-gate 
5210Sstevel@tonic-gate 	/*
5220Sstevel@tonic-gate 	 * We could be here with a zero timestamp. This could happen if:
5230Sstevel@tonic-gate 	 * an interrupt thread which no longer has a pinned thread underneath
5240Sstevel@tonic-gate 	 * it (i.e. it blocked at some point in its past) has finished running
5250Sstevel@tonic-gate 	 * its handler. intr_thread() updated the interrupt statistic for its
5260Sstevel@tonic-gate 	 * PIL and zeroed its timestamp. Since there was no pinned thread to
5270Sstevel@tonic-gate 	 * return to, swtch() gets called and we end up here.
5280Sstevel@tonic-gate 	 *
5290Sstevel@tonic-gate 	 * It can also happen if an interrupt thread in intr_thread() calls
5300Sstevel@tonic-gate 	 * preempt. It will have already taken care of updating stats. In
5310Sstevel@tonic-gate 	 * this event, the interrupt thread will be runnable.
5320Sstevel@tonic-gate 	 */
5330Sstevel@tonic-gate 	if (t->t_intr_start) {
5340Sstevel@tonic-gate 		do {
5350Sstevel@tonic-gate 			start = t->t_intr_start;
5360Sstevel@tonic-gate 			interval = gettick_counter() - start;
5370Sstevel@tonic-gate 		} while (cas64(&t->t_intr_start, start, 0) != start);
538590Sesolom 		cpu = CPU;
539590Sesolom 		if (cpu->cpu_m.divisor > 1)
540590Sesolom 			interval *= cpu->cpu_m.divisor;
541590Sesolom 		cpu->cpu_m.intrstat[t->t_pil][0] += interval;
542590Sesolom 
543590Sesolom 		atomic_add_64((uint64_t *)&cpu->cpu_intracct[cpu->cpu_mstate],
544590Sesolom 		    interval);
5450Sstevel@tonic-gate 	} else
5460Sstevel@tonic-gate 		ASSERT(t->t_intr == NULL || t->t_state == TS_RUN);
5470Sstevel@tonic-gate }
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate 
5500Sstevel@tonic-gate /*
5510Sstevel@tonic-gate  * An interrupt thread is returning from swtch(). Place a starting timestamp
5520Sstevel@tonic-gate  * in its thread structure.
5530Sstevel@tonic-gate  */
5540Sstevel@tonic-gate void
5550Sstevel@tonic-gate cpu_intr_swtch_exit(kthread_id_t t)
5560Sstevel@tonic-gate {
5570Sstevel@tonic-gate 	uint64_t ts;
5580Sstevel@tonic-gate 
5590Sstevel@tonic-gate 	ASSERT((t->t_flag & T_INTR_THREAD) != 0);
5600Sstevel@tonic-gate 	ASSERT(t->t_pil > 0 && t->t_pil <= LOCK_LEVEL);
5610Sstevel@tonic-gate 
5620Sstevel@tonic-gate 	do {
5630Sstevel@tonic-gate 		ts = t->t_intr_start;
5640Sstevel@tonic-gate 	} while (cas64(&t->t_intr_start, ts, gettick_counter()) != ts);
5650Sstevel@tonic-gate }
5660Sstevel@tonic-gate 
5670Sstevel@tonic-gate 
5680Sstevel@tonic-gate int
5690Sstevel@tonic-gate blacklist(int cmd, const char *scheme, nvlist_t *fmri, const char *class)
5700Sstevel@tonic-gate {
5710Sstevel@tonic-gate 	if (&plat_blacklist)
5720Sstevel@tonic-gate 		return (plat_blacklist(cmd, scheme, fmri, class));
5730Sstevel@tonic-gate 
5740Sstevel@tonic-gate 	return (ENOTSUP);
5750Sstevel@tonic-gate }
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate int
5780Sstevel@tonic-gate kdi_pread(caddr_t buf, size_t nbytes, uint64_t addr, size_t *ncopiedp)
5790Sstevel@tonic-gate {
5800Sstevel@tonic-gate 	extern void kdi_flush_caches(void);
5810Sstevel@tonic-gate 	size_t nread = 0;
5820Sstevel@tonic-gate 	uint32_t word;
5830Sstevel@tonic-gate 	int slop, i;
5840Sstevel@tonic-gate 
5850Sstevel@tonic-gate 	kdi_flush_caches();
5860Sstevel@tonic-gate 	membar_enter();
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate 	/* We might not begin on a word boundary. */
5890Sstevel@tonic-gate 	if ((slop = addr & 3) != 0) {
5900Sstevel@tonic-gate 		word = ldphys(addr & ~3);
5910Sstevel@tonic-gate 		for (i = slop; i < 4 && nbytes > 0; i++, nbytes--, nread++)
5920Sstevel@tonic-gate 			*buf++ = ((uchar_t *)&word)[i];
5930Sstevel@tonic-gate 		addr = roundup(addr, 4);
5940Sstevel@tonic-gate 	}
5950Sstevel@tonic-gate 
5960Sstevel@tonic-gate 	while (nbytes > 0) {
5970Sstevel@tonic-gate 		word = ldphys(addr);
5980Sstevel@tonic-gate 		for (i = 0; i < 4 && nbytes > 0; i++, nbytes--, nread++, addr++)
5990Sstevel@tonic-gate 			*buf++ = ((uchar_t *)&word)[i];
6000Sstevel@tonic-gate 	}
6010Sstevel@tonic-gate 
6020Sstevel@tonic-gate 	kdi_flush_caches();
6030Sstevel@tonic-gate 
6040Sstevel@tonic-gate 	*ncopiedp = nread;
6050Sstevel@tonic-gate 	return (0);
6060Sstevel@tonic-gate }
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate int
6090Sstevel@tonic-gate kdi_pwrite(caddr_t buf, size_t nbytes, uint64_t addr, size_t *ncopiedp)
6100Sstevel@tonic-gate {
6110Sstevel@tonic-gate 	extern void kdi_flush_caches(void);
6120Sstevel@tonic-gate 	size_t nwritten = 0;
6130Sstevel@tonic-gate 	uint32_t word;
6140Sstevel@tonic-gate 	int slop, i;
6150Sstevel@tonic-gate 
6160Sstevel@tonic-gate 	kdi_flush_caches();
6170Sstevel@tonic-gate 
6180Sstevel@tonic-gate 	/* We might not begin on a word boundary. */
6190Sstevel@tonic-gate 	if ((slop = addr & 3) != 0) {
6200Sstevel@tonic-gate 		word = ldphys(addr & ~3);
6210Sstevel@tonic-gate 		for (i = slop; i < 4 && nbytes > 0; i++, nbytes--, nwritten++)
6220Sstevel@tonic-gate 			((uchar_t *)&word)[i] = *buf++;
6230Sstevel@tonic-gate 		stphys(addr & ~3, word);
6240Sstevel@tonic-gate 		addr = roundup(addr, 4);
6250Sstevel@tonic-gate 	}
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate 	while (nbytes > 3) {
6280Sstevel@tonic-gate 		for (word = 0, i = 0; i < 4; i++, nbytes--, nwritten++)
6290Sstevel@tonic-gate 			((uchar_t *)&word)[i] = *buf++;
6300Sstevel@tonic-gate 		stphys(addr, word);
6310Sstevel@tonic-gate 		addr += 4;
6320Sstevel@tonic-gate 	}
6330Sstevel@tonic-gate 
6340Sstevel@tonic-gate 	/* We might not end with a whole word. */
6350Sstevel@tonic-gate 	if (nbytes > 0) {
6360Sstevel@tonic-gate 		word = ldphys(addr);
6370Sstevel@tonic-gate 		for (i = 0; nbytes > 0; i++, nbytes--, nwritten++)
6380Sstevel@tonic-gate 			((uchar_t *)&word)[i] = *buf++;
6390Sstevel@tonic-gate 		stphys(addr, word);
6400Sstevel@tonic-gate 	}
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate 	membar_enter();
6430Sstevel@tonic-gate 	kdi_flush_caches();
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate 	*ncopiedp = nwritten;
6460Sstevel@tonic-gate 	return (0);
6470Sstevel@tonic-gate }
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate static void
6500Sstevel@tonic-gate kdi_kernpanic(struct regs *regs, uint_t tt)
6510Sstevel@tonic-gate {
6520Sstevel@tonic-gate 	sync_reg_buf = *regs;
6530Sstevel@tonic-gate 	sync_tt = tt;
6540Sstevel@tonic-gate 
6550Sstevel@tonic-gate 	sync_handler();
6560Sstevel@tonic-gate }
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate static void
6590Sstevel@tonic-gate kdi_plat_call(void (*platfn)(void))
6600Sstevel@tonic-gate {
6610Sstevel@tonic-gate 	if (platfn != NULL) {
6620Sstevel@tonic-gate 		prom_suspend_prepost();
6630Sstevel@tonic-gate 		platfn();
6640Sstevel@tonic-gate 		prom_resume_prepost();
6650Sstevel@tonic-gate 	}
6660Sstevel@tonic-gate }
6670Sstevel@tonic-gate 
6680Sstevel@tonic-gate void
6690Sstevel@tonic-gate mach_kdi_init(kdi_t *kdi)
6700Sstevel@tonic-gate {
6710Sstevel@tonic-gate 	kdi->kdi_plat_call = kdi_plat_call;
6720Sstevel@tonic-gate 	kdi->mkdi_cpu_index = kdi_cpu_index;
6730Sstevel@tonic-gate 	kdi->mkdi_trap_vatotte = kdi_trap_vatotte;
6740Sstevel@tonic-gate 	kdi->mkdi_kernpanic = kdi_kernpanic;
6750Sstevel@tonic-gate }
676590Sesolom 
677590Sesolom 
678590Sesolom /*
679590Sesolom  * get_cpu_mstate() is passed an array of timestamps, NCMSTATES
680590Sesolom  * long, and it fills in the array with the time spent on cpu in
681590Sesolom  * each of the mstates, where time is returned in nsec.
682590Sesolom  *
683590Sesolom  * No guarantee is made that the returned values in times[] will
684590Sesolom  * monotonically increase on sequential calls, although this will
685590Sesolom  * be true in the long run. Any such guarantee must be handled by
686590Sesolom  * the caller, if needed. This can happen if we fail to account
687590Sesolom  * for elapsed time due to a generation counter conflict, yet we
688590Sesolom  * did account for it on a prior call (see below).
689590Sesolom  *
690590Sesolom  * The complication is that the cpu in question may be updating
691590Sesolom  * its microstate at the same time that we are reading it.
692590Sesolom  * Because the microstate is only updated when the CPU's state
693590Sesolom  * changes, the values in cpu_intracct[] can be indefinitely out
694590Sesolom  * of date. To determine true current values, it is necessary to
695590Sesolom  * compare the current time with cpu_mstate_start, and add the
696590Sesolom  * difference to times[cpu_mstate].
697590Sesolom  *
698590Sesolom  * This can be a problem if those values are changing out from
699590Sesolom  * under us. Because the code path in new_cpu_mstate() is
700590Sesolom  * performance critical, we have not added a lock to it. Instead,
701590Sesolom  * we have added a generation counter. Before beginning
702590Sesolom  * modifications, the counter is set to 0. After modifications,
703590Sesolom  * it is set to the old value plus one.
704590Sesolom  *
705590Sesolom  * get_cpu_mstate() will not consider the values of cpu_mstate
706590Sesolom  * and cpu_mstate_start to be usable unless the value of
707590Sesolom  * cpu_mstate_gen is both non-zero and unchanged, both before and
708590Sesolom  * after reading the mstate information. Note that we must
709590Sesolom  * protect against out-of-order loads around accesses to the
710590Sesolom  * generation counter. Also, this is a best effort approach in
711590Sesolom  * that we do not retry should the counter be found to have
712590Sesolom  * changed.
713590Sesolom  *
714590Sesolom  * cpu_intracct[] is used to identify time spent in each CPU
715590Sesolom  * mstate while handling interrupts. Such time should be reported
716590Sesolom  * against system time, and so is subtracted out from its
717590Sesolom  * corresponding cpu_acct[] time and added to
718590Sesolom  * cpu_acct[CMS_SYSTEM]. Additionally, intracct time is stored in
719590Sesolom  * %ticks, but acct time may be stored as %sticks, thus requiring
720590Sesolom  * different conversions before they can be compared.
721590Sesolom  */
722590Sesolom 
723590Sesolom void
724590Sesolom get_cpu_mstate(cpu_t *cpu, hrtime_t *times)
725590Sesolom {
726590Sesolom 	int i;
727590Sesolom 	hrtime_t now, start;
728590Sesolom 	uint16_t gen;
729590Sesolom 	uint16_t state;
730590Sesolom 	hrtime_t intracct[NCMSTATES];
731590Sesolom 
732590Sesolom 	/*
733590Sesolom 	 * Load all volatile state under the protection of membar.
734590Sesolom 	 * cpu_acct[cpu_mstate] must be loaded to avoid double counting
735590Sesolom 	 * of (now - cpu_mstate_start) by a change in CPU mstate that
736590Sesolom 	 * arrives after we make our last check of cpu_mstate_gen.
737590Sesolom 	 */
738590Sesolom 
739590Sesolom 	now = gethrtime_unscaled();
740590Sesolom 	gen = cpu->cpu_mstate_gen;
741590Sesolom 
742590Sesolom 	membar_consumer();	/* guarantee load ordering */
743590Sesolom 	start = cpu->cpu_mstate_start;
744590Sesolom 	state = cpu->cpu_mstate;
745590Sesolom 	for (i = 0; i < NCMSTATES; i++) {
746590Sesolom 		intracct[i] = cpu->cpu_intracct[i];
747590Sesolom 		times[i] = cpu->cpu_acct[i];
748590Sesolom 	}
749590Sesolom 	membar_consumer();	/* guarantee load ordering */
750590Sesolom 
751590Sesolom 	if (gen != 0 && gen == cpu->cpu_mstate_gen && now > start)
752590Sesolom 		times[state] += now - start;
753590Sesolom 
754590Sesolom 	for (i = 0; i < NCMSTATES; i++) {
755590Sesolom 		scalehrtime(&times[i]);
756590Sesolom 		intracct[i] = tick2ns((hrtime_t)intracct[i], cpu->cpu_id);
757590Sesolom 	}
758590Sesolom 
759590Sesolom 	for (i = 0; i < NCMSTATES; i++) {
760590Sesolom 		if (i == CMS_SYSTEM)
761590Sesolom 			continue;
762590Sesolom 		times[i] -= intracct[i];
763590Sesolom 		if (times[i] < 0) {
764590Sesolom 			intracct[i] += times[i];
765590Sesolom 			times[i] = 0;
766590Sesolom 		}
767590Sesolom 		times[CMS_SYSTEM] += intracct[i];
768590Sesolom 	}
769590Sesolom }
770