1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2023-2025 Ruslan Bukin <br@bsdpad.com> 5 * 6 * This work was supported by Innovate UK project 105694, "Digital Security 7 * by Design (DSbD) Technology Platform Prototype". 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 */ 30 31 #include <sys/param.h> 32 #include <sys/bitstring.h> 33 #include <sys/conf.h> 34 #include <sys/proc.h> 35 #include <sys/kernel.h> 36 #include <sys/malloc.h> 37 #include <sys/mman.h> 38 #include <sys/mutex.h> 39 #include <sys/refcount.h> 40 #include <sys/rwlock.h> 41 #include <sys/hwt.h> 42 43 #include <dev/hwt/hwt_hook.h> 44 #include <dev/hwt/hwt_context.h> 45 #include <dev/hwt/hwt_config.h> 46 #include <dev/hwt/hwt_thread.h> 47 #include <dev/hwt/hwt_owner.h> 48 #include <dev/hwt/hwt_vm.h> 49 #include <dev/hwt/hwt_cpu.h> 50 51 #define HWT_DEBUG 52 #undef HWT_DEBUG 53 54 #ifdef HWT_DEBUG 55 #define dprintf(fmt, ...) printf(fmt, ##__VA_ARGS__) 56 #else 57 #define dprintf(fmt, ...) 58 #endif 59 60 static MALLOC_DEFINE(M_HWT_CTX, "hwt_ctx", "Hardware Trace"); 61 62 static bitstr_t *ident_set; 63 static int ident_set_size; 64 static struct mtx ident_set_mutex; 65 66 static int 67 hwt_ctx_ident_alloc(int *new_ident) 68 { 69 70 mtx_lock(&ident_set_mutex); 71 bit_ffc(ident_set, ident_set_size, new_ident); 72 if (*new_ident == -1) { 73 mtx_unlock(&ident_set_mutex); 74 return (ENOMEM); 75 } 76 bit_set(ident_set, *new_ident); 77 mtx_unlock(&ident_set_mutex); 78 79 return (0); 80 } 81 82 static void 83 hwt_ctx_ident_free(int ident) 84 { 85 86 mtx_lock(&ident_set_mutex); 87 bit_clear(ident_set, ident); 88 mtx_unlock(&ident_set_mutex); 89 } 90 91 int 92 hwt_ctx_alloc(struct hwt_context **ctx0) 93 { 94 struct hwt_context *ctx; 95 int error; 96 97 ctx = malloc(sizeof(struct hwt_context), M_HWT_CTX, M_WAITOK | M_ZERO); 98 99 TAILQ_INIT(&ctx->records); 100 TAILQ_INIT(&ctx->threads); 101 TAILQ_INIT(&ctx->cpus); 102 mtx_init(&ctx->mtx, "ctx", NULL, MTX_SPIN); 103 mtx_init(&ctx->rec_mtx, "ctx_rec", NULL, MTX_DEF); 104 refcount_init(&ctx->refcnt, 0); 105 106 error = hwt_ctx_ident_alloc(&ctx->ident); 107 if (error) { 108 printf("could not allocate ident bit str\n"); 109 return (error); 110 } 111 112 *ctx0 = ctx; 113 114 return (0); 115 } 116 117 static void 118 hwt_ctx_free_cpus(struct hwt_context *ctx) 119 { 120 struct hwt_cpu *cpu; 121 122 do { 123 HWT_CTX_LOCK(ctx); 124 cpu = TAILQ_FIRST(&ctx->cpus); 125 if (cpu) 126 TAILQ_REMOVE(&ctx->cpus, cpu, next); 127 HWT_CTX_UNLOCK(ctx); 128 129 if (cpu == NULL) 130 break; 131 132 /* TODO: move vm_free() to cpu_free()? */ 133 hwt_vm_free(cpu->vm); 134 hwt_cpu_free(cpu); 135 } while (1); 136 } 137 138 static void 139 hwt_ctx_free_threads(struct hwt_context *ctx) 140 { 141 struct hwt_thread *thr; 142 143 dprintf("%s: remove threads\n", __func__); 144 145 do { 146 HWT_CTX_LOCK(ctx); 147 thr = TAILQ_FIRST(&ctx->threads); 148 if (thr) 149 TAILQ_REMOVE(&ctx->threads, thr, next); 150 HWT_CTX_UNLOCK(ctx); 151 152 if (thr == NULL) 153 break; 154 155 HWT_THR_LOCK(thr); 156 /* TODO: check if thr is sleeping before waking it up. */ 157 wakeup(thr); 158 HWT_THR_UNLOCK(thr); 159 160 if (refcount_release(&thr->refcnt)) 161 hwt_thread_free(thr); 162 } while (1); 163 } 164 165 void 166 hwt_ctx_free(struct hwt_context *ctx) 167 { 168 169 if (ctx->mode == HWT_MODE_CPU) 170 hwt_ctx_free_cpus(ctx); 171 else 172 hwt_ctx_free_threads(ctx); 173 174 hwt_config_free(ctx); 175 hwt_ctx_ident_free(ctx->ident); 176 free(ctx, M_HWT_CTX); 177 } 178 179 void 180 hwt_ctx_put(struct hwt_context *ctx) 181 { 182 183 refcount_release(&ctx->refcnt); 184 } 185 186 void 187 hwt_ctx_load(void) 188 { 189 190 ident_set_size = (1 << 8); 191 ident_set = bit_alloc(ident_set_size, M_HWT_CTX, M_WAITOK); 192 mtx_init(&ident_set_mutex, "ident set", NULL, MTX_DEF); 193 } 194 195 void 196 hwt_ctx_unload(void) 197 { 198 199 mtx_destroy(&ident_set_mutex); 200 free(ident_set, M_HWT_CTX); 201 } 202