xref: /illumos-gate/usr/src/uts/intel/fs/proc/prmachdep.c (revision 7c478bd9)
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 2004 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 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
34*7c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/cred.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/debug.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/inline.h>
39*7c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/proc.h>
41*7c478bd9Sstevel@tonic-gate #include <sys/regset.h>
42*7c478bd9Sstevel@tonic-gate #include <sys/privregs.h>
43*7c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
44*7c478bd9Sstevel@tonic-gate #include <sys/systm.h>
45*7c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
46*7c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
47*7c478bd9Sstevel@tonic-gate #include <sys/psw.h>
48*7c478bd9Sstevel@tonic-gate #include <sys/pcb.h>
49*7c478bd9Sstevel@tonic-gate #include <sys/buf.h>
50*7c478bd9Sstevel@tonic-gate #include <sys/signal.h>
51*7c478bd9Sstevel@tonic-gate #include <sys/user.h>
52*7c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
53*7c478bd9Sstevel@tonic-gate 
54*7c478bd9Sstevel@tonic-gate #include <sys/fault.h>
55*7c478bd9Sstevel@tonic-gate #include <sys/syscall.h>
56*7c478bd9Sstevel@tonic-gate #include <sys/procfs.h>
57*7c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
58*7c478bd9Sstevel@tonic-gate #include <sys/stack.h>
59*7c478bd9Sstevel@tonic-gate #include <sys/debugreg.h>
60*7c478bd9Sstevel@tonic-gate #include <sys/copyops.h>
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate #include <sys/mmu.h>
63*7c478bd9Sstevel@tonic-gate #include <sys/pte.h>
64*7c478bd9Sstevel@tonic-gate #include <sys/vmem.h>
65*7c478bd9Sstevel@tonic-gate #include <sys/mman.h>
66*7c478bd9Sstevel@tonic-gate #include <sys/vmparam.h>
67*7c478bd9Sstevel@tonic-gate #include <sys/fp.h>
68*7c478bd9Sstevel@tonic-gate #include <sys/archsystm.h>
69*7c478bd9Sstevel@tonic-gate #include <sys/vmsystm.h>
70*7c478bd9Sstevel@tonic-gate #include <vm/hat.h>
71*7c478bd9Sstevel@tonic-gate #include <vm/as.h>
72*7c478bd9Sstevel@tonic-gate #include <vm/seg.h>
73*7c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h>
74*7c478bd9Sstevel@tonic-gate #include <vm/seg_kp.h>
75*7c478bd9Sstevel@tonic-gate #include <vm/page.h>
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate #include <sys/sysi86.h>
78*7c478bd9Sstevel@tonic-gate 
79*7c478bd9Sstevel@tonic-gate #include <fs/proc/prdata.h>
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate int	prnwatch = 10000;	/* maximum number of watched areas */
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate /*
84*7c478bd9Sstevel@tonic-gate  * Force a thread into the kernel if it is not already there.
85*7c478bd9Sstevel@tonic-gate  * This is a no-op on uniprocessors.
86*7c478bd9Sstevel@tonic-gate  */
87*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
88*7c478bd9Sstevel@tonic-gate void
89*7c478bd9Sstevel@tonic-gate prpokethread(kthread_t *t)
90*7c478bd9Sstevel@tonic-gate {
91*7c478bd9Sstevel@tonic-gate 	if (t->t_state == TS_ONPROC && t->t_cpu != CPU)
92*7c478bd9Sstevel@tonic-gate 		poke_cpu(t->t_cpu->cpu_id);
93*7c478bd9Sstevel@tonic-gate }
94*7c478bd9Sstevel@tonic-gate 
95*7c478bd9Sstevel@tonic-gate /*
96*7c478bd9Sstevel@tonic-gate  * Return general registers.
97*7c478bd9Sstevel@tonic-gate  */
98*7c478bd9Sstevel@tonic-gate void
99*7c478bd9Sstevel@tonic-gate prgetprregs(klwp_t *lwp, prgregset_t prp)
100*7c478bd9Sstevel@tonic-gate {
101*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&lwptoproc(lwp)->p_lock));
102*7c478bd9Sstevel@tonic-gate 
103*7c478bd9Sstevel@tonic-gate 	getgregs(lwp, prp);
104*7c478bd9Sstevel@tonic-gate }
105*7c478bd9Sstevel@tonic-gate 
106*7c478bd9Sstevel@tonic-gate /*
107*7c478bd9Sstevel@tonic-gate  * Set general registers.
108*7c478bd9Sstevel@tonic-gate  * (Note: This can be an alias to setgregs().)
109*7c478bd9Sstevel@tonic-gate  */
110*7c478bd9Sstevel@tonic-gate void
111*7c478bd9Sstevel@tonic-gate prsetprregs(klwp_t *lwp, prgregset_t prp, int initial)
112*7c478bd9Sstevel@tonic-gate {
113*7c478bd9Sstevel@tonic-gate 	if (initial)		/* set initial values */
114*7c478bd9Sstevel@tonic-gate 		lwptoregs(lwp)->r_ps = PSL_USER;
115*7c478bd9Sstevel@tonic-gate 	(void) setgregs(lwp, prp);
116*7c478bd9Sstevel@tonic-gate }
117*7c478bd9Sstevel@tonic-gate 
118*7c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
119*7c478bd9Sstevel@tonic-gate 
120*7c478bd9Sstevel@tonic-gate /*
121*7c478bd9Sstevel@tonic-gate  * Convert prgregset32 to native prgregset
122*7c478bd9Sstevel@tonic-gate  */
123*7c478bd9Sstevel@tonic-gate void
124*7c478bd9Sstevel@tonic-gate prgregset_32ton(klwp_t *lwp, prgregset32_t src, prgregset_t dst)
125*7c478bd9Sstevel@tonic-gate {
126*7c478bd9Sstevel@tonic-gate 	struct regs *rp = lwptoregs(lwp);
127*7c478bd9Sstevel@tonic-gate 
128*7c478bd9Sstevel@tonic-gate 	dst[REG_GSBASE] = lwp->lwp_pcb.pcb_gsbase;
129*7c478bd9Sstevel@tonic-gate 	dst[REG_FSBASE] = lwp->lwp_pcb.pcb_fsbase;
130*7c478bd9Sstevel@tonic-gate 
131*7c478bd9Sstevel@tonic-gate 	dst[REG_DS] = (uint16_t)src[DS];
132*7c478bd9Sstevel@tonic-gate 	dst[REG_ES] = (uint16_t)src[ES];
133*7c478bd9Sstevel@tonic-gate 
134*7c478bd9Sstevel@tonic-gate 	dst[REG_GS] = (uint16_t)src[GS];
135*7c478bd9Sstevel@tonic-gate 	dst[REG_FS] = (uint16_t)src[FS];
136*7c478bd9Sstevel@tonic-gate 	dst[REG_SS] = (uint16_t)src[SS];
137*7c478bd9Sstevel@tonic-gate 	dst[REG_RSP] = (uint32_t)src[UESP];
138*7c478bd9Sstevel@tonic-gate 	dst[REG_RFL] =
139*7c478bd9Sstevel@tonic-gate 		(rp->r_ps & ~PSL_USERMASK) | (src[EFL] & PSL_USERMASK);
140*7c478bd9Sstevel@tonic-gate 	dst[REG_CS] = (uint16_t)src[CS];
141*7c478bd9Sstevel@tonic-gate 	dst[REG_RIP] = (uint32_t)src[EIP];
142*7c478bd9Sstevel@tonic-gate 	dst[REG_ERR] = (uint32_t)src[ERR];
143*7c478bd9Sstevel@tonic-gate 	dst[REG_TRAPNO] = (uint32_t)src[TRAPNO];
144*7c478bd9Sstevel@tonic-gate 	dst[REG_RAX] = (uint32_t)src[EAX];
145*7c478bd9Sstevel@tonic-gate 	dst[REG_RCX] = (uint32_t)src[ECX];
146*7c478bd9Sstevel@tonic-gate 	dst[REG_RDX] = (uint32_t)src[EDX];
147*7c478bd9Sstevel@tonic-gate 	dst[REG_RBX] = (uint32_t)src[EBX];
148*7c478bd9Sstevel@tonic-gate 	dst[REG_RBP] = (uint32_t)src[EBP];
149*7c478bd9Sstevel@tonic-gate 	dst[REG_RSI] = (uint32_t)src[ESI];
150*7c478bd9Sstevel@tonic-gate 	dst[REG_RDI] = (uint32_t)src[EDI];
151*7c478bd9Sstevel@tonic-gate 	dst[REG_R8] = dst[REG_R9] = dst[REG_R10] = dst[REG_R11] =
152*7c478bd9Sstevel@tonic-gate 	    dst[REG_R12] = dst[REG_R13] = dst[REG_R14] = dst[REG_R15] = 0;
153*7c478bd9Sstevel@tonic-gate }
154*7c478bd9Sstevel@tonic-gate 
155*7c478bd9Sstevel@tonic-gate /*
156*7c478bd9Sstevel@tonic-gate  * Return 32-bit general registers
157*7c478bd9Sstevel@tonic-gate  */
158*7c478bd9Sstevel@tonic-gate void
159*7c478bd9Sstevel@tonic-gate prgetprregs32(klwp_t *lwp, prgregset32_t prp)
160*7c478bd9Sstevel@tonic-gate {
161*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&lwptoproc(lwp)->p_lock));
162*7c478bd9Sstevel@tonic-gate 	getgregs32(lwp, prp);
163*7c478bd9Sstevel@tonic-gate }
164*7c478bd9Sstevel@tonic-gate 
165*7c478bd9Sstevel@tonic-gate #endif	/* _SYSCALL32_IMPL */
166*7c478bd9Sstevel@tonic-gate 
167*7c478bd9Sstevel@tonic-gate /*
168*7c478bd9Sstevel@tonic-gate  * Get the syscall return values for the lwp.
169*7c478bd9Sstevel@tonic-gate  */
170*7c478bd9Sstevel@tonic-gate int
171*7c478bd9Sstevel@tonic-gate prgetrvals(klwp_t *lwp, long *rval1, long *rval2)
172*7c478bd9Sstevel@tonic-gate {
173*7c478bd9Sstevel@tonic-gate 	struct regs *r = lwptoregs(lwp);
174*7c478bd9Sstevel@tonic-gate 
175*7c478bd9Sstevel@tonic-gate 	if (r->r_ps & PS_C)
176*7c478bd9Sstevel@tonic-gate 		return (r->r_r0);
177*7c478bd9Sstevel@tonic-gate 	if (lwp->lwp_eosys == JUSTRETURN) {
178*7c478bd9Sstevel@tonic-gate 		*rval1 = 0;
179*7c478bd9Sstevel@tonic-gate 		*rval2 = 0;
180*7c478bd9Sstevel@tonic-gate 	} else if (lwp_getdatamodel(lwp) != DATAMODEL_NATIVE) {
181*7c478bd9Sstevel@tonic-gate 		/*
182*7c478bd9Sstevel@tonic-gate 		 * XX64	Not sure we -really- need to do this, because the
183*7c478bd9Sstevel@tonic-gate 		 *	syscall return already masks off the bottom values ..?
184*7c478bd9Sstevel@tonic-gate 		 */
185*7c478bd9Sstevel@tonic-gate 		*rval1 = r->r_r0 & (uint32_t)0xffffffffu;
186*7c478bd9Sstevel@tonic-gate 		*rval2 = r->r_r1 & (uint32_t)0xffffffffu;
187*7c478bd9Sstevel@tonic-gate 	} else {
188*7c478bd9Sstevel@tonic-gate 		*rval1 = r->r_r0;
189*7c478bd9Sstevel@tonic-gate 		*rval2 = r->r_r1;
190*7c478bd9Sstevel@tonic-gate 	}
191*7c478bd9Sstevel@tonic-gate 	return (0);
192*7c478bd9Sstevel@tonic-gate }
193*7c478bd9Sstevel@tonic-gate 
194*7c478bd9Sstevel@tonic-gate /*
195*7c478bd9Sstevel@tonic-gate  * Does the system support floating-point, either through hardware
196*7c478bd9Sstevel@tonic-gate  * or by trapping and emulating floating-point machine instructions?
197*7c478bd9Sstevel@tonic-gate  */
198*7c478bd9Sstevel@tonic-gate int
199*7c478bd9Sstevel@tonic-gate prhasfp(void)
200*7c478bd9Sstevel@tonic-gate {
201*7c478bd9Sstevel@tonic-gate 	extern int fp_kind;
202*7c478bd9Sstevel@tonic-gate 
203*7c478bd9Sstevel@tonic-gate 	return (fp_kind != FP_NO);
204*7c478bd9Sstevel@tonic-gate }
205*7c478bd9Sstevel@tonic-gate 
206*7c478bd9Sstevel@tonic-gate /*
207*7c478bd9Sstevel@tonic-gate  * Get floating-point registers.
208*7c478bd9Sstevel@tonic-gate  */
209*7c478bd9Sstevel@tonic-gate void
210*7c478bd9Sstevel@tonic-gate prgetprfpregs(klwp_t *lwp, prfpregset_t *pfp)
211*7c478bd9Sstevel@tonic-gate {
212*7c478bd9Sstevel@tonic-gate 	bzero(pfp, sizeof (prfpregset_t));
213*7c478bd9Sstevel@tonic-gate 	getfpregs(lwp, pfp);
214*7c478bd9Sstevel@tonic-gate }
215*7c478bd9Sstevel@tonic-gate 
216*7c478bd9Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL)
217*7c478bd9Sstevel@tonic-gate void
218*7c478bd9Sstevel@tonic-gate prgetprfpregs32(klwp_t *lwp, prfpregset32_t *pfp)
219*7c478bd9Sstevel@tonic-gate {
220*7c478bd9Sstevel@tonic-gate 	bzero(pfp, sizeof (*pfp));
221*7c478bd9Sstevel@tonic-gate 	getfpregs32(lwp, pfp);
222*7c478bd9Sstevel@tonic-gate }
223*7c478bd9Sstevel@tonic-gate #endif	/* _SYSCALL32_IMPL */
224*7c478bd9Sstevel@tonic-gate 
225*7c478bd9Sstevel@tonic-gate /*
226*7c478bd9Sstevel@tonic-gate  * Set floating-point registers.
227*7c478bd9Sstevel@tonic-gate  * (Note: This can be an alias to setfpregs().)
228*7c478bd9Sstevel@tonic-gate  */
229*7c478bd9Sstevel@tonic-gate void
230*7c478bd9Sstevel@tonic-gate prsetprfpregs(klwp_t *lwp, prfpregset_t *pfp)
231*7c478bd9Sstevel@tonic-gate {
232*7c478bd9Sstevel@tonic-gate 	setfpregs(lwp, pfp);
233*7c478bd9Sstevel@tonic-gate }
234*7c478bd9Sstevel@tonic-gate 
235*7c478bd9Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL)
236*7c478bd9Sstevel@tonic-gate void
237*7c478bd9Sstevel@tonic-gate prsetprfpregs32(klwp_t *lwp, prfpregset32_t *pfp)
238*7c478bd9Sstevel@tonic-gate {
239*7c478bd9Sstevel@tonic-gate 	setfpregs32(lwp, pfp);
240*7c478bd9Sstevel@tonic-gate }
241*7c478bd9Sstevel@tonic-gate #endif	/* _SYSCALL32_IMPL */
242*7c478bd9Sstevel@tonic-gate 
243*7c478bd9Sstevel@tonic-gate /*
244*7c478bd9Sstevel@tonic-gate  * Does the system support extra register state?
245*7c478bd9Sstevel@tonic-gate  */
246*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
247*7c478bd9Sstevel@tonic-gate int
248*7c478bd9Sstevel@tonic-gate prhasx(proc_t *p)
249*7c478bd9Sstevel@tonic-gate {
250*7c478bd9Sstevel@tonic-gate 	return (0);
251*7c478bd9Sstevel@tonic-gate }
252*7c478bd9Sstevel@tonic-gate 
253*7c478bd9Sstevel@tonic-gate /*
254*7c478bd9Sstevel@tonic-gate  * Get the size of the extra registers.
255*7c478bd9Sstevel@tonic-gate  */
256*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
257*7c478bd9Sstevel@tonic-gate int
258*7c478bd9Sstevel@tonic-gate prgetprxregsize(proc_t *p)
259*7c478bd9Sstevel@tonic-gate {
260*7c478bd9Sstevel@tonic-gate 	return (0);
261*7c478bd9Sstevel@tonic-gate }
262*7c478bd9Sstevel@tonic-gate 
263*7c478bd9Sstevel@tonic-gate /*
264*7c478bd9Sstevel@tonic-gate  * Get extra registers.
265*7c478bd9Sstevel@tonic-gate  */
266*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
267*7c478bd9Sstevel@tonic-gate void
268*7c478bd9Sstevel@tonic-gate prgetprxregs(klwp_t *lwp, caddr_t prx)
269*7c478bd9Sstevel@tonic-gate {
270*7c478bd9Sstevel@tonic-gate 	/* no extra registers */
271*7c478bd9Sstevel@tonic-gate }
272*7c478bd9Sstevel@tonic-gate 
273*7c478bd9Sstevel@tonic-gate /*
274*7c478bd9Sstevel@tonic-gate  * Set extra registers.
275*7c478bd9Sstevel@tonic-gate  */
276*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
277*7c478bd9Sstevel@tonic-gate void
278*7c478bd9Sstevel@tonic-gate prsetprxregs(klwp_t *lwp, caddr_t prx)
279*7c478bd9Sstevel@tonic-gate {
280*7c478bd9Sstevel@tonic-gate 	/* no extra registers */
281*7c478bd9Sstevel@tonic-gate }
282*7c478bd9Sstevel@tonic-gate 
283*7c478bd9Sstevel@tonic-gate /*
284*7c478bd9Sstevel@tonic-gate  * Return the base (lower limit) of the process stack.
285*7c478bd9Sstevel@tonic-gate  */
286*7c478bd9Sstevel@tonic-gate caddr_t
287*7c478bd9Sstevel@tonic-gate prgetstackbase(proc_t *p)
288*7c478bd9Sstevel@tonic-gate {
289*7c478bd9Sstevel@tonic-gate 	return (p->p_usrstack - p->p_stksize);
290*7c478bd9Sstevel@tonic-gate }
291*7c478bd9Sstevel@tonic-gate 
292*7c478bd9Sstevel@tonic-gate /*
293*7c478bd9Sstevel@tonic-gate  * Return the "addr" field for pr_addr in prpsinfo_t.
294*7c478bd9Sstevel@tonic-gate  * This is a vestige of the past, so whatever we return is OK.
295*7c478bd9Sstevel@tonic-gate  */
296*7c478bd9Sstevel@tonic-gate caddr_t
297*7c478bd9Sstevel@tonic-gate prgetpsaddr(proc_t *p)
298*7c478bd9Sstevel@tonic-gate {
299*7c478bd9Sstevel@tonic-gate 	return ((caddr_t)p);
300*7c478bd9Sstevel@tonic-gate }
301*7c478bd9Sstevel@tonic-gate 
302*7c478bd9Sstevel@tonic-gate /*
303*7c478bd9Sstevel@tonic-gate  * Arrange to single-step the lwp.
304*7c478bd9Sstevel@tonic-gate  */
305*7c478bd9Sstevel@tonic-gate void
306*7c478bd9Sstevel@tonic-gate prstep(klwp_t *lwp, int watchstep)
307*7c478bd9Sstevel@tonic-gate {
308*7c478bd9Sstevel@tonic-gate 	struct regs *r = lwptoregs(lwp);
309*7c478bd9Sstevel@tonic-gate 
310*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&lwptoproc(lwp)->p_lock));
311*7c478bd9Sstevel@tonic-gate 
312*7c478bd9Sstevel@tonic-gate 	if (watchstep)
313*7c478bd9Sstevel@tonic-gate 		lwp->lwp_pcb.pcb_flags |= WATCH_STEP;
314*7c478bd9Sstevel@tonic-gate 	else
315*7c478bd9Sstevel@tonic-gate 		lwp->lwp_pcb.pcb_flags |= NORMAL_STEP;
316*7c478bd9Sstevel@tonic-gate 
317*7c478bd9Sstevel@tonic-gate 	r->r_ps |= PS_T;	/* set the trace flag in PSW */
318*7c478bd9Sstevel@tonic-gate }
319*7c478bd9Sstevel@tonic-gate 
320*7c478bd9Sstevel@tonic-gate /*
321*7c478bd9Sstevel@tonic-gate  * Undo prstep().
322*7c478bd9Sstevel@tonic-gate  */
323*7c478bd9Sstevel@tonic-gate void
324*7c478bd9Sstevel@tonic-gate prnostep(klwp_t *lwp)
325*7c478bd9Sstevel@tonic-gate {
326*7c478bd9Sstevel@tonic-gate 	struct regs *r = lwptoregs(lwp);
327*7c478bd9Sstevel@tonic-gate 
328*7c478bd9Sstevel@tonic-gate 	ASSERT(ttolwp(curthread) == lwp ||
329*7c478bd9Sstevel@tonic-gate 	    MUTEX_NOT_HELD(&lwptoproc(lwp)->p_lock));
330*7c478bd9Sstevel@tonic-gate 
331*7c478bd9Sstevel@tonic-gate 	r->r_ps &= ~PS_T;	/* turn off trace flag in PSW */
332*7c478bd9Sstevel@tonic-gate 	lwp->lwp_pcb.pcb_flags &= ~(NORMAL_STEP|WATCH_STEP|DEBUG_PENDING);
333*7c478bd9Sstevel@tonic-gate }
334*7c478bd9Sstevel@tonic-gate 
335*7c478bd9Sstevel@tonic-gate /*
336*7c478bd9Sstevel@tonic-gate  * Return non-zero if a single-step is in effect.
337*7c478bd9Sstevel@tonic-gate  */
338*7c478bd9Sstevel@tonic-gate int
339*7c478bd9Sstevel@tonic-gate prisstep(klwp_t *lwp)
340*7c478bd9Sstevel@tonic-gate {
341*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&lwptoproc(lwp)->p_lock));
342*7c478bd9Sstevel@tonic-gate 
343*7c478bd9Sstevel@tonic-gate 	return ((lwp->lwp_pcb.pcb_flags &
344*7c478bd9Sstevel@tonic-gate 		(NORMAL_STEP|WATCH_STEP|DEBUG_PENDING)) != 0);
345*7c478bd9Sstevel@tonic-gate }
346*7c478bd9Sstevel@tonic-gate 
347*7c478bd9Sstevel@tonic-gate /*
348*7c478bd9Sstevel@tonic-gate  * Set the PC to the specified virtual address.
349*7c478bd9Sstevel@tonic-gate  */
350*7c478bd9Sstevel@tonic-gate void
351*7c478bd9Sstevel@tonic-gate prsvaddr(klwp_t *lwp, caddr_t vaddr)
352*7c478bd9Sstevel@tonic-gate {
353*7c478bd9Sstevel@tonic-gate 	struct regs *r = lwptoregs(lwp);
354*7c478bd9Sstevel@tonic-gate 
355*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&lwptoproc(lwp)->p_lock));
356*7c478bd9Sstevel@tonic-gate 
357*7c478bd9Sstevel@tonic-gate 	r->r_pc = (uintptr_t)vaddr;
358*7c478bd9Sstevel@tonic-gate }
359*7c478bd9Sstevel@tonic-gate 
360*7c478bd9Sstevel@tonic-gate /*
361*7c478bd9Sstevel@tonic-gate  * Map address "addr" in address space "as" into a kernel virtual address.
362*7c478bd9Sstevel@tonic-gate  * The memory is guaranteed to be resident and locked down.
363*7c478bd9Sstevel@tonic-gate  */
364*7c478bd9Sstevel@tonic-gate caddr_t
365*7c478bd9Sstevel@tonic-gate prmapin(struct as *as, caddr_t addr, int writing)
366*7c478bd9Sstevel@tonic-gate {
367*7c478bd9Sstevel@tonic-gate 	page_t *pp;
368*7c478bd9Sstevel@tonic-gate 	caddr_t kaddr;
369*7c478bd9Sstevel@tonic-gate 	pfn_t pfnum;
370*7c478bd9Sstevel@tonic-gate 
371*7c478bd9Sstevel@tonic-gate 	/*
372*7c478bd9Sstevel@tonic-gate 	 * XXX - Because of past mistakes, we have bits being returned
373*7c478bd9Sstevel@tonic-gate 	 * by getpfnum that are actually the page type bits of the pte.
374*7c478bd9Sstevel@tonic-gate 	 * When the object we are trying to map is a memory page with
375*7c478bd9Sstevel@tonic-gate 	 * a page structure everything is ok and we can use the optimal
376*7c478bd9Sstevel@tonic-gate 	 * method, ppmapin.  Otherwise, we have to do something special.
377*7c478bd9Sstevel@tonic-gate 	 */
378*7c478bd9Sstevel@tonic-gate 	pfnum = hat_getpfnum(as->a_hat, addr);
379*7c478bd9Sstevel@tonic-gate 	if (pf_is_memory(pfnum)) {
380*7c478bd9Sstevel@tonic-gate 		pp = page_numtopp_nolock(pfnum);
381*7c478bd9Sstevel@tonic-gate 		if (pp != NULL) {
382*7c478bd9Sstevel@tonic-gate 			ASSERT(PAGE_LOCKED(pp));
383*7c478bd9Sstevel@tonic-gate 			kaddr = ppmapin(pp, writing ?
384*7c478bd9Sstevel@tonic-gate 			    (PROT_READ | PROT_WRITE) : PROT_READ, (caddr_t)-1);
385*7c478bd9Sstevel@tonic-gate 			return (kaddr + ((uintptr_t)addr & PAGEOFFSET));
386*7c478bd9Sstevel@tonic-gate 		}
387*7c478bd9Sstevel@tonic-gate 	}
388*7c478bd9Sstevel@tonic-gate 
389*7c478bd9Sstevel@tonic-gate 	/*
390*7c478bd9Sstevel@tonic-gate 	 * Oh well, we didn't have a page struct for the object we were
391*7c478bd9Sstevel@tonic-gate 	 * trying to map in; ppmapin doesn't handle devices, but allocating a
392*7c478bd9Sstevel@tonic-gate 	 * heap address allows ppmapout to free virtual space when done.
393*7c478bd9Sstevel@tonic-gate 	 */
394*7c478bd9Sstevel@tonic-gate 	kaddr = vmem_alloc(heap_arena, PAGESIZE, VM_SLEEP);
395*7c478bd9Sstevel@tonic-gate 
396*7c478bd9Sstevel@tonic-gate 	hat_devload(kas.a_hat, kaddr, MMU_PAGESIZE,  pfnum,
397*7c478bd9Sstevel@tonic-gate 	    writing ? (PROT_READ | PROT_WRITE) : PROT_READ, 0);
398*7c478bd9Sstevel@tonic-gate 
399*7c478bd9Sstevel@tonic-gate 	return (kaddr + ((uintptr_t)addr & PAGEOFFSET));
400*7c478bd9Sstevel@tonic-gate }
401*7c478bd9Sstevel@tonic-gate 
402*7c478bd9Sstevel@tonic-gate /*
403*7c478bd9Sstevel@tonic-gate  * Unmap address "addr" in address space "as"; inverse of prmapin().
404*7c478bd9Sstevel@tonic-gate  */
405*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
406*7c478bd9Sstevel@tonic-gate void
407*7c478bd9Sstevel@tonic-gate prmapout(struct as *as, caddr_t addr, caddr_t vaddr, int writing)
408*7c478bd9Sstevel@tonic-gate {
409*7c478bd9Sstevel@tonic-gate 	extern void ppmapout(caddr_t);
410*7c478bd9Sstevel@tonic-gate 
411*7c478bd9Sstevel@tonic-gate 	vaddr = (caddr_t)((uintptr_t)vaddr & PAGEMASK);
412*7c478bd9Sstevel@tonic-gate 	ppmapout(vaddr);
413*7c478bd9Sstevel@tonic-gate }
414*7c478bd9Sstevel@tonic-gate 
415*7c478bd9Sstevel@tonic-gate /*
416*7c478bd9Sstevel@tonic-gate  * Make sure the lwp is in an orderly state
417*7c478bd9Sstevel@tonic-gate  * for inspection by a debugger through /proc.
418*7c478bd9Sstevel@tonic-gate  * Called from stop() and from syslwp_create().
419*7c478bd9Sstevel@tonic-gate  */
420*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
421*7c478bd9Sstevel@tonic-gate void
422*7c478bd9Sstevel@tonic-gate prstop(int why, int what)
423*7c478bd9Sstevel@tonic-gate {
424*7c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
425*7c478bd9Sstevel@tonic-gate 	struct regs *r = lwptoregs(lwp);
426*7c478bd9Sstevel@tonic-gate 
427*7c478bd9Sstevel@tonic-gate 	/*
428*7c478bd9Sstevel@tonic-gate 	 * Make sure we don't deadlock on a recursive call
429*7c478bd9Sstevel@tonic-gate 	 * to prstop().  stop() tests the lwp_nostop flag.
430*7c478bd9Sstevel@tonic-gate 	 */
431*7c478bd9Sstevel@tonic-gate 	ASSERT(lwp->lwp_nostop == 0);
432*7c478bd9Sstevel@tonic-gate 	lwp->lwp_nostop = 1;
433*7c478bd9Sstevel@tonic-gate 
434*7c478bd9Sstevel@tonic-gate 	if (copyin_nowatch((caddr_t)r->r_pc, &lwp->lwp_pcb.pcb_instr,
435*7c478bd9Sstevel@tonic-gate 		    sizeof (lwp->lwp_pcb.pcb_instr)) == 0)
436*7c478bd9Sstevel@tonic-gate 		lwp->lwp_pcb.pcb_flags |= INSTR_VALID;
437*7c478bd9Sstevel@tonic-gate 	else {
438*7c478bd9Sstevel@tonic-gate 		lwp->lwp_pcb.pcb_flags &= ~INSTR_VALID;
439*7c478bd9Sstevel@tonic-gate 		lwp->lwp_pcb.pcb_instr = 0;
440*7c478bd9Sstevel@tonic-gate 	}
441*7c478bd9Sstevel@tonic-gate 
442*7c478bd9Sstevel@tonic-gate 	(void) save_syscall_args();
443*7c478bd9Sstevel@tonic-gate 	ASSERT(lwp->lwp_nostop == 1);
444*7c478bd9Sstevel@tonic-gate 	lwp->lwp_nostop = 0;
445*7c478bd9Sstevel@tonic-gate }
446*7c478bd9Sstevel@tonic-gate 
447*7c478bd9Sstevel@tonic-gate /*
448*7c478bd9Sstevel@tonic-gate  * Fetch the user-level instruction on which the lwp is stopped.
449*7c478bd9Sstevel@tonic-gate  * It was saved by the lwp itself, in prstop().
450*7c478bd9Sstevel@tonic-gate  * Return non-zero if the instruction is valid.
451*7c478bd9Sstevel@tonic-gate  */
452*7c478bd9Sstevel@tonic-gate int
453*7c478bd9Sstevel@tonic-gate prfetchinstr(klwp_t *lwp, ulong_t *ip)
454*7c478bd9Sstevel@tonic-gate {
455*7c478bd9Sstevel@tonic-gate 	*ip = (ulong_t)(instr_t)lwp->lwp_pcb.pcb_instr;
456*7c478bd9Sstevel@tonic-gate 	return (lwp->lwp_pcb.pcb_flags & INSTR_VALID);
457*7c478bd9Sstevel@tonic-gate }
458*7c478bd9Sstevel@tonic-gate 
459*7c478bd9Sstevel@tonic-gate /*
460*7c478bd9Sstevel@tonic-gate  * Called from trap() when a load or store instruction
461*7c478bd9Sstevel@tonic-gate  * falls in a watched page but is not a watchpoint.
462*7c478bd9Sstevel@tonic-gate  * We emulate the instruction in the kernel.
463*7c478bd9Sstevel@tonic-gate  */
464*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
465*7c478bd9Sstevel@tonic-gate int
466*7c478bd9Sstevel@tonic-gate pr_watch_emul(struct regs *rp, caddr_t addr, enum seg_rw rw)
467*7c478bd9Sstevel@tonic-gate {
468*7c478bd9Sstevel@tonic-gate #ifdef SOMEDAY
469*7c478bd9Sstevel@tonic-gate 	int res;
470*7c478bd9Sstevel@tonic-gate 	proc_t *p = curproc;
471*7c478bd9Sstevel@tonic-gate 	char *badaddr = (caddr_t)(-1);
472*7c478bd9Sstevel@tonic-gate 	int mapped;
473*7c478bd9Sstevel@tonic-gate 
474*7c478bd9Sstevel@tonic-gate 	/* prevent recursive calls to pr_watch_emul() */
475*7c478bd9Sstevel@tonic-gate 	ASSERT(!(curthread->t_flag & T_WATCHPT));
476*7c478bd9Sstevel@tonic-gate 	curthread->t_flag |= T_WATCHPT;
477*7c478bd9Sstevel@tonic-gate 
478*7c478bd9Sstevel@tonic-gate 	watch_disable_addr(addr, 8, rw);
479*7c478bd9Sstevel@tonic-gate 	res = do_unaligned(rp, &badaddr);
480*7c478bd9Sstevel@tonic-gate 	watch_enable_addr(addr, 8, rw);
481*7c478bd9Sstevel@tonic-gate 
482*7c478bd9Sstevel@tonic-gate 	curthread->t_flag &= ~T_WATCHPT;
483*7c478bd9Sstevel@tonic-gate 	if (res == SIMU_SUCCESS) {
484*7c478bd9Sstevel@tonic-gate 		/* adjust the pc */
485*7c478bd9Sstevel@tonic-gate 		return (1);
486*7c478bd9Sstevel@tonic-gate 	}
487*7c478bd9Sstevel@tonic-gate #endif
488*7c478bd9Sstevel@tonic-gate 	return (0);
489*7c478bd9Sstevel@tonic-gate }
490*7c478bd9Sstevel@tonic-gate 
491*7c478bd9Sstevel@tonic-gate /*
492*7c478bd9Sstevel@tonic-gate  * Return the number of active entries in the local descriptor table.
493*7c478bd9Sstevel@tonic-gate  */
494*7c478bd9Sstevel@tonic-gate int
495*7c478bd9Sstevel@tonic-gate prnldt(proc_t *p)
496*7c478bd9Sstevel@tonic-gate {
497*7c478bd9Sstevel@tonic-gate 	int limit, i, n;
498*7c478bd9Sstevel@tonic-gate 	user_desc_t *udp;
499*7c478bd9Sstevel@tonic-gate 
500*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_ldtlock));
501*7c478bd9Sstevel@tonic-gate 
502*7c478bd9Sstevel@tonic-gate 	/*
503*7c478bd9Sstevel@tonic-gate 	 * Currently 64 bit processes cannot have a private ldt.
504*7c478bd9Sstevel@tonic-gate 	 */
505*7c478bd9Sstevel@tonic-gate 	ASSERT(get_udatamodel() != DATAMODEL_LP64 || p->p_ldt == NULL);
506*7c478bd9Sstevel@tonic-gate 
507*7c478bd9Sstevel@tonic-gate 
508*7c478bd9Sstevel@tonic-gate 	if (p->p_ldt == NULL)
509*7c478bd9Sstevel@tonic-gate 		return (0);
510*7c478bd9Sstevel@tonic-gate 	n = 0;
511*7c478bd9Sstevel@tonic-gate 	limit = p->p_ldtlimit;
512*7c478bd9Sstevel@tonic-gate 	ASSERT(limit >= 0 && limit < MAXNLDT);
513*7c478bd9Sstevel@tonic-gate 
514*7c478bd9Sstevel@tonic-gate 	/*
515*7c478bd9Sstevel@tonic-gate 	 * Count all present user descriptors.
516*7c478bd9Sstevel@tonic-gate 	 */
517*7c478bd9Sstevel@tonic-gate 	for (i = LDT_UDBASE, udp = &p->p_ldt[i]; i <= limit; i++, udp++)
518*7c478bd9Sstevel@tonic-gate 		if (udp->usd_type != 0 || udp->usd_dpl != 0 || udp->usd_p != 0)
519*7c478bd9Sstevel@tonic-gate 			n++;
520*7c478bd9Sstevel@tonic-gate 	return (n);
521*7c478bd9Sstevel@tonic-gate }
522*7c478bd9Sstevel@tonic-gate 
523*7c478bd9Sstevel@tonic-gate /*
524*7c478bd9Sstevel@tonic-gate  * Fetch the active entries from the local descriptor table.
525*7c478bd9Sstevel@tonic-gate  */
526*7c478bd9Sstevel@tonic-gate void
527*7c478bd9Sstevel@tonic-gate prgetldt(proc_t *p, struct ssd *ssd)
528*7c478bd9Sstevel@tonic-gate {
529*7c478bd9Sstevel@tonic-gate 	int i, limit;
530*7c478bd9Sstevel@tonic-gate 	user_desc_t *udp;
531*7c478bd9Sstevel@tonic-gate 
532*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_ldtlock));
533*7c478bd9Sstevel@tonic-gate 
534*7c478bd9Sstevel@tonic-gate 	if (p->p_ldt == NULL)
535*7c478bd9Sstevel@tonic-gate 		return;
536*7c478bd9Sstevel@tonic-gate 
537*7c478bd9Sstevel@tonic-gate 	limit = p->p_ldtlimit;
538*7c478bd9Sstevel@tonic-gate 	ASSERT(limit >= 0 && limit < MAXNLDT);
539*7c478bd9Sstevel@tonic-gate 
540*7c478bd9Sstevel@tonic-gate 	/*
541*7c478bd9Sstevel@tonic-gate 	 * All present user descriptors.
542*7c478bd9Sstevel@tonic-gate 	 */
543*7c478bd9Sstevel@tonic-gate 	for (i = LDT_UDBASE, udp = &p->p_ldt[i]; i <= limit; i++, udp++)
544*7c478bd9Sstevel@tonic-gate 		if (udp->usd_type != 0 || udp->usd_dpl != 0 ||
545*7c478bd9Sstevel@tonic-gate 		    udp->usd_p != 0)
546*7c478bd9Sstevel@tonic-gate 			usd_to_ssd(udp, ssd++, SEL_LDT(i));
547*7c478bd9Sstevel@tonic-gate }
548