xref: /illumos-gate/usr/src/uts/i86pc/os/dtrace_subr.c (revision 86ef0a63)
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
5f498645aSahl  * Common Development and Distribution License (the "License").
6f498645aSahl  * 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  */
21f498645aSahl 
227c478bd9Sstevel@tonic-gate /*
23f34a7178SJoe Bonasera  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
2749048e7cSBryan Cantrill /*
2849048e7cSBryan Cantrill  * Copyright (c) 2011, Joyent, Inc. All rights reserved.
2949048e7cSBryan Cantrill  */
3049048e7cSBryan Cantrill 
317c478bd9Sstevel@tonic-gate #include <sys/dtrace.h>
327c478bd9Sstevel@tonic-gate #include <sys/fasttrap.h>
337c478bd9Sstevel@tonic-gate #include <sys/x_call.h>
347c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
357c478bd9Sstevel@tonic-gate #include <sys/trap.h>
367c478bd9Sstevel@tonic-gate #include <sys/psw.h>
377c478bd9Sstevel@tonic-gate #include <sys/privregs.h>
387c478bd9Sstevel@tonic-gate #include <sys/machsystm.h>
397c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h>
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate typedef struct dtrace_invop_hdlr {
427c478bd9Sstevel@tonic-gate 	int (*dtih_func)(uintptr_t, uintptr_t *, uintptr_t);
437c478bd9Sstevel@tonic-gate 	struct dtrace_invop_hdlr *dtih_next;
447c478bd9Sstevel@tonic-gate } dtrace_invop_hdlr_t;
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate dtrace_invop_hdlr_t *dtrace_invop_hdlr;
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate int
dtrace_invop(uintptr_t addr,uintptr_t * stack,uintptr_t eax)497c478bd9Sstevel@tonic-gate dtrace_invop(uintptr_t addr, uintptr_t *stack, uintptr_t eax)
507c478bd9Sstevel@tonic-gate {
517c478bd9Sstevel@tonic-gate 	dtrace_invop_hdlr_t *hdlr;
527c478bd9Sstevel@tonic-gate 	int rval;
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate 	for (hdlr = dtrace_invop_hdlr; hdlr != NULL; hdlr = hdlr->dtih_next) {
557c478bd9Sstevel@tonic-gate 		if ((rval = hdlr->dtih_func(addr, stack, eax)) != 0)
567c478bd9Sstevel@tonic-gate 			return (rval);
577c478bd9Sstevel@tonic-gate 	}
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate 	return (0);
607c478bd9Sstevel@tonic-gate }
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate void
dtrace_invop_add(int (* func)(uintptr_t,uintptr_t *,uintptr_t))637c478bd9Sstevel@tonic-gate dtrace_invop_add(int (*func)(uintptr_t, uintptr_t *, uintptr_t))
647c478bd9Sstevel@tonic-gate {
657c478bd9Sstevel@tonic-gate 	dtrace_invop_hdlr_t *hdlr;
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate 	hdlr = kmem_alloc(sizeof (dtrace_invop_hdlr_t), KM_SLEEP);
687c478bd9Sstevel@tonic-gate 	hdlr->dtih_func = func;
697c478bd9Sstevel@tonic-gate 	hdlr->dtih_next = dtrace_invop_hdlr;
707c478bd9Sstevel@tonic-gate 	dtrace_invop_hdlr = hdlr;
717c478bd9Sstevel@tonic-gate }
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate void
dtrace_invop_remove(int (* func)(uintptr_t,uintptr_t *,uintptr_t))747c478bd9Sstevel@tonic-gate dtrace_invop_remove(int (*func)(uintptr_t, uintptr_t *, uintptr_t))
757c478bd9Sstevel@tonic-gate {
767c478bd9Sstevel@tonic-gate 	dtrace_invop_hdlr_t *hdlr = dtrace_invop_hdlr, *prev = NULL;
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate 	for (;;) {
797c478bd9Sstevel@tonic-gate 		if (hdlr == NULL)
807c478bd9Sstevel@tonic-gate 			panic("attempt to remove non-existent invop handler");
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 		if (hdlr->dtih_func == func)
837c478bd9Sstevel@tonic-gate 			break;
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 		prev = hdlr;
867c478bd9Sstevel@tonic-gate 		hdlr = hdlr->dtih_next;
877c478bd9Sstevel@tonic-gate 	}
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	if (prev == NULL) {
907c478bd9Sstevel@tonic-gate 		ASSERT(dtrace_invop_hdlr == hdlr);
917c478bd9Sstevel@tonic-gate 		dtrace_invop_hdlr = hdlr->dtih_next;
927c478bd9Sstevel@tonic-gate 	} else {
937c478bd9Sstevel@tonic-gate 		ASSERT(dtrace_invop_hdlr != hdlr);
947c478bd9Sstevel@tonic-gate 		prev->dtih_next = hdlr->dtih_next;
957c478bd9Sstevel@tonic-gate 	}
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	kmem_free(hdlr, sizeof (dtrace_invop_hdlr_t));
987c478bd9Sstevel@tonic-gate }
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate int
dtrace_getipl(void)1017c478bd9Sstevel@tonic-gate dtrace_getipl(void)
1027c478bd9Sstevel@tonic-gate {
1037c478bd9Sstevel@tonic-gate 	return (CPU->cpu_pri);
1047c478bd9Sstevel@tonic-gate }
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1077c478bd9Sstevel@tonic-gate void
dtrace_toxic_ranges(void (* func)(uintptr_t base,uintptr_t limit))1087c478bd9Sstevel@tonic-gate dtrace_toxic_ranges(void (*func)(uintptr_t base, uintptr_t limit))
1097c478bd9Sstevel@tonic-gate {
1107c478bd9Sstevel@tonic-gate 	extern uintptr_t toxic_addr;
1117c478bd9Sstevel@tonic-gate 	extern size_t toxic_size;
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 	(*func)(0, _userlimit);
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	if (hole_end > hole_start)
1167c478bd9Sstevel@tonic-gate 		(*func)(hole_start, hole_end);
1177c478bd9Sstevel@tonic-gate 	(*func)(toxic_addr, toxic_addr + toxic_size);
1187c478bd9Sstevel@tonic-gate 	(*func)(0, _userlimit);
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate static int
dtrace_xcall_func(xc_arg_t arg1,xc_arg_t arg2,xc_arg_t arg3 __unused)122*027bcc9fSToomas Soome dtrace_xcall_func(xc_arg_t arg1, xc_arg_t arg2, xc_arg_t arg3 __unused)
1237c478bd9Sstevel@tonic-gate {
124*027bcc9fSToomas Soome 	dtrace_xcall_t func = (dtrace_xcall_t)arg1;
125*027bcc9fSToomas Soome 	(*func)((void*)arg2);
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	return (0);
1287c478bd9Sstevel@tonic-gate }
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1317c478bd9Sstevel@tonic-gate void
dtrace_xcall(processorid_t cpu,dtrace_xcall_t func,void * arg)1327c478bd9Sstevel@tonic-gate dtrace_xcall(processorid_t cpu, dtrace_xcall_t func, void *arg)
1337c478bd9Sstevel@tonic-gate {
1347c478bd9Sstevel@tonic-gate 	cpuset_t set;
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	CPUSET_ZERO(set);
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	if (cpu == DTRACE_CPUALL) {
1397c478bd9Sstevel@tonic-gate 		CPUSET_ALL(set);
1407c478bd9Sstevel@tonic-gate 	} else {
1417c478bd9Sstevel@tonic-gate 		CPUSET_ADD(set, cpu);
1427c478bd9Sstevel@tonic-gate 	}
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	kpreempt_disable();
145f34a7178SJoe Bonasera 	xc_sync((xc_arg_t)func, (xc_arg_t)arg, 0, CPUSET2BV(set),
146*027bcc9fSToomas Soome 	    dtrace_xcall_func);
1477c478bd9Sstevel@tonic-gate 	kpreempt_enable();
1487c478bd9Sstevel@tonic-gate }
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate void
dtrace_sync_func(void)1517c478bd9Sstevel@tonic-gate dtrace_sync_func(void)
1527c478bd9Sstevel@tonic-gate {}
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate void
dtrace_sync(void)1557c478bd9Sstevel@tonic-gate dtrace_sync(void)
1567c478bd9Sstevel@tonic-gate {
1577c478bd9Sstevel@tonic-gate 	dtrace_xcall(DTRACE_CPUALL, (dtrace_xcall_t)dtrace_sync_func, NULL);
1587c478bd9Sstevel@tonic-gate }
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate int (*dtrace_pid_probe_ptr)(struct regs *);
1617c478bd9Sstevel@tonic-gate int (*dtrace_return_probe_ptr)(struct regs *);
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate void
dtrace_user_probe(struct regs * rp,caddr_t addr,processorid_t cpuid)1647c478bd9Sstevel@tonic-gate dtrace_user_probe(struct regs *rp, caddr_t addr, processorid_t cpuid)
1657c478bd9Sstevel@tonic-gate {
1667c478bd9Sstevel@tonic-gate 	krwlock_t *rwp;
1677c478bd9Sstevel@tonic-gate 	proc_t *p = curproc;
1687c478bd9Sstevel@tonic-gate 	extern void trap(struct regs *, caddr_t, processorid_t);
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	if (USERMODE(rp->r_cs) || (rp->r_ps & PS_VM)) {
1717c478bd9Sstevel@tonic-gate 		if (curthread->t_cred != p->p_cred) {
1727c478bd9Sstevel@tonic-gate 			cred_t *oldcred = curthread->t_cred;
1737c478bd9Sstevel@tonic-gate 			/*
1747c478bd9Sstevel@tonic-gate 			 * DTrace accesses t_cred in probe context.  t_cred
1757c478bd9Sstevel@tonic-gate 			 * must always be either NULL, or point to a valid,
1767c478bd9Sstevel@tonic-gate 			 * allocated cred structure.
1777c478bd9Sstevel@tonic-gate 			 */
1787c478bd9Sstevel@tonic-gate 			curthread->t_cred = crgetcred();
1797c478bd9Sstevel@tonic-gate 			crfree(oldcred);
1807c478bd9Sstevel@tonic-gate 		}
1817c478bd9Sstevel@tonic-gate 	}
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	if (rp->r_trapno == T_DTRACE_RET) {
1847c478bd9Sstevel@tonic-gate 		uint8_t step = curthread->t_dtrace_step;
1857c478bd9Sstevel@tonic-gate 		uint8_t ret = curthread->t_dtrace_ret;
1867c478bd9Sstevel@tonic-gate 		uintptr_t npc = curthread->t_dtrace_npc;
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 		if (curthread->t_dtrace_ast) {
1897c478bd9Sstevel@tonic-gate 			aston(curthread);
1907c478bd9Sstevel@tonic-gate 			curthread->t_sig_check = 1;
1917c478bd9Sstevel@tonic-gate 		}
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 		/*
1947c478bd9Sstevel@tonic-gate 		 * Clear all user tracing flags.
1957c478bd9Sstevel@tonic-gate 		 */
1967c478bd9Sstevel@tonic-gate 		curthread->t_dtrace_ft = 0;
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 		/*
1997c478bd9Sstevel@tonic-gate 		 * If we weren't expecting to take a return probe trap, kill
2007c478bd9Sstevel@tonic-gate 		 * the process as though it had just executed an unassigned
2017c478bd9Sstevel@tonic-gate 		 * trap instruction.
2027c478bd9Sstevel@tonic-gate 		 */
2037c478bd9Sstevel@tonic-gate 		if (step == 0) {
2047c478bd9Sstevel@tonic-gate 			tsignal(curthread, SIGILL);
2057c478bd9Sstevel@tonic-gate 			return;
2067c478bd9Sstevel@tonic-gate 		}
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 		/*
2097c478bd9Sstevel@tonic-gate 		 * If we hit this trap unrelated to a return probe, we're
2107c478bd9Sstevel@tonic-gate 		 * just here to reset the AST flag since we deferred a signal
2117c478bd9Sstevel@tonic-gate 		 * until after we logically single-stepped the instruction we
2127c478bd9Sstevel@tonic-gate 		 * copied out.
2137c478bd9Sstevel@tonic-gate 		 */
2147c478bd9Sstevel@tonic-gate 		if (ret == 0) {
2157c478bd9Sstevel@tonic-gate 			rp->r_pc = npc;
2167c478bd9Sstevel@tonic-gate 			return;
2177c478bd9Sstevel@tonic-gate 		}
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 		/*
2207c478bd9Sstevel@tonic-gate 		 * We need to wait until after we've called the
2217c478bd9Sstevel@tonic-gate 		 * dtrace_return_probe_ptr function pointer to set %pc.
2227c478bd9Sstevel@tonic-gate 		 */
2237c478bd9Sstevel@tonic-gate 		rwp = &CPU->cpu_ft_lock;
2247c478bd9Sstevel@tonic-gate 		rw_enter(rwp, RW_READER);
2257c478bd9Sstevel@tonic-gate 		if (dtrace_return_probe_ptr != NULL)
2267c478bd9Sstevel@tonic-gate 			(void) (*dtrace_return_probe_ptr)(rp);
2277c478bd9Sstevel@tonic-gate 		rw_exit(rwp);
2287c478bd9Sstevel@tonic-gate 		rp->r_pc = npc;
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	} else if (rp->r_trapno == T_BPTFLT) {
231ddece0baSsethg 		uint8_t instr, instr2;
232ddece0baSsethg 		caddr_t linearpc;
2337c478bd9Sstevel@tonic-gate 		rwp = &CPU->cpu_ft_lock;
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 		/*
2367c478bd9Sstevel@tonic-gate 		 * The DTrace fasttrap provider uses the breakpoint trap
2377c478bd9Sstevel@tonic-gate 		 * (int 3). We let DTrace take the first crack at handling
2387c478bd9Sstevel@tonic-gate 		 * this trap; if it's not a probe that DTrace knowns about,
2397c478bd9Sstevel@tonic-gate 		 * we call into the trap() routine to handle it like a
2407c478bd9Sstevel@tonic-gate 		 * breakpoint placed by a conventional debugger.
2417c478bd9Sstevel@tonic-gate 		 */
2427c478bd9Sstevel@tonic-gate 		rw_enter(rwp, RW_READER);
2437c478bd9Sstevel@tonic-gate 		if (dtrace_pid_probe_ptr != NULL &&
2447c478bd9Sstevel@tonic-gate 		    (*dtrace_pid_probe_ptr)(rp) == 0) {
2457c478bd9Sstevel@tonic-gate 			rw_exit(rwp);
2467c478bd9Sstevel@tonic-gate 			return;
2477c478bd9Sstevel@tonic-gate 		}
2487c478bd9Sstevel@tonic-gate 		rw_exit(rwp);
2497c478bd9Sstevel@tonic-gate 
250ddece0baSsethg 		if (dtrace_linear_pc(rp, p, &linearpc) != 0) {
251ddece0baSsethg 			trap(rp, addr, cpuid);
252ddece0baSsethg 			return;
253ddece0baSsethg 		}
254ddece0baSsethg 
2557c478bd9Sstevel@tonic-gate 		/*
2567c478bd9Sstevel@tonic-gate 		 * If the instruction that caused the breakpoint trap doesn't
2577c478bd9Sstevel@tonic-gate 		 * look like an int 3 anymore, it may be that this tracepoint
2587c478bd9Sstevel@tonic-gate 		 * was removed just after the user thread executed it. In
2597c478bd9Sstevel@tonic-gate 		 * that case, return to user land to retry the instuction.
260ddece0baSsethg 		 * Note that we assume the length of the instruction to retry
261ddece0baSsethg 		 * is 1 byte because that's the length of FASTTRAP_INSTR.
262ddece0baSsethg 		 * We check for r_pc > 0 and > 2 so that we don't have to
263ddece0baSsethg 		 * deal with segment wraparound.
2647c478bd9Sstevel@tonic-gate 		 */
265ddece0baSsethg 		if (rp->r_pc > 0 && fuword8(linearpc - 1, &instr) == 0 &&
266ddece0baSsethg 		    instr != FASTTRAP_INSTR &&
267ddece0baSsethg 		    (instr != 3 || (rp->r_pc >= 2 &&
268ddece0baSsethg 		    (fuword8(linearpc - 2, &instr2) != 0 || instr2 != 0xCD)))) {
2697c478bd9Sstevel@tonic-gate 			rp->r_pc--;
2707c478bd9Sstevel@tonic-gate 			return;
2717c478bd9Sstevel@tonic-gate 		}
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 		trap(rp, addr, cpuid);
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 	} else {
2767c478bd9Sstevel@tonic-gate 		trap(rp, addr, cpuid);
2777c478bd9Sstevel@tonic-gate 	}
2787c478bd9Sstevel@tonic-gate }
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate void
dtrace_safe_synchronous_signal(void)2817c478bd9Sstevel@tonic-gate dtrace_safe_synchronous_signal(void)
2827c478bd9Sstevel@tonic-gate {
2837c478bd9Sstevel@tonic-gate 	kthread_t *t = curthread;
2847c478bd9Sstevel@tonic-gate 	struct regs *rp = lwptoregs(ttolwp(t));
2857c478bd9Sstevel@tonic-gate 	size_t isz = t->t_dtrace_npc - t->t_dtrace_pc;
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 	ASSERT(t->t_dtrace_on);
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 	/*
2907c478bd9Sstevel@tonic-gate 	 * If we're not in the range of scratch addresses, we're not actually
2917c478bd9Sstevel@tonic-gate 	 * tracing user instructions so turn off the flags. If the instruction
2927c478bd9Sstevel@tonic-gate 	 * we copied out caused a synchonous trap, reset the pc back to its
2937c478bd9Sstevel@tonic-gate 	 * original value and turn off the flags.
2947c478bd9Sstevel@tonic-gate 	 */
2957c478bd9Sstevel@tonic-gate 	if (rp->r_pc < t->t_dtrace_scrpc ||
2967c478bd9Sstevel@tonic-gate 	    rp->r_pc > t->t_dtrace_astpc + isz) {
2977c478bd9Sstevel@tonic-gate 		t->t_dtrace_ft = 0;
2987c478bd9Sstevel@tonic-gate 	} else if (rp->r_pc == t->t_dtrace_scrpc ||
2997c478bd9Sstevel@tonic-gate 	    rp->r_pc == t->t_dtrace_astpc) {
3007c478bd9Sstevel@tonic-gate 		rp->r_pc = t->t_dtrace_pc;
3017c478bd9Sstevel@tonic-gate 		t->t_dtrace_ft = 0;
3027c478bd9Sstevel@tonic-gate 	}
3037c478bd9Sstevel@tonic-gate }
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate int
dtrace_safe_defer_signal(void)3067c478bd9Sstevel@tonic-gate dtrace_safe_defer_signal(void)
3077c478bd9Sstevel@tonic-gate {
3087c478bd9Sstevel@tonic-gate 	kthread_t *t = curthread;
3097c478bd9Sstevel@tonic-gate 	struct regs *rp = lwptoregs(ttolwp(t));
3107c478bd9Sstevel@tonic-gate 	size_t isz = t->t_dtrace_npc - t->t_dtrace_pc;
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 	ASSERT(t->t_dtrace_on);
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	/*
3157c478bd9Sstevel@tonic-gate 	 * If we're not in the range of scratch addresses, we're not actually
3167c478bd9Sstevel@tonic-gate 	 * tracing user instructions so turn off the flags.
3177c478bd9Sstevel@tonic-gate 	 */
3187c478bd9Sstevel@tonic-gate 	if (rp->r_pc < t->t_dtrace_scrpc ||
3197c478bd9Sstevel@tonic-gate 	    rp->r_pc > t->t_dtrace_astpc + isz) {
3207c478bd9Sstevel@tonic-gate 		t->t_dtrace_ft = 0;
3217c478bd9Sstevel@tonic-gate 		return (0);
3227c478bd9Sstevel@tonic-gate 	}
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 	/*
32549048e7cSBryan Cantrill 	 * If we have executed the original instruction, but we have performed
32649048e7cSBryan Cantrill 	 * neither the jmp back to t->t_dtrace_npc nor the clean up of any
32749048e7cSBryan Cantrill 	 * registers used to emulate %rip-relative instructions in 64-bit mode,
32849048e7cSBryan Cantrill 	 * we'll save ourselves some effort by doing that here and taking the
32949048e7cSBryan Cantrill 	 * signal right away.  We detect this condition by seeing if the program
33049048e7cSBryan Cantrill 	 * counter is the range [scrpc + isz, astpc).
3317c478bd9Sstevel@tonic-gate 	 */
33249048e7cSBryan Cantrill 	if (rp->r_pc >= t->t_dtrace_scrpc + isz &&
33349048e7cSBryan Cantrill 	    rp->r_pc < t->t_dtrace_astpc) {
3347c478bd9Sstevel@tonic-gate 		/*
3357c478bd9Sstevel@tonic-gate 		 * If there is a scratch register and we're on the
3367c478bd9Sstevel@tonic-gate 		 * instruction immediately after the modified instruction,
3377c478bd9Sstevel@tonic-gate 		 * restore the value of that scratch register.
3387c478bd9Sstevel@tonic-gate 		 */
3397c478bd9Sstevel@tonic-gate 		if (t->t_dtrace_reg != 0 &&
3407c478bd9Sstevel@tonic-gate 		    rp->r_pc == t->t_dtrace_scrpc + isz) {
3417c478bd9Sstevel@tonic-gate 			switch (t->t_dtrace_reg) {
3427c478bd9Sstevel@tonic-gate 			case REG_RAX:
3437c478bd9Sstevel@tonic-gate 				rp->r_rax = t->t_dtrace_regv;
3447c478bd9Sstevel@tonic-gate 				break;
3457c478bd9Sstevel@tonic-gate 			case REG_RCX:
3467c478bd9Sstevel@tonic-gate 				rp->r_rcx = t->t_dtrace_regv;
3477c478bd9Sstevel@tonic-gate 				break;
3487c478bd9Sstevel@tonic-gate 			case REG_R8:
3497c478bd9Sstevel@tonic-gate 				rp->r_r8 = t->t_dtrace_regv;
3507c478bd9Sstevel@tonic-gate 				break;
3517c478bd9Sstevel@tonic-gate 			case REG_R9:
3527c478bd9Sstevel@tonic-gate 				rp->r_r9 = t->t_dtrace_regv;
3537c478bd9Sstevel@tonic-gate 				break;
3547c478bd9Sstevel@tonic-gate 			}
3557c478bd9Sstevel@tonic-gate 		}
3567c478bd9Sstevel@tonic-gate 		rp->r_pc = t->t_dtrace_npc;
3577c478bd9Sstevel@tonic-gate 		t->t_dtrace_ft = 0;
3587c478bd9Sstevel@tonic-gate 		return (0);
3597c478bd9Sstevel@tonic-gate 	}
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	/*
3627c478bd9Sstevel@tonic-gate 	 * Otherwise, make sure we'll return to the kernel after executing
3637c478bd9Sstevel@tonic-gate 	 * the copied out instruction and defer the signal.
3647c478bd9Sstevel@tonic-gate 	 */
3657c478bd9Sstevel@tonic-gate 	if (!t->t_dtrace_step) {
3667c478bd9Sstevel@tonic-gate 		ASSERT(rp->r_pc < t->t_dtrace_astpc);
3677c478bd9Sstevel@tonic-gate 		rp->r_pc += t->t_dtrace_astpc - t->t_dtrace_scrpc;
3687c478bd9Sstevel@tonic-gate 		t->t_dtrace_step = 1;
3697c478bd9Sstevel@tonic-gate 	}
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 	t->t_dtrace_ast = 1;
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 	return (1);
3747c478bd9Sstevel@tonic-gate }
375ae115bc7Smrj 
376ae115bc7Smrj /*
377ae115bc7Smrj  * Additional artificial frames for the machine type. For i86pc, we're already
378843e1988Sjohnlev  * accounted for, so return 0. On the hypervisor, we have an additional frame
379843e1988Sjohnlev  * (xen_callback_handler).
380ae115bc7Smrj  */
381ae115bc7Smrj int
dtrace_mach_aframes(void)382ae115bc7Smrj dtrace_mach_aframes(void)
383ae115bc7Smrj {
384843e1988Sjohnlev #ifdef __xpv
385843e1988Sjohnlev 	return (1);
386843e1988Sjohnlev #else
387ae115bc7Smrj 	return (0);
388843e1988Sjohnlev #endif
389ae115bc7Smrj }
390