xref: /freebsd/sys/cddl/dev/kinst/kinst.c (revision 744bfb213144c63cbaf38d91a1c4f7aebb9b9fbc)
1 /*
2  * SPDX-License-Identifier: CDDL 1.0
3  *
4  * Copyright 2022 Christos Margiolis <christos@FreeBSD.org>
5  */
6 
7 #include <sys/param.h>
8 #include <sys/systm.h>
9 #include <sys/conf.h>
10 #include <sys/kernel.h>
11 #include <sys/linker.h>
12 #include <sys/module.h>
13 
14 #include <sys/dtrace.h>
15 
16 #include "kinst.h"
17 
18 MALLOC_DEFINE(M_KINST, "kinst", "Kernel Instruction Tracing");
19 
20 static d_open_t		kinst_open;
21 static d_close_t	kinst_close;
22 static d_ioctl_t	kinst_ioctl;
23 
24 static void	kinst_provide_module(void *, modctl_t *);
25 static void	kinst_getargdesc(void *, dtrace_id_t, void *,
26 		    dtrace_argdesc_t *);
27 static void	kinst_destroy(void *, dtrace_id_t, void *);
28 static void	kinst_enable(void *, dtrace_id_t, void *);
29 static void	kinst_disable(void *, dtrace_id_t, void *);
30 static int	kinst_load(void *);
31 static int	kinst_unload(void *);
32 static int	kinst_modevent(module_t, int, void *);
33 
34 static dtrace_pattr_t kinst_attr = {
35 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
36 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
37 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA },
38 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
39 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_ISA },
40 };
41 
42 static dtrace_pops_t kinst_pops = {
43 	.dtps_provide		= NULL,
44 	.dtps_provide_module	= kinst_provide_module,
45 	.dtps_enable		= kinst_enable,
46 	.dtps_disable		= kinst_disable,
47 	.dtps_suspend		= NULL,
48 	.dtps_resume		= NULL,
49 	.dtps_getargdesc	= kinst_getargdesc,
50 	.dtps_getargval		= NULL,
51 	.dtps_usermode		= NULL,
52 	.dtps_destroy		= kinst_destroy
53 };
54 
55 static struct cdevsw kinst_cdevsw = {
56 	.d_name			= "kinst",
57 	.d_version		= D_VERSION,
58 	.d_flags		= D_TRACKCLOSE,
59 	.d_open			= kinst_open,
60 	.d_close		= kinst_close,
61 	.d_ioctl		= kinst_ioctl,
62 };
63 
64 static dtrace_provider_id_t	kinst_id;
65 struct kinst_probe_list	*kinst_probetab;
66 static struct cdev	*kinst_cdev;
67 
68 void
69 kinst_probe_create(struct kinst_probe *kp, linker_file_t lf)
70 {
71 	kp->kp_id = dtrace_probe_create(kinst_id, lf->filename,
72 	    kp->kp_func, kp->kp_name, 3, kp);
73 
74 	LIST_INSERT_HEAD(KINST_GETPROBE(kp->kp_patchpoint), kp, kp_hashnext);
75 }
76 
77 static int
78 kinst_open(struct cdev *dev __unused, int oflags __unused, int devtype __unused,
79     struct thread *td __unused)
80 {
81 	return (0);
82 }
83 
84 static int
85 kinst_close(struct cdev *dev __unused, int fflag __unused, int devtype __unused,
86     struct thread *td __unused)
87 {
88 	dtrace_condense(kinst_id);
89 	return (0);
90 }
91 
92 static int
93 kinst_linker_file_cb(linker_file_t lf, void *arg)
94 {
95 	dtrace_kinst_probedesc_t *pd;
96 
97 	pd = arg;
98 	if (pd->kpd_mod[0] != '\0' && strcmp(pd->kpd_mod, lf->filename) != 0)
99 		return (0);
100 
101 	/*
102 	 * Invoke kinst_make_probe_function() once for each function symbol in
103 	 * the module "lf".
104 	 */
105 	return (linker_file_function_listall(lf, kinst_make_probe, arg));
106 }
107 
108 static int
109 kinst_ioctl(struct cdev *dev __unused, u_long cmd, caddr_t addr,
110     int flags __unused, struct thread *td __unused)
111 {
112 	dtrace_kinst_probedesc_t *pd;
113 	int error = 0;
114 
115 	switch (cmd) {
116 	case KINSTIOC_MAKEPROBE:
117 		pd = (dtrace_kinst_probedesc_t *)addr;
118 		pd->kpd_func[sizeof(pd->kpd_func) - 1] = '\0';
119 		pd->kpd_mod[sizeof(pd->kpd_mod) - 1] = '\0';
120 
121 		/* Loop over all functions in the kernel and loaded modules. */
122 		error = linker_file_foreach(kinst_linker_file_cb, pd);
123 		break;
124 	default:
125 		error = ENOTTY;
126 		break;
127 	}
128 
129 	return (error);
130 }
131 
132 static void
133 kinst_provide_module(void *arg, modctl_t *lf)
134 {
135 }
136 
137 static void
138 kinst_getargdesc(void *arg, dtrace_id_t id, void *parg, dtrace_argdesc_t *desc)
139 {
140 	desc->dtargd_ndx = DTRACE_ARGNONE;
141 }
142 
143 static void
144 kinst_destroy(void *arg, dtrace_id_t id, void *parg)
145 {
146 	struct kinst_probe *kp = parg;
147 
148 	LIST_REMOVE(kp, kp_hashnext);
149 	free(kp, M_KINST);
150 }
151 
152 static void
153 kinst_enable(void *arg, dtrace_id_t id, void *parg)
154 {
155 	struct kinst_probe *kp = parg;
156 
157 	kinst_patch_tracepoint(kp, kp->kp_patchval);
158 }
159 
160 static void
161 kinst_disable(void *arg, dtrace_id_t id, void *parg)
162 {
163 	struct kinst_probe *kp = parg;
164 
165 	kinst_patch_tracepoint(kp, kp->kp_savedval);
166 }
167 
168 static int
169 kinst_load(void *dummy)
170 {
171 	int error;
172 
173 	error = kinst_trampoline_init();
174 	if (error != 0)
175 		return (error);
176 
177 	error = dtrace_register("kinst", &kinst_attr, DTRACE_PRIV_USER, NULL,
178 	    &kinst_pops, NULL, &kinst_id);
179 	if (error != 0) {
180 		kinst_trampoline_deinit();
181 		return (error);
182 	}
183 	kinst_probetab = malloc(KINST_PROBETAB_MAX *
184 	    sizeof(struct kinst_probe_list), M_KINST, M_WAITOK | M_ZERO);
185 	for (int i = 0; i < KINST_PROBETAB_MAX; i++)
186 		LIST_INIT(&kinst_probetab[i]);
187 	kinst_cdev = make_dev(&kinst_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600,
188 	    "dtrace/kinst");
189 	dtrace_invop_add(kinst_invop);
190 	return (0);
191 }
192 
193 static int
194 kinst_unload(void *dummy)
195 {
196 	free(kinst_probetab, M_KINST);
197 	kinst_trampoline_deinit();
198 	dtrace_invop_remove(kinst_invop);
199 	destroy_dev(kinst_cdev);
200 
201 	return (dtrace_unregister(kinst_id));
202 }
203 
204 static int
205 kinst_modevent(module_t mod __unused, int type, void *data __unused)
206 {
207 	int error = 0;
208 
209 	switch (type) {
210 	case MOD_LOAD:
211 		KINST_LOG(
212 		    "kinst: This provider is experimental, exercise caution");
213 		break;
214 	case MOD_UNLOAD:
215 		break;
216 	case MOD_SHUTDOWN:
217 		break;
218 	default:
219 		error = EOPNOTSUPP;
220 		break;
221 	}
222 
223 	return (error);
224 }
225 
226 SYSINIT(kinst_load, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, kinst_load, NULL);
227 SYSUNINIT(kinst_unload, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, kinst_unload,
228     NULL);
229 
230 DEV_MODULE(kinst, kinst_modevent, NULL);
231 MODULE_VERSION(kinst, 1);
232 MODULE_DEPEND(kinst, dtrace, 1, 1, 1);
233 MODULE_DEPEND(kinst, opensolaris, 1, 1, 1);
234