2 * Copyright (c) 1988 University of Utah.
3 * Copyright (c) 1982, 1986, 1990 The Regents of the University of California.
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.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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
39 * from: Utah $Hdr: mem.c 1.13 89/10/08$
40 * from: @(#)mem.c 7.2 (Berkeley) 5/9/91
41 * $FreeBSD: src/sys/i386/i386/mem.c,v 1.79.2.9 2003/01/04 22:58:01 njl Exp $
42 * $DragonFly: src/sys/kern/kern_memio.c,v 1.22 2006/10/23 21:50:30 dillon Exp $
49 #include <sys/param.h>
50 #include <sys/systm.h>
53 #include <sys/fcntl.h>
54 #include <sys/filio.h>
55 #include <sys/ioccom.h>
56 #include <sys/kernel.h>
57 #include <sys/malloc.h>
58 #include <sys/memrange.h>
60 #include <sys/random.h>
61 #include <sys/signalvar.h>
63 #include <sys/vnode.h>
65 #include <machine/frame.h>
66 #include <machine/psl.h>
67 #include <machine/specialreg.h>
68 #include <arch/isa/intr_machdep.h>
72 #include <vm/vm_extern.h>
75 static d_open_t mmopen;
76 static d_close_t mmclose;
77 static d_read_t mmread;
78 static d_write_t mmwrite;
79 static d_ioctl_t mmioctl;
80 static d_mmap_t memmmap;
81 static d_poll_t mmpoll;
84 static struct dev_ops mem_ops = {
85 { "mem", CDEV_MAJOR, D_MEM },
98 MALLOC_DEFINE(M_MEMDESC, "memdesc", "memory range descriptors");
99 static int mem_ioctl (cdev_t, u_long, caddr_t, int, struct ucred *);
100 static int random_ioctl (cdev_t, u_long, caddr_t, int, struct ucred *);
102 struct mem_range_softc mem_range_softc;
106 mmopen(struct dev_open_args *ap)
108 cdev_t dev = ap->a_head.a_dev;
111 switch (minor(dev)) {
114 if ((ap->a_oflags & FWRITE) && securelevel > 0)
118 error = suser_cred(ap->a_cred, 0);
123 curproc->p_md.md_regs->tf_eflags |= PSL_IOPL;
132 mmclose(struct dev_close_args *ap)
134 cdev_t dev = ap->a_head.a_dev;
136 switch (minor(dev)) {
138 curproc->p_md.md_regs->tf_eflags &= ~PSL_IOPL;
148 mmrw(cdev_t dev, struct uio *uio, int flags)
157 while (uio->uio_resid > 0 && error == 0) {
159 if (iov->iov_len == 0) {
162 if (uio->uio_iovcnt < 0)
166 switch (minor(dev)) {
169 * minor device 0 is physical memory, /dev/mem
173 pmap_kenter((vm_offset_t)ptvmmap, v);
174 o = (int)uio->uio_offset & PAGE_MASK;
175 c = (u_int)(PAGE_SIZE - ((int)iov->iov_base & PAGE_MASK));
176 c = min(c, (u_int)(PAGE_SIZE - o));
177 c = min(c, (u_int)iov->iov_len);
178 error = uiomove((caddr_t)&ptvmmap[o], (int)c, uio);
179 pmap_kremove((vm_offset_t)ptvmmap);
184 * minor device 1 is kernel memory, /dev/kmem
186 vm_offset_t addr, eaddr;
190 * Make sure that all of the pages are currently
191 * resident so that we don't create any zero-fill
194 addr = trunc_page(uio->uio_offset);
195 eaddr = round_page(uio->uio_offset + c);
197 if (addr < (vm_offset_t)VADDR(PTDPTDI, 0))
199 if (eaddr >= (vm_offset_t)VADDR(APTDPTDI, 0))
201 for (; addr < eaddr; addr += PAGE_SIZE)
202 if (pmap_extract(kernel_pmap, addr) == 0)
205 if (!kernacc((caddr_t)(int)uio->uio_offset, c,
206 uio->uio_rw == UIO_READ ?
207 VM_PROT_READ : VM_PROT_WRITE))
209 error = uiomove((caddr_t)(int)uio->uio_offset, (int)c, uio);
214 * minor device 2 is EOF/RATHOLE
216 if (uio->uio_rw == UIO_READ)
222 * minor device 3 (/dev/random) is source of filth
223 * on read, seeder on write
226 buf = kmalloc(PAGE_SIZE, M_TEMP, M_WAITOK);
227 c = min(iov->iov_len, PAGE_SIZE);
228 if (uio->uio_rw == UIO_WRITE) {
229 error = uiomove(buf, (int)c, uio);
231 error = add_buffer_randomness(buf, c);
233 poolsize = read_random(buf, c);
237 if ((flags & IO_NDELAY) != 0)
238 return (EWOULDBLOCK);
241 c = min(c, poolsize);
242 error = uiomove(buf, (int)c, uio);
247 * minor device 4 (/dev/urandom) is source of muck
248 * on read, writes are disallowed.
250 c = min(iov->iov_len, PAGE_SIZE);
251 if (uio->uio_rw == UIO_WRITE) {
255 if (CURSIG(curproc) != 0) {
257 * Use tsleep() to get the error code right.
258 * It should return immediately.
260 error = tsleep(&rand_bolt, PCATCH, "urand", 1);
261 if (error != 0 && error != EWOULDBLOCK)
265 buf = kmalloc(PAGE_SIZE, M_TEMP, M_WAITOK);
266 poolsize = read_random_unlimited(buf, c);
267 c = min(c, poolsize);
268 error = uiomove(buf, (int)c, uio);
272 * minor device 12 (/dev/zero) is source of nulls
273 * on read, write are disallowed.
275 if (uio->uio_rw == UIO_WRITE) {
281 kmalloc(PAGE_SIZE, M_TEMP, M_WAITOK);
282 bzero(zbuf, PAGE_SIZE);
284 c = min(iov->iov_len, PAGE_SIZE);
285 error = uiomove(zbuf, (int)c, uio);
294 uio->uio_offset += c;
303 mmread(struct dev_read_args *ap)
305 return(mmrw(ap->a_head.a_dev, ap->a_uio, ap->a_ioflag));
309 mmwrite(struct dev_write_args *ap)
311 return(mmrw(ap->a_head.a_dev, ap->a_uio, ap->a_ioflag));
318 /*******************************************************\
319 * allow user processes to MMAP some memory sections *
320 * instead of going through read/write *
321 \*******************************************************/
324 memmmap(struct dev_mmap_args *ap)
326 cdev_t dev = ap->a_head.a_dev;
328 switch (minor(dev)) {
331 * minor device 0 is physical memory
333 ap->a_result = i386_btop(ap->a_offset);
337 * minor device 1 is kernel memory
339 ap->a_result = i386_btop(vtophys(ap->a_offset));
348 mmioctl(struct dev_ioctl_args *ap)
350 cdev_t dev = ap->a_head.a_dev;
352 switch (minor(dev)) {
354 return mem_ioctl(dev, ap->a_cmd, ap->a_data,
355 ap->a_fflag, ap->a_cred);
358 return random_ioctl(dev, ap->a_cmd, ap->a_data,
359 ap->a_fflag, ap->a_cred);
365 * Operations for changing memory attributes.
367 * This is basically just an ioctl shim for mem_range_attr_get
368 * and mem_range_attr_set.
371 mem_ioctl(cdev_t dev, u_long cmd, caddr_t data, int flags, struct ucred *cred)
374 struct mem_range_op *mo = (struct mem_range_op *)data;
375 struct mem_range_desc *md;
377 /* is this for us? */
378 if ((cmd != MEMRANGE_GET) &&
379 (cmd != MEMRANGE_SET))
382 /* any chance we can handle this? */
383 if (mem_range_softc.mr_op == NULL)
386 /* do we have any descriptors? */
387 if (mem_range_softc.mr_ndesc == 0)
392 nd = imin(mo->mo_arg[0], mem_range_softc.mr_ndesc);
394 md = (struct mem_range_desc *)
395 kmalloc(nd * sizeof(struct mem_range_desc),
396 M_MEMDESC, M_WAITOK);
397 error = mem_range_attr_get(md, &nd);
399 error = copyout(md, mo->mo_desc,
400 nd * sizeof(struct mem_range_desc));
401 kfree(md, M_MEMDESC);
403 nd = mem_range_softc.mr_ndesc;
409 md = (struct mem_range_desc *)kmalloc(sizeof(struct mem_range_desc),
410 M_MEMDESC, M_WAITOK);
411 error = copyin(mo->mo_desc, md, sizeof(struct mem_range_desc));
412 /* clamp description string */
413 md->mr_owner[sizeof(md->mr_owner) - 1] = 0;
415 error = mem_range_attr_set(md, &mo->mo_arg[0]);
416 kfree(md, M_MEMDESC);
423 * Implementation-neutral, kernel-callable functions for manipulating
424 * memory range attributes.
427 mem_range_attr_get(struct mem_range_desc *mrd, int *arg)
429 /* can we handle this? */
430 if (mem_range_softc.mr_op == NULL)
434 *arg = mem_range_softc.mr_ndesc;
436 bcopy(mem_range_softc.mr_desc, mrd, (*arg) * sizeof(struct mem_range_desc));
442 mem_range_attr_set(struct mem_range_desc *mrd, int *arg)
444 /* can we handle this? */
445 if (mem_range_softc.mr_op == NULL)
448 return (mem_range_softc.mr_op->set(&mem_range_softc, mrd, arg));
453 mem_range_AP_init(void)
455 if (mem_range_softc.mr_op && mem_range_softc.mr_op->initAP)
456 return (mem_range_softc.mr_op->initAP(&mem_range_softc));
461 random_ioctl(cdev_t dev, u_long cmd, caddr_t data, int flags, struct ucred *cred)
467 * Even inspecting the state is privileged, since it gives a hint
468 * about how easily the randomness might be guessed.
473 /* Really handled in upper layer */
477 intr = *(int16_t *)data;
478 if ((error = suser_cred(cred, 0)) != 0)
480 if (intr < 0 || intr >= MAX_INTS)
482 register_randintr(intr);
485 intr = *(int16_t *)data;
486 if ((error = suser_cred(cred, 0)) != 0)
488 if (intr < 0 || intr >= MAX_INTS)
490 unregister_randintr(intr);
496 intr = *(int16_t *)data;
497 if ((error = suser_cred(cred, 0)) != 0)
499 if (intr < 0 || intr >= MAX_INTS)
501 intr = next_registered_randintr(intr);
502 if (intr == MAX_INTS)
504 *(u_int16_t *)data = intr;
514 mmpoll(struct dev_poll_args *ap)
516 cdev_t dev = ap->a_head.a_dev;
519 switch (minor(dev)) {
520 case 3: /* /dev/random */
521 revents = random_poll(dev, ap->a_events);
523 case 4: /* /dev/urandom */
525 revents = seltrue(dev, ap->a_events);
528 ap->a_events = revents;
533 iszerodev(cdev_t dev)
535 return ((major(dev) == mem_ops.head.maj)
536 && minor(dev) == 12);
540 mem_drvinit(void *unused)
543 /* Initialise memory range handling */
544 if (mem_range_softc.mr_op != NULL)
545 mem_range_softc.mr_op->init(&mem_range_softc);
547 dev_ops_add(&mem_ops, 0xf0, 0);
548 make_dev(&mem_ops, 0, UID_ROOT, GID_KMEM, 0640, "mem");
549 make_dev(&mem_ops, 1, UID_ROOT, GID_KMEM, 0640, "kmem");
550 make_dev(&mem_ops, 2, UID_ROOT, GID_WHEEL, 0666, "null");
551 make_dev(&mem_ops, 3, UID_ROOT, GID_WHEEL, 0644, "random");
552 make_dev(&mem_ops, 4, UID_ROOT, GID_WHEEL, 0644, "urandom");
553 make_dev(&mem_ops, 12, UID_ROOT, GID_WHEEL, 0666, "zero");
554 make_dev(&mem_ops, 14, UID_ROOT, GID_WHEEL, 0600, "io");
557 SYSINIT(memdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,mem_drvinit,NULL)