xref: /titanic_54/usr/src/uts/common/os/callb.c (revision 7469e2859a12165241f38a4de1b8004f2ffe7bcf)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52df1fe9cSrandyf  * Common Development and Distribution License (the "License").
62df1fe9cSrandyf  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22d3d50737SRafael Vanoni  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <sys/param.h>
277c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
287c478bd9Sstevel@tonic-gate #include <sys/types.h>
297c478bd9Sstevel@tonic-gate #include <sys/time.h>
307c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
317c478bd9Sstevel@tonic-gate #include <sys/systm.h>
327c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
337c478bd9Sstevel@tonic-gate #include <sys/user.h>
347c478bd9Sstevel@tonic-gate #include <sys/proc.h>
357c478bd9Sstevel@tonic-gate #include <sys/callb.h>
367c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
377c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
387c478bd9Sstevel@tonic-gate #include <sys/swap.h>
397c478bd9Sstevel@tonic-gate #include <sys/vmsystm.h>
407c478bd9Sstevel@tonic-gate #include <sys/class.h>
417c478bd9Sstevel@tonic-gate #include <sys/debug.h>
427c478bd9Sstevel@tonic-gate #include <sys/thread.h>
437c478bd9Sstevel@tonic-gate #include <sys/kobj.h>
447c478bd9Sstevel@tonic-gate #include <sys/ddi.h>	/* for delay() */
457c478bd9Sstevel@tonic-gate #include <sys/taskq.h>  /* For TASKQ_NAMELEN */
46*7469e285SDavid Hanisch #include <sys/list.h>
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate #define	CB_MAXNAME	TASKQ_NAMELEN
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate /*
517c478bd9Sstevel@tonic-gate  * The callb mechanism provides generic event scheduling/echoing.
527c478bd9Sstevel@tonic-gate  * A callb function is registered and called on behalf of the event.
537c478bd9Sstevel@tonic-gate  */
547c478bd9Sstevel@tonic-gate typedef struct callb {
55*7469e285SDavid Hanisch 	list_node_t	c_list; 	/* list in class */
567c478bd9Sstevel@tonic-gate 	kthread_id_t	c_thread;	/* ptr to caller's thread struct */
57*7469e285SDavid Hanisch 	int		c_refcount;	/* don't free this callb_t if > 0 */
58*7469e285SDavid Hanisch 	char		c_executing;	/* serialize call of c_func */
597c478bd9Sstevel@tonic-gate 	uchar_t		c_class;	/* this callb's class */
607c478bd9Sstevel@tonic-gate 	kcondvar_t	c_done_cv;	/* signal callb completion */
617c478bd9Sstevel@tonic-gate 	boolean_t	(*c_func)();	/* cb function: returns true if ok */
627c478bd9Sstevel@tonic-gate 	void		*c_arg;		/* arg to c_func */
637c478bd9Sstevel@tonic-gate 	char		c_name[CB_MAXNAME+1]; /* debug:max func name length */
647c478bd9Sstevel@tonic-gate } callb_t;
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate /*
677c478bd9Sstevel@tonic-gate  * Basic structure for a callb table.
687c478bd9Sstevel@tonic-gate  * All callbs are organized into different class groups described
697c478bd9Sstevel@tonic-gate  * by ct_class array.
707c478bd9Sstevel@tonic-gate  * The callbs within a class are single-linked and normally run by a
717c478bd9Sstevel@tonic-gate  * serial execution.
727c478bd9Sstevel@tonic-gate  */
737c478bd9Sstevel@tonic-gate typedef struct callb_table {
747c478bd9Sstevel@tonic-gate 	kmutex_t ct_lock;		/* protect all callb states */
757c478bd9Sstevel@tonic-gate 	int	ct_busy;		/* != 0 prevents additions */
767c478bd9Sstevel@tonic-gate 	kcondvar_t ct_busy_cv;		/* to wait for not busy    */
77*7469e285SDavid Hanisch 	list_t	ct_cb[NCBCLASS];	/* list of callb in a class */
787c478bd9Sstevel@tonic-gate } callb_table_t;
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate int callb_timeout_sec = CPR_KTHREAD_TIMEOUT_SEC;
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate static callb_id_t callb_add_common(boolean_t (*)(void *, int),
837c478bd9Sstevel@tonic-gate     void *, int, char *, kthread_id_t);
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate static callb_table_t callb_table;	/* system level callback table */
867c478bd9Sstevel@tonic-gate static callb_table_t *ct = &callb_table;
877c478bd9Sstevel@tonic-gate static kmutex_t	callb_safe_mutex;
887c478bd9Sstevel@tonic-gate callb_cpr_t	callb_cprinfo_safe = {
897c478bd9Sstevel@tonic-gate 	&callb_safe_mutex, CALLB_CPR_ALWAYS_SAFE, 0, 0, 0 };
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate /*
927c478bd9Sstevel@tonic-gate  * Init all callb tables in the system.
937c478bd9Sstevel@tonic-gate  */
947c478bd9Sstevel@tonic-gate void
callb_init()957c478bd9Sstevel@tonic-gate callb_init()
967c478bd9Sstevel@tonic-gate {
97*7469e285SDavid Hanisch 	int class;
98*7469e285SDavid Hanisch 
997c478bd9Sstevel@tonic-gate 	callb_table.ct_busy = 0;	/* mark table open for additions */
1007c478bd9Sstevel@tonic-gate 	mutex_init(&callb_safe_mutex, NULL, MUTEX_DEFAULT, NULL);
1017c478bd9Sstevel@tonic-gate 	mutex_init(&callb_table.ct_lock, NULL, MUTEX_DEFAULT, NULL);
102*7469e285SDavid Hanisch 	for (class = 0; class < NCBCLASS; class++) {
103*7469e285SDavid Hanisch 		list_create(&callb_table.ct_cb[class], sizeof(callb_t),
104*7469e285SDavid Hanisch 			offsetof(callb_t, c_list));
105*7469e285SDavid Hanisch 	}
1067c478bd9Sstevel@tonic-gate }
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate /*
1097c478bd9Sstevel@tonic-gate  * callout_add() is called to register func() be called later.
1107c478bd9Sstevel@tonic-gate  */
1117c478bd9Sstevel@tonic-gate static callb_id_t
callb_add_common(boolean_t (* func)(void * arg,int code),void * arg,int class,char * name,kthread_id_t t)1127c478bd9Sstevel@tonic-gate callb_add_common(boolean_t (*func)(void *arg, int code),
1137c478bd9Sstevel@tonic-gate     void *arg, int class, char *name, kthread_id_t t)
1147c478bd9Sstevel@tonic-gate {
1157c478bd9Sstevel@tonic-gate 	callb_t *cp;
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	ASSERT(class < NCBCLASS);
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	mutex_enter(&ct->ct_lock);
1207c478bd9Sstevel@tonic-gate 	while (ct->ct_busy)
1217c478bd9Sstevel@tonic-gate 		cv_wait(&ct->ct_busy_cv, &ct->ct_lock);
1227c478bd9Sstevel@tonic-gate 	cp = (callb_t *)kmem_zalloc(sizeof (callb_t), KM_SLEEP);
1237c478bd9Sstevel@tonic-gate 	cp->c_thread = t;
1247c478bd9Sstevel@tonic-gate 	cp->c_func = func;
1257c478bd9Sstevel@tonic-gate 	cp->c_arg = arg;
1267c478bd9Sstevel@tonic-gate 	cp->c_class = (uchar_t)class;
1277c478bd9Sstevel@tonic-gate #ifdef DEBUG
1287c478bd9Sstevel@tonic-gate 	if (strlen(name) > CB_MAXNAME)
1297c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "callb_add: name of callback function '%s' "
1307c478bd9Sstevel@tonic-gate 		    "too long -- truncated to %d chars",
1317c478bd9Sstevel@tonic-gate 		    name, CB_MAXNAME);
1327c478bd9Sstevel@tonic-gate #endif
1337c478bd9Sstevel@tonic-gate 	(void) strncpy(cp->c_name, name, CB_MAXNAME);
1347c478bd9Sstevel@tonic-gate 	cp->c_name[CB_MAXNAME] = '\0';
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	/*
1377c478bd9Sstevel@tonic-gate 	 * Insert the new callb at the head of its class list.
1387c478bd9Sstevel@tonic-gate 	 */
139*7469e285SDavid Hanisch 	list_insert_head(&ct->ct_cb[class], cp);
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 	mutex_exit(&ct->ct_lock);
1427c478bd9Sstevel@tonic-gate 	return ((callb_id_t)cp);
1437c478bd9Sstevel@tonic-gate }
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate /*
1467c478bd9Sstevel@tonic-gate  * The default function to add an entry to the callback table.  Since
1477c478bd9Sstevel@tonic-gate  * it uses curthread as the thread identifier to store in the table,
1487c478bd9Sstevel@tonic-gate  * it should be used for the normal case of a thread which is calling
1497c478bd9Sstevel@tonic-gate  * to add ITSELF to the table.
1507c478bd9Sstevel@tonic-gate  */
1517c478bd9Sstevel@tonic-gate callb_id_t
callb_add(boolean_t (* func)(void * arg,int code),void * arg,int class,char * name)1527c478bd9Sstevel@tonic-gate callb_add(boolean_t (*func)(void *arg, int code),
1537c478bd9Sstevel@tonic-gate     void *arg, int class, char *name)
1547c478bd9Sstevel@tonic-gate {
1557c478bd9Sstevel@tonic-gate 	return (callb_add_common(func, arg, class, name, curthread));
1567c478bd9Sstevel@tonic-gate }
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate /*
1597c478bd9Sstevel@tonic-gate  * A special version of callb_add() above for use by threads which
1607c478bd9Sstevel@tonic-gate  * might be adding an entry to the table on behalf of some other
1617c478bd9Sstevel@tonic-gate  * thread (for example, one which is constructed but not yet running).
1627c478bd9Sstevel@tonic-gate  * In this version the thread id is an argument.
1637c478bd9Sstevel@tonic-gate  */
1647c478bd9Sstevel@tonic-gate callb_id_t
callb_add_thread(boolean_t (* func)(void * arg,int code),void * arg,int class,char * name,kthread_id_t t)1657c478bd9Sstevel@tonic-gate callb_add_thread(boolean_t (*func)(void *arg, int code),
1667c478bd9Sstevel@tonic-gate     void *arg, int class, char *name, kthread_id_t t)
1677c478bd9Sstevel@tonic-gate {
1687c478bd9Sstevel@tonic-gate 	return (callb_add_common(func, arg, class, name, t));
1697c478bd9Sstevel@tonic-gate }
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate /*
1727c478bd9Sstevel@tonic-gate  * callout_delete() is called to remove an entry identified by id
1737c478bd9Sstevel@tonic-gate  * that was originally placed there by a call to callout_add().
1747c478bd9Sstevel@tonic-gate  * return -1 if fail to delete a callb entry otherwise return 0.
1757c478bd9Sstevel@tonic-gate  */
1767c478bd9Sstevel@tonic-gate int
callb_delete(callb_id_t id)1777c478bd9Sstevel@tonic-gate callb_delete(callb_id_t id)
1787c478bd9Sstevel@tonic-gate {
1797c478bd9Sstevel@tonic-gate 	callb_t *me = (callb_t *)id;
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	mutex_enter(&ct->ct_lock);
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	for (;;) {
1847c478bd9Sstevel@tonic-gate #ifdef DEBUG
185*7469e285SDavid Hanisch 		callb_t *cp;
186*7469e285SDavid Hanisch 
187*7469e285SDavid Hanisch 		cp = (callb_t *)list_head(&ct->ct_cb[me->c_class]);
188*7469e285SDavid Hanisch 		while (cp != NULL && cp != me)
189*7469e285SDavid Hanisch 			cp = (callb_t *)list_next(&ct->ct_cb[me->c_class], cp);
190*7469e285SDavid Hanisch 
191*7469e285SDavid Hanisch 		if (cp != me) {
1927c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "callb delete bogus entry 0x%p",
1937c478bd9Sstevel@tonic-gate 			    (void *)me);
1947c478bd9Sstevel@tonic-gate 			mutex_exit(&ct->ct_lock);
1957c478bd9Sstevel@tonic-gate 			return (-1);
1967c478bd9Sstevel@tonic-gate 		}
1977c478bd9Sstevel@tonic-gate #endif /* DEBUG */
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 		/*
2007c478bd9Sstevel@tonic-gate 		 * It is not allowed to delete a callb in the middle of
2017c478bd9Sstevel@tonic-gate 		 * executing otherwise, the callb_execute() will be confused.
2027c478bd9Sstevel@tonic-gate 		 */
203*7469e285SDavid Hanisch 		if (me->c_refcount == 0)
2047c478bd9Sstevel@tonic-gate 			break;
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 		cv_wait(&me->c_done_cv, &ct->ct_lock);
2077c478bd9Sstevel@tonic-gate 	}
2087c478bd9Sstevel@tonic-gate 	/* relink the class list */
209*7469e285SDavid Hanisch 	list_remove(&ct->ct_cb[me->c_class], me);
2107c478bd9Sstevel@tonic-gate 
211*7469e285SDavid Hanisch 	/* free callb */
212*7469e285SDavid Hanisch 	kmem_free(me, sizeof (callb_t));
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	mutex_exit(&ct->ct_lock);
2157c478bd9Sstevel@tonic-gate 	return (0);
2167c478bd9Sstevel@tonic-gate }
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate /*
2197c478bd9Sstevel@tonic-gate  * class:	indicates to execute all callbs in the same class;
2207c478bd9Sstevel@tonic-gate  * code:	optional argument for the callb functions.
2217c478bd9Sstevel@tonic-gate  * return:	 = 0: success
2227c478bd9Sstevel@tonic-gate  *		!= 0: ptr to string supplied when callback was registered
2237c478bd9Sstevel@tonic-gate  */
2247c478bd9Sstevel@tonic-gate void *
callb_execute_class(int class,int code)2257c478bd9Sstevel@tonic-gate callb_execute_class(int class, int code)
2267c478bd9Sstevel@tonic-gate {
2277c478bd9Sstevel@tonic-gate 	callb_t *cp;
2287c478bd9Sstevel@tonic-gate 	void *ret = NULL;
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	ASSERT(class < NCBCLASS);
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 	mutex_enter(&ct->ct_lock);
2337c478bd9Sstevel@tonic-gate 
234*7469e285SDavid Hanisch 	for (cp = (callb_t *)list_head(&ct->ct_cb[class]);
235*7469e285SDavid Hanisch 	    cp != NULL && ret == 0;
236*7469e285SDavid Hanisch 	    cp = (callb_t *)list_next(&ct->ct_cb[class], cp)) {
237*7469e285SDavid Hanisch 		cp->c_refcount++;
238*7469e285SDavid Hanisch 
239*7469e285SDavid Hanisch 		while (cp->c_executing)
2407c478bd9Sstevel@tonic-gate 			cv_wait(&cp->c_done_cv, &ct->ct_lock);
241*7469e285SDavid Hanisch 		cp->c_executing = 1;
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate #ifdef CALLB_DEBUG
2447c478bd9Sstevel@tonic-gate 		printf("callb_execute: name=%s func=%p arg=%p\n",
2457c478bd9Sstevel@tonic-gate 		    cp->c_name, (void *)cp->c_func, (void *)cp->c_arg);
2467c478bd9Sstevel@tonic-gate #endif /* CALLB_DEBUG */
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 		mutex_exit(&ct->ct_lock);
2497c478bd9Sstevel@tonic-gate 		/* If callback function fails, pass back client's name */
2507c478bd9Sstevel@tonic-gate 		if (!(*cp->c_func)(cp->c_arg, code))
2517c478bd9Sstevel@tonic-gate 			ret = cp->c_name;
2527c478bd9Sstevel@tonic-gate 		mutex_enter(&ct->ct_lock);
2537c478bd9Sstevel@tonic-gate 
254*7469e285SDavid Hanisch 		cp->c_executing = 0;
255*7469e285SDavid Hanisch 		cp->c_refcount--;
2567c478bd9Sstevel@tonic-gate 		cv_broadcast(&cp->c_done_cv);
2577c478bd9Sstevel@tonic-gate 	}
2587c478bd9Sstevel@tonic-gate 	mutex_exit(&ct->ct_lock);
2597c478bd9Sstevel@tonic-gate 	return (ret);
2607c478bd9Sstevel@tonic-gate }
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate /*
2637c478bd9Sstevel@tonic-gate  * callers make sure no recursive entries to this func.
2647c478bd9Sstevel@tonic-gate  * dp->cc_lockp is registered by callb_add to protect callb_cpr_t structure.
2657c478bd9Sstevel@tonic-gate  *
2667c478bd9Sstevel@tonic-gate  * When calling to stop a kernel thread (code == CB_CODE_CPR_CHKPT) we
2677c478bd9Sstevel@tonic-gate  * use a cv_timedwait() in case the kernel thread is blocked.
2687c478bd9Sstevel@tonic-gate  *
2697c478bd9Sstevel@tonic-gate  * Note that this is a generic callback handler for daemon CPR and
2707c478bd9Sstevel@tonic-gate  * should NOT be changed to accommodate any specific requirement in a daemon.
2717c478bd9Sstevel@tonic-gate  * Individual daemons that require changes to the handler shall write
2727c478bd9Sstevel@tonic-gate  * callback routines in their own daemon modules.
2737c478bd9Sstevel@tonic-gate  */
2747c478bd9Sstevel@tonic-gate boolean_t
callb_generic_cpr(void * arg,int code)2757c478bd9Sstevel@tonic-gate callb_generic_cpr(void *arg, int code)
2767c478bd9Sstevel@tonic-gate {
2777c478bd9Sstevel@tonic-gate 	callb_cpr_t *cp = (callb_cpr_t *)arg;
2787c478bd9Sstevel@tonic-gate 	clock_t ret = 0;			/* assume success */
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 	mutex_enter(cp->cc_lockp);
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 	switch (code) {
2837c478bd9Sstevel@tonic-gate 	case CB_CODE_CPR_CHKPT:
2847c478bd9Sstevel@tonic-gate 		cp->cc_events |= CALLB_CPR_START;
2852df1fe9cSrandyf #ifdef CPR_NOT_THREAD_SAFE
2867c478bd9Sstevel@tonic-gate 		while (!(cp->cc_events & CALLB_CPR_SAFE))
2877c478bd9Sstevel@tonic-gate 			/* cv_timedwait() returns -1 if it times out. */
288d3d50737SRafael Vanoni 			if ((ret = cv_reltimedwait(&cp->cc_callb_cv,
289d3d50737SRafael Vanoni 			    cp->cc_lockp, (callb_timeout_sec * hz),
290d3d50737SRafael Vanoni 			    TR_CLOCK_TICK)) == -1)
2917c478bd9Sstevel@tonic-gate 				break;
2922df1fe9cSrandyf #endif
2937c478bd9Sstevel@tonic-gate 		break;
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 	case CB_CODE_CPR_RESUME:
2967c478bd9Sstevel@tonic-gate 		cp->cc_events &= ~CALLB_CPR_START;
2977c478bd9Sstevel@tonic-gate 		cv_signal(&cp->cc_stop_cv);
2987c478bd9Sstevel@tonic-gate 		break;
2997c478bd9Sstevel@tonic-gate 	}
3007c478bd9Sstevel@tonic-gate 	mutex_exit(cp->cc_lockp);
3017c478bd9Sstevel@tonic-gate 	return (ret != -1);
3027c478bd9Sstevel@tonic-gate }
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate /*
3057c478bd9Sstevel@tonic-gate  * The generic callback function associated with kernel threads which
3067c478bd9Sstevel@tonic-gate  * are always considered safe.
3077c478bd9Sstevel@tonic-gate  */
3087c478bd9Sstevel@tonic-gate /* ARGSUSED */
3097c478bd9Sstevel@tonic-gate boolean_t
callb_generic_cpr_safe(void * arg,int code)3107c478bd9Sstevel@tonic-gate callb_generic_cpr_safe(void *arg, int code)
3117c478bd9Sstevel@tonic-gate {
3127c478bd9Sstevel@tonic-gate 	return (B_TRUE);
3137c478bd9Sstevel@tonic-gate }
3147c478bd9Sstevel@tonic-gate /*
3157c478bd9Sstevel@tonic-gate  * Prevent additions to callback table.
3167c478bd9Sstevel@tonic-gate  */
3177c478bd9Sstevel@tonic-gate void
callb_lock_table(void)3187c478bd9Sstevel@tonic-gate callb_lock_table(void)
3197c478bd9Sstevel@tonic-gate {
3207c478bd9Sstevel@tonic-gate 	mutex_enter(&ct->ct_lock);
3217c478bd9Sstevel@tonic-gate 	ASSERT(ct->ct_busy == 0);
3227c478bd9Sstevel@tonic-gate 	ct->ct_busy = 1;
3237c478bd9Sstevel@tonic-gate 	mutex_exit(&ct->ct_lock);
3247c478bd9Sstevel@tonic-gate }
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate /*
3277c478bd9Sstevel@tonic-gate  * Allow additions to callback table.
3287c478bd9Sstevel@tonic-gate  */
3297c478bd9Sstevel@tonic-gate void
callb_unlock_table(void)3307c478bd9Sstevel@tonic-gate callb_unlock_table(void)
3317c478bd9Sstevel@tonic-gate {
3327c478bd9Sstevel@tonic-gate 	mutex_enter(&ct->ct_lock);
3337c478bd9Sstevel@tonic-gate 	ASSERT(ct->ct_busy != 0);
3347c478bd9Sstevel@tonic-gate 	ct->ct_busy = 0;
3357c478bd9Sstevel@tonic-gate 	cv_broadcast(&ct->ct_busy_cv);
3367c478bd9Sstevel@tonic-gate 	mutex_exit(&ct->ct_lock);
3377c478bd9Sstevel@tonic-gate }
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate /*
3407c478bd9Sstevel@tonic-gate  * Return a boolean value indicating whether a particular kernel thread is
3417c478bd9Sstevel@tonic-gate  * stopped in accordance with the cpr callback protocol.  If returning
3427c478bd9Sstevel@tonic-gate  * false, also return a pointer to the thread name via the 2nd argument.
3437c478bd9Sstevel@tonic-gate  */
3447c478bd9Sstevel@tonic-gate boolean_t
callb_is_stopped(kthread_id_t tp,caddr_t * thread_name)3457c478bd9Sstevel@tonic-gate callb_is_stopped(kthread_id_t tp, caddr_t *thread_name)
3467c478bd9Sstevel@tonic-gate {
3477c478bd9Sstevel@tonic-gate 	callb_t *cp;
3487c478bd9Sstevel@tonic-gate 	boolean_t ret_val;
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 	mutex_enter(&ct->ct_lock);
3517c478bd9Sstevel@tonic-gate 
352*7469e285SDavid Hanisch 	for (cp = (callb_t *)list_head(&ct->ct_cb[CB_CL_CPR_DAEMON]);
353*7469e285SDavid Hanisch 	    cp != NULL && tp != cp->c_thread;
354*7469e285SDavid Hanisch 	    cp = (callb_t *)list_next(&ct->ct_cb[CB_CL_CPR_DAEMON], cp))
3557c478bd9Sstevel@tonic-gate 		;
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 	ret_val = (cp != NULL);
3587c478bd9Sstevel@tonic-gate 	if (ret_val) {
3597c478bd9Sstevel@tonic-gate 		/*
3607c478bd9Sstevel@tonic-gate 		 * We found the thread in the callback table and have
3617c478bd9Sstevel@tonic-gate 		 * provisionally set the return value to true.  Now
3627c478bd9Sstevel@tonic-gate 		 * see if it is marked "safe" and is sleeping or stopped.
3637c478bd9Sstevel@tonic-gate 		 */
3647c478bd9Sstevel@tonic-gate 		callb_cpr_t *ccp = (callb_cpr_t *)cp->c_arg;
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 		*thread_name = cp->c_name;	/* in case not stopped */
3677c478bd9Sstevel@tonic-gate 		mutex_enter(ccp->cc_lockp);
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 		if (ccp->cc_events & CALLB_CPR_SAFE) {
3707c478bd9Sstevel@tonic-gate 			int retry;
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 			mutex_exit(ccp->cc_lockp);
3737c478bd9Sstevel@tonic-gate 			for (retry = 0; retry < CALLB_MAX_RETRY; retry++) {
3747c478bd9Sstevel@tonic-gate 				thread_lock(tp);
3757c478bd9Sstevel@tonic-gate 				if (tp->t_state & (TS_SLEEP | TS_STOPPED)) {
3767c478bd9Sstevel@tonic-gate 					thread_unlock(tp);
3777c478bd9Sstevel@tonic-gate 					break;
3787c478bd9Sstevel@tonic-gate 				}
3797c478bd9Sstevel@tonic-gate 				thread_unlock(tp);
3807c478bd9Sstevel@tonic-gate 				delay(CALLB_THREAD_DELAY);
3817c478bd9Sstevel@tonic-gate 			}
3827c478bd9Sstevel@tonic-gate 			ret_val = retry < CALLB_MAX_RETRY;
3837c478bd9Sstevel@tonic-gate 		} else {
3847c478bd9Sstevel@tonic-gate 			ret_val =
3857c478bd9Sstevel@tonic-gate 			    (ccp->cc_events & CALLB_CPR_ALWAYS_SAFE) != 0;
3867c478bd9Sstevel@tonic-gate 			mutex_exit(ccp->cc_lockp);
3877c478bd9Sstevel@tonic-gate 		}
3887c478bd9Sstevel@tonic-gate 	} else {
3897c478bd9Sstevel@tonic-gate 		/*
3907c478bd9Sstevel@tonic-gate 		 * Thread not found in callback table.  Make the best
3917c478bd9Sstevel@tonic-gate 		 * attempt to identify the thread in the error message.
3927c478bd9Sstevel@tonic-gate 		 */
3937c478bd9Sstevel@tonic-gate 		ulong_t offset;
3947c478bd9Sstevel@tonic-gate 		char *sym = kobj_getsymname((uintptr_t)tp->t_startpc,
3957c478bd9Sstevel@tonic-gate 		    &offset);
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 		*thread_name = sym ? sym : "*unknown*";
3987c478bd9Sstevel@tonic-gate 	}
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 	mutex_exit(&ct->ct_lock);
4017c478bd9Sstevel@tonic-gate 	return (ret_val);
4027c478bd9Sstevel@tonic-gate }
403