xref: /freebsd/sys/cddl/dev/dtrace/dtrace_load.c (revision 0f7f3352c8bc463607912e2463d13e52d44a4cae)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  *
21  * $FreeBSD$
22  *
23  */
24 
25 #ifndef EARLY_AP_STARTUP
26 static void
27 dtrace_ap_start(void *dummy)
28 {
29 	int i;
30 
31 	mutex_enter(&cpu_lock);
32 
33 	/* Setup the rest of the CPUs. */
34 	CPU_FOREACH(i) {
35 		if (i == 0)
36 			continue;
37 
38 		(void) dtrace_cpu_setup(CPU_CONFIG, i);
39 	}
40 
41 	mutex_exit(&cpu_lock);
42 }
43 
44 SYSINIT(dtrace_ap_start, SI_SUB_SMP, SI_ORDER_ANY, dtrace_ap_start, NULL);
45 #endif
46 
47 static void
48 dtrace_load(void *dummy)
49 {
50 	dtrace_provider_id_t id;
51 #ifdef EARLY_AP_STARTUP
52 	int i;
53 #endif
54 
55 	/* Hook into the trap handler. */
56 	dtrace_trap_func = dtrace_trap;
57 
58 	/* Hang our hook for thread switches. */
59 	dtrace_vtime_switch_func = dtrace_vtime_switch;
60 
61 	/* Hang our hook for exceptions. */
62 	dtrace_invop_init();
63 
64 	dtrace_taskq = taskq_create("dtrace_taskq", 1, maxclsyspri, 0, 0, 0);
65 
66 	dtrace_arena = new_unrhdr(1, INT_MAX, &dtrace_unr_mtx);
67 
68 	/* Register callbacks for linker file load and unload events. */
69 	dtrace_kld_load_tag = EVENTHANDLER_REGISTER(kld_load,
70 	    dtrace_kld_load, NULL, EVENTHANDLER_PRI_ANY);
71 	dtrace_kld_unload_try_tag = EVENTHANDLER_REGISTER(kld_unload_try,
72 	    dtrace_kld_unload_try, NULL, EVENTHANDLER_PRI_ANY);
73 
74 	/*
75 	 * Initialise the mutexes without 'witness' because the dtrace
76 	 * code is mostly written to wait for memory. To have the
77 	 * witness code change a malloc() from M_WAITOK to M_NOWAIT
78 	 * because a lock is held would surely create a panic in a
79 	 * low memory situation. And that low memory situation might be
80 	 * the very problem we are trying to trace.
81 	 */
82 	mutex_init(&dtrace_lock,"dtrace probe state", MUTEX_DEFAULT, NULL);
83 	mutex_init(&dtrace_provider_lock,"dtrace provider state", MUTEX_DEFAULT, NULL);
84 	mutex_init(&dtrace_meta_lock,"dtrace meta-provider state", MUTEX_DEFAULT, NULL);
85 #ifdef DEBUG
86 	mutex_init(&dtrace_errlock,"dtrace error lock", MUTEX_DEFAULT, NULL);
87 #endif
88 
89 	mutex_enter(&dtrace_provider_lock);
90 	mutex_enter(&dtrace_lock);
91 	mutex_enter(&cpu_lock);
92 
93 	ASSERT(MUTEX_HELD(&cpu_lock));
94 
95 	dtrace_state_cache = kmem_cache_create("dtrace_state_cache",
96 	    sizeof (dtrace_dstate_percpu_t) * NCPU, DTRACE_STATE_ALIGN,
97 	    NULL, NULL, NULL, NULL, NULL, 0);
98 
99 	ASSERT(MUTEX_HELD(&cpu_lock));
100 	dtrace_bymod = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_mod),
101 	    offsetof(dtrace_probe_t, dtpr_nextmod),
102 	    offsetof(dtrace_probe_t, dtpr_prevmod));
103 
104 	dtrace_byfunc = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_func),
105 	    offsetof(dtrace_probe_t, dtpr_nextfunc),
106 	    offsetof(dtrace_probe_t, dtpr_prevfunc));
107 
108 	dtrace_byname = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_name),
109 	    offsetof(dtrace_probe_t, dtpr_nextname),
110 	    offsetof(dtrace_probe_t, dtpr_prevname));
111 
112 	if (dtrace_retain_max < 1) {
113 		cmn_err(CE_WARN, "illegal value (%lu) for dtrace_retain_max; "
114 		    "setting to 1", dtrace_retain_max);
115 		dtrace_retain_max = 1;
116 	}
117 
118 	/*
119 	 * Now discover our toxic ranges.
120 	 */
121 	dtrace_toxic_ranges(dtrace_toxrange_add);
122 
123 	/*
124 	 * Before we register ourselves as a provider to our own framework,
125 	 * we would like to assert that dtrace_provider is NULL -- but that's
126 	 * not true if we were loaded as a dependency of a DTrace provider.
127 	 * Once we've registered, we can assert that dtrace_provider is our
128 	 * pseudo provider.
129 	 */
130 	(void) dtrace_register("dtrace", &dtrace_provider_attr,
131 	    DTRACE_PRIV_NONE, 0, &dtrace_provider_ops, NULL, &id);
132 
133 	ASSERT(dtrace_provider != NULL);
134 	ASSERT((dtrace_provider_id_t)dtrace_provider == id);
135 
136 	dtrace_probeid_begin = dtrace_probe_create((dtrace_provider_id_t)
137 	    dtrace_provider, NULL, NULL, "BEGIN", 0, NULL);
138 	dtrace_probeid_end = dtrace_probe_create((dtrace_provider_id_t)
139 	    dtrace_provider, NULL, NULL, "END", 0, NULL);
140 	dtrace_probeid_error = dtrace_probe_create((dtrace_provider_id_t)
141 	    dtrace_provider, NULL, NULL, "ERROR", 1, NULL);
142 
143 	mutex_exit(&cpu_lock);
144 
145 	mutex_exit(&dtrace_lock);
146 	mutex_exit(&dtrace_provider_lock);
147 
148 	mutex_enter(&cpu_lock);
149 
150 #ifdef EARLY_AP_STARTUP
151 	CPU_FOREACH(i) {
152 		(void) dtrace_cpu_setup(CPU_CONFIG, i);
153 	}
154 #else
155 	/* Setup the boot CPU */
156 	(void) dtrace_cpu_setup(CPU_CONFIG, 0);
157 #endif
158 
159 	mutex_exit(&cpu_lock);
160 
161 	dtrace_dev = make_dev(&dtrace_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600,
162 	    "dtrace/dtrace");
163 	helper_dev = make_dev(&helper_cdevsw, 0, UID_ROOT, GID_WHEEL, 0660,
164 	    "dtrace/helper");
165 
166 	return;
167 }
168