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