xref: /titanic_51/usr/src/uts/common/os/callb.c (revision 2df1fe9ca32bb227b9158c67f5c00b54c20b10fd)
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
5*2df1fe9cSrandyf  * Common Development and Distribution License (the "License").
6*2df1fe9cSrandyf  * 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 /*
22*2df1fe9cSrandyf  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <sys/param.h>
297c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
307c478bd9Sstevel@tonic-gate #include <sys/types.h>
317c478bd9Sstevel@tonic-gate #include <sys/time.h>
327c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
337c478bd9Sstevel@tonic-gate #include <sys/systm.h>
347c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
357c478bd9Sstevel@tonic-gate #include <sys/user.h>
367c478bd9Sstevel@tonic-gate #include <sys/proc.h>
377c478bd9Sstevel@tonic-gate #include <sys/callb.h>
387c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
397c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
407c478bd9Sstevel@tonic-gate #include <sys/swap.h>
417c478bd9Sstevel@tonic-gate #include <sys/vmsystm.h>
427c478bd9Sstevel@tonic-gate #include <sys/class.h>
437c478bd9Sstevel@tonic-gate #include <sys/debug.h>
447c478bd9Sstevel@tonic-gate #include <sys/thread.h>
457c478bd9Sstevel@tonic-gate #include <sys/kobj.h>
467c478bd9Sstevel@tonic-gate #include <sys/ddi.h>	/* for delay() */
477c478bd9Sstevel@tonic-gate #include <sys/taskq.h>  /* For TASKQ_NAMELEN */
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate #define	CB_MAXNAME	TASKQ_NAMELEN
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate /*
527c478bd9Sstevel@tonic-gate  * The callb mechanism provides generic event scheduling/echoing.
537c478bd9Sstevel@tonic-gate  * A callb function is registered and called on behalf of the event.
547c478bd9Sstevel@tonic-gate  */
557c478bd9Sstevel@tonic-gate typedef struct callb {
567c478bd9Sstevel@tonic-gate 	struct callb	*c_next; 	/* next in class or on freelist */
577c478bd9Sstevel@tonic-gate 	kthread_id_t	c_thread;	/* ptr to caller's thread struct */
587c478bd9Sstevel@tonic-gate 	char		c_flag;		/* info about the callb state */
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  * callb c_flag bitmap definitions
687c478bd9Sstevel@tonic-gate  */
697c478bd9Sstevel@tonic-gate #define	CALLB_FREE		0x0
707c478bd9Sstevel@tonic-gate #define	CALLB_TAKEN		0x1
717c478bd9Sstevel@tonic-gate #define	CALLB_EXECUTING		0x2
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate /*
747c478bd9Sstevel@tonic-gate  * Basic structure for a callb table.
757c478bd9Sstevel@tonic-gate  * All callbs are organized into different class groups described
767c478bd9Sstevel@tonic-gate  * by ct_class array.
777c478bd9Sstevel@tonic-gate  * The callbs within a class are single-linked and normally run by a
787c478bd9Sstevel@tonic-gate  * serial execution.
797c478bd9Sstevel@tonic-gate  */
807c478bd9Sstevel@tonic-gate typedef struct callb_table {
817c478bd9Sstevel@tonic-gate 	kmutex_t ct_lock;		/* protect all callb states */
827c478bd9Sstevel@tonic-gate 	callb_t	*ct_freelist; 		/* free callb structures */
837c478bd9Sstevel@tonic-gate 	int	ct_busy;		/* != 0 prevents additions */
847c478bd9Sstevel@tonic-gate 	kcondvar_t ct_busy_cv;		/* to wait for not busy    */
857c478bd9Sstevel@tonic-gate 	int	ct_ncallb; 		/* num of callbs allocated */
867c478bd9Sstevel@tonic-gate 	callb_t	*ct_first_cb[NCBCLASS];	/* ptr to 1st callb in a class */
877c478bd9Sstevel@tonic-gate } callb_table_t;
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate int callb_timeout_sec = CPR_KTHREAD_TIMEOUT_SEC;
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate static callb_id_t callb_add_common(boolean_t (*)(void *, int),
927c478bd9Sstevel@tonic-gate     void *, int, char *, kthread_id_t);
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate static callb_table_t callb_table;	/* system level callback table */
957c478bd9Sstevel@tonic-gate static callb_table_t *ct = &callb_table;
967c478bd9Sstevel@tonic-gate static kmutex_t	callb_safe_mutex;
977c478bd9Sstevel@tonic-gate callb_cpr_t	callb_cprinfo_safe = {
987c478bd9Sstevel@tonic-gate 	&callb_safe_mutex, CALLB_CPR_ALWAYS_SAFE, 0, 0, 0 };
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate /*
1017c478bd9Sstevel@tonic-gate  * Init all callb tables in the system.
1027c478bd9Sstevel@tonic-gate  */
1037c478bd9Sstevel@tonic-gate void
1047c478bd9Sstevel@tonic-gate callb_init()
1057c478bd9Sstevel@tonic-gate {
1067c478bd9Sstevel@tonic-gate 	callb_table.ct_busy = 0;	/* mark table open for additions */
1077c478bd9Sstevel@tonic-gate 	mutex_init(&callb_safe_mutex, NULL, MUTEX_DEFAULT, NULL);
1087c478bd9Sstevel@tonic-gate 	mutex_init(&callb_table.ct_lock, NULL, MUTEX_DEFAULT, NULL);
1097c478bd9Sstevel@tonic-gate }
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate /*
1127c478bd9Sstevel@tonic-gate  * callout_add() is called to register func() be called later.
1137c478bd9Sstevel@tonic-gate  */
1147c478bd9Sstevel@tonic-gate static callb_id_t
1157c478bd9Sstevel@tonic-gate callb_add_common(boolean_t (*func)(void *arg, int code),
1167c478bd9Sstevel@tonic-gate     void *arg, int class, char *name, kthread_id_t t)
1177c478bd9Sstevel@tonic-gate {
1187c478bd9Sstevel@tonic-gate 	callb_t *cp;
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	ASSERT(class < NCBCLASS);
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	mutex_enter(&ct->ct_lock);
1237c478bd9Sstevel@tonic-gate 	while (ct->ct_busy)
1247c478bd9Sstevel@tonic-gate 		cv_wait(&ct->ct_busy_cv, &ct->ct_lock);
1257c478bd9Sstevel@tonic-gate 	if ((cp = ct->ct_freelist) == NULL) {
1267c478bd9Sstevel@tonic-gate 		ct->ct_ncallb++;
1277c478bd9Sstevel@tonic-gate 		cp = (callb_t *)kmem_zalloc(sizeof (callb_t), KM_SLEEP);
1287c478bd9Sstevel@tonic-gate 	}
1297c478bd9Sstevel@tonic-gate 	ct->ct_freelist = cp->c_next;
1307c478bd9Sstevel@tonic-gate 	cp->c_thread = t;
1317c478bd9Sstevel@tonic-gate 	cp->c_func = func;
1327c478bd9Sstevel@tonic-gate 	cp->c_arg = arg;
1337c478bd9Sstevel@tonic-gate 	cp->c_class = (uchar_t)class;
1347c478bd9Sstevel@tonic-gate 	cp->c_flag |= CALLB_TAKEN;
1357c478bd9Sstevel@tonic-gate #ifdef DEBUG
1367c478bd9Sstevel@tonic-gate 	if (strlen(name) > CB_MAXNAME)
1377c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "callb_add: name of callback function '%s' "
1387c478bd9Sstevel@tonic-gate 		    "too long -- truncated to %d chars",
1397c478bd9Sstevel@tonic-gate 		    name, CB_MAXNAME);
1407c478bd9Sstevel@tonic-gate #endif
1417c478bd9Sstevel@tonic-gate 	(void) strncpy(cp->c_name, name, CB_MAXNAME);
1427c478bd9Sstevel@tonic-gate 	cp->c_name[CB_MAXNAME] = '\0';
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	/*
1457c478bd9Sstevel@tonic-gate 	 * Insert the new callb at the head of its class list.
1467c478bd9Sstevel@tonic-gate 	 */
1477c478bd9Sstevel@tonic-gate 	cp->c_next = ct->ct_first_cb[class];
1487c478bd9Sstevel@tonic-gate 	ct->ct_first_cb[class] = cp;
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	mutex_exit(&ct->ct_lock);
1517c478bd9Sstevel@tonic-gate 	return ((callb_id_t)cp);
1527c478bd9Sstevel@tonic-gate }
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate /*
1557c478bd9Sstevel@tonic-gate  * The default function to add an entry to the callback table.  Since
1567c478bd9Sstevel@tonic-gate  * it uses curthread as the thread identifier to store in the table,
1577c478bd9Sstevel@tonic-gate  * it should be used for the normal case of a thread which is calling
1587c478bd9Sstevel@tonic-gate  * to add ITSELF to the table.
1597c478bd9Sstevel@tonic-gate  */
1607c478bd9Sstevel@tonic-gate callb_id_t
1617c478bd9Sstevel@tonic-gate callb_add(boolean_t (*func)(void *arg, int code),
1627c478bd9Sstevel@tonic-gate     void *arg, int class, char *name)
1637c478bd9Sstevel@tonic-gate {
1647c478bd9Sstevel@tonic-gate 	return (callb_add_common(func, arg, class, name, curthread));
1657c478bd9Sstevel@tonic-gate }
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate /*
1687c478bd9Sstevel@tonic-gate  * A special version of callb_add() above for use by threads which
1697c478bd9Sstevel@tonic-gate  * might be adding an entry to the table on behalf of some other
1707c478bd9Sstevel@tonic-gate  * thread (for example, one which is constructed but not yet running).
1717c478bd9Sstevel@tonic-gate  * In this version the thread id is an argument.
1727c478bd9Sstevel@tonic-gate  */
1737c478bd9Sstevel@tonic-gate callb_id_t
1747c478bd9Sstevel@tonic-gate callb_add_thread(boolean_t (*func)(void *arg, int code),
1757c478bd9Sstevel@tonic-gate     void *arg, int class, char *name, kthread_id_t t)
1767c478bd9Sstevel@tonic-gate {
1777c478bd9Sstevel@tonic-gate 	return (callb_add_common(func, arg, class, name, t));
1787c478bd9Sstevel@tonic-gate }
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate /*
1817c478bd9Sstevel@tonic-gate  * callout_delete() is called to remove an entry identified by id
1827c478bd9Sstevel@tonic-gate  * that was originally placed there by a call to callout_add().
1837c478bd9Sstevel@tonic-gate  * return -1 if fail to delete a callb entry otherwise return 0.
1847c478bd9Sstevel@tonic-gate  */
1857c478bd9Sstevel@tonic-gate int
1867c478bd9Sstevel@tonic-gate callb_delete(callb_id_t id)
1877c478bd9Sstevel@tonic-gate {
1887c478bd9Sstevel@tonic-gate 	callb_t **pp;
1897c478bd9Sstevel@tonic-gate 	callb_t *me = (callb_t *)id;
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	mutex_enter(&ct->ct_lock);
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	for (;;) {
1947c478bd9Sstevel@tonic-gate 		pp = &ct->ct_first_cb[me->c_class];
1957c478bd9Sstevel@tonic-gate 		while (*pp != NULL && *pp != me)
1967c478bd9Sstevel@tonic-gate 			pp = &(*pp)->c_next;
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate #ifdef DEBUG
1997c478bd9Sstevel@tonic-gate 		if (*pp != me) {
2007c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "callb delete bogus entry 0x%p",
2017c478bd9Sstevel@tonic-gate 			    (void *)me);
2027c478bd9Sstevel@tonic-gate 			mutex_exit(&ct->ct_lock);
2037c478bd9Sstevel@tonic-gate 			return (-1);
2047c478bd9Sstevel@tonic-gate 		}
2057c478bd9Sstevel@tonic-gate #endif /* DEBUG */
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 		/*
2087c478bd9Sstevel@tonic-gate 		 * It is not allowed to delete a callb in the middle of
2097c478bd9Sstevel@tonic-gate 		 * executing otherwise, the callb_execute() will be confused.
2107c478bd9Sstevel@tonic-gate 		 */
2117c478bd9Sstevel@tonic-gate 		if (!(me->c_flag & CALLB_EXECUTING))
2127c478bd9Sstevel@tonic-gate 			break;
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 		cv_wait(&me->c_done_cv, &ct->ct_lock);
2157c478bd9Sstevel@tonic-gate 	}
2167c478bd9Sstevel@tonic-gate 	/* relink the class list */
2177c478bd9Sstevel@tonic-gate 	*pp = me->c_next;
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	/* clean up myself and return the free callb to the head of freelist */
2207c478bd9Sstevel@tonic-gate 	me->c_flag = CALLB_FREE;
2217c478bd9Sstevel@tonic-gate 	me->c_next = ct->ct_freelist;
2227c478bd9Sstevel@tonic-gate 	ct->ct_freelist = me;
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	mutex_exit(&ct->ct_lock);
2257c478bd9Sstevel@tonic-gate 	return (0);
2267c478bd9Sstevel@tonic-gate }
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate /*
2297c478bd9Sstevel@tonic-gate  * class:	indicates to execute all callbs in the same class;
2307c478bd9Sstevel@tonic-gate  * code:	optional argument for the callb functions.
2317c478bd9Sstevel@tonic-gate  * return:	 = 0: success
2327c478bd9Sstevel@tonic-gate  *		!= 0: ptr to string supplied when callback was registered
2337c478bd9Sstevel@tonic-gate  */
2347c478bd9Sstevel@tonic-gate void *
2357c478bd9Sstevel@tonic-gate callb_execute_class(int class, int code)
2367c478bd9Sstevel@tonic-gate {
2377c478bd9Sstevel@tonic-gate 	callb_t *cp;
2387c478bd9Sstevel@tonic-gate 	void *ret = NULL;
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 	ASSERT(class < NCBCLASS);
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	mutex_enter(&ct->ct_lock);
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	for (cp = ct->ct_first_cb[class];
2457c478bd9Sstevel@tonic-gate 	    cp != NULL && ret == 0; cp = cp->c_next) {
2467c478bd9Sstevel@tonic-gate 		while (cp->c_flag & CALLB_EXECUTING)
2477c478bd9Sstevel@tonic-gate 			cv_wait(&cp->c_done_cv, &ct->ct_lock);
2487c478bd9Sstevel@tonic-gate 		/*
2497c478bd9Sstevel@tonic-gate 		 * cont if the callb is deleted while we're sleeping
2507c478bd9Sstevel@tonic-gate 		 */
2517c478bd9Sstevel@tonic-gate 		if (cp->c_flag == CALLB_FREE)
2527c478bd9Sstevel@tonic-gate 			continue;
2537c478bd9Sstevel@tonic-gate 		cp->c_flag |= CALLB_EXECUTING;
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate #ifdef CALLB_DEBUG
2567c478bd9Sstevel@tonic-gate 		printf("callb_execute: name=%s func=%p arg=%p\n",
2577c478bd9Sstevel@tonic-gate 		    cp->c_name, (void *)cp->c_func, (void *)cp->c_arg);
2587c478bd9Sstevel@tonic-gate #endif /* CALLB_DEBUG */
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 		mutex_exit(&ct->ct_lock);
2617c478bd9Sstevel@tonic-gate 		/* If callback function fails, pass back client's name */
2627c478bd9Sstevel@tonic-gate 		if (!(*cp->c_func)(cp->c_arg, code))
2637c478bd9Sstevel@tonic-gate 			ret = cp->c_name;
2647c478bd9Sstevel@tonic-gate 		mutex_enter(&ct->ct_lock);
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 		cp->c_flag &= ~CALLB_EXECUTING;
2677c478bd9Sstevel@tonic-gate 		cv_broadcast(&cp->c_done_cv);
2687c478bd9Sstevel@tonic-gate 	}
2697c478bd9Sstevel@tonic-gate 	mutex_exit(&ct->ct_lock);
2707c478bd9Sstevel@tonic-gate 	return (ret);
2717c478bd9Sstevel@tonic-gate }
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate /*
2747c478bd9Sstevel@tonic-gate  * callers make sure no recursive entries to this func.
2757c478bd9Sstevel@tonic-gate  * dp->cc_lockp is registered by callb_add to protect callb_cpr_t structure.
2767c478bd9Sstevel@tonic-gate  *
2777c478bd9Sstevel@tonic-gate  * When calling to stop a kernel thread (code == CB_CODE_CPR_CHKPT) we
2787c478bd9Sstevel@tonic-gate  * use a cv_timedwait() in case the kernel thread is blocked.
2797c478bd9Sstevel@tonic-gate  *
2807c478bd9Sstevel@tonic-gate  * Note that this is a generic callback handler for daemon CPR and
2817c478bd9Sstevel@tonic-gate  * should NOT be changed to accommodate any specific requirement in a daemon.
2827c478bd9Sstevel@tonic-gate  * Individual daemons that require changes to the handler shall write
2837c478bd9Sstevel@tonic-gate  * callback routines in their own daemon modules.
2847c478bd9Sstevel@tonic-gate  */
2857c478bd9Sstevel@tonic-gate boolean_t
2867c478bd9Sstevel@tonic-gate callb_generic_cpr(void *arg, int code)
2877c478bd9Sstevel@tonic-gate {
2887c478bd9Sstevel@tonic-gate 	callb_cpr_t *cp = (callb_cpr_t *)arg;
2897c478bd9Sstevel@tonic-gate 	clock_t ret = 0;			/* assume success */
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 	mutex_enter(cp->cc_lockp);
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 	switch (code) {
2947c478bd9Sstevel@tonic-gate 	case CB_CODE_CPR_CHKPT:
2957c478bd9Sstevel@tonic-gate 		cp->cc_events |= CALLB_CPR_START;
296*2df1fe9cSrandyf #ifdef CPR_NOT_THREAD_SAFE
2977c478bd9Sstevel@tonic-gate 		while (!(cp->cc_events & CALLB_CPR_SAFE))
2987c478bd9Sstevel@tonic-gate 			/* cv_timedwait() returns -1 if it times out. */
2997c478bd9Sstevel@tonic-gate 			if ((ret = cv_timedwait(&cp->cc_callb_cv,
3007c478bd9Sstevel@tonic-gate 			    cp->cc_lockp,
3017c478bd9Sstevel@tonic-gate 			    lbolt + callb_timeout_sec * hz)) == -1)
3027c478bd9Sstevel@tonic-gate 				break;
303*2df1fe9cSrandyf #endif
3047c478bd9Sstevel@tonic-gate 		break;
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	case CB_CODE_CPR_RESUME:
3077c478bd9Sstevel@tonic-gate 		cp->cc_events &= ~CALLB_CPR_START;
3087c478bd9Sstevel@tonic-gate 		cv_signal(&cp->cc_stop_cv);
3097c478bd9Sstevel@tonic-gate 		break;
3107c478bd9Sstevel@tonic-gate 	}
3117c478bd9Sstevel@tonic-gate 	mutex_exit(cp->cc_lockp);
3127c478bd9Sstevel@tonic-gate 	return (ret != -1);
3137c478bd9Sstevel@tonic-gate }
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate /*
3167c478bd9Sstevel@tonic-gate  * The generic callback function associated with kernel threads which
3177c478bd9Sstevel@tonic-gate  * are always considered safe.
3187c478bd9Sstevel@tonic-gate  */
3197c478bd9Sstevel@tonic-gate /* ARGSUSED */
3207c478bd9Sstevel@tonic-gate boolean_t
3217c478bd9Sstevel@tonic-gate callb_generic_cpr_safe(void *arg, int code)
3227c478bd9Sstevel@tonic-gate {
3237c478bd9Sstevel@tonic-gate 	return (B_TRUE);
3247c478bd9Sstevel@tonic-gate }
3257c478bd9Sstevel@tonic-gate /*
3267c478bd9Sstevel@tonic-gate  * Prevent additions to callback table.
3277c478bd9Sstevel@tonic-gate  */
3287c478bd9Sstevel@tonic-gate void
3297c478bd9Sstevel@tonic-gate callb_lock_table(void)
3307c478bd9Sstevel@tonic-gate {
3317c478bd9Sstevel@tonic-gate 	mutex_enter(&ct->ct_lock);
3327c478bd9Sstevel@tonic-gate 	ASSERT(ct->ct_busy == 0);
3337c478bd9Sstevel@tonic-gate 	ct->ct_busy = 1;
3347c478bd9Sstevel@tonic-gate 	mutex_exit(&ct->ct_lock);
3357c478bd9Sstevel@tonic-gate }
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate /*
3387c478bd9Sstevel@tonic-gate  * Allow additions to callback table.
3397c478bd9Sstevel@tonic-gate  */
3407c478bd9Sstevel@tonic-gate void
3417c478bd9Sstevel@tonic-gate callb_unlock_table(void)
3427c478bd9Sstevel@tonic-gate {
3437c478bd9Sstevel@tonic-gate 	mutex_enter(&ct->ct_lock);
3447c478bd9Sstevel@tonic-gate 	ASSERT(ct->ct_busy != 0);
3457c478bd9Sstevel@tonic-gate 	ct->ct_busy = 0;
3467c478bd9Sstevel@tonic-gate 	cv_broadcast(&ct->ct_busy_cv);
3477c478bd9Sstevel@tonic-gate 	mutex_exit(&ct->ct_lock);
3487c478bd9Sstevel@tonic-gate }
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate /*
3517c478bd9Sstevel@tonic-gate  * Return a boolean value indicating whether a particular kernel thread is
3527c478bd9Sstevel@tonic-gate  * stopped in accordance with the cpr callback protocol.  If returning
3537c478bd9Sstevel@tonic-gate  * false, also return a pointer to the thread name via the 2nd argument.
3547c478bd9Sstevel@tonic-gate  */
3557c478bd9Sstevel@tonic-gate boolean_t
3567c478bd9Sstevel@tonic-gate callb_is_stopped(kthread_id_t tp, caddr_t *thread_name)
3577c478bd9Sstevel@tonic-gate {
3587c478bd9Sstevel@tonic-gate 	callb_t *cp;
3597c478bd9Sstevel@tonic-gate 	boolean_t ret_val;
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	mutex_enter(&ct->ct_lock);
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 	for (cp = ct->ct_first_cb[CB_CL_CPR_DAEMON];
3647c478bd9Sstevel@tonic-gate 	    cp != NULL && tp != cp->c_thread; cp = cp->c_next)
3657c478bd9Sstevel@tonic-gate 		;
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 	ret_val = (cp != NULL);
3687c478bd9Sstevel@tonic-gate 	if (ret_val) {
3697c478bd9Sstevel@tonic-gate 		/*
3707c478bd9Sstevel@tonic-gate 		 * We found the thread in the callback table and have
3717c478bd9Sstevel@tonic-gate 		 * provisionally set the return value to true.  Now
3727c478bd9Sstevel@tonic-gate 		 * see if it is marked "safe" and is sleeping or stopped.
3737c478bd9Sstevel@tonic-gate 		 */
3747c478bd9Sstevel@tonic-gate 		callb_cpr_t *ccp = (callb_cpr_t *)cp->c_arg;
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 		*thread_name = cp->c_name;	/* in case not stopped */
3777c478bd9Sstevel@tonic-gate 		mutex_enter(ccp->cc_lockp);
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 		if (ccp->cc_events & CALLB_CPR_SAFE) {
3807c478bd9Sstevel@tonic-gate 			int retry;
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 			mutex_exit(ccp->cc_lockp);
3837c478bd9Sstevel@tonic-gate 			for (retry = 0; retry < CALLB_MAX_RETRY; retry++) {
3847c478bd9Sstevel@tonic-gate 				thread_lock(tp);
3857c478bd9Sstevel@tonic-gate 				if (tp->t_state & (TS_SLEEP | TS_STOPPED)) {
3867c478bd9Sstevel@tonic-gate 					thread_unlock(tp);
3877c478bd9Sstevel@tonic-gate 					break;
3887c478bd9Sstevel@tonic-gate 				}
3897c478bd9Sstevel@tonic-gate 				thread_unlock(tp);
3907c478bd9Sstevel@tonic-gate 				delay(CALLB_THREAD_DELAY);
3917c478bd9Sstevel@tonic-gate 			}
3927c478bd9Sstevel@tonic-gate 			ret_val = retry < CALLB_MAX_RETRY;
3937c478bd9Sstevel@tonic-gate 		} else {
3947c478bd9Sstevel@tonic-gate 			ret_val =
3957c478bd9Sstevel@tonic-gate 			    (ccp->cc_events & CALLB_CPR_ALWAYS_SAFE) != 0;
3967c478bd9Sstevel@tonic-gate 			mutex_exit(ccp->cc_lockp);
3977c478bd9Sstevel@tonic-gate 		}
3987c478bd9Sstevel@tonic-gate 	} else {
3997c478bd9Sstevel@tonic-gate 		/*
4007c478bd9Sstevel@tonic-gate 		 * Thread not found in callback table.  Make the best
4017c478bd9Sstevel@tonic-gate 		 * attempt to identify the thread in the error message.
4027c478bd9Sstevel@tonic-gate 		 */
4037c478bd9Sstevel@tonic-gate 		ulong_t offset;
4047c478bd9Sstevel@tonic-gate 		char *sym = kobj_getsymname((uintptr_t)tp->t_startpc,
4057c478bd9Sstevel@tonic-gate 		    &offset);
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 		*thread_name = sym ? sym : "*unknown*";
4087c478bd9Sstevel@tonic-gate 	}
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 	mutex_exit(&ct->ct_lock);
4117c478bd9Sstevel@tonic-gate 	return (ret_val);
4127c478bd9Sstevel@tonic-gate }
413