#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/mman.h>
#include <sys/mutex.h>
#include <sys/refcount.h>
#include <sys/rwlock.h>
#include <sys/hwt.h>
#include <dev/hwt/hwt_hook.h>
#include <dev/hwt/hwt_context.h>
#include <dev/hwt/hwt_contexthash.h>
#include <dev/hwt/hwt_config.h>
#include <dev/hwt/hwt_cpu.h>
#include <dev/hwt/hwt_thread.h>
#include <dev/hwt/hwt_owner.h>
#include <dev/hwt/hwt_ownerhash.h>
#include <dev/hwt/hwt_backend.h>
#include <dev/hwt/hwt_vm.h>
#include <dev/hwt/hwt_record.h>
#define HWT_DEBUG
#undef HWT_DEBUG
#ifdef HWT_DEBUG
#define dprintf(fmt, ...) printf(fmt, ##__VA_ARGS__)
#else
#define dprintf(fmt, ...)
#endif
static MALLOC_DEFINE(M_HWT_OWNER, "hwt_owner", "Hardware Trace");
struct hwt_context *
hwt_owner_lookup_ctx(struct hwt_owner *ho, pid_t pid)
{
struct hwt_context *ctx;
mtx_lock(&ho->mtx);
LIST_FOREACH(ctx, &ho->hwts, next_hwts) {
if (ctx->pid == pid) {
mtx_unlock(&ho->mtx);
return (ctx);
}
}
mtx_unlock(&ho->mtx);
return (NULL);
}
#if 0
struct hwt_context *
hwt_owner_lookup_ctx_by_cpu(struct hwt_owner *ho, int cpu)
{
struct hwt_context *ctx;
mtx_lock(&ho->mtx);
LIST_FOREACH(ctx, &ho->hwts, next_hwts) {
if (ctx->cpu == cpu) {
mtx_unlock(&ho->mtx);
return (ctx);
}
}
mtx_unlock(&ho->mtx);
return (NULL);
}
#endif
struct hwt_owner *
hwt_owner_alloc(struct proc *p)
{
struct hwt_owner *ho;
ho = malloc(sizeof(struct hwt_owner), M_HWT_OWNER,
M_WAITOK | M_ZERO);
ho->p = p;
LIST_INIT(&ho->hwts);
mtx_init(&ho->mtx, "hwts", NULL, MTX_DEF);
return (ho);
}
void
hwt_owner_shutdown(struct hwt_owner *ho)
{
struct hwt_context *ctx;
dprintf("%s: stopping hwt owner\n", __func__);
while (1) {
mtx_lock(&ho->mtx);
ctx = LIST_FIRST(&ho->hwts);
if (ctx)
LIST_REMOVE(ctx, next_hwts);
mtx_unlock(&ho->mtx);
if (ctx == NULL)
break;
if (ctx->mode == HWT_MODE_THREAD)
hwt_contexthash_remove(ctx);
HWT_CTX_LOCK(ctx);
ctx->state = 0;
HWT_CTX_UNLOCK(ctx);
while (refcount_load(&ctx->refcnt) > 0)
continue;
hwt_backend_deinit(ctx);
hwt_record_free_all(ctx);
hwt_ctx_free(ctx);
}
hwt_ownerhash_remove(ho);
free(ho, M_HWT_OWNER);
}