17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
5a859926elowe * Common Development and Distribution License (the "License").
6a859926elowe * You may not use this file except in compliance with the License.
77c478bdstevel@tonic-gate *
87c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bdstevel@tonic-gate * See the License for the specific language governing permissions
117c478bdstevel@tonic-gate * and limitations under the License.
127c478bdstevel@tonic-gate *
137c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bdstevel@tonic-gate *
197c478bdstevel@tonic-gate * CDDL HEADER END
207c478bdstevel@tonic-gate */
217c478bdstevel@tonic-gate/*
22bd28a47Prashanth Sreenivasa * Copyright (c) 1993, 2010, Oracle and/or its affiliates. All rights reserved.
23088d69fJerry Jelinek * Copyright (c) 2017, Joyent, Inc.  All rights reserved.
247c478bdstevel@tonic-gate */
257c478bdstevel@tonic-gate
267c478bdstevel@tonic-gate#include <sys/types.h>
277c478bdstevel@tonic-gate#include <sys/kstat.h>
287c478bdstevel@tonic-gate#include <sys/param.h>
297c478bdstevel@tonic-gate#include <sys/stack.h>
307c478bdstevel@tonic-gate#include <sys/regset.h>
317c478bdstevel@tonic-gate#include <sys/thread.h>
327c478bdstevel@tonic-gate#include <sys/proc.h>
337c478bdstevel@tonic-gate#include <sys/procfs_isa.h>
347c478bdstevel@tonic-gate#include <sys/kmem.h>
357c478bdstevel@tonic-gate#include <sys/cpuvar.h>
367c478bdstevel@tonic-gate#include <sys/systm.h>
377c478bdstevel@tonic-gate#include <sys/machpcb.h>
387c478bdstevel@tonic-gate#include <sys/machasi.h>
397c478bdstevel@tonic-gate#include <sys/vis.h>
407c478bdstevel@tonic-gate#include <sys/fpu/fpusystm.h>
417c478bdstevel@tonic-gate#include <sys/cpu_module.h>
427c478bdstevel@tonic-gate#include <sys/privregs.h>
437c478bdstevel@tonic-gate#include <sys/archsystm.h>
447c478bdstevel@tonic-gate#include <sys/atomic.h>
457c478bdstevel@tonic-gate#include <sys/cmn_err.h>
467c478bdstevel@tonic-gate#include <sys/time.h>
477c478bdstevel@tonic-gate#include <sys/clock.h>
487c478bdstevel@tonic-gate#include <sys/cmp.h>
497c478bdstevel@tonic-gate#include <sys/platform_module.h>
507c478bdstevel@tonic-gate#include <sys/bl.h>
517c478bdstevel@tonic-gate#include <sys/nvpair.h>
527c478bdstevel@tonic-gate#include <sys/kdi_impl.h>
537c478bdstevel@tonic-gate#include <sys/machsystm.h>
547c478bdstevel@tonic-gate#include <sys/sysmacros.h>
557c478bdstevel@tonic-gate#include <sys/promif.h>
567c478bdstevel@tonic-gate#include <sys/pool_pset.h>
57ae115bcmrj#include <sys/mem.h>
58ae115bcmrj#include <sys/dumphdr.h>
59a859926elowe#include <vm/seg_kmem.h>
60ae115bcmrj#include <sys/hold_page.h>
61ae115bcmrj#include <sys/cpu.h>
62d3d5073Rafael Vanoni#include <sys/ivintr.h>
63d3d5073Rafael Vanoni#include <sys/clock_impl.h>
64bd28a47Prashanth Sreenivasa#include <sys/machclock.h>
657c478bdstevel@tonic-gate
667c478bdstevel@tonic-gateint maxphys = MMU_PAGESIZE * 16;	/* 128k */
677c478bdstevel@tonic-gateint klustsize = MMU_PAGESIZE * 16;	/* 128k */
687c478bdstevel@tonic-gate
697c478bdstevel@tonic-gate/*
707c478bdstevel@tonic-gate * Initialize kernel thread's stack.
717c478bdstevel@tonic-gate */
727c478bdstevel@tonic-gatecaddr_t
737c478bdstevel@tonic-gatethread_stk_init(caddr_t stk)
747c478bdstevel@tonic-gate{
757c478bdstevel@tonic-gate	kfpu_t *fp;
767c478bdstevel@tonic-gate	ulong_t align;
777c478bdstevel@tonic-gate
787c478bdstevel@tonic-gate	/* allocate extra space for floating point state */
797c478bdstevel@tonic-gate	stk -= SA(sizeof (kfpu_t) + GSR_SIZE);
807c478bdstevel@tonic-gate	align = (uintptr_t)stk & 0x3f;
817c478bdstevel@tonic-gate	stk -= align;		/* force v9_fpu to be 16 byte aligned */
827c478bdstevel@tonic-gate	fp = (kfpu_t *)stk;
837c478bdstevel@tonic-gate	fp->fpu_fprs = 0;
847c478bdstevel@tonic-gate
857c478bdstevel@tonic-gate	stk -= SA(MINFRAME);
867c478bdstevel@tonic-gate	return (stk);
877c478bdstevel@tonic-gate}
887c478bdstevel@tonic-gate
89a859926elowe#define	WIN32_SIZE	(MAXWIN * sizeof (struct rwindow32))
90a859926elowe#define	WIN64_SIZE	(MAXWIN * sizeof (struct rwindow64))
91a859926elowe
92a859926elowekmem_cache_t	*wbuf32_cache;
93a859926elowekmem_cache_t	*wbuf64_cache;
94a859926elowe
95a859926elowevoid
96a859926elowelwp_stk_cache_init(void)
97a859926elowe{
9838b87cdelowe	/*
9938b87cdelowe	 * Window buffers are allocated from the static arena
10038b87cdelowe	 * because they are accessed at TL>0. We also must use
10138b87cdelowe	 * KMC_NOHASH to prevent them from straddling page
10238b87cdelowe	 * boundaries as they are accessed by physical address.
10338b87cdelowe	 */
104a859926elowe	wbuf32_cache = kmem_cache_create("wbuf32_cache", WIN32_SIZE,
10538b87cdelowe	    0, NULL, NULL, NULL, NULL, static_arena, KMC_NOHASH);
106a859926elowe	wbuf64_cache = kmem_cache_create("wbuf64_cache", WIN64_SIZE,
10738b87cdelowe	    0, NULL, NULL, NULL, NULL, static_arena, KMC_NOHASH);
108a859926elowe}
109a859926elowe
1107c478bdstevel@tonic-gate/*
1117c478bdstevel@tonic-gate * Initialize lwp's kernel stack.
1127c478bdstevel@tonic-gate * Note that now that the floating point register save area (kfpu_t)
1137c478bdstevel@tonic-gate * has been broken out from machpcb and aligned on a 64 byte boundary so that
1147c478bdstevel@tonic-gate * we can do block load/stores to/from it, there are a couple of potential
1157c478bdstevel@tonic-gate * optimizations to save stack space. 1. The floating point register save
1167c478bdstevel@tonic-gate * area could be aligned on a 16 byte boundary, and the floating point code
1177c478bdstevel@tonic-gate * changed to (a) check the alignment and (b) use different save/restore
1187c478bdstevel@tonic-gate * macros depending upon the alignment. 2. The lwp_stk_init code below
1197c478bdstevel@tonic-gate * could be changed to calculate if less space would be wasted if machpcb
1207c478bdstevel@tonic-gate * was first instead of second. However there is a REGOFF macro used in
1217c478bdstevel@tonic-gate * locore, syscall_trap, machdep and mlsetup that assumes that the saved
1227c478bdstevel@tonic-gate * register area is a fixed distance from the %sp, and would have to be
1237c478bdstevel@tonic-gate * changed to a pointer or something...JJ said later.
1247c478bdstevel@tonic-gate */
1257c478bdstevel@tonic-gatecaddr_t
1267c478bdstevel@tonic-gatelwp_stk_init(klwp_t *lwp, caddr_t stk)
1277c478bdstevel@tonic-gate{
1287c478bdstevel@tonic-gate	struct machpcb *mpcb;
1297c478bdstevel@tonic-gate	kfpu_t *fp;
1307c478bdstevel@tonic-gate	uintptr_t aln;
1317c478bdstevel@tonic-gate
1327c478bdstevel@tonic-gate	stk -= SA(sizeof (kfpu_t) + GSR_SIZE);
1337c478bdstevel@tonic-gate	aln = (uintptr_t)stk & 0x3F;
1347c478bdstevel@tonic-gate	stk -= aln;
1357c478bdstevel@tonic-gate	fp = (kfpu_t *)stk;
1367c478bdstevel@tonic-gate	stk -= SA(sizeof (struct machpcb));
1377c478bdstevel@tonic-gate	mpcb = (struct machpcb *)stk;
1387c478bdstevel@tonic-gate	bzero(mpcb, sizeof (struct machpcb));
1397c478bdstevel@tonic-gate	bzero(fp, sizeof (kfpu_t) + GSR_SIZE);
1407c478bdstevel@tonic-gate	lwp->lwp_regs = (void *)&mpcb->mpcb_regs;
1417c478bdstevel@tonic-gate	lwp->lwp_fpu = (void *)fp;
1427c478bdstevel@tonic-gate	mpcb->mpcb_fpu = fp;
1437c478bdstevel@tonic-gate	mpcb->mpcb_fpu->fpu_q = mpcb->mpcb_fpu_q;
1447c478bdstevel@tonic-gate	mpcb->mpcb_thread = lwp->lwp_thread;
1457c478bdstevel@tonic-gate	mpcb->mpcb_wbcnt = 0;
1467c478bdstevel@tonic-gate	if (lwp->lwp_procp->p_model == DATAMODEL_ILP32) {
1477c478bdstevel@tonic-gate		mpcb->mpcb_wstate = WSTATE_USER32;
148a859926elowe		mpcb->mpcb_wbuf = kmem_cache_alloc(wbuf32_cache, KM_SLEEP);
1497c478bdstevel@tonic-gate	} else {
1507c478bdstevel@tonic-gate		mpcb->mpcb_wstate = WSTATE_USER64;
151a859926elowe		mpcb->mpcb_wbuf = kmem_cache_alloc(wbuf64_cache, KM_SLEEP);
1527c478bdstevel@tonic-gate	}
1537c478bdstevel@tonic-gate	ASSERT(((uintptr_t)mpcb->mpcb_wbuf & 7) == 0);
1547c478bdstevel@tonic-gate	mpcb->mpcb_wbuf_pa = va_to_pa(mpcb->mpcb_wbuf);
1557c478bdstevel@tonic-gate	mpcb->mpcb_pa = va_to_pa(mpcb);
1567c478bdstevel@tonic-gate	return (stk);
1577c478bdstevel@tonic-gate}
1587c478bdstevel@tonic-gate
1597c478bdstevel@tonic-gatevoid
1607c478bdstevel@tonic-gatelwp_stk_fini(klwp_t *lwp)
1617c478bdstevel@tonic-gate{
1627c478bdstevel@tonic-gate	struct machpcb *mpcb = lwptompcb(lwp);
1637c478bdstevel@tonic-gate
1647c478bdstevel@tonic-gate	/*
1657c478bdstevel@tonic-gate	 * there might be windows still in the wbuf due to unmapped
1667c478bdstevel@tonic-gate	 * stack, misaligned stack pointer, etc.  We just free it.
1677c478bdstevel@tonic-gate	 */
1687c478bdstevel@tonic-gate	mpcb->mpcb_wbcnt = 0;
1697c478bdstevel@tonic-gate	if (mpcb->mpcb_wstate == WSTATE_USER32)
170a859926elowe		kmem_cache_free(wbuf32_cache, mpcb->mpcb_wbuf);
1717c478bdstevel@tonic-gate	else
172a859926elowe		kmem_cache_free(wbuf64_cache, mpcb->mpcb_wbuf);
1737c478bdstevel@tonic-gate	mpcb->mpcb_wbuf = NULL;
1747c478bdstevel@tonic-gate	mpcb->mpcb_wbuf_pa = -1;
1757c478bdstevel@tonic-gate}
1767c478bdstevel@tonic-gate
177088d69fJerry Jelinek/*ARGSUSED*/
178088d69fJerry Jelinekvoid
179088d69fJerry Jelineklwp_fp_init(klwp_t *lwp)
180088d69fJerry Jelinek{
181088d69fJerry Jelinek}
1827c478bdstevel@tonic-gate
1837c478bdstevel@tonic-gate/*
1847c478bdstevel@tonic-gate * Copy regs from parent to child.
1857c478bdstevel@tonic-gate */
1867c478bdstevel@tonic-gatevoid
1877c478bdstevel@tonic-gatelwp_forkregs(klwp_t *lwp, klwp_t *clwp)
1887c478bdstevel@tonic-gate{
1897c478bdstevel@tonic-gate	kthread_t *t, *pt = lwptot(lwp);
1907c478bdstevel@tonic-gate	struct machpcb *mpcb = lwptompcb(clwp);
1917c478bdstevel@tonic-gate	struct machpcb *pmpcb = lwptompcb(lwp);
1927c478bdstevel@tonic-gate	kfpu_t *fp, *pfp = lwptofpu(lwp);
1937c478bdstevel@tonic-gate	caddr_t wbuf;
1947c478bdstevel@tonic-gate	uint_t wstate;
1957c478bdstevel@tonic-gate
1967c478bdstevel@tonic-gate	t = mpcb->mpcb_thread;
1977c478bdstevel@tonic-gate	/*
1987c478bdstevel@tonic-gate	 * remember child's fp and wbuf since they will get erased during
1997c478bdstevel@tonic-gate	 * the bcopy.
2007c478bdstevel@tonic-gate	 */
2017c478bdstevel@tonic-gate	fp = mpcb->mpcb_fpu;
2027c478bdstevel@tonic-gate	wbuf = mpcb->mpcb_wbuf;
2037c478bdstevel@tonic-gate	wstate = mpcb->mpcb_wstate;
2047c478bdstevel@tonic-gate	/*
2057c478bdstevel@tonic-gate	 * Don't copy mpcb_frame since we hand-crafted it
2067c478bdstevel@tonic-gate	 * in thread_load().
2077c478bdstevel@tonic-gate	 */
2087c478bdstevel@tonic-gate	bcopy(lwp->lwp_regs, clwp->lwp_regs, sizeof (struct machpcb) - REGOFF);
2097c478bdstevel@tonic-gate	mpcb->mpcb_thread = t;
2107c478bdstevel@tonic-gate	mpcb->mpcb_fpu = fp;
2117c478bdstevel@tonic-gate	fp->fpu_q = mpcb->mpcb_fpu_q;
2127c478bdstevel@tonic-gate
2137c478bdstevel@tonic-gate	/*
2147c478bdstevel@tonic-gate	 * It is theoretically possibly for the lwp's wstate to
2157c478bdstevel@tonic-gate	 * be different from its value assigned in lwp_stk_init,
2167c478bdstevel@tonic-gate	 * since lwp_stk_init assumed the data model of the process.
2177c478bdstevel@tonic-gate	 * Here, we took on the data model of the cloned lwp.
2187c478bdstevel@tonic-gate	 */
2197c478bdstevel@tonic-gate	if (mpcb->mpcb_wstate != wstate) {
2207c478bdstevel@tonic-gate		if (wstate == WSTATE_USER32) {
221a859926elowe			kmem_cache_free(wbuf32_cache, wbuf);
222a859926elowe			wbuf = kmem_cache_alloc(wbuf64_cache, KM_SLEEP);
2237c478bdstevel@tonic-gate			wstate = WSTATE_USER64;
2247c478bdstevel@tonic-gate		} else {
225a859926elowe			kmem_cache_free(wbuf64_cache, wbuf);
226a859926elowe			wbuf = kmem_cache_alloc(wbuf32_cache, KM_SLEEP);
2277c478bdstevel@tonic-gate			wstate = WSTATE_USER32;
2287c478bdstevel@tonic-gate		}
2297c478bdstevel@tonic-gate	}
2307c478bdstevel@tonic-gate
2317c478bdstevel@tonic-gate	mpcb->mpcb_pa = va_to_pa(mpcb);
2327c478bdstevel@tonic-gate	mpcb->mpcb_wbuf = wbuf;
2337c478bdstevel@tonic-gate	mpcb->mpcb_wbuf_pa = va_to_pa(wbuf);
2347c478bdstevel@tonic-gate
2357c478bdstevel@tonic-gate	ASSERT(mpcb->mpcb_wstate == wstate);
2367c478bdstevel@tonic-gate
2377c478bdstevel@tonic-gate	if (mpcb->mpcb_wbcnt != 0) {
2387c478bdstevel@tonic-gate		bcopy(pmpcb->mpcb_wbuf, mpcb->mpcb_wbuf,
2397c478bdstevel@tonic-gate		    mpcb->mpcb_wbcnt * ((mpcb->mpcb_wstate == WSTATE_USER32) ?
2407c478bdstevel@tonic-gate		    sizeof (struct rwindow32) : sizeof (struct rwindow64)));
2417c478bdstevel@tonic-gate	}
2427c478bdstevel@tonic-gate
2437c478bdstevel@tonic-gate	if (pt == curthread)
2447c478bdstevel@tonic-gate		pfp->fpu_fprs = _fp_read_fprs();
2457c478bdstevel@tonic-gate	if ((pfp->fpu_en) || (pfp->fpu_fprs & FPRS_FEF)) {
2467c478bdstevel@tonic-gate		if (pt == curthread && fpu_exists) {
2477c478bdstevel@tonic-gate			save_gsr(clwp->lwp_fpu);
2487c478bdstevel@tonic-gate		} else {
2497c478bdstevel@tonic-gate			uint64_t gsr;
2507c478bdstevel@tonic-gate			gsr = get_gsr(lwp->lwp_fpu);
2517c478bdstevel@tonic-gate			set_gsr(gsr, clwp->lwp_fpu);
2527c478bdstevel@tonic-gate		}
2537c478bdstevel@tonic-gate		fp_fork(lwp, clwp);
2547c478bdstevel@tonic-gate	}
2557c478bdstevel@tonic-gate}
2567c478bdstevel@tonic-gate
2577c478bdstevel@tonic-gate/*
2587c478bdstevel@tonic-gate * Free lwp fpu regs.
2597c478bdstevel@tonic-gate */
2607c478bdstevel@tonic-gatevoid
2617c478bdstevel@tonic-gatelwp_freeregs(klwp_t *lwp, int isexec)
2627c478bdstevel@tonic-gate{
2637c478bdstevel@tonic-gate	kfpu_t *fp = lwptofpu(lwp);
2647c478bdstevel@tonic-gate
2657c478bdstevel@tonic-gate	if (lwptot(lwp) == curthread)
2667c478bdstevel@tonic-gate		fp->fpu_fprs = _fp_read_fprs();
2677c478bdstevel@tonic-gate	if ((fp->fpu_en) || (fp->fpu_fprs & FPRS_FEF))
2687c478bdstevel@tonic-gate		fp_free(fp, isexec);
2697c478bdstevel@tonic-gate}
2707c478bdstevel@tonic-gate
2717c478bdstevel@tonic-gate/*
272fd9e763edp * These function are currently unused on sparc.
2739acbbeann */
2749acbbeann/*ARGSUSED*/
2759acbbeannvoid
2769acbbeannlwp_attach_brand_hdlrs(klwp_t *lwp)
2779acbbeann{}
2789acbbeann
279fd9e763edp/*ARGSUSED*/
280fd9e763edpvoid
281fd9e763edplwp_detach_brand_hdlrs(klwp_t *lwp)
282fd9e763edp{}
283fd9e763edp
2849acbbeann/*
2857c478bdstevel@tonic-gate * fill in the extra register state area specified with the
2867c478bdstevel@tonic-gate * specified lwp's platform-dependent non-floating-point extra
2877c478bdstevel@tonic-gate * register state information
2887c478bdstevel@tonic-gate */
2897c478bdstevel@tonic-gate/* ARGSUSED */
2907c478bdstevel@tonic-gatevoid
2917c478bdstevel@tonic-gatexregs_getgfiller(klwp_id_t lwp, caddr_t xrp)
2927c478bdstevel@tonic-gate{
2937c478bdstevel@tonic-gate	/* for sun4u nothing to do here, added for symmetry */
2947c478bdstevel@tonic-gate}
2957c478bdstevel@tonic-gate
2967c478bdstevel@tonic-gate/*
2977c478bdstevel@tonic-gate * fill in the extra register state area specified with the specified lwp's
2987c478bdstevel@tonic-gate * platform-dependent floating-point extra register state information.
2997c478bdstevel@tonic-gate * NOTE:  'lwp' might not correspond to 'curthread' since this is
3007c478bdstevel@tonic-gate * called from code in /proc to get the registers of another lwp.
3017c478bdstevel@tonic-gate */
3027c478bdstevel@tonic-gatevoid
3037c478bdstevel@tonic-gatexregs_getfpfiller(klwp_id_t lwp, caddr_t xrp)
3047c478bdstevel@tonic-gate{
3057c478bdstevel@tonic-gate	prxregset_t *xregs = (prxregset_t *)xrp;
3067c478bdstevel@tonic-gate	kfpu_t *fp = lwptofpu(lwp);
3077c478bdstevel@tonic-gate	uint32_t fprs = (FPRS_FEF|FPRS_DU|FPRS_DL);
3087c478bdstevel@tonic-gate	uint64_t gsr;
3097c478bdstevel@tonic-gate
3107c478bdstevel@tonic-gate	/*
3117c478bdstevel@tonic-gate	 * fp_fksave() does not flush the GSR register into
3127c478bdstevel@tonic-gate	 * the lwp area, so do it now
3137c478bdstevel@tonic-gate	 */
3147c478bdstevel@tonic-gate	kpreempt_disable();
3157c478bdstevel@tonic-gate	if (ttolwp(curthread) == lwp && fpu_exists) {
3167c478bdstevel@tonic-gate		fp->fpu_fprs = _fp_read_fprs();
3177c478bdstevel@tonic-gate		if ((fp->fpu_fprs & FPRS_FEF) != FPRS_FEF) {
3187c478bdstevel@tonic-gate			_fp_write_fprs(fprs);
3197c478bdstevel@tonic-gate			fp->fpu_fprs = (V9_FPU_FPRS_TYPE)fprs;
3207c478bdstevel@tonic-gate		}
3217c478bdstevel@tonic-gate		save_gsr(fp);
3227c478bdstevel@tonic-gate	}
3237c478bdstevel@tonic-gate	gsr = get_gsr(fp);
3247c478bdstevel@tonic-gate	kpreempt_enable();
3257c478bdstevel@tonic-gate	PRXREG_GSR(xregs) = gsr;
3267c478bdstevel@tonic-gate}
3277c478bdstevel@tonic-gate
3287c478bdstevel@tonic-gate/*
3297c478bdstevel@tonic-gate * set the specified lwp's platform-dependent non-floating-point
3307c478bdstevel@tonic-gate * extra register state based on the specified input
3317c478bdstevel@tonic-gate */
3327c478bdstevel@tonic-gate/* ARGSUSED */
3337c478bdstevel@tonic-gatevoid
3347c478bdstevel@tonic-gatexregs_setgfiller(klwp_id_t lwp, caddr_t xrp)
3357c478bdstevel@tonic-gate{
3367c478bdstevel@tonic-gate	/* for sun4u nothing to do here, added for symmetry */
3377c478bdstevel@tonic-gate}
3387c478bdstevel@tonic-gate
3397c478bdstevel@tonic-gate/*
3407c478bdstevel@tonic-gate * set the specified lwp's platform-dependent floating-point
3417c478bdstevel@tonic-gate * extra register state based on the specified input
3427c478bdstevel@tonic-gate */
3437c478bdstevel@tonic-gatevoid
3447c478bdstevel@tonic-gatexregs_setfpfiller(klwp_id_t lwp, caddr_t xrp)
3457c478bdstevel@tonic-gate{
3467c478bdstevel@tonic-gate	prxregset_t *xregs = (prxregset_t *)xrp;
3477c478bdstevel@tonic-gate	kfpu_t *fp = lwptofpu(lwp);
3487c478bdstevel@tonic-gate	uint32_t fprs = (FPRS_FEF|FPRS_DU|FPRS_DL);
3497c478bdstevel@tonic-gate	uint64_t gsr = PRXREG_GSR(xregs);
3507c478bdstevel@tonic-gate
3517c478bdstevel@tonic-gate	kpreempt_disable();
3527c478bdstevel@tonic-gate	set_gsr(gsr, lwptofpu(lwp));
3537c478bdstevel@tonic-gate
3547c478bdstevel@tonic-gate	if ((lwp == ttolwp(curthread)) && fpu_exists) {
3557c478bdstevel@tonic-gate		fp->fpu_fprs = _fp_read_fprs();
3567c478bdstevel@tonic-gate		if ((fp->fpu_fprs & FPRS_FEF) != FPRS_FEF) {
3577c478bdstevel@tonic-gate			_fp_write_fprs(fprs);
3587c478bdstevel@tonic-gate			fp->fpu_fprs = (V9_FPU_FPRS_TYPE)fprs;
3597c478bdstevel@tonic-gate		}
3607c478bdstevel@tonic-gate		restore_gsr(lwptofpu(lwp));
3617c478bdstevel@tonic-gate	}
3627c478bdstevel@tonic-gate	kpreempt_enable();
3637c478bdstevel@tonic-gate}
3647c478bdstevel@tonic-gate
3657c478bdstevel@tonic-gate/*
3667c478bdstevel@tonic-gate * fill in the sun4u asrs, ie, the lwp's platform-dependent
3677c478bdstevel@tonic-gate * non-floating-point extra register state information
3687c478bdstevel@tonic-gate */
3697c478bdstevel@tonic-gate/* ARGSUSED */
3707c478bdstevel@tonic-gatevoid
3717c478bdstevel@tonic-gategetasrs(klwp_t *lwp, asrset_t asr)
3727c478bdstevel@tonic-gate{
3737c478bdstevel@tonic-gate	/* for sun4u nothing to do here, added for symmetry */
3747c478bdstevel@tonic-gate}
3757c478bdstevel@tonic-gate
3767c478bdstevel@tonic-gate/*
3777c478bdstevel@tonic-gate * fill in the sun4u asrs, ie, the lwp's platform-dependent
3787c478bdstevel@tonic-gate * floating-point extra register state information
3797c478bdstevel@tonic-gate */
3807c478bdstevel@tonic-gatevoid
3817c478bdstevel@tonic-gategetfpasrs(klwp_t *lwp, asrset_t asr)
3827c478bdstevel@tonic-gate{
3837c478bdstevel@tonic-gate	kfpu_t *fp = lwptofpu(lwp);
3847c478bdstevel@tonic-gate	uint32_t fprs = (FPRS_FEF|FPRS_DU|FPRS_DL);
3857c478bdstevel@tonic-gate
3867c478bdstevel@tonic-gate	kpreempt_disable();
3877c478bdstevel@tonic-gate	if (ttolwp(curthread) == lwp)
3887c478bdstevel@tonic-gate		fp->fpu_fprs = _fp_read_fprs();
3897c478bdstevel@tonic-gate	if ((fp->fpu_en) || (fp->fpu_fprs & FPRS_FEF)) {
3907c478bdstevel@tonic-gate		if (fpu_exists && ttolwp(curthread) == lwp) {
3917c478bdstevel@tonic-gate			if ((fp->fpu_fprs & FPRS_FEF) != FPRS_FEF) {
3927c478bdstevel@tonic-gate				_fp_write_fprs(fprs);
3937c478bdstevel@tonic-gate				fp->fpu_fprs = (V9_FPU_FPRS_TYPE)fprs;
3947c478bdstevel@tonic-gate			}
3957c478bdstevel@tonic-gate			save_gsr(fp);
3967c478bdstevel@tonic-gate		}
3977c478bdstevel@tonic-gate		asr[ASR_GSR] = (int64_t)get_gsr(fp);
3987c478bdstevel@tonic-gate	}
3997c478bdstevel@tonic-gate	kpreempt_enable();
4007c478bdstevel@tonic-gate}
4017c478bdstevel@tonic-gate
4027c478bdstevel@tonic-gate/*
4037c478bdstevel@tonic-gate * set the sun4u asrs, ie, the lwp's platform-dependent
4047c478bdstevel@tonic-gate * non-floating-point extra register state information
4057c478bdstevel@tonic-gate */
4067c478bdstevel@tonic-gate/* ARGSUSED */
4077c478bdstevel@tonic-gatevoid
4087c478bdstevel@tonic-gatesetasrs(klwp_t *lwp, asrset_t asr)
4097c478bdstevel@tonic-gate{
4107c478bdstevel@tonic-gate	/* for sun4u nothing to do here, added for symmetry */
4117c478bdstevel@tonic-gate}
4127c478bdstevel@tonic-gate
4137c478bdstevel@tonic-gatevoid
4147c478bdstevel@tonic-gatesetfpasrs(klwp_t *lwp, asrset_t asr)
4157c478bdstevel@tonic-gate{
4167c478bdstevel@tonic-gate	kfpu_t *fp = lwptofpu(lwp);
4177c478bdstevel@tonic-gate	uint32_t fprs = (FPRS_FEF|FPRS_DU|FPRS_DL);
4187c478bdstevel@tonic-gate
4197c478bdstevel@tonic-gate	kpreempt_disable();
4207c478bdstevel@tonic-gate	if (ttolwp(curthread) == lwp)
4217c478bdstevel@tonic-gate		fp->fpu_fprs = _fp_read_fprs();
4227c478bdstevel@tonic-gate	if ((fp->fpu_en) || (fp->fpu_fprs & FPRS_FEF)) {
4237c478bdstevel@tonic-gate		set_gsr(asr[ASR_GSR], fp);
4247c478bdstevel@tonic-gate		if (fpu_exists && ttolwp(curthread) == lwp) {
4257c478bdstevel@tonic-gate			if ((fp->fpu_fprs & FPRS_FEF) != FPRS_FEF) {
4267c478bdstevel@tonic-gate				_fp_write_fprs(fprs);
4277c478bdstevel@tonic-gate				fp->fpu_fprs = (V9_FPU_FPRS_TYPE)fprs;
4287c478bdstevel@tonic-gate			}
4297c478bdstevel@tonic-gate			restore_gsr(fp);
4307c478bdstevel@tonic-gate		}
4317c478bdstevel@tonic-gate	}
4327c478bdstevel@tonic-gate	kpreempt_enable();
4337c478bdstevel@tonic-gate}
4347c478bdstevel@tonic-gate
4357c478bdstevel@tonic-gate/*
4367c478bdstevel@tonic-gate * Create interrupt kstats for this CPU.
4377c478bdstevel@tonic-gate */
4387c478bdstevel@tonic-gatevoid
4397c478bdstevel@tonic-gatecpu_create_intrstat(cpu_t *cp)
4407c478bdstevel@tonic-gate{
4417c478bdstevel@tonic-gate	int		i;
4427c478bdstevel@tonic-gate	kstat_t		*intr_ksp;
4437c478bdstevel@tonic-gate	kstat_named_t	*knp;
4447c478bdstevel@tonic-gate	char		name[KSTAT_STRLEN];
4457c478bdstevel@tonic-gate	zoneid_t	zoneid;
4467c478bdstevel@tonic-gate
4477c478bdstevel@tonic-gate	ASSERT(MUTEX_HELD(&cpu_lock));
4487c478bdstevel@tonic-gate
4497c478bdstevel@tonic-gate	if (pool_pset_enabled())
4507c478bdstevel@tonic-gate		zoneid = GLOBAL_ZONEID;
4517c478bdstevel@tonic-gate	else
4527c478bdstevel@tonic-gate		zoneid = ALL_ZONES;
4537c478bdstevel@tonic-gate
4547c478bdstevel@tonic-gate	intr_ksp = kstat_create_zone("cpu", cp->cpu_id, "intrstat", "misc",
4557c478bdstevel@tonic-gate	    KSTAT_TYPE_NAMED, PIL_MAX * 2, NULL, zoneid);
4567c478bdstevel@tonic-gate
4577c478bdstevel@tonic-gate	/*
4587c478bdstevel@tonic-gate	 * Initialize each PIL's named kstat
4597c478bdstevel@tonic-gate	 */
4607c478bdstevel@tonic-gate	if (intr_ksp != NULL) {
4617c478bdstevel@tonic-gate		intr_ksp->ks_update = cpu_kstat_intrstat_update;
4627c478bdstevel@tonic-gate		knp = (kstat_named_t *)intr_ksp->ks_data;
4637c478bdstevel@tonic-gate		intr_ksp->ks_private = cp;
4647c478bdstevel@tonic-gate		for (i = 0; i < PIL_MAX; i++) {
4657c478bdstevel@tonic-gate			(void) snprintf(name, KSTAT_STRLEN, "level-%d-time",
4667c478bdstevel@tonic-gate			    i + 1);
4677c478bdstevel@tonic-gate			kstat_named_init(&knp[i * 2], name, KSTAT_DATA_UINT64);
4687c478bdstevel@tonic-gate			(void) snprintf(name, KSTAT_STRLEN, "level-%d-count",
4697c478bdstevel@tonic-gate			    i + 1);
4707c478bdstevel@tonic-gate			kstat_named_init(&knp[(i * 2) + 1], name,
4717c478bdstevel@tonic-gate			    KSTAT_DATA_UINT64);
4727c478bdstevel@tonic-gate		}
4737c478bdstevel@tonic-gate		kstat_install(intr_ksp);
4747c478bdstevel@tonic-gate	}
4757c478bdstevel@tonic-gate}
4767c478bdstevel@tonic-gate
4777c478bdstevel@tonic-gate/*
4787c478bdstevel@tonic-gate * Delete interrupt kstats for this CPU.
4797c478bdstevel@tonic-gate */
4807c478bdstevel@tonic-gatevoid
4817c478bdstevel@tonic-gatecpu_delete_intrstat(cpu_t *cp)
4827c478bdstevel@tonic-gate{
4837c478bdstevel@tonic-gate	kstat_delete_byname_zone("cpu", cp->cpu_id, "intrstat", ALL_ZONES);
4847c478bdstevel@tonic-gate}
4857c478bdstevel@tonic-gate
4867c478bdstevel@tonic-gate/*
4877c478bdstevel@tonic-gate * Convert interrupt statistics from CPU ticks to nanoseconds and
4887c478bdstevel@tonic-gate * update kstat.
4897c478bdstevel@tonic-gate */
4907c478bdstevel@tonic-gateint
4917c478bdstevel@tonic-gatecpu_kstat_intrstat_update(kstat_t *ksp, int rw)
4927c478bdstevel@tonic-gate{
4937c478bdstevel@tonic-gate	kstat_named_t	*knp = ksp->ks_data;
4947c478bdstevel@tonic-gate	cpu_t		*cpup = (cpu_t *)ksp->ks_private;
4957c478bdstevel@tonic-gate	int		i;
4967c478bdstevel@tonic-gate
4977c478bdstevel@tonic-gate	if (rw == KSTAT_WRITE)
4987c478bdstevel@tonic-gate		return (EACCES);
4997c478bdstevel@tonic-gate
5007c478bdstevel@tonic-gate	/*
5017c478bdstevel@tonic-gate	 * We use separate passes to copy and convert the statistics to
5027c478bdstevel@tonic-gate	 * nanoseconds. This assures that the snapshot of the data is as
5037c478bdstevel@tonic-gate	 * self-consistent as possible.
5047c478bdstevel@tonic-gate	 */
5057c478bdstevel@tonic-gate
5067c478bdstevel@tonic-gate	for (i = 0; i < PIL_MAX; i++) {
5077c478bdstevel@tonic-gate		knp[i * 2].value.ui64 = cpup->cpu_m.intrstat[i + 1][0];
5087c478bdstevel@tonic-gate		knp[(i * 2) + 1].value.ui64 = cpup->cpu_stats.sys.intr[i];
5097c478bdstevel@tonic-gate	}
5107c478bdstevel@tonic-gate
5117c478bdstevel@tonic-gate	for (i = 0; i < PIL_MAX; i++) {
5127c478bdstevel@tonic-gate		knp[i * 2].value.ui64 =
5137c478bdstevel@tonic-gate		    (uint64_t)tick2ns((hrtime_t)knp[i * 2].value.ui64,
514843e198johnlev		    cpup->cpu_id);
5157c478bdstevel@tonic-gate	}
5167c478bdstevel@tonic-gate
5177c478bdstevel@tonic-gate	return (0);
5187c478bdstevel@tonic-gate}
5197c478bdstevel@tonic-gate
5207c478bdstevel@tonic-gate/*
5217c478bdstevel@tonic-gate * Called by common/os/cpu.c for psrinfo(1m) kstats
5227c478bdstevel@tonic-gate */
5237c478bdstevel@tonic-gatechar *
5247c478bdstevel@tonic-gatecpu_fru_fmri(cpu_t *cp)
5257c478bdstevel@tonic-gate{
5267c478bdstevel@tonic-gate	return (cpunodes[cp->cpu_id].fru_fmri);
5277c478bdstevel@tonic-gate}
5287c478bdstevel@tonic-gate
5297c478bdstevel@tonic-gate/*
5307c478bdstevel@tonic-gate * An interrupt thread is ending a time slice, so compute the interval it
5317c478bdstevel@tonic-gate * ran for and update the statistic for its PIL.
5327c478bdstevel@tonic-gate */
5337c478bdstevel@tonic-gatevoid
5347c478bdstevel@tonic-gatecpu_intr_swtch_enter(kthread_id_t t)
5357c478bdstevel@tonic-gate{
5367c478bdstevel@tonic-gate	uint64_t	interval;
5377c478bdstevel@tonic-gate	uint64_t	start;
538eda8946esolom	cpu_t		*cpu;
5397c478bdstevel@tonic-gate
5407c478bdstevel@tonic-gate	ASSERT((t->t_flag & T_INTR_THREAD) != 0);
5417c478bdstevel@tonic-gate	ASSERT(t->t_pil > 0 && t->t_pil <= LOCK_LEVEL);
5427c478bdstevel@tonic-gate
5437c478bdstevel@tonic-gate	/*
5447c478bdstevel@tonic-gate	 * We could be here with a zero timestamp. This could happen if:
5457c478bdstevel@tonic-gate	 * an interrupt thread which no longer has a pinned thread underneath
5467c478bdstevel@tonic-gate	 * it (i.e. it blocked at some point in its past) has finished running
5477c478bdstevel@tonic-gate	 * its handler. intr_thread() updated the interrupt statistic for its
5487c478bdstevel@tonic-gate	 * PIL and zeroed its timestamp. Since there was no pinned thread to
5497c478bdstevel@tonic-gate	 * return to, swtch() gets called and we end up here.
5507c478bdstevel@tonic-gate	 *
5517c478bdstevel@tonic-gate	 * It can also happen if an interrupt thread in intr_thread() calls
5527c478bdstevel@tonic-gate	 * preempt. It will have already taken care of updating stats. In
5537c478bdstevel@tonic-gate	 * this event, the interrupt thread will be runnable.
5547c478bdstevel@tonic-gate	 */
5557c478bdstevel@tonic-gate	if (t->t_intr_start) {
5567c478bdstevel@tonic-gate		do {
5577c478bdstevel@tonic-gate			start = t->t_intr_start;
558bd28a47Prashanth Sreenivasa			interval = CLOCK_TICK_COUNTER() - start;
55975d9446Josef 'Jeff' Sipek		} while (atomic_cas_64(&t->t_intr_start, start, 0) != start);
560eda8946esolom		cpu = CPU;
561eda8946esolom		if (cpu->cpu_m.divisor > 1)
562eda8946esolom			interval *= cpu->cpu_m.divisor;
563eda8946esolom		cpu->cpu_m.intrstat[t->t_pil][0] += interval;
564eda8946esolom
565eda8946esolom		atomic_add_64((uint64_t *)&cpu->cpu_intracct[cpu->cpu_mstate],
566eda8946esolom		    interval);
5677c478bdstevel@tonic-gate	} else
5687c478bdstevel@tonic-gate		ASSERT(t->t_intr == NULL || t->t_state == TS_RUN);
5697c478bdstevel@tonic-gate}
5707c478bdstevel@tonic-gate
5717c478bdstevel@tonic-gate
5727c478bdstevel@tonic-gate/*
5737c478bdstevel@tonic-gate * An interrupt thread is returning from swtch(). Place a starting timestamp
5747c478bdstevel@tonic-gate * in its thread structure.
5757c478bdstevel@tonic-gate */
5767c478bdstevel@tonic-gatevoid
5777c478bdstevel@tonic-gatecpu_intr_swtch_exit(kthread_id_t t)
5787c478bdstevel@tonic-gate{
5797c478bdstevel@tonic-gate	uint64_t ts;
5807c478bdstevel@tonic-gate
5817c478bdstevel@tonic-gate	ASSERT((t->t_flag & T_INTR_THREAD) != 0);
5827c478bdstevel@tonic-gate	ASSERT(t->t_pil > 0 && t->t_pil <= LOCK_LEVEL);
5837c478bdstevel@tonic-gate
5847c478bdstevel@tonic-gate	do {
5857c478bdstevel@tonic-gate		ts = t->t_intr_start;
58675d9446Josef 'Jeff' Sipek	} while (atomic_cas_64(&t->t_intr_start, ts, CLOCK_TICK_COUNTER()) !=
58775d9446Josef 'Jeff' Sipek	    ts);
5887c478bdstevel@tonic-gate}
5897c478bdstevel@tonic-gate
5907c478bdstevel@tonic-gate
5917c478bdstevel@tonic-gateint
5927c478bdstevel@tonic-gateblacklist(int cmd, const char *scheme, nvlist_t *fmri, const char *class)
5937c478bdstevel@tonic-gate{
5947c478bdstevel@tonic-gate	if (&plat_blacklist)
5957c478bdstevel@tonic-gate		return (plat_blacklist(cmd, scheme, fmri, class));
5967c478bdstevel@tonic-gate
5977c478bdstevel@tonic-gate	return (ENOTSUP);
5987c478bdstevel@tonic-gate}
5997c478bdstevel@tonic-gate
6007c478bdstevel@tonic-gateint
6017c478bdstevel@tonic-gatekdi_pread(caddr_t buf, size_t nbytes, uint64_t addr, size_t *ncopiedp)
6027c478bdstevel@tonic-gate{
6037c478bdstevel@tonic-gate	extern void kdi_flush_caches(void);
6047c478bdstevel@tonic-gate	size_t nread = 0;
6057c478bdstevel@tonic-gate	uint32_t word;
6067c478bdstevel@tonic-gate	int slop, i;
6077c478bdstevel@tonic-gate
6087c478bdstevel@tonic-gate	kdi_flush_caches();
6097c478bdstevel@tonic-gate	membar_enter();
6107c478bdstevel@tonic-gate
6117c478bdstevel@tonic-gate	/* We might not begin on a word boundary. */
6127c478bdstevel@tonic-gate	if ((slop = addr & 3) != 0) {
6137c478bdstevel@tonic-gate		word = ldphys(addr & ~3);
6147c478bdstevel@tonic-gate		for (i = slop; i < 4 && nbytes > 0; i++, nbytes--, nread++)
6157c478bdstevel@tonic-gate			*buf++ = ((uchar_t *)&word)[i];
6167c478bdstevel@tonic-gate		addr = roundup(addr, 4);
6177c478bdstevel@tonic-gate	}
6187c478bdstevel@tonic-gate
6197c478bdstevel@tonic-gate	while (nbytes > 0) {
6207c478bdstevel@tonic-gate		word = ldphys(addr);
6217c478bdstevel@tonic-gate		for (i = 0; i < 4 && nbytes > 0; i++, nbytes--, nread++, addr++)
6227c478bdstevel@tonic-gate			*buf++ = ((uchar_t *)&word)[i];
6237c478bdstevel@tonic-gate	}
6247c478bdstevel@tonic-gate
6257c478bdstevel@tonic-gate	kdi_flush_caches();
6267c478bdstevel@tonic-gate
6277c478bdstevel@tonic-gate	*ncopiedp = nread;
6287c478bdstevel@tonic-gate	return (0);
6297c478bdstevel@tonic-gate}
6307c478bdstevel@tonic-gate
6317c478bdstevel@tonic-gateint
6327c478bdstevel@tonic-gatekdi_pwrite(caddr_t buf, size_t nbytes, uint64_t addr, size_t *ncopiedp)
6337c478bdstevel@tonic-gate{
6347c478bdstevel@tonic-gate	extern void kdi_flush_caches(void);
6357c478bdstevel@tonic-gate	size_t nwritten = 0;
6367c478bdstevel@tonic-gate	uint32_t word;
6377c478bdstevel@tonic-gate	int slop, i;
6387c478bdstevel@tonic-gate
6397c478bdstevel@tonic-gate	kdi_flush_caches();
6407c478bdstevel@tonic-gate
6417c478bdstevel@tonic-gate	/* We might not begin on a word boundary. */
6427c478bdstevel@tonic-gate	if ((slop = addr & 3) != 0) {
6437c478bdstevel@tonic-gate		word = ldphys(addr & ~3);
6447c478bdstevel@tonic-gate		for (i = slop; i < 4 && nbytes > 0; i++, nbytes--, nwritten++)
6457c478bdstevel@tonic-gate			((uchar_t *)&word)[i] = *buf++;
6467c478bdstevel@tonic-gate		stphys(addr & ~3, word);
6477c478bdstevel@tonic-gate		addr = roundup(addr, 4);
6487c478bdstevel@tonic-gate	}
6497c478bdstevel@tonic-gate
6507c478bdstevel@tonic-gate	while (nbytes > 3) {
6517c478bdstevel@tonic-gate		for (word = 0, i = 0; i < 4; i++, nbytes--, nwritten++)
6527c478bdstevel@tonic-gate			((uchar_t *)&word)[i] = *buf++;
6537c478bdstevel@tonic-gate		stphys(addr, word);
6547c478bdstevel@tonic-gate		addr += 4;
6557c478bdstevel@tonic-gate	}
6567c478bdstevel@tonic-gate
6577c478bdstevel@tonic-gate	/* We might not end with a whole word. */
6587c478bdstevel@tonic-gate	if (nbytes > 0) {
6597c478bdstevel@tonic-gate		word = ldphys(addr);
6607c478bdstevel@tonic-gate		for (i = 0; nbytes > 0; i++, nbytes--, nwritten++)
6617c478bdstevel@tonic-gate			((uchar_t *)&word)[i] = *buf++;
6627c478bdstevel@tonic-gate		stphys(addr, word);
6637c478bdstevel@tonic-gate	}
6647c478bdstevel@tonic-gate
6657c478bdstevel@tonic-gate	membar_enter();
6667c478bdstevel@tonic-gate	kdi_flush_caches();
6677c478bdstevel@tonic-gate
6687c478bdstevel@tonic-gate	*ncopiedp = nwritten;
6697c478bdstevel@tonic-gate	return (0);
6707c478bdstevel@tonic-gate}
6717c478bdstevel@tonic-gate
6727c478bdstevel@tonic-gatestatic void
6737c478bdstevel@tonic-gatekdi_kernpanic(struct regs *regs, uint_t tt)
6747c478bdstevel@tonic-gate{
6757c478bdstevel@tonic-gate	sync_reg_buf = *regs;
6767c478bdstevel@tonic-gate	sync_tt = tt;
6777c478bdstevel@tonic-gate
6787c478bdstevel@tonic-gate	sync_handler();
6797c478bdstevel@tonic-gate}
6807c478bdstevel@tonic-gate
6817c478bdstevel@tonic-gatestatic void
6827c478bdstevel@tonic-gatekdi_plat_call(void (*platfn)(void))
6837c478bdstevel@tonic-gate{
6847c478bdstevel@tonic-gate	if (platfn != NULL) {
6857c478bdstevel@tonic-gate		prom_suspend_prepost();
6867c478bdstevel@tonic-gate		platfn();
6877c478bdstevel@tonic-gate		prom_resume_prepost();
6887c478bdstevel@tonic-gate	}
6897c478bdstevel@tonic-gate}
6907c478bdstevel@tonic-gate
691d3d5073Rafael Vanoni/*
692d3d5073Rafael Vanoni * kdi_system_claim and release are defined here for all sun4 platforms and
693d3d5073Rafael Vanoni * pointed to by mach_kdi_init() to provide default callbacks for such systems.
694d3d5073Rafael Vanoni * Specific sun4u or sun4v platforms may implement their own claim and release
695d3d5073Rafael Vanoni * routines, at which point their respective callbacks will be updated.
696d3d5073Rafael Vanoni */
697d3d5073Rafael Vanonistatic void
698d3d5073Rafael Vanonikdi_system_claim(void)
699d3d5073Rafael Vanoni{
700d3d5073Rafael Vanoni	lbolt_debug_entry();
701d3d5073Rafael Vanoni}
702d3d5073Rafael Vanoni
703d3d5073Rafael Vanonistatic void
704d3d5073Rafael Vanonikdi_system_release(void)
705d3d5073Rafael Vanoni{
706d3d5073Rafael Vanoni	lbolt_debug_return();
707d3d5073Rafael Vanoni}
708d3d5073Rafael Vanoni
7097c478bdstevel@tonic-gatevoid
7107c478bdstevel@tonic-gatemach_kdi_init(kdi_t *kdi)
7117c478bdstevel@tonic-gate{
7127c478bdstevel@tonic-gate	kdi->kdi_plat_call = kdi_plat_call;
713ae115bcmrj	kdi->kdi_kmdb_enter = kmdb_enter;
714d3d5073Rafael Vanoni	kdi->pkdi_system_claim = kdi_system_claim;
715d3d5073Rafael Vanoni	kdi->pkdi_system_release = kdi_system_release;
7167c478bdstevel@tonic-gate	kdi->mkdi_cpu_index = kdi_cpu_index;
7177c478bdstevel@tonic-gate	kdi->mkdi_trap_vatotte = kdi_trap_vatotte;
7187c478bdstevel@tonic-gate	kdi->mkdi_kernpanic = kdi_kernpanic;
7197c478bdstevel@tonic-gate}
720eda8946esolom
721eda8946esolom
722eda8946esolom/*
723eda8946esolom * get_cpu_mstate() is passed an array of timestamps, NCMSTATES
724eda8946esolom * long, and it fills in the array with the time spent on cpu in
725eda8946esolom * each of the mstates, where time is returned in nsec.
726eda8946esolom *
727eda8946esolom * No guarantee is made that the returned values in times[] will
728eda8946esolom * monotonically increase on sequential calls, although this will
729eda8946esolom * be true in the long run. Any such guarantee must be handled by
730eda8946esolom * the caller, if needed. This can happen if we fail to account
731eda8946esolom * for elapsed time due to a generation counter conflict, yet we
732eda8946esolom * did account for it on a prior call (see below).
733eda8946esolom *
734eda8946esolom * The complication is that the cpu in question may be updating
735eda8946esolom * its microstate at the same time that we are reading it.
736eda8946esolom * Because the microstate is only updated when the CPU's state
737eda8946esolom * changes, the values in cpu_intracct[] can be indefinitely out
738eda8946esolom * of date. To determine true current values, it is necessary to
739eda8946esolom * compare the current time with cpu_mstate_start, and add the
740eda8946esolom * difference to times[cpu_mstate].
741eda8946esolom *
742eda8946esolom * This can be a problem if those values are changing out from
743eda8946esolom * under us. Because the code path in new_cpu_mstate() is
744eda8946esolom * performance critical, we have not added a lock to it. Instead,
745eda8946esolom * we have added a generation counter. Before beginning
746eda8946esolom * modifications, the counter is set to 0. After modifications,
747eda8946esolom * it is set to the old value plus one.
748eda8946esolom *
749eda8946esolom * get_cpu_mstate() will not consider the values of cpu_mstate
750eda8946esolom * and cpu_mstate_start to be usable unless the value of
751eda8946esolom * cpu_mstate_gen is both non-zero and unchanged, both before and
752eda8946esolom * after reading the mstate information. Note that we must
753eda8946esolom * protect against out-of-order loads around accesses to the
754eda8946esolom * generation counter. Also, this is a best effort approach in
755eda8946esolom * that we do not retry should the counter be found to have
756eda8946esolom * changed.
757eda8946esolom *
758eda8946esolom * cpu_intracct[] is used to identify time spent in each CPU
759eda8946esolom * mstate while handling interrupts. Such time should be reported
760eda8946esolom * against system time, and so is subtracted out from its
761eda8946esolom * corresponding cpu_acct[] time and added to
762eda8946esolom * cpu_acct[CMS_SYSTEM]. Additionally, intracct time is stored in
763eda8946esolom * %ticks, but acct time may be stored as %sticks, thus requiring
764eda8946esolom * different conversions before they can be compared.
765eda8946esolom */
766eda8946esolom
767eda8946esolomvoid
768eda8946esolomget_cpu_mstate(cpu_t *cpu, hrtime_t *times)
769eda8946esolom{
770eda8946esolom	int i;
771eda8946esolom	hrtime_t now, start;
772eda8946esolom	uint16_t gen;
773eda8946esolom	uint16_t state;
774eda8946esolom	hrtime_t intracct[NCMSTATES];
775eda8946esolom
776eda8946esolom	/*
777eda8946esolom	 * Load all volatile state under the protection of membar.
778eda8946esolom	 * cpu_acct[cpu_mstate] must be loaded to avoid double counting
779eda8946esolom	 * of (now - cpu_mstate_start) by a change in CPU mstate that
780eda8946esolom	 * arrives after we make our last check of cpu_mstate_gen.
781eda8946esolom	 */
782eda8946esolom
783eda8946esolom	now = gethrtime_unscaled();
784eda8946esolom	gen = cpu->cpu_mstate_gen;
785eda8946esolom
786eda8946esolom	membar_consumer();	/* guarantee load ordering */
787eda8946esolom	start = cpu->cpu_mstate_start;
788eda8946esolom	state = cpu->cpu_mstate;
789eda8946esolom	for (i = 0; i < NCMSTATES; i++) {
790eda8946esolom		intracct[i] = cpu->cpu_intracct[i];
791eda8946esolom		times[i] = cpu->cpu_acct[i];
792eda8946esolom	}
793eda8946esolom	membar_consumer();	/* guarantee load ordering */
794eda8946esolom
795eda8946esolom	if (gen != 0 && gen == cpu->cpu_mstate_gen && now > start)
796eda8946esolom		times[state] += now - start;
797eda8946esolom
798eda8946esolom	for (i = 0; i < NCMSTATES; i++) {
799eda8946esolom		scalehrtime(&times[i]);
800eda8946esolom		intracct[i] = tick2ns((hrtime_t)intracct[i], cpu->cpu_id);
801eda8946esolom	}
802eda8946esolom
803eda8946esolom	for (i = 0; i < NCMSTATES; i++) {
804eda8946esolom		if (i == CMS_SYSTEM)
805eda8946esolom			continue;
806eda8946esolom		times[i] -= intracct[i];
807eda8946esolom		if (times[i] < 0) {
808eda8946esolom			intracct[i] += times[i];
809eda8946esolom			times[i] = 0;
810eda8946esolom		}
811eda8946esolom		times[CMS_SYSTEM] += intracct[i];
812eda8946esolom	}
813eda8946esolom}
814ae115bcmrj
815ae115bcmrjvoid
816ae115bcmrjmach_cpu_pause(volatile char *safe)
817ae115bcmrj{
818ae115bcmrj	/*
819