drm: Fix debug messages issues introduced by the Linux 4.9 update
[dragonfly.git] / sys / kern / kern_memio.c
CommitLineData
984263bc
MD
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.
dc71b7ab 19 * 3. Neither the name of the University nor the names of its contributors
984263bc
MD
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
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.
34 *
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 $
38 */
39
40/*
41 * Memory special file
42 */
43
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>
446b4829 50#include <sys/interrupt.h>
984263bc
MD
51#include <sys/kernel.h>
52#include <sys/malloc.h>
53#include <sys/memrange.h>
54#include <sys/proc.h>
895c1f85 55#include <sys/priv.h>
64b5a8a5 56#include <sys/queue.h>
984263bc
MD
57#include <sys/random.h>
58#include <sys/signalvar.h>
59#include <sys/uio.h>
60#include <sys/vnode.h>
cf9961f7 61#include <sys/sysctl.h>
984263bc 62
9f889dc4 63#include <sys/signal2.h>
64b5a8a5 64#include <sys/spinlock2.h>
9f889dc4 65
984263bc
MD
66#include <vm/vm.h>
67#include <vm/pmap.h>
64b5a8a5 68#include <vm/vm_map.h>
984263bc
MD
69#include <vm/vm_extern.h>
70
71
72static d_open_t mmopen;
73static d_close_t mmclose;
fef8985e
MD
74static d_read_t mmread;
75static d_write_t mmwrite;
984263bc 76static d_ioctl_t mmioctl;
0adbcbd6 77#if 0
984263bc 78static d_mmap_t memmmap;
0adbcbd6 79#endif
6dcd6ba9 80static d_kqfilter_t mmkqfilter;
64b5a8a5 81static int memuksmap(vm_map_backing_t ba, int op, cdev_t dev, vm_page_t fake);
984263bc
MD
82
83#define CDEV_MAJOR 2
fef8985e 84static struct dev_ops mem_ops = {
0c5ceff2
MD
85 { "mem", 0, D_MPSAFE | D_QUICK },
86 .d_open = mmopen,
87 .d_close = mmclose,
88 .d_read = mmread,
89 .d_write = mmwrite,
90 .d_ioctl = mmioctl,
91 .d_kqfilter = mmkqfilter,
92#if 0
93 .d_mmap = memmmap,
94#endif
95 .d_uksmap = memuksmap
96};
97
7e81bb37
IV
98static struct dev_ops mem_ops_mem = {
99 { "mem", 0, D_MEM | D_MPSAFE | D_QUICK },
100 .d_open = mmopen,
101 .d_close = mmclose,
102 .d_read = mmread,
103 .d_write = mmwrite,
104 .d_ioctl = mmioctl,
105 .d_kqfilter = mmkqfilter,
106#if 0
107 .d_mmap = memmmap,
108#endif
109 .d_uksmap = memuksmap
110};
111
0c5ceff2 112static struct dev_ops mem_ops_noq = {
88abd8b5 113 { "mem", 0, D_MPSAFE },
fef8985e
MD
114 .d_open = mmopen,
115 .d_close = mmclose,
116 .d_read = mmread,
117 .d_write = mmwrite,
118 .d_ioctl = mmioctl,
6dcd6ba9 119 .d_kqfilter = mmkqfilter,
0adbcbd6 120#if 0
fef8985e 121 .d_mmap = memmmap,
0adbcbd6
MD
122#endif
123 .d_uksmap = memuksmap
984263bc
MD
124};
125
7e071e7a 126static int rand_bolt;
984263bc 127static caddr_t zbuf;
b9a1f8e1 128static cdev_t zerodev = NULL;
5a4bb8ec 129static struct lock mem_lock = LOCK_INITIALIZER("memlk", 0, 0);
984263bc
MD
130
131MALLOC_DEFINE(M_MEMDESC, "memdesc", "memory range descriptors");
b13267a5
MD
132static int mem_ioctl (cdev_t, u_long, caddr_t, int, struct ucred *);
133static int random_ioctl (cdev_t, u_long, caddr_t, int, struct ucred *);
984263bc
MD
134
135struct mem_range_softc mem_range_softc;
136
cf9961f7
MD
137static int seedenable;
138SYSCTL_INT(_kern, OID_AUTO, seedenable, CTLFLAG_RW, &seedenable, 0, "");
984263bc
MD
139
140static int
fef8985e 141mmopen(struct dev_open_args *ap)
984263bc 142{
b13267a5 143 cdev_t dev = ap->a_head.a_dev;
fef8985e 144 int error;
41c20dac 145
984263bc 146 switch (minor(dev)) {
fef8985e
MD
147 case 0:
148 case 1:
12081e87
MD
149 /*
150 * /dev/mem and /dev/kmem
151 */
7e42c007
MD
152 if (ap->a_oflags & FWRITE) {
153 if (securelevel > 0 || kernel_mem_readonly)
154 return (EPERM);
155 }
fc26c9f1 156 error = 0;
fef8985e 157 break;
12081e87
MD
158 case 6:
159 /*
160 * /dev/kpmap can only be opened for reading.
161 */
162 if (ap->a_oflags & FWRITE)
163 return (EPERM);
164 error = 0;
165 break;
984263bc 166 case 14:
895c1f85 167 error = priv_check_cred(ap->a_cred, PRIV_ROOT, 0);
fef8985e 168 if (error != 0)
fc26c9f1 169 break;
7e42c007 170 if (securelevel > 0 || kernel_mem_readonly) {
fc26c9f1
MD
171 error = EPERM;
172 break;
173 }
174 error = cpu_set_iopl();
984263bc
MD
175 break;
176 default:
fc26c9f1 177 error = 0;
984263bc
MD
178 break;
179 }
fc26c9f1 180 return (error);
984263bc
MD
181}
182
183static int
fef8985e 184mmclose(struct dev_close_args *ap)
984263bc 185{
b13267a5 186 cdev_t dev = ap->a_head.a_dev;
fc26c9f1 187 int error;
984263bc
MD
188
189 switch (minor(dev)) {
984263bc 190 case 14:
fc26c9f1 191 error = cpu_clr_iopl();
984263bc
MD
192 break;
193 default:
fc26c9f1 194 error = 0;
984263bc
MD
195 break;
196 }
fc26c9f1 197 return (error);
984263bc
MD
198}
199
fef8985e 200
984263bc 201static int
b13267a5 202mmrw(cdev_t dev, struct uio *uio, int flags)
984263bc 203{
c9faf524 204 int o;
74fa35b9 205 u_int c;
984263bc 206 u_int poolsize;
74fa35b9 207 u_long v;
c9faf524 208 struct iovec *iov;
984263bc
MD
209 int error = 0;
210 caddr_t buf = NULL;
211
212 while (uio->uio_resid > 0 && error == 0) {
213 iov = uio->uio_iov;
214 if (iov->iov_len == 0) {
215 uio->uio_iov++;
216 uio->uio_iovcnt--;
217 if (uio->uio_iovcnt < 0)
218 panic("mmrw");
219 continue;
220 }
221 switch (minor(dev)) {
984263bc 222 case 0:
207bedb9
MD
223 /*
224 * minor device 0 is physical memory, /dev/mem
225 */
984263bc 226 v = uio->uio_offset;
74fa35b9 227 v &= ~(long)PAGE_MASK;
984263bc
MD
228 pmap_kenter((vm_offset_t)ptvmmap, v);
229 o = (int)uio->uio_offset & PAGE_MASK;
973c11b9 230 c = (u_int)(PAGE_SIZE - ((uintptr_t)iov->iov_base & PAGE_MASK));
984263bc
MD
231 c = min(c, (u_int)(PAGE_SIZE - o));
232 c = min(c, (u_int)iov->iov_len);
233 error = uiomove((caddr_t)&ptvmmap[o], (int)c, uio);
234 pmap_kremove((vm_offset_t)ptvmmap);
235 continue;
236
984263bc 237 case 1: {
207bedb9
MD
238 /*
239 * minor device 1 is kernel memory, /dev/kmem
240 */
c439ad8f 241 vm_offset_t saddr, eaddr;
460426e6
MD
242 int prot;
243
984263bc
MD
244 c = iov->iov_len;
245
246 /*
03debc4a
MD
247 * Make sure that all of the pages are currently
248 * resident so that we don't create any zero-fill
249 * pages.
984263bc 250 */
c439ad8f 251 saddr = trunc_page(uio->uio_offset);
984263bc 252 eaddr = round_page(uio->uio_offset + c);
460426e6 253 if (saddr > eaddr)
984263bc 254 return EFAULT;
460426e6
MD
255
256 /*
257 * Make sure the kernel addresses are mapped.
258 * platform_direct_mapped() can be used to bypass
259 * default mapping via the page table (virtual kernels
260 * contain a lot of out-of-band data).
261 */
262 prot = VM_PROT_READ;
263 if (uio->uio_rw != UIO_READ)
264 prot |= VM_PROT_WRITE;
265 error = kvm_access_check(saddr, eaddr, prot);
266 if (error)
267 return (error);
268 error = uiomove((caddr_t)(vm_offset_t)uio->uio_offset,
269 (int)c, uio);
984263bc
MD
270 continue;
271 }
984263bc 272 case 2:
207bedb9 273 /*
2ca4923a 274 * minor device 2 (/dev/null) is EOF/RATHOLE
207bedb9 275 */
984263bc
MD
276 if (uio->uio_rw == UIO_READ)
277 return (0);
278 c = iov->iov_len;
279 break;
984263bc 280 case 3:
207bedb9
MD
281 /*
282 * minor device 3 (/dev/random) is source of filth
c26b3d4d 283 * on read, seeder on write
207bedb9 284 */
984263bc 285 if (buf == NULL)
efda3bd0 286 buf = kmalloc(PAGE_SIZE, M_TEMP, M_WAITOK);
984263bc 287 c = min(iov->iov_len, PAGE_SIZE);
c26b3d4d
MD
288 if (uio->uio_rw == UIO_WRITE) {
289 error = uiomove(buf, (int)c, uio);
cf9961f7
MD
290 if (error == 0 &&
291 seedenable &&
292 securelevel <= 0) {
790110cd 293 error = add_buffer_randomness_src(buf, c, RAND_SRC_SEEDING);
cf9961f7
MD
294 } else if (error == 0) {
295 error = EPERM;
296 }
c26b3d4d
MD
297 } else {
298 poolsize = read_random(buf, c);
299 if (poolsize == 0) {
300 if (buf)
efda3bd0 301 kfree(buf, M_TEMP);
c26b3d4d
MD
302 if ((flags & IO_NDELAY) != 0)
303 return (EWOULDBLOCK);
304 return (0);
305 }
306 c = min(c, poolsize);
307 error = uiomove(buf, (int)c, uio);
984263bc 308 }
984263bc 309 continue;
984263bc 310 case 4:
207bedb9
MD
311 /*
312 * minor device 4 (/dev/urandom) is source of muck
c26b3d4d 313 * on read, writes are disallowed.
207bedb9 314 */
c26b3d4d 315 c = min(iov->iov_len, PAGE_SIZE);
984263bc 316 if (uio->uio_rw == UIO_WRITE) {
c26b3d4d 317 error = EPERM;
984263bc
MD
318 break;
319 }
08f2f1bb 320 if (CURSIG(curthread->td_lwp) != 0) {
984263bc
MD
321 /*
322 * Use tsleep() to get the error code right.
323 * It should return immediately.
324 */
377d4740 325 error = tsleep(&rand_bolt, PCATCH, "urand", 1);
984263bc
MD
326 if (error != 0 && error != EWOULDBLOCK)
327 continue;
328 }
329 if (buf == NULL)
efda3bd0 330 buf = kmalloc(PAGE_SIZE, M_TEMP, M_WAITOK);
984263bc
MD
331 poolsize = read_random_unlimited(buf, c);
332 c = min(c, poolsize);
333 error = uiomove(buf, (int)c, uio);
334 continue;
0adbcbd6
MD
335 /* case 5: read/write not supported, mmap only */
336 /* case 6: read/write not supported, mmap only */
984263bc 337 case 12:
207bedb9
MD
338 /*
339 * minor device 12 (/dev/zero) is source of nulls
c26b3d4d 340 * on read, write are disallowed.
207bedb9 341 */
984263bc
MD
342 if (uio->uio_rw == UIO_WRITE) {
343 c = iov->iov_len;
344 break;
345 }
346 if (zbuf == NULL) {
e7b4468c
SW
347 zbuf = (caddr_t)kmalloc(PAGE_SIZE, M_TEMP,
348 M_WAITOK | M_ZERO);
984263bc
MD
349 }
350 c = min(iov->iov_len, PAGE_SIZE);
351 error = uiomove(zbuf, (int)c, uio);
352 continue;
984263bc
MD
353 default:
354 return (ENODEV);
355 }
356 if (error)
357 break;
656849c6 358 iov->iov_base = (char *)iov->iov_base + c;
984263bc
MD
359 iov->iov_len -= c;
360 uio->uio_offset += c;
361 uio->uio_resid -= c;
362 }
363 if (buf)
efda3bd0 364 kfree(buf, M_TEMP);
984263bc
MD
365 return (error);
366}
367
fef8985e
MD
368static int
369mmread(struct dev_read_args *ap)
370{
371 return(mmrw(ap->a_head.a_dev, ap->a_uio, ap->a_ioflag));
372}
373
374static int
375mmwrite(struct dev_write_args *ap)
376{
377 return(mmrw(ap->a_head.a_dev, ap->a_uio, ap->a_ioflag));
378}
379
984263bc
MD
380/*******************************************************\
381* allow user processes to MMAP some memory sections *
382* instead of going through read/write *
383\*******************************************************/
fef8985e 384
64b5a8a5
MD
385static int user_kernel_mapping(vm_map_backing_t ba, int num,
386 vm_ooffset_t offset, vm_ooffset_t *resultp);
0adbcbd6 387
984263bc 388static int
64b5a8a5 389memuksmap(vm_map_backing_t ba, int op, cdev_t dev, vm_page_t fake)
984263bc 390{
0adbcbd6
MD
391 vm_ooffset_t result;
392 int error;
64b5a8a5 393 struct lwp *lp;
984263bc 394
64b5a8a5 395 error = 0;
0adbcbd6 396
64b5a8a5
MD
397 switch(op) {
398 case UKSMAPOP_ADD:
0adbcbd6 399 /*
4aa6d05c
MD
400 * We only need to track mappings for /dev/lpmap, all process
401 * mappings will be deleted when the process exits and we
402 * do not need to track kernel mappings.
0adbcbd6 403 */
4aa6d05c
MD
404 if (minor(dev) == 7) {
405 lp = ba->aux_info;
406 spin_lock(&lp->lwp_spin);
407 TAILQ_INSERT_TAIL(&lp->lwp_lpmap_backing_list,
408 ba, entry);
409 spin_unlock(&lp->lwp_spin);
410 }
0adbcbd6 411 break;
64b5a8a5 412 case UKSMAPOP_REM:
0adbcbd6 413 /*
4aa6d05c
MD
414 * We only need to track mappings for /dev/lpmap, all process
415 * mappings will be deleted when the process exits and we
416 * do not need to track kernel mappings.
0adbcbd6 417 */
4aa6d05c
MD
418 if (minor(dev) == 7) {
419 lp = ba->aux_info;
64b5a8a5
MD
420 spin_lock(&lp->lwp_spin);
421 TAILQ_REMOVE(&lp->lwp_lpmap_backing_list, ba, entry);
64b5a8a5 422 spin_unlock(&lp->lwp_spin);
64b5a8a5 423 }
0adbcbd6 424 break;
64b5a8a5
MD
425 case UKSMAPOP_FAULT:
426 switch (minor(dev)) {
427 case 0:
428 /*
429 * minor device 0 is physical memory
430 */
431 fake->phys_addr = ptoa(fake->pindex);
432 break;
433 case 1:
434 /*
435 * minor device 1 is kernel memory
436 */
437 fake->phys_addr = vtophys(ptoa(fake->pindex));
438 break;
439 case 5:
440 case 6:
441 case 7:
442 /*
443 * minor device 5 is /dev/upmap (see sys/upmap.h)
444 * minor device 6 is /dev/kpmap (see sys/upmap.h)
445 * minor device 7 is /dev/lpmap (see sys/upmap.h)
446 */
447 result = 0;
448 error = user_kernel_mapping(ba,
449 minor(dev),
450 ptoa(fake->pindex),
451 &result);
452 fake->phys_addr = result;
453 break;
454 default:
455 error = EINVAL;
456 break;
457 }
0adbcbd6 458 break;
984263bc 459 default:
0adbcbd6
MD
460 error = EINVAL;
461 break;
984263bc 462 }
0adbcbd6 463 return error;
984263bc
MD
464}
465
466static int
fef8985e 467mmioctl(struct dev_ioctl_args *ap)
984263bc 468{
b13267a5 469 cdev_t dev = ap->a_head.a_dev;
9f889dc4
MD
470 int error;
471
5a4bb8ec 472 lockmgr(&mem_lock, LK_EXCLUSIVE);
984263bc
MD
473
474 switch (minor(dev)) {
475 case 0:
9f889dc4
MD
476 error = mem_ioctl(dev, ap->a_cmd, ap->a_data,
477 ap->a_fflag, ap->a_cred);
478 break;
984263bc
MD
479 case 3:
480 case 4:
9f889dc4
MD
481 error = random_ioctl(dev, ap->a_cmd, ap->a_data,
482 ap->a_fflag, ap->a_cred);
483 break;
484 default:
485 error = ENODEV;
486 break;
984263bc 487 }
9f889dc4 488
5a4bb8ec
MD
489 lockmgr(&mem_lock, LK_RELEASE);
490
9f889dc4 491 return (error);
984263bc
MD
492}
493
494/*
495 * Operations for changing memory attributes.
496 *
497 * This is basically just an ioctl shim for mem_range_attr_get
498 * and mem_range_attr_set.
499 */
500static int
b13267a5 501mem_ioctl(cdev_t dev, u_long cmd, caddr_t data, int flags, struct ucred *cred)
984263bc
MD
502{
503 int nd, error = 0;
504 struct mem_range_op *mo = (struct mem_range_op *)data;
505 struct mem_range_desc *md;
506
507 /* is this for us? */
508 if ((cmd != MEMRANGE_GET) &&
509 (cmd != MEMRANGE_SET))
510 return (ENOTTY);
511
512 /* any chance we can handle this? */
513 if (mem_range_softc.mr_op == NULL)
514 return (EOPNOTSUPP);
515
516 /* do we have any descriptors? */
517 if (mem_range_softc.mr_ndesc == 0)
518 return (ENXIO);
519
520 switch (cmd) {
521 case MEMRANGE_GET:
522 nd = imin(mo->mo_arg[0], mem_range_softc.mr_ndesc);
523 if (nd > 0) {
524 md = (struct mem_range_desc *)
77652cad 525 kmalloc(nd * sizeof(struct mem_range_desc),
984263bc
MD
526 M_MEMDESC, M_WAITOK);
527 error = mem_range_attr_get(md, &nd);
528 if (!error)
529 error = copyout(md, mo->mo_desc,
530 nd * sizeof(struct mem_range_desc));
efda3bd0 531 kfree(md, M_MEMDESC);
984263bc
MD
532 } else {
533 nd = mem_range_softc.mr_ndesc;
534 }
535 mo->mo_arg[0] = nd;
536 break;
537
538 case MEMRANGE_SET:
77652cad 539 md = (struct mem_range_desc *)kmalloc(sizeof(struct mem_range_desc),
984263bc
MD
540 M_MEMDESC, M_WAITOK);
541 error = copyin(mo->mo_desc, md, sizeof(struct mem_range_desc));
542 /* clamp description string */
543 md->mr_owner[sizeof(md->mr_owner) - 1] = 0;
544 if (error == 0)
545 error = mem_range_attr_set(md, &mo->mo_arg[0]);
efda3bd0 546 kfree(md, M_MEMDESC);
984263bc
MD
547 break;
548 }
549 return (error);
550}
551
552/*
553 * Implementation-neutral, kernel-callable functions for manipulating
554 * memory range attributes.
555 */
556int
f123d5a1 557mem_range_attr_get(struct mem_range_desc *mrd, int *arg)
984263bc
MD
558{
559 /* can we handle this? */
560 if (mem_range_softc.mr_op == NULL)
561 return (EOPNOTSUPP);
562
563 if (*arg == 0) {
564 *arg = mem_range_softc.mr_ndesc;
565 } else {
566 bcopy(mem_range_softc.mr_desc, mrd, (*arg) * sizeof(struct mem_range_desc));
567 }
568 return (0);
569}
570
571int
f123d5a1 572mem_range_attr_set(struct mem_range_desc *mrd, int *arg)
984263bc
MD
573{
574 /* can we handle this? */
575 if (mem_range_softc.mr_op == NULL)
576 return (EOPNOTSUPP);
577
578 return (mem_range_softc.mr_op->set(&mem_range_softc, mrd, arg));
579}
580
984263bc
MD
581void
582mem_range_AP_init(void)
583{
584 if (mem_range_softc.mr_op && mem_range_softc.mr_op->initAP)
d63cf994 585 mem_range_softc.mr_op->initAP(&mem_range_softc);
984263bc 586}
984263bc
MD
587
588static int
b13267a5 589random_ioctl(cdev_t dev, u_long cmd, caddr_t data, int flags, struct ucred *cred)
984263bc 590{
5f456c40
MD
591 int error;
592 int intr;
984263bc
MD
593
594 /*
984263bc
MD
595 * Even inspecting the state is privileged, since it gives a hint
596 * about how easily the randomness might be guessed.
597 */
5f456c40
MD
598 error = 0;
599
984263bc
MD
600 switch (cmd) {
601 /* Really handled in upper layer */
602 case FIOASYNC:
984263bc
MD
603 break;
604 case MEM_SETIRQ:
5f456c40 605 intr = *(int16_t *)data;
895c1f85 606 if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0)
984263bc 607 break;
5f456c40
MD
608 if (intr < 0 || intr >= MAX_INTS)
609 return (EINVAL);
7e071e7a 610 register_randintr(intr);
984263bc
MD
611 break;
612 case MEM_CLEARIRQ:
5f456c40 613 intr = *(int16_t *)data;
895c1f85 614 if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0)
984263bc 615 break;
5f456c40
MD
616 if (intr < 0 || intr >= MAX_INTS)
617 return (EINVAL);
7e071e7a 618 unregister_randintr(intr);
984263bc
MD
619 break;
620 case MEM_RETURNIRQ:
5f456c40
MD
621 error = ENOTSUP;
622 break;
623 case MEM_FINDIRQ:
624 intr = *(int16_t *)data;
895c1f85 625 if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0)
5f456c40
MD
626 break;
627 if (intr < 0 || intr >= MAX_INTS)
628 return (EINVAL);
629 intr = next_registered_randintr(intr);
630 if (intr == MAX_INTS)
631 return (ENOENT);
632 *(u_int16_t *)data = intr;
633 break;
634 default:
635 error = ENOTSUP;
984263bc
MD
636 break;
637 }
5f456c40 638 return (error);
984263bc
MD
639}
640
6dcd6ba9
SG
641static int
642mm_filter_read(struct knote *kn, long hint)
643{
644 return (1);
645}
646
2ca4923a
MD
647static int
648mm_filter_write(struct knote *kn, long hint)
649{
650 return (1);
651}
652
6dcd6ba9
SG
653static void
654dummy_filter_detach(struct knote *kn) {}
655
a081e067 656/* Implemented in kern_nrandom.c */
6dcd6ba9 657static struct filterops random_read_filtops =
a081e067 658 { FILTEROP_ISFD|FILTEROP_MPSAFE, NULL, dummy_filter_detach, random_filter_read };
6dcd6ba9
SG
659
660static struct filterops mm_read_filtops =
a081e067 661 { FILTEROP_ISFD|FILTEROP_MPSAFE, NULL, dummy_filter_detach, mm_filter_read };
6dcd6ba9 662
2ca4923a 663static struct filterops mm_write_filtops =
a081e067 664 { FILTEROP_ISFD|FILTEROP_MPSAFE, NULL, dummy_filter_detach, mm_filter_write };
2ca4923a 665
59b728a7 666static int
6dcd6ba9
SG
667mmkqfilter(struct dev_kqfilter_args *ap)
668{
669 struct knote *kn = ap->a_kn;
670 cdev_t dev = ap->a_head.a_dev;
671
672 ap->a_result = 0;
673 switch (kn->kn_filter) {
674 case EVFILT_READ:
675 switch (minor(dev)) {
676 case 3:
677 kn->kn_fop = &random_read_filtops;
678 break;
679 default:
680 kn->kn_fop = &mm_read_filtops;
681 break;
682 }
683 break;
2ca4923a
MD
684 case EVFILT_WRITE:
685 kn->kn_fop = &mm_write_filtops;
686 break;
6dcd6ba9 687 default:
b287d649 688 ap->a_result = EOPNOTSUPP;
6dcd6ba9
SG
689 return (0);
690 }
691
692 return (0);
693}
694
984263bc 695int
f123d5a1 696iszerodev(cdev_t dev)
984263bc 697{
964d9977 698 return (zerodev == dev);
984263bc
MD
699}
700
0adbcbd6 701/*
64b5a8a5 702 * /dev/lpmap, /dev/upmap, /dev/kpmap.
0adbcbd6
MD
703 */
704static int
64b5a8a5
MD
705user_kernel_mapping(vm_map_backing_t ba, int num, vm_ooffset_t offset,
706 vm_ooffset_t *resultp)
0adbcbd6 707{
87116512 708 struct proc *p;
64b5a8a5 709 struct lwp *lp;
0adbcbd6 710 int error;
87116512 711 int invfork;
0adbcbd6 712
64b5a8a5 713 if (offset < 0)
0adbcbd6 714 return (EINVAL);
87116512 715
0adbcbd6
MD
716 error = EINVAL;
717
718 switch(num) {
719 case 5:
720 /*
721 * /dev/upmap - maps RW per-process shared user-kernel area.
722 */
4aa6d05c
MD
723
724 /*
725 * If this is a child currently in vfork the pmap is shared
726 * with the parent! We need to actually set-up the parent's
727 * p_upmap, not the child's, and we need to set the invfork
728 * flag. Userland will probably adjust its static state so
729 * it must be consistent with the parent or userland will be
730 * really badly confused.
731 *
732 * (this situation can happen when user code in vfork() calls
733 * libc's getpid() or some other function which then decides
734 * it wants the upmap).
735 */
736 p = ba->aux_info;
737 if (p == NULL)
738 break;
739 if (p->p_flags & P_PPWAIT) {
740 p = p->p_pptr;
741 if (p == NULL)
742 return (EINVAL);
743 invfork = 1;
744 } else {
745 invfork = 0;
746 }
747
748 /*
749 * Create the kernel structure as required, set the invfork
750 * flag if we are faulting in on a vfork().
751 */
0adbcbd6 752 if (p->p_upmap == NULL)
87116512 753 proc_usermap(p, invfork);
4aa6d05c 754 if (p->p_upmap && invfork)
87116512
MD
755 p->p_upmap->invfork = invfork;
756
4aa6d05c
MD
757 /*
758 * Extract address for pmap
759 */
12081e87
MD
760 if (p->p_upmap &&
761 offset < roundup2(sizeof(*p->p_upmap), PAGE_SIZE)) {
0adbcbd6 762 /* only good for current process */
12081e87
MD
763 *resultp = pmap_kextract((vm_offset_t)p->p_upmap +
764 offset);
0adbcbd6
MD
765 error = 0;
766 }
767 break;
768 case 6:
769 /*
770 * /dev/kpmap - maps RO shared kernel global page
4aa6d05c
MD
771 *
772 * Extract address for pmap
0adbcbd6 773 */
12081e87
MD
774 if (kpmap &&
775 offset < roundup2(sizeof(*kpmap), PAGE_SIZE)) {
4aa6d05c 776 *resultp = pmap_kextract((vm_offset_t)kpmap + offset);
0adbcbd6
MD
777 error = 0;
778 }
779 break;
64b5a8a5
MD
780 case 7:
781 /*
782 * /dev/lpmap - maps RW per-thread shared user-kernel area.
64b5a8a5 783 */
4aa6d05c
MD
784 lp = ba->aux_info;
785 if (lp == NULL)
64b5a8a5 786 break;
64b5a8a5
MD
787
788 /*
4aa6d05c 789 * Create the kernel structure as required
64b5a8a5
MD
790 */
791 if (lp->lwp_lpmap == NULL)
4aa6d05c 792 lwp_usermap(lp, -1); /* second arg not yet XXX */
64b5a8a5 793
4aa6d05c
MD
794 /*
795 * Extract address for pmap
796 */
64b5a8a5
MD
797 if (lp->lwp_lpmap &&
798 offset < roundup2(sizeof(*lp->lwp_lpmap), PAGE_SIZE)) {
799 /* only good for current process */
800 *resultp = pmap_kextract((vm_offset_t)lp->lwp_lpmap +
801 offset);
802 error = 0;
803 }
64b5a8a5 804 break;
0adbcbd6
MD
805 default:
806 break;
807 }
808 return error;
809}
810
984263bc
MD
811static void
812mem_drvinit(void *unused)
813{
814
815 /* Initialise memory range handling */
816 if (mem_range_softc.mr_op != NULL)
817 mem_range_softc.mr_op->init(&mem_range_softc);
818
7e81bb37
IV
819 make_dev(&mem_ops_mem, 0, UID_ROOT, GID_KMEM, 0640, "mem");
820 make_dev(&mem_ops_mem, 1, UID_ROOT, GID_KMEM, 0640, "kmem");
fef8985e
MD
821 make_dev(&mem_ops, 2, UID_ROOT, GID_WHEEL, 0666, "null");
822 make_dev(&mem_ops, 3, UID_ROOT, GID_WHEEL, 0644, "random");
823 make_dev(&mem_ops, 4, UID_ROOT, GID_WHEEL, 0644, "urandom");
0adbcbd6
MD
824 make_dev(&mem_ops, 5, UID_ROOT, GID_WHEEL, 0666, "upmap");
825 make_dev(&mem_ops, 6, UID_ROOT, GID_WHEEL, 0444, "kpmap");
64b5a8a5 826 make_dev(&mem_ops, 7, UID_ROOT, GID_WHEEL, 0666, "lpmap");
b9a1f8e1 827 zerodev = make_dev(&mem_ops, 12, UID_ROOT, GID_WHEEL, 0666, "zero");
0c5ceff2 828 make_dev(&mem_ops_noq, 14, UID_ROOT, GID_WHEEL, 0600, "io");
984263bc
MD
829}
830
f3f3eadb
SW
831SYSINIT(memdev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE + CDEV_MAJOR, mem_drvinit,
832 NULL);
984263bc 833