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/proc.h>
33 #include <sys/kernel.h>
34 #include <sys/lock.h>
35 #include <sys/malloc.h>
36 #include <sys/mutex.h>
37 #include <sys/hwt.h>
38
39 #include <vm/vm.h>
40
41 #include <dev/hwt/hwt_hook.h>
42 #include <dev/hwt/hwt_context.h>
43 #include <dev/hwt/hwt_contexthash.h>
44 #include <dev/hwt/hwt_config.h>
45 #include <dev/hwt/hwt_thread.h>
46 #include <dev/hwt/hwt_record.h>
47 #include <dev/hwt/hwt_cpu.h>
48
49 #define HWT_CPU_DEBUG
50 #undef HWT_CPU_DEBUG
51
52 #ifdef HWT_CPU_DEBUG
53 #define dprintf(fmt, ...) printf(fmt, ##__VA_ARGS__)
54 #else
55 #define dprintf(fmt, ...)
56 #endif
57
58 static MALLOC_DEFINE(M_HWT_CPU, "hwt_cpu", "HWT cpu");
59
60 struct hwt_cpu *
hwt_cpu_alloc(void)61 hwt_cpu_alloc(void)
62 {
63 struct hwt_cpu *cpu;
64
65 cpu = malloc(sizeof(struct hwt_cpu), M_HWT_CPU, M_WAITOK | M_ZERO);
66
67 return (cpu);
68 }
69
70 void
hwt_cpu_free(struct hwt_cpu * cpu)71 hwt_cpu_free(struct hwt_cpu *cpu)
72 {
73
74 free(cpu, M_HWT_CPU);
75 }
76
77 struct hwt_cpu *
hwt_cpu_first(struct hwt_context * ctx)78 hwt_cpu_first(struct hwt_context *ctx)
79 {
80 struct hwt_cpu *cpu;
81
82 HWT_CTX_ASSERT_LOCKED(ctx);
83
84 cpu = TAILQ_FIRST(&ctx->cpus);
85
86 KASSERT(cpu != NULL, ("cpu is NULL"));
87
88 return (cpu);
89 }
90
91 struct hwt_cpu *
hwt_cpu_get(struct hwt_context * ctx,int cpu_id)92 hwt_cpu_get(struct hwt_context *ctx, int cpu_id)
93 {
94 struct hwt_cpu *cpu, *tcpu;
95
96 HWT_CTX_ASSERT_LOCKED(ctx);
97
98 TAILQ_FOREACH_SAFE(cpu, &ctx->cpus, next, tcpu) {
99 KASSERT(cpu != NULL, ("cpu is NULL"));
100 if (cpu->cpu_id == cpu_id) {
101 return cpu;
102 }
103 }
104
105 return (NULL);
106 }
107
108 void
hwt_cpu_insert(struct hwt_context * ctx,struct hwt_cpu * cpu)109 hwt_cpu_insert(struct hwt_context *ctx, struct hwt_cpu *cpu)
110 {
111
112 HWT_CTX_ASSERT_LOCKED(ctx);
113
114 TAILQ_INSERT_TAIL(&ctx->cpus, cpu, next);
115 }
116