xref: /freebsd/sys/cddl/dev/dtrace/amd64/dtrace_subr.c (revision 1a149d65baed31b90a0b8abe2d6c5a568122a5ec)
191eaf3e1SJohn Birrell /*
291eaf3e1SJohn Birrell  * CDDL HEADER START
391eaf3e1SJohn Birrell  *
491eaf3e1SJohn Birrell  * The contents of this file are subject to the terms of the
591eaf3e1SJohn Birrell  * Common Development and Distribution License, Version 1.0 only
691eaf3e1SJohn Birrell  * (the "License").  You may not use this file except in compliance
791eaf3e1SJohn Birrell  * with the License.
891eaf3e1SJohn Birrell  *
991eaf3e1SJohn Birrell  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
1091eaf3e1SJohn Birrell  * or http://www.opensolaris.org/os/licensing.
1191eaf3e1SJohn Birrell  * See the License for the specific language governing permissions
1291eaf3e1SJohn Birrell  * and limitations under the License.
1391eaf3e1SJohn Birrell  *
1491eaf3e1SJohn Birrell  * When distributing Covered Code, include this CDDL HEADER in each
1591eaf3e1SJohn Birrell  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1691eaf3e1SJohn Birrell  * If applicable, add the following below this CDDL HEADER, with the
1791eaf3e1SJohn Birrell  * fields enclosed by brackets "[]" replaced with your own identifying
1891eaf3e1SJohn Birrell  * information: Portions Copyright [yyyy] [name of copyright owner]
1991eaf3e1SJohn Birrell  *
2091eaf3e1SJohn Birrell  * CDDL HEADER END
2191eaf3e1SJohn Birrell  *
2291eaf3e1SJohn Birrell  * $FreeBSD$
2391eaf3e1SJohn Birrell  *
2491eaf3e1SJohn Birrell  */
2591eaf3e1SJohn Birrell /*
2691eaf3e1SJohn Birrell  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
2791eaf3e1SJohn Birrell  * Use is subject to license terms.
2891eaf3e1SJohn Birrell  */
2991eaf3e1SJohn Birrell 
304737d389SGeorge V. Neville-Neil /*
314737d389SGeorge V. Neville-Neil  * Copyright (c) 2011, Joyent, Inc. All rights reserved.
324737d389SGeorge V. Neville-Neil  */
334737d389SGeorge V. Neville-Neil 
3491eaf3e1SJohn Birrell #include <sys/param.h>
3591eaf3e1SJohn Birrell #include <sys/systm.h>
3691eaf3e1SJohn Birrell #include <sys/kernel.h>
3791eaf3e1SJohn Birrell #include <sys/malloc.h>
38bdd101c4SMark Johnston #include <sys/proc.h>
3991eaf3e1SJohn Birrell #include <sys/smp.h>
4091eaf3e1SJohn Birrell #include <sys/dtrace_impl.h>
4191eaf3e1SJohn Birrell #include <sys/dtrace_bsd.h>
42bdd101c4SMark Johnston #include <cddl/dev/dtrace/dtrace_cddl.h>
4391eaf3e1SJohn Birrell #include <machine/clock.h>
447174af79SMark Johnston #include <machine/cpufunc.h>
4591eaf3e1SJohn Birrell #include <machine/frame.h>
469ce875d9SKonstantin Belousov #include <machine/md_var.h>
477174af79SMark Johnston #include <machine/psl.h>
48d41e41f9SJohn Baldwin #include <machine/trap.h>
4991eaf3e1SJohn Birrell #include <vm/pmap.h>
5091eaf3e1SJohn Birrell 
5157d025c3SGeorge V. Neville-Neil extern void dtrace_getnanotime(struct timespec *tsp);
529093dd9aSMark Johnston extern int (*dtrace_invop_jump_addr)(struct trapframe *);
5357d025c3SGeorge V. Neville-Neil 
543ba8e9dcSMark Johnston int	dtrace_invop(uintptr_t, struct trapframe *, void **);
559093dd9aSMark Johnston int	dtrace_invop_start(struct trapframe *frame);
569093dd9aSMark Johnston void	dtrace_invop_init(void);
579093dd9aSMark Johnston void	dtrace_invop_uninit(void);
5891eaf3e1SJohn Birrell 
5991eaf3e1SJohn Birrell typedef struct dtrace_invop_hdlr {
606c280659SMark Johnston 	int (*dtih_func)(uintptr_t, struct trapframe *, uintptr_t);
6191eaf3e1SJohn Birrell 	struct dtrace_invop_hdlr *dtih_next;
6291eaf3e1SJohn Birrell } dtrace_invop_hdlr_t;
6391eaf3e1SJohn Birrell 
6491eaf3e1SJohn Birrell dtrace_invop_hdlr_t *dtrace_invop_hdlr;
6591eaf3e1SJohn Birrell 
6691eaf3e1SJohn Birrell int
673ba8e9dcSMark Johnston dtrace_invop(uintptr_t addr, struct trapframe *frame, void **scratch)
6891eaf3e1SJohn Birrell {
69bdd101c4SMark Johnston 	struct thread *td;
7091eaf3e1SJohn Birrell 	dtrace_invop_hdlr_t *hdlr;
7191eaf3e1SJohn Birrell 	int rval;
7291eaf3e1SJohn Birrell 
73bdd101c4SMark Johnston 	td = curthread;
74bdd101c4SMark Johnston 	td->t_dtrace_trapframe = frame;
75bdd101c4SMark Johnston 	rval = 0;
763ba8e9dcSMark Johnston 	for (hdlr = dtrace_invop_hdlr; hdlr != NULL; hdlr = hdlr->dtih_next) {
773ba8e9dcSMark Johnston 		rval = hdlr->dtih_func(addr, frame, (uintptr_t)scratch);
783ba8e9dcSMark Johnston 		if (rval != 0)
79bdd101c4SMark Johnston 			break;
803ba8e9dcSMark Johnston 	}
81bdd101c4SMark Johnston 	td->t_dtrace_trapframe = NULL;
82bdd101c4SMark Johnston 	return (rval);
8391eaf3e1SJohn Birrell }
8491eaf3e1SJohn Birrell 
8591eaf3e1SJohn Birrell void
866c280659SMark Johnston dtrace_invop_add(int (*func)(uintptr_t, struct trapframe *, uintptr_t))
8791eaf3e1SJohn Birrell {
8891eaf3e1SJohn Birrell 	dtrace_invop_hdlr_t *hdlr;
8991eaf3e1SJohn Birrell 
9091eaf3e1SJohn Birrell 	hdlr = kmem_alloc(sizeof (dtrace_invop_hdlr_t), KM_SLEEP);
9191eaf3e1SJohn Birrell 	hdlr->dtih_func = func;
9291eaf3e1SJohn Birrell 	hdlr->dtih_next = dtrace_invop_hdlr;
9391eaf3e1SJohn Birrell 	dtrace_invop_hdlr = hdlr;
9491eaf3e1SJohn Birrell }
9591eaf3e1SJohn Birrell 
9691eaf3e1SJohn Birrell void
976c280659SMark Johnston dtrace_invop_remove(int (*func)(uintptr_t, struct trapframe *, uintptr_t))
9891eaf3e1SJohn Birrell {
9991eaf3e1SJohn Birrell 	dtrace_invop_hdlr_t *hdlr = dtrace_invop_hdlr, *prev = NULL;
10091eaf3e1SJohn Birrell 
10191eaf3e1SJohn Birrell 	for (;;) {
10291eaf3e1SJohn Birrell 		if (hdlr == NULL)
10391eaf3e1SJohn Birrell 			panic("attempt to remove non-existent invop handler");
10491eaf3e1SJohn Birrell 
10591eaf3e1SJohn Birrell 		if (hdlr->dtih_func == func)
10691eaf3e1SJohn Birrell 			break;
10791eaf3e1SJohn Birrell 
10891eaf3e1SJohn Birrell 		prev = hdlr;
10991eaf3e1SJohn Birrell 		hdlr = hdlr->dtih_next;
11091eaf3e1SJohn Birrell 	}
11191eaf3e1SJohn Birrell 
11291eaf3e1SJohn Birrell 	if (prev == NULL) {
11391eaf3e1SJohn Birrell 		ASSERT(dtrace_invop_hdlr == hdlr);
11491eaf3e1SJohn Birrell 		dtrace_invop_hdlr = hdlr->dtih_next;
11591eaf3e1SJohn Birrell 	} else {
11691eaf3e1SJohn Birrell 		ASSERT(dtrace_invop_hdlr != hdlr);
11791eaf3e1SJohn Birrell 		prev->dtih_next = hdlr->dtih_next;
11891eaf3e1SJohn Birrell 	}
11991eaf3e1SJohn Birrell 
12091eaf3e1SJohn Birrell 	kmem_free(hdlr, 0);
12191eaf3e1SJohn Birrell }
12291eaf3e1SJohn Birrell 
1239093dd9aSMark Johnston void
1249093dd9aSMark Johnston dtrace_invop_init(void)
1259093dd9aSMark Johnston {
1269093dd9aSMark Johnston 
1279093dd9aSMark Johnston 	dtrace_invop_jump_addr = dtrace_invop_start;
1289093dd9aSMark Johnston }
1299093dd9aSMark Johnston 
1309093dd9aSMark Johnston void
1319093dd9aSMark Johnston dtrace_invop_uninit(void)
1329093dd9aSMark Johnston {
1339093dd9aSMark Johnston 
1349093dd9aSMark Johnston 	dtrace_invop_jump_addr = NULL;
1359093dd9aSMark Johnston }
1369093dd9aSMark Johnston 
13791eaf3e1SJohn Birrell /*ARGSUSED*/
13891eaf3e1SJohn Birrell void
13991eaf3e1SJohn Birrell dtrace_toxic_ranges(void (*func)(uintptr_t base, uintptr_t limit))
14091eaf3e1SJohn Birrell {
1419ce875d9SKonstantin Belousov 	(*func)(0, la57 ? (uintptr_t)addr_P5Tmap : (uintptr_t)addr_P4Tmap);
14291eaf3e1SJohn Birrell }
14391eaf3e1SJohn Birrell 
14491eaf3e1SJohn Birrell void
14591eaf3e1SJohn Birrell dtrace_xcall(processorid_t cpu, dtrace_xcall_t func, void *arg)
14691eaf3e1SJohn Birrell {
14771a19bdcSAttilio Rao 	cpuset_t cpus;
14891eaf3e1SJohn Birrell 
14991eaf3e1SJohn Birrell 	if (cpu == DTRACE_CPUALL)
15091eaf3e1SJohn Birrell 		cpus = all_cpus;
15191eaf3e1SJohn Birrell 	else
15271a19bdcSAttilio Rao 		CPU_SETOF(cpu, &cpus);
15391eaf3e1SJohn Birrell 
15467d955aaSPatrick Kelsey 	smp_rendezvous_cpus(cpus, smp_no_rendezvous_barrier, func,
15567d955aaSPatrick Kelsey 	    smp_no_rendezvous_barrier, arg);
15691eaf3e1SJohn Birrell }
15791eaf3e1SJohn Birrell 
15891eaf3e1SJohn Birrell static void
15991eaf3e1SJohn Birrell dtrace_sync_func(void)
16091eaf3e1SJohn Birrell {
16191eaf3e1SJohn Birrell }
16291eaf3e1SJohn Birrell 
16391eaf3e1SJohn Birrell void
16491eaf3e1SJohn Birrell dtrace_sync(void)
16591eaf3e1SJohn Birrell {
16691eaf3e1SJohn Birrell         dtrace_xcall(DTRACE_CPUALL, (dtrace_xcall_t)dtrace_sync_func, NULL);
16791eaf3e1SJohn Birrell }
16891eaf3e1SJohn Birrell 
16991eaf3e1SJohn Birrell #ifdef notyet
17091eaf3e1SJohn Birrell void
17191eaf3e1SJohn Birrell dtrace_safe_synchronous_signal(void)
17291eaf3e1SJohn Birrell {
17391eaf3e1SJohn Birrell 	kthread_t *t = curthread;
17491eaf3e1SJohn Birrell 	struct regs *rp = lwptoregs(ttolwp(t));
17591eaf3e1SJohn Birrell 	size_t isz = t->t_dtrace_npc - t->t_dtrace_pc;
17691eaf3e1SJohn Birrell 
17791eaf3e1SJohn Birrell 	ASSERT(t->t_dtrace_on);
17891eaf3e1SJohn Birrell 
17991eaf3e1SJohn Birrell 	/*
18091eaf3e1SJohn Birrell 	 * If we're not in the range of scratch addresses, we're not actually
18191eaf3e1SJohn Birrell 	 * tracing user instructions so turn off the flags. If the instruction
18291eaf3e1SJohn Birrell 	 * we copied out caused a synchonous trap, reset the pc back to its
18391eaf3e1SJohn Birrell 	 * original value and turn off the flags.
18491eaf3e1SJohn Birrell 	 */
18591eaf3e1SJohn Birrell 	if (rp->r_pc < t->t_dtrace_scrpc ||
18691eaf3e1SJohn Birrell 	    rp->r_pc > t->t_dtrace_astpc + isz) {
18791eaf3e1SJohn Birrell 		t->t_dtrace_ft = 0;
18891eaf3e1SJohn Birrell 	} else if (rp->r_pc == t->t_dtrace_scrpc ||
18991eaf3e1SJohn Birrell 	    rp->r_pc == t->t_dtrace_astpc) {
19091eaf3e1SJohn Birrell 		rp->r_pc = t->t_dtrace_pc;
19191eaf3e1SJohn Birrell 		t->t_dtrace_ft = 0;
19291eaf3e1SJohn Birrell 	}
19391eaf3e1SJohn Birrell }
19491eaf3e1SJohn Birrell 
19591eaf3e1SJohn Birrell int
19691eaf3e1SJohn Birrell dtrace_safe_defer_signal(void)
19791eaf3e1SJohn Birrell {
19891eaf3e1SJohn Birrell 	kthread_t *t = curthread;
19991eaf3e1SJohn Birrell 	struct regs *rp = lwptoregs(ttolwp(t));
20091eaf3e1SJohn Birrell 	size_t isz = t->t_dtrace_npc - t->t_dtrace_pc;
20191eaf3e1SJohn Birrell 
20291eaf3e1SJohn Birrell 	ASSERT(t->t_dtrace_on);
20391eaf3e1SJohn Birrell 
20491eaf3e1SJohn Birrell 	/*
20591eaf3e1SJohn Birrell 	 * If we're not in the range of scratch addresses, we're not actually
20691eaf3e1SJohn Birrell 	 * tracing user instructions so turn off the flags.
20791eaf3e1SJohn Birrell 	 */
20891eaf3e1SJohn Birrell 	if (rp->r_pc < t->t_dtrace_scrpc ||
20991eaf3e1SJohn Birrell 	    rp->r_pc > t->t_dtrace_astpc + isz) {
21091eaf3e1SJohn Birrell 		t->t_dtrace_ft = 0;
21191eaf3e1SJohn Birrell 		return (0);
21291eaf3e1SJohn Birrell 	}
21391eaf3e1SJohn Birrell 
21491eaf3e1SJohn Birrell 	/*
2154737d389SGeorge V. Neville-Neil 	 * If we have executed the original instruction, but we have performed
2164737d389SGeorge V. Neville-Neil 	 * neither the jmp back to t->t_dtrace_npc nor the clean up of any
2174737d389SGeorge V. Neville-Neil 	 * registers used to emulate %rip-relative instructions in 64-bit mode,
2184737d389SGeorge V. Neville-Neil 	 * we'll save ourselves some effort by doing that here and taking the
2194737d389SGeorge V. Neville-Neil 	 * signal right away.  We detect this condition by seeing if the program
2204737d389SGeorge V. Neville-Neil 	 * counter is the range [scrpc + isz, astpc).
22191eaf3e1SJohn Birrell 	 */
2224737d389SGeorge V. Neville-Neil 	if (rp->r_pc >= t->t_dtrace_scrpc + isz &&
2234737d389SGeorge V. Neville-Neil 	    rp->r_pc < t->t_dtrace_astpc) {
22491eaf3e1SJohn Birrell #ifdef __amd64
22591eaf3e1SJohn Birrell 		/*
22691eaf3e1SJohn Birrell 		 * If there is a scratch register and we're on the
22791eaf3e1SJohn Birrell 		 * instruction immediately after the modified instruction,
22891eaf3e1SJohn Birrell 		 * restore the value of that scratch register.
22991eaf3e1SJohn Birrell 		 */
23091eaf3e1SJohn Birrell 		if (t->t_dtrace_reg != 0 &&
23191eaf3e1SJohn Birrell 		    rp->r_pc == t->t_dtrace_scrpc + isz) {
23291eaf3e1SJohn Birrell 			switch (t->t_dtrace_reg) {
23391eaf3e1SJohn Birrell 			case REG_RAX:
23491eaf3e1SJohn Birrell 				rp->r_rax = t->t_dtrace_regv;
23591eaf3e1SJohn Birrell 				break;
23691eaf3e1SJohn Birrell 			case REG_RCX:
23791eaf3e1SJohn Birrell 				rp->r_rcx = t->t_dtrace_regv;
23891eaf3e1SJohn Birrell 				break;
23991eaf3e1SJohn Birrell 			case REG_R8:
24091eaf3e1SJohn Birrell 				rp->r_r8 = t->t_dtrace_regv;
24191eaf3e1SJohn Birrell 				break;
24291eaf3e1SJohn Birrell 			case REG_R9:
24391eaf3e1SJohn Birrell 				rp->r_r9 = t->t_dtrace_regv;
24491eaf3e1SJohn Birrell 				break;
24591eaf3e1SJohn Birrell 			}
24691eaf3e1SJohn Birrell 		}
24791eaf3e1SJohn Birrell #endif
24891eaf3e1SJohn Birrell 		rp->r_pc = t->t_dtrace_npc;
24991eaf3e1SJohn Birrell 		t->t_dtrace_ft = 0;
25091eaf3e1SJohn Birrell 		return (0);
25191eaf3e1SJohn Birrell 	}
25291eaf3e1SJohn Birrell 
25391eaf3e1SJohn Birrell 	/*
25491eaf3e1SJohn Birrell 	 * Otherwise, make sure we'll return to the kernel after executing
25591eaf3e1SJohn Birrell 	 * the copied out instruction and defer the signal.
25691eaf3e1SJohn Birrell 	 */
25791eaf3e1SJohn Birrell 	if (!t->t_dtrace_step) {
25891eaf3e1SJohn Birrell 		ASSERT(rp->r_pc < t->t_dtrace_astpc);
25991eaf3e1SJohn Birrell 		rp->r_pc += t->t_dtrace_astpc - t->t_dtrace_scrpc;
26091eaf3e1SJohn Birrell 		t->t_dtrace_step = 1;
26191eaf3e1SJohn Birrell 	}
26291eaf3e1SJohn Birrell 
26391eaf3e1SJohn Birrell 	t->t_dtrace_ast = 1;
26491eaf3e1SJohn Birrell 
26591eaf3e1SJohn Birrell 	return (1);
26691eaf3e1SJohn Birrell }
26791eaf3e1SJohn Birrell #endif
26891eaf3e1SJohn Birrell 
26991eaf3e1SJohn Birrell static int64_t	tgt_cpu_tsc;
27091eaf3e1SJohn Birrell static int64_t	hst_cpu_tsc;
27191eaf3e1SJohn Birrell static int64_t	tsc_skew[MAXCPU];
272b064b6d1SAndriy Gapon static uint64_t	nsec_scale;
273b064b6d1SAndriy Gapon 
274b064b6d1SAndriy Gapon /* See below for the explanation of this macro. */
275b064b6d1SAndriy Gapon #define SCALE_SHIFT	28
27691eaf3e1SJohn Birrell 
277fdce57a0SJohn Baldwin static void
278fdce57a0SJohn Baldwin dtrace_gethrtime_init_cpu(void *arg)
279fdce57a0SJohn Baldwin {
280fdce57a0SJohn Baldwin 	uintptr_t cpu = (uintptr_t) arg;
281fdce57a0SJohn Baldwin 
282fdce57a0SJohn Baldwin 	if (cpu == curcpu)
283fdce57a0SJohn Baldwin 		tgt_cpu_tsc = rdtsc();
284fdce57a0SJohn Baldwin 	else
285fdce57a0SJohn Baldwin 		hst_cpu_tsc = rdtsc();
286fdce57a0SJohn Baldwin }
287fdce57a0SJohn Baldwin 
288fdce57a0SJohn Baldwin #ifdef EARLY_AP_STARTUP
289fdce57a0SJohn Baldwin static void
290fdce57a0SJohn Baldwin dtrace_gethrtime_init(void *arg)
291fdce57a0SJohn Baldwin {
292fdce57a0SJohn Baldwin 	struct pcpu *pc;
293fdce57a0SJohn Baldwin 	uint64_t tsc_f;
294fdce57a0SJohn Baldwin 	cpuset_t map;
295fdce57a0SJohn Baldwin 	int i;
296fdce57a0SJohn Baldwin #else
297e1e33ff9SMark Johnston /*
298e1e33ff9SMark Johnston  * Get the frequency and scale factor as early as possible so that they can be
299e1e33ff9SMark Johnston  * used for boot-time tracing.
300e1e33ff9SMark Johnston  */
30191eaf3e1SJohn Birrell static void
302e1e33ff9SMark Johnston dtrace_gethrtime_init_early(void *arg)
30391eaf3e1SJohn Birrell {
304b064b6d1SAndriy Gapon 	uint64_t tsc_f;
305fdce57a0SJohn Baldwin #endif
306b064b6d1SAndriy Gapon 
307b064b6d1SAndriy Gapon 	/*
308b064b6d1SAndriy Gapon 	 * Get TSC frequency known at this moment.
309b064b6d1SAndriy Gapon 	 * This should be constant if TSC is invariant.
310b064b6d1SAndriy Gapon 	 * Otherwise tick->time conversion will be inaccurate, but
311b064b6d1SAndriy Gapon 	 * will preserve monotonic property of TSC.
312b064b6d1SAndriy Gapon 	 */
3133453537fSJung-uk Kim 	tsc_f = atomic_load_acq_64(&tsc_freq);
314b064b6d1SAndriy Gapon 
315b064b6d1SAndriy Gapon 	/*
316b064b6d1SAndriy Gapon 	 * The following line checks that nsec_scale calculated below
317b064b6d1SAndriy Gapon 	 * doesn't overflow 32-bit unsigned integer, so that it can multiply
318b064b6d1SAndriy Gapon 	 * another 32-bit integer without overflowing 64-bit.
319b064b6d1SAndriy Gapon 	 * Thus minimum supported TSC frequency is 62.5MHz.
320b064b6d1SAndriy Gapon 	 */
321e1e33ff9SMark Johnston 	KASSERT(tsc_f > (NANOSEC >> (32 - SCALE_SHIFT)),
322e1e33ff9SMark Johnston 	    ("TSC frequency is too low"));
323b064b6d1SAndriy Gapon 
324b064b6d1SAndriy Gapon 	/*
325b064b6d1SAndriy Gapon 	 * We scale up NANOSEC/tsc_f ratio to preserve as much precision
326b064b6d1SAndriy Gapon 	 * as possible.
327b064b6d1SAndriy Gapon 	 * 2^28 factor was chosen quite arbitrarily from practical
328b064b6d1SAndriy Gapon 	 * considerations:
329b064b6d1SAndriy Gapon 	 * - it supports TSC frequencies as low as 62.5MHz (see above);
330b064b6d1SAndriy Gapon 	 * - it provides quite good precision (e < 0.01%) up to THz
331b064b6d1SAndriy Gapon 	 *   (terahertz) values;
332b064b6d1SAndriy Gapon 	 */
333b064b6d1SAndriy Gapon 	nsec_scale = ((uint64_t)NANOSEC << SCALE_SHIFT) / tsc_f;
334fdce57a0SJohn Baldwin #ifndef EARLY_AP_STARTUP
335e1e33ff9SMark Johnston }
336e1e33ff9SMark Johnston SYSINIT(dtrace_gethrtime_init_early, SI_SUB_CPU, SI_ORDER_ANY,
337e1e33ff9SMark Johnston     dtrace_gethrtime_init_early, NULL);
338e1e33ff9SMark Johnston 
339e1e33ff9SMark Johnston static void
340e1e33ff9SMark Johnston dtrace_gethrtime_init(void *arg)
341e1e33ff9SMark Johnston {
342e1e33ff9SMark Johnston 	struct pcpu *pc;
343e1e33ff9SMark Johnston 	cpuset_t map;
344e1e33ff9SMark Johnston 	int i;
345fdce57a0SJohn Baldwin #endif
34691eaf3e1SJohn Birrell 
347a4b59d3dSMark Johnston 	if (vm_guest != VM_GUEST_NO)
348e362e590SMark Johnston 		return;
349e362e590SMark Johnston 
35091eaf3e1SJohn Birrell 	/* The current CPU is the reference one. */
3517becfa95SAndriy Gapon 	sched_pin();
35291eaf3e1SJohn Birrell 	tsc_skew[curcpu] = 0;
3533aa6d94eSJohn Baldwin 	CPU_FOREACH(i) {
35491eaf3e1SJohn Birrell 		if (i == curcpu)
35591eaf3e1SJohn Birrell 			continue;
35691eaf3e1SJohn Birrell 
3577becfa95SAndriy Gapon 		pc = pcpu_find(i);
358ada5b739SAttilio Rao 		CPU_SETOF(PCPU_GET(cpuid), &map);
359ada5b739SAttilio Rao 		CPU_SET(pc->pc_cpuid, &map);
36091eaf3e1SJohn Birrell 
361d9b8935fSAndriy Gapon 		smp_rendezvous_cpus(map, NULL,
36291eaf3e1SJohn Birrell 		    dtrace_gethrtime_init_cpu,
36367d955aaSPatrick Kelsey 		    smp_no_rendezvous_barrier, (void *)(uintptr_t) i);
36491eaf3e1SJohn Birrell 
36591eaf3e1SJohn Birrell 		tsc_skew[i] = tgt_cpu_tsc - hst_cpu_tsc;
36691eaf3e1SJohn Birrell 	}
3677becfa95SAndriy Gapon 	sched_unpin();
36891eaf3e1SJohn Birrell }
369fdce57a0SJohn Baldwin #ifdef EARLY_AP_STARTUP
370fdce57a0SJohn Baldwin SYSINIT(dtrace_gethrtime_init, SI_SUB_DTRACE, SI_ORDER_ANY,
371fdce57a0SJohn Baldwin     dtrace_gethrtime_init, NULL);
372fdce57a0SJohn Baldwin #else
373e1e33ff9SMark Johnston SYSINIT(dtrace_gethrtime_init, SI_SUB_SMP, SI_ORDER_ANY, dtrace_gethrtime_init,
374e1e33ff9SMark Johnston     NULL);
375fdce57a0SJohn Baldwin #endif
37691eaf3e1SJohn Birrell 
37791eaf3e1SJohn Birrell /*
37891eaf3e1SJohn Birrell  * DTrace needs a high resolution time function which can
37991eaf3e1SJohn Birrell  * be called from a probe context and guaranteed not to have
38091eaf3e1SJohn Birrell  * instrumented with probes itself.
38191eaf3e1SJohn Birrell  *
38291eaf3e1SJohn Birrell  * Returns nanoseconds since boot.
38391eaf3e1SJohn Birrell  */
38491eaf3e1SJohn Birrell uint64_t
3856c7828a2SMark Johnston dtrace_gethrtime(void)
38691eaf3e1SJohn Birrell {
387b064b6d1SAndriy Gapon 	uint64_t tsc;
3886c7828a2SMark Johnston 	uint32_t lo, hi;
3896c7828a2SMark Johnston 	register_t rflags;
390b064b6d1SAndriy Gapon 
391b064b6d1SAndriy Gapon 	/*
392b064b6d1SAndriy Gapon 	 * We split TSC value into lower and higher 32-bit halves and separately
393b064b6d1SAndriy Gapon 	 * scale them with nsec_scale, then we scale them down by 2^28
394b064b6d1SAndriy Gapon 	 * (see nsec_scale calculations) taking into account 32-bit shift of
395b064b6d1SAndriy Gapon 	 * the higher half and finally add.
396b064b6d1SAndriy Gapon 	 */
3976c7828a2SMark Johnston 	rflags = intr_disable();
398db5c7d36SZachary Loafman 	tsc = rdtsc() - tsc_skew[curcpu];
3996c7828a2SMark Johnston 	intr_restore(rflags);
4006c7828a2SMark Johnston 
401b064b6d1SAndriy Gapon 	lo = tsc;
402b064b6d1SAndriy Gapon 	hi = tsc >> 32;
403b064b6d1SAndriy Gapon 	return (((lo * nsec_scale) >> SCALE_SHIFT) +
404b064b6d1SAndriy Gapon 	    ((hi * nsec_scale) << (32 - SCALE_SHIFT)));
40591eaf3e1SJohn Birrell }
40691eaf3e1SJohn Birrell 
40791eaf3e1SJohn Birrell uint64_t
40891eaf3e1SJohn Birrell dtrace_gethrestime(void)
40991eaf3e1SJohn Birrell {
41057d025c3SGeorge V. Neville-Neil 	struct timespec current_time;
41157d025c3SGeorge V. Neville-Neil 
41257d025c3SGeorge V. Neville-Neil 	dtrace_getnanotime(&current_time);
41357d025c3SGeorge V. Neville-Neil 
414d638e8dcSGeorge V. Neville-Neil 	return (current_time.tv_sec * 1000000000ULL + current_time.tv_nsec);
41591eaf3e1SJohn Birrell }
41691eaf3e1SJohn Birrell 
4175a5f9d21SMark Johnston /* Function to handle DTrace traps during probes. See amd64/amd64/trap.c. */
41891eaf3e1SJohn Birrell int
419cafe8744SMark Johnston dtrace_trap(struct trapframe *frame, u_int type)
42091eaf3e1SJohn Birrell {
421a11ac730SMark Johnston 	uint16_t nofault;
422a11ac730SMark Johnston 
42391eaf3e1SJohn Birrell 	/*
42491eaf3e1SJohn Birrell 	 * A trap can occur while DTrace executes a probe. Before
42591eaf3e1SJohn Birrell 	 * executing the probe, DTrace blocks re-scheduling and sets
426291624fdSMark Johnston 	 * a flag in its per-cpu flags to indicate that it doesn't
4276bccea7cSRebecca Cran 	 * want to fault. On returning from the probe, the no-fault
42891eaf3e1SJohn Birrell 	 * flag is cleared and finally re-scheduling is enabled.
42991eaf3e1SJohn Birrell 	 *
43091eaf3e1SJohn Birrell 	 * Check if DTrace has enabled 'no-fault' mode:
43191eaf3e1SJohn Birrell 	 */
432a11ac730SMark Johnston 	sched_pin();
433a11ac730SMark Johnston 	nofault = cpu_core[curcpu].cpuc_dtrace_flags & CPU_DTRACE_NOFAULT;
434a11ac730SMark Johnston 	sched_unpin();
435a11ac730SMark Johnston 	if (nofault) {
436a11ac730SMark Johnston 		KASSERT((read_rflags() & PSL_I) == 0, ("interrupts enabled"));
437a11ac730SMark Johnston 
43891eaf3e1SJohn Birrell 		/*
43991eaf3e1SJohn Birrell 		 * There are only a couple of trap types that are expected.
44091eaf3e1SJohn Birrell 		 * All the rest will be handled in the usual way.
44191eaf3e1SJohn Birrell 		 */
442cafe8744SMark Johnston 		switch (type) {
44391eaf3e1SJohn Birrell 		/* General protection fault. */
44491eaf3e1SJohn Birrell 		case T_PROTFLT:
44591eaf3e1SJohn Birrell 			/* Flag an illegal operation. */
44691eaf3e1SJohn Birrell 			cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP;
44791eaf3e1SJohn Birrell 
44891eaf3e1SJohn Birrell 			/*
44991eaf3e1SJohn Birrell 			 * Offset the instruction pointer to the instruction
45091eaf3e1SJohn Birrell 			 * following the one causing the fault.
45191eaf3e1SJohn Birrell 			 */
452*1a149d65SChristos Margiolis 			frame->tf_rip += dtrace_instr_size((uint8_t *) frame->tf_rip);
45391eaf3e1SJohn Birrell 			return (1);
45491eaf3e1SJohn Birrell 		/* Page fault. */
45591eaf3e1SJohn Birrell 		case T_PAGEFLT:
45691eaf3e1SJohn Birrell 			/* Flag a bad address. */
45791eaf3e1SJohn Birrell 			cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_BADADDR;
45891eaf3e1SJohn Birrell 			cpu_core[curcpu].cpuc_dtrace_illval = frame->tf_addr;
45991eaf3e1SJohn Birrell 
46091eaf3e1SJohn Birrell 			/*
46191eaf3e1SJohn Birrell 			 * Offset the instruction pointer to the instruction
46291eaf3e1SJohn Birrell 			 * following the one causing the fault.
46391eaf3e1SJohn Birrell 			 */
464*1a149d65SChristos Margiolis 			frame->tf_rip += dtrace_instr_size((uint8_t *) frame->tf_rip);
46591eaf3e1SJohn Birrell 			return (1);
46691eaf3e1SJohn Birrell 		default:
46791eaf3e1SJohn Birrell 			/* Handle all other traps in the usual way. */
46891eaf3e1SJohn Birrell 			break;
46991eaf3e1SJohn Birrell 		}
47091eaf3e1SJohn Birrell 	}
47191eaf3e1SJohn Birrell 
47291eaf3e1SJohn Birrell 	/* Handle the trap in the usual way. */
47391eaf3e1SJohn Birrell 	return (0);
47491eaf3e1SJohn Birrell }
475