1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1988 University of Utah.
5 * Copyright (c) 1982, 1986, 1990 The Regents of the University of California.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * the Systems Programming Group of the University of Utah Computer
10 * Science Department, and code derived from software contributed to
11 * Berkeley by William Jolitz.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 *
37 * from: Utah $Hdr: mem.c 1.13 89/10/08$
38 */
39
40 #include <sys/cdefs.h>
41 /*
42 * Memory special file
43 */
44
45 #include <sys/param.h>
46 #include <sys/conf.h>
47 #include <sys/fcntl.h>
48 #include <sys/ioccom.h>
49 #include <sys/kernel.h>
50 #include <sys/lock.h>
51 #include <sys/malloc.h>
52 #include <sys/memrange.h>
53 #include <sys/module.h>
54 #include <sys/mutex.h>
55 #include <sys/sx.h>
56 #include <sys/proc.h>
57 #include <sys/signalvar.h>
58 #include <sys/systm.h>
59 #include <sys/uio.h>
60
61 #include <machine/specialreg.h>
62
63 #include <vm/vm.h>
64 #include <vm/pmap.h>
65 #include <vm/vm_extern.h>
66
67 #include <machine/memdev.h>
68
69 /*
70 * Used in /dev/mem drivers and elsewhere
71 */
72 MALLOC_DEFINE(M_MEMDESC, "memdesc", "memory range descriptors");
73
74 static struct sx memsxlock;
75 SX_SYSINIT(memsxlockinit, &memsxlock, "/dev/mem lock");
76
77 /* ARGSUSED */
78 int
memrw(struct cdev * dev,struct uio * uio,int flags)79 memrw(struct cdev *dev, struct uio *uio, int flags)
80 {
81 int o;
82 u_int c = 0;
83 vm_paddr_t pa;
84 struct iovec *iov;
85 int error = 0;
86 vm_offset_t addr;
87
88 if (dev2unit(dev) != CDEV_MINOR_MEM && dev2unit(dev) != CDEV_MINOR_KMEM)
89 return EIO;
90
91 if (dev2unit(dev) == CDEV_MINOR_KMEM && uio->uio_resid > 0) {
92 if (!kernacc((caddr_t)(int)uio->uio_offset, uio->uio_resid,
93 uio->uio_rw == UIO_READ ? VM_PROT_READ : VM_PROT_WRITE))
94 return (EFAULT);
95 }
96
97 while (uio->uio_resid > 0 && error == 0) {
98 iov = uio->uio_iov;
99 if (iov->iov_len == 0) {
100 uio->uio_iov++;
101 uio->uio_iovcnt--;
102 if (uio->uio_iovcnt < 0)
103 panic("memrw");
104 continue;
105 }
106 if (dev2unit(dev) == CDEV_MINOR_MEM) {
107 if (uio->uio_offset > cpu_getmaxphyaddr()) {
108 error = EFAULT;
109 break;
110 }
111 pa = trunc_page(uio->uio_offset);
112 } else {
113 /*
114 * Extract the physical page since the mapping may
115 * change at any time. This avoids panics on page
116 * fault in this case but will cause reading/writing
117 * to the wrong page.
118 * Hopefully an application will notice the wrong
119 * data on read access and refrain from writing.
120 * This should be replaced by a special uiomove
121 * type function that just returns an error if there
122 * is a page fault on a kernel page.
123 */
124 addr = trunc_page(uio->uio_offset);
125 pa = pmap_extract(kernel_pmap, addr);
126 if (pa == 0)
127 return EFAULT;
128 }
129
130 /*
131 * XXX UPS This should just use sf_buf_alloc.
132 * Unfortunately sf_buf_alloc needs a vm_page
133 * and we may want to look at memory not covered
134 * by the page array.
135 */
136
137 sx_xlock(&memsxlock);
138 pmap_kenter((vm_offset_t)ptvmmap, pa);
139 pmap_invalidate_page(kernel_pmap,(vm_offset_t)ptvmmap);
140
141 o = (int)uio->uio_offset & PAGE_MASK;
142 c = PAGE_SIZE - o;
143 c = min(c, (u_int)iov->iov_len);
144 error = uiomove((caddr_t)&ptvmmap[o], (int)c, uio);
145 pmap_qremove((vm_offset_t)ptvmmap, 1);
146 sx_xunlock(&memsxlock);
147 }
148
149 return (error);
150 }
151
152 /*
153 * allow user processes to MMAP some memory sections
154 * instead of going through read/write
155 */
156 /* ARGSUSED */
157 int
memmmap(struct cdev * dev,vm_ooffset_t offset,vm_paddr_t * paddr,int prot __unused,vm_memattr_t * memattr __unused)158 memmmap(struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr,
159 int prot __unused, vm_memattr_t *memattr __unused)
160 {
161 if (dev2unit(dev) == CDEV_MINOR_MEM) {
162 if (offset > cpu_getmaxphyaddr())
163 return (-1);
164 *paddr = offset;
165 return (0);
166 }
167 return (-1);
168 }
169
170 /*
171 * Operations for changing memory attributes.
172 *
173 * This is basically just an ioctl shim for mem_range_attr_get
174 * and mem_range_attr_set.
175 */
176 int
memioctl_md(struct cdev * dev __unused,u_long cmd,caddr_t data,int flags,struct thread * td)177 memioctl_md(struct cdev *dev __unused, u_long cmd, caddr_t data, int flags,
178 struct thread *td)
179 {
180 int nd, error = 0;
181 struct mem_range_op *mo = (struct mem_range_op *)data;
182 struct mem_range_desc *md;
183
184 /* is this for us? */
185 if ((cmd != MEMRANGE_GET) &&
186 (cmd != MEMRANGE_SET))
187 return (ENOTTY);
188
189 /* any chance we can handle this? */
190 if (mem_range_softc.mr_op == NULL)
191 return (EOPNOTSUPP);
192
193 /* do we have any descriptors? */
194 if (mem_range_softc.mr_ndesc == 0)
195 return (ENXIO);
196
197 switch (cmd) {
198 case MEMRANGE_GET:
199 nd = imin(mo->mo_arg[0], mem_range_softc.mr_ndesc);
200 if (nd > 0) {
201 md = (struct mem_range_desc *)
202 malloc(nd * sizeof(struct mem_range_desc),
203 M_MEMDESC, M_WAITOK);
204 error = mem_range_attr_get(md, &nd);
205 if (!error)
206 error = copyout(md, mo->mo_desc,
207 nd * sizeof(struct mem_range_desc));
208 free(md, M_MEMDESC);
209 }
210 else
211 nd = mem_range_softc.mr_ndesc;
212 mo->mo_arg[0] = nd;
213 break;
214
215 case MEMRANGE_SET:
216 md = (struct mem_range_desc *)malloc(sizeof(struct mem_range_desc),
217 M_MEMDESC, M_WAITOK);
218 error = copyin(mo->mo_desc, md, sizeof(struct mem_range_desc));
219 /* clamp description string */
220 md->mr_owner[sizeof(md->mr_owner) - 1] = 0;
221 if (error == 0)
222 error = mem_range_attr_set(md, &mo->mo_arg[0]);
223 free(md, M_MEMDESC);
224 break;
225 }
226 return (error);
227 }
228