xref: /illumos-gate/usr/src/uts/i86pc/os/dtrace_subr.c (revision 60405de4d8688d96dd05157c28db3ade5c9bc234)
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 /*
23  * Copyright 2006 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_pid_probe_ptr)(struct regs *);
173 int (*dtrace_return_probe_ptr)(struct regs *);
174 
175 void
176 dtrace_user_probe(struct regs *rp, caddr_t addr, processorid_t cpuid)
177 {
178 	krwlock_t *rwp;
179 	proc_t *p = curproc;
180 	extern void trap(struct regs *, caddr_t, processorid_t);
181 
182 	if (USERMODE(rp->r_cs) || (rp->r_ps & PS_VM)) {
183 		if (curthread->t_cred != p->p_cred) {
184 			cred_t *oldcred = curthread->t_cred;
185 			/*
186 			 * DTrace accesses t_cred in probe context.  t_cred
187 			 * must always be either NULL, or point to a valid,
188 			 * allocated cred structure.
189 			 */
190 			curthread->t_cred = crgetcred();
191 			crfree(oldcred);
192 		}
193 	}
194 
195 	if (rp->r_trapno == T_DTRACE_RET) {
196 		uint8_t step = curthread->t_dtrace_step;
197 		uint8_t ret = curthread->t_dtrace_ret;
198 		uintptr_t npc = curthread->t_dtrace_npc;
199 
200 		if (curthread->t_dtrace_ast) {
201 			aston(curthread);
202 			curthread->t_sig_check = 1;
203 		}
204 
205 		/*
206 		 * Clear all user tracing flags.
207 		 */
208 		curthread->t_dtrace_ft = 0;
209 
210 		/*
211 		 * If we weren't expecting to take a return probe trap, kill
212 		 * the process as though it had just executed an unassigned
213 		 * trap instruction.
214 		 */
215 		if (step == 0) {
216 			tsignal(curthread, SIGILL);
217 			return;
218 		}
219 
220 		/*
221 		 * If we hit this trap unrelated to a return probe, we're
222 		 * just here to reset the AST flag since we deferred a signal
223 		 * until after we logically single-stepped the instruction we
224 		 * copied out.
225 		 */
226 		if (ret == 0) {
227 			rp->r_pc = npc;
228 			return;
229 		}
230 
231 		/*
232 		 * We need to wait until after we've called the
233 		 * dtrace_return_probe_ptr function pointer to set %pc.
234 		 */
235 		rwp = &CPU->cpu_ft_lock;
236 		rw_enter(rwp, RW_READER);
237 		if (dtrace_return_probe_ptr != NULL)
238 			(void) (*dtrace_return_probe_ptr)(rp);
239 		rw_exit(rwp);
240 		rp->r_pc = npc;
241 
242 	} else if (rp->r_trapno == T_BPTFLT) {
243 		uint8_t instr;
244 		rwp = &CPU->cpu_ft_lock;
245 
246 		/*
247 		 * The DTrace fasttrap provider uses the breakpoint trap
248 		 * (int 3). We let DTrace take the first crack at handling
249 		 * this trap; if it's not a probe that DTrace knowns about,
250 		 * we call into the trap() routine to handle it like a
251 		 * breakpoint placed by a conventional debugger.
252 		 */
253 		rw_enter(rwp, RW_READER);
254 		if (dtrace_pid_probe_ptr != NULL &&
255 		    (*dtrace_pid_probe_ptr)(rp) == 0) {
256 			rw_exit(rwp);
257 			return;
258 		}
259 		rw_exit(rwp);
260 
261 		/*
262 		 * If the instruction that caused the breakpoint trap doesn't
263 		 * look like an int 3 anymore, it may be that this tracepoint
264 		 * was removed just after the user thread executed it. In
265 		 * that case, return to user land to retry the instuction.
266 		 */
267 		if (fuword8((void *)(rp->r_pc - 1), &instr) == 0 &&
268 		    instr != FASTTRAP_INSTR) {
269 			rp->r_pc--;
270 			return;
271 		}
272 
273 		trap(rp, addr, cpuid);
274 
275 	} else {
276 		trap(rp, addr, cpuid);
277 	}
278 }
279 
280 void
281 dtrace_safe_synchronous_signal(void)
282 {
283 	kthread_t *t = curthread;
284 	struct regs *rp = lwptoregs(ttolwp(t));
285 	size_t isz = t->t_dtrace_npc - t->t_dtrace_pc;
286 
287 	ASSERT(t->t_dtrace_on);
288 
289 	/*
290 	 * If we're not in the range of scratch addresses, we're not actually
291 	 * tracing user instructions so turn off the flags. If the instruction
292 	 * we copied out caused a synchonous trap, reset the pc back to its
293 	 * original value and turn off the flags.
294 	 */
295 	if (rp->r_pc < t->t_dtrace_scrpc ||
296 	    rp->r_pc > t->t_dtrace_astpc + isz) {
297 		t->t_dtrace_ft = 0;
298 	} else if (rp->r_pc == t->t_dtrace_scrpc ||
299 	    rp->r_pc == t->t_dtrace_astpc) {
300 		rp->r_pc = t->t_dtrace_pc;
301 		t->t_dtrace_ft = 0;
302 	}
303 }
304 
305 int
306 dtrace_safe_defer_signal(void)
307 {
308 	kthread_t *t = curthread;
309 	struct regs *rp = lwptoregs(ttolwp(t));
310 	size_t isz = t->t_dtrace_npc - t->t_dtrace_pc;
311 
312 	ASSERT(t->t_dtrace_on);
313 
314 	/*
315 	 * If we're not in the range of scratch addresses, we're not actually
316 	 * tracing user instructions so turn off the flags.
317 	 */
318 	if (rp->r_pc < t->t_dtrace_scrpc ||
319 	    rp->r_pc > t->t_dtrace_astpc + isz) {
320 		t->t_dtrace_ft = 0;
321 		return (0);
322 	}
323 
324 	/*
325 	 * If we've executed the original instruction, but haven't performed
326 	 * the jmp back to t->t_dtrace_npc or the clean up of any registers
327 	 * used to emulate %rip-relative instructions in 64-bit mode, do that
328 	 * here and take the signal right away. We detect this condition by
329 	 * seeing if the program counter is the range [scrpc + isz, astpc).
330 	 */
331 	if (t->t_dtrace_astpc - rp->r_pc <
332 	    t->t_dtrace_astpc - t->t_dtrace_scrpc - isz) {
333 #ifdef __amd64
334 		/*
335 		 * If there is a scratch register and we're on the
336 		 * instruction immediately after the modified instruction,
337 		 * restore the value of that scratch register.
338 		 */
339 		if (t->t_dtrace_reg != 0 &&
340 		    rp->r_pc == t->t_dtrace_scrpc + isz) {
341 			switch (t->t_dtrace_reg) {
342 			case REG_RAX:
343 				rp->r_rax = t->t_dtrace_regv;
344 				break;
345 			case REG_RCX:
346 				rp->r_rcx = t->t_dtrace_regv;
347 				break;
348 			case REG_R8:
349 				rp->r_r8 = t->t_dtrace_regv;
350 				break;
351 			case REG_R9:
352 				rp->r_r9 = t->t_dtrace_regv;
353 				break;
354 			}
355 		}
356 #endif
357 		rp->r_pc = t->t_dtrace_npc;
358 		t->t_dtrace_ft = 0;
359 		return (0);
360 	}
361 
362 	/*
363 	 * Otherwise, make sure we'll return to the kernel after executing
364 	 * the copied out instruction and defer the signal.
365 	 */
366 	if (!t->t_dtrace_step) {
367 		ASSERT(rp->r_pc < t->t_dtrace_astpc);
368 		rp->r_pc += t->t_dtrace_astpc - t->t_dtrace_scrpc;
369 		t->t_dtrace_step = 1;
370 	}
371 
372 	t->t_dtrace_ast = 1;
373 
374 	return (1);
375 }
376