xref: /illumos-gate/usr/src/uts/common/tnf/tnf_res.c (revision 7c478bd9)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate /*
30*7c478bd9Sstevel@tonic-gate  * "Resident" part of TNF -- this has to be around even when the
31*7c478bd9Sstevel@tonic-gate  * driver is not loaded.
32*7c478bd9Sstevel@tonic-gate  */
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate #ifndef NPROBE
35*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/systm.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/thread.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/klwp.h>
39*7c478bd9Sstevel@tonic-gate #include <sys/proc.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
41*7c478bd9Sstevel@tonic-gate #include <sys/msacct.h>
42*7c478bd9Sstevel@tonic-gate #include <sys/tnf_com.h>
43*7c478bd9Sstevel@tonic-gate #include <sys/tnf_writer.h>
44*7c478bd9Sstevel@tonic-gate #include <sys/tnf_probe.h>
45*7c478bd9Sstevel@tonic-gate #include <sys/tnf.h>
46*7c478bd9Sstevel@tonic-gate #include <sys/debug.h>
47*7c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
48*7c478bd9Sstevel@tonic-gate #include <sys/kobj.h>
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate #include "tnf_buf.h"
51*7c478bd9Sstevel@tonic-gate #include "tnf_types.h"
52*7c478bd9Sstevel@tonic-gate #include "tnf_trace.h"
53*7c478bd9Sstevel@tonic-gate 
54*7c478bd9Sstevel@tonic-gate /*
55*7c478bd9Sstevel@tonic-gate  * Defines
56*7c478bd9Sstevel@tonic-gate  */
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate #define	TNF_PC_COUNT	8
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate /*
61*7c478bd9Sstevel@tonic-gate  * Declarations
62*7c478bd9Sstevel@tonic-gate  */
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate /*
65*7c478bd9Sstevel@tonic-gate  * TNF kernel probe management externs
66*7c478bd9Sstevel@tonic-gate  */
67*7c478bd9Sstevel@tonic-gate extern tnf_probe_control_t	*__tnf_probe_list_head;
68*7c478bd9Sstevel@tonic-gate extern tnf_tag_data_t		*__tnf_tag_list_head;
69*7c478bd9Sstevel@tonic-gate extern int			tnf_changed_probe_list;
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate /*
72*7c478bd9Sstevel@tonic-gate  * This makes the state of the TNFW_B_STOPPED bit externally visible
73*7c478bd9Sstevel@tonic-gate  * in the kernel.
74*7c478bd9Sstevel@tonic-gate  */
75*7c478bd9Sstevel@tonic-gate volatile int tnf_tracing_active = 0;
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate /*
78*7c478bd9Sstevel@tonic-gate  * The trace buffer pointer
79*7c478bd9Sstevel@tonic-gate  */
80*7c478bd9Sstevel@tonic-gate caddr_t tnf_buf;
81*7c478bd9Sstevel@tonic-gate 
82*7c478bd9Sstevel@tonic-gate /*
83*7c478bd9Sstevel@tonic-gate  * Stub definitions for tag data pointers
84*7c478bd9Sstevel@tonic-gate  */
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate /* tnf_writer module */
87*7c478bd9Sstevel@tonic-gate tnf_tag_data_t *tnf_inline_tag_data = NULL;
88*7c478bd9Sstevel@tonic-gate tnf_tag_data_t *tnf_tagged_tag_data = NULL;
89*7c478bd9Sstevel@tonic-gate tnf_tag_data_t *tnf_scalar_tag_data = NULL;
90*7c478bd9Sstevel@tonic-gate tnf_tag_data_t *tnf_char_tag_data = NULL;
91*7c478bd9Sstevel@tonic-gate tnf_tag_data_t *tnf_int8_tag_data = NULL;
92*7c478bd9Sstevel@tonic-gate tnf_tag_data_t *tnf_uint8_tag_data = NULL;
93*7c478bd9Sstevel@tonic-gate tnf_tag_data_t *tnf_int16_tag_data = NULL;
94*7c478bd9Sstevel@tonic-gate tnf_tag_data_t *tnf_uint16_tag_data = NULL;
95*7c478bd9Sstevel@tonic-gate tnf_tag_data_t *tnf_int32_tag_data = NULL;
96*7c478bd9Sstevel@tonic-gate tnf_tag_data_t *tnf_uint32_tag_data = NULL;
97*7c478bd9Sstevel@tonic-gate tnf_tag_data_t *tnf_int64_tag_data = NULL;
98*7c478bd9Sstevel@tonic-gate tnf_tag_data_t *tnf_uint64_tag_data = NULL;
99*7c478bd9Sstevel@tonic-gate tnf_tag_data_t *tnf_float32_tag_data = NULL;
100*7c478bd9Sstevel@tonic-gate tnf_tag_data_t *tnf_float64_tag_data = NULL;
101*7c478bd9Sstevel@tonic-gate tnf_tag_data_t *tnf_array_tag_data = NULL;
102*7c478bd9Sstevel@tonic-gate tnf_tag_data_t *tnf_string_tag_data = NULL;
103*7c478bd9Sstevel@tonic-gate tnf_tag_data_t *tnf_type_array_tag_data = NULL;
104*7c478bd9Sstevel@tonic-gate tnf_tag_data_t *tnf_name_array_tag_data = NULL;
105*7c478bd9Sstevel@tonic-gate tnf_tag_data_t *tnf_derived_tag_data = NULL;
106*7c478bd9Sstevel@tonic-gate tnf_tag_data_t *tnf_align_tag_data = NULL;
107*7c478bd9Sstevel@tonic-gate tnf_tag_data_t *tnf_derived_base_tag_data = NULL;
108*7c478bd9Sstevel@tonic-gate tnf_tag_data_t *tnf_element_type_tag_data = NULL;
109*7c478bd9Sstevel@tonic-gate tnf_tag_data_t *tnf_header_size_tag_data = NULL;
110*7c478bd9Sstevel@tonic-gate tnf_tag_data_t *tnf_name_tag_data = NULL;
111*7c478bd9Sstevel@tonic-gate tnf_tag_data_t *tnf_opaque_tag_data = NULL;
112*7c478bd9Sstevel@tonic-gate tnf_tag_data_t *tnf_properties_tag_data = NULL;
113*7c478bd9Sstevel@tonic-gate tnf_tag_data_t *tnf_self_size_tag_data = NULL;
114*7c478bd9Sstevel@tonic-gate tnf_tag_data_t *tnf_size_tag_data = NULL;
115*7c478bd9Sstevel@tonic-gate tnf_tag_data_t *tnf_slot_names_tag_data = NULL;
116*7c478bd9Sstevel@tonic-gate tnf_tag_data_t *tnf_slot_types_tag_data = NULL;
117*7c478bd9Sstevel@tonic-gate tnf_tag_data_t *tnf_tag_tag_data = NULL;
118*7c478bd9Sstevel@tonic-gate tnf_tag_data_t *tnf_tag_arg_tag_data = NULL;
119*7c478bd9Sstevel@tonic-gate tnf_tag_data_t *tnf_type_size_tag_data = NULL;
120*7c478bd9Sstevel@tonic-gate tnf_tag_data_t *tnf_struct_tag_data = NULL;
121*7c478bd9Sstevel@tonic-gate tnf_tag_data_t *tnf_file_header_tag_data = NULL;
122*7c478bd9Sstevel@tonic-gate tnf_tag_data_t *tnf_block_header_tag_data = NULL;
123*7c478bd9Sstevel@tonic-gate tnf_tag_data_t *tnf_type_tag_data = NULL;
124*7c478bd9Sstevel@tonic-gate tnf_tag_data_t *tnf_array_type_tag_data = NULL;
125*7c478bd9Sstevel@tonic-gate tnf_tag_data_t *tnf_derived_type_tag_data = NULL;
126*7c478bd9Sstevel@tonic-gate tnf_tag_data_t *tnf_scalar_type_tag_data = NULL;
127*7c478bd9Sstevel@tonic-gate tnf_tag_data_t *tnf_struct_type_tag_data = NULL;
128*7c478bd9Sstevel@tonic-gate 
129*7c478bd9Sstevel@tonic-gate /* tnf_trace module */
130*7c478bd9Sstevel@tonic-gate tnf_tag_data_t *tnf_probe_event_tag_data = NULL;
131*7c478bd9Sstevel@tonic-gate tnf_tag_data_t *tnf_time_base_tag_data = NULL;
132*7c478bd9Sstevel@tonic-gate tnf_tag_data_t *tnf_time_delta_tag_data = NULL;
133*7c478bd9Sstevel@tonic-gate tnf_tag_data_t *tnf_pid_tag_data = NULL;
134*7c478bd9Sstevel@tonic-gate tnf_tag_data_t *tnf_lwpid_tag_data = NULL;
135*7c478bd9Sstevel@tonic-gate tnf_tag_data_t *tnf_kthread_id_tag_data = NULL;
136*7c478bd9Sstevel@tonic-gate tnf_tag_data_t *tnf_cpuid_tag_data = NULL;
137*7c478bd9Sstevel@tonic-gate tnf_tag_data_t *tnf_device_tag_data = NULL;
138*7c478bd9Sstevel@tonic-gate tnf_tag_data_t *tnf_symbol_tag_data = NULL;
139*7c478bd9Sstevel@tonic-gate tnf_tag_data_t *tnf_symbols_tag_data = NULL;
140*7c478bd9Sstevel@tonic-gate tnf_tag_data_t *tnf_sysnum_tag_data = NULL;
141*7c478bd9Sstevel@tonic-gate tnf_tag_data_t *tnf_microstate_tag_data = NULL;
142*7c478bd9Sstevel@tonic-gate tnf_tag_data_t *tnf_offset_tag_data = NULL;
143*7c478bd9Sstevel@tonic-gate tnf_tag_data_t *tnf_fault_type_tag_data = NULL;
144*7c478bd9Sstevel@tonic-gate tnf_tag_data_t *tnf_seg_access_tag_data = NULL;
145*7c478bd9Sstevel@tonic-gate tnf_tag_data_t *tnf_bioflags_tag_data = NULL;
146*7c478bd9Sstevel@tonic-gate tnf_tag_data_t *tnf_diskaddr_tag_data = NULL;
147*7c478bd9Sstevel@tonic-gate tnf_tag_data_t *tnf_kernel_schedule_tag_data = NULL;
148*7c478bd9Sstevel@tonic-gate 
149*7c478bd9Sstevel@tonic-gate tnf_tag_data_t *tnf_probe_type_tag_data = NULL;
150*7c478bd9Sstevel@tonic-gate 
151*7c478bd9Sstevel@tonic-gate /* Exported properties */
152*7c478bd9Sstevel@tonic-gate tnf_tag_data_t	***tnf_user_struct_properties = NULL;
153*7c478bd9Sstevel@tonic-gate 
154*7c478bd9Sstevel@tonic-gate /*
155*7c478bd9Sstevel@tonic-gate  * tnf_thread_create()
156*7c478bd9Sstevel@tonic-gate  * Called from thread_create() to initialize thread's tracing state.
157*7c478bd9Sstevel@tonic-gate  * XXX Do this when tracing is first turned on
158*7c478bd9Sstevel@tonic-gate  */
159*7c478bd9Sstevel@tonic-gate 
160*7c478bd9Sstevel@tonic-gate void
tnf_thread_create(kthread_t * t)161*7c478bd9Sstevel@tonic-gate tnf_thread_create(kthread_t *t)
162*7c478bd9Sstevel@tonic-gate {
163*7c478bd9Sstevel@tonic-gate 	/* If the allocation fails, this thread doesn't trace */
164*7c478bd9Sstevel@tonic-gate 	t->t_tnf_tpdp = kmem_zalloc(sizeof (tnf_ops_t), KM_NOSLEEP);
165*7c478bd9Sstevel@tonic-gate 
166*7c478bd9Sstevel@tonic-gate 	TNF_PROBE_3(thread_create, "thread", /* CSTYLED */,
167*7c478bd9Sstevel@tonic-gate 		tnf_kthread_id,	tid,		t,
168*7c478bd9Sstevel@tonic-gate 		tnf_pid,	pid,		ttoproc(t)->p_pid,
169*7c478bd9Sstevel@tonic-gate 		tnf_symbol,	start_pc,	t->t_startpc);
170*7c478bd9Sstevel@tonic-gate }
171*7c478bd9Sstevel@tonic-gate 
172*7c478bd9Sstevel@tonic-gate /*
173*7c478bd9Sstevel@tonic-gate  * tnf_thread_exit()
174*7c478bd9Sstevel@tonic-gate  * Called from thread_exit() and lwp_exit() if thread has a tpdp.
175*7c478bd9Sstevel@tonic-gate  * From this point on, we're off the allthreads list
176*7c478bd9Sstevel@tonic-gate  */
177*7c478bd9Sstevel@tonic-gate 
178*7c478bd9Sstevel@tonic-gate void
tnf_thread_exit(void)179*7c478bd9Sstevel@tonic-gate tnf_thread_exit(void)
180*7c478bd9Sstevel@tonic-gate {
181*7c478bd9Sstevel@tonic-gate 	tnf_ops_t *ops;
182*7c478bd9Sstevel@tonic-gate 	tnf_block_header_t *block;
183*7c478bd9Sstevel@tonic-gate 
184*7c478bd9Sstevel@tonic-gate 	TNF_PROBE_0(thread_exit, "thread", /* CSTYLED */);
185*7c478bd9Sstevel@tonic-gate         /* LINTED pointer cast may result in improper alignment */
186*7c478bd9Sstevel@tonic-gate 	ops = (tnf_ops_t *)curthread->t_tnf_tpdp;
187*7c478bd9Sstevel@tonic-gate 	/*
188*7c478bd9Sstevel@tonic-gate 	 * Mark ops as busy from now on, so it will never be used
189*7c478bd9Sstevel@tonic-gate 	 * again.  If we fail on the busy lock, the buffer
190*7c478bd9Sstevel@tonic-gate 	 * deallocation code is cleaning our ops, so we don't need to
191*7c478bd9Sstevel@tonic-gate 	 * do anything.  If we get the lock and the buffer exists,
192*7c478bd9Sstevel@tonic-gate 	 * release all blocks we hold.  Once we're off allthreads,
193*7c478bd9Sstevel@tonic-gate 	 * the deallocator will not examine our ops.
194*7c478bd9Sstevel@tonic-gate 	 */
195*7c478bd9Sstevel@tonic-gate 	if (ops->busy)
196*7c478bd9Sstevel@tonic-gate 		return;
197*7c478bd9Sstevel@tonic-gate 	LOCK_INIT_HELD(&ops->busy);
198*7c478bd9Sstevel@tonic-gate 	if (tnf_buf != NULL) {
199*7c478bd9Sstevel@tonic-gate 		/* Release any A-locks held */
200*7c478bd9Sstevel@tonic-gate 		block = ops->wcb.tnfw_w_pos.tnfw_w_block;
201*7c478bd9Sstevel@tonic-gate 		ops->wcb.tnfw_w_pos.tnfw_w_block = NULL;
202*7c478bd9Sstevel@tonic-gate 		if (block != NULL)
203*7c478bd9Sstevel@tonic-gate 			lock_clear(&block->A_lock);
204*7c478bd9Sstevel@tonic-gate 		block = ops->wcb.tnfw_w_tag_pos.tnfw_w_block;
205*7c478bd9Sstevel@tonic-gate 		ops->wcb.tnfw_w_tag_pos.tnfw_w_block = NULL;
206*7c478bd9Sstevel@tonic-gate 		if (block != NULL)
207*7c478bd9Sstevel@tonic-gate 			lock_clear(&block->A_lock);
208*7c478bd9Sstevel@tonic-gate 	}
209*7c478bd9Sstevel@tonic-gate }
210*7c478bd9Sstevel@tonic-gate 
211*7c478bd9Sstevel@tonic-gate /*
212*7c478bd9Sstevel@tonic-gate  * Called from thread_free() if thread has tpdp.
213*7c478bd9Sstevel@tonic-gate  */
214*7c478bd9Sstevel@tonic-gate 
215*7c478bd9Sstevel@tonic-gate void
tnf_thread_free(kthread_t * t)216*7c478bd9Sstevel@tonic-gate tnf_thread_free(kthread_t *t)
217*7c478bd9Sstevel@tonic-gate {
218*7c478bd9Sstevel@tonic-gate 	tnf_ops_t *ops;
219*7c478bd9Sstevel@tonic-gate 	/* LINTED pointer cast may result in improper alignment */
220*7c478bd9Sstevel@tonic-gate 	ops = (tnf_ops_t *)t->t_tnf_tpdp;
221*7c478bd9Sstevel@tonic-gate 	t->t_tnf_tpdp = NULL;
222*7c478bd9Sstevel@tonic-gate 	kmem_free(ops, sizeof (*ops));
223*7c478bd9Sstevel@tonic-gate }
224*7c478bd9Sstevel@tonic-gate 
225*7c478bd9Sstevel@tonic-gate /*
226*7c478bd9Sstevel@tonic-gate  * tnf_thread_queue()
227*7c478bd9Sstevel@tonic-gate  * Probe wrapper called when tracing is enabled and a thread is
228*7c478bd9Sstevel@tonic-gate  * placed on some dispatch queue.
229*7c478bd9Sstevel@tonic-gate  */
230*7c478bd9Sstevel@tonic-gate 
231*7c478bd9Sstevel@tonic-gate void
tnf_thread_queue(kthread_t * t,cpu_t * cp,pri_t tpri)232*7c478bd9Sstevel@tonic-gate tnf_thread_queue(kthread_t *t, cpu_t *cp, pri_t tpri)
233*7c478bd9Sstevel@tonic-gate {
234*7c478bd9Sstevel@tonic-gate 	TNF_PROBE_4(thread_queue, "dispatcher", /* CSTYLED */,
235*7c478bd9Sstevel@tonic-gate 		tnf_kthread_id,		tid,		t,
236*7c478bd9Sstevel@tonic-gate 		tnf_cpuid,		cpuid,		cp->cpu_id,
237*7c478bd9Sstevel@tonic-gate 		tnf_long,		priority,	tpri,
238*7c478bd9Sstevel@tonic-gate 		tnf_ulong,		queue_length,
239*7c478bd9Sstevel@tonic-gate 			/* cp->cpu_disp->disp_q[tpri].dq_sruncnt */
240*7c478bd9Sstevel@tonic-gate 			cp->cpu_disp->disp_nrunnable);
241*7c478bd9Sstevel@tonic-gate 
242*7c478bd9Sstevel@tonic-gate 	TNF_PROBE_2(thread_state, "thread", /* CSTYLED */,
243*7c478bd9Sstevel@tonic-gate 		tnf_kthread_id,		tid,		t,
244*7c478bd9Sstevel@tonic-gate 		tnf_microstate,		state,		LMS_WAIT_CPU);
245*7c478bd9Sstevel@tonic-gate }
246*7c478bd9Sstevel@tonic-gate 
247*7c478bd9Sstevel@tonic-gate /*
248*7c478bd9Sstevel@tonic-gate  * pcstack(): fill in, NULL-terminate and return pc stack.
249*7c478bd9Sstevel@tonic-gate  */
250*7c478bd9Sstevel@tonic-gate 
251*7c478bd9Sstevel@tonic-gate static pc_t *
pcstack(pc_t * pcs)252*7c478bd9Sstevel@tonic-gate pcstack(pc_t *pcs)
253*7c478bd9Sstevel@tonic-gate {
254*7c478bd9Sstevel@tonic-gate 	uint_t n;
255*7c478bd9Sstevel@tonic-gate 
256*7c478bd9Sstevel@tonic-gate 	n = getpcstack(pcs, TNF_PC_COUNT);
257*7c478bd9Sstevel@tonic-gate 	pcs[n] = 0;
258*7c478bd9Sstevel@tonic-gate 	return (pcs);
259*7c478bd9Sstevel@tonic-gate }
260*7c478bd9Sstevel@tonic-gate 
261*7c478bd9Sstevel@tonic-gate /*
262*7c478bd9Sstevel@tonic-gate  * tnf_thread_switch()
263*7c478bd9Sstevel@tonic-gate  * Probe wrapper called when tracing enabled and curthread is about to
264*7c478bd9Sstevel@tonic-gate  * switch to the next thread.
265*7c478bd9Sstevel@tonic-gate  * XXX Simple sleepstate and runstate calculations
266*7c478bd9Sstevel@tonic-gate  */
267*7c478bd9Sstevel@tonic-gate 
268*7c478bd9Sstevel@tonic-gate #define	SLPSTATE(t, ts)					\
269*7c478bd9Sstevel@tonic-gate 	(((ts) == TS_STOPPED) ? LMS_STOPPED :		\
270*7c478bd9Sstevel@tonic-gate 	    ((t)->t_wchan0 ? LMS_USER_LOCK : LMS_SLEEP))
271*7c478bd9Sstevel@tonic-gate 
272*7c478bd9Sstevel@tonic-gate #define	RUNSTATE(next, lwp)				\
273*7c478bd9Sstevel@tonic-gate 	((((lwp = ttolwp(next)) != NULL) &&		\
274*7c478bd9Sstevel@tonic-gate 		lwp->lwp_state == LWP_USER) ?		\
275*7c478bd9Sstevel@tonic-gate 		LMS_USER : LMS_SYSTEM)
276*7c478bd9Sstevel@tonic-gate 
277*7c478bd9Sstevel@tonic-gate void
tnf_thread_switch(kthread_t * next)278*7c478bd9Sstevel@tonic-gate tnf_thread_switch(kthread_t *next)
279*7c478bd9Sstevel@tonic-gate {
280*7c478bd9Sstevel@tonic-gate 	kthread_t	*t;
281*7c478bd9Sstevel@tonic-gate 	klwp_t		*lwp;
282*7c478bd9Sstevel@tonic-gate 	caddr_t		ztpdp;
283*7c478bd9Sstevel@tonic-gate 	int		borrow;
284*7c478bd9Sstevel@tonic-gate 	uint_t		ts;
285*7c478bd9Sstevel@tonic-gate 	pc_t		pcs[TNF_PC_COUNT + 1];
286*7c478bd9Sstevel@tonic-gate 
287*7c478bd9Sstevel@tonic-gate 	t = curthread;
288*7c478bd9Sstevel@tonic-gate 	ts = t->t_state;
289*7c478bd9Sstevel@tonic-gate 
290*7c478bd9Sstevel@tonic-gate 	/*
291*7c478bd9Sstevel@tonic-gate 	 * If we're a zombie, borrow idle thread's tpdp.  This lets
292*7c478bd9Sstevel@tonic-gate 	 * the driver decide whether the buffer is busy by examining
293*7c478bd9Sstevel@tonic-gate 	 * allthreads (idle threads are always on the list).
294*7c478bd9Sstevel@tonic-gate 	 */
295*7c478bd9Sstevel@tonic-gate 	if ((borrow = (ts == TS_ZOMB)) != 0) {
296*7c478bd9Sstevel@tonic-gate 		ztpdp = t->t_tnf_tpdp;
297*7c478bd9Sstevel@tonic-gate 		t->t_tnf_tpdp = CPU->cpu_idle_thread->t_tnf_tpdp;
298*7c478bd9Sstevel@tonic-gate 		goto do_next;
299*7c478bd9Sstevel@tonic-gate 	}
300*7c478bd9Sstevel@tonic-gate 
301*7c478bd9Sstevel@tonic-gate 	/*
302*7c478bd9Sstevel@tonic-gate 	 * If we're blocked, test the blockage probe
303*7c478bd9Sstevel@tonic-gate 	 */
304*7c478bd9Sstevel@tonic-gate 	if (ts == TS_SLEEP && t->t_wchan)
305*7c478bd9Sstevel@tonic-gate #if defined(__sparc)
306*7c478bd9Sstevel@tonic-gate 		TNF_PROBE_2(thread_block, "synch", /* CSTYLED */,
307*7c478bd9Sstevel@tonic-gate 		    tnf_opaque,	  reason,	t->t_wchan,
308*7c478bd9Sstevel@tonic-gate 		    tnf_symbols,  stack,	(pc_t *)pcstack(pcs));
309*7c478bd9Sstevel@tonic-gate #else /* defined(__sparc) */
310*7c478bd9Sstevel@tonic-gate 		TNF_PROBE_2(thread_block, "synch", /* CSTYLED */,
311*7c478bd9Sstevel@tonic-gate 		    tnf_opaque,   reason,	t->t_wchan,
312*7c478bd9Sstevel@tonic-gate 		    tnf_symbols,  stack,	(tnf_opaque_t *)pcstack(pcs));
313*7c478bd9Sstevel@tonic-gate #endif /* defined(__sparc) */
314*7c478bd9Sstevel@tonic-gate 
315*7c478bd9Sstevel@tonic-gate 	/*
316*7c478bd9Sstevel@tonic-gate 	 * Record outgoing thread's state
317*7c478bd9Sstevel@tonic-gate 	 * Kernel thread ID is implicit in schedule record
318*7c478bd9Sstevel@tonic-gate 	 * supress lint: cast from 32-bit integer to 8-bit integer
319*7c478bd9Sstevel@tonic-gate 	 * tnf_microstate_t = tnf_uint8_t
320*7c478bd9Sstevel@tonic-gate 	 */
321*7c478bd9Sstevel@tonic-gate #if defined(_LP64)
322*7c478bd9Sstevel@tonic-gate 	/* LINTED */
323*7c478bd9Sstevel@tonic-gate 	TNF_PROBE_1(thread_state, "thread", /* CSTYLED */,
324*7c478bd9Sstevel@tonic-gate 	    tnf_microstate,	state,		SLPSTATE(t, ts));
325*7c478bd9Sstevel@tonic-gate #else
326*7c478bd9Sstevel@tonic-gate 	TNF_PROBE_1(thread_state, "thread", /* CSTYLED */,
327*7c478bd9Sstevel@tonic-gate 		tnf_microstate,	state,	SLPSTATE(t, ts));
328*7c478bd9Sstevel@tonic-gate #endif
329*7c478bd9Sstevel@tonic-gate 
330*7c478bd9Sstevel@tonic-gate do_next:
331*7c478bd9Sstevel@tonic-gate 	/*
332*7c478bd9Sstevel@tonic-gate 	 * Record incoming thread's state
333*7c478bd9Sstevel@tonic-gate 	 *
334*7c478bd9Sstevel@tonic-gate 	 * supress lint: cast from 32-bit integer to 8-bit integer
335*7c478bd9Sstevel@tonic-gate 	 * tnf_microstate_t = tnf_uint8_t
336*7c478bd9Sstevel@tonic-gate 	 */
337*7c478bd9Sstevel@tonic-gate #if defined(_LP64)
338*7c478bd9Sstevel@tonic-gate 	/* LINTED */
339*7c478bd9Sstevel@tonic-gate 	TNF_PROBE_2(thread_state, "thread", /* CSTYLED */,
340*7c478bd9Sstevel@tonic-gate 	    tnf_kthread_id,	tid,		next,
341*7c478bd9Sstevel@tonic-gate 	    tnf_microstate,	state,		RUNSTATE(next, lwp));
342*7c478bd9Sstevel@tonic-gate #else
343*7c478bd9Sstevel@tonic-gate 	TNF_PROBE_2(thread_state, "thread", /* CSTYLED */,
344*7c478bd9Sstevel@tonic-gate 		tnf_kthread_id,	tid,	next,
345*7c478bd9Sstevel@tonic-gate 		tnf_microstate,	state,	RUNSTATE(next, lwp));
346*7c478bd9Sstevel@tonic-gate #endif
347*7c478bd9Sstevel@tonic-gate 
348*7c478bd9Sstevel@tonic-gate 	/*
349*7c478bd9Sstevel@tonic-gate 	 * If we borrowed idle thread's tpdp above, restore the zombies
350*7c478bd9Sstevel@tonic-gate 	 * tpdp so that it will be freed from tnf_thread_free().
351*7c478bd9Sstevel@tonic-gate 	 */
352*7c478bd9Sstevel@tonic-gate 	if (borrow)
353*7c478bd9Sstevel@tonic-gate 		t->t_tnf_tpdp = ztpdp;
354*7c478bd9Sstevel@tonic-gate 
355*7c478bd9Sstevel@tonic-gate }
356*7c478bd9Sstevel@tonic-gate 
357*7c478bd9Sstevel@tonic-gate #endif	/* NPROBE */
358*7c478bd9Sstevel@tonic-gate 
359*7c478bd9Sstevel@tonic-gate /*
360*7c478bd9Sstevel@tonic-gate  * tnf_mod_load (and tnf_mod_unload), when called from a client module's _init
361*7c478bd9Sstevel@tonic-gate  * (and _fini) entry points are insufficient mechanisms for maintaining the
362*7c478bd9Sstevel@tonic-gate  * consistency of __tnf_probe_list_head and __tnf_tag_list_head whenever that
363*7c478bd9Sstevel@tonic-gate  * module is loaded or unloaded.  The problem occurs because loading a module,
364*7c478bd9Sstevel@tonic-gate  * at which time the modules probes are linked into the two lists, and
365*7c478bd9Sstevel@tonic-gate  * installing that module are separate operations.  This means that it is
366*7c478bd9Sstevel@tonic-gate  * possible for a module to be loaded, not installed, and unloaded without
367*7c478bd9Sstevel@tonic-gate  * calling _init and _fini.  If the module contains TNF probes, the probe and
368*7c478bd9Sstevel@tonic-gate  * tag lists will contain references to data addresses freed when the module
369*7c478bd9Sstevel@tonic-gate  * is unloaded.
370*7c478bd9Sstevel@tonic-gate  *
371*7c478bd9Sstevel@tonic-gate  * The implemented solution for maintaining the lists is to perform the
372*7c478bd9Sstevel@tonic-gate  * unsplicing when the module is unloaded. (Splicing into the lists, "probe
373*7c478bd9Sstevel@tonic-gate  * discovery", is done when krtld processes relocation references when it
374*7c478bd9Sstevel@tonic-gate  * loads the module; this information is not available for subsequent
375*7c478bd9Sstevel@tonic-gate  * operations on the module.
376*7c478bd9Sstevel@tonic-gate  */
377*7c478bd9Sstevel@tonic-gate int
tnf_mod_load()378*7c478bd9Sstevel@tonic-gate tnf_mod_load()
379*7c478bd9Sstevel@tonic-gate {
380*7c478bd9Sstevel@tonic-gate 	return (0);
381*7c478bd9Sstevel@tonic-gate }
382*7c478bd9Sstevel@tonic-gate 
383*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
384*7c478bd9Sstevel@tonic-gate int
tnf_mod_unload(struct modlinkage * mlp)385*7c478bd9Sstevel@tonic-gate tnf_mod_unload(struct modlinkage *mlp)
386*7c478bd9Sstevel@tonic-gate {
387*7c478bd9Sstevel@tonic-gate 	return (0);
388*7c478bd9Sstevel@tonic-gate }
389