xref: /freebsd/sys/cddl/dev/dtrace/dtrace_load.c (revision 5d12db2dafece9f6a0453c4a45c4abed6b1e15ec)
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  */
22 
23 #ifndef EARLY_AP_STARTUP
24 static void
dtrace_ap_start(void * dummy)25 dtrace_ap_start(void *dummy)
26 {
27 	int i;
28 
29 	mutex_enter(&cpu_lock);
30 
31 	/* Setup the rest of the CPUs. */
32 	CPU_FOREACH(i) {
33 		if (i == 0)
34 			continue;
35 
36 		(void) dtrace_cpu_setup(CPU_CONFIG, i);
37 	}
38 
39 	mutex_exit(&cpu_lock);
40 }
41 
42 SYSINIT(dtrace_ap_start, SI_SUB_SMP, SI_ORDER_ANY, dtrace_ap_start, NULL);
43 #endif
44 
45 static void
dtrace_load(void * dummy)46 dtrace_load(void *dummy)
47 {
48 	dtrace_provider_id_t id;
49 #ifdef EARLY_AP_STARTUP
50 	int i;
51 #endif
52 
53 #ifndef illumos
54 	/*
55 	 * DTrace uses negative logic for the destructive mode switch, so it
56 	 * is required to translate from the sysctl which uses positive logic.
57 	 */
58 	if (dtrace_allow_destructive)
59 		dtrace_destructive_disallow = 0;
60 	else
61 		dtrace_destructive_disallow = 1;
62 #endif
63 
64 	/* Hook into the trap handler. */
65 	dtrace_trap_func = dtrace_trap;
66 
67 	/* Hang our hook for thread switches. */
68 	dtrace_vtime_switch_func = dtrace_vtime_switch;
69 
70 	/* Hang our hook for exceptions. */
71 	dtrace_invop_init();
72 
73 	dtrace_taskq = taskq_create("dtrace_taskq", 1, maxclsyspri, 0, 0, 0);
74 
75 	dtrace_arena = new_unrhdr(1, INT_MAX, &dtrace_unr_mtx);
76 
77 	/* Register callbacks for linker file load and unload events. */
78 	dtrace_kld_load_tag = EVENTHANDLER_REGISTER(kld_load,
79 	    dtrace_kld_load, NULL, EVENTHANDLER_PRI_ANY);
80 	dtrace_kld_unload_try_tag = EVENTHANDLER_REGISTER(kld_unload_try,
81 	    dtrace_kld_unload_try, NULL, EVENTHANDLER_PRI_ANY);
82 
83 	/*
84 	 * Initialise the mutexes without 'witness' because the dtrace
85 	 * code is mostly written to wait for memory. To have the
86 	 * witness code change a malloc() from M_WAITOK to M_NOWAIT
87 	 * because a lock is held would surely create a panic in a
88 	 * low memory situation. And that low memory situation might be
89 	 * the very problem we are trying to trace.
90 	 */
91 	mutex_init(&dtrace_lock,"dtrace probe state", MUTEX_DEFAULT, NULL);
92 	mutex_init(&dtrace_provider_lock,"dtrace provider state", MUTEX_DEFAULT, NULL);
93 	mutex_init(&dtrace_meta_lock,"dtrace meta-provider state", MUTEX_DEFAULT, NULL);
94 #ifdef DEBUG
95 	mutex_init(&dtrace_errlock,"dtrace error lock", MUTEX_DEFAULT, NULL);
96 #endif
97 
98 	mutex_enter(&cpu_lock);
99 	mutex_enter(&dtrace_provider_lock);
100 	mutex_enter(&dtrace_lock);
101 
102 	dtrace_state_cache = kmem_cache_create("dtrace_state_cache",
103 	    sizeof (dtrace_dstate_percpu_t) * (mp_maxid + 1),
104 	    DTRACE_STATE_ALIGN, NULL, NULL, NULL, NULL, NULL, 0);
105 
106 	ASSERT(MUTEX_HELD(&cpu_lock));
107 	dtrace_bymod = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_mod),
108 	    offsetof(dtrace_probe_t, dtpr_nextmod),
109 	    offsetof(dtrace_probe_t, dtpr_prevmod));
110 
111 	dtrace_byfunc = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_func),
112 	    offsetof(dtrace_probe_t, dtpr_nextfunc),
113 	    offsetof(dtrace_probe_t, dtpr_prevfunc));
114 
115 	dtrace_byname = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_name),
116 	    offsetof(dtrace_probe_t, dtpr_nextname),
117 	    offsetof(dtrace_probe_t, dtpr_prevname));
118 
119 	if (dtrace_retain_max < 1) {
120 		cmn_err(CE_WARN, "illegal value (%zu) for dtrace_retain_max; "
121 		    "setting to 1", dtrace_retain_max);
122 		dtrace_retain_max = 1;
123 	}
124 
125 	/*
126 	 * Now discover our toxic ranges.
127 	 */
128 	dtrace_toxic_ranges(dtrace_toxrange_add);
129 
130 	/*
131 	 * Before we register ourselves as a provider to our own framework,
132 	 * we would like to assert that dtrace_provider is NULL -- but that's
133 	 * not true if we were loaded as a dependency of a DTrace provider.
134 	 * Once we've registered, we can assert that dtrace_provider is our
135 	 * pseudo provider.
136 	 */
137 	(void) dtrace_register("dtrace", &dtrace_provider_attr,
138 	    DTRACE_PRIV_NONE, 0, &dtrace_provider_ops, NULL, &id);
139 
140 	ASSERT(dtrace_provider != NULL);
141 	ASSERT((dtrace_provider_id_t)dtrace_provider == id);
142 
143 	dtrace_probeid_begin = dtrace_probe_create((dtrace_provider_id_t)
144 	    dtrace_provider, NULL, NULL, "BEGIN", 0, NULL);
145 	dtrace_probeid_end = dtrace_probe_create((dtrace_provider_id_t)
146 	    dtrace_provider, NULL, NULL, "END", 0, NULL);
147 	dtrace_probeid_error = dtrace_probe_create((dtrace_provider_id_t)
148 	    dtrace_provider, NULL, NULL, "ERROR", 1, NULL);
149 
150 	mutex_exit(&dtrace_lock);
151 	mutex_exit(&dtrace_provider_lock);
152 
153 #ifdef EARLY_AP_STARTUP
154 	CPU_FOREACH(i) {
155 		(void) dtrace_cpu_setup(CPU_CONFIG, i);
156 	}
157 #else
158 	/* Setup the boot CPU */
159 	(void) dtrace_cpu_setup(CPU_CONFIG, 0);
160 #endif
161 
162 	mutex_exit(&cpu_lock);
163 
164 	dtrace_dev = make_dev(&dtrace_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600,
165 	    "dtrace/dtrace");
166 	helper_dev = make_dev(&helper_cdevsw, 0, UID_ROOT, GID_WHEEL, 0660,
167 	    "dtrace/helper");
168 }
169