#include <sys/param.h>
#include <sys/t_lock.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/sysmacros.h>
#include <sys/systm.h>
#include <sys/cpuvar.h>
#include <sys/user.h>
#include <sys/proc.h>
#include <sys/callb.h>
#include <sys/kmem.h>
#include <sys/cmn_err.h>
#include <sys/swap.h>
#include <sys/vmsystm.h>
#include <sys/class.h>
#include <sys/debug.h>
#include <sys/thread.h>
#include <sys/kobj.h>
#include <sys/ddi.h>
#include <sys/taskq.h>
typedef struct callb {
struct callb *c_next;
kthread_id_t c_thread;
char c_flag;
uchar_t c_class;
kcondvar_t c_done_cv;
boolean_t (*c_func)();
void *c_arg;
char c_name[CB_MAXNAME+1];
} callb_t;
#define CALLB_FREE 0x0
#define CALLB_TAKEN 0x1
#define CALLB_EXECUTING 0x2
typedef struct callb_table {
kmutex_t ct_lock;
callb_t *ct_freelist;
int ct_busy;
kcondvar_t ct_busy_cv;
int ct_ncallb;
callb_t *ct_first_cb[NCBCLASS];
} callb_table_t;
int callb_timeout_sec = CPR_KTHREAD_TIMEOUT_SEC;
static callb_id_t callb_add_common(boolean_t (*)(void *, int),
void *, int, char *, kthread_id_t);
static callb_table_t callb_table;
static callb_table_t *ct = &callb_table;
static kmutex_t callb_safe_mutex;
callb_cpr_t callb_cprinfo_safe = {
&callb_safe_mutex, CALLB_CPR_ALWAYS_SAFE, 0, 0, 0 };
void
callb_init()
{
callb_table.ct_busy = 0;
mutex_init(&callb_safe_mutex, NULL, MUTEX_DEFAULT, NULL);
mutex_init(&callb_table.ct_lock, NULL, MUTEX_DEFAULT, NULL);
}
static callb_id_t
callb_add_common(boolean_t (*func)(void *arg, int code),
void *arg, int class, char *name, kthread_id_t t)
{
callb_t *cp;
ASSERT(class < NCBCLASS);
mutex_enter(&ct->ct_lock);
while (ct->ct_busy)
cv_wait(&ct->ct_busy_cv, &ct->ct_lock);
if ((cp = ct->ct_freelist) == NULL) {
ct->ct_ncallb++;
cp = (callb_t *)kmem_zalloc(sizeof (callb_t), KM_SLEEP);
}
ct->ct_freelist = cp->c_next;
cp->c_thread = t;
cp->c_func = func;
cp->c_arg = arg;
cp->c_class = (uchar_t)class;
cp->c_flag |= CALLB_TAKEN;
#ifdef DEBUG
if (strlen(name) > CB_MAXNAME)
cmn_err(CE_WARN, "callb_add: name of callback function '%s' "
"too long -- truncated to %d chars",
name, CB_MAXNAME);
#endif
(void) strncpy(cp->c_name, name, CB_MAXNAME);
cp->c_name[CB_MAXNAME] = '\0';
cp->c_next = ct->ct_first_cb[class];
ct->ct_first_cb[class] = cp;
mutex_exit(&ct->ct_lock);
return ((callb_id_t)cp);
}
callb_id_t
callb_add(boolean_t (*func)(void *arg, int code),
void *arg, int class, char *name)
{
return (callb_add_common(func, arg, class, name, curthread));
}
callb_id_t
callb_add_thread(boolean_t (*func)(void *arg, int code),
void *arg, int class, char *name, kthread_id_t t)
{
return (callb_add_common(func, arg, class, name, t));
}
int
callb_delete(callb_id_t id)
{
callb_t **pp;
callb_t *me = (callb_t *)id;
mutex_enter(&ct->ct_lock);
for (;;) {
pp = &ct->ct_first_cb[me->c_class];
while (*pp != NULL && *pp != me)
pp = &(*pp)->c_next;
#ifdef DEBUG
if (*pp != me) {
cmn_err(CE_WARN, "callb delete bogus entry 0x%p",
(void *)me);
mutex_exit(&ct->ct_lock);
return (-1);
}
#endif
if (!(me->c_flag & CALLB_EXECUTING))
break;
cv_wait(&me->c_done_cv, &ct->ct_lock);
}
*pp = me->c_next;
me->c_flag = CALLB_FREE;
me->c_next = ct->ct_freelist;
ct->ct_freelist = me;
mutex_exit(&ct->ct_lock);
return (0);
}
void *
callb_execute_class(int class, int code)
{
callb_t *cp;
void *ret = NULL;
ASSERT(class < NCBCLASS);
mutex_enter(&ct->ct_lock);
for (cp = ct->ct_first_cb[class];
cp != NULL && ret == 0; cp = cp->c_next) {
while (cp->c_flag & CALLB_EXECUTING)
cv_wait(&cp->c_done_cv, &ct->ct_lock);
if (cp->c_flag == CALLB_FREE)
continue;
cp->c_flag |= CALLB_EXECUTING;
#ifdef CALLB_DEBUG
printf("callb_execute: name=%s func=%p arg=%p\n",
cp->c_name, (void *)cp->c_func, (void *)cp->c_arg);
#endif
mutex_exit(&ct->ct_lock);
if (!(*cp->c_func)(cp->c_arg, code))
ret = cp->c_name;
mutex_enter(&ct->ct_lock);
cp->c_flag &= ~CALLB_EXECUTING;
cv_broadcast(&cp->c_done_cv);
}
mutex_exit(&ct->ct_lock);
return (ret);
}
boolean_t
callb_generic_cpr(void *arg, int code)
{
callb_cpr_t *cp = (callb_cpr_t *)arg;
clock_t ret = 0;
mutex_enter(cp->cc_lockp);
switch (code) {
case CB_CODE_CPR_CHKPT:
cp->cc_events |= CALLB_CPR_START;
#ifdef CPR_NOT_THREAD_SAFE
while (!(cp->cc_events & CALLB_CPR_SAFE))
if ((ret = cv_reltimedwait(&cp->cc_callb_cv,
cp->cc_lockp, (callb_timeout_sec * hz),
TR_CLOCK_TICK)) == -1)
break;
#endif
break;
case CB_CODE_CPR_RESUME:
cp->cc_events &= ~CALLB_CPR_START;
cv_signal(&cp->cc_stop_cv);
break;
}
mutex_exit(cp->cc_lockp);
return (ret != -1);
}
boolean_t
callb_generic_cpr_safe(void *arg, int code)
{
return (B_TRUE);
}
void
callb_lock_table(void)
{
mutex_enter(&ct->ct_lock);
ASSERT(ct->ct_busy == 0);
ct->ct_busy = 1;
mutex_exit(&ct->ct_lock);
}
void
callb_unlock_table(void)
{
mutex_enter(&ct->ct_lock);
ASSERT(ct->ct_busy != 0);
ct->ct_busy = 0;
cv_broadcast(&ct->ct_busy_cv);
mutex_exit(&ct->ct_lock);
}
boolean_t
callb_is_stopped(kthread_id_t tp, caddr_t *thread_name)
{
callb_t *cp;
boolean_t ret_val;
mutex_enter(&ct->ct_lock);
for (cp = ct->ct_first_cb[CB_CL_CPR_DAEMON];
cp != NULL && tp != cp->c_thread; cp = cp->c_next)
;
ret_val = (cp != NULL);
if (ret_val) {
callb_cpr_t *ccp = (callb_cpr_t *)cp->c_arg;
*thread_name = cp->c_name;
mutex_enter(ccp->cc_lockp);
if (ccp->cc_events & CALLB_CPR_SAFE) {
int retry;
mutex_exit(ccp->cc_lockp);
for (retry = 0; retry < CALLB_MAX_RETRY; retry++) {
thread_lock(tp);
if (tp->t_state & (TS_SLEEP | TS_STOPPED)) {
thread_unlock(tp);
break;
}
thread_unlock(tp);
delay(CALLB_THREAD_DELAY);
}
ret_val = retry < CALLB_MAX_RETRY;
} else {
ret_val =
(ccp->cc_events & CALLB_CPR_ALWAYS_SAFE) != 0;
mutex_exit(ccp->cc_lockp);
}
} else {
ulong_t offset;
char *sym = kobj_getsymname((uintptr_t)tp->t_startpc,
&offset);
*thread_name = sym ? sym : "*unknown*";
}
mutex_exit(&ct->ct_lock);
return (ret_val);
}