xref: /freebsd/sys/compat/lindebugfs/lindebugfs.c (revision 1dfcff294e44d4b45813288ef4095c36abb22f0e)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2016-2018, Matthew Macy <mmacy@freebsd.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/queue.h>
35 #include <sys/blist.h>
36 #include <sys/conf.h>
37 #include <sys/exec.h>
38 #include <sys/filedesc.h>
39 #include <sys/kernel.h>
40 #include <sys/linker.h>
41 #include <sys/malloc.h>
42 #include <sys/mount.h>
43 #include <sys/mutex.h>
44 #include <sys/proc.h>
45 #include <sys/resourcevar.h>
46 #include <sys/sbuf.h>
47 #include <sys/smp.h>
48 #include <sys/socket.h>
49 #include <sys/vnode.h>
50 #include <sys/bus.h>
51 #include <sys/pciio.h>
52 
53 #include <dev/pci/pcivar.h>
54 #include <dev/pci/pcireg.h>
55 
56 #include <net/if.h>
57 
58 #include <vm/vm.h>
59 #include <vm/pmap.h>
60 #include <vm/vm_map.h>
61 #include <vm/vm_param.h>
62 #include <vm/vm_object.h>
63 #include <vm/swap_pager.h>
64 
65 #include <machine/bus.h>
66 
67 #include <compat/linux/linux_ioctl.h>
68 #include <compat/linux/linux_mib.h>
69 #include <compat/linux/linux_util.h>
70 #include <fs/pseudofs/pseudofs.h>
71 
72 #include <linux/debugfs.h>
73 #include <linux/seq_file.h>
74 #include <linux/compat.h>
75 
76 MALLOC_DEFINE(M_DFSINT, "debugfsint", "Linux debugfs internal");
77 
78 static struct pfs_node *debugfs_root;
79 
80 #define DM_SYMLINK 0x1
81 #define DM_DIR 0x2
82 #define DM_FILE 0x3
83 
84 struct dentry_meta {
85 	struct dentry dm_dnode;
86 	const struct file_operations *dm_fops;
87 	void *dm_data;
88 	umode_t dm_mode;
89 	int dm_type;
90 };
91 
92 static int
93 debugfs_attr(PFS_ATTR_ARGS)
94 {
95 	struct dentry_meta *dm;
96 
97 	dm = pn->pn_data;
98 
99 	vap->va_mode = dm->dm_mode;
100 	return (0);
101 }
102 
103 static int
104 debugfs_destroy(PFS_DESTROY_ARGS)
105 {
106 	struct dentry_meta *dm;
107 
108 	dm = pn->pn_data;
109 	if (dm->dm_type == DM_SYMLINK)
110 		free(dm->dm_data, M_DFSINT);
111 
112 	free(dm, M_DFSINT);
113 	return (0);
114 }
115 
116 static int
117 debugfs_fill(PFS_FILL_ARGS)
118 {
119 	struct dentry_meta *d;
120 	struct linux_file lf = {};
121 	struct seq_file *sf;
122 	struct vnode vn;
123 	void *buf;
124 	int rc;
125 	size_t len;
126 	off_t off;
127 
128 	d = pn->pn_data;
129 
130 	if ((rc = linux_set_current_flags(curthread, M_NOWAIT)))
131 		return (rc);
132 	vn.v_data = d->dm_data;
133 	if (uio->uio_rw == UIO_READ) {
134 		buf = uio->uio_iov[0].iov_base;
135 		len = min(uio->uio_iov[0].iov_len, uio->uio_resid);
136 	} else {
137 		sbuf_finish(sb);
138 		buf = sbuf_data(sb);
139 		len = sbuf_len(sb);
140 	}
141 	off = 0;
142 	rc = d->dm_fops->open(&vn, &lf);
143 	if (rc < 0) {
144 #ifdef INVARIANTS
145 		printf("%s:%d open failed with %d\n", __FUNCTION__, __LINE__, rc);
146 #endif
147 		return (-rc);
148 	}
149 	sf = lf.private_data;
150 	sf->buf = sb;
151 	if (uio->uio_rw == UIO_READ) {
152 		if (d->dm_fops->read)
153 			rc = d->dm_fops->read(&lf, NULL, len, &off);
154 		else
155 			rc = -ENODEV;
156 	} else {
157 		if (d->dm_fops->write)
158 			rc = d->dm_fops->write(&lf, buf, len, &off);
159 		else
160 			rc = -ENODEV;
161 	}
162 	if (d->dm_fops->release)
163 		d->dm_fops->release(&vn, &lf);
164 	else
165 		single_release(&vn, &lf);
166 
167 	if (rc < 0) {
168 #ifdef INVARIANTS
169 		printf("%s:%d read/write failed with %d\n", __FUNCTION__, __LINE__, rc);
170 #endif
171 		return (-rc);
172 	}
173 	return (0);
174 }
175 
176 static int
177 debugfs_fill_data(PFS_FILL_ARGS)
178 {
179 	struct dentry_meta *dm;
180 
181 	dm = pn->pn_data;
182 	sbuf_printf(sb, "%s", (char *)dm->dm_data);
183 	return (0);
184 }
185 
186 struct dentry *
187 debugfs_create_file(const char *name, umode_t mode,
188 		    struct dentry *parent, void *data,
189 		    const struct file_operations *fops)
190 {
191 	struct dentry_meta *dm;
192 	struct dentry *dnode;
193 	struct pfs_node *pnode;
194 	int flags;
195 
196 	dm = malloc(sizeof(*dm), M_DFSINT, M_NOWAIT | M_ZERO);
197 	if (dm == NULL)
198 		return (NULL);
199 	dnode = &dm->dm_dnode;
200 	dm->dm_fops = fops;
201 	dm->dm_data = data;
202 	dm->dm_mode = mode;
203 	dm->dm_type = DM_FILE;
204 	if (parent != NULL)
205 		pnode = parent->d_pfs_node;
206 	else
207 		pnode = debugfs_root;
208 
209 	flags = fops->write ? PFS_RDWR : PFS_RD;
210 	dnode->d_pfs_node = pfs_create_file(pnode, name, debugfs_fill,
211 	    debugfs_attr, NULL, debugfs_destroy, flags | PFS_NOWAIT);
212 	if (dnode->d_pfs_node == NULL) {
213 		free(dm, M_DFSINT);
214 		return (NULL);
215 	}
216 	dnode->d_pfs_node->pn_data = dm;
217 
218 	return (dnode);
219 }
220 
221 struct dentry *
222 debugfs_create_dir(const char *name, struct dentry *parent)
223 {
224 	struct dentry_meta *dm;
225 	struct dentry *dnode;
226 	struct pfs_node *pnode;
227 
228 	dm = malloc(sizeof(*dm), M_DFSINT, M_NOWAIT | M_ZERO);
229 	if (dm == NULL)
230 		return (NULL);
231 	dnode = &dm->dm_dnode;
232 	dm->dm_mode = 0700;
233 	dm->dm_type = DM_DIR;
234 	if (parent != NULL)
235 		pnode = parent->d_pfs_node;
236 	else
237 		pnode = debugfs_root;
238 
239 	dnode->d_pfs_node = pfs_create_dir(pnode, name, debugfs_attr, NULL, debugfs_destroy, PFS_RD | PFS_NOWAIT);
240 	if (dnode->d_pfs_node == NULL) {
241 		free(dm, M_DFSINT);
242 		return (NULL);
243 	}
244 	dnode->d_pfs_node->pn_data = dm;
245 	return (dnode);
246 }
247 
248 struct dentry *
249 debugfs_create_symlink(const char *name, struct dentry *parent,
250 	const char *dest)
251 {
252 	struct dentry_meta *dm;
253 	struct dentry *dnode;
254 	struct pfs_node *pnode;
255 	void *data;
256 
257 	data = strdup_flags(dest, M_DFSINT, M_NOWAIT);
258 	if (data == NULL)
259 		return (NULL);
260 	dm = malloc(sizeof(*dm), M_DFSINT, M_NOWAIT | M_ZERO);
261 	if (dm == NULL)
262 		goto fail1;
263 	dnode = &dm->dm_dnode;
264 	dm->dm_mode = 0700;
265 	dm->dm_type = DM_SYMLINK;
266 	dm->dm_data = data;
267 	if (parent != NULL)
268 		pnode = parent->d_pfs_node;
269 	else
270 		pnode = debugfs_root;
271 
272 	dnode->d_pfs_node = pfs_create_link(pnode, name, &debugfs_fill_data, NULL, NULL, NULL, PFS_NOWAIT);
273 	if (dnode->d_pfs_node == NULL)
274 		goto fail;
275 	dnode->d_pfs_node->pn_data = dm;
276 	return (dnode);
277  fail:
278 	free(dm, M_DFSINT);
279  fail1:
280 	free(data, M_DFSINT);
281 	return (NULL);
282 }
283 
284 void
285 debugfs_remove(struct dentry *dnode)
286 {
287 	if (dnode == NULL)
288 		return;
289 
290 	pfs_destroy(dnode->d_pfs_node);
291 }
292 
293 void
294 debugfs_remove_recursive(struct dentry *dnode)
295 {
296 	if (dnode == NULL)
297 		return;
298 
299 	pfs_destroy(dnode->d_pfs_node);
300 }
301 
302 static int
303 debugfs_init(PFS_INIT_ARGS)
304 {
305 
306 	debugfs_root = pi->pi_root;
307 
308 	(void)debugfs_create_symlink("kcov", NULL, "/dev/kcov");
309 
310 	return (0);
311 }
312 
313 static int
314 debugfs_uninit(PFS_INIT_ARGS)
315 {
316 	return (0);
317 }
318 
319 #ifdef PR_ALLOW_MOUNT_LINSYSFS
320 PSEUDOFS(debugfs, 1, PR_ALLOW_MOUNT_LINSYSFS);
321 #else
322 PSEUDOFS(debugfs, 1, VFCF_JAIL);
323 #endif
324 MODULE_DEPEND(lindebugfs, linuxkpi, 1, 1, 1);
325