xref: /freebsd-14.2/sys/i386/i386/mem.c (revision 685dc743)
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  *	from: @(#)mem.c	7.2 (Berkeley) 5/9/91
39  */
40 
41 #include <sys/cdefs.h>
42 /*
43  * Memory special file
44  */
45 
46 #include <sys/param.h>
47 #include <sys/conf.h>
48 #include <sys/fcntl.h>
49 #include <sys/ioccom.h>
50 #include <sys/kernel.h>
51 #include <sys/lock.h>
52 #include <sys/malloc.h>
53 #include <sys/memrange.h>
54 #include <sys/module.h>
55 #include <sys/mutex.h>
56 #include <sys/sx.h>
57 #include <sys/proc.h>
58 #include <sys/signalvar.h>
59 #include <sys/systm.h>
60 #include <sys/uio.h>
61 
62 #include <machine/specialreg.h>
63 
64 #include <vm/vm.h>
65 #include <vm/pmap.h>
66 #include <vm/vm_extern.h>
67 
68 #include <machine/memdev.h>
69 
70 /*
71  * Used in /dev/mem drivers and elsewhere
72  */
73 MALLOC_DEFINE(M_MEMDESC, "memdesc", "memory range descriptors");
74 
75 static struct sx memsxlock;
76 SX_SYSINIT(memsxlockinit, &memsxlock, "/dev/mem lock");
77 
78 /* ARGSUSED */
79 int
memrw(struct cdev * dev,struct uio * uio,int flags)80 memrw(struct cdev *dev, struct uio *uio, int flags)
81 {
82 	int o;
83 	u_int c = 0;
84 	vm_paddr_t pa;
85 	struct iovec *iov;
86 	int error = 0;
87 	vm_offset_t addr;
88 
89 	if (dev2unit(dev) != CDEV_MINOR_MEM && dev2unit(dev) != CDEV_MINOR_KMEM)
90 		return EIO;
91 
92 	if (dev2unit(dev) == CDEV_MINOR_KMEM && uio->uio_resid > 0) {
93 		if (!kernacc((caddr_t)(int)uio->uio_offset, uio->uio_resid,
94 		    uio->uio_rw == UIO_READ ?  VM_PROT_READ : VM_PROT_WRITE))
95 			return (EFAULT);
96 	}
97 
98 	while (uio->uio_resid > 0 && error == 0) {
99 		iov = uio->uio_iov;
100 		if (iov->iov_len == 0) {
101 			uio->uio_iov++;
102 			uio->uio_iovcnt--;
103 			if (uio->uio_iovcnt < 0)
104 				panic("memrw");
105 			continue;
106 		}
107 		if (dev2unit(dev) == CDEV_MINOR_MEM) {
108 			if (uio->uio_offset > cpu_getmaxphyaddr()) {
109 				error = EFAULT;
110 				break;
111 			}
112 			pa = trunc_page(uio->uio_offset);
113 		} else {
114 			/*
115 			 * Extract the physical page since the mapping may
116 			 * change at any time. This avoids panics on page
117 			 * fault in this case but will cause reading/writing
118 			 * to the wrong page.
119 			 * Hopefully an application will notice the wrong
120 			 * data on read access and refrain from writing.
121 			 * This should be replaced by a special uiomove
122 			 * type function that just returns an error if there
123 			 * is a page fault on a kernel page.
124 			 */
125 			addr = trunc_page(uio->uio_offset);
126 			pa = pmap_extract(kernel_pmap, addr);
127 			if (pa == 0)
128 				return EFAULT;
129 		}
130 
131 		/*
132 		 * XXX UPS This should just use sf_buf_alloc.
133 		 * Unfortunately sf_buf_alloc needs a vm_page
134 		 * and we may want to look at memory not covered
135 		 * by the page array.
136 		 */
137 
138 		sx_xlock(&memsxlock);
139 		pmap_kenter((vm_offset_t)ptvmmap, pa);
140 		pmap_invalidate_page(kernel_pmap,(vm_offset_t)ptvmmap);
141 
142 		o = (int)uio->uio_offset & PAGE_MASK;
143 		c = PAGE_SIZE - o;
144 		c = min(c, (u_int)iov->iov_len);
145 		error = uiomove((caddr_t)&ptvmmap[o], (int)c, uio);
146 		pmap_qremove((vm_offset_t)ptvmmap, 1);
147 		sx_xunlock(&memsxlock);
148 	}
149 
150 	return (error);
151 }
152 
153 /*
154  * allow user processes to MMAP some memory sections
155  * instead of going through read/write
156  */
157 /* ARGSUSED */
158 int
memmmap(struct cdev * dev,vm_ooffset_t offset,vm_paddr_t * paddr,int prot __unused,vm_memattr_t * memattr __unused)159 memmmap(struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr,
160     int prot __unused, vm_memattr_t *memattr __unused)
161 {
162 	if (dev2unit(dev) == CDEV_MINOR_MEM) {
163 		if (offset > cpu_getmaxphyaddr())
164 			return (-1);
165 		*paddr = offset;
166 		return (0);
167 	}
168 	return (-1);
169 }
170 
171 /*
172  * Operations for changing memory attributes.
173  *
174  * This is basically just an ioctl shim for mem_range_attr_get
175  * and mem_range_attr_set.
176  */
177 int
memioctl_md(struct cdev * dev __unused,u_long cmd,caddr_t data,int flags,struct thread * td)178 memioctl_md(struct cdev *dev __unused, u_long cmd, caddr_t data, int flags,
179     struct thread *td)
180 {
181 	int nd, error = 0;
182 	struct mem_range_op *mo = (struct mem_range_op *)data;
183 	struct mem_range_desc *md;
184 
185 	/* is this for us? */
186 	if ((cmd != MEMRANGE_GET) &&
187 	    (cmd != MEMRANGE_SET))
188 		return (ENOTTY);
189 
190 	/* any chance we can handle this? */
191 	if (mem_range_softc.mr_op == NULL)
192 		return (EOPNOTSUPP);
193 
194 	/* do we have any descriptors? */
195 	if (mem_range_softc.mr_ndesc == 0)
196 		return (ENXIO);
197 
198 	switch (cmd) {
199 	case MEMRANGE_GET:
200 		nd = imin(mo->mo_arg[0], mem_range_softc.mr_ndesc);
201 		if (nd > 0) {
202 			md = (struct mem_range_desc *)
203 				malloc(nd * sizeof(struct mem_range_desc),
204 				       M_MEMDESC, M_WAITOK);
205 			error = mem_range_attr_get(md, &nd);
206 			if (!error)
207 				error = copyout(md, mo->mo_desc,
208 					nd * sizeof(struct mem_range_desc));
209 			free(md, M_MEMDESC);
210 		}
211 		else
212 			nd = mem_range_softc.mr_ndesc;
213 		mo->mo_arg[0] = nd;
214 		break;
215 
216 	case MEMRANGE_SET:
217 		md = (struct mem_range_desc *)malloc(sizeof(struct mem_range_desc),
218 						    M_MEMDESC, M_WAITOK);
219 		error = copyin(mo->mo_desc, md, sizeof(struct mem_range_desc));
220 		/* clamp description string */
221 		md->mr_owner[sizeof(md->mr_owner) - 1] = 0;
222 		if (error == 0)
223 			error = mem_range_attr_set(md, &mo->mo_arg[0]);
224 		free(md, M_MEMDESC);
225 		break;
226 	}
227 	return (error);
228 }
229