xref: /onnv-gate/usr/src/uts/intel/fs/proc/prmachdep.c (revision 10230:ea2823eb6ac6)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
53446Smrj  * Common Development and Distribution License (the "License").
63446Smrj  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*10230SRoger.Faulkner@Sun.COM 
220Sstevel@tonic-gate /*
23*10230SRoger.Faulkner@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #include <sys/types.h>
310Sstevel@tonic-gate #include <sys/t_lock.h>
320Sstevel@tonic-gate #include <sys/param.h>
330Sstevel@tonic-gate #include <sys/cred.h>
340Sstevel@tonic-gate #include <sys/debug.h>
350Sstevel@tonic-gate #include <sys/inline.h>
360Sstevel@tonic-gate #include <sys/kmem.h>
370Sstevel@tonic-gate #include <sys/proc.h>
380Sstevel@tonic-gate #include <sys/regset.h>
390Sstevel@tonic-gate #include <sys/privregs.h>
400Sstevel@tonic-gate #include <sys/sysmacros.h>
410Sstevel@tonic-gate #include <sys/systm.h>
420Sstevel@tonic-gate #include <sys/vfs.h>
430Sstevel@tonic-gate #include <sys/vnode.h>
440Sstevel@tonic-gate #include <sys/psw.h>
450Sstevel@tonic-gate #include <sys/pcb.h>
460Sstevel@tonic-gate #include <sys/buf.h>
470Sstevel@tonic-gate #include <sys/signal.h>
480Sstevel@tonic-gate #include <sys/user.h>
490Sstevel@tonic-gate #include <sys/cpuvar.h>
500Sstevel@tonic-gate 
510Sstevel@tonic-gate #include <sys/fault.h>
520Sstevel@tonic-gate #include <sys/syscall.h>
530Sstevel@tonic-gate #include <sys/procfs.h>
540Sstevel@tonic-gate #include <sys/cmn_err.h>
550Sstevel@tonic-gate #include <sys/stack.h>
560Sstevel@tonic-gate #include <sys/debugreg.h>
570Sstevel@tonic-gate #include <sys/copyops.h>
580Sstevel@tonic-gate 
590Sstevel@tonic-gate #include <sys/vmem.h>
600Sstevel@tonic-gate #include <sys/mman.h>
610Sstevel@tonic-gate #include <sys/vmparam.h>
620Sstevel@tonic-gate #include <sys/fp.h>
630Sstevel@tonic-gate #include <sys/archsystm.h>
640Sstevel@tonic-gate #include <sys/vmsystm.h>
650Sstevel@tonic-gate #include <vm/hat.h>
660Sstevel@tonic-gate #include <vm/as.h>
670Sstevel@tonic-gate #include <vm/seg.h>
680Sstevel@tonic-gate #include <vm/seg_kmem.h>
690Sstevel@tonic-gate #include <vm/seg_kp.h>
700Sstevel@tonic-gate #include <vm/page.h>
710Sstevel@tonic-gate 
720Sstevel@tonic-gate #include <sys/sysi86.h>
730Sstevel@tonic-gate 
740Sstevel@tonic-gate #include <fs/proc/prdata.h>
750Sstevel@tonic-gate 
760Sstevel@tonic-gate int	prnwatch = 10000;	/* maximum number of watched areas */
770Sstevel@tonic-gate 
780Sstevel@tonic-gate /*
790Sstevel@tonic-gate  * Force a thread into the kernel if it is not already there.
800Sstevel@tonic-gate  * This is a no-op on uniprocessors.
810Sstevel@tonic-gate  */
820Sstevel@tonic-gate /* ARGSUSED */
830Sstevel@tonic-gate void
prpokethread(kthread_t * t)840Sstevel@tonic-gate prpokethread(kthread_t *t)
850Sstevel@tonic-gate {
860Sstevel@tonic-gate 	if (t->t_state == TS_ONPROC && t->t_cpu != CPU)
870Sstevel@tonic-gate 		poke_cpu(t->t_cpu->cpu_id);
880Sstevel@tonic-gate }
890Sstevel@tonic-gate 
900Sstevel@tonic-gate /*
910Sstevel@tonic-gate  * Return general registers.
920Sstevel@tonic-gate  */
930Sstevel@tonic-gate void
prgetprregs(klwp_t * lwp,prgregset_t prp)940Sstevel@tonic-gate prgetprregs(klwp_t *lwp, prgregset_t prp)
950Sstevel@tonic-gate {
960Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&lwptoproc(lwp)->p_lock));
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	getgregs(lwp, prp);
990Sstevel@tonic-gate }
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate /*
1020Sstevel@tonic-gate  * Set general registers.
1030Sstevel@tonic-gate  * (Note: This can be an alias to setgregs().)
1040Sstevel@tonic-gate  */
1050Sstevel@tonic-gate void
prsetprregs(klwp_t * lwp,prgregset_t prp,int initial)1060Sstevel@tonic-gate prsetprregs(klwp_t *lwp, prgregset_t prp, int initial)
1070Sstevel@tonic-gate {
1080Sstevel@tonic-gate 	if (initial)		/* set initial values */
1090Sstevel@tonic-gate 		lwptoregs(lwp)->r_ps = PSL_USER;
1100Sstevel@tonic-gate 	(void) setgregs(lwp, prp);
1110Sstevel@tonic-gate }
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate /*
1160Sstevel@tonic-gate  * Convert prgregset32 to native prgregset
1170Sstevel@tonic-gate  */
1180Sstevel@tonic-gate void
prgregset_32ton(klwp_t * lwp,prgregset32_t src,prgregset_t dst)1190Sstevel@tonic-gate prgregset_32ton(klwp_t *lwp, prgregset32_t src, prgregset_t dst)
1200Sstevel@tonic-gate {
1210Sstevel@tonic-gate 	struct regs *rp = lwptoregs(lwp);
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 	dst[REG_GSBASE] = lwp->lwp_pcb.pcb_gsbase;
1240Sstevel@tonic-gate 	dst[REG_FSBASE] = lwp->lwp_pcb.pcb_fsbase;
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate 	dst[REG_DS] = (uint16_t)src[DS];
1270Sstevel@tonic-gate 	dst[REG_ES] = (uint16_t)src[ES];
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate 	dst[REG_GS] = (uint16_t)src[GS];
1300Sstevel@tonic-gate 	dst[REG_FS] = (uint16_t)src[FS];
1310Sstevel@tonic-gate 	dst[REG_SS] = (uint16_t)src[SS];
1320Sstevel@tonic-gate 	dst[REG_RSP] = (uint32_t)src[UESP];
1330Sstevel@tonic-gate 	dst[REG_RFL] =
134*10230SRoger.Faulkner@Sun.COM 	    (rp->r_ps & ~PSL_USERMASK) | (src[EFL] & PSL_USERMASK);
1350Sstevel@tonic-gate 	dst[REG_CS] = (uint16_t)src[CS];
1360Sstevel@tonic-gate 	dst[REG_RIP] = (uint32_t)src[EIP];
1370Sstevel@tonic-gate 	dst[REG_ERR] = (uint32_t)src[ERR];
1380Sstevel@tonic-gate 	dst[REG_TRAPNO] = (uint32_t)src[TRAPNO];
1390Sstevel@tonic-gate 	dst[REG_RAX] = (uint32_t)src[EAX];
1400Sstevel@tonic-gate 	dst[REG_RCX] = (uint32_t)src[ECX];
1410Sstevel@tonic-gate 	dst[REG_RDX] = (uint32_t)src[EDX];
1420Sstevel@tonic-gate 	dst[REG_RBX] = (uint32_t)src[EBX];
1430Sstevel@tonic-gate 	dst[REG_RBP] = (uint32_t)src[EBP];
1440Sstevel@tonic-gate 	dst[REG_RSI] = (uint32_t)src[ESI];
1450Sstevel@tonic-gate 	dst[REG_RDI] = (uint32_t)src[EDI];
1460Sstevel@tonic-gate 	dst[REG_R8] = dst[REG_R9] = dst[REG_R10] = dst[REG_R11] =
1470Sstevel@tonic-gate 	    dst[REG_R12] = dst[REG_R13] = dst[REG_R14] = dst[REG_R15] = 0;
1480Sstevel@tonic-gate }
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate /*
1510Sstevel@tonic-gate  * Return 32-bit general registers
1520Sstevel@tonic-gate  */
1530Sstevel@tonic-gate void
prgetprregs32(klwp_t * lwp,prgregset32_t prp)1540Sstevel@tonic-gate prgetprregs32(klwp_t *lwp, prgregset32_t prp)
1550Sstevel@tonic-gate {
1560Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&lwptoproc(lwp)->p_lock));
1570Sstevel@tonic-gate 	getgregs32(lwp, prp);
1580Sstevel@tonic-gate }
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate #endif	/* _SYSCALL32_IMPL */
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate /*
1630Sstevel@tonic-gate  * Get the syscall return values for the lwp.
1640Sstevel@tonic-gate  */
1650Sstevel@tonic-gate int
prgetrvals(klwp_t * lwp,long * rval1,long * rval2)1660Sstevel@tonic-gate prgetrvals(klwp_t *lwp, long *rval1, long *rval2)
1670Sstevel@tonic-gate {
1680Sstevel@tonic-gate 	struct regs *r = lwptoregs(lwp);
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 	if (r->r_ps & PS_C)
1710Sstevel@tonic-gate 		return (r->r_r0);
1720Sstevel@tonic-gate 	if (lwp->lwp_eosys == JUSTRETURN) {
1730Sstevel@tonic-gate 		*rval1 = 0;
1740Sstevel@tonic-gate 		*rval2 = 0;
1750Sstevel@tonic-gate 	} else if (lwp_getdatamodel(lwp) != DATAMODEL_NATIVE) {
1760Sstevel@tonic-gate 		/*
1770Sstevel@tonic-gate 		 * XX64	Not sure we -really- need to do this, because the
1780Sstevel@tonic-gate 		 *	syscall return already masks off the bottom values ..?
1790Sstevel@tonic-gate 		 */
1800Sstevel@tonic-gate 		*rval1 = r->r_r0 & (uint32_t)0xffffffffu;
1810Sstevel@tonic-gate 		*rval2 = r->r_r1 & (uint32_t)0xffffffffu;
1820Sstevel@tonic-gate 	} else {
1830Sstevel@tonic-gate 		*rval1 = r->r_r0;
1840Sstevel@tonic-gate 		*rval2 = r->r_r1;
1850Sstevel@tonic-gate 	}
1860Sstevel@tonic-gate 	return (0);
1870Sstevel@tonic-gate }
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate /*
1900Sstevel@tonic-gate  * Does the system support floating-point, either through hardware
1910Sstevel@tonic-gate  * or by trapping and emulating floating-point machine instructions?
1920Sstevel@tonic-gate  */
1930Sstevel@tonic-gate int
prhasfp(void)1940Sstevel@tonic-gate prhasfp(void)
1950Sstevel@tonic-gate {
1960Sstevel@tonic-gate 	extern int fp_kind;
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 	return (fp_kind != FP_NO);
1990Sstevel@tonic-gate }
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate /*
2020Sstevel@tonic-gate  * Get floating-point registers.
2030Sstevel@tonic-gate  */
2040Sstevel@tonic-gate void
prgetprfpregs(klwp_t * lwp,prfpregset_t * pfp)2050Sstevel@tonic-gate prgetprfpregs(klwp_t *lwp, prfpregset_t *pfp)
2060Sstevel@tonic-gate {
2070Sstevel@tonic-gate 	bzero(pfp, sizeof (prfpregset_t));
2080Sstevel@tonic-gate 	getfpregs(lwp, pfp);
2090Sstevel@tonic-gate }
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL)
2120Sstevel@tonic-gate void
prgetprfpregs32(klwp_t * lwp,prfpregset32_t * pfp)2130Sstevel@tonic-gate prgetprfpregs32(klwp_t *lwp, prfpregset32_t *pfp)
2140Sstevel@tonic-gate {
2150Sstevel@tonic-gate 	bzero(pfp, sizeof (*pfp));
2160Sstevel@tonic-gate 	getfpregs32(lwp, pfp);
2170Sstevel@tonic-gate }
2180Sstevel@tonic-gate #endif	/* _SYSCALL32_IMPL */
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate /*
2210Sstevel@tonic-gate  * Set floating-point registers.
2220Sstevel@tonic-gate  * (Note: This can be an alias to setfpregs().)
2230Sstevel@tonic-gate  */
2240Sstevel@tonic-gate void
prsetprfpregs(klwp_t * lwp,prfpregset_t * pfp)2250Sstevel@tonic-gate prsetprfpregs(klwp_t *lwp, prfpregset_t *pfp)
2260Sstevel@tonic-gate {
2270Sstevel@tonic-gate 	setfpregs(lwp, pfp);
2280Sstevel@tonic-gate }
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL)
2310Sstevel@tonic-gate void
prsetprfpregs32(klwp_t * lwp,prfpregset32_t * pfp)2320Sstevel@tonic-gate prsetprfpregs32(klwp_t *lwp, prfpregset32_t *pfp)
2330Sstevel@tonic-gate {
2340Sstevel@tonic-gate 	setfpregs32(lwp, pfp);
2350Sstevel@tonic-gate }
2360Sstevel@tonic-gate #endif	/* _SYSCALL32_IMPL */
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate /*
2390Sstevel@tonic-gate  * Does the system support extra register state?
2400Sstevel@tonic-gate  */
2410Sstevel@tonic-gate /* ARGSUSED */
2420Sstevel@tonic-gate int
prhasx(proc_t * p)2430Sstevel@tonic-gate prhasx(proc_t *p)
2440Sstevel@tonic-gate {
2450Sstevel@tonic-gate 	return (0);
2460Sstevel@tonic-gate }
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate /*
2490Sstevel@tonic-gate  * Get the size of the extra registers.
2500Sstevel@tonic-gate  */
2510Sstevel@tonic-gate /* ARGSUSED */
2520Sstevel@tonic-gate int
prgetprxregsize(proc_t * p)2530Sstevel@tonic-gate prgetprxregsize(proc_t *p)
2540Sstevel@tonic-gate {
2550Sstevel@tonic-gate 	return (0);
2560Sstevel@tonic-gate }
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate /*
2590Sstevel@tonic-gate  * Get extra registers.
2600Sstevel@tonic-gate  */
2610Sstevel@tonic-gate /*ARGSUSED*/
2620Sstevel@tonic-gate void
prgetprxregs(klwp_t * lwp,caddr_t prx)2630Sstevel@tonic-gate prgetprxregs(klwp_t *lwp, caddr_t prx)
2640Sstevel@tonic-gate {
2650Sstevel@tonic-gate 	/* no extra registers */
2660Sstevel@tonic-gate }
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate /*
2690Sstevel@tonic-gate  * Set extra registers.
2700Sstevel@tonic-gate  */
2710Sstevel@tonic-gate /*ARGSUSED*/
2720Sstevel@tonic-gate void
prsetprxregs(klwp_t * lwp,caddr_t prx)2730Sstevel@tonic-gate prsetprxregs(klwp_t *lwp, caddr_t prx)
2740Sstevel@tonic-gate {
2750Sstevel@tonic-gate 	/* no extra registers */
2760Sstevel@tonic-gate }
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate /*
2790Sstevel@tonic-gate  * Return the base (lower limit) of the process stack.
2800Sstevel@tonic-gate  */
2810Sstevel@tonic-gate caddr_t
prgetstackbase(proc_t * p)2820Sstevel@tonic-gate prgetstackbase(proc_t *p)
2830Sstevel@tonic-gate {
2840Sstevel@tonic-gate 	return (p->p_usrstack - p->p_stksize);
2850Sstevel@tonic-gate }
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate /*
2880Sstevel@tonic-gate  * Return the "addr" field for pr_addr in prpsinfo_t.
2890Sstevel@tonic-gate  * This is a vestige of the past, so whatever we return is OK.
2900Sstevel@tonic-gate  */
2910Sstevel@tonic-gate caddr_t
prgetpsaddr(proc_t * p)2920Sstevel@tonic-gate prgetpsaddr(proc_t *p)
2930Sstevel@tonic-gate {
2940Sstevel@tonic-gate 	return ((caddr_t)p);
2950Sstevel@tonic-gate }
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate /*
2980Sstevel@tonic-gate  * Arrange to single-step the lwp.
2990Sstevel@tonic-gate  */
3000Sstevel@tonic-gate void
prstep(klwp_t * lwp,int watchstep)3010Sstevel@tonic-gate prstep(klwp_t *lwp, int watchstep)
3020Sstevel@tonic-gate {
3031468Smarx 	ASSERT(MUTEX_NOT_HELD(&lwptoproc(lwp)->p_lock));
3040Sstevel@tonic-gate 
3051468Smarx 	/*
3061468Smarx 	 * flag LWP so that its r_efl trace bit (PS_T) will be set on
3071468Smarx 	 * next return to usermode.
3081468Smarx 	 */
3091468Smarx 	lwp->lwp_pcb.pcb_flags |= REQUEST_STEP;
3101468Smarx 	lwp->lwp_pcb.pcb_flags &= ~REQUEST_NOSTEP;
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 	if (watchstep)
3130Sstevel@tonic-gate 		lwp->lwp_pcb.pcb_flags |= WATCH_STEP;
3140Sstevel@tonic-gate 	else
3150Sstevel@tonic-gate 		lwp->lwp_pcb.pcb_flags |= NORMAL_STEP;
3160Sstevel@tonic-gate 
3171468Smarx 	aston(lwptot(lwp));	/* let trap() set PS_T in rp->r_efl */
3180Sstevel@tonic-gate }
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate /*
3210Sstevel@tonic-gate  * Undo prstep().
3220Sstevel@tonic-gate  */
3230Sstevel@tonic-gate void
prnostep(klwp_t * lwp)3240Sstevel@tonic-gate prnostep(klwp_t *lwp)
3250Sstevel@tonic-gate {
3260Sstevel@tonic-gate 	ASSERT(ttolwp(curthread) == lwp ||
3270Sstevel@tonic-gate 	    MUTEX_NOT_HELD(&lwptoproc(lwp)->p_lock));
3280Sstevel@tonic-gate 
3291468Smarx 	/*
3301468Smarx 	 * flag LWP so that its r_efl trace bit (PS_T) will be cleared on
3311468Smarx 	 * next return to usermode.
3321468Smarx 	 */
3331468Smarx 	lwp->lwp_pcb.pcb_flags |= REQUEST_NOSTEP;
3341468Smarx 
3351468Smarx 	lwp->lwp_pcb.pcb_flags &=
3361468Smarx 	    ~(REQUEST_STEP|NORMAL_STEP|WATCH_STEP|DEBUG_PENDING);
3371468Smarx 
3381468Smarx 	aston(lwptot(lwp));	/* let trap() clear PS_T in rp->r_efl */
3390Sstevel@tonic-gate }
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate /*
3420Sstevel@tonic-gate  * Return non-zero if a single-step is in effect.
3430Sstevel@tonic-gate  */
3440Sstevel@tonic-gate int
prisstep(klwp_t * lwp)3450Sstevel@tonic-gate prisstep(klwp_t *lwp)
3460Sstevel@tonic-gate {
3470Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&lwptoproc(lwp)->p_lock));
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate 	return ((lwp->lwp_pcb.pcb_flags &
350*10230SRoger.Faulkner@Sun.COM 	    (NORMAL_STEP|WATCH_STEP|DEBUG_PENDING)) != 0);
3510Sstevel@tonic-gate }
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate /*
3540Sstevel@tonic-gate  * Set the PC to the specified virtual address.
3550Sstevel@tonic-gate  */
3560Sstevel@tonic-gate void
prsvaddr(klwp_t * lwp,caddr_t vaddr)3570Sstevel@tonic-gate prsvaddr(klwp_t *lwp, caddr_t vaddr)
3580Sstevel@tonic-gate {
3590Sstevel@tonic-gate 	struct regs *r = lwptoregs(lwp);
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&lwptoproc(lwp)->p_lock));
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate 	r->r_pc = (uintptr_t)vaddr;
3640Sstevel@tonic-gate }
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate /*
3670Sstevel@tonic-gate  * Map address "addr" in address space "as" into a kernel virtual address.
3680Sstevel@tonic-gate  * The memory is guaranteed to be resident and locked down.
3690Sstevel@tonic-gate  */
3700Sstevel@tonic-gate caddr_t
prmapin(struct as * as,caddr_t addr,int writing)3710Sstevel@tonic-gate prmapin(struct as *as, caddr_t addr, int writing)
3720Sstevel@tonic-gate {
3730Sstevel@tonic-gate 	page_t *pp;
3740Sstevel@tonic-gate 	caddr_t kaddr;
3750Sstevel@tonic-gate 	pfn_t pfnum;
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate 	/*
3780Sstevel@tonic-gate 	 * XXX - Because of past mistakes, we have bits being returned
3790Sstevel@tonic-gate 	 * by getpfnum that are actually the page type bits of the pte.
3800Sstevel@tonic-gate 	 * When the object we are trying to map is a memory page with
3810Sstevel@tonic-gate 	 * a page structure everything is ok and we can use the optimal
3820Sstevel@tonic-gate 	 * method, ppmapin.  Otherwise, we have to do something special.
3830Sstevel@tonic-gate 	 */
3840Sstevel@tonic-gate 	pfnum = hat_getpfnum(as->a_hat, addr);
3850Sstevel@tonic-gate 	if (pf_is_memory(pfnum)) {
3860Sstevel@tonic-gate 		pp = page_numtopp_nolock(pfnum);
3870Sstevel@tonic-gate 		if (pp != NULL) {
3880Sstevel@tonic-gate 			ASSERT(PAGE_LOCKED(pp));
3890Sstevel@tonic-gate 			kaddr = ppmapin(pp, writing ?
3900Sstevel@tonic-gate 			    (PROT_READ | PROT_WRITE) : PROT_READ, (caddr_t)-1);
3910Sstevel@tonic-gate 			return (kaddr + ((uintptr_t)addr & PAGEOFFSET));
3920Sstevel@tonic-gate 		}
3930Sstevel@tonic-gate 	}
3940Sstevel@tonic-gate 
3950Sstevel@tonic-gate 	/*
3960Sstevel@tonic-gate 	 * Oh well, we didn't have a page struct for the object we were
3970Sstevel@tonic-gate 	 * trying to map in; ppmapin doesn't handle devices, but allocating a
3980Sstevel@tonic-gate 	 * heap address allows ppmapout to free virtual space when done.
3990Sstevel@tonic-gate 	 */
4000Sstevel@tonic-gate 	kaddr = vmem_alloc(heap_arena, PAGESIZE, VM_SLEEP);
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate 	hat_devload(kas.a_hat, kaddr, MMU_PAGESIZE,  pfnum,
4030Sstevel@tonic-gate 	    writing ? (PROT_READ | PROT_WRITE) : PROT_READ, 0);
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate 	return (kaddr + ((uintptr_t)addr & PAGEOFFSET));
4060Sstevel@tonic-gate }
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate /*
4090Sstevel@tonic-gate  * Unmap address "addr" in address space "as"; inverse of prmapin().
4100Sstevel@tonic-gate  */
4110Sstevel@tonic-gate /* ARGSUSED */
4120Sstevel@tonic-gate void
prmapout(struct as * as,caddr_t addr,caddr_t vaddr,int writing)4130Sstevel@tonic-gate prmapout(struct as *as, caddr_t addr, caddr_t vaddr, int writing)
4140Sstevel@tonic-gate {
4150Sstevel@tonic-gate 	extern void ppmapout(caddr_t);
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate 	vaddr = (caddr_t)((uintptr_t)vaddr & PAGEMASK);
4180Sstevel@tonic-gate 	ppmapout(vaddr);
4190Sstevel@tonic-gate }
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate /*
4220Sstevel@tonic-gate  * Make sure the lwp is in an orderly state
4230Sstevel@tonic-gate  * for inspection by a debugger through /proc.
424*10230SRoger.Faulkner@Sun.COM  *
425*10230SRoger.Faulkner@Sun.COM  * This needs to be called only once while the current thread remains in the
426*10230SRoger.Faulkner@Sun.COM  * kernel and needs to be called while holding no resources (mutex locks, etc).
427*10230SRoger.Faulkner@Sun.COM  *
428*10230SRoger.Faulkner@Sun.COM  * As a hedge against these conditions, if prstop() is called repeatedly
429*10230SRoger.Faulkner@Sun.COM  * before prunstop() is called, it does nothing and just returns.
430*10230SRoger.Faulkner@Sun.COM  *
431*10230SRoger.Faulkner@Sun.COM  * prunstop() must be called before the thread returns to user level.
4320Sstevel@tonic-gate  */
4330Sstevel@tonic-gate /* ARGSUSED */
4340Sstevel@tonic-gate void
prstop(int why,int what)4350Sstevel@tonic-gate prstop(int why, int what)
4360Sstevel@tonic-gate {
4370Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
4380Sstevel@tonic-gate 	struct regs *r = lwptoregs(lwp);
4390Sstevel@tonic-gate 
440*10230SRoger.Faulkner@Sun.COM 	if (lwp->lwp_pcb.pcb_flags & PRSTOP_CALLED)
441*10230SRoger.Faulkner@Sun.COM 		return;
442*10230SRoger.Faulkner@Sun.COM 
4430Sstevel@tonic-gate 	/*
4443930Snr123932 	 * Make sure we don't deadlock on a recursive call
4453930Snr123932 	 * to prstop().  stop() tests the lwp_nostop flag.
4460Sstevel@tonic-gate 	 */
4473930Snr123932 	ASSERT(lwp->lwp_nostop == 0);
4483930Snr123932 	lwp->lwp_nostop = 1;
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate 	if (copyin_nowatch((caddr_t)r->r_pc, &lwp->lwp_pcb.pcb_instr,
451*10230SRoger.Faulkner@Sun.COM 	    sizeof (lwp->lwp_pcb.pcb_instr)) == 0)
4520Sstevel@tonic-gate 		lwp->lwp_pcb.pcb_flags |= INSTR_VALID;
4530Sstevel@tonic-gate 	else {
4540Sstevel@tonic-gate 		lwp->lwp_pcb.pcb_flags &= ~INSTR_VALID;
4550Sstevel@tonic-gate 		lwp->lwp_pcb.pcb_instr = 0;
4560Sstevel@tonic-gate 	}
4570Sstevel@tonic-gate 
4580Sstevel@tonic-gate 	(void) save_syscall_args();
4593930Snr123932 	ASSERT(lwp->lwp_nostop == 1);
4603930Snr123932 	lwp->lwp_nostop = 0;
461*10230SRoger.Faulkner@Sun.COM 
462*10230SRoger.Faulkner@Sun.COM 	lwp->lwp_pcb.pcb_flags |= PRSTOP_CALLED;
463*10230SRoger.Faulkner@Sun.COM 	aston(curthread);	/* so prunstop() will be called */
464*10230SRoger.Faulkner@Sun.COM }
465*10230SRoger.Faulkner@Sun.COM 
466*10230SRoger.Faulkner@Sun.COM /*
467*10230SRoger.Faulkner@Sun.COM  * Inform prstop() that it should do its work again
468*10230SRoger.Faulkner@Sun.COM  * the next time it is called.
469*10230SRoger.Faulkner@Sun.COM  */
470*10230SRoger.Faulkner@Sun.COM void
prunstop(void)471*10230SRoger.Faulkner@Sun.COM prunstop(void)
472*10230SRoger.Faulkner@Sun.COM {
473*10230SRoger.Faulkner@Sun.COM 	ttolwp(curthread)->lwp_pcb.pcb_flags &= ~PRSTOP_CALLED;
4740Sstevel@tonic-gate }
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate /*
4770Sstevel@tonic-gate  * Fetch the user-level instruction on which the lwp is stopped.
4780Sstevel@tonic-gate  * It was saved by the lwp itself, in prstop().
4790Sstevel@tonic-gate  * Return non-zero if the instruction is valid.
4800Sstevel@tonic-gate  */
4810Sstevel@tonic-gate int
prfetchinstr(klwp_t * lwp,ulong_t * ip)4820Sstevel@tonic-gate prfetchinstr(klwp_t *lwp, ulong_t *ip)
4830Sstevel@tonic-gate {
4840Sstevel@tonic-gate 	*ip = (ulong_t)(instr_t)lwp->lwp_pcb.pcb_instr;
4850Sstevel@tonic-gate 	return (lwp->lwp_pcb.pcb_flags & INSTR_VALID);
4860Sstevel@tonic-gate }
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate /*
4890Sstevel@tonic-gate  * Called from trap() when a load or store instruction
4900Sstevel@tonic-gate  * falls in a watched page but is not a watchpoint.
4910Sstevel@tonic-gate  * We emulate the instruction in the kernel.
4920Sstevel@tonic-gate  */
4930Sstevel@tonic-gate /* ARGSUSED */
4940Sstevel@tonic-gate int
pr_watch_emul(struct regs * rp,caddr_t addr,enum seg_rw rw)4950Sstevel@tonic-gate pr_watch_emul(struct regs *rp, caddr_t addr, enum seg_rw rw)
4960Sstevel@tonic-gate {
4970Sstevel@tonic-gate #ifdef SOMEDAY
4980Sstevel@tonic-gate 	int res;
4990Sstevel@tonic-gate 	proc_t *p = curproc;
5000Sstevel@tonic-gate 	char *badaddr = (caddr_t)(-1);
5010Sstevel@tonic-gate 	int mapped;
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate 	/* prevent recursive calls to pr_watch_emul() */
5040Sstevel@tonic-gate 	ASSERT(!(curthread->t_flag & T_WATCHPT));
5050Sstevel@tonic-gate 	curthread->t_flag |= T_WATCHPT;
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate 	watch_disable_addr(addr, 8, rw);
5080Sstevel@tonic-gate 	res = do_unaligned(rp, &badaddr);
5090Sstevel@tonic-gate 	watch_enable_addr(addr, 8, rw);
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate 	curthread->t_flag &= ~T_WATCHPT;
5120Sstevel@tonic-gate 	if (res == SIMU_SUCCESS) {
5130Sstevel@tonic-gate 		/* adjust the pc */
5140Sstevel@tonic-gate 		return (1);
5150Sstevel@tonic-gate 	}
5160Sstevel@tonic-gate #endif
5170Sstevel@tonic-gate 	return (0);
5180Sstevel@tonic-gate }
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate /*
5210Sstevel@tonic-gate  * Return the number of active entries in the local descriptor table.
5220Sstevel@tonic-gate  */
5230Sstevel@tonic-gate int
prnldt(proc_t * p)5240Sstevel@tonic-gate prnldt(proc_t *p)
5250Sstevel@tonic-gate {
5260Sstevel@tonic-gate 	int limit, i, n;
5270Sstevel@tonic-gate 	user_desc_t *udp;
5280Sstevel@tonic-gate 
5290Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_ldtlock));
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate 	/*
5323939Ssethg 	 * Currently 64 bit processes cannot have private LDTs.
5330Sstevel@tonic-gate 	 */
5343939Ssethg 	ASSERT(p->p_model != DATAMODEL_LP64 || p->p_ldt == NULL);
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate 	if (p->p_ldt == NULL)
5370Sstevel@tonic-gate 		return (0);
5380Sstevel@tonic-gate 	n = 0;
5390Sstevel@tonic-gate 	limit = p->p_ldtlimit;
5400Sstevel@tonic-gate 	ASSERT(limit >= 0 && limit < MAXNLDT);
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate 	/*
5430Sstevel@tonic-gate 	 * Count all present user descriptors.
5440Sstevel@tonic-gate 	 */
5450Sstevel@tonic-gate 	for (i = LDT_UDBASE, udp = &p->p_ldt[i]; i <= limit; i++, udp++)
5460Sstevel@tonic-gate 		if (udp->usd_type != 0 || udp->usd_dpl != 0 || udp->usd_p != 0)
5470Sstevel@tonic-gate 			n++;
5480Sstevel@tonic-gate 	return (n);
5490Sstevel@tonic-gate }
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate /*
5520Sstevel@tonic-gate  * Fetch the active entries from the local descriptor table.
5530Sstevel@tonic-gate  */
5540Sstevel@tonic-gate void
prgetldt(proc_t * p,struct ssd * ssd)5550Sstevel@tonic-gate prgetldt(proc_t *p, struct ssd *ssd)
5560Sstevel@tonic-gate {
5570Sstevel@tonic-gate 	int i, limit;
5580Sstevel@tonic-gate 	user_desc_t *udp;
5590Sstevel@tonic-gate 
5600Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_ldtlock));
5610Sstevel@tonic-gate 
5620Sstevel@tonic-gate 	if (p->p_ldt == NULL)
5630Sstevel@tonic-gate 		return;
5640Sstevel@tonic-gate 
5650Sstevel@tonic-gate 	limit = p->p_ldtlimit;
5660Sstevel@tonic-gate 	ASSERT(limit >= 0 && limit < MAXNLDT);
5670Sstevel@tonic-gate 
5680Sstevel@tonic-gate 	/*
5690Sstevel@tonic-gate 	 * All present user descriptors.
5700Sstevel@tonic-gate 	 */
5710Sstevel@tonic-gate 	for (i = LDT_UDBASE, udp = &p->p_ldt[i]; i <= limit; i++, udp++)
5720Sstevel@tonic-gate 		if (udp->usd_type != 0 || udp->usd_dpl != 0 ||
5730Sstevel@tonic-gate 		    udp->usd_p != 0)
5740Sstevel@tonic-gate 			usd_to_ssd(udp, ssd++, SEL_LDT(i));
5750Sstevel@tonic-gate }
576