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