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/kernel.h> 33 #include <sys/malloc.h> 34 #include <sys/mman.h> 35 #include <sys/mutex.h> 36 #include <sys/refcount.h> 37 #include <sys/rwlock.h> 38 #include <sys/hwt.h> 39 40 #include <dev/hwt/hwt_hook.h> 41 #include <dev/hwt/hwt_context.h> 42 #include <dev/hwt/hwt_contexthash.h> 43 #include <dev/hwt/hwt_config.h> 44 #include <dev/hwt/hwt_cpu.h> 45 #include <dev/hwt/hwt_thread.h> 46 #include <dev/hwt/hwt_owner.h> 47 #include <dev/hwt/hwt_ownerhash.h> 48 #include <dev/hwt/hwt_backend.h> 49 #include <dev/hwt/hwt_vm.h> 50 #include <dev/hwt/hwt_record.h> 51 52 #define HWT_DEBUG 53 #undef HWT_DEBUG 54 55 #ifdef HWT_DEBUG 56 #define dprintf(fmt, ...) printf(fmt, ##__VA_ARGS__) 57 #else 58 #define dprintf(fmt, ...) 59 #endif 60 61 static MALLOC_DEFINE(M_HWT_OWNER, "hwt_owner", "Hardware Trace"); 62 63 struct hwt_context * 64 hwt_owner_lookup_ctx(struct hwt_owner *ho, pid_t pid) 65 { 66 struct hwt_context *ctx; 67 68 mtx_lock(&ho->mtx); 69 LIST_FOREACH(ctx, &ho->hwts, next_hwts) { 70 if (ctx->pid == pid) { 71 mtx_unlock(&ho->mtx); 72 return (ctx); 73 } 74 } 75 mtx_unlock(&ho->mtx); 76 77 return (NULL); 78 } 79 80 #if 0 81 struct hwt_context * 82 hwt_owner_lookup_ctx_by_cpu(struct hwt_owner *ho, int cpu) 83 { 84 struct hwt_context *ctx; 85 86 mtx_lock(&ho->mtx); 87 LIST_FOREACH(ctx, &ho->hwts, next_hwts) { 88 if (ctx->cpu == cpu) { 89 mtx_unlock(&ho->mtx); 90 return (ctx); 91 } 92 } 93 mtx_unlock(&ho->mtx); 94 95 return (NULL); 96 } 97 #endif 98 99 struct hwt_owner * 100 hwt_owner_alloc(struct proc *p) 101 { 102 struct hwt_owner *ho; 103 104 ho = malloc(sizeof(struct hwt_owner), M_HWT_OWNER, 105 M_WAITOK | M_ZERO); 106 ho->p = p; 107 108 LIST_INIT(&ho->hwts); 109 mtx_init(&ho->mtx, "hwts", NULL, MTX_DEF); 110 111 return (ho); 112 } 113 114 void 115 hwt_owner_shutdown(struct hwt_owner *ho) 116 { 117 struct hwt_context *ctx; 118 119 dprintf("%s: stopping hwt owner\n", __func__); 120 121 while (1) { 122 mtx_lock(&ho->mtx); 123 ctx = LIST_FIRST(&ho->hwts); 124 if (ctx) 125 LIST_REMOVE(ctx, next_hwts); 126 mtx_unlock(&ho->mtx); 127 128 if (ctx == NULL) 129 break; 130 131 if (ctx->mode == HWT_MODE_THREAD) 132 hwt_contexthash_remove(ctx); 133 134 /* 135 * A hook could be still dealing with this ctx right here. 136 */ 137 138 HWT_CTX_LOCK(ctx); 139 ctx->state = 0; 140 HWT_CTX_UNLOCK(ctx); 141 142 /* Ensure hooks invocation is now completed. */ 143 while (refcount_load(&ctx->refcnt) > 0) 144 continue; 145 146 /* 147 * Note that a thread could be still sleeping on msleep(9). 148 */ 149 150 hwt_backend_deinit(ctx); 151 hwt_record_free_all(ctx); 152 hwt_ctx_free(ctx); 153 } 154 155 hwt_ownerhash_remove(ho); 156 free(ho, M_HWT_OWNER); 157 } 158