xref: /freebsd/sys/cddl/dev/dtrace/dtrace_load.c (revision 95ee2897)
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 (the "License").
691eaf3e1SJohn Birrell  * You may not use this file except in compliance with the License.
791eaf3e1SJohn Birrell  *
891eaf3e1SJohn Birrell  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
991eaf3e1SJohn Birrell  * or http://www.opensolaris.org/os/licensing.
1091eaf3e1SJohn Birrell  * See the License for the specific language governing permissions
1191eaf3e1SJohn Birrell  * and limitations under the License.
1291eaf3e1SJohn Birrell  *
1391eaf3e1SJohn Birrell  * When distributing Covered Code, include this CDDL HEADER in each
1491eaf3e1SJohn Birrell  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1591eaf3e1SJohn Birrell  * If applicable, add the following below this CDDL HEADER, with the
1691eaf3e1SJohn Birrell  * fields enclosed by brackets "[]" replaced with your own identifying
1791eaf3e1SJohn Birrell  * information: Portions Copyright [yyyy] [name of copyright owner]
1891eaf3e1SJohn Birrell  *
1991eaf3e1SJohn Birrell  * CDDL HEADER END
2091eaf3e1SJohn Birrell  *
2191eaf3e1SJohn Birrell  */
2291eaf3e1SJohn Birrell 
23fdce57a0SJohn Baldwin #ifndef EARLY_AP_STARTUP
2491eaf3e1SJohn Birrell static void
dtrace_ap_start(void * dummy)2591eaf3e1SJohn Birrell dtrace_ap_start(void *dummy)
2691eaf3e1SJohn Birrell {
2791eaf3e1SJohn Birrell 	int i;
2891eaf3e1SJohn Birrell 
2991eaf3e1SJohn Birrell 	mutex_enter(&cpu_lock);
3091eaf3e1SJohn Birrell 
3191eaf3e1SJohn Birrell 	/* Setup the rest of the CPUs. */
323aa6d94eSJohn Baldwin 	CPU_FOREACH(i) {
333aa6d94eSJohn Baldwin 		if (i == 0)
3491eaf3e1SJohn Birrell 			continue;
3591eaf3e1SJohn Birrell 
3691eaf3e1SJohn Birrell 		(void) dtrace_cpu_setup(CPU_CONFIG, i);
3791eaf3e1SJohn Birrell 	}
3891eaf3e1SJohn Birrell 
3991eaf3e1SJohn Birrell 	mutex_exit(&cpu_lock);
4091eaf3e1SJohn Birrell }
4191eaf3e1SJohn Birrell 
4291eaf3e1SJohn Birrell SYSINIT(dtrace_ap_start, SI_SUB_SMP, SI_ORDER_ANY, dtrace_ap_start, NULL);
43fdce57a0SJohn Baldwin #endif
4491eaf3e1SJohn Birrell 
4591eaf3e1SJohn Birrell static void
dtrace_load(void * dummy)4691eaf3e1SJohn Birrell dtrace_load(void *dummy)
4791eaf3e1SJohn Birrell {
4891eaf3e1SJohn Birrell 	dtrace_provider_id_t id;
49fdce57a0SJohn Baldwin #ifdef EARLY_AP_STARTUP
50fdce57a0SJohn Baldwin 	int i;
51fdce57a0SJohn Baldwin #endif
5291eaf3e1SJohn Birrell 
53cdaa8777SGeorge V. Neville-Neil #ifndef illumos
54cdaa8777SGeorge V. Neville-Neil 	/*
55cdaa8777SGeorge V. Neville-Neil 	 * DTrace uses negative logic for the destructive mode switch, so it
56cdaa8777SGeorge V. Neville-Neil 	 * is required to translate from the sysctl which uses positive logic.
57cdaa8777SGeorge V. Neville-Neil 	 */
58cdaa8777SGeorge V. Neville-Neil 	if (dtrace_allow_destructive)
59cdaa8777SGeorge V. Neville-Neil 		dtrace_destructive_disallow = 0;
60cdaa8777SGeorge V. Neville-Neil 	else
61cdaa8777SGeorge V. Neville-Neil 		dtrace_destructive_disallow = 1;
62cdaa8777SGeorge V. Neville-Neil #endif
63cdaa8777SGeorge V. Neville-Neil 
6491eaf3e1SJohn Birrell 	/* Hook into the trap handler. */
6591eaf3e1SJohn Birrell 	dtrace_trap_func = dtrace_trap;
6691eaf3e1SJohn Birrell 
6791eaf3e1SJohn Birrell 	/* Hang our hook for thread switches. */
6891eaf3e1SJohn Birrell 	dtrace_vtime_switch_func = dtrace_vtime_switch;
6991eaf3e1SJohn Birrell 
7091eaf3e1SJohn Birrell 	/* Hang our hook for exceptions. */
7191eaf3e1SJohn Birrell 	dtrace_invop_init();
7291eaf3e1SJohn Birrell 
73cb7320ceSMark Johnston 	dtrace_taskq = taskq_create("dtrace_taskq", 1, maxclsyspri, 0, 0, 0);
74cb7320ceSMark Johnston 
75b69b2ff5SMark Johnston 	dtrace_arena = new_unrhdr(1, INT_MAX, &dtrace_unr_mtx);
76b69b2ff5SMark Johnston 
7712ede07aSMark Johnston 	/* Register callbacks for linker file load and unload events. */
7812ede07aSMark Johnston 	dtrace_kld_load_tag = EVENTHANDLER_REGISTER(kld_load,
7912ede07aSMark Johnston 	    dtrace_kld_load, NULL, EVENTHANDLER_PRI_ANY);
8029f4e216SMark Johnston 	dtrace_kld_unload_try_tag = EVENTHANDLER_REGISTER(kld_unload_try,
8129f4e216SMark Johnston 	    dtrace_kld_unload_try, NULL, EVENTHANDLER_PRI_ANY);
828776669bSMark Johnston 
8391eaf3e1SJohn Birrell 	/*
8491eaf3e1SJohn Birrell 	 * Initialise the mutexes without 'witness' because the dtrace
8591eaf3e1SJohn Birrell 	 * code is mostly written to wait for memory. To have the
8691eaf3e1SJohn Birrell 	 * witness code change a malloc() from M_WAITOK to M_NOWAIT
8791eaf3e1SJohn Birrell 	 * because a lock is held would surely create a panic in a
8891eaf3e1SJohn Birrell 	 * low memory situation. And that low memory situation might be
8991eaf3e1SJohn Birrell 	 * the very problem we are trying to trace.
9091eaf3e1SJohn Birrell 	 */
9191eaf3e1SJohn Birrell 	mutex_init(&dtrace_lock,"dtrace probe state", MUTEX_DEFAULT, NULL);
9291eaf3e1SJohn Birrell 	mutex_init(&dtrace_provider_lock,"dtrace provider state", MUTEX_DEFAULT, NULL);
9391eaf3e1SJohn Birrell 	mutex_init(&dtrace_meta_lock,"dtrace meta-provider state", MUTEX_DEFAULT, NULL);
94c319ea15SAndriy Gapon #ifdef DEBUG
9591eaf3e1SJohn Birrell 	mutex_init(&dtrace_errlock,"dtrace error lock", MUTEX_DEFAULT, NULL);
96c319ea15SAndriy Gapon #endif
9791eaf3e1SJohn Birrell 
9823bff607SMark Johnston 	mutex_enter(&cpu_lock);
9991eaf3e1SJohn Birrell 	mutex_enter(&dtrace_provider_lock);
10091eaf3e1SJohn Birrell 	mutex_enter(&dtrace_lock);
10191eaf3e1SJohn Birrell 
10291eaf3e1SJohn Birrell 	dtrace_state_cache = kmem_cache_create("dtrace_state_cache",
10391eaf3e1SJohn Birrell 	    sizeof (dtrace_dstate_percpu_t) * NCPU, DTRACE_STATE_ALIGN,
10491eaf3e1SJohn Birrell 	    NULL, NULL, NULL, NULL, NULL, 0);
10591eaf3e1SJohn Birrell 
10691eaf3e1SJohn Birrell 	ASSERT(MUTEX_HELD(&cpu_lock));
10791eaf3e1SJohn Birrell 	dtrace_bymod = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_mod),
10891eaf3e1SJohn Birrell 	    offsetof(dtrace_probe_t, dtpr_nextmod),
10991eaf3e1SJohn Birrell 	    offsetof(dtrace_probe_t, dtpr_prevmod));
11091eaf3e1SJohn Birrell 
11191eaf3e1SJohn Birrell 	dtrace_byfunc = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_func),
11291eaf3e1SJohn Birrell 	    offsetof(dtrace_probe_t, dtpr_nextfunc),
11391eaf3e1SJohn Birrell 	    offsetof(dtrace_probe_t, dtpr_prevfunc));
11491eaf3e1SJohn Birrell 
11591eaf3e1SJohn Birrell 	dtrace_byname = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_name),
11691eaf3e1SJohn Birrell 	    offsetof(dtrace_probe_t, dtpr_nextname),
11791eaf3e1SJohn Birrell 	    offsetof(dtrace_probe_t, dtpr_prevname));
11891eaf3e1SJohn Birrell 
11991eaf3e1SJohn Birrell 	if (dtrace_retain_max < 1) {
12066b8ecedSKonstantin Belousov 		cmn_err(CE_WARN, "illegal value (%zu) for dtrace_retain_max; "
12191eaf3e1SJohn Birrell 		    "setting to 1", dtrace_retain_max);
12291eaf3e1SJohn Birrell 		dtrace_retain_max = 1;
12391eaf3e1SJohn Birrell 	}
12491eaf3e1SJohn Birrell 
12591eaf3e1SJohn Birrell 	/*
12691eaf3e1SJohn Birrell 	 * Now discover our toxic ranges.
12791eaf3e1SJohn Birrell 	 */
12891eaf3e1SJohn Birrell 	dtrace_toxic_ranges(dtrace_toxrange_add);
12991eaf3e1SJohn Birrell 
13091eaf3e1SJohn Birrell 	/*
13191eaf3e1SJohn Birrell 	 * Before we register ourselves as a provider to our own framework,
13291eaf3e1SJohn Birrell 	 * we would like to assert that dtrace_provider is NULL -- but that's
13391eaf3e1SJohn Birrell 	 * not true if we were loaded as a dependency of a DTrace provider.
13491eaf3e1SJohn Birrell 	 * Once we've registered, we can assert that dtrace_provider is our
13591eaf3e1SJohn Birrell 	 * pseudo provider.
13691eaf3e1SJohn Birrell 	 */
13791eaf3e1SJohn Birrell 	(void) dtrace_register("dtrace", &dtrace_provider_attr,
13891eaf3e1SJohn Birrell 	    DTRACE_PRIV_NONE, 0, &dtrace_provider_ops, NULL, &id);
13991eaf3e1SJohn Birrell 
14091eaf3e1SJohn Birrell 	ASSERT(dtrace_provider != NULL);
14191eaf3e1SJohn Birrell 	ASSERT((dtrace_provider_id_t)dtrace_provider == id);
14291eaf3e1SJohn Birrell 
14391eaf3e1SJohn Birrell 	dtrace_probeid_begin = dtrace_probe_create((dtrace_provider_id_t)
14491eaf3e1SJohn Birrell 	    dtrace_provider, NULL, NULL, "BEGIN", 0, NULL);
14591eaf3e1SJohn Birrell 	dtrace_probeid_end = dtrace_probe_create((dtrace_provider_id_t)
14691eaf3e1SJohn Birrell 	    dtrace_provider, NULL, NULL, "END", 0, NULL);
14791eaf3e1SJohn Birrell 	dtrace_probeid_error = dtrace_probe_create((dtrace_provider_id_t)
14891eaf3e1SJohn Birrell 	    dtrace_provider, NULL, NULL, "ERROR", 1, NULL);
14991eaf3e1SJohn Birrell 
15091eaf3e1SJohn Birrell 	mutex_exit(&dtrace_lock);
15191eaf3e1SJohn Birrell 	mutex_exit(&dtrace_provider_lock);
15291eaf3e1SJohn Birrell 
153fdce57a0SJohn Baldwin #ifdef EARLY_AP_STARTUP
154fdce57a0SJohn Baldwin 	CPU_FOREACH(i) {
155fdce57a0SJohn Baldwin 		(void) dtrace_cpu_setup(CPU_CONFIG, i);
156fdce57a0SJohn Baldwin 	}
157fdce57a0SJohn Baldwin #else
15891eaf3e1SJohn Birrell 	/* Setup the boot CPU */
15991eaf3e1SJohn Birrell 	(void) dtrace_cpu_setup(CPU_CONFIG, 0);
160fdce57a0SJohn Baldwin #endif
16191eaf3e1SJohn Birrell 
16291eaf3e1SJohn Birrell 	mutex_exit(&cpu_lock);
16391eaf3e1SJohn Birrell 
164c6f5742fSRui Paulo 	dtrace_dev = make_dev(&dtrace_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600,
165c6f5742fSRui Paulo 	    "dtrace/dtrace");
166ea950d20SRui Paulo 	helper_dev = make_dev(&helper_cdevsw, 0, UID_ROOT, GID_WHEEL, 0660,
167c6f5742fSRui Paulo 	    "dtrace/helper");
16891eaf3e1SJohn Birrell }
169