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