#include <sys/cdefs.h>
#include "opt_ddb.h"
#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/linker.h>
#include <sys/lock.h>
#include <sys/malloc.h>
#include <sys/mutex.h>
#include <sys/systm.h>
static STAILQ_HEAD(, intr_config_hook) intr_config_hook_list =
STAILQ_HEAD_INITIALIZER(intr_config_hook_list);
static struct intr_config_hook *next_to_notify;
static struct mtx intr_config_hook_lock;
MTX_SYSINIT(intr_config_hook, &intr_config_hook_lock, "intr config", MTX_DEF);
static void run_interrupt_driven_config_hooks(void);
struct oneshot_config_hook {
struct intr_config_hook
och_hook;
ich_func_t och_func;
void *och_arg;
};
static void
config_intrhook_oneshot_func(void *arg)
{
struct oneshot_config_hook *ohook;
ohook = arg;
ohook->och_func(ohook->och_arg);
config_intrhook_disestablish(&ohook->och_hook);
free(ohook, M_DEVBUF);
}
#define WARNING_INTERVAL_SECS 60
static void
run_interrupt_driven_config_hooks_warning(int warned)
{
struct intr_config_hook *hook_entry;
char namebuf[64];
long offset;
if (warned < 6) {
printf("run_interrupt_driven_hooks: still waiting after %d "
"seconds for", warned * WARNING_INTERVAL_SECS);
STAILQ_FOREACH(hook_entry, &intr_config_hook_list, ich_links) {
if (linker_search_symbol_name(
(caddr_t)hook_entry->ich_func, namebuf,
sizeof(namebuf), &offset) == 0)
printf(" %s", namebuf);
else
printf(" %p", hook_entry->ich_func);
}
printf("\n");
}
KASSERT(warned < 6,
("run_interrupt_driven_config_hooks: waited too long"));
}
static void
run_interrupt_driven_config_hooks(void)
{
static int running;
struct intr_config_hook *hook_entry;
TSENTER();
mtx_lock(&intr_config_hook_lock);
if (running != 0) {
mtx_unlock(&intr_config_hook_lock);
return;
}
running = 1;
while (next_to_notify != NULL) {
hook_entry = next_to_notify;
next_to_notify = STAILQ_NEXT(hook_entry, ich_links);
hook_entry->ich_state = ICHS_RUNNING;
mtx_unlock(&intr_config_hook_lock);
(*hook_entry->ich_func)(hook_entry->ich_arg);
mtx_lock(&intr_config_hook_lock);
}
running = 0;
mtx_unlock(&intr_config_hook_lock);
TSEXIT();
}
static void
boot_run_interrupt_driven_config_hooks(void *dummy)
{
int warned;
run_interrupt_driven_config_hooks();
TSWAIT("config hooks");
mtx_lock(&intr_config_hook_lock);
warned = 0;
while (!STAILQ_EMPTY(&intr_config_hook_list)) {
if (msleep(&intr_config_hook_list, &intr_config_hook_lock,
0, "conifhk", WARNING_INTERVAL_SECS * hz) ==
EWOULDBLOCK) {
mtx_unlock(&intr_config_hook_lock);
warned++;
run_interrupt_driven_config_hooks_warning(warned);
mtx_lock(&intr_config_hook_lock);
}
}
mtx_unlock(&intr_config_hook_lock);
TSUNWAIT("config hooks");
}
SYSINIT(intr_config_hooks, SI_SUB_INT_CONFIG_HOOKS, SI_ORDER_FIRST,
boot_run_interrupt_driven_config_hooks, NULL);
int
config_intrhook_establish(struct intr_config_hook *hook)
{
struct intr_config_hook *hook_entry;
TSHOLD("config hooks");
mtx_lock(&intr_config_hook_lock);
STAILQ_FOREACH(hook_entry, &intr_config_hook_list, ich_links)
if (hook_entry == hook)
break;
if (hook_entry != NULL) {
mtx_unlock(&intr_config_hook_lock);
printf("config_intrhook_establish: establishing an "
"already established hook.\n");
return (1);
}
STAILQ_INSERT_TAIL(&intr_config_hook_list, hook, ich_links);
if (next_to_notify == NULL)
next_to_notify = hook;
hook->ich_state = ICHS_QUEUED;
mtx_unlock(&intr_config_hook_lock);
if (cold == 0)
run_interrupt_driven_config_hooks();
return (0);
}
void
config_intrhook_oneshot(ich_func_t func, void *arg)
{
struct oneshot_config_hook *ohook;
ohook = malloc(sizeof(*ohook), M_DEVBUF, M_WAITOK);
ohook->och_func = func;
ohook->och_arg = arg;
ohook->och_hook.ich_func = config_intrhook_oneshot_func;
ohook->och_hook.ich_arg = ohook;
config_intrhook_establish(&ohook->och_hook);
}
static void
config_intrhook_disestablish_locked(struct intr_config_hook *hook)
{
struct intr_config_hook *hook_entry;
STAILQ_FOREACH(hook_entry, &intr_config_hook_list, ich_links)
if (hook_entry == hook)
break;
if (hook_entry == NULL)
panic("config_intrhook_disestablish: disestablishing an "
"unestablished hook");
if (next_to_notify == hook)
next_to_notify = STAILQ_NEXT(hook, ich_links);
STAILQ_REMOVE(&intr_config_hook_list, hook, intr_config_hook, ich_links);
TSRELEASE("config hooks");
hook->ich_state = ICHS_DONE;
wakeup(&intr_config_hook_list);
}
void
config_intrhook_disestablish(struct intr_config_hook *hook)
{
mtx_lock(&intr_config_hook_lock);
config_intrhook_disestablish_locked(hook);
mtx_unlock(&intr_config_hook_lock);
}
int
config_intrhook_drain(struct intr_config_hook *hook)
{
mtx_lock(&intr_config_hook_lock);
if (hook->ich_state == ICHS_DONE) {
mtx_unlock(&intr_config_hook_lock);
return (ICHS_DONE);
}
if (hook->ich_state == ICHS_QUEUED) {
config_intrhook_disestablish_locked(hook);
mtx_unlock(&intr_config_hook_lock);
return (ICHS_QUEUED);
}
while (hook->ich_state != ICHS_DONE) {
if (msleep(&intr_config_hook_list, &intr_config_hook_lock,
0, "confhd", hz) == EWOULDBLOCK) {
}
}
mtx_unlock(&intr_config_hook_lock);
return (ICHS_RUNNING);
}
#ifdef DDB
#include <ddb/ddb.h>
DB_SHOW_COMMAND_FLAGS(conifhk, db_show_conifhk, DB_CMD_MEMSAFE)
{
struct intr_config_hook *hook_entry;
char namebuf[64];
long offset;
STAILQ_FOREACH(hook_entry, &intr_config_hook_list, ich_links) {
if (linker_ddb_search_symbol_name(
(caddr_t)hook_entry->ich_func, namebuf, sizeof(namebuf),
&offset) == 0) {
db_printf("hook: %p at %s+%#lx arg: %p\n",
hook_entry->ich_func, namebuf, offset,
hook_entry->ich_arg);
} else {
db_printf("hook: %p at ??+?? arg %p\n",
hook_entry->ich_func, hook_entry->ich_arg);
}
}
}
#endif