xref: /illumos-gate/usr/src/uts/sun4/os/machdep.c (revision 7c478bd95313f5f23a4c958a745db2134aa0324)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
30*7c478bd9Sstevel@tonic-gate #include <sys/kstat.h>
31*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
32*7c478bd9Sstevel@tonic-gate #include <sys/stack.h>
33*7c478bd9Sstevel@tonic-gate #include <sys/regset.h>
34*7c478bd9Sstevel@tonic-gate #include <sys/thread.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/proc.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/procfs_isa.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
39*7c478bd9Sstevel@tonic-gate #include <sys/systm.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/machpcb.h>
41*7c478bd9Sstevel@tonic-gate #include <sys/machasi.h>
42*7c478bd9Sstevel@tonic-gate #include <sys/vis.h>
43*7c478bd9Sstevel@tonic-gate #include <sys/fpu/fpusystm.h>
44*7c478bd9Sstevel@tonic-gate #include <sys/cpu_module.h>
45*7c478bd9Sstevel@tonic-gate #include <sys/privregs.h>
46*7c478bd9Sstevel@tonic-gate #include <sys/archsystm.h>
47*7c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
48*7c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
49*7c478bd9Sstevel@tonic-gate #include <sys/time.h>
50*7c478bd9Sstevel@tonic-gate #include <sys/clock.h>
51*7c478bd9Sstevel@tonic-gate #include <sys/chip.h>
52*7c478bd9Sstevel@tonic-gate #include <sys/cmp.h>
53*7c478bd9Sstevel@tonic-gate #include <sys/platform_module.h>
54*7c478bd9Sstevel@tonic-gate #include <sys/bl.h>
55*7c478bd9Sstevel@tonic-gate #include <sys/nvpair.h>
56*7c478bd9Sstevel@tonic-gate #include <sys/kdi_impl.h>
57*7c478bd9Sstevel@tonic-gate #include <sys/machsystm.h>
58*7c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
59*7c478bd9Sstevel@tonic-gate #include <sys/promif.h>
60*7c478bd9Sstevel@tonic-gate #include <sys/pool_pset.h>
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate int maxphys = MMU_PAGESIZE * 16;	/* 128k */
63*7c478bd9Sstevel@tonic-gate int klustsize = MMU_PAGESIZE * 16;	/* 128k */
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate /*
66*7c478bd9Sstevel@tonic-gate  * Initialize kernel thread's stack.
67*7c478bd9Sstevel@tonic-gate  */
68*7c478bd9Sstevel@tonic-gate caddr_t
69*7c478bd9Sstevel@tonic-gate thread_stk_init(caddr_t stk)
70*7c478bd9Sstevel@tonic-gate {
71*7c478bd9Sstevel@tonic-gate 	kfpu_t *fp;
72*7c478bd9Sstevel@tonic-gate 	ulong_t align;
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate 	/* allocate extra space for floating point state */
75*7c478bd9Sstevel@tonic-gate 	stk -= SA(sizeof (kfpu_t) + GSR_SIZE);
76*7c478bd9Sstevel@tonic-gate 	align = (uintptr_t)stk & 0x3f;
77*7c478bd9Sstevel@tonic-gate 	stk -= align;		/* force v9_fpu to be 16 byte aligned */
78*7c478bd9Sstevel@tonic-gate 	fp = (kfpu_t *)stk;
79*7c478bd9Sstevel@tonic-gate 	fp->fpu_fprs = 0;
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate 	stk -= SA(MINFRAME);
82*7c478bd9Sstevel@tonic-gate 	return (stk);
83*7c478bd9Sstevel@tonic-gate }
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate /*
86*7c478bd9Sstevel@tonic-gate  * Initialize lwp's kernel stack.
87*7c478bd9Sstevel@tonic-gate  * Note that now that the floating point register save area (kfpu_t)
88*7c478bd9Sstevel@tonic-gate  * has been broken out from machpcb and aligned on a 64 byte boundary so that
89*7c478bd9Sstevel@tonic-gate  * we can do block load/stores to/from it, there are a couple of potential
90*7c478bd9Sstevel@tonic-gate  * optimizations to save stack space. 1. The floating point register save
91*7c478bd9Sstevel@tonic-gate  * area could be aligned on a 16 byte boundary, and the floating point code
92*7c478bd9Sstevel@tonic-gate  * changed to (a) check the alignment and (b) use different save/restore
93*7c478bd9Sstevel@tonic-gate  * macros depending upon the alignment. 2. The lwp_stk_init code below
94*7c478bd9Sstevel@tonic-gate  * could be changed to calculate if less space would be wasted if machpcb
95*7c478bd9Sstevel@tonic-gate  * was first instead of second. However there is a REGOFF macro used in
96*7c478bd9Sstevel@tonic-gate  * locore, syscall_trap, machdep and mlsetup that assumes that the saved
97*7c478bd9Sstevel@tonic-gate  * register area is a fixed distance from the %sp, and would have to be
98*7c478bd9Sstevel@tonic-gate  * changed to a pointer or something...JJ said later.
99*7c478bd9Sstevel@tonic-gate  */
100*7c478bd9Sstevel@tonic-gate caddr_t
101*7c478bd9Sstevel@tonic-gate lwp_stk_init(klwp_t *lwp, caddr_t stk)
102*7c478bd9Sstevel@tonic-gate {
103*7c478bd9Sstevel@tonic-gate 	struct machpcb *mpcb;
104*7c478bd9Sstevel@tonic-gate 	kfpu_t *fp;
105*7c478bd9Sstevel@tonic-gate 	uintptr_t aln;
106*7c478bd9Sstevel@tonic-gate 
107*7c478bd9Sstevel@tonic-gate 	stk -= SA(sizeof (kfpu_t) + GSR_SIZE);
108*7c478bd9Sstevel@tonic-gate 	aln = (uintptr_t)stk & 0x3F;
109*7c478bd9Sstevel@tonic-gate 	stk -= aln;
110*7c478bd9Sstevel@tonic-gate 	fp = (kfpu_t *)stk;
111*7c478bd9Sstevel@tonic-gate 	stk -= SA(sizeof (struct machpcb));
112*7c478bd9Sstevel@tonic-gate 	mpcb = (struct machpcb *)stk;
113*7c478bd9Sstevel@tonic-gate 	bzero(mpcb, sizeof (struct machpcb));
114*7c478bd9Sstevel@tonic-gate 	bzero(fp, sizeof (kfpu_t) + GSR_SIZE);
115*7c478bd9Sstevel@tonic-gate 	lwp->lwp_regs = (void *)&mpcb->mpcb_regs;
116*7c478bd9Sstevel@tonic-gate 	lwp->lwp_fpu = (void *)fp;
117*7c478bd9Sstevel@tonic-gate 	mpcb->mpcb_fpu = fp;
118*7c478bd9Sstevel@tonic-gate 	mpcb->mpcb_fpu->fpu_q = mpcb->mpcb_fpu_q;
119*7c478bd9Sstevel@tonic-gate 	mpcb->mpcb_thread = lwp->lwp_thread;
120*7c478bd9Sstevel@tonic-gate 	mpcb->mpcb_wbcnt = 0;
121*7c478bd9Sstevel@tonic-gate 	if (lwp->lwp_procp->p_model == DATAMODEL_ILP32) {
122*7c478bd9Sstevel@tonic-gate 		mpcb->mpcb_wstate = WSTATE_USER32;
123*7c478bd9Sstevel@tonic-gate 		mpcb->mpcb_wbuf = kmem_alloc(MAXWIN * sizeof (struct rwindow32),
124*7c478bd9Sstevel@tonic-gate 		    KM_SLEEP);
125*7c478bd9Sstevel@tonic-gate 	} else {
126*7c478bd9Sstevel@tonic-gate 		mpcb->mpcb_wstate = WSTATE_USER64;
127*7c478bd9Sstevel@tonic-gate 		mpcb->mpcb_wbuf = kmem_alloc(MAXWIN * sizeof (struct rwindow64),
128*7c478bd9Sstevel@tonic-gate 		    KM_SLEEP);
129*7c478bd9Sstevel@tonic-gate 	}
130*7c478bd9Sstevel@tonic-gate 	ASSERT(((uintptr_t)mpcb->mpcb_wbuf & 7) == 0);
131*7c478bd9Sstevel@tonic-gate 	mpcb->mpcb_wbuf_pa = va_to_pa(mpcb->mpcb_wbuf);
132*7c478bd9Sstevel@tonic-gate 	mpcb->mpcb_pa = va_to_pa(mpcb);
133*7c478bd9Sstevel@tonic-gate 	return (stk);
134*7c478bd9Sstevel@tonic-gate }
135*7c478bd9Sstevel@tonic-gate 
136*7c478bd9Sstevel@tonic-gate void
137*7c478bd9Sstevel@tonic-gate lwp_stk_fini(klwp_t *lwp)
138*7c478bd9Sstevel@tonic-gate {
139*7c478bd9Sstevel@tonic-gate 	struct machpcb *mpcb = lwptompcb(lwp);
140*7c478bd9Sstevel@tonic-gate 
141*7c478bd9Sstevel@tonic-gate 	/*
142*7c478bd9Sstevel@tonic-gate 	 * there might be windows still in the wbuf due to unmapped
143*7c478bd9Sstevel@tonic-gate 	 * stack, misaligned stack pointer, etc.  We just free it.
144*7c478bd9Sstevel@tonic-gate 	 */
145*7c478bd9Sstevel@tonic-gate 	mpcb->mpcb_wbcnt = 0;
146*7c478bd9Sstevel@tonic-gate 	if (mpcb->mpcb_wstate == WSTATE_USER32)
147*7c478bd9Sstevel@tonic-gate 		kmem_free(mpcb->mpcb_wbuf, MAXWIN * sizeof (struct rwindow32));
148*7c478bd9Sstevel@tonic-gate 	else
149*7c478bd9Sstevel@tonic-gate 		kmem_free(mpcb->mpcb_wbuf, MAXWIN * sizeof (struct rwindow64));
150*7c478bd9Sstevel@tonic-gate 	mpcb->mpcb_wbuf = NULL;
151*7c478bd9Sstevel@tonic-gate 	mpcb->mpcb_wbuf_pa = -1;
152*7c478bd9Sstevel@tonic-gate }
153*7c478bd9Sstevel@tonic-gate 
154*7c478bd9Sstevel@tonic-gate 
155*7c478bd9Sstevel@tonic-gate /*
156*7c478bd9Sstevel@tonic-gate  * Copy regs from parent to child.
157*7c478bd9Sstevel@tonic-gate  */
158*7c478bd9Sstevel@tonic-gate void
159*7c478bd9Sstevel@tonic-gate lwp_forkregs(klwp_t *lwp, klwp_t *clwp)
160*7c478bd9Sstevel@tonic-gate {
161*7c478bd9Sstevel@tonic-gate 	kthread_t *t, *pt = lwptot(lwp);
162*7c478bd9Sstevel@tonic-gate 	struct machpcb *mpcb = lwptompcb(clwp);
163*7c478bd9Sstevel@tonic-gate 	struct machpcb *pmpcb = lwptompcb(lwp);
164*7c478bd9Sstevel@tonic-gate 	kfpu_t *fp, *pfp = lwptofpu(lwp);
165*7c478bd9Sstevel@tonic-gate 	caddr_t wbuf;
166*7c478bd9Sstevel@tonic-gate 	uint_t wstate;
167*7c478bd9Sstevel@tonic-gate 
168*7c478bd9Sstevel@tonic-gate 	t = mpcb->mpcb_thread;
169*7c478bd9Sstevel@tonic-gate 	/*
170*7c478bd9Sstevel@tonic-gate 	 * remember child's fp and wbuf since they will get erased during
171*7c478bd9Sstevel@tonic-gate 	 * the bcopy.
172*7c478bd9Sstevel@tonic-gate 	 */
173*7c478bd9Sstevel@tonic-gate 	fp = mpcb->mpcb_fpu;
174*7c478bd9Sstevel@tonic-gate 	wbuf = mpcb->mpcb_wbuf;
175*7c478bd9Sstevel@tonic-gate 	wstate = mpcb->mpcb_wstate;
176*7c478bd9Sstevel@tonic-gate 	/*
177*7c478bd9Sstevel@tonic-gate 	 * Don't copy mpcb_frame since we hand-crafted it
178*7c478bd9Sstevel@tonic-gate 	 * in thread_load().
179*7c478bd9Sstevel@tonic-gate 	 */
180*7c478bd9Sstevel@tonic-gate 	bcopy(lwp->lwp_regs, clwp->lwp_regs, sizeof (struct machpcb) - REGOFF);
181*7c478bd9Sstevel@tonic-gate 	mpcb->mpcb_thread = t;
182*7c478bd9Sstevel@tonic-gate 	mpcb->mpcb_fpu = fp;
183*7c478bd9Sstevel@tonic-gate 	fp->fpu_q = mpcb->mpcb_fpu_q;
184*7c478bd9Sstevel@tonic-gate 
185*7c478bd9Sstevel@tonic-gate 	/*
186*7c478bd9Sstevel@tonic-gate 	 * It is theoretically possibly for the lwp's wstate to
187*7c478bd9Sstevel@tonic-gate 	 * be different from its value assigned in lwp_stk_init,
188*7c478bd9Sstevel@tonic-gate 	 * since lwp_stk_init assumed the data model of the process.
189*7c478bd9Sstevel@tonic-gate 	 * Here, we took on the data model of the cloned lwp.
190*7c478bd9Sstevel@tonic-gate 	 */
191*7c478bd9Sstevel@tonic-gate 	if (mpcb->mpcb_wstate != wstate) {
192*7c478bd9Sstevel@tonic-gate 		size_t osize, size;
193*7c478bd9Sstevel@tonic-gate 
194*7c478bd9Sstevel@tonic-gate 		if (wstate == WSTATE_USER32) {
195*7c478bd9Sstevel@tonic-gate 			osize = MAXWIN * sizeof (struct rwindow32);
196*7c478bd9Sstevel@tonic-gate 			size = MAXWIN * sizeof (struct rwindow64);
197*7c478bd9Sstevel@tonic-gate 			wstate = WSTATE_USER64;
198*7c478bd9Sstevel@tonic-gate 		} else {
199*7c478bd9Sstevel@tonic-gate 			osize = MAXWIN * sizeof (struct rwindow64);
200*7c478bd9Sstevel@tonic-gate 			size = MAXWIN * sizeof (struct rwindow32);
201*7c478bd9Sstevel@tonic-gate 			wstate = WSTATE_USER32;
202*7c478bd9Sstevel@tonic-gate 		}
203*7c478bd9Sstevel@tonic-gate 		kmem_free(wbuf, osize);
204*7c478bd9Sstevel@tonic-gate 		wbuf = kmem_alloc(size, KM_SLEEP);
205*7c478bd9Sstevel@tonic-gate 	}
206*7c478bd9Sstevel@tonic-gate 
207*7c478bd9Sstevel@tonic-gate 	mpcb->mpcb_pa = va_to_pa(mpcb);
208*7c478bd9Sstevel@tonic-gate 	mpcb->mpcb_wbuf = wbuf;
209*7c478bd9Sstevel@tonic-gate 	mpcb->mpcb_wbuf_pa = va_to_pa(wbuf);
210*7c478bd9Sstevel@tonic-gate 
211*7c478bd9Sstevel@tonic-gate 	ASSERT(mpcb->mpcb_wstate == wstate);
212*7c478bd9Sstevel@tonic-gate 
213*7c478bd9Sstevel@tonic-gate 	if (mpcb->mpcb_wbcnt != 0) {
214*7c478bd9Sstevel@tonic-gate 		bcopy(pmpcb->mpcb_wbuf, mpcb->mpcb_wbuf,
215*7c478bd9Sstevel@tonic-gate 		    mpcb->mpcb_wbcnt * ((mpcb->mpcb_wstate == WSTATE_USER32) ?
216*7c478bd9Sstevel@tonic-gate 		    sizeof (struct rwindow32) : sizeof (struct rwindow64)));
217*7c478bd9Sstevel@tonic-gate 	}
218*7c478bd9Sstevel@tonic-gate 
219*7c478bd9Sstevel@tonic-gate 	if (pt == curthread)
220*7c478bd9Sstevel@tonic-gate 		pfp->fpu_fprs = _fp_read_fprs();
221*7c478bd9Sstevel@tonic-gate 	if ((pfp->fpu_en) || (pfp->fpu_fprs & FPRS_FEF)) {
222*7c478bd9Sstevel@tonic-gate 		if (pt == curthread && fpu_exists) {
223*7c478bd9Sstevel@tonic-gate 			save_gsr(clwp->lwp_fpu);
224*7c478bd9Sstevel@tonic-gate 		} else {
225*7c478bd9Sstevel@tonic-gate 			uint64_t gsr;
226*7c478bd9Sstevel@tonic-gate 			gsr = get_gsr(lwp->lwp_fpu);
227*7c478bd9Sstevel@tonic-gate 			set_gsr(gsr, clwp->lwp_fpu);
228*7c478bd9Sstevel@tonic-gate 		}
229*7c478bd9Sstevel@tonic-gate 		fp_fork(lwp, clwp);
230*7c478bd9Sstevel@tonic-gate 	}
231*7c478bd9Sstevel@tonic-gate }
232*7c478bd9Sstevel@tonic-gate 
233*7c478bd9Sstevel@tonic-gate /*
234*7c478bd9Sstevel@tonic-gate  * Free lwp fpu regs.
235*7c478bd9Sstevel@tonic-gate  */
236*7c478bd9Sstevel@tonic-gate void
237*7c478bd9Sstevel@tonic-gate lwp_freeregs(klwp_t *lwp, int isexec)
238*7c478bd9Sstevel@tonic-gate {
239*7c478bd9Sstevel@tonic-gate 	kfpu_t *fp = lwptofpu(lwp);
240*7c478bd9Sstevel@tonic-gate 
241*7c478bd9Sstevel@tonic-gate 	if (lwptot(lwp) == curthread)
242*7c478bd9Sstevel@tonic-gate 		fp->fpu_fprs = _fp_read_fprs();
243*7c478bd9Sstevel@tonic-gate 	if ((fp->fpu_en) || (fp->fpu_fprs & FPRS_FEF))
244*7c478bd9Sstevel@tonic-gate 		fp_free(fp, isexec);
245*7c478bd9Sstevel@tonic-gate }
246*7c478bd9Sstevel@tonic-gate 
247*7c478bd9Sstevel@tonic-gate /*
248*7c478bd9Sstevel@tonic-gate  * fill in the extra register state area specified with the
249*7c478bd9Sstevel@tonic-gate  * specified lwp's platform-dependent non-floating-point extra
250*7c478bd9Sstevel@tonic-gate  * register state information
251*7c478bd9Sstevel@tonic-gate  */
252*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
253*7c478bd9Sstevel@tonic-gate void
254*7c478bd9Sstevel@tonic-gate xregs_getgfiller(klwp_id_t lwp, caddr_t xrp)
255*7c478bd9Sstevel@tonic-gate {
256*7c478bd9Sstevel@tonic-gate 	/* for sun4u nothing to do here, added for symmetry */
257*7c478bd9Sstevel@tonic-gate }
258*7c478bd9Sstevel@tonic-gate 
259*7c478bd9Sstevel@tonic-gate /*
260*7c478bd9Sstevel@tonic-gate  * fill in the extra register state area specified with the specified lwp's
261*7c478bd9Sstevel@tonic-gate  * platform-dependent floating-point extra register state information.
262*7c478bd9Sstevel@tonic-gate  * NOTE:  'lwp' might not correspond to 'curthread' since this is
263*7c478bd9Sstevel@tonic-gate  * called from code in /proc to get the registers of another lwp.
264*7c478bd9Sstevel@tonic-gate  */
265*7c478bd9Sstevel@tonic-gate void
266*7c478bd9Sstevel@tonic-gate xregs_getfpfiller(klwp_id_t lwp, caddr_t xrp)
267*7c478bd9Sstevel@tonic-gate {
268*7c478bd9Sstevel@tonic-gate 	prxregset_t *xregs = (prxregset_t *)xrp;
269*7c478bd9Sstevel@tonic-gate 	kfpu_t *fp = lwptofpu(lwp);
270*7c478bd9Sstevel@tonic-gate 	uint32_t fprs = (FPRS_FEF|FPRS_DU|FPRS_DL);
271*7c478bd9Sstevel@tonic-gate 	uint64_t gsr;
272*7c478bd9Sstevel@tonic-gate 
273*7c478bd9Sstevel@tonic-gate 	/*
274*7c478bd9Sstevel@tonic-gate 	 * fp_fksave() does not flush the GSR register into
275*7c478bd9Sstevel@tonic-gate 	 * the lwp area, so do it now
276*7c478bd9Sstevel@tonic-gate 	 */
277*7c478bd9Sstevel@tonic-gate 	kpreempt_disable();
278*7c478bd9Sstevel@tonic-gate 	if (ttolwp(curthread) == lwp && fpu_exists) {
279*7c478bd9Sstevel@tonic-gate 		fp->fpu_fprs = _fp_read_fprs();
280*7c478bd9Sstevel@tonic-gate 		if ((fp->fpu_fprs & FPRS_FEF) != FPRS_FEF) {
281*7c478bd9Sstevel@tonic-gate 			_fp_write_fprs(fprs);
282*7c478bd9Sstevel@tonic-gate 			fp->fpu_fprs = (V9_FPU_FPRS_TYPE)fprs;
283*7c478bd9Sstevel@tonic-gate 		}
284*7c478bd9Sstevel@tonic-gate 		save_gsr(fp);
285*7c478bd9Sstevel@tonic-gate 	}
286*7c478bd9Sstevel@tonic-gate 	gsr = get_gsr(fp);
287*7c478bd9Sstevel@tonic-gate 	kpreempt_enable();
288*7c478bd9Sstevel@tonic-gate 	PRXREG_GSR(xregs) = gsr;
289*7c478bd9Sstevel@tonic-gate }
290*7c478bd9Sstevel@tonic-gate 
291*7c478bd9Sstevel@tonic-gate /*
292*7c478bd9Sstevel@tonic-gate  * set the specified lwp's platform-dependent non-floating-point
293*7c478bd9Sstevel@tonic-gate  * extra register state based on the specified input
294*7c478bd9Sstevel@tonic-gate  */
295*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
296*7c478bd9Sstevel@tonic-gate void
297*7c478bd9Sstevel@tonic-gate xregs_setgfiller(klwp_id_t lwp, caddr_t xrp)
298*7c478bd9Sstevel@tonic-gate {
299*7c478bd9Sstevel@tonic-gate 	/* for sun4u nothing to do here, added for symmetry */
300*7c478bd9Sstevel@tonic-gate }
301*7c478bd9Sstevel@tonic-gate 
302*7c478bd9Sstevel@tonic-gate /*
303*7c478bd9Sstevel@tonic-gate  * set the specified lwp's platform-dependent floating-point
304*7c478bd9Sstevel@tonic-gate  * extra register state based on the specified input
305*7c478bd9Sstevel@tonic-gate  */
306*7c478bd9Sstevel@tonic-gate void
307*7c478bd9Sstevel@tonic-gate xregs_setfpfiller(klwp_id_t lwp, caddr_t xrp)
308*7c478bd9Sstevel@tonic-gate {
309*7c478bd9Sstevel@tonic-gate 	prxregset_t *xregs = (prxregset_t *)xrp;
310*7c478bd9Sstevel@tonic-gate 	kfpu_t *fp = lwptofpu(lwp);
311*7c478bd9Sstevel@tonic-gate 	uint32_t fprs = (FPRS_FEF|FPRS_DU|FPRS_DL);
312*7c478bd9Sstevel@tonic-gate 	uint64_t gsr = PRXREG_GSR(xregs);
313*7c478bd9Sstevel@tonic-gate 
314*7c478bd9Sstevel@tonic-gate 	kpreempt_disable();
315*7c478bd9Sstevel@tonic-gate 	set_gsr(gsr, lwptofpu(lwp));
316*7c478bd9Sstevel@tonic-gate 
317*7c478bd9Sstevel@tonic-gate 	if ((lwp == ttolwp(curthread)) && fpu_exists) {
318*7c478bd9Sstevel@tonic-gate 		fp->fpu_fprs = _fp_read_fprs();
319*7c478bd9Sstevel@tonic-gate 		if ((fp->fpu_fprs & FPRS_FEF) != FPRS_FEF) {
320*7c478bd9Sstevel@tonic-gate 			_fp_write_fprs(fprs);
321*7c478bd9Sstevel@tonic-gate 			fp->fpu_fprs = (V9_FPU_FPRS_TYPE)fprs;
322*7c478bd9Sstevel@tonic-gate 		}
323*7c478bd9Sstevel@tonic-gate 		restore_gsr(lwptofpu(lwp));
324*7c478bd9Sstevel@tonic-gate 	}
325*7c478bd9Sstevel@tonic-gate 	kpreempt_enable();
326*7c478bd9Sstevel@tonic-gate }
327*7c478bd9Sstevel@tonic-gate 
328*7c478bd9Sstevel@tonic-gate /*
329*7c478bd9Sstevel@tonic-gate  * fill in the sun4u asrs, ie, the lwp's platform-dependent
330*7c478bd9Sstevel@tonic-gate  * non-floating-point extra register state information
331*7c478bd9Sstevel@tonic-gate  */
332*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
333*7c478bd9Sstevel@tonic-gate void
334*7c478bd9Sstevel@tonic-gate getasrs(klwp_t *lwp, asrset_t asr)
335*7c478bd9Sstevel@tonic-gate {
336*7c478bd9Sstevel@tonic-gate 	/* for sun4u nothing to do here, added for symmetry */
337*7c478bd9Sstevel@tonic-gate }
338*7c478bd9Sstevel@tonic-gate 
339*7c478bd9Sstevel@tonic-gate /*
340*7c478bd9Sstevel@tonic-gate  * fill in the sun4u asrs, ie, the lwp's platform-dependent
341*7c478bd9Sstevel@tonic-gate  * floating-point extra register state information
342*7c478bd9Sstevel@tonic-gate  */
343*7c478bd9Sstevel@tonic-gate void
344*7c478bd9Sstevel@tonic-gate getfpasrs(klwp_t *lwp, asrset_t asr)
345*7c478bd9Sstevel@tonic-gate {
346*7c478bd9Sstevel@tonic-gate 	kfpu_t *fp = lwptofpu(lwp);
347*7c478bd9Sstevel@tonic-gate 	uint32_t fprs = (FPRS_FEF|FPRS_DU|FPRS_DL);
348*7c478bd9Sstevel@tonic-gate 
349*7c478bd9Sstevel@tonic-gate 	kpreempt_disable();
350*7c478bd9Sstevel@tonic-gate 	if (ttolwp(curthread) == lwp)
351*7c478bd9Sstevel@tonic-gate 		fp->fpu_fprs = _fp_read_fprs();
352*7c478bd9Sstevel@tonic-gate 	if ((fp->fpu_en) || (fp->fpu_fprs & FPRS_FEF)) {
353*7c478bd9Sstevel@tonic-gate 		if (fpu_exists && ttolwp(curthread) == lwp) {
354*7c478bd9Sstevel@tonic-gate 			if ((fp->fpu_fprs & FPRS_FEF) != FPRS_FEF) {
355*7c478bd9Sstevel@tonic-gate 				_fp_write_fprs(fprs);
356*7c478bd9Sstevel@tonic-gate 				fp->fpu_fprs = (V9_FPU_FPRS_TYPE)fprs;
357*7c478bd9Sstevel@tonic-gate 			}
358*7c478bd9Sstevel@tonic-gate 			save_gsr(fp);
359*7c478bd9Sstevel@tonic-gate 		}
360*7c478bd9Sstevel@tonic-gate 		asr[ASR_GSR] = (int64_t)get_gsr(fp);
361*7c478bd9Sstevel@tonic-gate 	}
362*7c478bd9Sstevel@tonic-gate 	kpreempt_enable();
363*7c478bd9Sstevel@tonic-gate }
364*7c478bd9Sstevel@tonic-gate 
365*7c478bd9Sstevel@tonic-gate /*
366*7c478bd9Sstevel@tonic-gate  * set the sun4u asrs, ie, the lwp's platform-dependent
367*7c478bd9Sstevel@tonic-gate  * non-floating-point extra register state information
368*7c478bd9Sstevel@tonic-gate  */
369*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
370*7c478bd9Sstevel@tonic-gate void
371*7c478bd9Sstevel@tonic-gate setasrs(klwp_t *lwp, asrset_t asr)
372*7c478bd9Sstevel@tonic-gate {
373*7c478bd9Sstevel@tonic-gate 	/* for sun4u nothing to do here, added for symmetry */
374*7c478bd9Sstevel@tonic-gate }
375*7c478bd9Sstevel@tonic-gate 
376*7c478bd9Sstevel@tonic-gate void
377*7c478bd9Sstevel@tonic-gate setfpasrs(klwp_t *lwp, asrset_t asr)
378*7c478bd9Sstevel@tonic-gate {
379*7c478bd9Sstevel@tonic-gate 	kfpu_t *fp = lwptofpu(lwp);
380*7c478bd9Sstevel@tonic-gate 	uint32_t fprs = (FPRS_FEF|FPRS_DU|FPRS_DL);
381*7c478bd9Sstevel@tonic-gate 
382*7c478bd9Sstevel@tonic-gate 	kpreempt_disable();
383*7c478bd9Sstevel@tonic-gate 	if (ttolwp(curthread) == lwp)
384*7c478bd9Sstevel@tonic-gate 		fp->fpu_fprs = _fp_read_fprs();
385*7c478bd9Sstevel@tonic-gate 	if ((fp->fpu_en) || (fp->fpu_fprs & FPRS_FEF)) {
386*7c478bd9Sstevel@tonic-gate 		set_gsr(asr[ASR_GSR], fp);
387*7c478bd9Sstevel@tonic-gate 		if (fpu_exists && ttolwp(curthread) == lwp) {
388*7c478bd9Sstevel@tonic-gate 			if ((fp->fpu_fprs & FPRS_FEF) != FPRS_FEF) {
389*7c478bd9Sstevel@tonic-gate 				_fp_write_fprs(fprs);
390*7c478bd9Sstevel@tonic-gate 				fp->fpu_fprs = (V9_FPU_FPRS_TYPE)fprs;
391*7c478bd9Sstevel@tonic-gate 			}
392*7c478bd9Sstevel@tonic-gate 			restore_gsr(fp);
393*7c478bd9Sstevel@tonic-gate 		}
394*7c478bd9Sstevel@tonic-gate 	}
395*7c478bd9Sstevel@tonic-gate 	kpreempt_enable();
396*7c478bd9Sstevel@tonic-gate }
397*7c478bd9Sstevel@tonic-gate 
398*7c478bd9Sstevel@tonic-gate /*
399*7c478bd9Sstevel@tonic-gate  * Create interrupt kstats for this CPU.
400*7c478bd9Sstevel@tonic-gate  */
401*7c478bd9Sstevel@tonic-gate void
402*7c478bd9Sstevel@tonic-gate cpu_create_intrstat(cpu_t *cp)
403*7c478bd9Sstevel@tonic-gate {
404*7c478bd9Sstevel@tonic-gate 	int		i;
405*7c478bd9Sstevel@tonic-gate 	kstat_t		*intr_ksp;
406*7c478bd9Sstevel@tonic-gate 	kstat_named_t	*knp;
407*7c478bd9Sstevel@tonic-gate 	char		name[KSTAT_STRLEN];
408*7c478bd9Sstevel@tonic-gate 	zoneid_t	zoneid;
409*7c478bd9Sstevel@tonic-gate 
410*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
411*7c478bd9Sstevel@tonic-gate 
412*7c478bd9Sstevel@tonic-gate 	if (pool_pset_enabled())
413*7c478bd9Sstevel@tonic-gate 		zoneid = GLOBAL_ZONEID;
414*7c478bd9Sstevel@tonic-gate 	else
415*7c478bd9Sstevel@tonic-gate 		zoneid = ALL_ZONES;
416*7c478bd9Sstevel@tonic-gate 
417*7c478bd9Sstevel@tonic-gate 	intr_ksp = kstat_create_zone("cpu", cp->cpu_id, "intrstat", "misc",
418*7c478bd9Sstevel@tonic-gate 	    KSTAT_TYPE_NAMED, PIL_MAX * 2, NULL, zoneid);
419*7c478bd9Sstevel@tonic-gate 
420*7c478bd9Sstevel@tonic-gate 	/*
421*7c478bd9Sstevel@tonic-gate 	 * Initialize each PIL's named kstat
422*7c478bd9Sstevel@tonic-gate 	 */
423*7c478bd9Sstevel@tonic-gate 	if (intr_ksp != NULL) {
424*7c478bd9Sstevel@tonic-gate 		intr_ksp->ks_update = cpu_kstat_intrstat_update;
425*7c478bd9Sstevel@tonic-gate 		knp = (kstat_named_t *)intr_ksp->ks_data;
426*7c478bd9Sstevel@tonic-gate 		intr_ksp->ks_private = cp;
427*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < PIL_MAX; i++) {
428*7c478bd9Sstevel@tonic-gate 			(void) snprintf(name, KSTAT_STRLEN, "level-%d-time",
429*7c478bd9Sstevel@tonic-gate 			    i + 1);
430*7c478bd9Sstevel@tonic-gate 			kstat_named_init(&knp[i * 2], name, KSTAT_DATA_UINT64);
431*7c478bd9Sstevel@tonic-gate 			(void) snprintf(name, KSTAT_STRLEN, "level-%d-count",
432*7c478bd9Sstevel@tonic-gate 			    i + 1);
433*7c478bd9Sstevel@tonic-gate 			kstat_named_init(&knp[(i * 2) + 1], name,
434*7c478bd9Sstevel@tonic-gate 			    KSTAT_DATA_UINT64);
435*7c478bd9Sstevel@tonic-gate 		}
436*7c478bd9Sstevel@tonic-gate 		kstat_install(intr_ksp);
437*7c478bd9Sstevel@tonic-gate 	}
438*7c478bd9Sstevel@tonic-gate }
439*7c478bd9Sstevel@tonic-gate 
440*7c478bd9Sstevel@tonic-gate /*
441*7c478bd9Sstevel@tonic-gate  * Delete interrupt kstats for this CPU.
442*7c478bd9Sstevel@tonic-gate  */
443*7c478bd9Sstevel@tonic-gate void
444*7c478bd9Sstevel@tonic-gate cpu_delete_intrstat(cpu_t *cp)
445*7c478bd9Sstevel@tonic-gate {
446*7c478bd9Sstevel@tonic-gate 	kstat_delete_byname_zone("cpu", cp->cpu_id, "intrstat", ALL_ZONES);
447*7c478bd9Sstevel@tonic-gate }
448*7c478bd9Sstevel@tonic-gate 
449*7c478bd9Sstevel@tonic-gate /*
450*7c478bd9Sstevel@tonic-gate  * Convert interrupt statistics from CPU ticks to nanoseconds and
451*7c478bd9Sstevel@tonic-gate  * update kstat.
452*7c478bd9Sstevel@tonic-gate  */
453*7c478bd9Sstevel@tonic-gate int
454*7c478bd9Sstevel@tonic-gate cpu_kstat_intrstat_update(kstat_t *ksp, int rw)
455*7c478bd9Sstevel@tonic-gate {
456*7c478bd9Sstevel@tonic-gate 	kstat_named_t	*knp = ksp->ks_data;
457*7c478bd9Sstevel@tonic-gate 	cpu_t		*cpup = (cpu_t *)ksp->ks_private;
458*7c478bd9Sstevel@tonic-gate 	int		i;
459*7c478bd9Sstevel@tonic-gate 
460*7c478bd9Sstevel@tonic-gate 	if (rw == KSTAT_WRITE)
461*7c478bd9Sstevel@tonic-gate 		return (EACCES);
462*7c478bd9Sstevel@tonic-gate 
463*7c478bd9Sstevel@tonic-gate 	/*
464*7c478bd9Sstevel@tonic-gate 	 * We use separate passes to copy and convert the statistics to
465*7c478bd9Sstevel@tonic-gate 	 * nanoseconds. This assures that the snapshot of the data is as
466*7c478bd9Sstevel@tonic-gate 	 * self-consistent as possible.
467*7c478bd9Sstevel@tonic-gate 	 */
468*7c478bd9Sstevel@tonic-gate 
469*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < PIL_MAX; i++) {
470*7c478bd9Sstevel@tonic-gate 		knp[i * 2].value.ui64 = cpup->cpu_m.intrstat[i + 1][0];
471*7c478bd9Sstevel@tonic-gate 		knp[(i * 2) + 1].value.ui64 = cpup->cpu_stats.sys.intr[i];
472*7c478bd9Sstevel@tonic-gate 	}
473*7c478bd9Sstevel@tonic-gate 
474*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < PIL_MAX; i++) {
475*7c478bd9Sstevel@tonic-gate 		knp[i * 2].value.ui64 =
476*7c478bd9Sstevel@tonic-gate 		    (uint64_t)tick2ns((hrtime_t)knp[i * 2].value.ui64,
477*7c478bd9Sstevel@tonic-gate 			cpup->cpu_id);
478*7c478bd9Sstevel@tonic-gate 	}
479*7c478bd9Sstevel@tonic-gate 
480*7c478bd9Sstevel@tonic-gate 	return (0);
481*7c478bd9Sstevel@tonic-gate }
482*7c478bd9Sstevel@tonic-gate 
483*7c478bd9Sstevel@tonic-gate /*
484*7c478bd9Sstevel@tonic-gate  * Called by common/os/cpu.c for psrinfo(1m) kstats
485*7c478bd9Sstevel@tonic-gate  */
486*7c478bd9Sstevel@tonic-gate char *
487*7c478bd9Sstevel@tonic-gate cpu_fru_fmri(cpu_t *cp)
488*7c478bd9Sstevel@tonic-gate {
489*7c478bd9Sstevel@tonic-gate 	return (cpunodes[cp->cpu_id].fru_fmri);
490*7c478bd9Sstevel@tonic-gate }
491*7c478bd9Sstevel@tonic-gate 
492*7c478bd9Sstevel@tonic-gate /*
493*7c478bd9Sstevel@tonic-gate  * An interrupt thread is ending a time slice, so compute the interval it
494*7c478bd9Sstevel@tonic-gate  * ran for and update the statistic for its PIL.
495*7c478bd9Sstevel@tonic-gate  */
496*7c478bd9Sstevel@tonic-gate void
497*7c478bd9Sstevel@tonic-gate cpu_intr_swtch_enter(kthread_id_t t)
498*7c478bd9Sstevel@tonic-gate {
499*7c478bd9Sstevel@tonic-gate 	uint64_t	interval;
500*7c478bd9Sstevel@tonic-gate 	uint64_t	start;
501*7c478bd9Sstevel@tonic-gate 
502*7c478bd9Sstevel@tonic-gate 	ASSERT((t->t_flag & T_INTR_THREAD) != 0);
503*7c478bd9Sstevel@tonic-gate 	ASSERT(t->t_pil > 0 && t->t_pil <= LOCK_LEVEL);
504*7c478bd9Sstevel@tonic-gate 
505*7c478bd9Sstevel@tonic-gate 	/*
506*7c478bd9Sstevel@tonic-gate 	 * We could be here with a zero timestamp. This could happen if:
507*7c478bd9Sstevel@tonic-gate 	 * an interrupt thread which no longer has a pinned thread underneath
508*7c478bd9Sstevel@tonic-gate 	 * it (i.e. it blocked at some point in its past) has finished running
509*7c478bd9Sstevel@tonic-gate 	 * its handler. intr_thread() updated the interrupt statistic for its
510*7c478bd9Sstevel@tonic-gate 	 * PIL and zeroed its timestamp. Since there was no pinned thread to
511*7c478bd9Sstevel@tonic-gate 	 * return to, swtch() gets called and we end up here.
512*7c478bd9Sstevel@tonic-gate 	 *
513*7c478bd9Sstevel@tonic-gate 	 * It can also happen if an interrupt thread in intr_thread() calls
514*7c478bd9Sstevel@tonic-gate 	 * preempt. It will have already taken care of updating stats. In
515*7c478bd9Sstevel@tonic-gate 	 * this event, the interrupt thread will be runnable.
516*7c478bd9Sstevel@tonic-gate 	 */
517*7c478bd9Sstevel@tonic-gate 	if (t->t_intr_start) {
518*7c478bd9Sstevel@tonic-gate 		do {
519*7c478bd9Sstevel@tonic-gate 			start = t->t_intr_start;
520*7c478bd9Sstevel@tonic-gate 			interval = gettick_counter() - start;
521*7c478bd9Sstevel@tonic-gate 		} while (cas64(&t->t_intr_start, start, 0) != start);
522*7c478bd9Sstevel@tonic-gate 		if (CPU->cpu_m.divisor > 1)
523*7c478bd9Sstevel@tonic-gate 			interval *= CPU->cpu_m.divisor;
524*7c478bd9Sstevel@tonic-gate 		CPU->cpu_m.intrstat[t->t_pil][0] += interval;
525*7c478bd9Sstevel@tonic-gate 	} else
526*7c478bd9Sstevel@tonic-gate 		ASSERT(t->t_intr == NULL || t->t_state == TS_RUN);
527*7c478bd9Sstevel@tonic-gate }
528*7c478bd9Sstevel@tonic-gate 
529*7c478bd9Sstevel@tonic-gate 
530*7c478bd9Sstevel@tonic-gate /*
531*7c478bd9Sstevel@tonic-gate  * An interrupt thread is returning from swtch(). Place a starting timestamp
532*7c478bd9Sstevel@tonic-gate  * in its thread structure.
533*7c478bd9Sstevel@tonic-gate  */
534*7c478bd9Sstevel@tonic-gate void
535*7c478bd9Sstevel@tonic-gate cpu_intr_swtch_exit(kthread_id_t t)
536*7c478bd9Sstevel@tonic-gate {
537*7c478bd9Sstevel@tonic-gate 	uint64_t ts;
538*7c478bd9Sstevel@tonic-gate 
539*7c478bd9Sstevel@tonic-gate 	ASSERT((t->t_flag & T_INTR_THREAD) != 0);
540*7c478bd9Sstevel@tonic-gate 	ASSERT(t->t_pil > 0 && t->t_pil <= LOCK_LEVEL);
541*7c478bd9Sstevel@tonic-gate 
542*7c478bd9Sstevel@tonic-gate 	do {
543*7c478bd9Sstevel@tonic-gate 		ts = t->t_intr_start;
544*7c478bd9Sstevel@tonic-gate 	} while (cas64(&t->t_intr_start, ts, gettick_counter()) != ts);
545*7c478bd9Sstevel@tonic-gate }
546*7c478bd9Sstevel@tonic-gate 
547*7c478bd9Sstevel@tonic-gate 
548*7c478bd9Sstevel@tonic-gate int
549*7c478bd9Sstevel@tonic-gate blacklist(int cmd, const char *scheme, nvlist_t *fmri, const char *class)
550*7c478bd9Sstevel@tonic-gate {
551*7c478bd9Sstevel@tonic-gate 	if (&plat_blacklist)
552*7c478bd9Sstevel@tonic-gate 		return (plat_blacklist(cmd, scheme, fmri, class));
553*7c478bd9Sstevel@tonic-gate 
554*7c478bd9Sstevel@tonic-gate 	return (ENOTSUP);
555*7c478bd9Sstevel@tonic-gate }
556*7c478bd9Sstevel@tonic-gate 
557*7c478bd9Sstevel@tonic-gate int
558*7c478bd9Sstevel@tonic-gate kdi_pread(caddr_t buf, size_t nbytes, uint64_t addr, size_t *ncopiedp)
559*7c478bd9Sstevel@tonic-gate {
560*7c478bd9Sstevel@tonic-gate 	extern void kdi_flush_caches(void);
561*7c478bd9Sstevel@tonic-gate 	size_t nread = 0;
562*7c478bd9Sstevel@tonic-gate 	uint32_t word;
563*7c478bd9Sstevel@tonic-gate 	int slop, i;
564*7c478bd9Sstevel@tonic-gate 
565*7c478bd9Sstevel@tonic-gate 	kdi_flush_caches();
566*7c478bd9Sstevel@tonic-gate 	membar_enter();
567*7c478bd9Sstevel@tonic-gate 
568*7c478bd9Sstevel@tonic-gate 	/* We might not begin on a word boundary. */
569*7c478bd9Sstevel@tonic-gate 	if ((slop = addr & 3) != 0) {
570*7c478bd9Sstevel@tonic-gate 		word = ldphys(addr & ~3);
571*7c478bd9Sstevel@tonic-gate 		for (i = slop; i < 4 && nbytes > 0; i++, nbytes--, nread++)
572*7c478bd9Sstevel@tonic-gate 			*buf++ = ((uchar_t *)&word)[i];
573*7c478bd9Sstevel@tonic-gate 		addr = roundup(addr, 4);
574*7c478bd9Sstevel@tonic-gate 	}
575*7c478bd9Sstevel@tonic-gate 
576*7c478bd9Sstevel@tonic-gate 	while (nbytes > 0) {
577*7c478bd9Sstevel@tonic-gate 		word = ldphys(addr);
578*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < 4 && nbytes > 0; i++, nbytes--, nread++, addr++)
579*7c478bd9Sstevel@tonic-gate 			*buf++ = ((uchar_t *)&word)[i];
580*7c478bd9Sstevel@tonic-gate 	}
581*7c478bd9Sstevel@tonic-gate 
582*7c478bd9Sstevel@tonic-gate 	kdi_flush_caches();
583*7c478bd9Sstevel@tonic-gate 
584*7c478bd9Sstevel@tonic-gate 	*ncopiedp = nread;
585*7c478bd9Sstevel@tonic-gate 	return (0);
586*7c478bd9Sstevel@tonic-gate }
587*7c478bd9Sstevel@tonic-gate 
588*7c478bd9Sstevel@tonic-gate int
589*7c478bd9Sstevel@tonic-gate kdi_pwrite(caddr_t buf, size_t nbytes, uint64_t addr, size_t *ncopiedp)
590*7c478bd9Sstevel@tonic-gate {
591*7c478bd9Sstevel@tonic-gate 	extern void kdi_flush_caches(void);
592*7c478bd9Sstevel@tonic-gate 	size_t nwritten = 0;
593*7c478bd9Sstevel@tonic-gate 	uint32_t word;
594*7c478bd9Sstevel@tonic-gate 	int slop, i;
595*7c478bd9Sstevel@tonic-gate 
596*7c478bd9Sstevel@tonic-gate 	kdi_flush_caches();
597*7c478bd9Sstevel@tonic-gate 
598*7c478bd9Sstevel@tonic-gate 	/* We might not begin on a word boundary. */
599*7c478bd9Sstevel@tonic-gate 	if ((slop = addr & 3) != 0) {
600*7c478bd9Sstevel@tonic-gate 		word = ldphys(addr & ~3);
601*7c478bd9Sstevel@tonic-gate 		for (i = slop; i < 4 && nbytes > 0; i++, nbytes--, nwritten++)
602*7c478bd9Sstevel@tonic-gate 			((uchar_t *)&word)[i] = *buf++;
603*7c478bd9Sstevel@tonic-gate 		stphys(addr & ~3, word);
604*7c478bd9Sstevel@tonic-gate 		addr = roundup(addr, 4);
605*7c478bd9Sstevel@tonic-gate 	}
606*7c478bd9Sstevel@tonic-gate 
607*7c478bd9Sstevel@tonic-gate 	while (nbytes > 3) {
608*7c478bd9Sstevel@tonic-gate 		for (word = 0, i = 0; i < 4; i++, nbytes--, nwritten++)
609*7c478bd9Sstevel@tonic-gate 			((uchar_t *)&word)[i] = *buf++;
610*7c478bd9Sstevel@tonic-gate 		stphys(addr, word);
611*7c478bd9Sstevel@tonic-gate 		addr += 4;
612*7c478bd9Sstevel@tonic-gate 	}
613*7c478bd9Sstevel@tonic-gate 
614*7c478bd9Sstevel@tonic-gate 	/* We might not end with a whole word. */
615*7c478bd9Sstevel@tonic-gate 	if (nbytes > 0) {
616*7c478bd9Sstevel@tonic-gate 		word = ldphys(addr);
617*7c478bd9Sstevel@tonic-gate 		for (i = 0; nbytes > 0; i++, nbytes--, nwritten++)
618*7c478bd9Sstevel@tonic-gate 			((uchar_t *)&word)[i] = *buf++;
619*7c478bd9Sstevel@tonic-gate 		stphys(addr, word);
620*7c478bd9Sstevel@tonic-gate 	}
621*7c478bd9Sstevel@tonic-gate 
622*7c478bd9Sstevel@tonic-gate 	membar_enter();
623*7c478bd9Sstevel@tonic-gate 	kdi_flush_caches();
624*7c478bd9Sstevel@tonic-gate 
625*7c478bd9Sstevel@tonic-gate 	*ncopiedp = nwritten;
626*7c478bd9Sstevel@tonic-gate 	return (0);
627*7c478bd9Sstevel@tonic-gate }
628*7c478bd9Sstevel@tonic-gate 
629*7c478bd9Sstevel@tonic-gate static void
630*7c478bd9Sstevel@tonic-gate kdi_kernpanic(struct regs *regs, uint_t tt)
631*7c478bd9Sstevel@tonic-gate {
632*7c478bd9Sstevel@tonic-gate 	sync_reg_buf = *regs;
633*7c478bd9Sstevel@tonic-gate 	sync_tt = tt;
634*7c478bd9Sstevel@tonic-gate 
635*7c478bd9Sstevel@tonic-gate 	sync_handler();
636*7c478bd9Sstevel@tonic-gate }
637*7c478bd9Sstevel@tonic-gate 
638*7c478bd9Sstevel@tonic-gate static void
639*7c478bd9Sstevel@tonic-gate kdi_plat_call(void (*platfn)(void))
640*7c478bd9Sstevel@tonic-gate {
641*7c478bd9Sstevel@tonic-gate 	if (platfn != NULL) {
642*7c478bd9Sstevel@tonic-gate 		prom_suspend_prepost();
643*7c478bd9Sstevel@tonic-gate 		platfn();
644*7c478bd9Sstevel@tonic-gate 		prom_resume_prepost();
645*7c478bd9Sstevel@tonic-gate 	}
646*7c478bd9Sstevel@tonic-gate }
647*7c478bd9Sstevel@tonic-gate 
648*7c478bd9Sstevel@tonic-gate void
649*7c478bd9Sstevel@tonic-gate mach_kdi_init(kdi_t *kdi)
650*7c478bd9Sstevel@tonic-gate {
651*7c478bd9Sstevel@tonic-gate 	kdi->kdi_plat_call = kdi_plat_call;
652*7c478bd9Sstevel@tonic-gate 	kdi->mkdi_cpu_index = kdi_cpu_index;
653*7c478bd9Sstevel@tonic-gate 	kdi->mkdi_trap_vatotte = kdi_trap_vatotte;
654*7c478bd9Sstevel@tonic-gate 	kdi->mkdi_kernpanic = kdi_kernpanic;
655*7c478bd9Sstevel@tonic-gate }
656