| 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 | /* User-visible header. */ |
| 30 | |
| 31 | #include <sys/param.h> |
| 32 | #include <sys/cpuset.h> |
| 33 | #include <sys/types.h> |
| 34 | #include <sys/hwt_record.h> |
| 35 | |
| 36 | #ifndef _SYS_HWT_H_ |
| 37 | #define _SYS_HWT_H_ |
| 38 | |
| 39 | #define	HWT_MAGIC		0x42 |
| 40 | #define	HWT_IOC_ALLOC		_IOW(HWT_MAGIC, 0x00, struct hwt_alloc) |
| 41 | #define	HWT_IOC_START		_IOW(HWT_MAGIC, 0x01, struct hwt_start) |
| 42 | #define	HWT_IOC_STOP		_IOW(HWT_MAGIC, 0x02, struct hwt_stop) |
| 43 | #define	HWT_IOC_RECORD_GET	_IOW(HWT_MAGIC, 0x03, struct hwt_record_get) |
| 44 | #define	HWT_IOC_BUFPTR_GET	_IOW(HWT_MAGIC, 0x04, struct hwt_bufptr_get) |
| 45 | #define	HWT_IOC_SET_CONFIG	_IOW(HWT_MAGIC, 0x05, struct hwt_set_config) |
| 46 | #define	HWT_IOC_WAKEUP		_IOW(HWT_MAGIC, 0x06, struct hwt_wakeup) |
| 47 | #define	HWT_IOC_SVC_BUF		_IOW(HWT_MAGIC, 0x07, struct hwt_svc_buf) |
| 48 | |
| 49 | #define	HWT_BACKEND_MAXNAMELEN	256 |
| 50 | |
| 51 | #define	HWT_MODE_THREAD		1 |
| 52 | #define	HWT_MODE_CPU		2 |
| 53 | |
| 54 | struct hwt_alloc { |
| 55 | 	size_t		bufsize; |
| 56 | 	int		mode; |
| 57 | 	pid_t		pid;		/* thread mode */ |
| 58 | 	cpuset_t	*cpu_map;	/* cpu mode only */ |
| 59 | 	size_t		cpusetsize; |
| 60 | 	const char	*backend_name; |
| 61 | 	int		*ident; |
| 62 | 	int		kqueue_fd; |
| 63 | } __aligned(16); |
| 64 | |
| 65 | struct hwt_start { |
| 66 | 	int		reserved; |
| 67 | } __aligned(16); |
| 68 | |
| 69 | struct hwt_stop { |
| 70 | 	int		reserved; |
| 71 | } __aligned(16); |
| 72 | |
| 73 | struct hwt_wakeup { |
| 74 | 	int		reserved; |
| 75 | } __aligned(16); |
| 76 | |
| 77 | struct hwt_record_user_entry { |
| 78 | 	enum hwt_record_type	record_type; |
| 79 | 	union { |
| 80 | 		/* |
| 81 | 		 * Used for MMAP, EXECUTABLE, INTERP, |
| 82 | 		 * and KERNEL records. |
| 83 | 		 */ |
| 84 | 		struct { |
| 85 | 			char fullpath[MAXPATHLEN]; |
| 86 | 			uintptr_t addr; |
| 87 | 			uintptr_t baseaddr; |
| 88 | 		}; |
| 89 | 		/* Used for BUFFER records. */ |
| 90 | 		struct { |
| 91 | 			int buf_id; |
| 92 | 			int curpage; |
| 93 | 			vm_offset_t offset; |
| 94 | 		}; |
| 95 | 		/* Used for THREAD_* records. */ |
| 96 | 		int thread_id; |
| 97 | 	}; |
| 98 | } __aligned(16); |
| 99 | |
| 100 | struct hwt_record_get { |
| 101 | 	struct hwt_record_user_entry	*records; |
| 102 | 	int				*nentries; |
| 103 | 	int wait; |
| 104 | } __aligned(16); |
| 105 | |
| 106 | struct hwt_bufptr_get { |
| 107 | 	int		*ident; |
| 108 | 	vm_offset_t	*offset; |
| 109 | 	uint64_t	*data; |
| 110 | } __aligned(16); |
| 111 | |
| 112 | struct hwt_set_config { |
| 113 | 	/* Configuration of ctx. */ |
| 114 | 	int			pause_on_mmap; |
| 115 | |
| 116 | 	/* The following passed to backend as is. */ |
| 117 | 	void			*config; |
| 118 | 	size_t			config_size; |
| 119 | 	int			config_version; |
| 120 | } __aligned(16); |
| 121 | |
| 122 | struct hwt_svc_buf { |
| 123 | 	/* The following passed to backend as is. */ |
| 124 | 	void			*data; |
| 125 | 	size_t			data_size; |
| 126 | 	int			data_version; |
| 127 | } __aligned(16); |
| 128 | |
| 129 | #endif /* !_SYS_HWT_H_ */ |