libc/citrus: Catch a NULL pointer earlier, check for empty string later.
[dragonfly.git] / sys / kern / kern_memio.c
blob1cda4e38a53cebcd218324bf972e5016c756f60a
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.
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
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. 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.
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.
35 * from: Utah $Hdr: mem.c 1.13 89/10/08$
36 * from: @(#)mem.c 7.2 (Berkeley) 5/9/91
37 * $FreeBSD: src/sys/i386/i386/mem.c,v 1.79.2.9 2003/01/04 22:58:01 njl Exp $
41 * Memory special file
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/buf.h>
47 #include <sys/conf.h>
48 #include <sys/fcntl.h>
49 #include <sys/filio.h>
50 #include <sys/kernel.h>
51 #include <sys/malloc.h>
52 #include <sys/memrange.h>
53 #include <sys/proc.h>
54 #include <sys/priv.h>
55 #include <sys/random.h>
56 #include <sys/signalvar.h>
57 #include <sys/uio.h>
58 #include <sys/vnode.h>
59 #include <sys/sysctl.h>
61 #include <sys/signal2.h>
63 #include <vm/vm.h>
64 #include <vm/pmap.h>
65 #include <vm/vm_extern.h>
68 static d_open_t mmopen;
69 static d_close_t mmclose;
70 static d_read_t mmread;
71 static d_write_t mmwrite;
72 static d_ioctl_t mmioctl;
73 #if 0
74 static d_mmap_t memmmap;
75 #endif
76 static d_kqfilter_t mmkqfilter;
77 static int memuksmap(cdev_t dev, vm_page_t fake);
79 #define CDEV_MAJOR 2
80 static struct dev_ops mem_ops = {
81 { "mem", 0, D_MPSAFE | D_QUICK },
82 .d_open = mmopen,
83 .d_close = mmclose,
84 .d_read = mmread,
85 .d_write = mmwrite,
86 .d_ioctl = mmioctl,
87 .d_kqfilter = mmkqfilter,
88 #if 0
89 .d_mmap = memmmap,
90 #endif
91 .d_uksmap = memuksmap
94 static struct dev_ops mem_ops_noq = {
95 { "mem", 0, D_MPSAFE },
96 .d_open = mmopen,
97 .d_close = mmclose,
98 .d_read = mmread,
99 .d_write = mmwrite,
100 .d_ioctl = mmioctl,
101 .d_kqfilter = mmkqfilter,
102 #if 0
103 .d_mmap = memmmap,
104 #endif
105 .d_uksmap = memuksmap
108 static int rand_bolt;
109 static caddr_t zbuf;
110 static cdev_t zerodev = NULL;
111 static struct lock mem_lock = LOCK_INITIALIZER("memlk", 0, 0);
113 MALLOC_DEFINE(M_MEMDESC, "memdesc", "memory range descriptors");
114 static int mem_ioctl (cdev_t, u_long, caddr_t, int, struct ucred *);
115 static int random_ioctl (cdev_t, u_long, caddr_t, int, struct ucred *);
117 struct mem_range_softc mem_range_softc;
119 static int seedenable;
120 SYSCTL_INT(_kern, OID_AUTO, seedenable, CTLFLAG_RW, &seedenable, 0, "");
122 static int
123 mmopen(struct dev_open_args *ap)
125 cdev_t dev = ap->a_head.a_dev;
126 int error;
128 switch (minor(dev)) {
129 case 0:
130 case 1:
132 * /dev/mem and /dev/kmem
134 if (ap->a_oflags & FWRITE) {
135 if (securelevel > 0 || kernel_mem_readonly)
136 return (EPERM);
138 error = 0;
139 break;
140 case 6:
142 * /dev/kpmap can only be opened for reading.
144 if (ap->a_oflags & FWRITE)
145 return (EPERM);
146 error = 0;
147 break;
148 case 14:
149 error = priv_check_cred(ap->a_cred, PRIV_ROOT, 0);
150 if (error != 0)
151 break;
152 if (securelevel > 0 || kernel_mem_readonly) {
153 error = EPERM;
154 break;
156 error = cpu_set_iopl();
157 break;
158 default:
159 error = 0;
160 break;
162 return (error);
165 static int
166 mmclose(struct dev_close_args *ap)
168 cdev_t dev = ap->a_head.a_dev;
169 int error;
171 switch (minor(dev)) {
172 case 14:
173 error = cpu_clr_iopl();
174 break;
175 default:
176 error = 0;
177 break;
179 return (error);
183 static int
184 mmrw(cdev_t dev, struct uio *uio, int flags)
186 int o;
187 u_int c;
188 u_int poolsize;
189 u_long v;
190 struct iovec *iov;
191 int error = 0;
192 caddr_t buf = NULL;
194 while (uio->uio_resid > 0 && error == 0) {
195 iov = uio->uio_iov;
196 if (iov->iov_len == 0) {
197 uio->uio_iov++;
198 uio->uio_iovcnt--;
199 if (uio->uio_iovcnt < 0)
200 panic("mmrw");
201 continue;
203 switch (minor(dev)) {
204 case 0:
206 * minor device 0 is physical memory, /dev/mem
208 v = uio->uio_offset;
209 v &= ~(long)PAGE_MASK;
210 pmap_kenter((vm_offset_t)ptvmmap, v);
211 o = (int)uio->uio_offset & PAGE_MASK;
212 c = (u_int)(PAGE_SIZE - ((uintptr_t)iov->iov_base & PAGE_MASK));
213 c = min(c, (u_int)(PAGE_SIZE - o));
214 c = min(c, (u_int)iov->iov_len);
215 error = uiomove((caddr_t)&ptvmmap[o], (int)c, uio);
216 pmap_kremove((vm_offset_t)ptvmmap);
217 continue;
219 case 1: {
221 * minor device 1 is kernel memory, /dev/kmem
223 vm_offset_t saddr, eaddr;
224 int prot;
226 c = iov->iov_len;
229 * Make sure that all of the pages are currently
230 * resident so that we don't create any zero-fill
231 * pages.
233 saddr = trunc_page(uio->uio_offset);
234 eaddr = round_page(uio->uio_offset + c);
235 if (saddr > eaddr)
236 return EFAULT;
239 * Make sure the kernel addresses are mapped.
240 * platform_direct_mapped() can be used to bypass
241 * default mapping via the page table (virtual kernels
242 * contain a lot of out-of-band data).
244 prot = VM_PROT_READ;
245 if (uio->uio_rw != UIO_READ)
246 prot |= VM_PROT_WRITE;
247 error = kvm_access_check(saddr, eaddr, prot);
248 if (error)
249 return (error);
250 error = uiomove((caddr_t)(vm_offset_t)uio->uio_offset,
251 (int)c, uio);
252 continue;
254 case 2:
256 * minor device 2 (/dev/null) is EOF/RATHOLE
258 if (uio->uio_rw == UIO_READ)
259 return (0);
260 c = iov->iov_len;
261 break;
262 case 3:
264 * minor device 3 (/dev/random) is source of filth
265 * on read, seeder on write
267 if (buf == NULL)
268 buf = kmalloc(PAGE_SIZE, M_TEMP, M_WAITOK);
269 c = min(iov->iov_len, PAGE_SIZE);
270 if (uio->uio_rw == UIO_WRITE) {
271 error = uiomove(buf, (int)c, uio);
272 if (error == 0 &&
273 seedenable &&
274 securelevel <= 0) {
275 error = add_buffer_randomness_src(buf, c, RAND_SRC_SEEDING);
276 } else if (error == 0) {
277 error = EPERM;
279 } else {
280 poolsize = read_random(buf, c);
281 if (poolsize == 0) {
282 if (buf)
283 kfree(buf, M_TEMP);
284 if ((flags & IO_NDELAY) != 0)
285 return (EWOULDBLOCK);
286 return (0);
288 c = min(c, poolsize);
289 error = uiomove(buf, (int)c, uio);
291 continue;
292 case 4:
294 * minor device 4 (/dev/urandom) is source of muck
295 * on read, writes are disallowed.
297 c = min(iov->iov_len, PAGE_SIZE);
298 if (uio->uio_rw == UIO_WRITE) {
299 error = EPERM;
300 break;
302 if (CURSIG(curthread->td_lwp) != 0) {
304 * Use tsleep() to get the error code right.
305 * It should return immediately.
307 error = tsleep(&rand_bolt, PCATCH, "urand", 1);
308 if (error != 0 && error != EWOULDBLOCK)
309 continue;
311 if (buf == NULL)
312 buf = kmalloc(PAGE_SIZE, M_TEMP, M_WAITOK);
313 poolsize = read_random_unlimited(buf, c);
314 c = min(c, poolsize);
315 error = uiomove(buf, (int)c, uio);
316 continue;
317 /* case 5: read/write not supported, mmap only */
318 /* case 6: read/write not supported, mmap only */
319 case 12:
321 * minor device 12 (/dev/zero) is source of nulls
322 * on read, write are disallowed.
324 if (uio->uio_rw == UIO_WRITE) {
325 c = iov->iov_len;
326 break;
328 if (zbuf == NULL) {
329 zbuf = (caddr_t)kmalloc(PAGE_SIZE, M_TEMP,
330 M_WAITOK | M_ZERO);
332 c = min(iov->iov_len, PAGE_SIZE);
333 error = uiomove(zbuf, (int)c, uio);
334 continue;
335 default:
336 return (ENODEV);
338 if (error)
339 break;
340 iov->iov_base = (char *)iov->iov_base + c;
341 iov->iov_len -= c;
342 uio->uio_offset += c;
343 uio->uio_resid -= c;
345 if (buf)
346 kfree(buf, M_TEMP);
347 return (error);
350 static int
351 mmread(struct dev_read_args *ap)
353 return(mmrw(ap->a_head.a_dev, ap->a_uio, ap->a_ioflag));
356 static int
357 mmwrite(struct dev_write_args *ap)
359 return(mmrw(ap->a_head.a_dev, ap->a_uio, ap->a_ioflag));
362 /*******************************************************\
363 * allow user processes to MMAP some memory sections *
364 * instead of going through read/write *
365 \*******************************************************/
367 static int user_kernel_mapping(int num, vm_ooffset_t offset,
368 vm_ooffset_t *resultp);
370 #if 0
372 static int
373 memmmap(struct dev_mmap_args *ap)
375 cdev_t dev = ap->a_head.a_dev;
376 vm_ooffset_t result;
377 int error;
379 switch (minor(dev)) {
380 case 0:
382 * minor device 0 is physical memory
384 ap->a_result = atop(ap->a_offset);
385 error = 0;
386 break;
387 case 1:
389 * minor device 1 is kernel memory
391 ap->a_result = atop(vtophys(ap->a_offset));
392 error = 0;
393 break;
394 case 5:
395 case 6:
397 * minor device 5 is /dev/upmap (see sys/upmap.h)
398 * minor device 6 is /dev/kpmap (see sys/upmap.h)
400 result = 0;
401 error = user_kernel_mapping(minor(dev), ap->a_offset, &result);
402 ap->a_result = atop(result);
403 break;
404 default:
405 error = EINVAL;
406 break;
408 return error;
411 #endif
413 static int
414 memuksmap(cdev_t dev, vm_page_t fake)
416 vm_ooffset_t result;
417 int error;
419 switch (minor(dev)) {
420 case 0:
422 * minor device 0 is physical memory
424 fake->phys_addr = ptoa(fake->pindex);
425 error = 0;
426 break;
427 case 1:
429 * minor device 1 is kernel memory
431 fake->phys_addr = vtophys(ptoa(fake->pindex));
432 error = 0;
433 break;
434 case 5:
435 case 6:
437 * minor device 5 is /dev/upmap (see sys/upmap.h)
438 * minor device 6 is /dev/kpmap (see sys/upmap.h)
440 result = 0;
441 error = user_kernel_mapping(minor(dev),
442 ptoa(fake->pindex), &result);
443 fake->phys_addr = result;
444 break;
445 default:
446 error = EINVAL;
447 break;
449 return error;
452 static int
453 mmioctl(struct dev_ioctl_args *ap)
455 cdev_t dev = ap->a_head.a_dev;
456 int error;
458 lockmgr(&mem_lock, LK_EXCLUSIVE);
460 switch (minor(dev)) {
461 case 0:
462 error = mem_ioctl(dev, ap->a_cmd, ap->a_data,
463 ap->a_fflag, ap->a_cred);
464 break;
465 case 3:
466 case 4:
467 error = random_ioctl(dev, ap->a_cmd, ap->a_data,
468 ap->a_fflag, ap->a_cred);
469 break;
470 default:
471 error = ENODEV;
472 break;
475 lockmgr(&mem_lock, LK_RELEASE);
477 return (error);
481 * Operations for changing memory attributes.
483 * This is basically just an ioctl shim for mem_range_attr_get
484 * and mem_range_attr_set.
486 static int
487 mem_ioctl(cdev_t dev, u_long cmd, caddr_t data, int flags, struct ucred *cred)
489 int nd, error = 0;
490 struct mem_range_op *mo = (struct mem_range_op *)data;
491 struct mem_range_desc *md;
493 /* is this for us? */
494 if ((cmd != MEMRANGE_GET) &&
495 (cmd != MEMRANGE_SET))
496 return (ENOTTY);
498 /* any chance we can handle this? */
499 if (mem_range_softc.mr_op == NULL)
500 return (EOPNOTSUPP);
502 /* do we have any descriptors? */
503 if (mem_range_softc.mr_ndesc == 0)
504 return (ENXIO);
506 switch (cmd) {
507 case MEMRANGE_GET:
508 nd = imin(mo->mo_arg[0], mem_range_softc.mr_ndesc);
509 if (nd > 0) {
510 md = (struct mem_range_desc *)
511 kmalloc(nd * sizeof(struct mem_range_desc),
512 M_MEMDESC, M_WAITOK);
513 error = mem_range_attr_get(md, &nd);
514 if (!error)
515 error = copyout(md, mo->mo_desc,
516 nd * sizeof(struct mem_range_desc));
517 kfree(md, M_MEMDESC);
518 } else {
519 nd = mem_range_softc.mr_ndesc;
521 mo->mo_arg[0] = nd;
522 break;
524 case MEMRANGE_SET:
525 md = (struct mem_range_desc *)kmalloc(sizeof(struct mem_range_desc),
526 M_MEMDESC, M_WAITOK);
527 error = copyin(mo->mo_desc, md, sizeof(struct mem_range_desc));
528 /* clamp description string */
529 md->mr_owner[sizeof(md->mr_owner) - 1] = 0;
530 if (error == 0)
531 error = mem_range_attr_set(md, &mo->mo_arg[0]);
532 kfree(md, M_MEMDESC);
533 break;
535 return (error);
539 * Implementation-neutral, kernel-callable functions for manipulating
540 * memory range attributes.
543 mem_range_attr_get(struct mem_range_desc *mrd, int *arg)
545 /* can we handle this? */
546 if (mem_range_softc.mr_op == NULL)
547 return (EOPNOTSUPP);
549 if (*arg == 0) {
550 *arg = mem_range_softc.mr_ndesc;
551 } else {
552 bcopy(mem_range_softc.mr_desc, mrd, (*arg) * sizeof(struct mem_range_desc));
554 return (0);
558 mem_range_attr_set(struct mem_range_desc *mrd, int *arg)
560 /* can we handle this? */
561 if (mem_range_softc.mr_op == NULL)
562 return (EOPNOTSUPP);
564 return (mem_range_softc.mr_op->set(&mem_range_softc, mrd, arg));
567 void
568 mem_range_AP_init(void)
570 if (mem_range_softc.mr_op && mem_range_softc.mr_op->initAP)
571 mem_range_softc.mr_op->initAP(&mem_range_softc);
574 static int
575 random_ioctl(cdev_t dev, u_long cmd, caddr_t data, int flags, struct ucred *cred)
577 int error;
578 int intr;
581 * Even inspecting the state is privileged, since it gives a hint
582 * about how easily the randomness might be guessed.
584 error = 0;
586 switch (cmd) {
587 /* Really handled in upper layer */
588 case FIOASYNC:
589 break;
590 case MEM_SETIRQ:
591 intr = *(int16_t *)data;
592 if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0)
593 break;
594 if (intr < 0 || intr >= MAX_INTS)
595 return (EINVAL);
596 register_randintr(intr);
597 break;
598 case MEM_CLEARIRQ:
599 intr = *(int16_t *)data;
600 if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0)
601 break;
602 if (intr < 0 || intr >= MAX_INTS)
603 return (EINVAL);
604 unregister_randintr(intr);
605 break;
606 case MEM_RETURNIRQ:
607 error = ENOTSUP;
608 break;
609 case MEM_FINDIRQ:
610 intr = *(int16_t *)data;
611 if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0)
612 break;
613 if (intr < 0 || intr >= MAX_INTS)
614 return (EINVAL);
615 intr = next_registered_randintr(intr);
616 if (intr == MAX_INTS)
617 return (ENOENT);
618 *(u_int16_t *)data = intr;
619 break;
620 default:
621 error = ENOTSUP;
622 break;
624 return (error);
627 static int
628 mm_filter_read(struct knote *kn, long hint)
630 return (1);
633 static int
634 mm_filter_write(struct knote *kn, long hint)
636 return (1);
639 static void
640 dummy_filter_detach(struct knote *kn) {}
642 /* Implemented in kern_nrandom.c */
643 static struct filterops random_read_filtops =
644 { FILTEROP_ISFD|FILTEROP_MPSAFE, NULL, dummy_filter_detach, random_filter_read };
646 static struct filterops mm_read_filtops =
647 { FILTEROP_ISFD|FILTEROP_MPSAFE, NULL, dummy_filter_detach, mm_filter_read };
649 static struct filterops mm_write_filtops =
650 { FILTEROP_ISFD|FILTEROP_MPSAFE, NULL, dummy_filter_detach, mm_filter_write };
652 static int
653 mmkqfilter(struct dev_kqfilter_args *ap)
655 struct knote *kn = ap->a_kn;
656 cdev_t dev = ap->a_head.a_dev;
658 ap->a_result = 0;
659 switch (kn->kn_filter) {
660 case EVFILT_READ:
661 switch (minor(dev)) {
662 case 3:
663 kn->kn_fop = &random_read_filtops;
664 break;
665 default:
666 kn->kn_fop = &mm_read_filtops;
667 break;
669 break;
670 case EVFILT_WRITE:
671 kn->kn_fop = &mm_write_filtops;
672 break;
673 default:
674 ap->a_result = EOPNOTSUPP;
675 return (0);
678 return (0);
682 iszerodev(cdev_t dev)
684 return (zerodev == dev);
688 * /dev/upmap and /dev/kpmap.
690 static int
691 user_kernel_mapping(int num, vm_ooffset_t offset, vm_ooffset_t *resultp)
693 struct proc *p;
694 int error;
695 int invfork;
697 if ((p = curproc) == NULL)
698 return (EINVAL);
701 * If this is a child currently in vfork the pmap is shared with
702 * the parent! We need to actually set-up the parent's p_upmap,
703 * not the child's, and we need to set the invfork flag. Userland
704 * will probably adjust its static state so it must be consistent
705 * with the parent or userland will be really badly confused.
707 * (this situation can happen when user code in vfork() calls
708 * libc's getpid() or some other function which then decides
709 * it wants the upmap).
711 if (p->p_flags & P_PPWAIT) {
712 p = p->p_pptr;
713 if (p == NULL)
714 return (EINVAL);
715 invfork = 1;
716 } else {
717 invfork = 0;
720 error = EINVAL;
722 switch(num) {
723 case 5:
725 * /dev/upmap - maps RW per-process shared user-kernel area.
727 if (p->p_upmap == NULL)
728 proc_usermap(p, invfork);
729 else if (invfork)
730 p->p_upmap->invfork = invfork;
732 if (p->p_upmap &&
733 offset < roundup2(sizeof(*p->p_upmap), PAGE_SIZE)) {
734 /* only good for current process */
735 *resultp = pmap_kextract((vm_offset_t)p->p_upmap +
736 offset);
737 error = 0;
739 break;
740 case 6:
742 * /dev/kpmap - maps RO shared kernel global page
744 if (kpmap &&
745 offset < roundup2(sizeof(*kpmap), PAGE_SIZE)) {
746 *resultp = pmap_kextract((vm_offset_t)kpmap +
747 offset);
748 error = 0;
750 break;
751 default:
752 break;
754 return error;
757 static void
758 mem_drvinit(void *unused)
761 /* Initialise memory range handling */
762 if (mem_range_softc.mr_op != NULL)
763 mem_range_softc.mr_op->init(&mem_range_softc);
765 make_dev(&mem_ops, 0, UID_ROOT, GID_KMEM, 0640, "mem");
766 make_dev(&mem_ops, 1, UID_ROOT, GID_KMEM, 0640, "kmem");
767 make_dev(&mem_ops, 2, UID_ROOT, GID_WHEEL, 0666, "null");
768 make_dev(&mem_ops, 3, UID_ROOT, GID_WHEEL, 0644, "random");
769 make_dev(&mem_ops, 4, UID_ROOT, GID_WHEEL, 0644, "urandom");
770 make_dev(&mem_ops, 5, UID_ROOT, GID_WHEEL, 0666, "upmap");
771 make_dev(&mem_ops, 6, UID_ROOT, GID_WHEEL, 0444, "kpmap");
772 zerodev = make_dev(&mem_ops, 12, UID_ROOT, GID_WHEEL, 0666, "zero");
773 make_dev(&mem_ops_noq, 14, UID_ROOT, GID_WHEEL, 0600, "io");
776 SYSINIT(memdev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE + CDEV_MAJOR, mem_drvinit,
777 NULL);