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
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.
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]
26 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
27 * Use is subject to license terms.
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/types.h>
33 #include <sys/kernel.h>
34 #include <sys/malloc.h>
37 #include <sys/dtrace_impl.h>
38 #include <sys/dtrace_bsd.h>
39 #include <machine/clock.h>
40 #include <machine/frame.h>
43 extern uintptr_t kernelbase;
44 extern uintptr_t dtrace_in_probe_addr;
45 extern int dtrace_in_probe;
47 int dtrace_invop(uintptr_t, uintptr_t *, uintptr_t);
49 typedef struct dtrace_invop_hdlr {
50 int (*dtih_func)(uintptr_t, uintptr_t *, uintptr_t);
51 struct dtrace_invop_hdlr *dtih_next;
52 } dtrace_invop_hdlr_t;
54 dtrace_invop_hdlr_t *dtrace_invop_hdlr;
57 dtrace_invop(uintptr_t addr, uintptr_t *stack, uintptr_t eax)
59 dtrace_invop_hdlr_t *hdlr;
62 for (hdlr = dtrace_invop_hdlr; hdlr != NULL; hdlr = hdlr->dtih_next)
63 if ((rval = hdlr->dtih_func(addr, stack, eax)) != 0)
70 dtrace_invop_add(int (*func)(uintptr_t, uintptr_t *, uintptr_t))
72 dtrace_invop_hdlr_t *hdlr;
74 hdlr = kmem_alloc(sizeof (dtrace_invop_hdlr_t), KM_SLEEP);
75 hdlr->dtih_func = func;
76 hdlr->dtih_next = dtrace_invop_hdlr;
77 dtrace_invop_hdlr = hdlr;
81 dtrace_invop_remove(int (*func)(uintptr_t, uintptr_t *, uintptr_t))
83 dtrace_invop_hdlr_t *hdlr = dtrace_invop_hdlr, *prev = NULL;
87 panic("attempt to remove non-existent invop handler");
89 if (hdlr->dtih_func == func)
93 hdlr = hdlr->dtih_next;
97 ASSERT(dtrace_invop_hdlr == hdlr);
98 dtrace_invop_hdlr = hdlr->dtih_next;
100 ASSERT(dtrace_invop_hdlr != hdlr);
101 prev->dtih_next = hdlr->dtih_next;
108 dtrace_toxic_ranges(void (*func)(uintptr_t base, uintptr_t limit))
110 (*func)(0, kernelbase);
114 dtrace_xcall(processorid_t cpu, dtrace_xcall_t func, void *arg)
120 if (cpu == DTRACE_CPUALL)
123 cpus = (cpumask_t) (1 << cpu);
125 /* If the current CPU is in the set, call the function directly: */
126 if ((cpus & (1 << curcpu)) != 0) {
129 /* Mask the current CPU from the set */
130 cpus &= ~(1 << curcpu);
133 /* If there are any CPUs in the set, cross-call to those CPUs */
135 smp_rendezvous_cpus(cpus, NULL, func, smp_no_rendevous_barrier, arg);
141 dtrace_sync_func(void)
148 dtrace_xcall(DTRACE_CPUALL, (dtrace_xcall_t)dtrace_sync_func, NULL);
152 int (*dtrace_fasttrap_probe_ptr)(struct regs *);
153 int (*dtrace_pid_probe_ptr)(struct regs *);
154 int (*dtrace_return_probe_ptr)(struct regs *);
157 dtrace_user_probe(struct regs *rp, caddr_t addr, processorid_t cpuid)
161 extern void trap(struct regs *, caddr_t, processorid_t);
163 if (USERMODE(rp->r_cs) || (rp->r_ps & PS_VM)) {
164 if (curthread->t_cred != p->p_cred) {
165 cred_t *oldcred = curthread->t_cred;
167 * DTrace accesses t_cred in probe context. t_cred
168 * must always be either NULL, or point to a valid,
169 * allocated cred structure.
171 curthread->t_cred = crgetcred();
176 if (rp->r_trapno == T_DTRACE_RET) {
177 uint8_t step = curthread->t_dtrace_step;
178 uint8_t ret = curthread->t_dtrace_ret;
179 uintptr_t npc = curthread->t_dtrace_npc;
181 if (curthread->t_dtrace_ast) {
183 curthread->t_sig_check = 1;
187 * Clear all user tracing flags.
189 curthread->t_dtrace_ft = 0;
192 * If we weren't expecting to take a return probe trap, kill
193 * the process as though it had just executed an unassigned
197 tsignal(curthread, SIGILL);
202 * If we hit this trap unrelated to a return probe, we're
203 * just here to reset the AST flag since we deferred a signal
204 * until after we logically single-stepped the instruction we
213 * We need to wait until after we've called the
214 * dtrace_return_probe_ptr function pointer to set %pc.
216 rwp = &CPU->cpu_ft_lock;
217 rw_enter(rwp, RW_READER);
218 if (dtrace_return_probe_ptr != NULL)
219 (void) (*dtrace_return_probe_ptr)(rp);
223 } else if (rp->r_trapno == T_DTRACE_PROBE) {
224 rwp = &CPU->cpu_ft_lock;
225 rw_enter(rwp, RW_READER);
226 if (dtrace_fasttrap_probe_ptr != NULL)
227 (void) (*dtrace_fasttrap_probe_ptr)(rp);
230 } else if (rp->r_trapno == T_BPTFLT) {
232 rwp = &CPU->cpu_ft_lock;
235 * The DTrace fasttrap provider uses the breakpoint trap
236 * (int 3). We let DTrace take the first crack at handling
237 * this trap; if it's not a probe that DTrace knowns about,
238 * we call into the trap() routine to handle it like a
239 * breakpoint placed by a conventional debugger.
241 rw_enter(rwp, RW_READER);
242 if (dtrace_pid_probe_ptr != NULL &&
243 (*dtrace_pid_probe_ptr)(rp) == 0) {
250 * If the instruction that caused the breakpoint trap doesn't
251 * look like an int 3 anymore, it may be that this tracepoint
252 * was removed just after the user thread executed it. In
253 * that case, return to user land to retry the instuction.
255 if (fuword8((void *)(rp->r_pc - 1), &instr) == 0 &&
256 instr != FASTTRAP_INSTR) {
261 trap(rp, addr, cpuid);
264 trap(rp, addr, cpuid);
269 dtrace_safe_synchronous_signal(void)
271 kthread_t *t = curthread;
272 struct regs *rp = lwptoregs(ttolwp(t));
273 size_t isz = t->t_dtrace_npc - t->t_dtrace_pc;
275 ASSERT(t->t_dtrace_on);
278 * If we're not in the range of scratch addresses, we're not actually
279 * tracing user instructions so turn off the flags. If the instruction
280 * we copied out caused a synchonous trap, reset the pc back to its
281 * original value and turn off the flags.
283 if (rp->r_pc < t->t_dtrace_scrpc ||
284 rp->r_pc > t->t_dtrace_astpc + isz) {
286 } else if (rp->r_pc == t->t_dtrace_scrpc ||
287 rp->r_pc == t->t_dtrace_astpc) {
288 rp->r_pc = t->t_dtrace_pc;
294 dtrace_safe_defer_signal(void)
296 kthread_t *t = curthread;
297 struct regs *rp = lwptoregs(ttolwp(t));
298 size_t isz = t->t_dtrace_npc - t->t_dtrace_pc;
300 ASSERT(t->t_dtrace_on);
303 * If we're not in the range of scratch addresses, we're not actually
304 * tracing user instructions so turn off the flags.
306 if (rp->r_pc < t->t_dtrace_scrpc ||
307 rp->r_pc > t->t_dtrace_astpc + isz) {
313 * If we've executed the original instruction, but haven't performed
314 * the jmp back to t->t_dtrace_npc or the clean up of any registers
315 * used to emulate %rip-relative instructions in 64-bit mode, do that
316 * here and take the signal right away. We detect this condition by
317 * seeing if the program counter is the range [scrpc + isz, astpc).
319 if (t->t_dtrace_astpc - rp->r_pc <
320 t->t_dtrace_astpc - t->t_dtrace_scrpc - isz) {
323 * If there is a scratch register and we're on the
324 * instruction immediately after the modified instruction,
325 * restore the value of that scratch register.
327 if (t->t_dtrace_reg != 0 &&
328 rp->r_pc == t->t_dtrace_scrpc + isz) {
329 switch (t->t_dtrace_reg) {
331 rp->r_rax = t->t_dtrace_regv;
334 rp->r_rcx = t->t_dtrace_regv;
337 rp->r_r8 = t->t_dtrace_regv;
340 rp->r_r9 = t->t_dtrace_regv;
345 rp->r_pc = t->t_dtrace_npc;
351 * Otherwise, make sure we'll return to the kernel after executing
352 * the copied out instruction and defer the signal.
354 if (!t->t_dtrace_step) {
355 ASSERT(rp->r_pc < t->t_dtrace_astpc);
356 rp->r_pc += t->t_dtrace_astpc - t->t_dtrace_scrpc;
357 t->t_dtrace_step = 1;
366 static int64_t tgt_cpu_tsc;
367 static int64_t hst_cpu_tsc;
368 static int64_t tsc_skew[MAXCPU];
371 dtrace_gethrtime_init_sync(void *arg)
375 * Delay this function from returning on one
376 * of the CPUs to check that the synchronisation
379 uintptr_t cpu = (uintptr_t) arg;
383 for (i = 0; i < 1000000000; i++)
384 tgt_cpu_tsc = rdtsc();
391 dtrace_gethrtime_init_cpu(void *arg)
393 uintptr_t cpu = (uintptr_t) arg;
396 tgt_cpu_tsc = rdtsc();
398 hst_cpu_tsc = rdtsc();
402 dtrace_gethrtime_init(void *arg)
408 /* The current CPU is the reference one. */
409 tsc_skew[curcpu] = 0;
411 for (i = 0; i <= mp_maxid; i++) {
415 if ((cp = pcpu_find(i)) == NULL)
419 map |= (1 << curcpu);
422 smp_rendezvous_cpus(map, dtrace_gethrtime_init_sync,
423 dtrace_gethrtime_init_cpu,
424 smp_no_rendevous_barrier, (void *)(uintptr_t) i);
426 tsc_skew[i] = tgt_cpu_tsc - hst_cpu_tsc;
430 SYSINIT(dtrace_gethrtime_init, SI_SUB_SMP, SI_ORDER_ANY, dtrace_gethrtime_init, NULL);
433 * DTrace needs a high resolution time function which can
434 * be called from a probe context and guaranteed not to have
435 * instrumented with probes itself.
437 * Returns nanoseconds since boot.
442 return ((rdtsc() + tsc_skew[curcpu]) * (int64_t) 1000000000 / tsc_freq);
446 dtrace_gethrestime(void)
448 printf("%s(%d): XXX\n",__func__,__LINE__);
452 /* Function to handle DTrace traps during probes. See i386/i386/trap.c */
454 dtrace_trap(struct trapframe *frame, u_int type)
457 * A trap can occur while DTrace executes a probe. Before
458 * executing the probe, DTrace blocks re-scheduling and sets
459 * a flag in it's per-cpu flags to indicate that it doesn't
460 * want to fault. On returning from the the probe, the no-fault
461 * flag is cleared and finally re-scheduling is enabled.
463 * Check if DTrace has enabled 'no-fault' mode:
466 if ((cpu_core[curcpu].cpuc_dtrace_flags & CPU_DTRACE_NOFAULT) != 0) {
468 * There are only a couple of trap types that are expected.
469 * All the rest will be handled in the usual way.
472 /* General protection fault. */
474 /* Flag an illegal operation. */
475 cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP;
478 * Offset the instruction pointer to the instruction
479 * following the one causing the fault.
481 frame->tf_eip += dtrace_instr_size((u_char *) frame->tf_eip);
485 /* Flag a bad address. */
486 cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_BADADDR;
487 cpu_core[curcpu].cpuc_dtrace_illval = rcr2();
490 * Offset the instruction pointer to the instruction
491 * following the one causing the fault.
493 frame->tf_eip += dtrace_instr_size((u_char *) frame->tf_eip);
496 /* Handle all other traps in the usual way. */
501 /* Handle the trap in the usual way. */