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.
31 * Copyright (c) 2011, Joyent, Inc. All rights reserved.
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/types.h>
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
41 #include <sys/dtrace_impl.h>
42 #include <sys/dtrace_bsd.h>
43 #include <machine/clock.h>
44 #include <machine/cpufunc.h>
45 #include <machine/frame.h>
46 #include <machine/md_var.h>
47 #include <machine/psl.h>
48 #include <machine/trap.h>
51 extern void dtrace_getnanotime(struct timespec *tsp);
52 extern int (*dtrace_invop_jump_addr)(struct trapframe *);
54 int dtrace_invop(uintptr_t, struct trapframe *, uintptr_t);
55 int dtrace_invop_start(struct trapframe *frame);
56 void dtrace_invop_init(void);
57 void dtrace_invop_uninit(void);
59 typedef struct dtrace_invop_hdlr {
60 int (*dtih_func)(uintptr_t, struct trapframe *, uintptr_t);
61 struct dtrace_invop_hdlr *dtih_next;
62 } dtrace_invop_hdlr_t;
64 dtrace_invop_hdlr_t *dtrace_invop_hdlr;
67 dtrace_invop(uintptr_t addr, struct trapframe *frame, uintptr_t eax)
69 dtrace_invop_hdlr_t *hdlr;
72 for (hdlr = dtrace_invop_hdlr; hdlr != NULL; hdlr = hdlr->dtih_next)
73 if ((rval = hdlr->dtih_func(addr, frame, eax)) != 0)
80 dtrace_invop_add(int (*func)(uintptr_t, struct trapframe *, uintptr_t))
82 dtrace_invop_hdlr_t *hdlr;
84 hdlr = kmem_alloc(sizeof (dtrace_invop_hdlr_t), KM_SLEEP);
85 hdlr->dtih_func = func;
86 hdlr->dtih_next = dtrace_invop_hdlr;
87 dtrace_invop_hdlr = hdlr;
91 dtrace_invop_remove(int (*func)(uintptr_t, struct trapframe *, uintptr_t))
93 dtrace_invop_hdlr_t *hdlr = dtrace_invop_hdlr, *prev = NULL;
97 panic("attempt to remove non-existent invop handler");
99 if (hdlr->dtih_func == func)
103 hdlr = hdlr->dtih_next;
107 ASSERT(dtrace_invop_hdlr == hdlr);
108 dtrace_invop_hdlr = hdlr->dtih_next;
110 ASSERT(dtrace_invop_hdlr != hdlr);
111 prev->dtih_next = hdlr->dtih_next;
118 dtrace_invop_init(void)
121 dtrace_invop_jump_addr = dtrace_invop_start;
125 dtrace_invop_uninit(void)
128 dtrace_invop_jump_addr = NULL;
133 dtrace_toxic_ranges(void (*func)(uintptr_t base, uintptr_t limit))
135 (*func)(0, la57 ? (uintptr_t)addr_P5Tmap : (uintptr_t)addr_P4Tmap);
139 dtrace_xcall(processorid_t cpu, dtrace_xcall_t func, void *arg)
143 if (cpu == DTRACE_CPUALL)
146 CPU_SETOF(cpu, &cpus);
148 smp_rendezvous_cpus(cpus, smp_no_rendezvous_barrier, func,
149 smp_no_rendezvous_barrier, arg);
153 dtrace_sync_func(void)
160 dtrace_xcall(DTRACE_CPUALL, (dtrace_xcall_t)dtrace_sync_func, NULL);
165 dtrace_safe_synchronous_signal(void)
167 kthread_t *t = curthread;
168 struct regs *rp = lwptoregs(ttolwp(t));
169 size_t isz = t->t_dtrace_npc - t->t_dtrace_pc;
171 ASSERT(t->t_dtrace_on);
174 * If we're not in the range of scratch addresses, we're not actually
175 * tracing user instructions so turn off the flags. If the instruction
176 * we copied out caused a synchonous trap, reset the pc back to its
177 * original value and turn off the flags.
179 if (rp->r_pc < t->t_dtrace_scrpc ||
180 rp->r_pc > t->t_dtrace_astpc + isz) {
182 } else if (rp->r_pc == t->t_dtrace_scrpc ||
183 rp->r_pc == t->t_dtrace_astpc) {
184 rp->r_pc = t->t_dtrace_pc;
190 dtrace_safe_defer_signal(void)
192 kthread_t *t = curthread;
193 struct regs *rp = lwptoregs(ttolwp(t));
194 size_t isz = t->t_dtrace_npc - t->t_dtrace_pc;
196 ASSERT(t->t_dtrace_on);
199 * If we're not in the range of scratch addresses, we're not actually
200 * tracing user instructions so turn off the flags.
202 if (rp->r_pc < t->t_dtrace_scrpc ||
203 rp->r_pc > t->t_dtrace_astpc + isz) {
209 * If we have executed the original instruction, but we have performed
210 * neither the jmp back to t->t_dtrace_npc nor the clean up of any
211 * registers used to emulate %rip-relative instructions in 64-bit mode,
212 * we'll save ourselves some effort by doing that here and taking the
213 * signal right away. We detect this condition by seeing if the program
214 * counter is the range [scrpc + isz, astpc).
216 if (rp->r_pc >= t->t_dtrace_scrpc + isz &&
217 rp->r_pc < t->t_dtrace_astpc) {
220 * If there is a scratch register and we're on the
221 * instruction immediately after the modified instruction,
222 * restore the value of that scratch register.
224 if (t->t_dtrace_reg != 0 &&
225 rp->r_pc == t->t_dtrace_scrpc + isz) {
226 switch (t->t_dtrace_reg) {
228 rp->r_rax = t->t_dtrace_regv;
231 rp->r_rcx = t->t_dtrace_regv;
234 rp->r_r8 = t->t_dtrace_regv;
237 rp->r_r9 = t->t_dtrace_regv;
242 rp->r_pc = t->t_dtrace_npc;
248 * Otherwise, make sure we'll return to the kernel after executing
249 * the copied out instruction and defer the signal.
251 if (!t->t_dtrace_step) {
252 ASSERT(rp->r_pc < t->t_dtrace_astpc);
253 rp->r_pc += t->t_dtrace_astpc - t->t_dtrace_scrpc;
254 t->t_dtrace_step = 1;
263 static int64_t tgt_cpu_tsc;
264 static int64_t hst_cpu_tsc;
265 static int64_t tsc_skew[MAXCPU];
266 static uint64_t nsec_scale;
268 /* See below for the explanation of this macro. */
269 #define SCALE_SHIFT 28
272 dtrace_gethrtime_init_cpu(void *arg)
274 uintptr_t cpu = (uintptr_t) arg;
277 tgt_cpu_tsc = rdtsc();
279 hst_cpu_tsc = rdtsc();
282 #ifdef EARLY_AP_STARTUP
284 dtrace_gethrtime_init(void *arg)
292 * Get the frequency and scale factor as early as possible so that they can be
293 * used for boot-time tracing.
296 dtrace_gethrtime_init_early(void *arg)
302 * Get TSC frequency known at this moment.
303 * This should be constant if TSC is invariant.
304 * Otherwise tick->time conversion will be inaccurate, but
305 * will preserve monotonic property of TSC.
307 tsc_f = atomic_load_acq_64(&tsc_freq);
310 * The following line checks that nsec_scale calculated below
311 * doesn't overflow 32-bit unsigned integer, so that it can multiply
312 * another 32-bit integer without overflowing 64-bit.
313 * Thus minimum supported TSC frequency is 62.5MHz.
315 KASSERT(tsc_f > (NANOSEC >> (32 - SCALE_SHIFT)),
316 ("TSC frequency is too low"));
319 * We scale up NANOSEC/tsc_f ratio to preserve as much precision
321 * 2^28 factor was chosen quite arbitrarily from practical
323 * - it supports TSC frequencies as low as 62.5MHz (see above);
324 * - it provides quite good precision (e < 0.01%) up to THz
325 * (terahertz) values;
327 nsec_scale = ((uint64_t)NANOSEC << SCALE_SHIFT) / tsc_f;
328 #ifndef EARLY_AP_STARTUP
330 SYSINIT(dtrace_gethrtime_init_early, SI_SUB_CPU, SI_ORDER_ANY,
331 dtrace_gethrtime_init_early, NULL);
334 dtrace_gethrtime_init(void *arg)
341 if (vm_guest != VM_GUEST_NO)
344 /* The current CPU is the reference one. */
346 tsc_skew[curcpu] = 0;
352 CPU_SETOF(PCPU_GET(cpuid), &map);
353 CPU_SET(pc->pc_cpuid, &map);
355 smp_rendezvous_cpus(map, NULL,
356 dtrace_gethrtime_init_cpu,
357 smp_no_rendezvous_barrier, (void *)(uintptr_t) i);
359 tsc_skew[i] = tgt_cpu_tsc - hst_cpu_tsc;
363 #ifdef EARLY_AP_STARTUP
364 SYSINIT(dtrace_gethrtime_init, SI_SUB_DTRACE, SI_ORDER_ANY,
365 dtrace_gethrtime_init, NULL);
367 SYSINIT(dtrace_gethrtime_init, SI_SUB_SMP, SI_ORDER_ANY, dtrace_gethrtime_init,
372 * DTrace needs a high resolution time function which can
373 * be called from a probe context and guaranteed not to have
374 * instrumented with probes itself.
376 * Returns nanoseconds since boot.
379 dtrace_gethrtime(void)
386 * We split TSC value into lower and higher 32-bit halves and separately
387 * scale them with nsec_scale, then we scale them down by 2^28
388 * (see nsec_scale calculations) taking into account 32-bit shift of
389 * the higher half and finally add.
391 rflags = intr_disable();
392 tsc = rdtsc() - tsc_skew[curcpu];
393 intr_restore(rflags);
397 return (((lo * nsec_scale) >> SCALE_SHIFT) +
398 ((hi * nsec_scale) << (32 - SCALE_SHIFT)));
402 dtrace_gethrestime(void)
404 struct timespec current_time;
406 dtrace_getnanotime(¤t_time);
408 return (current_time.tv_sec * 1000000000ULL + current_time.tv_nsec);
411 /* Function to handle DTrace traps during probes. See amd64/amd64/trap.c. */
413 dtrace_trap(struct trapframe *frame, u_int type)
418 * A trap can occur while DTrace executes a probe. Before
419 * executing the probe, DTrace blocks re-scheduling and sets
420 * a flag in its per-cpu flags to indicate that it doesn't
421 * want to fault. On returning from the probe, the no-fault
422 * flag is cleared and finally re-scheduling is enabled.
424 * Check if DTrace has enabled 'no-fault' mode:
427 nofault = cpu_core[curcpu].cpuc_dtrace_flags & CPU_DTRACE_NOFAULT;
430 KASSERT((read_rflags() & PSL_I) == 0, ("interrupts enabled"));
433 * There are only a couple of trap types that are expected.
434 * All the rest will be handled in the usual way.
437 /* General protection fault. */
439 /* Flag an illegal operation. */
440 cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP;
443 * Offset the instruction pointer to the instruction
444 * following the one causing the fault.
446 frame->tf_rip += dtrace_instr_size((u_char *) frame->tf_rip);
450 /* Flag a bad address. */
451 cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_BADADDR;
452 cpu_core[curcpu].cpuc_dtrace_illval = frame->tf_addr;
455 * Offset the instruction pointer to the instruction
456 * following the one causing the fault.
458 frame->tf_rip += dtrace_instr_size((u_char *) frame->tf_rip);
461 /* Handle all other traps in the usual way. */
466 /* Handle the trap in the usual way. */