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