xref: /freebsd/sys/amd64/amd64/mem.c (revision 7660b554bc59a07be0431c17e0e33815818baa69)
1 /*-
2  * Copyright (c) 1988 University of Utah.
3  * Copyright (c) 1982, 1986, 1990 The Regents of the University of California.
4  * All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * the Systems Programming Group of the University of Utah Computer
8  * Science Department, and code derived from software contributed to
9  * Berkeley by William Jolitz.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the University of
22  *	California, Berkeley and its contributors.
23  * 4. Neither the name of the University nor the names of its contributors
24  *    may be used to endorse or promote products derived from this software
25  *    without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37  * SUCH DAMAGE.
38  *
39  *	Utah $Hdr: mem.c 1.13 89/10/08$
40  *	@(#)mem.c	7.2 (Berkeley) 5/9/91
41  */
42 
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
45 
46 /*
47  * Memory special file
48  */
49 
50 #include <sys/param.h>
51 #include <sys/conf.h>
52 #include <sys/fcntl.h>
53 #include <sys/ioccom.h>
54 #include <sys/kernel.h>
55 #include <sys/lock.h>
56 #include <sys/malloc.h>
57 #include <sys/memrange.h>
58 #include <sys/mutex.h>
59 #include <sys/proc.h>
60 #include <sys/signalvar.h>
61 #include <sys/systm.h>
62 #include <sys/uio.h>
63 
64 #include <machine/db_machdep.h>
65 #include <machine/frame.h>
66 #include <machine/psl.h>
67 #include <machine/specialreg.h>
68 #include <machine/vmparam.h>
69 
70 #include <vm/vm.h>
71 #include <vm/pmap.h>
72 #include <vm/vm_extern.h>
73 
74 static dev_t memdev, kmemdev, iodev;
75 
76 static	d_open_t	mmopen;
77 static	d_close_t	mmclose;
78 static	d_read_t	mmrw;
79 static	d_ioctl_t	mmioctl;
80 static	d_mmap_t	memmmap;
81 
82 #define CDEV_MAJOR 2
83 static struct cdevsw mem_cdevsw = {
84 	.d_open =	mmopen,
85 	.d_close =	mmclose,
86 	.d_read =	mmrw,
87 	.d_write =	mmrw,
88 	.d_ioctl =	mmioctl,
89 	.d_mmap =	memmmap,
90 	.d_name =	"mem",
91 	.d_maj =	CDEV_MAJOR,
92 	.d_flags =	D_MEM,
93 };
94 
95 MALLOC_DEFINE(M_MEMDESC, "memdesc", "memory range descriptors");
96 
97 struct mem_range_softc mem_range_softc;
98 
99 static int
100 mmclose(dev_t dev, int flags, int fmt, struct thread *td)
101 {
102 	switch (minor(dev)) {
103 	case 14:
104 		td->td_frame->tf_rflags &= ~PSL_IOPL;
105 	}
106 	return (0);
107 }
108 
109 static int
110 mmopen(dev_t dev, int flags, int fmt, struct thread *td)
111 {
112 	int error;
113 
114 	switch (minor(dev)) {
115 	case 0:
116 	case 1:
117 		if (flags & FWRITE) {
118 			error = securelevel_gt(td->td_ucred, 0);
119 			if (error != 0)
120 				return (error);
121 		}
122 		break;
123 	case 14:
124 		error = suser(td);
125 		if (error != 0)
126 			return (error);
127 		error = securelevel_gt(td->td_ucred, 0);
128 		if (error != 0)
129 			return (error);
130 		td->td_frame->tf_rflags |= PSL_IOPL;
131 		break;
132 	}
133 	return (0);
134 }
135 
136 /*ARGSUSED*/
137 static int
138 mmrw(dev_t dev, struct uio *uio, int flags)
139 {
140 	int o;
141 	u_long c = 0, v;
142 	struct iovec *iov;
143 	int error = 0;
144 	vm_offset_t addr, eaddr;
145 
146 	GIANT_REQUIRED;
147 
148 	while (uio->uio_resid > 0 && error == 0) {
149 		iov = uio->uio_iov;
150 		if (iov->iov_len == 0) {
151 			uio->uio_iov++;
152 			uio->uio_iovcnt--;
153 			if (uio->uio_iovcnt < 0)
154 				panic("mmrw");
155 			continue;
156 		}
157 		switch (minor(dev)) {
158 
159 /* minor device 0 is physical memory */
160 		case 0:
161 			v = uio->uio_offset;
162 kmemphys:
163 			o = v & PAGE_MASK;
164 			c = min(uio->uio_resid, (u_int)(PAGE_SIZE - o));
165 			error = uiomove((void *)PHYS_TO_DMAP(v), (int)c, uio);
166 			continue;
167 
168 /* minor device 1 is kernel memory */
169 		case 1:
170 			v = uio->uio_offset;
171 
172 			if (v >= DMAP_MIN_ADDRESS && v < DMAP_MAX_ADDRESS) {
173 				v = DMAP_TO_PHYS(v);
174 				goto kmemphys;
175 			}
176 
177 			c = iov->iov_len;
178 			/*
179 			 * Make sure that all of the pages are currently resident so
180 			 * that we don't create any zero-fill pages.
181 			 */
182 			addr = trunc_page(v);
183 			eaddr = round_page(v + c);
184 
185 			if (addr < (vm_offset_t)KERNBASE)
186 				return (EFAULT);
187 			for (; addr < eaddr; addr += PAGE_SIZE)
188 				if (pmap_extract(kernel_pmap, addr) == 0)
189 					return (EFAULT);
190 
191 			if (!kernacc((caddr_t)(long)v, c,
192 			    uio->uio_rw == UIO_READ ?
193 			    VM_PROT_READ : VM_PROT_WRITE))
194 				return (EFAULT);
195 
196 			error = uiomove((caddr_t)(long)v, (int)c, uio);
197 			continue;
198 
199 		default:
200 			return (ENODEV);
201 		}
202 
203 		if (error)
204 			break;
205 		iov->iov_base = (char *)iov->iov_base + c;
206 		iov->iov_len -= c;
207 		uio->uio_offset += c;
208 		uio->uio_resid -= c;
209 	}
210 	return (error);
211 }
212 
213 /*******************************************************\
214 * allow user processes to MMAP some memory sections	*
215 * instead of going through read/write			*
216 \*******************************************************/
217 static int
218 memmmap(dev_t dev, vm_offset_t offset, vm_paddr_t *paddr, int prot)
219 {
220 	switch (minor(dev))
221 	{
222 
223 	/* minor device 0 is physical memory */
224 	case 0:
225 		*paddr = offset;
226 		break;
227 
228 	/* minor device 1 is kernel memory */
229 	case 1:
230         	*paddr = vtophys(offset);
231 		break;
232 
233 	default:
234 		return (-1);
235 	}
236 	return (0);
237 }
238 
239 /*
240  * Operations for changing memory attributes.
241  *
242  * This is basically just an ioctl shim for mem_range_attr_get
243  * and mem_range_attr_set.
244  */
245 static int
246 mmioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct thread *td)
247 {
248 	int nd, error = 0;
249 	struct mem_range_op *mo = (struct mem_range_op *)data;
250 	struct mem_range_desc *md;
251 
252 	/* is this for us? */
253 	if ((cmd != MEMRANGE_GET) &&
254 	    (cmd != MEMRANGE_SET))
255 		return (ENOTTY);
256 
257 	/* any chance we can handle this? */
258 	if (mem_range_softc.mr_op == NULL)
259 		return (EOPNOTSUPP);
260 
261 	/* do we have any descriptors? */
262 	if (mem_range_softc.mr_ndesc == 0)
263 		return (ENXIO);
264 
265 	switch (cmd) {
266 	case MEMRANGE_GET:
267 		nd = imin(mo->mo_arg[0], mem_range_softc.mr_ndesc);
268 		if (nd > 0) {
269 			md = (struct mem_range_desc *)
270 				malloc(nd * sizeof(struct mem_range_desc),
271 				       M_MEMDESC, M_WAITOK);
272 			error = mem_range_attr_get(md, &nd);
273 			if (!error)
274 				error = copyout(md, mo->mo_desc,
275 					nd * sizeof(struct mem_range_desc));
276 			free(md, M_MEMDESC);
277 		}
278 		else
279 			nd = mem_range_softc.mr_ndesc;
280 		mo->mo_arg[0] = nd;
281 		break;
282 
283 	case MEMRANGE_SET:
284 		md = (struct mem_range_desc *)malloc(sizeof(struct mem_range_desc),
285 						    M_MEMDESC, M_WAITOK);
286 		error = copyin(mo->mo_desc, md, sizeof(struct mem_range_desc));
287 		/* clamp description string */
288 		md->mr_owner[sizeof(md->mr_owner) - 1] = 0;
289 		if (error == 0)
290 			error = mem_range_attr_set(md, &mo->mo_arg[0]);
291 		free(md, M_MEMDESC);
292 		break;
293 	}
294 	return (error);
295 }
296 
297 /*
298  * Implementation-neutral, kernel-callable functions for manipulating
299  * memory range attributes.
300  */
301 int
302 mem_range_attr_get(struct mem_range_desc *mrd, int *arg)
303 {
304 	/* can we handle this? */
305 	if (mem_range_softc.mr_op == NULL)
306 		return (EOPNOTSUPP);
307 
308 	if (*arg == 0)
309 		*arg = mem_range_softc.mr_ndesc;
310 	else
311 		bcopy(mem_range_softc.mr_desc, mrd,
312 			(*arg) * sizeof(struct mem_range_desc));
313 	return (0);
314 }
315 
316 int
317 mem_range_attr_set(struct mem_range_desc *mrd, int *arg)
318 {
319 	/* can we handle this? */
320 	if (mem_range_softc.mr_op == NULL)
321 		return (EOPNOTSUPP);
322 
323 	return (mem_range_softc.mr_op->set(&mem_range_softc, mrd, arg));
324 }
325 
326 static int
327 mem_modevent(module_t mod, int type, void *data)
328 {
329 	switch(type) {
330 	case MOD_LOAD:
331 		if (bootverbose)
332 			printf("mem: <memory & I/O>\n");
333 		/* Initialise memory range handling */
334 		if (mem_range_softc.mr_op != NULL)
335 			mem_range_softc.mr_op->init(&mem_range_softc);
336 
337 		memdev = make_dev(&mem_cdevsw, 0, UID_ROOT, GID_KMEM,
338 			0640, "mem");
339 		kmemdev = make_dev(&mem_cdevsw, 1, UID_ROOT, GID_KMEM,
340 			0640, "kmem");
341 		iodev = make_dev(&mem_cdevsw, 14, UID_ROOT, GID_WHEEL,
342 			0600, "io");
343 		return (0);
344 
345 	case MOD_UNLOAD:
346 		destroy_dev(memdev);
347 		destroy_dev(kmemdev);
348 		destroy_dev(iodev);
349 		return (0);
350 
351 	case MOD_SHUTDOWN:
352 		return (0);
353 
354 	default:
355 		return (EOPNOTSUPP);
356 	}
357 }
358 
359 DEV_MODULE(mem, mem_modevent, NULL);
360