xref: /illumos-gate/usr/src/uts/intel/fs/proc/prmachdep.c (revision 65a89a64c60f3061bbe2381edaacc81660af9a95)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*65a89a64Smarx  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include <sys/types.h>
347c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
357c478bd9Sstevel@tonic-gate #include <sys/param.h>
367c478bd9Sstevel@tonic-gate #include <sys/cred.h>
377c478bd9Sstevel@tonic-gate #include <sys/debug.h>
387c478bd9Sstevel@tonic-gate #include <sys/inline.h>
397c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
407c478bd9Sstevel@tonic-gate #include <sys/proc.h>
417c478bd9Sstevel@tonic-gate #include <sys/regset.h>
427c478bd9Sstevel@tonic-gate #include <sys/privregs.h>
437c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
447c478bd9Sstevel@tonic-gate #include <sys/systm.h>
457c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
467c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
477c478bd9Sstevel@tonic-gate #include <sys/psw.h>
487c478bd9Sstevel@tonic-gate #include <sys/pcb.h>
497c478bd9Sstevel@tonic-gate #include <sys/buf.h>
507c478bd9Sstevel@tonic-gate #include <sys/signal.h>
517c478bd9Sstevel@tonic-gate #include <sys/user.h>
527c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate #include <sys/fault.h>
557c478bd9Sstevel@tonic-gate #include <sys/syscall.h>
567c478bd9Sstevel@tonic-gate #include <sys/procfs.h>
577c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
587c478bd9Sstevel@tonic-gate #include <sys/stack.h>
597c478bd9Sstevel@tonic-gate #include <sys/debugreg.h>
607c478bd9Sstevel@tonic-gate #include <sys/copyops.h>
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate #include <sys/mmu.h>
637c478bd9Sstevel@tonic-gate #include <sys/pte.h>
647c478bd9Sstevel@tonic-gate #include <sys/vmem.h>
657c478bd9Sstevel@tonic-gate #include <sys/mman.h>
667c478bd9Sstevel@tonic-gate #include <sys/vmparam.h>
677c478bd9Sstevel@tonic-gate #include <sys/fp.h>
687c478bd9Sstevel@tonic-gate #include <sys/archsystm.h>
697c478bd9Sstevel@tonic-gate #include <sys/vmsystm.h>
707c478bd9Sstevel@tonic-gate #include <vm/hat.h>
717c478bd9Sstevel@tonic-gate #include <vm/as.h>
727c478bd9Sstevel@tonic-gate #include <vm/seg.h>
737c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h>
747c478bd9Sstevel@tonic-gate #include <vm/seg_kp.h>
757c478bd9Sstevel@tonic-gate #include <vm/page.h>
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate #include <sys/sysi86.h>
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate #include <fs/proc/prdata.h>
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate int	prnwatch = 10000;	/* maximum number of watched areas */
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate /*
847c478bd9Sstevel@tonic-gate  * Force a thread into the kernel if it is not already there.
857c478bd9Sstevel@tonic-gate  * This is a no-op on uniprocessors.
867c478bd9Sstevel@tonic-gate  */
877c478bd9Sstevel@tonic-gate /* ARGSUSED */
887c478bd9Sstevel@tonic-gate void
897c478bd9Sstevel@tonic-gate prpokethread(kthread_t *t)
907c478bd9Sstevel@tonic-gate {
917c478bd9Sstevel@tonic-gate 	if (t->t_state == TS_ONPROC && t->t_cpu != CPU)
927c478bd9Sstevel@tonic-gate 		poke_cpu(t->t_cpu->cpu_id);
937c478bd9Sstevel@tonic-gate }
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate /*
967c478bd9Sstevel@tonic-gate  * Return general registers.
977c478bd9Sstevel@tonic-gate  */
987c478bd9Sstevel@tonic-gate void
997c478bd9Sstevel@tonic-gate prgetprregs(klwp_t *lwp, prgregset_t prp)
1007c478bd9Sstevel@tonic-gate {
1017c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&lwptoproc(lwp)->p_lock));
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 	getgregs(lwp, prp);
1047c478bd9Sstevel@tonic-gate }
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate /*
1077c478bd9Sstevel@tonic-gate  * Set general registers.
1087c478bd9Sstevel@tonic-gate  * (Note: This can be an alias to setgregs().)
1097c478bd9Sstevel@tonic-gate  */
1107c478bd9Sstevel@tonic-gate void
1117c478bd9Sstevel@tonic-gate prsetprregs(klwp_t *lwp, prgregset_t prp, int initial)
1127c478bd9Sstevel@tonic-gate {
1137c478bd9Sstevel@tonic-gate 	if (initial)		/* set initial values */
1147c478bd9Sstevel@tonic-gate 		lwptoregs(lwp)->r_ps = PSL_USER;
1157c478bd9Sstevel@tonic-gate 	(void) setgregs(lwp, prp);
1167c478bd9Sstevel@tonic-gate }
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate /*
1217c478bd9Sstevel@tonic-gate  * Convert prgregset32 to native prgregset
1227c478bd9Sstevel@tonic-gate  */
1237c478bd9Sstevel@tonic-gate void
1247c478bd9Sstevel@tonic-gate prgregset_32ton(klwp_t *lwp, prgregset32_t src, prgregset_t dst)
1257c478bd9Sstevel@tonic-gate {
1267c478bd9Sstevel@tonic-gate 	struct regs *rp = lwptoregs(lwp);
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	dst[REG_GSBASE] = lwp->lwp_pcb.pcb_gsbase;
1297c478bd9Sstevel@tonic-gate 	dst[REG_FSBASE] = lwp->lwp_pcb.pcb_fsbase;
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	dst[REG_DS] = (uint16_t)src[DS];
1327c478bd9Sstevel@tonic-gate 	dst[REG_ES] = (uint16_t)src[ES];
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	dst[REG_GS] = (uint16_t)src[GS];
1357c478bd9Sstevel@tonic-gate 	dst[REG_FS] = (uint16_t)src[FS];
1367c478bd9Sstevel@tonic-gate 	dst[REG_SS] = (uint16_t)src[SS];
1377c478bd9Sstevel@tonic-gate 	dst[REG_RSP] = (uint32_t)src[UESP];
1387c478bd9Sstevel@tonic-gate 	dst[REG_RFL] =
1397c478bd9Sstevel@tonic-gate 		(rp->r_ps & ~PSL_USERMASK) | (src[EFL] & PSL_USERMASK);
1407c478bd9Sstevel@tonic-gate 	dst[REG_CS] = (uint16_t)src[CS];
1417c478bd9Sstevel@tonic-gate 	dst[REG_RIP] = (uint32_t)src[EIP];
1427c478bd9Sstevel@tonic-gate 	dst[REG_ERR] = (uint32_t)src[ERR];
1437c478bd9Sstevel@tonic-gate 	dst[REG_TRAPNO] = (uint32_t)src[TRAPNO];
1447c478bd9Sstevel@tonic-gate 	dst[REG_RAX] = (uint32_t)src[EAX];
1457c478bd9Sstevel@tonic-gate 	dst[REG_RCX] = (uint32_t)src[ECX];
1467c478bd9Sstevel@tonic-gate 	dst[REG_RDX] = (uint32_t)src[EDX];
1477c478bd9Sstevel@tonic-gate 	dst[REG_RBX] = (uint32_t)src[EBX];
1487c478bd9Sstevel@tonic-gate 	dst[REG_RBP] = (uint32_t)src[EBP];
1497c478bd9Sstevel@tonic-gate 	dst[REG_RSI] = (uint32_t)src[ESI];
1507c478bd9Sstevel@tonic-gate 	dst[REG_RDI] = (uint32_t)src[EDI];
1517c478bd9Sstevel@tonic-gate 	dst[REG_R8] = dst[REG_R9] = dst[REG_R10] = dst[REG_R11] =
1527c478bd9Sstevel@tonic-gate 	    dst[REG_R12] = dst[REG_R13] = dst[REG_R14] = dst[REG_R15] = 0;
1537c478bd9Sstevel@tonic-gate }
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate /*
1567c478bd9Sstevel@tonic-gate  * Return 32-bit general registers
1577c478bd9Sstevel@tonic-gate  */
1587c478bd9Sstevel@tonic-gate void
1597c478bd9Sstevel@tonic-gate prgetprregs32(klwp_t *lwp, prgregset32_t prp)
1607c478bd9Sstevel@tonic-gate {
1617c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&lwptoproc(lwp)->p_lock));
1627c478bd9Sstevel@tonic-gate 	getgregs32(lwp, prp);
1637c478bd9Sstevel@tonic-gate }
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate #endif	/* _SYSCALL32_IMPL */
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate /*
1687c478bd9Sstevel@tonic-gate  * Get the syscall return values for the lwp.
1697c478bd9Sstevel@tonic-gate  */
1707c478bd9Sstevel@tonic-gate int
1717c478bd9Sstevel@tonic-gate prgetrvals(klwp_t *lwp, long *rval1, long *rval2)
1727c478bd9Sstevel@tonic-gate {
1737c478bd9Sstevel@tonic-gate 	struct regs *r = lwptoregs(lwp);
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	if (r->r_ps & PS_C)
1767c478bd9Sstevel@tonic-gate 		return (r->r_r0);
1777c478bd9Sstevel@tonic-gate 	if (lwp->lwp_eosys == JUSTRETURN) {
1787c478bd9Sstevel@tonic-gate 		*rval1 = 0;
1797c478bd9Sstevel@tonic-gate 		*rval2 = 0;
1807c478bd9Sstevel@tonic-gate 	} else if (lwp_getdatamodel(lwp) != DATAMODEL_NATIVE) {
1817c478bd9Sstevel@tonic-gate 		/*
1827c478bd9Sstevel@tonic-gate 		 * XX64	Not sure we -really- need to do this, because the
1837c478bd9Sstevel@tonic-gate 		 *	syscall return already masks off the bottom values ..?
1847c478bd9Sstevel@tonic-gate 		 */
1857c478bd9Sstevel@tonic-gate 		*rval1 = r->r_r0 & (uint32_t)0xffffffffu;
1867c478bd9Sstevel@tonic-gate 		*rval2 = r->r_r1 & (uint32_t)0xffffffffu;
1877c478bd9Sstevel@tonic-gate 	} else {
1887c478bd9Sstevel@tonic-gate 		*rval1 = r->r_r0;
1897c478bd9Sstevel@tonic-gate 		*rval2 = r->r_r1;
1907c478bd9Sstevel@tonic-gate 	}
1917c478bd9Sstevel@tonic-gate 	return (0);
1927c478bd9Sstevel@tonic-gate }
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate /*
1957c478bd9Sstevel@tonic-gate  * Does the system support floating-point, either through hardware
1967c478bd9Sstevel@tonic-gate  * or by trapping and emulating floating-point machine instructions?
1977c478bd9Sstevel@tonic-gate  */
1987c478bd9Sstevel@tonic-gate int
1997c478bd9Sstevel@tonic-gate prhasfp(void)
2007c478bd9Sstevel@tonic-gate {
2017c478bd9Sstevel@tonic-gate 	extern int fp_kind;
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	return (fp_kind != FP_NO);
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate /*
2077c478bd9Sstevel@tonic-gate  * Get floating-point registers.
2087c478bd9Sstevel@tonic-gate  */
2097c478bd9Sstevel@tonic-gate void
2107c478bd9Sstevel@tonic-gate prgetprfpregs(klwp_t *lwp, prfpregset_t *pfp)
2117c478bd9Sstevel@tonic-gate {
2127c478bd9Sstevel@tonic-gate 	bzero(pfp, sizeof (prfpregset_t));
2137c478bd9Sstevel@tonic-gate 	getfpregs(lwp, pfp);
2147c478bd9Sstevel@tonic-gate }
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL)
2177c478bd9Sstevel@tonic-gate void
2187c478bd9Sstevel@tonic-gate prgetprfpregs32(klwp_t *lwp, prfpregset32_t *pfp)
2197c478bd9Sstevel@tonic-gate {
2207c478bd9Sstevel@tonic-gate 	bzero(pfp, sizeof (*pfp));
2217c478bd9Sstevel@tonic-gate 	getfpregs32(lwp, pfp);
2227c478bd9Sstevel@tonic-gate }
2237c478bd9Sstevel@tonic-gate #endif	/* _SYSCALL32_IMPL */
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate /*
2267c478bd9Sstevel@tonic-gate  * Set floating-point registers.
2277c478bd9Sstevel@tonic-gate  * (Note: This can be an alias to setfpregs().)
2287c478bd9Sstevel@tonic-gate  */
2297c478bd9Sstevel@tonic-gate void
2307c478bd9Sstevel@tonic-gate prsetprfpregs(klwp_t *lwp, prfpregset_t *pfp)
2317c478bd9Sstevel@tonic-gate {
2327c478bd9Sstevel@tonic-gate 	setfpregs(lwp, pfp);
2337c478bd9Sstevel@tonic-gate }
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL)
2367c478bd9Sstevel@tonic-gate void
2377c478bd9Sstevel@tonic-gate prsetprfpregs32(klwp_t *lwp, prfpregset32_t *pfp)
2387c478bd9Sstevel@tonic-gate {
2397c478bd9Sstevel@tonic-gate 	setfpregs32(lwp, pfp);
2407c478bd9Sstevel@tonic-gate }
2417c478bd9Sstevel@tonic-gate #endif	/* _SYSCALL32_IMPL */
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate /*
2447c478bd9Sstevel@tonic-gate  * Does the system support extra register state?
2457c478bd9Sstevel@tonic-gate  */
2467c478bd9Sstevel@tonic-gate /* ARGSUSED */
2477c478bd9Sstevel@tonic-gate int
2487c478bd9Sstevel@tonic-gate prhasx(proc_t *p)
2497c478bd9Sstevel@tonic-gate {
2507c478bd9Sstevel@tonic-gate 	return (0);
2517c478bd9Sstevel@tonic-gate }
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate /*
2547c478bd9Sstevel@tonic-gate  * Get the size of the extra registers.
2557c478bd9Sstevel@tonic-gate  */
2567c478bd9Sstevel@tonic-gate /* ARGSUSED */
2577c478bd9Sstevel@tonic-gate int
2587c478bd9Sstevel@tonic-gate prgetprxregsize(proc_t *p)
2597c478bd9Sstevel@tonic-gate {
2607c478bd9Sstevel@tonic-gate 	return (0);
2617c478bd9Sstevel@tonic-gate }
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate /*
2647c478bd9Sstevel@tonic-gate  * Get extra registers.
2657c478bd9Sstevel@tonic-gate  */
2667c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2677c478bd9Sstevel@tonic-gate void
2687c478bd9Sstevel@tonic-gate prgetprxregs(klwp_t *lwp, caddr_t prx)
2697c478bd9Sstevel@tonic-gate {
2707c478bd9Sstevel@tonic-gate 	/* no extra registers */
2717c478bd9Sstevel@tonic-gate }
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate /*
2747c478bd9Sstevel@tonic-gate  * Set extra registers.
2757c478bd9Sstevel@tonic-gate  */
2767c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2777c478bd9Sstevel@tonic-gate void
2787c478bd9Sstevel@tonic-gate prsetprxregs(klwp_t *lwp, caddr_t prx)
2797c478bd9Sstevel@tonic-gate {
2807c478bd9Sstevel@tonic-gate 	/* no extra registers */
2817c478bd9Sstevel@tonic-gate }
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate /*
2847c478bd9Sstevel@tonic-gate  * Return the base (lower limit) of the process stack.
2857c478bd9Sstevel@tonic-gate  */
2867c478bd9Sstevel@tonic-gate caddr_t
2877c478bd9Sstevel@tonic-gate prgetstackbase(proc_t *p)
2887c478bd9Sstevel@tonic-gate {
2897c478bd9Sstevel@tonic-gate 	return (p->p_usrstack - p->p_stksize);
2907c478bd9Sstevel@tonic-gate }
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate /*
2937c478bd9Sstevel@tonic-gate  * Return the "addr" field for pr_addr in prpsinfo_t.
2947c478bd9Sstevel@tonic-gate  * This is a vestige of the past, so whatever we return is OK.
2957c478bd9Sstevel@tonic-gate  */
2967c478bd9Sstevel@tonic-gate caddr_t
2977c478bd9Sstevel@tonic-gate prgetpsaddr(proc_t *p)
2987c478bd9Sstevel@tonic-gate {
2997c478bd9Sstevel@tonic-gate 	return ((caddr_t)p);
3007c478bd9Sstevel@tonic-gate }
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate /*
3037c478bd9Sstevel@tonic-gate  * Arrange to single-step the lwp.
3047c478bd9Sstevel@tonic-gate  */
3057c478bd9Sstevel@tonic-gate void
3067c478bd9Sstevel@tonic-gate prstep(klwp_t *lwp, int watchstep)
3077c478bd9Sstevel@tonic-gate {
3087c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&lwptoproc(lwp)->p_lock));
3097c478bd9Sstevel@tonic-gate 
310*65a89a64Smarx 	/*
311*65a89a64Smarx 	 * flag LWP so that its r_efl trace bit (PS_T) will be set on
312*65a89a64Smarx 	 * next return to usermode.
313*65a89a64Smarx 	 */
314*65a89a64Smarx 	lwp->lwp_pcb.pcb_flags |= REQUEST_STEP;
315*65a89a64Smarx 	lwp->lwp_pcb.pcb_flags &= ~REQUEST_NOSTEP;
316*65a89a64Smarx 
3177c478bd9Sstevel@tonic-gate 	if (watchstep)
3187c478bd9Sstevel@tonic-gate 		lwp->lwp_pcb.pcb_flags |= WATCH_STEP;
3197c478bd9Sstevel@tonic-gate 	else
3207c478bd9Sstevel@tonic-gate 		lwp->lwp_pcb.pcb_flags |= NORMAL_STEP;
3217c478bd9Sstevel@tonic-gate 
322*65a89a64Smarx 	aston(lwptot(lwp));	/* let trap() set PS_T in rp->r_efl */
3237c478bd9Sstevel@tonic-gate }
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate /*
3267c478bd9Sstevel@tonic-gate  * Undo prstep().
3277c478bd9Sstevel@tonic-gate  */
3287c478bd9Sstevel@tonic-gate void
3297c478bd9Sstevel@tonic-gate prnostep(klwp_t *lwp)
3307c478bd9Sstevel@tonic-gate {
3317c478bd9Sstevel@tonic-gate 	ASSERT(ttolwp(curthread) == lwp ||
3327c478bd9Sstevel@tonic-gate 	    MUTEX_NOT_HELD(&lwptoproc(lwp)->p_lock));
3337c478bd9Sstevel@tonic-gate 
334*65a89a64Smarx 	/*
335*65a89a64Smarx 	 * flag LWP so that its r_efl trace bit (PS_T) will be cleared on
336*65a89a64Smarx 	 * next return to usermode.
337*65a89a64Smarx 	 */
338*65a89a64Smarx 	lwp->lwp_pcb.pcb_flags |= REQUEST_NOSTEP;
339*65a89a64Smarx 
340*65a89a64Smarx 	lwp->lwp_pcb.pcb_flags &=
341*65a89a64Smarx 	    ~(REQUEST_STEP|NORMAL_STEP|WATCH_STEP|DEBUG_PENDING);
342*65a89a64Smarx 
343*65a89a64Smarx 	aston(lwptot(lwp));	/* let trap() clear PS_T in rp->r_efl */
3447c478bd9Sstevel@tonic-gate }
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate /*
3477c478bd9Sstevel@tonic-gate  * Return non-zero if a single-step is in effect.
3487c478bd9Sstevel@tonic-gate  */
3497c478bd9Sstevel@tonic-gate int
3507c478bd9Sstevel@tonic-gate prisstep(klwp_t *lwp)
3517c478bd9Sstevel@tonic-gate {
3527c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&lwptoproc(lwp)->p_lock));
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 	return ((lwp->lwp_pcb.pcb_flags &
3557c478bd9Sstevel@tonic-gate 		(NORMAL_STEP|WATCH_STEP|DEBUG_PENDING)) != 0);
3567c478bd9Sstevel@tonic-gate }
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate /*
3597c478bd9Sstevel@tonic-gate  * Set the PC to the specified virtual address.
3607c478bd9Sstevel@tonic-gate  */
3617c478bd9Sstevel@tonic-gate void
3627c478bd9Sstevel@tonic-gate prsvaddr(klwp_t *lwp, caddr_t vaddr)
3637c478bd9Sstevel@tonic-gate {
3647c478bd9Sstevel@tonic-gate 	struct regs *r = lwptoregs(lwp);
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&lwptoproc(lwp)->p_lock));
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 	r->r_pc = (uintptr_t)vaddr;
3697c478bd9Sstevel@tonic-gate }
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate /*
3727c478bd9Sstevel@tonic-gate  * Map address "addr" in address space "as" into a kernel virtual address.
3737c478bd9Sstevel@tonic-gate  * The memory is guaranteed to be resident and locked down.
3747c478bd9Sstevel@tonic-gate  */
3757c478bd9Sstevel@tonic-gate caddr_t
3767c478bd9Sstevel@tonic-gate prmapin(struct as *as, caddr_t addr, int writing)
3777c478bd9Sstevel@tonic-gate {
3787c478bd9Sstevel@tonic-gate 	page_t *pp;
3797c478bd9Sstevel@tonic-gate 	caddr_t kaddr;
3807c478bd9Sstevel@tonic-gate 	pfn_t pfnum;
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	/*
3837c478bd9Sstevel@tonic-gate 	 * XXX - Because of past mistakes, we have bits being returned
3847c478bd9Sstevel@tonic-gate 	 * by getpfnum that are actually the page type bits of the pte.
3857c478bd9Sstevel@tonic-gate 	 * When the object we are trying to map is a memory page with
3867c478bd9Sstevel@tonic-gate 	 * a page structure everything is ok and we can use the optimal
3877c478bd9Sstevel@tonic-gate 	 * method, ppmapin.  Otherwise, we have to do something special.
3887c478bd9Sstevel@tonic-gate 	 */
3897c478bd9Sstevel@tonic-gate 	pfnum = hat_getpfnum(as->a_hat, addr);
3907c478bd9Sstevel@tonic-gate 	if (pf_is_memory(pfnum)) {
3917c478bd9Sstevel@tonic-gate 		pp = page_numtopp_nolock(pfnum);
3927c478bd9Sstevel@tonic-gate 		if (pp != NULL) {
3937c478bd9Sstevel@tonic-gate 			ASSERT(PAGE_LOCKED(pp));
3947c478bd9Sstevel@tonic-gate 			kaddr = ppmapin(pp, writing ?
3957c478bd9Sstevel@tonic-gate 			    (PROT_READ | PROT_WRITE) : PROT_READ, (caddr_t)-1);
3967c478bd9Sstevel@tonic-gate 			return (kaddr + ((uintptr_t)addr & PAGEOFFSET));
3977c478bd9Sstevel@tonic-gate 		}
3987c478bd9Sstevel@tonic-gate 	}
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 	/*
4017c478bd9Sstevel@tonic-gate 	 * Oh well, we didn't have a page struct for the object we were
4027c478bd9Sstevel@tonic-gate 	 * trying to map in; ppmapin doesn't handle devices, but allocating a
4037c478bd9Sstevel@tonic-gate 	 * heap address allows ppmapout to free virtual space when done.
4047c478bd9Sstevel@tonic-gate 	 */
4057c478bd9Sstevel@tonic-gate 	kaddr = vmem_alloc(heap_arena, PAGESIZE, VM_SLEEP);
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 	hat_devload(kas.a_hat, kaddr, MMU_PAGESIZE,  pfnum,
4087c478bd9Sstevel@tonic-gate 	    writing ? (PROT_READ | PROT_WRITE) : PROT_READ, 0);
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 	return (kaddr + ((uintptr_t)addr & PAGEOFFSET));
4117c478bd9Sstevel@tonic-gate }
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate /*
4147c478bd9Sstevel@tonic-gate  * Unmap address "addr" in address space "as"; inverse of prmapin().
4157c478bd9Sstevel@tonic-gate  */
4167c478bd9Sstevel@tonic-gate /* ARGSUSED */
4177c478bd9Sstevel@tonic-gate void
4187c478bd9Sstevel@tonic-gate prmapout(struct as *as, caddr_t addr, caddr_t vaddr, int writing)
4197c478bd9Sstevel@tonic-gate {
4207c478bd9Sstevel@tonic-gate 	extern void ppmapout(caddr_t);
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 	vaddr = (caddr_t)((uintptr_t)vaddr & PAGEMASK);
4237c478bd9Sstevel@tonic-gate 	ppmapout(vaddr);
4247c478bd9Sstevel@tonic-gate }
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate /*
4277c478bd9Sstevel@tonic-gate  * Make sure the lwp is in an orderly state
4287c478bd9Sstevel@tonic-gate  * for inspection by a debugger through /proc.
4297c478bd9Sstevel@tonic-gate  * Called from stop() and from syslwp_create().
4307c478bd9Sstevel@tonic-gate  */
4317c478bd9Sstevel@tonic-gate /* ARGSUSED */
4327c478bd9Sstevel@tonic-gate void
4337c478bd9Sstevel@tonic-gate prstop(int why, int what)
4347c478bd9Sstevel@tonic-gate {
4357c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
4367c478bd9Sstevel@tonic-gate 	struct regs *r = lwptoregs(lwp);
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 	/*
43916ade92dScwb 	 * Make sure we don't deadlock on a recursive call to prstop().
44016ade92dScwb 	 * stop() tests the lwp_nostop_r and lwp_nostop flags.
4417c478bd9Sstevel@tonic-gate 	 */
44216ade92dScwb 	lwp->lwp_nostop_r++;
44316ade92dScwb 	lwp->lwp_nostop++;
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 	if (copyin_nowatch((caddr_t)r->r_pc, &lwp->lwp_pcb.pcb_instr,
4467c478bd9Sstevel@tonic-gate 		    sizeof (lwp->lwp_pcb.pcb_instr)) == 0)
4477c478bd9Sstevel@tonic-gate 		lwp->lwp_pcb.pcb_flags |= INSTR_VALID;
4487c478bd9Sstevel@tonic-gate 	else {
4497c478bd9Sstevel@tonic-gate 		lwp->lwp_pcb.pcb_flags &= ~INSTR_VALID;
4507c478bd9Sstevel@tonic-gate 		lwp->lwp_pcb.pcb_instr = 0;
4517c478bd9Sstevel@tonic-gate 	}
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 	(void) save_syscall_args();
45416ade92dScwb 	lwp->lwp_nostop--;
45516ade92dScwb 	lwp->lwp_nostop_r--;
4567c478bd9Sstevel@tonic-gate }
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate /*
4597c478bd9Sstevel@tonic-gate  * Fetch the user-level instruction on which the lwp is stopped.
4607c478bd9Sstevel@tonic-gate  * It was saved by the lwp itself, in prstop().
4617c478bd9Sstevel@tonic-gate  * Return non-zero if the instruction is valid.
4627c478bd9Sstevel@tonic-gate  */
4637c478bd9Sstevel@tonic-gate int
4647c478bd9Sstevel@tonic-gate prfetchinstr(klwp_t *lwp, ulong_t *ip)
4657c478bd9Sstevel@tonic-gate {
4667c478bd9Sstevel@tonic-gate 	*ip = (ulong_t)(instr_t)lwp->lwp_pcb.pcb_instr;
4677c478bd9Sstevel@tonic-gate 	return (lwp->lwp_pcb.pcb_flags & INSTR_VALID);
4687c478bd9Sstevel@tonic-gate }
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate /*
4717c478bd9Sstevel@tonic-gate  * Called from trap() when a load or store instruction
4727c478bd9Sstevel@tonic-gate  * falls in a watched page but is not a watchpoint.
4737c478bd9Sstevel@tonic-gate  * We emulate the instruction in the kernel.
4747c478bd9Sstevel@tonic-gate  */
4757c478bd9Sstevel@tonic-gate /* ARGSUSED */
4767c478bd9Sstevel@tonic-gate int
4777c478bd9Sstevel@tonic-gate pr_watch_emul(struct regs *rp, caddr_t addr, enum seg_rw rw)
4787c478bd9Sstevel@tonic-gate {
4797c478bd9Sstevel@tonic-gate #ifdef SOMEDAY
4807c478bd9Sstevel@tonic-gate 	int res;
4817c478bd9Sstevel@tonic-gate 	proc_t *p = curproc;
4827c478bd9Sstevel@tonic-gate 	char *badaddr = (caddr_t)(-1);
4837c478bd9Sstevel@tonic-gate 	int mapped;
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 	/* prevent recursive calls to pr_watch_emul() */
4867c478bd9Sstevel@tonic-gate 	ASSERT(!(curthread->t_flag & T_WATCHPT));
4877c478bd9Sstevel@tonic-gate 	curthread->t_flag |= T_WATCHPT;
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate 	watch_disable_addr(addr, 8, rw);
4907c478bd9Sstevel@tonic-gate 	res = do_unaligned(rp, &badaddr);
4917c478bd9Sstevel@tonic-gate 	watch_enable_addr(addr, 8, rw);
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 	curthread->t_flag &= ~T_WATCHPT;
4947c478bd9Sstevel@tonic-gate 	if (res == SIMU_SUCCESS) {
4957c478bd9Sstevel@tonic-gate 		/* adjust the pc */
4967c478bd9Sstevel@tonic-gate 		return (1);
4977c478bd9Sstevel@tonic-gate 	}
4987c478bd9Sstevel@tonic-gate #endif
4997c478bd9Sstevel@tonic-gate 	return (0);
5007c478bd9Sstevel@tonic-gate }
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate /*
5037c478bd9Sstevel@tonic-gate  * Return the number of active entries in the local descriptor table.
5047c478bd9Sstevel@tonic-gate  */
5057c478bd9Sstevel@tonic-gate int
5067c478bd9Sstevel@tonic-gate prnldt(proc_t *p)
5077c478bd9Sstevel@tonic-gate {
5087c478bd9Sstevel@tonic-gate 	int limit, i, n;
5097c478bd9Sstevel@tonic-gate 	user_desc_t *udp;
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_ldtlock));
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 	/*
5147c478bd9Sstevel@tonic-gate 	 * Currently 64 bit processes cannot have a private ldt.
5157c478bd9Sstevel@tonic-gate 	 */
5167c478bd9Sstevel@tonic-gate 	ASSERT(get_udatamodel() != DATAMODEL_LP64 || p->p_ldt == NULL);
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 	if (p->p_ldt == NULL)
5207c478bd9Sstevel@tonic-gate 		return (0);
5217c478bd9Sstevel@tonic-gate 	n = 0;
5227c478bd9Sstevel@tonic-gate 	limit = p->p_ldtlimit;
5237c478bd9Sstevel@tonic-gate 	ASSERT(limit >= 0 && limit < MAXNLDT);
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 	/*
5267c478bd9Sstevel@tonic-gate 	 * Count all present user descriptors.
5277c478bd9Sstevel@tonic-gate 	 */
5287c478bd9Sstevel@tonic-gate 	for (i = LDT_UDBASE, udp = &p->p_ldt[i]; i <= limit; i++, udp++)
5297c478bd9Sstevel@tonic-gate 		if (udp->usd_type != 0 || udp->usd_dpl != 0 || udp->usd_p != 0)
5307c478bd9Sstevel@tonic-gate 			n++;
5317c478bd9Sstevel@tonic-gate 	return (n);
5327c478bd9Sstevel@tonic-gate }
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate /*
5357c478bd9Sstevel@tonic-gate  * Fetch the active entries from the local descriptor table.
5367c478bd9Sstevel@tonic-gate  */
5377c478bd9Sstevel@tonic-gate void
5387c478bd9Sstevel@tonic-gate prgetldt(proc_t *p, struct ssd *ssd)
5397c478bd9Sstevel@tonic-gate {
5407c478bd9Sstevel@tonic-gate 	int i, limit;
5417c478bd9Sstevel@tonic-gate 	user_desc_t *udp;
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_ldtlock));
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate 	if (p->p_ldt == NULL)
5467c478bd9Sstevel@tonic-gate 		return;
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 	limit = p->p_ldtlimit;
5497c478bd9Sstevel@tonic-gate 	ASSERT(limit >= 0 && limit < MAXNLDT);
5507c478bd9Sstevel@tonic-gate 
5517c478bd9Sstevel@tonic-gate 	/*
5527c478bd9Sstevel@tonic-gate 	 * All present user descriptors.
5537c478bd9Sstevel@tonic-gate 	 */
5547c478bd9Sstevel@tonic-gate 	for (i = LDT_UDBASE, udp = &p->p_ldt[i]; i <= limit; i++, udp++)
5557c478bd9Sstevel@tonic-gate 		if (udp->usd_type != 0 || udp->usd_dpl != 0 ||
5567c478bd9Sstevel@tonic-gate 		    udp->usd_p != 0)
5577c478bd9Sstevel@tonic-gate 			usd_to_ssd(udp, ssd++, SEL_LDT(i));
5587c478bd9Sstevel@tonic-gate }
559