xref: /freebsd/sys/amd64/amd64/mem.c (revision d2387d42b8da231a5b95cbc313825fb2aadf26f6)
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  *	from: Utah $Hdr: mem.c 1.13 89/10/08$
40  *	from: @(#)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_version =	D_VERSION,
85 	.d_open =	mmopen,
86 	.d_close =	mmclose,
87 	.d_read =	mmrw,
88 	.d_write =	mmrw,
89 	.d_ioctl =	mmioctl,
90 	.d_mmap =	memmmap,
91 	.d_name =	"mem",
92 	.d_maj =	CDEV_MAJOR,
93 	.d_flags =	D_MEM | D_NEEDGIANT,
94 };
95 
96 MALLOC_DEFINE(M_MEMDESC, "memdesc", "memory range descriptors");
97 
98 struct mem_range_softc mem_range_softc;
99 
100 static int
101 mmclose(dev_t dev, int flags, int fmt, struct thread *td)
102 {
103 	switch (minor(dev)) {
104 	case 14:
105 		td->td_frame->tf_rflags &= ~PSL_IOPL;
106 	}
107 	return (0);
108 }
109 
110 static int
111 mmopen(dev_t dev, int flags, int fmt, struct thread *td)
112 {
113 	int error;
114 
115 	switch (minor(dev)) {
116 	case 0:
117 	case 1:
118 		if (flags & FWRITE) {
119 			error = securelevel_gt(td->td_ucred, 0);
120 			if (error != 0)
121 				return (error);
122 		}
123 		break;
124 	case 14:
125 		error = suser(td);
126 		if (error != 0)
127 			return (error);
128 		error = securelevel_gt(td->td_ucred, 0);
129 		if (error != 0)
130 			return (error);
131 		td->td_frame->tf_rflags |= PSL_IOPL;
132 		break;
133 	}
134 	return (0);
135 }
136 
137 /*ARGSUSED*/
138 static int
139 mmrw(dev_t dev, struct uio *uio, int flags)
140 {
141 	int o;
142 	u_long c = 0, v;
143 	struct iovec *iov;
144 	int error = 0;
145 	vm_offset_t addr, eaddr;
146 
147 	GIANT_REQUIRED;
148 
149 	while (uio->uio_resid > 0 && error == 0) {
150 		iov = uio->uio_iov;
151 		if (iov->iov_len == 0) {
152 			uio->uio_iov++;
153 			uio->uio_iovcnt--;
154 			if (uio->uio_iovcnt < 0)
155 				panic("mmrw");
156 			continue;
157 		}
158 		switch (minor(dev)) {
159 
160 /* minor device 0 is physical memory */
161 		case 0:
162 			v = uio->uio_offset;
163 kmemphys:
164 			o = v & PAGE_MASK;
165 			c = min(uio->uio_resid, (u_int)(PAGE_SIZE - o));
166 			error = uiomove((void *)PHYS_TO_DMAP(v), (int)c, uio);
167 			continue;
168 
169 /* minor device 1 is kernel memory */
170 		case 1:
171 			v = uio->uio_offset;
172 
173 			if (v >= DMAP_MIN_ADDRESS && v < DMAP_MAX_ADDRESS) {
174 				v = DMAP_TO_PHYS(v);
175 				goto kmemphys;
176 			}
177 
178 			c = iov->iov_len;
179 
180 			/*
181 			 * Make sure that all of the pages are currently resident so
182 			 * that we don't create any zero-fill pages.
183 			 */
184 			addr = trunc_page(v);
185 			eaddr = round_page(v + c);
186 
187 			if (addr < (vm_offset_t)KERNBASE)
188 				return (EFAULT);
189 			for (; addr < eaddr; addr += PAGE_SIZE)
190 				if (pmap_extract(kernel_pmap, addr) == 0)
191 					return (EFAULT);
192 
193 			if (!kernacc((caddr_t)(long)v, c,
194 			    uio->uio_rw == UIO_READ ?
195 			    VM_PROT_READ : VM_PROT_WRITE))
196 				return (EFAULT);
197 
198 			error = uiomove((caddr_t)(long)v, (int)c, uio);
199 			continue;
200 
201 		default:
202 			return (ENODEV);
203 		}
204 
205 		if (error)
206 			break;
207 		iov->iov_base = (char *)iov->iov_base + c;
208 		iov->iov_len -= c;
209 		uio->uio_offset += c;
210 		uio->uio_resid -= c;
211 	}
212 	return (error);
213 }
214 
215 /*******************************************************\
216 * allow user processes to MMAP some memory sections	*
217 * instead of going through read/write			*
218 \*******************************************************/
219 static int
220 memmmap(dev_t dev, vm_offset_t offset, vm_paddr_t *paddr, int prot)
221 {
222 	switch (minor(dev))
223 	{
224 
225 	/* minor device 0 is physical memory */
226 	case 0:
227 		*paddr = offset;
228 		break;
229 
230 	/* minor device 1 is kernel memory */
231 	case 1:
232         	*paddr = vtophys(offset);
233 		break;
234 
235 	default:
236 		return (-1);
237 	}
238 	return (0);
239 }
240 
241 /*
242  * Operations for changing memory attributes.
243  *
244  * This is basically just an ioctl shim for mem_range_attr_get
245  * and mem_range_attr_set.
246  */
247 static int
248 mmioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct thread *td)
249 {
250 	int nd, error = 0;
251 	struct mem_range_op *mo = (struct mem_range_op *)data;
252 	struct mem_range_desc *md;
253 
254 	/* is this for us? */
255 	if ((cmd != MEMRANGE_GET) &&
256 	    (cmd != MEMRANGE_SET))
257 		return (ENOTTY);
258 
259 	/* any chance we can handle this? */
260 	if (mem_range_softc.mr_op == NULL)
261 		return (EOPNOTSUPP);
262 
263 	/* do we have any descriptors? */
264 	if (mem_range_softc.mr_ndesc == 0)
265 		return (ENXIO);
266 
267 	switch (cmd) {
268 	case MEMRANGE_GET:
269 		nd = imin(mo->mo_arg[0], mem_range_softc.mr_ndesc);
270 		if (nd > 0) {
271 			md = (struct mem_range_desc *)
272 				malloc(nd * sizeof(struct mem_range_desc),
273 				       M_MEMDESC, M_WAITOK);
274 			error = mem_range_attr_get(md, &nd);
275 			if (!error)
276 				error = copyout(md, mo->mo_desc,
277 					nd * sizeof(struct mem_range_desc));
278 			free(md, M_MEMDESC);
279 		}
280 		else
281 			nd = mem_range_softc.mr_ndesc;
282 		mo->mo_arg[0] = nd;
283 		break;
284 
285 	case MEMRANGE_SET:
286 		md = (struct mem_range_desc *)malloc(sizeof(struct mem_range_desc),
287 						    M_MEMDESC, M_WAITOK);
288 		error = copyin(mo->mo_desc, md, sizeof(struct mem_range_desc));
289 		/* clamp description string */
290 		md->mr_owner[sizeof(md->mr_owner) - 1] = 0;
291 		if (error == 0)
292 			error = mem_range_attr_set(md, &mo->mo_arg[0]);
293 		free(md, M_MEMDESC);
294 		break;
295 	}
296 	return (error);
297 }
298 
299 /*
300  * Implementation-neutral, kernel-callable functions for manipulating
301  * memory range attributes.
302  */
303 int
304 mem_range_attr_get(struct mem_range_desc *mrd, int *arg)
305 {
306 	/* can we handle this? */
307 	if (mem_range_softc.mr_op == NULL)
308 		return (EOPNOTSUPP);
309 
310 	if (*arg == 0)
311 		*arg = mem_range_softc.mr_ndesc;
312 	else
313 		bcopy(mem_range_softc.mr_desc, mrd,
314 			(*arg) * sizeof(struct mem_range_desc));
315 	return (0);
316 }
317 
318 int
319 mem_range_attr_set(struct mem_range_desc *mrd, int *arg)
320 {
321 	/* can we handle this? */
322 	if (mem_range_softc.mr_op == NULL)
323 		return (EOPNOTSUPP);
324 
325 	return (mem_range_softc.mr_op->set(&mem_range_softc, mrd, arg));
326 }
327 
328 #ifdef SMP
329 void
330 mem_range_AP_init(void)
331 {
332 	if (mem_range_softc.mr_op && mem_range_softc.mr_op->initAP)
333 		(mem_range_softc.mr_op->initAP(&mem_range_softc));
334 }
335 #endif
336 
337 static int
338 mem_modevent(module_t mod, int type, void *data)
339 {
340 	switch(type) {
341 	case MOD_LOAD:
342 		if (bootverbose)
343 			printf("mem: <memory & I/O>\n");
344 		/* Initialise memory range handling */
345 		if (mem_range_softc.mr_op != NULL)
346 			mem_range_softc.mr_op->init(&mem_range_softc);
347 
348 		memdev = make_dev(&mem_cdevsw, 0, UID_ROOT, GID_KMEM,
349 			0640, "mem");
350 		kmemdev = make_dev(&mem_cdevsw, 1, UID_ROOT, GID_KMEM,
351 			0640, "kmem");
352 		iodev = make_dev(&mem_cdevsw, 14, UID_ROOT, GID_WHEEL,
353 			0600, "io");
354 		return (0);
355 
356 	case MOD_UNLOAD:
357 		destroy_dev(memdev);
358 		destroy_dev(kmemdev);
359 		destroy_dev(iodev);
360 		return (0);
361 
362 	case MOD_SHUTDOWN:
363 		return (0);
364 
365 	default:
366 		return (EOPNOTSUPP);
367 	}
368 }
369 
370 DEV_MODULE(mem, mem_modevent, NULL);
371