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