xref: /freebsd/sys/cddl/dev/dtrace/amd64/dtrace_subr.c (revision cc3da1955c16df5eb0019e0fef810696b035b7cf)
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  */
2391eaf3e1SJohn Birrell /*
2491eaf3e1SJohn Birrell  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
2591eaf3e1SJohn Birrell  * Use is subject to license terms.
2691eaf3e1SJohn Birrell  */
2791eaf3e1SJohn Birrell 
284737d389SGeorge V. Neville-Neil /*
294737d389SGeorge V. Neville-Neil  * Copyright (c) 2011, Joyent, Inc. All rights reserved.
304737d389SGeorge V. Neville-Neil  */
314737d389SGeorge V. Neville-Neil 
3291eaf3e1SJohn Birrell #include <sys/param.h>
3391eaf3e1SJohn Birrell #include <sys/systm.h>
3491eaf3e1SJohn Birrell #include <sys/kernel.h>
3591eaf3e1SJohn Birrell #include <sys/malloc.h>
36*cc3da195SMark Johnston #include <sys/msan.h>
37bdd101c4SMark Johnston #include <sys/proc.h>
3891eaf3e1SJohn Birrell #include <sys/smp.h>
3991eaf3e1SJohn Birrell #include <sys/dtrace_impl.h>
4091eaf3e1SJohn Birrell #include <sys/dtrace_bsd.h>
41bdd101c4SMark Johnston #include <cddl/dev/dtrace/dtrace_cddl.h>
4291eaf3e1SJohn Birrell #include <machine/clock.h>
437174af79SMark Johnston #include <machine/cpufunc.h>
4491eaf3e1SJohn Birrell #include <machine/frame.h>
459ce875d9SKonstantin Belousov #include <machine/md_var.h>
467174af79SMark Johnston #include <machine/psl.h>
47d41e41f9SJohn Baldwin #include <machine/trap.h>
4891eaf3e1SJohn Birrell #include <vm/pmap.h>
4991eaf3e1SJohn Birrell 
5057d025c3SGeorge V. Neville-Neil extern void dtrace_getnanotime(struct timespec *tsp);
519093dd9aSMark Johnston extern int (*dtrace_invop_jump_addr)(struct trapframe *);
5257d025c3SGeorge V. Neville-Neil 
533ba8e9dcSMark Johnston int	dtrace_invop(uintptr_t, struct trapframe *, void **);
549093dd9aSMark Johnston int	dtrace_invop_start(struct trapframe *frame);
559093dd9aSMark Johnston void	dtrace_invop_init(void);
569093dd9aSMark Johnston void	dtrace_invop_uninit(void);
5791eaf3e1SJohn Birrell 
5891eaf3e1SJohn Birrell typedef struct dtrace_invop_hdlr {
596c280659SMark Johnston 	int (*dtih_func)(uintptr_t, struct trapframe *, uintptr_t);
6091eaf3e1SJohn Birrell 	struct dtrace_invop_hdlr *dtih_next;
6191eaf3e1SJohn Birrell } dtrace_invop_hdlr_t;
6291eaf3e1SJohn Birrell 
6391eaf3e1SJohn Birrell dtrace_invop_hdlr_t *dtrace_invop_hdlr;
6491eaf3e1SJohn Birrell 
6591eaf3e1SJohn Birrell int
dtrace_invop(uintptr_t addr,struct trapframe * frame,void ** scratch)663ba8e9dcSMark Johnston dtrace_invop(uintptr_t addr, struct trapframe *frame, void **scratch)
6791eaf3e1SJohn Birrell {
68bdd101c4SMark Johnston 	struct thread *td;
6991eaf3e1SJohn Birrell 	dtrace_invop_hdlr_t *hdlr;
7091eaf3e1SJohn Birrell 	int rval;
7191eaf3e1SJohn Birrell 
72*cc3da195SMark Johnston 	kmsan_mark(frame, sizeof(*frame), KMSAN_STATE_INITED);
73*cc3da195SMark Johnston 
74bdd101c4SMark Johnston 	td = curthread;
75bdd101c4SMark Johnston 	td->t_dtrace_trapframe = frame;
76bdd101c4SMark Johnston 	rval = 0;
773ba8e9dcSMark Johnston 	for (hdlr = dtrace_invop_hdlr; hdlr != NULL; hdlr = hdlr->dtih_next) {
783ba8e9dcSMark Johnston 		rval = hdlr->dtih_func(addr, frame, (uintptr_t)scratch);
793ba8e9dcSMark Johnston 		if (rval != 0)
80bdd101c4SMark Johnston 			break;
813ba8e9dcSMark Johnston 	}
82bdd101c4SMark Johnston 	td->t_dtrace_trapframe = NULL;
83bdd101c4SMark Johnston 	return (rval);
8491eaf3e1SJohn Birrell }
8591eaf3e1SJohn Birrell 
8691eaf3e1SJohn Birrell void
dtrace_invop_add(int (* func)(uintptr_t,struct trapframe *,uintptr_t))876c280659SMark Johnston dtrace_invop_add(int (*func)(uintptr_t, struct trapframe *, uintptr_t))
8891eaf3e1SJohn Birrell {
8991eaf3e1SJohn Birrell 	dtrace_invop_hdlr_t *hdlr;
9091eaf3e1SJohn Birrell 
9191eaf3e1SJohn Birrell 	hdlr = kmem_alloc(sizeof (dtrace_invop_hdlr_t), KM_SLEEP);
9291eaf3e1SJohn Birrell 	hdlr->dtih_func = func;
9391eaf3e1SJohn Birrell 	hdlr->dtih_next = dtrace_invop_hdlr;
9491eaf3e1SJohn Birrell 	dtrace_invop_hdlr = hdlr;
9591eaf3e1SJohn Birrell }
9691eaf3e1SJohn Birrell 
9791eaf3e1SJohn Birrell void
dtrace_invop_remove(int (* func)(uintptr_t,struct trapframe *,uintptr_t))986c280659SMark Johnston dtrace_invop_remove(int (*func)(uintptr_t, struct trapframe *, uintptr_t))
9991eaf3e1SJohn Birrell {
10091eaf3e1SJohn Birrell 	dtrace_invop_hdlr_t *hdlr = dtrace_invop_hdlr, *prev = NULL;
10191eaf3e1SJohn Birrell 
10291eaf3e1SJohn Birrell 	for (;;) {
10391eaf3e1SJohn Birrell 		if (hdlr == NULL)
10491eaf3e1SJohn Birrell 			panic("attempt to remove non-existent invop handler");
10591eaf3e1SJohn Birrell 
10691eaf3e1SJohn Birrell 		if (hdlr->dtih_func == func)
10791eaf3e1SJohn Birrell 			break;
10891eaf3e1SJohn Birrell 
10991eaf3e1SJohn Birrell 		prev = hdlr;
11091eaf3e1SJohn Birrell 		hdlr = hdlr->dtih_next;
11191eaf3e1SJohn Birrell 	}
11291eaf3e1SJohn Birrell 
11391eaf3e1SJohn Birrell 	if (prev == NULL) {
11491eaf3e1SJohn Birrell 		ASSERT(dtrace_invop_hdlr == hdlr);
11591eaf3e1SJohn Birrell 		dtrace_invop_hdlr = hdlr->dtih_next;
11691eaf3e1SJohn Birrell 	} else {
11791eaf3e1SJohn Birrell 		ASSERT(dtrace_invop_hdlr != hdlr);
11891eaf3e1SJohn Birrell 		prev->dtih_next = hdlr->dtih_next;
11991eaf3e1SJohn Birrell 	}
12091eaf3e1SJohn Birrell 
12191eaf3e1SJohn Birrell 	kmem_free(hdlr, 0);
12291eaf3e1SJohn Birrell }
12391eaf3e1SJohn Birrell 
1249093dd9aSMark Johnston void
dtrace_invop_init(void)1259093dd9aSMark Johnston dtrace_invop_init(void)
1269093dd9aSMark Johnston {
1279093dd9aSMark Johnston 
1289093dd9aSMark Johnston 	dtrace_invop_jump_addr = dtrace_invop_start;
1299093dd9aSMark Johnston }
1309093dd9aSMark Johnston 
1319093dd9aSMark Johnston void
dtrace_invop_uninit(void)1329093dd9aSMark Johnston dtrace_invop_uninit(void)
1339093dd9aSMark Johnston {
1349093dd9aSMark Johnston 
1359093dd9aSMark Johnston 	dtrace_invop_jump_addr = NULL;
1369093dd9aSMark Johnston }
1379093dd9aSMark Johnston 
13891eaf3e1SJohn Birrell /*ARGSUSED*/
13991eaf3e1SJohn Birrell void
dtrace_toxic_ranges(void (* func)(uintptr_t base,uintptr_t limit))14091eaf3e1SJohn Birrell dtrace_toxic_ranges(void (*func)(uintptr_t base, uintptr_t limit))
14191eaf3e1SJohn Birrell {
1429ce875d9SKonstantin Belousov 	(*func)(0, la57 ? (uintptr_t)addr_P5Tmap : (uintptr_t)addr_P4Tmap);
14391eaf3e1SJohn Birrell }
14491eaf3e1SJohn Birrell 
14591eaf3e1SJohn Birrell void
dtrace_xcall(processorid_t cpu,dtrace_xcall_t func,void * arg)14691eaf3e1SJohn Birrell dtrace_xcall(processorid_t cpu, dtrace_xcall_t func, void *arg)
14791eaf3e1SJohn Birrell {
14871a19bdcSAttilio Rao 	cpuset_t cpus;
14991eaf3e1SJohn Birrell 
15091eaf3e1SJohn Birrell 	if (cpu == DTRACE_CPUALL)
15191eaf3e1SJohn Birrell 		cpus = all_cpus;
15291eaf3e1SJohn Birrell 	else
15371a19bdcSAttilio Rao 		CPU_SETOF(cpu, &cpus);
15491eaf3e1SJohn Birrell 
15567d955aaSPatrick Kelsey 	smp_rendezvous_cpus(cpus, smp_no_rendezvous_barrier, func,
15667d955aaSPatrick Kelsey 	    smp_no_rendezvous_barrier, arg);
15791eaf3e1SJohn Birrell }
15891eaf3e1SJohn Birrell 
15991eaf3e1SJohn Birrell static void
dtrace_sync_func(void)16091eaf3e1SJohn Birrell dtrace_sync_func(void)
16191eaf3e1SJohn Birrell {
16291eaf3e1SJohn Birrell }
16391eaf3e1SJohn Birrell 
16491eaf3e1SJohn Birrell void
dtrace_sync(void)16591eaf3e1SJohn Birrell dtrace_sync(void)
16691eaf3e1SJohn Birrell {
16791eaf3e1SJohn Birrell         dtrace_xcall(DTRACE_CPUALL, (dtrace_xcall_t)dtrace_sync_func, NULL);
16891eaf3e1SJohn Birrell }
16991eaf3e1SJohn Birrell 
17091eaf3e1SJohn Birrell #ifdef notyet
17191eaf3e1SJohn Birrell void
dtrace_safe_synchronous_signal(void)17291eaf3e1SJohn Birrell dtrace_safe_synchronous_signal(void)
17391eaf3e1SJohn Birrell {
17491eaf3e1SJohn Birrell 	kthread_t *t = curthread;
17591eaf3e1SJohn Birrell 	struct regs *rp = lwptoregs(ttolwp(t));
17691eaf3e1SJohn Birrell 	size_t isz = t->t_dtrace_npc - t->t_dtrace_pc;
17791eaf3e1SJohn Birrell 
17891eaf3e1SJohn Birrell 	ASSERT(t->t_dtrace_on);
17991eaf3e1SJohn Birrell 
18091eaf3e1SJohn Birrell 	/*
18191eaf3e1SJohn Birrell 	 * If we're not in the range of scratch addresses, we're not actually
18291eaf3e1SJohn Birrell 	 * tracing user instructions so turn off the flags. If the instruction
18391eaf3e1SJohn Birrell 	 * we copied out caused a synchonous trap, reset the pc back to its
18491eaf3e1SJohn Birrell 	 * original value and turn off the flags.
18591eaf3e1SJohn Birrell 	 */
18691eaf3e1SJohn Birrell 	if (rp->r_pc < t->t_dtrace_scrpc ||
18791eaf3e1SJohn Birrell 	    rp->r_pc > t->t_dtrace_astpc + isz) {
18891eaf3e1SJohn Birrell 		t->t_dtrace_ft = 0;
18991eaf3e1SJohn Birrell 	} else if (rp->r_pc == t->t_dtrace_scrpc ||
19091eaf3e1SJohn Birrell 	    rp->r_pc == t->t_dtrace_astpc) {
19191eaf3e1SJohn Birrell 		rp->r_pc = t->t_dtrace_pc;
19291eaf3e1SJohn Birrell 		t->t_dtrace_ft = 0;
19391eaf3e1SJohn Birrell 	}
19491eaf3e1SJohn Birrell }
19591eaf3e1SJohn Birrell 
19691eaf3e1SJohn Birrell int
dtrace_safe_defer_signal(void)19791eaf3e1SJohn Birrell dtrace_safe_defer_signal(void)
19891eaf3e1SJohn Birrell {
19991eaf3e1SJohn Birrell 	kthread_t *t = curthread;
20091eaf3e1SJohn Birrell 	struct regs *rp = lwptoregs(ttolwp(t));
20191eaf3e1SJohn Birrell 	size_t isz = t->t_dtrace_npc - t->t_dtrace_pc;
20291eaf3e1SJohn Birrell 
20391eaf3e1SJohn Birrell 	ASSERT(t->t_dtrace_on);
20491eaf3e1SJohn Birrell 
20591eaf3e1SJohn Birrell 	/*
20691eaf3e1SJohn Birrell 	 * If we're not in the range of scratch addresses, we're not actually
20791eaf3e1SJohn Birrell 	 * tracing user instructions so turn off the flags.
20891eaf3e1SJohn Birrell 	 */
20991eaf3e1SJohn Birrell 	if (rp->r_pc < t->t_dtrace_scrpc ||
21091eaf3e1SJohn Birrell 	    rp->r_pc > t->t_dtrace_astpc + isz) {
21191eaf3e1SJohn Birrell 		t->t_dtrace_ft = 0;
21291eaf3e1SJohn Birrell 		return (0);
21391eaf3e1SJohn Birrell 	}
21491eaf3e1SJohn Birrell 
21591eaf3e1SJohn Birrell 	/*
2164737d389SGeorge V. Neville-Neil 	 * If we have executed the original instruction, but we have performed
2174737d389SGeorge V. Neville-Neil 	 * neither the jmp back to t->t_dtrace_npc nor the clean up of any
2184737d389SGeorge V. Neville-Neil 	 * registers used to emulate %rip-relative instructions in 64-bit mode,
2194737d389SGeorge V. Neville-Neil 	 * we'll save ourselves some effort by doing that here and taking the
2204737d389SGeorge V. Neville-Neil 	 * signal right away.  We detect this condition by seeing if the program
2214737d389SGeorge V. Neville-Neil 	 * counter is the range [scrpc + isz, astpc).
22291eaf3e1SJohn Birrell 	 */
2234737d389SGeorge V. Neville-Neil 	if (rp->r_pc >= t->t_dtrace_scrpc + isz &&
2244737d389SGeorge V. Neville-Neil 	    rp->r_pc < t->t_dtrace_astpc) {
22591eaf3e1SJohn Birrell #ifdef __amd64
22691eaf3e1SJohn Birrell 		/*
22791eaf3e1SJohn Birrell 		 * If there is a scratch register and we're on the
22891eaf3e1SJohn Birrell 		 * instruction immediately after the modified instruction,
22991eaf3e1SJohn Birrell 		 * restore the value of that scratch register.
23091eaf3e1SJohn Birrell 		 */
23191eaf3e1SJohn Birrell 		if (t->t_dtrace_reg != 0 &&
23291eaf3e1SJohn Birrell 		    rp->r_pc == t->t_dtrace_scrpc + isz) {
23391eaf3e1SJohn Birrell 			switch (t->t_dtrace_reg) {
23491eaf3e1SJohn Birrell 			case REG_RAX:
23591eaf3e1SJohn Birrell 				rp->r_rax = t->t_dtrace_regv;
23691eaf3e1SJohn Birrell 				break;
23791eaf3e1SJohn Birrell 			case REG_RCX:
23891eaf3e1SJohn Birrell 				rp->r_rcx = t->t_dtrace_regv;
23991eaf3e1SJohn Birrell 				break;
24091eaf3e1SJohn Birrell 			case REG_R8:
24191eaf3e1SJohn Birrell 				rp->r_r8 = t->t_dtrace_regv;
24291eaf3e1SJohn Birrell 				break;
24391eaf3e1SJohn Birrell 			case REG_R9:
24491eaf3e1SJohn Birrell 				rp->r_r9 = t->t_dtrace_regv;
24591eaf3e1SJohn Birrell 				break;
24691eaf3e1SJohn Birrell 			}
24791eaf3e1SJohn Birrell 		}
24891eaf3e1SJohn Birrell #endif
24991eaf3e1SJohn Birrell 		rp->r_pc = t->t_dtrace_npc;
25091eaf3e1SJohn Birrell 		t->t_dtrace_ft = 0;
25191eaf3e1SJohn Birrell 		return (0);
25291eaf3e1SJohn Birrell 	}
25391eaf3e1SJohn Birrell 
25491eaf3e1SJohn Birrell 	/*
25591eaf3e1SJohn Birrell 	 * Otherwise, make sure we'll return to the kernel after executing
25691eaf3e1SJohn Birrell 	 * the copied out instruction and defer the signal.
25791eaf3e1SJohn Birrell 	 */
25891eaf3e1SJohn Birrell 	if (!t->t_dtrace_step) {
25991eaf3e1SJohn Birrell 		ASSERT(rp->r_pc < t->t_dtrace_astpc);
26091eaf3e1SJohn Birrell 		rp->r_pc += t->t_dtrace_astpc - t->t_dtrace_scrpc;
26191eaf3e1SJohn Birrell 		t->t_dtrace_step = 1;
26291eaf3e1SJohn Birrell 	}
26391eaf3e1SJohn Birrell 
26491eaf3e1SJohn Birrell 	t->t_dtrace_ast = 1;
26591eaf3e1SJohn Birrell 
26691eaf3e1SJohn Birrell 	return (1);
26791eaf3e1SJohn Birrell }
26891eaf3e1SJohn Birrell #endif
26991eaf3e1SJohn Birrell 
27091eaf3e1SJohn Birrell static int64_t	tgt_cpu_tsc;
27191eaf3e1SJohn Birrell static int64_t	hst_cpu_tsc;
27291eaf3e1SJohn Birrell static int64_t	tsc_skew[MAXCPU];
273b064b6d1SAndriy Gapon static uint64_t	nsec_scale;
274b064b6d1SAndriy Gapon 
275b064b6d1SAndriy Gapon /* See below for the explanation of this macro. */
276b064b6d1SAndriy Gapon #define SCALE_SHIFT	28
27791eaf3e1SJohn Birrell 
278fdce57a0SJohn Baldwin static void
dtrace_gethrtime_init_cpu(void * arg)279fdce57a0SJohn Baldwin dtrace_gethrtime_init_cpu(void *arg)
280fdce57a0SJohn Baldwin {
281fdce57a0SJohn Baldwin 	uintptr_t cpu = (uintptr_t) arg;
282fdce57a0SJohn Baldwin 
283fdce57a0SJohn Baldwin 	if (cpu == curcpu)
284fdce57a0SJohn Baldwin 		tgt_cpu_tsc = rdtsc();
285fdce57a0SJohn Baldwin 	else
286fdce57a0SJohn Baldwin 		hst_cpu_tsc = rdtsc();
287fdce57a0SJohn Baldwin }
288fdce57a0SJohn Baldwin 
289fdce57a0SJohn Baldwin static void
dtrace_gethrtime_init(void * arg)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;
296b064b6d1SAndriy Gapon 
297b064b6d1SAndriy Gapon 	/*
298b064b6d1SAndriy Gapon 	 * Get TSC frequency known at this moment.
299b064b6d1SAndriy Gapon 	 * This should be constant if TSC is invariant.
300b064b6d1SAndriy Gapon 	 * Otherwise tick->time conversion will be inaccurate, but
301b064b6d1SAndriy Gapon 	 * will preserve monotonic property of TSC.
302b064b6d1SAndriy Gapon 	 */
3033453537fSJung-uk Kim 	tsc_f = atomic_load_acq_64(&tsc_freq);
304b064b6d1SAndriy Gapon 
305b064b6d1SAndriy Gapon 	/*
306b064b6d1SAndriy Gapon 	 * The following line checks that nsec_scale calculated below
307b064b6d1SAndriy Gapon 	 * doesn't overflow 32-bit unsigned integer, so that it can multiply
308b064b6d1SAndriy Gapon 	 * another 32-bit integer without overflowing 64-bit.
309b064b6d1SAndriy Gapon 	 * Thus minimum supported TSC frequency is 62.5MHz.
310b064b6d1SAndriy Gapon 	 */
311e1e33ff9SMark Johnston 	KASSERT(tsc_f > (NANOSEC >> (32 - SCALE_SHIFT)),
312e1e33ff9SMark Johnston 	    ("TSC frequency is too low"));
313b064b6d1SAndriy Gapon 
314b064b6d1SAndriy Gapon 	/*
315b064b6d1SAndriy Gapon 	 * We scale up NANOSEC/tsc_f ratio to preserve as much precision
316b064b6d1SAndriy Gapon 	 * as possible.
317b064b6d1SAndriy Gapon 	 * 2^28 factor was chosen quite arbitrarily from practical
318b064b6d1SAndriy Gapon 	 * considerations:
319b064b6d1SAndriy Gapon 	 * - it supports TSC frequencies as low as 62.5MHz (see above);
320b064b6d1SAndriy Gapon 	 * - it provides quite good precision (e < 0.01%) up to THz
321b064b6d1SAndriy Gapon 	 *   (terahertz) values;
322b064b6d1SAndriy Gapon 	 */
323b064b6d1SAndriy Gapon 	nsec_scale = ((uint64_t)NANOSEC << SCALE_SHIFT) / tsc_f;
32491eaf3e1SJohn Birrell 
325a4b59d3dSMark Johnston 	if (vm_guest != VM_GUEST_NO)
326e362e590SMark Johnston 		return;
327e362e590SMark Johnston 
32891eaf3e1SJohn Birrell 	/* The current CPU is the reference one. */
3297becfa95SAndriy Gapon 	sched_pin();
33091eaf3e1SJohn Birrell 	tsc_skew[curcpu] = 0;
3313aa6d94eSJohn Baldwin 	CPU_FOREACH(i) {
33291eaf3e1SJohn Birrell 		if (i == curcpu)
33391eaf3e1SJohn Birrell 			continue;
33491eaf3e1SJohn Birrell 
3357becfa95SAndriy Gapon 		pc = pcpu_find(i);
336ada5b739SAttilio Rao 		CPU_SETOF(PCPU_GET(cpuid), &map);
337ada5b739SAttilio Rao 		CPU_SET(pc->pc_cpuid, &map);
33891eaf3e1SJohn Birrell 
339d9b8935fSAndriy Gapon 		smp_rendezvous_cpus(map, NULL,
34091eaf3e1SJohn Birrell 		    dtrace_gethrtime_init_cpu,
34167d955aaSPatrick Kelsey 		    smp_no_rendezvous_barrier, (void *)(uintptr_t) i);
34291eaf3e1SJohn Birrell 
34391eaf3e1SJohn Birrell 		tsc_skew[i] = tgt_cpu_tsc - hst_cpu_tsc;
34491eaf3e1SJohn Birrell 	}
3457becfa95SAndriy Gapon 	sched_unpin();
34691eaf3e1SJohn Birrell }
347fdce57a0SJohn Baldwin SYSINIT(dtrace_gethrtime_init, SI_SUB_DTRACE, SI_ORDER_ANY,
348fdce57a0SJohn Baldwin     dtrace_gethrtime_init, NULL);
34991eaf3e1SJohn Birrell 
35091eaf3e1SJohn Birrell /*
35191eaf3e1SJohn Birrell  * DTrace needs a high resolution time function which can
35291eaf3e1SJohn Birrell  * be called from a probe context and guaranteed not to have
35391eaf3e1SJohn Birrell  * instrumented with probes itself.
35491eaf3e1SJohn Birrell  *
35591eaf3e1SJohn Birrell  * Returns nanoseconds since boot.
35691eaf3e1SJohn Birrell  */
35791eaf3e1SJohn Birrell uint64_t
dtrace_gethrtime(void)3586c7828a2SMark Johnston dtrace_gethrtime(void)
35991eaf3e1SJohn Birrell {
360b064b6d1SAndriy Gapon 	uint64_t tsc;
3616c7828a2SMark Johnston 	uint32_t lo, hi;
3626c7828a2SMark Johnston 	register_t rflags;
363b064b6d1SAndriy Gapon 
364b064b6d1SAndriy Gapon 	/*
365b064b6d1SAndriy Gapon 	 * We split TSC value into lower and higher 32-bit halves and separately
366b064b6d1SAndriy Gapon 	 * scale them with nsec_scale, then we scale them down by 2^28
367b064b6d1SAndriy Gapon 	 * (see nsec_scale calculations) taking into account 32-bit shift of
368b064b6d1SAndriy Gapon 	 * the higher half and finally add.
369b064b6d1SAndriy Gapon 	 */
3706c7828a2SMark Johnston 	rflags = intr_disable();
371db5c7d36SZachary Loafman 	tsc = rdtsc() - tsc_skew[curcpu];
3726c7828a2SMark Johnston 	intr_restore(rflags);
3736c7828a2SMark Johnston 
374b064b6d1SAndriy Gapon 	lo = tsc;
375b064b6d1SAndriy Gapon 	hi = tsc >> 32;
376b064b6d1SAndriy Gapon 	return (((lo * nsec_scale) >> SCALE_SHIFT) +
377b064b6d1SAndriy Gapon 	    ((hi * nsec_scale) << (32 - SCALE_SHIFT)));
37891eaf3e1SJohn Birrell }
37991eaf3e1SJohn Birrell 
38091eaf3e1SJohn Birrell uint64_t
dtrace_gethrestime(void)38191eaf3e1SJohn Birrell dtrace_gethrestime(void)
38291eaf3e1SJohn Birrell {
38357d025c3SGeorge V. Neville-Neil 	struct timespec current_time;
38457d025c3SGeorge V. Neville-Neil 
38557d025c3SGeorge V. Neville-Neil 	dtrace_getnanotime(&current_time);
38657d025c3SGeorge V. Neville-Neil 
387d638e8dcSGeorge V. Neville-Neil 	return (current_time.tv_sec * 1000000000ULL + current_time.tv_nsec);
38891eaf3e1SJohn Birrell }
38991eaf3e1SJohn Birrell 
3905a5f9d21SMark Johnston /* Function to handle DTrace traps during probes. See amd64/amd64/trap.c. */
39191eaf3e1SJohn Birrell int
dtrace_trap(struct trapframe * frame,u_int type)392cafe8744SMark Johnston dtrace_trap(struct trapframe *frame, u_int type)
39391eaf3e1SJohn Birrell {
394a11ac730SMark Johnston 	uint16_t nofault;
395a11ac730SMark Johnston 
39691eaf3e1SJohn Birrell 	/*
39791eaf3e1SJohn Birrell 	 * A trap can occur while DTrace executes a probe. Before
39891eaf3e1SJohn Birrell 	 * executing the probe, DTrace blocks re-scheduling and sets
399291624fdSMark Johnston 	 * a flag in its per-cpu flags to indicate that it doesn't
4006bccea7cSRebecca Cran 	 * want to fault. On returning from the probe, the no-fault
40191eaf3e1SJohn Birrell 	 * flag is cleared and finally re-scheduling is enabled.
40291eaf3e1SJohn Birrell 	 *
40391eaf3e1SJohn Birrell 	 * Check if DTrace has enabled 'no-fault' mode:
40491eaf3e1SJohn Birrell 	 */
405a11ac730SMark Johnston 	sched_pin();
406a11ac730SMark Johnston 	nofault = cpu_core[curcpu].cpuc_dtrace_flags & CPU_DTRACE_NOFAULT;
407a11ac730SMark Johnston 	sched_unpin();
408a11ac730SMark Johnston 	if (nofault) {
409a11ac730SMark Johnston 		KASSERT((read_rflags() & PSL_I) == 0, ("interrupts enabled"));
410a11ac730SMark Johnston 
41191eaf3e1SJohn Birrell 		/*
41291eaf3e1SJohn Birrell 		 * There are only a couple of trap types that are expected.
41391eaf3e1SJohn Birrell 		 * All the rest will be handled in the usual way.
41491eaf3e1SJohn Birrell 		 */
415cafe8744SMark Johnston 		switch (type) {
41691eaf3e1SJohn Birrell 		/* General protection fault. */
41791eaf3e1SJohn Birrell 		case T_PROTFLT:
41891eaf3e1SJohn Birrell 			/* Flag an illegal operation. */
41991eaf3e1SJohn Birrell 			cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP;
42091eaf3e1SJohn Birrell 
42191eaf3e1SJohn Birrell 			/*
42291eaf3e1SJohn Birrell 			 * Offset the instruction pointer to the instruction
42391eaf3e1SJohn Birrell 			 * following the one causing the fault.
42491eaf3e1SJohn Birrell 			 */
4251a149d65SChristos Margiolis 			frame->tf_rip += dtrace_instr_size((uint8_t *) frame->tf_rip);
42691eaf3e1SJohn Birrell 			return (1);
42791eaf3e1SJohn Birrell 		/* Page fault. */
42891eaf3e1SJohn Birrell 		case T_PAGEFLT:
42991eaf3e1SJohn Birrell 			/* Flag a bad address. */
43091eaf3e1SJohn Birrell 			cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_BADADDR;
43191eaf3e1SJohn Birrell 			cpu_core[curcpu].cpuc_dtrace_illval = frame->tf_addr;
43291eaf3e1SJohn Birrell 
43391eaf3e1SJohn Birrell 			/*
43491eaf3e1SJohn Birrell 			 * Offset the instruction pointer to the instruction
43591eaf3e1SJohn Birrell 			 * following the one causing the fault.
43691eaf3e1SJohn Birrell 			 */
4371a149d65SChristos Margiolis 			frame->tf_rip += dtrace_instr_size((uint8_t *) frame->tf_rip);
43891eaf3e1SJohn Birrell 			return (1);
43991eaf3e1SJohn Birrell 		default:
44091eaf3e1SJohn Birrell 			/* Handle all other traps in the usual way. */
44191eaf3e1SJohn Birrell 			break;
44291eaf3e1SJohn Birrell 		}
44391eaf3e1SJohn Birrell 	}
44491eaf3e1SJohn Birrell 
44591eaf3e1SJohn Birrell 	/* Handle the trap in the usual way. */
44691eaf3e1SJohn Birrell 	return (0);
44791eaf3e1SJohn Birrell }
448