xref: /freebsd/sys/dev/hwt/hwt_context.h (revision df114daef4c48548c3c2b86717612761185ae18f)
1 /*-
2  * Copyright (c) 2023-2025 Ruslan Bukin <br@bsdpad.com>
3  *
4  * This work was supported by Innovate UK project 105694, "Digital Security
5  * by Design (DSbD) Technology Platform Prototype".
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #ifndef _DEV_HWT_HWT_CONTEXT_H_
30 #define _DEV_HWT_HWT_CONTEXT_H_
31 
32 enum hwt_ctx_state {
33 	CTX_STATE_STOPPED,
34 	CTX_STATE_RUNNING,
35 };
36 
37 struct hwt_context {
38 	TAILQ_HEAD(, hwt_record_entry)	records;
39 
40 	LIST_ENTRY(hwt_context)		next_hch; /* Entry in contexthash. */
41 	LIST_ENTRY(hwt_context)		next_hwts; /* Entry in ho->hwts. */
42 
43 	int				mode;
44 	int				ident;
45 
46 	int				kqueue_fd;
47 	struct thread			*hwt_td;
48 
49 	/* CPU mode. */
50 	cpuset_t			cpu_map;
51 	TAILQ_HEAD(, hwt_cpu)		cpus;
52 
53 	/* Thread mode. */
54 	struct proc			*proc; /* Target proc. */
55 	pid_t				pid; /* Target pid. */
56 	TAILQ_HEAD(, hwt_thread)	threads;
57 	int				thread_counter;
58 	int				pause_on_mmap;
59 
60 	size_t				bufsize; /* Trace bufsize for each vm.*/
61 
62 	void				*config;
63 	size_t				config_size;
64 	int				config_version;
65 
66 	struct hwt_owner		*hwt_owner;
67 	struct hwt_backend		*hwt_backend;
68 
69 	struct mtx			mtx;
70 	struct mtx			rec_mtx;
71 	enum hwt_ctx_state		state;
72 	int				refcnt;
73 };
74 
75 #define	HWT_CTX_LOCK(ctx)		mtx_lock_spin(&(ctx)->mtx)
76 #define	HWT_CTX_UNLOCK(ctx)		mtx_unlock_spin(&(ctx)->mtx)
77 #define	HWT_CTX_ASSERT_LOCKED(ctx)	mtx_assert(&(ctx)->mtx, MA_OWNED)
78 
79 int hwt_ctx_alloc(struct hwt_context **ctx0);
80 void hwt_ctx_free(struct hwt_context *ctx);
81 void hwt_ctx_put(struct hwt_context *ctx);
82 
83 void hwt_ctx_load(void);
84 void hwt_ctx_unload(void);
85 
86 #endif /* !_DEV_HWT_HWT_CONTEXT_H_ */
87