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