#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/kobj.h>
#include <sys/lock.h>
#include <sys/malloc.h>
#include <sys/mutex.h>
#include <sys/sysctl.h>
#ifndef TEST
#include <sys/systm.h>
#endif
#ifdef TEST
#include "usertest.h"
#endif
static MALLOC_DEFINE(M_KOBJ, "kobj", "Kernel object structures");
#ifdef KOBJ_STATS
u_int kobj_lookup_hits;
u_int kobj_lookup_misses;
SYSCTL_UINT(_kern, OID_AUTO, kobj_hits, CTLFLAG_RD,
&kobj_lookup_hits, 0, "");
SYSCTL_UINT(_kern, OID_AUTO, kobj_misses, CTLFLAG_RD,
&kobj_lookup_misses, 0, "");
#endif
static struct mtx kobj_mtx;
static int kobj_mutex_inited;
static int kobj_next_id = 1;
#define KOBJ_LOCK() mtx_lock(&kobj_mtx)
#define KOBJ_UNLOCK() mtx_unlock(&kobj_mtx)
#define KOBJ_ASSERT(what) mtx_assert(&kobj_mtx, what);
SYSCTL_INT(_kern, OID_AUTO, kobj_methodcount, CTLFLAG_RD,
&kobj_next_id, 0,
"Number of kernel object methods registered");
static void
kobj_init_mutex(void *arg)
{
if (!kobj_mutex_inited) {
mtx_init(&kobj_mtx, "kobj", NULL, MTX_DEF);
kobj_mutex_inited = 1;
}
}
SYSINIT(kobj, SI_SUB_LOCK, SI_ORDER_ANY, kobj_init_mutex, NULL);
static const struct kobj_method null_method = {
0, 0,
};
int
kobj_error_method(void)
{
return ENXIO;
}
static void
kobj_class_compile_common(kobj_class_t cls, kobj_ops_t ops)
{
kobj_method_t *m;
int i;
if (cls->ops)
return;
for (m = cls->methods; m->desc; m++) {
if (m->desc->id == 0)
m->desc->id = kobj_next_id++;
}
for (i = 0; i < KOBJ_CACHE_SIZE; i++)
ops->cache[i] = &null_method;
ops->cls = cls;
cls->ops = ops;
}
static int
kobj_class_compile1(kobj_class_t cls, int mflags)
{
kobj_ops_t ops;
KOBJ_ASSERT(MA_NOTOWNED);
ops = malloc(sizeof(struct kobj_ops), M_KOBJ, mflags);
if (ops == NULL)
return (ENOMEM);
KOBJ_LOCK();
if (cls->ops) {
KOBJ_UNLOCK();
free(ops, M_KOBJ);
return (0);
}
kobj_class_compile_common(cls, ops);
KOBJ_UNLOCK();
return (0);
}
void
kobj_class_compile(kobj_class_t cls)
{
int error __diagused;
error = kobj_class_compile1(cls, M_WAITOK);
KASSERT(error == 0, ("kobj_class_compile1 returned %d", error));
}
void
kobj_class_compile_static(kobj_class_t cls, kobj_ops_t ops)
{
KASSERT(kobj_mutex_inited == 0,
("%s: only supported during early cycles", __func__));
cls->refs++;
kobj_class_compile_common(cls, ops);
}
static kobj_method_t*
kobj_lookup_method_class(kobj_class_t cls, kobjop_desc_t desc)
{
kobj_method_t *methods = cls->methods;
kobj_method_t *ce;
for (ce = methods; ce && ce->desc; ce++) {
if (ce->desc == desc) {
return ce;
}
}
return NULL;
}
static kobj_method_t*
kobj_lookup_method_mi(kobj_class_t cls,
kobjop_desc_t desc)
{
kobj_method_t *ce;
kobj_class_t *basep;
ce = kobj_lookup_method_class(cls, desc);
if (ce)
return ce;
basep = cls->baseclasses;
if (basep) {
for (; *basep; basep++) {
ce = kobj_lookup_method_mi(*basep, desc);
if (ce)
return ce;
}
}
return NULL;
}
kobj_method_t*
kobj_lookup_method(kobj_class_t cls,
kobj_method_t **cep,
kobjop_desc_t desc)
{
kobj_method_t *ce;
ce = kobj_lookup_method_mi(cls, desc);
if (!ce)
ce = &desc->deflt;
if (cep)
*cep = ce;
return ce;
}
void
kobj_class_free(kobj_class_t cls)
{
void* ops = NULL;
KOBJ_ASSERT(MA_NOTOWNED);
KOBJ_LOCK();
if (cls->refs == 0) {
ops = cls->ops;
cls->ops = NULL;
}
KOBJ_UNLOCK();
if (ops)
free(ops, M_KOBJ);
}
static void
kobj_init_common(kobj_t obj, kobj_class_t cls)
{
obj->ops = cls->ops;
cls->refs++;
}
static int
kobj_init1(kobj_t obj, kobj_class_t cls, int mflags)
{
int error;
KOBJ_LOCK();
while (cls->ops == NULL) {
KOBJ_UNLOCK();
error = kobj_class_compile1(cls, mflags);
if (error != 0)
return (error);
KOBJ_LOCK();
}
kobj_init_common(obj, cls);
KOBJ_UNLOCK();
return (0);
}
kobj_t
kobj_create(kobj_class_t cls, struct malloc_type *mtype, int mflags)
{
kobj_t obj;
obj = malloc(cls->size, mtype, mflags | M_ZERO);
if (obj == NULL)
return (NULL);
if (kobj_init1(obj, cls, mflags) != 0) {
free(obj, mtype);
return (NULL);
}
return (obj);
}
void
kobj_init(kobj_t obj, kobj_class_t cls)
{
int error;
error = kobj_init1(obj, cls, M_NOWAIT);
if (error != 0)
panic("kobj_init1 failed: error %d", error);
}
void
kobj_init_static(kobj_t obj, kobj_class_t cls)
{
KASSERT(kobj_mutex_inited == 0,
("%s: only supported during early cycles", __func__));
kobj_init_common(obj, cls);
}
void
kobj_delete(kobj_t obj, struct malloc_type *mtype)
{
kobj_class_t cls = obj->ops->cls;
int refs;
KOBJ_ASSERT(MA_NOTOWNED);
KOBJ_LOCK();
cls->refs--;
refs = cls->refs;
KOBJ_UNLOCK();
if (!refs)
kobj_class_free(cls);
obj->ops = NULL;
if (mtype)
free(obj, mtype);
}