1/*	$NetBSD: dtrace_load.c,v 1.3 2011/08/31 21:57:16 christos 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: src/sys/cddl/dev/dtrace/dtrace_load.c,v 1.2.2.1 2009/08/03 08:13:06 kensmith Exp $
24 *
25 */
26
27static void dtrace_debug_init(void *);
28static void dtrace_anon_init(void *dummy);
29void dtrace_gethrtime_init(void *);
30
31int dtrace_helptrace_size=0;
32
33#ifndef mutex_init
34#define	mutex_init(a, b, c, d)	mutex_init(a, c, IPL_NONE)
35#endif
36
37static void
38dtrace_load(void *dummy)
39{
40	dtrace_provider_id_t id;
41	CPU_INFO_ITERATOR cpuind;
42	struct cpu_info *cinfo;
43
44	dtrace_debug_init(NULL);
45	dtrace_gethrtime_init(NULL);
46
47	/* Hook into the trap handler. */
48	dtrace_trap_func = dtrace_trap;
49
50	/* Hang our hook for thread switches. */
51	dtrace_vtime_switch_func = dtrace_vtime_switch;
52
53	/* Hang our hook for exceptions. */
54	dtrace_invop_init();
55
56	/*
57	 * XXX This is a short term hack to avoid having to comment
58	 * out lots and lots of lock/unlock calls.
59	 */
60	mutex_init(&mod_lock,"XXX mod_lock hack", MUTEX_DEFAULT, NULL);
61
62	/*
63	 * Initialise the mutexes without 'witness' because the dtrace
64	 * code is mostly written to wait for memory. To have the
65	 * witness code change a malloc() from M_WAITOK to M_NOWAIT
66	 * because a lock is held would surely create a panic in a
67	 * low memory situation. And that low memory situation might be
68	 * the very problem we are trying to trace.
69	 */
70	mutex_init(&dtrace_lock,"dtrace probe state", MUTEX_DEFAULT, NULL);
71	mutex_init(&dtrace_provider_lock,"dtrace provider state", MUTEX_DEFAULT, NULL);
72	mutex_init(&dtrace_meta_lock,"dtrace meta-provider state", MUTEX_DEFAULT, NULL);
73	mutex_init(&dtrace_errlock,"dtrace error lock", MUTEX_DEFAULT, NULL);
74
75	mutex_enter(&dtrace_provider_lock);
76	mutex_enter(&dtrace_lock);
77	mutex_enter(&cpu_lock);
78
79	ASSERT(MUTEX_HELD(&cpu_lock));
80
81	dtrace_arena = vmem_create("dtrace", 1, INT_MAX, 1,
82			NULL, NULL, NULL, 0, VM_SLEEP, IPL_NONE);
83
84	dtrace_state_cache = kmem_cache_create(__UNCONST("dtrace_state_cache"),
85	    sizeof (dtrace_dstate_percpu_t) * NCPU, DTRACE_STATE_ALIGN,
86	    NULL, NULL, NULL, NULL, NULL, 0);
87
88	ASSERT(MUTEX_HELD(&cpu_lock));
89	dtrace_bymod = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_mod),
90	    offsetof(dtrace_probe_t, dtpr_nextmod),
91	    offsetof(dtrace_probe_t, dtpr_prevmod));
92
93	dtrace_byfunc = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_func),
94	    offsetof(dtrace_probe_t, dtpr_nextfunc),
95	    offsetof(dtrace_probe_t, dtpr_prevfunc));
96
97	dtrace_byname = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_name),
98	    offsetof(dtrace_probe_t, dtpr_nextname),
99	    offsetof(dtrace_probe_t, dtpr_prevname));
100
101	if (dtrace_retain_max < 1) {
102		cmn_err(CE_WARN, "illegal value (%lu) for dtrace_retain_max; "
103		    "setting to 1", dtrace_retain_max);
104		dtrace_retain_max = 1;
105	}
106
107	/*
108	 * Now discover our toxic ranges.
109	 */
110	dtrace_toxic_ranges(dtrace_toxrange_add);
111
112	/*
113	 * Before we register ourselves as a provider to our own framework,
114	 * we would like to assert that dtrace_provider is NULL -- but that's
115	 * not true if we were loaded as a dependency of a DTrace provider.
116	 * Once we've registered, we can assert that dtrace_provider is our
117	 * pseudo provider.
118	 */
119	(void) dtrace_register("dtrace", &dtrace_provider_attr,
120	    DTRACE_PRIV_NONE, 0, &dtrace_provider_ops, NULL, &id);
121
122	ASSERT(dtrace_provider != NULL);
123	ASSERT((dtrace_provider_id_t)dtrace_provider == id);
124
125	dtrace_probeid_begin = dtrace_probe_create((dtrace_provider_id_t)
126	    dtrace_provider, NULL, NULL, "BEGIN", 0, NULL);
127	dtrace_probeid_end = dtrace_probe_create((dtrace_provider_id_t)
128	    dtrace_provider, NULL, NULL, "END", 0, NULL);
129	dtrace_probeid_error = dtrace_probe_create((dtrace_provider_id_t)
130	    dtrace_provider, NULL, NULL, "ERROR", 1, NULL);
131
132	mutex_exit(&cpu_lock);
133
134	/*
135	 * If DTrace helper tracing is enabled, we need to allocate the
136	 * trace buffer and initialize the values.
137	 */
138	if (dtrace_helptrace_enabled) {
139		ASSERT(dtrace_helptrace_buffer == NULL);
140		dtrace_helptrace_buffer =
141		    kmem_zalloc(dtrace_helptrace_bufsize, KM_SLEEP);
142		dtrace_helptrace_next = 0;
143		dtrace_helptrace_size = dtrace_helptrace_bufsize;
144	}
145
146	mutex_exit(&dtrace_lock);
147	mutex_exit(&dtrace_provider_lock);
148
149	mutex_enter(&cpu_lock);
150
151	/* Setup the CPUs */
152	for (CPU_INFO_FOREACH(cpuind, cinfo)) {
153		(void) dtrace_cpu_setup(CPU_CONFIG, cpu_index(cinfo));
154	}
155
156	mutex_exit(&cpu_lock);
157
158	dtrace_anon_init(NULL);
159#if 0
160	dtrace_dev = make_dev(&dtrace_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, "dtrace/dtrace");
161#endif
162
163	return;
164}
165