xref: /xnu-10063.121.3/bsd/dev/arm64/sdt_arm.c (revision 2c2f96dc2b9a4408a43d3150ae9c105355ca3daa)
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/debug.h>
28 #include <kern/thread.h>
29 #include <mach/thread_status.h>
30 #include <mach/vm_param.h>
31 
32 #include <sys/dtrace.h>
33 #include <sys/dtrace_impl.h>
34 
35 #include <sys/dtrace_glue.h>
36 
37 #include <sys/sdt_impl.h>
38 
39 extern sdt_probe_t      **sdt_probetab;
40 
41 int
sdt_invop(__unused uintptr_t addr,__unused uintptr_t * stack,__unused uintptr_t eax)42 sdt_invop(__unused uintptr_t addr, __unused uintptr_t *stack, __unused uintptr_t eax)
43 {
44 #pragma unused(eax)
45 	sdt_probe_t *sdt = sdt_probetab[SDT_ADDR2NDX(addr)];
46 
47 	for (; sdt != NULL; sdt = sdt->sdp_hashnext) {
48 		if ((uintptr_t) sdt->sdp_patchpoint == addr) {
49 			struct arm_saved_state* regs = (struct arm_saved_state*) stack;
50 
51 			dtrace_probe(sdt->sdp_id, get_saved_state_reg(regs, 0), get_saved_state_reg(regs, 1),
52 			    get_saved_state_reg(regs, 2), get_saved_state_reg(regs, 3), get_saved_state_reg(regs, 4));
53 
54 			return DTRACE_INVOP_NOP;
55 		}
56 	}
57 
58 	return 0;
59 }
60 
61 struct frame {
62 	struct frame *backchain;
63 	uintptr_t retaddr;
64 };
65 
66 /*ARGSUSED*/
67 uint64_t
sdt_getarg(void * arg,dtrace_id_t id,void * parg,int argno,int aframes)68 sdt_getarg(void *arg, dtrace_id_t id, void *parg, int argno, int aframes)
69 {
70 #pragma unused(arg,id,parg)     /* __APPLE__ */
71 
72 	uint64_t val = 0;
73 	struct frame *fp = (struct frame *)__builtin_frame_address(0);
74 	uintptr_t *stack;
75 	uintptr_t pc;
76 	int i;
77 
78 	/*
79 	 * A total of eight arguments are passed via registers;  any argument
80 	 * with an index of 7 or lower is therefore in a register.
81 	 */
82 
83 	int inreg = 7;
84 
85 	for (i = 1; i <= aframes; i++) {
86 		fp = fp->backchain;
87 #if __has_feature(ptrauth_returns)
88 		pc = (uintptr_t)ptrauth_strip((void*)fp->retaddr, ptrauth_key_return_address);
89 #else
90 		pc = fp->retaddr;
91 #endif
92 
93 		if (dtrace_invop_callsite_pre != NULL
94 		    && pc > (uintptr_t)dtrace_invop_callsite_pre
95 		    && pc <= (uintptr_t)dtrace_invop_callsite_post) {
96 			/*
97 			 * When we pass through the invalid op handler,
98 			 * we expect to find the save area structure,
99 			 * pushed on the stack where we took the trap.
100 			 * If the argument we seek is passed in a register, then
101 			 * we can load it directly from this saved area.
102 			 * If the argument we seek is passed on the stack, then
103 			 * we increment the frame pointer further, to find the
104 			 * pushed args
105 			 */
106 
107 			/* fp points to the dtrace_invop activation */
108 			fp = fp->backchain; /* fbt_perfCallback  */
109 			fp = fp->backchain; /* sleh_synchronous */
110 			fp = fp->backchain; /* fleh_synchronous */
111 
112 			arm_saved_state_t *tagged_regs = (arm_saved_state_t *)((uintptr_t *)&fp[1]);
113 			arm_saved_state64_t *saved_state = saved_state64(tagged_regs);
114 
115 			if (argno <= inreg) {
116 				/* The argument will be in a register */
117 				stack = (uintptr_t *)&saved_state->x[0];
118 			} else {
119 				/* The argument will be found on the stack */
120 				fp = (struct frame *)(saved_state->sp);
121 				stack = (uintptr_t *)&fp[0]; /* Find marshalled arguments */
122 				argno -= (inreg + 1);
123 			}
124 			goto load;
125 		}
126 	}
127 
128 	/*
129 	 * We know that we did not come through a trap to get into
130 	 * dtrace_probe() --  We arrive here when the provider has
131 	 * called dtrace_probe() directly.
132 	 * The probe ID is the first argument to dtrace_probe().
133 	 * We must advance beyond that to get the argX.
134 	 */
135 	argno++; /* Advance past probeID */
136 
137 	if (argno <= inreg) {
138 		/*
139 		 * This shouldn't happen.  If the argument is passed in a
140 		 * register then it should have been, well, passed in a
141 		 * register...
142 		 */
143 		DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP);
144 		return 0;
145 	}
146 
147 	argno -= (inreg + 1);
148 	stack = (uintptr_t *)&fp[1]; /* Find marshalled arguments */
149 
150 load:
151 	DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT);
152 	/* dtrace_probe arguments arg0 .. arg4 are 64bits wide */
153 	val = (uint64_t)(*(((uintptr_t *)stack) + argno));
154 	DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT);
155 	return val;
156 }
157