xref: /titanic_44/usr/src/uts/sun4/os/dtrace_subr.c (revision 9df12a23948bd40cbe37ce88d84e272c3894e675)
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/atomic.h>
33 #include <sys/machsystm.h>
34 
35 static void
36 dtrace_xcall_func(uint64_t arg1, uint64_t arg2)
37 {
38 	(*(dtrace_xcall_t)arg1)((void *)(arg2));
39 }
40 
41 void
42 dtrace_xcall(processorid_t cpu, dtrace_xcall_t func, void *arg)
43 {
44 	if (cpu == DTRACE_CPUALL) {
45 		xc_all(dtrace_xcall_func, (uint64_t)func, (uint64_t)arg);
46 	} else {
47 		xc_one(cpu, dtrace_xcall_func, (uint64_t)func, (uint64_t)arg);
48 	}
49 }
50 
51 /*ARGSUSED*/
52 static void
53 dtrace_sync_func(uint64_t arg1, uint64_t arg2)
54 {
55 	membar_consumer();
56 }
57 
58 void
59 dtrace_sync(void)
60 {
61 	membar_producer();
62 	xc_all(dtrace_sync_func, 0, 0);
63 }
64 
65 void
66 dtrace_toxic_ranges(void (*func)(uintptr_t base, uintptr_t limit))
67 {
68 	(*func)(PIOMAPBASE, PIOMAPBASE + PIOMAPSIZE);
69 	(*func)(OFW_START_ADDR, OFW_END_ADDR);
70 
71 	if (hole_end > hole_start)
72 		(*func)((uintptr_t)hole_start, (uintptr_t)hole_end);
73 }
74 
75 int (*dtrace_fasttrap_probe_ptr)(struct regs *);
76 
77 void
78 dtrace_fasttrap_probe(struct regs *rp)
79 {
80 	krwlock_t *rwp = &CPU->cpu_ft_lock;
81 
82 	rw_enter(rwp, RW_READER);
83 	if (dtrace_fasttrap_probe_ptr == NULL) {
84 		rw_exit(rwp);
85 		rp->r_pc = rp->r_npc;
86 		rp->r_npc = rp->r_pc + 4;
87 	} else {
88 		(void) (*dtrace_fasttrap_probe_ptr)(rp);
89 		rw_exit(rwp);
90 	}
91 }
92 
93 int (*dtrace_pid_probe_ptr)(struct regs *);
94 
95 void
96 dtrace_pid_probe(struct regs *rp)
97 {
98 	krwlock_t *rwp = &CPU->cpu_ft_lock;
99 	uint32_t instr;
100 
101 	/*
102 	 * This trap should only be invoked if there's a corresponding
103 	 * enabled dtrace probe. If there isn't, send SIGILL as though
104 	 * the process had executed an invalid trap instruction.
105 	 */
106 	rw_enter(rwp, RW_READER);
107 	if (dtrace_pid_probe_ptr != NULL && (*dtrace_pid_probe_ptr)(rp) == 0) {
108 		rw_exit(rwp);
109 		return;
110 	}
111 	rw_exit(rwp);
112 
113 	/*
114 	 * It is possible that we were preempted after entering the kernel,
115 	 * and the tracepoint was removed. If it appears that the process hit
116 	 * our reserved trap instruction, we call send SIGILL just as though
117 	 * the user had executed an unused trap instruction.
118 	 */
119 	if (fuword32((void *)rp->r_pc, &instr) != 0 ||
120 	    instr == FASTTRAP_INSTR) {
121 		sigqueue_t *sqp = kmem_zalloc(sizeof (sigqueue_t), KM_SLEEP);
122 		proc_t *p = curproc;
123 
124 		sqp->sq_info.si_signo = SIGILL;
125 		sqp->sq_info.si_code = ILL_ILLTRP;
126 		sqp->sq_info.si_addr = (caddr_t)rp->r_pc;
127 		sqp->sq_info.si_trapno = 0x38;
128 
129 		mutex_enter(&p->p_lock);
130 		sigaddqa(p, curthread, sqp);
131 		mutex_exit(&p->p_lock);
132 		aston(curthread);
133 	}
134 }
135 
136 int (*dtrace_return_probe_ptr)(struct regs *);
137 
138 void
139 dtrace_return_probe(struct regs *rp)
140 {
141 	krwlock_t *rwp;
142 	uintptr_t npc = curthread->t_dtrace_npc;
143 	uint8_t step = curthread->t_dtrace_step;
144 	uint8_t ret = curthread->t_dtrace_ret;
145 
146 	if (curthread->t_dtrace_ast) {
147 		aston(curthread);
148 		curthread->t_sig_check = 1;
149 	}
150 
151 	/*
152 	 * Clear all user tracing flags.
153 	 */
154 	curthread->t_dtrace_ft = 0;
155 
156 	/*
157 	 * If we weren't expecting to take a return probe trap, kill the
158 	 * process as though it had just executed an unassigned trap
159 	 * instruction.
160 	 */
161 	if (step == 0) {
162 		tsignal(curthread, SIGILL);
163 		return;
164 	}
165 
166 	ASSERT(rp->r_npc == rp->r_pc + 4);
167 
168 	/*
169 	 * If we hit this trap unrelated to a return probe, we're just here
170 	 * to reset the AST flag since we deferred a signal until after we
171 	 * logically single-stepped the instruction we copied out.
172 	 */
173 	if (ret == 0) {
174 		rp->r_pc = npc;
175 		rp->r_npc = npc + 4;
176 		return;
177 	}
178 
179 	/*
180 	 * We need to wait until after we've called the dtrace_return_probe_ptr
181 	 * function pointer to set %pc and %npc.
182 	 */
183 	rwp = &CPU->cpu_ft_lock;
184 	rw_enter(rwp, RW_READER);
185 	if (dtrace_return_probe_ptr != NULL)
186 		(void) (*dtrace_return_probe_ptr)(rp);
187 	rw_exit(rwp);
188 	rp->r_pc = npc;
189 	rp->r_npc = npc + 4;
190 }
191 
192 void
193 dtrace_safe_synchronous_signal(void)
194 {
195 	kthread_t *t = curthread;
196 	struct regs *rp = lwptoregs(ttolwp(t));
197 
198 	ASSERT(t->t_dtrace_on);
199 
200 	/*
201 	 * If we're not actively tracing an instruction, turn off tracing
202 	 * flags. If the instruction we copied out caused a synchronous
203 	 * trap, reset the pc and npc back to their original values and turn
204 	 * off the flags.
205 	 */
206 	if (rp->r_pc != t->t_dtrace_scrpc && rp->r_pc != t->t_dtrace_astpc &&
207 	    rp->r_npc != t->t_dtrace_astpc) {
208 		t->t_dtrace_ft = 0;
209 	} else if (rp->r_pc == t->t_dtrace_scrpc) {
210 		rp->r_pc = t->t_dtrace_pc;
211 		rp->r_npc = t->t_dtrace_npc;
212 		t->t_dtrace_ft = 0;
213 	}
214 }
215 
216 int
217 dtrace_safe_defer_signal(void)
218 {
219 	kthread_t *t = curthread;
220 	struct regs *rp = lwptoregs(ttolwp(t));
221 
222 	ASSERT(t->t_dtrace_on);
223 
224 	/*
225 	 * If we're not actively tracing an instruction, turn off tracing
226 	 * flags.
227 	 */
228 	if (rp->r_pc != t->t_dtrace_scrpc && rp->r_pc != t->t_dtrace_astpc &&
229 	    rp->r_npc != t->t_dtrace_astpc) {
230 		t->t_dtrace_ft = 0;
231 		return (0);
232 	}
233 
234 	/*
235 	 * Otherwise, make sure we'll return to the kernel after executing
236 	 * the instruction we copied out.
237 	 */
238 	if (!t->t_dtrace_step) {
239 		ASSERT(rp->r_pc == t->t_dtrace_scrpc);
240 		rp->r_npc = t->t_dtrace_astpc;
241 		t->t_dtrace_step = 1;
242 	}
243 
244 	t->t_dtrace_ast = 1;
245 
246 	return (1);
247 }
248