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.32 2008/07/23 16:39:28 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/kernel.h>
56 #include <sys/malloc.h>
57 #include <sys/memrange.h>
60 #include <sys/random.h>
61 #include <sys/signalvar.h>
62 #include <sys/signal2.h>
64 #include <sys/vnode.h>
68 #include <vm/vm_extern.h>
71 static d_open_t mmopen;
72 static d_close_t mmclose;
73 static d_read_t mmread;
74 static d_write_t mmwrite;
75 static d_ioctl_t mmioctl;
76 static d_mmap_t memmmap;
77 static d_poll_t mmpoll;
78 static d_kqfilter_t mmkqfilter;
81 static struct dev_ops mem_ops = {
82 { "mem", CDEV_MAJOR, D_MEM | D_MPSAFE_READ | D_MPSAFE_WRITE | D_KQFILTER },
89 .d_kqfilter = mmkqfilter,
95 static cdev_t zerodev = NULL;
97 MALLOC_DEFINE(M_MEMDESC, "memdesc", "memory range descriptors");
98 static int mem_ioctl (cdev_t, u_long, caddr_t, int, struct ucred *);
99 static int random_ioctl (cdev_t, u_long, caddr_t, int, struct ucred *);
101 struct mem_range_softc mem_range_softc;
105 mmopen(struct dev_open_args *ap)
107 cdev_t dev = ap->a_head.a_dev;
110 switch (minor(dev)) {
113 if (ap->a_oflags & FWRITE) {
114 if (securelevel > 0 || kernel_mem_readonly)
120 error = priv_check_cred(ap->a_cred, PRIV_ROOT, 0);
123 if (securelevel > 0 || kernel_mem_readonly) {
127 error = cpu_set_iopl();
137 mmclose(struct dev_close_args *ap)
139 cdev_t dev = ap->a_head.a_dev;
142 switch (minor(dev)) {
144 error = cpu_clr_iopl();
155 mmrw(cdev_t dev, struct uio *uio, int flags)
164 while (uio->uio_resid > 0 && error == 0) {
166 if (iov->iov_len == 0) {
169 if (uio->uio_iovcnt < 0)
173 switch (minor(dev)) {
176 * minor device 0 is physical memory, /dev/mem
180 pmap_kenter((vm_offset_t)ptvmmap, v);
181 o = (int)uio->uio_offset & PAGE_MASK;
182 c = (u_int)(PAGE_SIZE - ((uintptr_t)iov->iov_base & PAGE_MASK));
183 c = min(c, (u_int)(PAGE_SIZE - o));
184 c = min(c, (u_int)iov->iov_len);
185 error = uiomove((caddr_t)&ptvmmap[o], (int)c, uio);
186 pmap_kremove((vm_offset_t)ptvmmap);
191 * minor device 1 is kernel memory, /dev/kmem
193 vm_offset_t saddr, eaddr;
199 * Make sure that all of the pages are currently
200 * resident so that we don't create any zero-fill
203 saddr = trunc_page(uio->uio_offset);
204 eaddr = round_page(uio->uio_offset + c);
209 * Make sure the kernel addresses are mapped.
210 * platform_direct_mapped() can be used to bypass
211 * default mapping via the page table (virtual kernels
212 * contain a lot of out-of-band data).
215 if (uio->uio_rw != UIO_READ)
216 prot |= VM_PROT_WRITE;
217 error = kvm_access_check(saddr, eaddr, prot);
220 error = uiomove((caddr_t)(vm_offset_t)uio->uio_offset,
226 * minor device 2 is EOF/RATHOLE
228 if (uio->uio_rw == UIO_READ)
234 * minor device 3 (/dev/random) is source of filth
235 * on read, seeder on write
238 buf = kmalloc(PAGE_SIZE, M_TEMP, M_WAITOK);
239 c = min(iov->iov_len, PAGE_SIZE);
240 if (uio->uio_rw == UIO_WRITE) {
241 error = uiomove(buf, (int)c, uio);
243 error = add_buffer_randomness(buf, c);
245 poolsize = read_random(buf, c);
249 if ((flags & IO_NDELAY) != 0)
250 return (EWOULDBLOCK);
253 c = min(c, poolsize);
254 error = uiomove(buf, (int)c, uio);
259 * minor device 4 (/dev/urandom) is source of muck
260 * on read, writes are disallowed.
262 c = min(iov->iov_len, PAGE_SIZE);
263 if (uio->uio_rw == UIO_WRITE) {
267 if (CURSIG(curthread->td_lwp) != 0) {
269 * Use tsleep() to get the error code right.
270 * It should return immediately.
272 error = tsleep(&rand_bolt, PCATCH, "urand", 1);
273 if (error != 0 && error != EWOULDBLOCK)
277 buf = kmalloc(PAGE_SIZE, M_TEMP, M_WAITOK);
278 poolsize = read_random_unlimited(buf, c);
279 c = min(c, poolsize);
280 error = uiomove(buf, (int)c, uio);
284 * minor device 12 (/dev/zero) is source of nulls
285 * on read, write are disallowed.
287 if (uio->uio_rw == UIO_WRITE) {
292 zbuf = (caddr_t)kmalloc(PAGE_SIZE, M_TEMP,
295 c = min(iov->iov_len, PAGE_SIZE);
296 error = uiomove(zbuf, (int)c, uio);
303 iov->iov_base = (char *)iov->iov_base + c;
305 uio->uio_offset += c;
314 mmread(struct dev_read_args *ap)
316 return(mmrw(ap->a_head.a_dev, ap->a_uio, ap->a_ioflag));
320 mmwrite(struct dev_write_args *ap)
322 return(mmrw(ap->a_head.a_dev, ap->a_uio, ap->a_ioflag));
329 /*******************************************************\
330 * allow user processes to MMAP some memory sections *
331 * instead of going through read/write *
332 \*******************************************************/
335 memmmap(struct dev_mmap_args *ap)
337 cdev_t dev = ap->a_head.a_dev;
339 switch (minor(dev)) {
342 * minor device 0 is physical memory
344 #if defined(__i386__)
345 ap->a_result = i386_btop(ap->a_offset);
346 #elif defined(__x86_64__)
347 ap->a_result = x86_64_btop(ap->a_offset);
352 * minor device 1 is kernel memory
354 #if defined(__i386__)
355 ap->a_result = i386_btop(vtophys(ap->a_offset));
356 #elif defined(__x86_64__)
357 ap->a_result = x86_64_btop(vtophys(ap->a_offset));
367 mmioctl(struct dev_ioctl_args *ap)
369 cdev_t dev = ap->a_head.a_dev;
371 switch (minor(dev)) {
373 return mem_ioctl(dev, ap->a_cmd, ap->a_data,
374 ap->a_fflag, ap->a_cred);
377 return random_ioctl(dev, ap->a_cmd, ap->a_data,
378 ap->a_fflag, ap->a_cred);
384 * Operations for changing memory attributes.
386 * This is basically just an ioctl shim for mem_range_attr_get
387 * and mem_range_attr_set.
390 mem_ioctl(cdev_t dev, u_long cmd, caddr_t data, int flags, struct ucred *cred)
393 struct mem_range_op *mo = (struct mem_range_op *)data;
394 struct mem_range_desc *md;
396 /* is this for us? */
397 if ((cmd != MEMRANGE_GET) &&
398 (cmd != MEMRANGE_SET))
401 /* any chance we can handle this? */
402 if (mem_range_softc.mr_op == NULL)
405 /* do we have any descriptors? */
406 if (mem_range_softc.mr_ndesc == 0)
411 nd = imin(mo->mo_arg[0], mem_range_softc.mr_ndesc);
413 md = (struct mem_range_desc *)
414 kmalloc(nd * sizeof(struct mem_range_desc),
415 M_MEMDESC, M_WAITOK);
416 error = mem_range_attr_get(md, &nd);
418 error = copyout(md, mo->mo_desc,
419 nd * sizeof(struct mem_range_desc));
420 kfree(md, M_MEMDESC);
422 nd = mem_range_softc.mr_ndesc;
428 md = (struct mem_range_desc *)kmalloc(sizeof(struct mem_range_desc),
429 M_MEMDESC, M_WAITOK);
430 error = copyin(mo->mo_desc, md, sizeof(struct mem_range_desc));
431 /* clamp description string */
432 md->mr_owner[sizeof(md->mr_owner) - 1] = 0;
434 error = mem_range_attr_set(md, &mo->mo_arg[0]);
435 kfree(md, M_MEMDESC);
442 * Implementation-neutral, kernel-callable functions for manipulating
443 * memory range attributes.
446 mem_range_attr_get(struct mem_range_desc *mrd, int *arg)
448 /* can we handle this? */
449 if (mem_range_softc.mr_op == NULL)
453 *arg = mem_range_softc.mr_ndesc;
455 bcopy(mem_range_softc.mr_desc, mrd, (*arg) * sizeof(struct mem_range_desc));
461 mem_range_attr_set(struct mem_range_desc *mrd, int *arg)
463 /* can we handle this? */
464 if (mem_range_softc.mr_op == NULL)
467 return (mem_range_softc.mr_op->set(&mem_range_softc, mrd, arg));
472 mem_range_AP_init(void)
474 if (mem_range_softc.mr_op && mem_range_softc.mr_op->initAP)
475 return (mem_range_softc.mr_op->initAP(&mem_range_softc));
480 random_ioctl(cdev_t dev, u_long cmd, caddr_t data, int flags, struct ucred *cred)
486 * Even inspecting the state is privileged, since it gives a hint
487 * about how easily the randomness might be guessed.
492 /* Really handled in upper layer */
496 intr = *(int16_t *)data;
497 if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0)
499 if (intr < 0 || intr >= MAX_INTS)
501 register_randintr(intr);
504 intr = *(int16_t *)data;
505 if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0)
507 if (intr < 0 || intr >= MAX_INTS)
509 unregister_randintr(intr);
515 intr = *(int16_t *)data;
516 if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0)
518 if (intr < 0 || intr >= MAX_INTS)
520 intr = next_registered_randintr(intr);
521 if (intr == MAX_INTS)
523 *(u_int16_t *)data = intr;
533 mmpoll(struct dev_poll_args *ap)
535 cdev_t dev = ap->a_head.a_dev;
538 switch (minor(dev)) {
539 case 3: /* /dev/random */
540 revents = random_poll(dev, ap->a_events);
542 case 4: /* /dev/urandom */
544 revents = seltrue(dev, ap->a_events);
547 ap->a_events = revents;
552 mm_filter_read(struct knote *kn, long hint)
558 dummy_filter_detach(struct knote *kn) {}
560 static struct filterops random_read_filtops =
561 { 1, NULL, dummy_filter_detach, random_filter_read };
563 static struct filterops mm_read_filtops =
564 { 1, NULL, dummy_filter_detach, mm_filter_read };
567 mmkqfilter(struct dev_kqfilter_args *ap)
569 struct knote *kn = ap->a_kn;
570 cdev_t dev = ap->a_head.a_dev;
573 switch (kn->kn_filter) {
575 switch (minor(dev)) {
577 kn->kn_fop = &random_read_filtops;
580 kn->kn_fop = &mm_read_filtops;
585 ap->a_result = EOPNOTSUPP;
593 iszerodev(cdev_t dev)
595 return (zerodev == dev);
599 mem_drvinit(void *unused)
602 /* Initialise memory range handling */
603 if (mem_range_softc.mr_op != NULL)
604 mem_range_softc.mr_op->init(&mem_range_softc);
606 make_dev(&mem_ops, 0, UID_ROOT, GID_KMEM, 0640, "mem");
607 make_dev(&mem_ops, 1, UID_ROOT, GID_KMEM, 0640, "kmem");
608 make_dev(&mem_ops, 2, UID_ROOT, GID_WHEEL, 0666, "null");
609 make_dev(&mem_ops, 3, UID_ROOT, GID_WHEEL, 0644, "random");
610 make_dev(&mem_ops, 4, UID_ROOT, GID_WHEEL, 0644, "urandom");
611 zerodev = make_dev(&mem_ops, 12, UID_ROOT, GID_WHEEL, 0666, "zero");
612 make_dev(&mem_ops, 14, UID_ROOT, GID_WHEEL, 0600, "io");
615 SYSINIT(memdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,mem_drvinit,NULL)