xref: /xnu-8020.140.41/bsd/dev/arm/sdt_arm.c (revision 27b03b360a988dfd3dfdf34262bb0042026747cc)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <kern/cpu_data.h>
27 #include <kern/thread.h>
28 #include <mach/thread_status.h>
29 #include <mach/vm_param.h>
30 
31 #include <sys/dtrace.h>
32 #include <sys/dtrace_impl.h>
33 
34 #include <sys/dtrace_glue.h>
35 
36 #include <sys/sdt_impl.h>
37 
38 extern sdt_probe_t      **sdt_probetab;
39 
40 int
sdt_invop(__unused uintptr_t addr,__unused uintptr_t * stack,__unused uintptr_t eax)41 sdt_invop(__unused uintptr_t addr, __unused uintptr_t *stack, __unused uintptr_t eax)
42 {
43 #pragma unused(eax)
44 	sdt_probe_t *sdt = sdt_probetab[SDT_ADDR2NDX(addr)];
45 
46 	for (; sdt != NULL; sdt = sdt->sdp_hashnext) {
47 		if ((uintptr_t) sdt->sdp_patchpoint == addr) {
48 			struct arm_saved_state* regs = (struct arm_saved_state*) stack;
49 			uintptr_t stack4 = *((uintptr_t*) regs->sp);
50 
51 			dtrace_probe(sdt->sdp_id, regs->r[0], regs->r[1], regs->r[2], regs->r[3], stack4);
52 
53 			return DTRACE_INVOP_NOP;
54 		}
55 	}
56 
57 	return 0;
58 }
59 
60 struct frame {
61 	struct frame *backchain;
62 	uintptr_t retaddr;
63 };
64 
65 /*ARGSUSED*/
66 uint64_t
sdt_getarg(void * arg,dtrace_id_t id,void * parg,int argno,int aframes)67 sdt_getarg(void *arg, dtrace_id_t id, void *parg, int argno, int aframes)
68 {
69 #pragma unused(arg,id,parg)     /* __APPLE__ */
70 	uint64_t val = 0;
71 	struct frame *fp = (struct frame *)__builtin_frame_address(0);
72 	uintptr_t *stack;
73 	uintptr_t pc;
74 	int i;
75 
76 	/*
77 	 * On ARM, up to four args are passed via registers; r0,r1,r2,r3
78 	 * So coming into this function, arg >= 4 should be on the stack.
79 	 * e.g. arg==5 refers to the 6th arg passed to the probed function.
80 	 */
81 	int inreg = 4;
82 
83 	for (i = 1; i <= aframes; i++) {
84 		fp = fp->backchain;
85 		pc = fp->retaddr;
86 
87 		if (dtrace_invop_callsite_pre != NULL
88 		    && pc > (uintptr_t)dtrace_invop_callsite_pre
89 		    && pc <= (uintptr_t)dtrace_invop_callsite_post) {
90 			/*
91 			 * When we pass through the invalid op handler,
92 			 * we expect to find the save area structure,
93 			 * pushed on the stack where we took the trap.
94 			 * If the argument we seek is passed in a register, then
95 			 * we can load it directly from this saved area.
96 			 * If the argument we seek is passed on the stack, then
97 			 * we increment the frame pointer further, to find the
98 			 * pushed args
99 			 */
100 
101 			/* fp points to the dtrace_invop activation */
102 			fp = fp->backchain; /* to the fbt_perfCallback activation */
103 			fp = fp->backchain; /* to the sleh_undef activation */
104 
105 #if __BIGGEST_ALIGNMENT__ > 4
106 			/**
107 			 * rdar://problem/24228656: On armv7k, the stack is realigned in sleh_undef2 to
108 			 * be 16-bytes aligned and the old value is pushed to
109 			 * the stack, so we retrieve it from here
110 			 */
111 			arm_saved_state_t *saved_state = (arm_saved_state_t *)(uintptr_t*)*((uintptr_t *)&fp[1]);
112 #else
113 			arm_saved_state_t *saved_state = (arm_saved_state_t *)((uintptr_t *)&fp[1]);
114 #endif
115 			if (argno <= inreg) {
116 				/* For clarity only... should not get here */
117 				stack = (uintptr_t *)&saved_state->r[0];
118 			} else {
119 				fp = (struct frame *)(saved_state->sp);
120 				stack = (uintptr_t *)&fp[0]; /* Find marshalled arguments */
121 				argno -= inreg;
122 			}
123 			goto load;
124 		}
125 	}
126 
127 	/*
128 	 * We know that we did not come through a trap to get into
129 	 * dtrace_probe() --  We arrive here when the provider has
130 	 * called dtrace_probe() directly.
131 	 * The probe ID is the first argument to dtrace_probe().
132 	 * We must advance beyond that to get the argX.
133 	 */
134 	argno++; /* Advance past probeID */
135 
136 	if (argno <= inreg) {
137 		/*
138 		 * This shouldn't happen.  If the argument is passed in a
139 		 * register then it should have been, well, passed in a
140 		 * register...
141 		 */
142 		DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
143 		return 0;
144 	}
145 
146 	argno -= (inreg + 1);
147 	stack = (uintptr_t *)&fp[1]; /* Find marshalled arguments */
148 
149 load:
150 	DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
151 	/* dtrace_probe arguments arg0 .. arg4 are 64bits wide */
152 	val = (uint64_t)(*(((uintptr_t *)stack) + argno));
153 	DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
154 	return val;
155 }
156