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/platform/pc32/i386/Attic/mem.c,v 1.16 2006/07/10 21:06:08 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 <i386/isa/intr_machdep.h>
72 #include <vm/vm_extern.h>
75 static d_open_t mmopen;
76 static d_close_t mmclose;
78 static d_ioctl_t mmioctl;
79 static d_mmap_t memmmap;
80 static d_poll_t mmpoll;
83 static struct cdevsw mem_cdevsw = {
97 /* strategy */ nostrategy,
102 static int rand_bolt;
105 MALLOC_DEFINE(M_MEMDESC, "memdesc", "memory range descriptors");
106 static int mem_ioctl (dev_t, u_long, caddr_t, int, struct thread *);
107 static int random_ioctl (dev_t, u_long, caddr_t, int, struct thread *);
109 struct mem_range_softc mem_range_softc;
113 mmclose(dev_t dev, int flags, int fmt, struct thread *td)
115 struct proc *p = td->td_proc;
117 switch (minor(dev)) {
119 p->p_md.md_regs->tf_eflags &= ~PSL_IOPL;
128 mmopen(dev_t dev, int flags, int fmt, struct thread *td)
131 struct proc *p = td->td_proc;
133 switch (minor(dev)) {
136 if ((flags & FWRITE) && securelevel > 0)
145 p->p_md.md_regs->tf_eflags |= PSL_IOPL;
154 mmrw(dev, uio, flags)
166 while (uio->uio_resid > 0 && error == 0) {
168 if (iov->iov_len == 0) {
171 if (uio->uio_iovcnt < 0)
175 switch (minor(dev)) {
178 * minor device 0 is physical memory, /dev/mem
182 pmap_kenter((vm_offset_t)ptvmmap, v);
183 o = (int)uio->uio_offset & PAGE_MASK;
184 c = (u_int)(PAGE_SIZE - ((int)iov->iov_base & PAGE_MASK));
185 c = min(c, (u_int)(PAGE_SIZE - o));
186 c = min(c, (u_int)iov->iov_len);
187 error = uiomove((caddr_t)&ptvmmap[o], (int)c, uio);
188 pmap_kremove((vm_offset_t)ptvmmap);
193 * minor device 1 is kernel memory, /dev/kmem
195 vm_offset_t addr, eaddr;
199 * Make sure that all of the pages are currently
200 * resident so that we don't create any zero-fill
203 addr = trunc_page(uio->uio_offset);
204 eaddr = round_page(uio->uio_offset + c);
206 if (addr < (vm_offset_t)VADDR(PTDPTDI, 0))
208 if (eaddr >= (vm_offset_t)VADDR(APTDPTDI, 0))
210 for (; addr < eaddr; addr += PAGE_SIZE)
211 if (pmap_extract(kernel_pmap, addr) == 0)
214 if (!kernacc((caddr_t)(int)uio->uio_offset, c,
215 uio->uio_rw == UIO_READ ?
216 VM_PROT_READ : VM_PROT_WRITE))
218 error = uiomove((caddr_t)(int)uio->uio_offset, (int)c, uio);
223 * minor device 2 is EOF/RATHOLE
225 if (uio->uio_rw == UIO_READ)
231 * minor device 3 (/dev/random) is source of filth
232 * on read, seeder on write
235 buf = malloc(PAGE_SIZE, M_TEMP, M_WAITOK);
236 c = min(iov->iov_len, PAGE_SIZE);
237 if (uio->uio_rw == UIO_WRITE) {
238 error = uiomove(buf, (int)c, uio);
240 error = add_buffer_randomness(buf, c);
242 poolsize = read_random(buf, c);
246 if ((flags & IO_NDELAY) != 0)
247 return (EWOULDBLOCK);
250 c = min(c, poolsize);
251 error = uiomove(buf, (int)c, uio);
256 * minor device 4 (/dev/urandom) is source of muck
257 * on read, writes are disallowed.
259 c = min(iov->iov_len, PAGE_SIZE);
260 if (uio->uio_rw == UIO_WRITE) {
264 if (CURSIG(curproc) != 0) {
266 * Use tsleep() to get the error code right.
267 * It should return immediately.
269 error = tsleep(&rand_bolt, PCATCH, "urand", 1);
270 if (error != 0 && error != EWOULDBLOCK)
274 buf = malloc(PAGE_SIZE, M_TEMP, M_WAITOK);
275 poolsize = read_random_unlimited(buf, c);
276 c = min(c, poolsize);
277 error = uiomove(buf, (int)c, uio);
281 * minor device 12 (/dev/zero) is source of nulls
282 * on read, write are disallowed.
284 if (uio->uio_rw == UIO_WRITE) {
290 malloc(PAGE_SIZE, M_TEMP, M_WAITOK);
291 bzero(zbuf, PAGE_SIZE);
293 c = min(iov->iov_len, PAGE_SIZE);
294 error = uiomove(zbuf, (int)c, uio);
303 uio->uio_offset += c;
314 /*******************************************************\
315 * allow user processes to MMAP some memory sections *
316 * instead of going through read/write *
317 \*******************************************************/
319 memmmap(dev_t dev, vm_offset_t offset, int nprot)
324 /* minor device 0 is physical memory */
326 return i386_btop(offset);
328 /* minor device 1 is kernel memory */
330 return i386_btop(vtophys(offset));
338 mmioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct thread *td)
341 switch (minor(dev)) {
343 return mem_ioctl(dev, cmd, data, flags, td);
346 return random_ioctl(dev, cmd, data, flags, td);
352 * Operations for changing memory attributes.
354 * This is basically just an ioctl shim for mem_range_attr_get
355 * and mem_range_attr_set.
358 mem_ioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct thread *td)
361 struct mem_range_op *mo = (struct mem_range_op *)data;
362 struct mem_range_desc *md;
364 /* is this for us? */
365 if ((cmd != MEMRANGE_GET) &&
366 (cmd != MEMRANGE_SET))
369 /* any chance we can handle this? */
370 if (mem_range_softc.mr_op == NULL)
373 /* do we have any descriptors? */
374 if (mem_range_softc.mr_ndesc == 0)
379 nd = imin(mo->mo_arg[0], mem_range_softc.mr_ndesc);
381 md = (struct mem_range_desc *)
382 malloc(nd * sizeof(struct mem_range_desc),
383 M_MEMDESC, M_WAITOK);
384 error = mem_range_attr_get(md, &nd);
386 error = copyout(md, mo->mo_desc,
387 nd * sizeof(struct mem_range_desc));
390 nd = mem_range_softc.mr_ndesc;
396 md = (struct mem_range_desc *)malloc(sizeof(struct mem_range_desc),
397 M_MEMDESC, M_WAITOK);
398 error = copyin(mo->mo_desc, md, sizeof(struct mem_range_desc));
399 /* clamp description string */
400 md->mr_owner[sizeof(md->mr_owner) - 1] = 0;
402 error = mem_range_attr_set(md, &mo->mo_arg[0]);
410 * Implementation-neutral, kernel-callable functions for manipulating
411 * memory range attributes.
414 mem_range_attr_get(mrd, arg)
415 struct mem_range_desc *mrd;
418 /* can we handle this? */
419 if (mem_range_softc.mr_op == NULL)
423 *arg = mem_range_softc.mr_ndesc;
425 bcopy(mem_range_softc.mr_desc, mrd, (*arg) * sizeof(struct mem_range_desc));
431 mem_range_attr_set(mrd, arg)
432 struct mem_range_desc *mrd;
435 /* can we handle this? */
436 if (mem_range_softc.mr_op == NULL)
439 return (mem_range_softc.mr_op->set(&mem_range_softc, mrd, arg));
444 mem_range_AP_init(void)
446 if (mem_range_softc.mr_op && mem_range_softc.mr_op->initAP)
447 return (mem_range_softc.mr_op->initAP(&mem_range_softc));
452 random_ioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct thread *td)
458 * Even inspecting the state is privileged, since it gives a hint
459 * about how easily the randomness might be guessed.
464 /* Really handled in upper layer */
468 intr = *(int16_t *)data;
469 if ((error = suser(td)) != 0)
471 if (intr < 0 || intr >= MAX_INTS)
473 register_randintr(intr);
476 intr = *(int16_t *)data;
477 if ((error = suser(td)) != 0)
479 if (intr < 0 || intr >= MAX_INTS)
481 unregister_randintr(intr);
487 intr = *(int16_t *)data;
488 if ((error = suser(td)) != 0)
490 if (intr < 0 || intr >= MAX_INTS)
492 intr = next_registered_randintr(intr);
493 if (intr == MAX_INTS)
495 *(u_int16_t *)data = intr;
505 mmpoll(dev_t dev, int events, struct thread *td)
507 switch (minor(dev)) {
508 case 3: /* /dev/random */
509 return random_poll(dev, events, td);
510 case 4: /* /dev/urandom */
512 return seltrue(dev, events, td);
520 return ((major(dev) == mem_cdevsw.d_maj)
521 && minor(dev) == 12);
525 mem_drvinit(void *unused)
528 /* Initialise memory range handling */
529 if (mem_range_softc.mr_op != NULL)
530 mem_range_softc.mr_op->init(&mem_range_softc);
532 cdevsw_add(&mem_cdevsw, 0xf0, 0);
533 make_dev(&mem_cdevsw, 0, UID_ROOT, GID_KMEM, 0640, "mem");
534 make_dev(&mem_cdevsw, 1, UID_ROOT, GID_KMEM, 0640, "kmem");
535 make_dev(&mem_cdevsw, 2, UID_ROOT, GID_WHEEL, 0666, "null");
536 make_dev(&mem_cdevsw, 3, UID_ROOT, GID_WHEEL, 0644, "random");
537 make_dev(&mem_cdevsw, 4, UID_ROOT, GID_WHEEL, 0644, "urandom");
538 make_dev(&mem_cdevsw, 12, UID_ROOT, GID_WHEEL, 0666, "zero");
539 make_dev(&mem_cdevsw, 14, UID_ROOT, GID_WHEEL, 0600, "io");
542 SYSINIT(memdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,mem_drvinit,NULL)