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