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/malloc.h>
35 #include <sys/mutex.h>
36 #include <sys/lock.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
48 #define HWT_MAXCONFIGSIZE PAGE_SIZE
49
50 #define HWT_CONFIG_DEBUG
51 #undef HWT_CONFIG_DEBUG
52
53 #ifdef HWT_CONFIG_DEBUG
54 #define dprintf(fmt, ...) printf(fmt, ##__VA_ARGS__)
55 #else
56 #define dprintf(fmt, ...)
57 #endif
58
59 static MALLOC_DEFINE(M_HWT_CONFIG, "hwt_config", "HWT config");
60
61 int
hwt_config_set(struct thread * td,struct hwt_context * ctx,struct hwt_set_config * sconf)62 hwt_config_set(struct thread *td, struct hwt_context *ctx,
63 struct hwt_set_config *sconf)
64 {
65 size_t config_size;
66 void *old_config;
67 void *config;
68 int error;
69
70 config_size = sconf->config_size;
71 if (config_size == 0)
72 return (0);
73
74 if (config_size > HWT_MAXCONFIGSIZE)
75 return (EFBIG);
76
77 config = malloc(config_size, M_HWT_CONFIG, M_WAITOK | M_ZERO);
78
79 error = copyin(sconf->config, config, config_size);
80 if (error) {
81 free(config, M_HWT_CONFIG);
82 return (error);
83 }
84
85 HWT_CTX_LOCK(ctx);
86 old_config = ctx->config;
87 ctx->config = config;
88 ctx->config_size = sconf->config_size;
89 ctx->config_version = sconf->config_version;
90 HWT_CTX_UNLOCK(ctx);
91
92 if (old_config != NULL)
93 free(old_config, M_HWT_CONFIG);
94
95 return (error);
96 }
97
98 void
hwt_config_free(struct hwt_context * ctx)99 hwt_config_free(struct hwt_context *ctx)
100 {
101
102 if (ctx->config == NULL)
103 return;
104
105 free(ctx->config, M_HWT_CONFIG);
106
107 ctx->config = NULL;
108 }
109