fopsmapper.c revision 1.2.4.2 1 1.2.4.2 martin /* $NetBSD: fopsmapper.c,v 1.2.4.2 2020/04/13 08:05:11 martin Exp $ */
2 1.2.4.2 martin
3 1.2.4.2 martin /*-
4 1.2.4.2 martin * Copyright (c) 2020 The NetBSD Foundation, Inc.
5 1.2.4.2 martin * All rights reserved.
6 1.2.4.2 martin *
7 1.2.4.2 martin * Redistribution and use in source and binary forms, with or without
8 1.2.4.2 martin * modification, are permitted provided that the following conditions
9 1.2.4.2 martin * are met:
10 1.2.4.2 martin * 1. Redistributions of source code must retain the above copyright
11 1.2.4.2 martin * notice, this list of conditions and the following disclaimer.
12 1.2.4.2 martin * 2. Redistributions in binary form must reproduce the above copyright
13 1.2.4.2 martin * notice, this list of conditions and the following disclaimer in the
14 1.2.4.2 martin * documentation and/or other materials provided with the distribution.
15 1.2.4.2 martin *
16 1.2.4.2 martin * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.2.4.2 martin * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.2.4.2 martin * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.2.4.2 martin * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.2.4.2 martin * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.2.4.2 martin * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.2.4.2 martin * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.2.4.2 martin * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.2.4.2 martin * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.2.4.2 martin * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.2.4.2 martin * POSSIBILITY OF SUCH DAMAGE.
27 1.2.4.2 martin */
28 1.2.4.2 martin
29 1.2.4.2 martin #include <sys/cdefs.h>
30 1.2.4.2 martin __KERNEL_RCSID(0, "$NetBSD: fopsmapper.c,v 1.2.4.2 2020/04/13 08:05:11 martin Exp $");
31 1.2.4.2 martin
32 1.2.4.2 martin #include <sys/module.h>
33 1.2.4.2 martin #include <sys/param.h>
34 1.2.4.2 martin #include <sys/systm.h>
35 1.2.4.2 martin #include <sys/kernel.h>
36 1.2.4.2 martin
37 1.2.4.2 martin #include <sys/conf.h>
38 1.2.4.2 martin #include <sys/file.h>
39 1.2.4.2 martin #include <sys/filedesc.h>
40 1.2.4.2 martin #include <sys/kmem.h>
41 1.2.4.2 martin #include <sys/mman.h>
42 1.2.4.2 martin #include <sys/mutex.h>
43 1.2.4.2 martin #include <uvm/uvm_extern.h>
44 1.2.4.2 martin
45 1.2.4.2 martin /*
46 1.2.4.2 martin * To use this module you need to:
47 1.2.4.2 martin *
48 1.2.4.2 martin * mknod /dev/fopsmapper c 351 0
49 1.2.4.2 martin *
50 1.2.4.2 martin */
51 1.2.4.2 martin
52 1.2.4.2 martin dev_type_open(fopsmapper_open);
53 1.2.4.2 martin
54 1.2.4.2 martin const struct cdevsw fopsmapper_cdevsw = {
55 1.2.4.2 martin .d_open = fopsmapper_open,
56 1.2.4.2 martin .d_close = noclose,
57 1.2.4.2 martin .d_read = noread,
58 1.2.4.2 martin .d_write = nowrite,
59 1.2.4.2 martin .d_ioctl = noioctl,
60 1.2.4.2 martin .d_stop = nostop,
61 1.2.4.2 martin .d_tty = notty,
62 1.2.4.2 martin .d_poll = nopoll,
63 1.2.4.2 martin .d_mmap = nommap,
64 1.2.4.2 martin .d_kqfilter = nokqfilter,
65 1.2.4.2 martin .d_discard = nodiscard,
66 1.2.4.2 martin .d_flag = D_OTHER
67 1.2.4.2 martin };
68 1.2.4.2 martin
69 1.2.4.2 martin static int fopsmapper_mmap(file_t *, off_t *, size_t,
70 1.2.4.2 martin int, int *, int *,struct uvm_object **, int *);
71 1.2.4.2 martin static int fopsmapper_close(file_t *);
72 1.2.4.2 martin
73 1.2.4.2 martin const struct fileops mapper_fileops = {
74 1.2.4.2 martin .fo_read = fbadop_read,
75 1.2.4.2 martin .fo_write = fbadop_write,
76 1.2.4.2 martin .fo_ioctl = fbadop_ioctl,
77 1.2.4.2 martin .fo_fcntl = fnullop_fcntl,
78 1.2.4.2 martin .fo_poll = fnullop_poll,
79 1.2.4.2 martin .fo_stat = fbadop_stat,
80 1.2.4.2 martin .fo_close = fopsmapper_close,
81 1.2.4.2 martin .fo_kqfilter = fnullop_kqfilter,
82 1.2.4.2 martin .fo_restart = fnullop_restart,
83 1.2.4.2 martin .fo_mmap = fopsmapper_mmap,
84 1.2.4.2 martin };
85 1.2.4.2 martin
86 1.2.4.2 martin typedef struct fopsmapper_softc {
87 1.2.4.2 martin char *buf;
88 1.2.4.2 martin struct uvm_object *uobj;
89 1.2.4.2 martin size_t bufsize;
90 1.2.4.2 martin } fops_t;
91 1.2.4.2 martin
92 1.2.4.2 martin int
93 1.2.4.2 martin fopsmapper_open(dev_t dev, int flag, int mode, struct lwp *l)
94 1.2.4.2 martin {
95 1.2.4.2 martin fops_t *fo;
96 1.2.4.2 martin struct file *fp;
97 1.2.4.2 martin int fd, error;
98 1.2.4.2 martin
99 1.2.4.2 martin if ((error = fd_allocfile(&fp, &fd)) != 0)
100 1.2.4.2 martin return error;
101 1.2.4.2 martin
102 1.2.4.2 martin fo = kmem_zalloc(sizeof(*fo), KM_SLEEP);
103 1.2.4.2 martin
104 1.2.4.2 martin return fd_clone(fp, fd, flag, &mapper_fileops, fo);
105 1.2.4.2 martin }
106 1.2.4.2 martin
107 1.2.4.2 martin int
108 1.2.4.2 martin fopsmapper_mmap(file_t * fp, off_t * offp, size_t size, int prot,
109 1.2.4.2 martin int *flagsp, int *advicep, struct uvm_object **uobjp,
110 1.2.4.2 martin int *maxprotp)
111 1.2.4.2 martin {
112 1.2.4.2 martin fops_t *fo;
113 1.2.4.2 martin int error;
114 1.2.4.2 martin
115 1.2.4.2 martin if (prot & PROT_EXEC)
116 1.2.4.2 martin return EACCES;
117 1.2.4.2 martin
118 1.2.4.2 martin if (size != (size_t)PAGE_SIZE)
119 1.2.4.2 martin return EINVAL;
120 1.2.4.2 martin
121 1.2.4.2 martin if ((fo = fp->f_data) == NULL)
122 1.2.4.2 martin return ENXIO;
123 1.2.4.2 martin
124 1.2.4.2 martin fo->bufsize = size;
125 1.2.4.2 martin fo->uobj = uao_create(size, 0);
126 1.2.4.2 martin
127 1.2.4.2 martin fo->buf = NULL;
128 1.2.4.2 martin /* Map the uvm object into kernel */
129 1.2.4.2 martin error = uvm_map(kernel_map, (vaddr_t *) &fo->buf, fo->bufsize,
130 1.2.4.2 martin fo->uobj, 0, 0,
131 1.2.4.2 martin UVM_MAPFLAG(UVM_PROT_RW, UVM_PROT_RW,
132 1.2.4.2 martin UVM_INH_SHARE,UVM_ADV_RANDOM, 0));
133 1.2.4.2 martin
134 1.2.4.2 martin if (error) {
135 1.2.4.2 martin uao_detach(fo->uobj);
136 1.2.4.2 martin return error;
137 1.2.4.2 martin }
138 1.2.4.2 martin snprintf(fo->buf, 13, "Hey There!");
139 1.2.4.2 martin
140 1.2.4.2 martin /* Get the reference of uobj */
141 1.2.4.2 martin uao_reference(fo->uobj);
142 1.2.4.2 martin
143 1.2.4.2 martin *uobjp = fo->uobj;
144 1.2.4.2 martin *maxprotp = prot;
145 1.2.4.2 martin *advicep = UVM_ADV_RANDOM;
146 1.2.4.2 martin
147 1.2.4.2 martin return 0;
148 1.2.4.2 martin }
149 1.2.4.2 martin
150 1.2.4.2 martin int
151 1.2.4.2 martin fopsmapper_close(file_t * fp)
152 1.2.4.2 martin {
153 1.2.4.2 martin fops_t *fo;
154 1.2.4.2 martin
155 1.2.4.2 martin fo = fp->f_data;
156 1.2.4.2 martin KASSERT(fo != NULL);
157 1.2.4.2 martin
158 1.2.4.2 martin if (fo->buf != NULL)
159 1.2.4.2 martin uvm_deallocate(kernel_map, (vaddr_t)fo->buf, fo->bufsize);
160 1.2.4.2 martin
161 1.2.4.2 martin kmem_free(fo, sizeof(*fo));
162 1.2.4.2 martin
163 1.2.4.2 martin return 0;
164 1.2.4.2 martin }
165 1.2.4.2 martin
166 1.2.4.2 martin MODULE(MODULE_CLASS_MISC, fopsmapper, NULL);
167 1.2.4.2 martin
168 1.2.4.2 martin static int
169 1.2.4.2 martin fopsmapper_modcmd(modcmd_t cmd, void *arg __unused)
170 1.2.4.2 martin {
171 1.2.4.2 martin int cmajor = 351, bmajor = -1;
172 1.2.4.2 martin
173 1.2.4.2 martin switch (cmd) {
174 1.2.4.2 martin case MODULE_CMD_INIT:
175 1.2.4.2 martin if (devsw_attach("fopsmapper", NULL, &bmajor,
176 1.2.4.2 martin &fopsmapper_cdevsw, &cmajor))
177 1.2.4.2 martin return ENXIO;
178 1.2.4.2 martin return 0;
179 1.2.4.2 martin case MODULE_CMD_FINI:
180 1.2.4.2 martin return EBUSY;
181 1.2.4.2 martin default:
182 1.2.4.2 martin return ENOTTY;
183 1.2.4.2 martin }
184 1.2.4.2 martin }
185