xref: /titanic_44/usr/src/uts/i86pc/os/dtrace_subr.c (revision ea8dc4b6d2251b437950c0056bc626b311c73c27)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/dtrace.h>
30 #include <sys/fasttrap.h>
31 #include <sys/x_call.h>
32 #include <sys/cmn_err.h>
33 #include <sys/trap.h>
34 #include <sys/psw.h>
35 #include <sys/privregs.h>
36 #include <sys/machsystm.h>
37 #include <vm/seg_kmem.h>
38 
39 typedef struct dtrace_invop_hdlr {
40 	int (*dtih_func)(uintptr_t, uintptr_t *, uintptr_t);
41 	struct dtrace_invop_hdlr *dtih_next;
42 } dtrace_invop_hdlr_t;
43 
44 dtrace_invop_hdlr_t *dtrace_invop_hdlr;
45 
46 int
47 dtrace_invop(uintptr_t addr, uintptr_t *stack, uintptr_t eax)
48 {
49 	dtrace_invop_hdlr_t *hdlr;
50 	int rval;
51 
52 	for (hdlr = dtrace_invop_hdlr; hdlr != NULL; hdlr = hdlr->dtih_next) {
53 		if ((rval = hdlr->dtih_func(addr, stack, eax)) != 0)
54 			return (rval);
55 	}
56 
57 	return (0);
58 }
59 
60 void
61 dtrace_invop_add(int (*func)(uintptr_t, uintptr_t *, uintptr_t))
62 {
63 	dtrace_invop_hdlr_t *hdlr;
64 
65 	hdlr = kmem_alloc(sizeof (dtrace_invop_hdlr_t), KM_SLEEP);
66 	hdlr->dtih_func = func;
67 	hdlr->dtih_next = dtrace_invop_hdlr;
68 	dtrace_invop_hdlr = hdlr;
69 }
70 
71 void
72 dtrace_invop_remove(int (*func)(uintptr_t, uintptr_t *, uintptr_t))
73 {
74 	dtrace_invop_hdlr_t *hdlr = dtrace_invop_hdlr, *prev = NULL;
75 
76 	for (;;) {
77 		if (hdlr == NULL)
78 			panic("attempt to remove non-existent invop handler");
79 
80 		if (hdlr->dtih_func == func)
81 			break;
82 
83 		prev = hdlr;
84 		hdlr = hdlr->dtih_next;
85 	}
86 
87 	if (prev == NULL) {
88 		ASSERT(dtrace_invop_hdlr == hdlr);
89 		dtrace_invop_hdlr = hdlr->dtih_next;
90 	} else {
91 		ASSERT(dtrace_invop_hdlr != hdlr);
92 		prev->dtih_next = hdlr->dtih_next;
93 	}
94 
95 	kmem_free(hdlr, sizeof (dtrace_invop_hdlr_t));
96 }
97 
98 int
99 dtrace_getipl(void)
100 {
101 	return (CPU->cpu_pri);
102 }
103 
104 /*ARGSUSED*/
105 void
106 dtrace_toxic_ranges(void (*func)(uintptr_t base, uintptr_t limit))
107 {
108 #ifdef __amd64
109 	extern uintptr_t toxic_addr;
110 	extern size_t toxic_size;
111 
112 	(*func)(0, _userlimit);
113 
114 	if (hole_end > hole_start)
115 		(*func)(hole_start, hole_end);
116 	(*func)(toxic_addr, toxic_addr + toxic_size);
117 #else
118 	extern void *device_arena_contains(void *, size_t, size_t *);
119 	caddr_t	vaddr;
120 	size_t	len;
121 
122 	for (vaddr = (caddr_t)kernelbase; vaddr < (caddr_t)KERNEL_TEXT;
123 	    vaddr += len) {
124 		len = (caddr_t)KERNEL_TEXT - vaddr;
125 		vaddr = device_arena_contains(vaddr, len, &len);
126 		if (vaddr == NULL)
127 		    break;
128 		(*func)((uintptr_t)vaddr, (uintptr_t)vaddr + len);
129 	}
130 #endif
131 	(*func)(0, _userlimit);
132 }
133 
134 static int
135 dtrace_xcall_func(dtrace_xcall_t func, void *arg)
136 {
137 	(*func)(arg);
138 
139 	return (0);
140 }
141 
142 /*ARGSUSED*/
143 void
144 dtrace_xcall(processorid_t cpu, dtrace_xcall_t func, void *arg)
145 {
146 	cpuset_t set;
147 
148 	CPUSET_ZERO(set);
149 
150 	if (cpu == DTRACE_CPUALL) {
151 		CPUSET_ALL(set);
152 	} else {
153 		CPUSET_ADD(set, cpu);
154 	}
155 
156 	kpreempt_disable();
157 	xc_sync((xc_arg_t)func, (xc_arg_t)arg, 0, X_CALL_HIPRI, set,
158 		(xc_func_t)dtrace_xcall_func);
159 	kpreempt_enable();
160 }
161 
162 void
163 dtrace_sync_func(void)
164 {}
165 
166 void
167 dtrace_sync(void)
168 {
169 	dtrace_xcall(DTRACE_CPUALL, (dtrace_xcall_t)dtrace_sync_func, NULL);
170 }
171 
172 int (*dtrace_fasttrap_probe_ptr)(struct regs *);
173 int (*dtrace_pid_probe_ptr)(struct regs *);
174 int (*dtrace_return_probe_ptr)(struct regs *);
175 
176 void
177 dtrace_user_probe(struct regs *rp, caddr_t addr, processorid_t cpuid)
178 {
179 	krwlock_t *rwp;
180 	proc_t *p = curproc;
181 	extern void trap(struct regs *, caddr_t, processorid_t);
182 
183 	if (USERMODE(rp->r_cs) || (rp->r_ps & PS_VM)) {
184 		if (curthread->t_cred != p->p_cred) {
185 			cred_t *oldcred = curthread->t_cred;
186 			/*
187 			 * DTrace accesses t_cred in probe context.  t_cred
188 			 * must always be either NULL, or point to a valid,
189 			 * allocated cred structure.
190 			 */
191 			curthread->t_cred = crgetcred();
192 			crfree(oldcred);
193 		}
194 	}
195 
196 	if (rp->r_trapno == T_DTRACE_RET) {
197 		uint8_t step = curthread->t_dtrace_step;
198 		uint8_t ret = curthread->t_dtrace_ret;
199 		uintptr_t npc = curthread->t_dtrace_npc;
200 
201 		if (curthread->t_dtrace_ast) {
202 			aston(curthread);
203 			curthread->t_sig_check = 1;
204 		}
205 
206 		/*
207 		 * Clear all user tracing flags.
208 		 */
209 		curthread->t_dtrace_ft = 0;
210 
211 		/*
212 		 * If we weren't expecting to take a return probe trap, kill
213 		 * the process as though it had just executed an unassigned
214 		 * trap instruction.
215 		 */
216 		if (step == 0) {
217 			tsignal(curthread, SIGILL);
218 			return;
219 		}
220 
221 		/*
222 		 * If we hit this trap unrelated to a return probe, we're
223 		 * just here to reset the AST flag since we deferred a signal
224 		 * until after we logically single-stepped the instruction we
225 		 * copied out.
226 		 */
227 		if (ret == 0) {
228 			rp->r_pc = npc;
229 			return;
230 		}
231 
232 		/*
233 		 * We need to wait until after we've called the
234 		 * dtrace_return_probe_ptr function pointer to set %pc.
235 		 */
236 		rwp = &CPU->cpu_ft_lock;
237 		rw_enter(rwp, RW_READER);
238 		if (dtrace_return_probe_ptr != NULL)
239 			(void) (*dtrace_return_probe_ptr)(rp);
240 		rw_exit(rwp);
241 		rp->r_pc = npc;
242 
243 	} else if (rp->r_trapno == T_DTRACE_PROBE) {
244 		rwp = &CPU->cpu_ft_lock;
245 		rw_enter(rwp, RW_READER);
246 		if (dtrace_fasttrap_probe_ptr != NULL)
247 			(void) (*dtrace_fasttrap_probe_ptr)(rp);
248 		rw_exit(rwp);
249 
250 	} else if (rp->r_trapno == T_BPTFLT) {
251 		uint8_t instr;
252 		rwp = &CPU->cpu_ft_lock;
253 
254 		/*
255 		 * The DTrace fasttrap provider uses the breakpoint trap
256 		 * (int 3). We let DTrace take the first crack at handling
257 		 * this trap; if it's not a probe that DTrace knowns about,
258 		 * we call into the trap() routine to handle it like a
259 		 * breakpoint placed by a conventional debugger.
260 		 */
261 		rw_enter(rwp, RW_READER);
262 		if (dtrace_pid_probe_ptr != NULL &&
263 		    (*dtrace_pid_probe_ptr)(rp) == 0) {
264 			rw_exit(rwp);
265 			return;
266 		}
267 		rw_exit(rwp);
268 
269 		/*
270 		 * If the instruction that caused the breakpoint trap doesn't
271 		 * look like an int 3 anymore, it may be that this tracepoint
272 		 * was removed just after the user thread executed it. In
273 		 * that case, return to user land to retry the instuction.
274 		 */
275 		if (fuword8((void *)(rp->r_pc - 1), &instr) == 0 &&
276 		    instr != FASTTRAP_INSTR) {
277 			rp->r_pc--;
278 			return;
279 		}
280 
281 		trap(rp, addr, cpuid);
282 
283 	} else {
284 		trap(rp, addr, cpuid);
285 	}
286 }
287 
288 void
289 dtrace_safe_synchronous_signal(void)
290 {
291 	kthread_t *t = curthread;
292 	struct regs *rp = lwptoregs(ttolwp(t));
293 	size_t isz = t->t_dtrace_npc - t->t_dtrace_pc;
294 
295 	ASSERT(t->t_dtrace_on);
296 
297 	/*
298 	 * If we're not in the range of scratch addresses, we're not actually
299 	 * tracing user instructions so turn off the flags. If the instruction
300 	 * we copied out caused a synchonous trap, reset the pc back to its
301 	 * original value and turn off the flags.
302 	 */
303 	if (rp->r_pc < t->t_dtrace_scrpc ||
304 	    rp->r_pc > t->t_dtrace_astpc + isz) {
305 		t->t_dtrace_ft = 0;
306 	} else if (rp->r_pc == t->t_dtrace_scrpc ||
307 	    rp->r_pc == t->t_dtrace_astpc) {
308 		rp->r_pc = t->t_dtrace_pc;
309 		t->t_dtrace_ft = 0;
310 	}
311 }
312 
313 int
314 dtrace_safe_defer_signal(void)
315 {
316 	kthread_t *t = curthread;
317 	struct regs *rp = lwptoregs(ttolwp(t));
318 	size_t isz = t->t_dtrace_npc - t->t_dtrace_pc;
319 
320 	ASSERT(t->t_dtrace_on);
321 
322 	/*
323 	 * If we're not in the range of scratch addresses, we're not actually
324 	 * tracing user instructions so turn off the flags.
325 	 */
326 	if (rp->r_pc < t->t_dtrace_scrpc ||
327 	    rp->r_pc > t->t_dtrace_astpc + isz) {
328 		t->t_dtrace_ft = 0;
329 		return (0);
330 	}
331 
332 	/*
333 	 * If we've executed the original instruction, but haven't performed
334 	 * the jmp back to t->t_dtrace_npc or the clean up of any registers
335 	 * used to emulate %rip-relative instructions in 64-bit mode, do that
336 	 * here and take the signal right away. We detect this condition by
337 	 * seeing if the program counter is the range [scrpc + isz, astpc).
338 	 */
339 	if (t->t_dtrace_astpc - rp->r_pc <
340 	    t->t_dtrace_astpc - t->t_dtrace_scrpc - isz) {
341 #ifdef __amd64
342 		/*
343 		 * If there is a scratch register and we're on the
344 		 * instruction immediately after the modified instruction,
345 		 * restore the value of that scratch register.
346 		 */
347 		if (t->t_dtrace_reg != 0 &&
348 		    rp->r_pc == t->t_dtrace_scrpc + isz) {
349 			switch (t->t_dtrace_reg) {
350 			case REG_RAX:
351 				rp->r_rax = t->t_dtrace_regv;
352 				break;
353 			case REG_RCX:
354 				rp->r_rcx = t->t_dtrace_regv;
355 				break;
356 			case REG_R8:
357 				rp->r_r8 = t->t_dtrace_regv;
358 				break;
359 			case REG_R9:
360 				rp->r_r9 = t->t_dtrace_regv;
361 				break;
362 			}
363 		}
364 #endif
365 		rp->r_pc = t->t_dtrace_npc;
366 		t->t_dtrace_ft = 0;
367 		return (0);
368 	}
369 
370 	/*
371 	 * Otherwise, make sure we'll return to the kernel after executing
372 	 * the copied out instruction and defer the signal.
373 	 */
374 	if (!t->t_dtrace_step) {
375 		ASSERT(rp->r_pc < t->t_dtrace_astpc);
376 		rp->r_pc += t->t_dtrace_astpc - t->t_dtrace_scrpc;
377 		t->t_dtrace_step = 1;
378 	}
379 
380 	t->t_dtrace_ast = 1;
381 
382 	return (1);
383 }
384