510818236e541defd18fd81bbe530a51b1ca0c1b
[dragonfly.git] / sys / kern / kern_memio.c
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.
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.
26  *
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
37  * SUCH DAMAGE.
38  *
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.27 2007/01/12 03:05:49 dillon Exp $
43  */
44
45 /*
46  * Memory special file
47  */
48
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/buf.h>
52 #include <sys/conf.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>
59 #include <sys/proc.h>
60 #include <sys/random.h>
61 #include <sys/signalvar.h>
62 #include <sys/uio.h>
63 #include <sys/vnode.h>
64
65 #include <vm/vm.h>
66 #include <vm/pmap.h>
67 #include <vm/vm_extern.h>
68
69
70 static  d_open_t        mmopen;
71 static  d_close_t       mmclose;
72 static  d_read_t        mmread;
73 static  d_write_t       mmwrite;
74 static  d_ioctl_t       mmioctl;
75 static  d_mmap_t        memmmap;
76 static  d_poll_t        mmpoll;
77
78 #define CDEV_MAJOR 2
79 static struct dev_ops mem_ops = {
80         { "mem", CDEV_MAJOR, D_MEM },
81         .d_open =       mmopen,
82         .d_close =      mmclose,
83         .d_read =       mmread,
84         .d_write =      mmwrite,
85         .d_ioctl =      mmioctl,
86         .d_poll =       mmpoll,
87         .d_mmap =       memmmap,
88 };
89
90 static int rand_bolt;
91 static caddr_t  zbuf;
92
93 MALLOC_DEFINE(M_MEMDESC, "memdesc", "memory range descriptors");
94 static int mem_ioctl (cdev_t, u_long, caddr_t, int, struct ucred *);
95 static int random_ioctl (cdev_t, u_long, caddr_t, int, struct ucred *);
96
97 struct mem_range_softc mem_range_softc;
98
99
100 static int
101 mmopen(struct dev_open_args *ap)
102 {
103         cdev_t dev = ap->a_head.a_dev;
104         int error;
105
106         switch (minor(dev)) {
107         case 0:
108         case 1:
109                 if ((ap->a_oflags & FWRITE) && securelevel > 0)
110                         return (EPERM);
111                 error = 0;
112                 break;
113         case 14:
114                 error = suser_cred(ap->a_cred, 0);
115                 if (error != 0)
116                         break;
117                 if (securelevel > 0) {
118                         error = EPERM;
119                         break;
120                 }
121                 error = cpu_set_iopl();
122                 break;
123         default:
124                 error = 0;
125                 break;
126         }
127         return (error);
128 }
129
130 static int
131 mmclose(struct dev_close_args *ap)
132 {
133         cdev_t dev = ap->a_head.a_dev;
134         int error;
135
136         switch (minor(dev)) {
137         case 14:
138                 error = cpu_clr_iopl();
139                 break;
140         default:
141                 error = 0;
142                 break;
143         }
144         return (error);
145 }
146
147
148 static int
149 mmrw(cdev_t dev, struct uio *uio, int flags)
150 {
151         int o;
152         u_int c, v;
153         u_int poolsize;
154         struct iovec *iov;
155         int error = 0;
156         caddr_t buf = NULL;
157
158         while (uio->uio_resid > 0 && error == 0) {
159                 iov = uio->uio_iov;
160                 if (iov->iov_len == 0) {
161                         uio->uio_iov++;
162                         uio->uio_iovcnt--;
163                         if (uio->uio_iovcnt < 0)
164                                 panic("mmrw");
165                         continue;
166                 }
167                 switch (minor(dev)) {
168                 case 0:
169                         /*
170                          * minor device 0 is physical memory, /dev/mem 
171                          */
172                         v = uio->uio_offset;
173                         v &= ~PAGE_MASK;
174                         pmap_kenter((vm_offset_t)ptvmmap, v);
175                         o = (int)uio->uio_offset & PAGE_MASK;
176                         c = (u_int)(PAGE_SIZE - ((int)iov->iov_base & PAGE_MASK));
177                         c = min(c, (u_int)(PAGE_SIZE - o));
178                         c = min(c, (u_int)iov->iov_len);
179                         error = uiomove((caddr_t)&ptvmmap[o], (int)c, uio);
180                         pmap_kremove((vm_offset_t)ptvmmap);
181                         continue;
182
183                 case 1: {
184                         /*
185                          * minor device 1 is kernel memory, /dev/kmem 
186                          */
187                         vm_offset_t saddr, eaddr;
188                         int prot;
189
190                         c = iov->iov_len;
191
192                         /*
193                          * Make sure that all of the pages are currently 
194                          * resident so that we don't create any zero-fill
195                          * pages.
196                          */
197                         saddr = trunc_page(uio->uio_offset);
198                         eaddr = round_page(uio->uio_offset + c);
199                         if (saddr > eaddr)
200                                 return EFAULT;
201
202                         /*
203                          * Make sure the kernel addresses are mapped.
204                          * platform_direct_mapped() can be used to bypass
205                          * default mapping via the page table (virtual kernels
206                          * contain a lot of out-of-band data).
207                          */
208                         prot = VM_PROT_READ;
209                         if (uio->uio_rw != UIO_READ)
210                                 prot |= VM_PROT_WRITE;
211                         error = kvm_access_check(saddr, eaddr, prot);
212                         if (error)
213                                 return (error);
214                         error = uiomove((caddr_t)(vm_offset_t)uio->uio_offset,
215                                         (int)c, uio);
216                         continue;
217                 }
218                 case 2:
219                         /*
220                          * minor device 2 is EOF/RATHOLE 
221                          */
222                         if (uio->uio_rw == UIO_READ)
223                                 return (0);
224                         c = iov->iov_len;
225                         break;
226                 case 3:
227                         /*
228                          * minor device 3 (/dev/random) is source of filth
229                          * on read, seeder on write
230                          */
231                         if (buf == NULL)
232                                 buf = kmalloc(PAGE_SIZE, M_TEMP, M_WAITOK);
233                         c = min(iov->iov_len, PAGE_SIZE);
234                         if (uio->uio_rw == UIO_WRITE) {
235                                 error = uiomove(buf, (int)c, uio);
236                                 if (error == 0)
237                                         error = add_buffer_randomness(buf, c);
238                         } else {
239                                 poolsize = read_random(buf, c);
240                                 if (poolsize == 0) {
241                                         if (buf)
242                                                 kfree(buf, M_TEMP);
243                                         if ((flags & IO_NDELAY) != 0)
244                                                 return (EWOULDBLOCK);
245                                         return (0);
246                                 }
247                                 c = min(c, poolsize);
248                                 error = uiomove(buf, (int)c, uio);
249                         }
250                         continue;
251                 case 4:
252                         /*
253                          * minor device 4 (/dev/urandom) is source of muck
254                          * on read, writes are disallowed.
255                          */
256                         c = min(iov->iov_len, PAGE_SIZE);
257                         if (uio->uio_rw == UIO_WRITE) {
258                                 error = EPERM;
259                                 break;
260                         }
261                         if (CURSIG(curproc) != 0) {
262                                 /*
263                                  * Use tsleep() to get the error code right.
264                                  * It should return immediately.
265                                  */
266                                 error = tsleep(&rand_bolt, PCATCH, "urand", 1);
267                                 if (error != 0 && error != EWOULDBLOCK)
268                                         continue;
269                         }
270                         if (buf == NULL)
271                                 buf = kmalloc(PAGE_SIZE, M_TEMP, M_WAITOK);
272                         poolsize = read_random_unlimited(buf, c);
273                         c = min(c, poolsize);
274                         error = uiomove(buf, (int)c, uio);
275                         continue;
276                 case 12:
277                         /*
278                          * minor device 12 (/dev/zero) is source of nulls 
279                          * on read, write are disallowed.
280                          */
281                         if (uio->uio_rw == UIO_WRITE) {
282                                 c = iov->iov_len;
283                                 break;
284                         }
285                         if (zbuf == NULL) {
286                                 zbuf = (caddr_t)
287                                     kmalloc(PAGE_SIZE, M_TEMP, M_WAITOK);
288                                 bzero(zbuf, PAGE_SIZE);
289                         }
290                         c = min(iov->iov_len, PAGE_SIZE);
291                         error = uiomove(zbuf, (int)c, uio);
292                         continue;
293                 default:
294                         return (ENODEV);
295                 }
296                 if (error)
297                         break;
298                 iov->iov_base += c;
299                 iov->iov_len -= c;
300                 uio->uio_offset += c;
301                 uio->uio_resid -= c;
302         }
303         if (buf)
304                 kfree(buf, M_TEMP);
305         return (error);
306 }
307
308 static int
309 mmread(struct dev_read_args *ap)
310 {
311         return(mmrw(ap->a_head.a_dev, ap->a_uio, ap->a_ioflag));
312 }
313
314 static int
315 mmwrite(struct dev_write_args *ap)
316 {
317         return(mmrw(ap->a_head.a_dev, ap->a_uio, ap->a_ioflag));
318 }
319
320
321
322
323
324 /*******************************************************\
325 * allow user processes to MMAP some memory sections     *
326 * instead of going through read/write                   *
327 \*******************************************************/
328
329 static int
330 memmmap(struct dev_mmap_args *ap)
331 {
332         cdev_t dev = ap->a_head.a_dev;
333
334         switch (minor(dev)) {
335         case 0:
336                 /* 
337                  * minor device 0 is physical memory 
338                  */
339                 ap->a_result = i386_btop(ap->a_offset);
340                 return 0;
341         case 1:
342                 /*
343                  * minor device 1 is kernel memory 
344                  */
345                 ap->a_result = i386_btop(vtophys(ap->a_offset));
346                 return 0;
347
348         default:
349                 return EINVAL;
350         }
351 }
352
353 static int
354 mmioctl(struct dev_ioctl_args *ap)
355 {
356         cdev_t dev = ap->a_head.a_dev;
357
358         switch (minor(dev)) {
359         case 0:
360                 return mem_ioctl(dev, ap->a_cmd, ap->a_data,
361                                  ap->a_fflag, ap->a_cred);
362         case 3:
363         case 4:
364                 return random_ioctl(dev, ap->a_cmd, ap->a_data,
365                                     ap->a_fflag, ap->a_cred);
366         }
367         return (ENODEV);
368 }
369
370 /*
371  * Operations for changing memory attributes.
372  *
373  * This is basically just an ioctl shim for mem_range_attr_get
374  * and mem_range_attr_set.
375  */
376 static int 
377 mem_ioctl(cdev_t dev, u_long cmd, caddr_t data, int flags, struct ucred *cred)
378 {
379         int nd, error = 0;
380         struct mem_range_op *mo = (struct mem_range_op *)data;
381         struct mem_range_desc *md;
382         
383         /* is this for us? */
384         if ((cmd != MEMRANGE_GET) &&
385             (cmd != MEMRANGE_SET))
386                 return (ENOTTY);
387
388         /* any chance we can handle this? */
389         if (mem_range_softc.mr_op == NULL)
390                 return (EOPNOTSUPP);
391
392         /* do we have any descriptors? */
393         if (mem_range_softc.mr_ndesc == 0)
394                 return (ENXIO);
395
396         switch (cmd) {
397         case MEMRANGE_GET:
398                 nd = imin(mo->mo_arg[0], mem_range_softc.mr_ndesc);
399                 if (nd > 0) {
400                         md = (struct mem_range_desc *)
401                                 kmalloc(nd * sizeof(struct mem_range_desc),
402                                        M_MEMDESC, M_WAITOK);
403                         error = mem_range_attr_get(md, &nd);
404                         if (!error)
405                                 error = copyout(md, mo->mo_desc, 
406                                         nd * sizeof(struct mem_range_desc));
407                         kfree(md, M_MEMDESC);
408                 } else {
409                         nd = mem_range_softc.mr_ndesc;
410                 }
411                 mo->mo_arg[0] = nd;
412                 break;
413                 
414         case MEMRANGE_SET:
415                 md = (struct mem_range_desc *)kmalloc(sizeof(struct mem_range_desc),
416                                                     M_MEMDESC, M_WAITOK);
417                 error = copyin(mo->mo_desc, md, sizeof(struct mem_range_desc));
418                 /* clamp description string */
419                 md->mr_owner[sizeof(md->mr_owner) - 1] = 0;
420                 if (error == 0)
421                         error = mem_range_attr_set(md, &mo->mo_arg[0]);
422                 kfree(md, M_MEMDESC);
423                 break;
424         }
425         return (error);
426 }
427
428 /*
429  * Implementation-neutral, kernel-callable functions for manipulating
430  * memory range attributes.
431  */
432 int
433 mem_range_attr_get(struct mem_range_desc *mrd, int *arg)
434 {
435         /* can we handle this? */
436         if (mem_range_softc.mr_op == NULL)
437                 return (EOPNOTSUPP);
438
439         if (*arg == 0) {
440                 *arg = mem_range_softc.mr_ndesc;
441         } else {
442                 bcopy(mem_range_softc.mr_desc, mrd, (*arg) * sizeof(struct mem_range_desc));
443         }
444         return (0);
445 }
446
447 int
448 mem_range_attr_set(struct mem_range_desc *mrd, int *arg)
449 {
450         /* can we handle this? */
451         if (mem_range_softc.mr_op == NULL)
452                 return (EOPNOTSUPP);
453
454         return (mem_range_softc.mr_op->set(&mem_range_softc, mrd, arg));
455 }
456
457 #ifdef SMP
458 void
459 mem_range_AP_init(void)
460 {
461         if (mem_range_softc.mr_op && mem_range_softc.mr_op->initAP)
462                 return (mem_range_softc.mr_op->initAP(&mem_range_softc));
463 }
464 #endif
465
466 static int 
467 random_ioctl(cdev_t dev, u_long cmd, caddr_t data, int flags, struct ucred *cred)
468 {
469         int error;
470         int intr;
471         
472         /*
473          * Even inspecting the state is privileged, since it gives a hint
474          * about how easily the randomness might be guessed.
475          */
476         error = 0;
477
478         switch (cmd) {
479         /* Really handled in upper layer */
480         case FIOASYNC:
481                 break;
482         case MEM_SETIRQ:
483                 intr = *(int16_t *)data;
484                 if ((error = suser_cred(cred, 0)) != 0)
485                         break;
486                 if (intr < 0 || intr >= MAX_INTS)
487                         return (EINVAL);
488                 register_randintr(intr);
489                 break;
490         case MEM_CLEARIRQ:
491                 intr = *(int16_t *)data;
492                 if ((error = suser_cred(cred, 0)) != 0)
493                         break;
494                 if (intr < 0 || intr >= MAX_INTS)
495                         return (EINVAL);
496                 unregister_randintr(intr);
497                 break;
498         case MEM_RETURNIRQ:
499                 error = ENOTSUP;
500                 break;
501         case MEM_FINDIRQ:
502                 intr = *(int16_t *)data;
503                 if ((error = suser_cred(cred, 0)) != 0)
504                         break;
505                 if (intr < 0 || intr >= MAX_INTS)
506                         return (EINVAL);
507                 intr = next_registered_randintr(intr);
508                 if (intr == MAX_INTS)
509                         return (ENOENT);
510                 *(u_int16_t *)data = intr;
511                 break;
512         default:
513                 error = ENOTSUP;
514                 break;
515         }
516         return (error);
517 }
518
519 int
520 mmpoll(struct dev_poll_args *ap)
521 {
522         cdev_t dev = ap->a_head.a_dev;
523         int revents;
524
525         switch (minor(dev)) {
526         case 3:         /* /dev/random */
527                 revents = random_poll(dev, ap->a_events);
528                 break;
529         case 4:         /* /dev/urandom */
530         default:
531                 revents = seltrue(dev, ap->a_events);
532                 break;
533         }
534         ap->a_events = revents;
535         return (0);
536 }
537
538 int
539 iszerodev(cdev_t dev)
540 {
541         return ((major(dev) == mem_ops.head.maj)
542           && minor(dev) == 12);
543 }
544
545 static void
546 mem_drvinit(void *unused)
547 {
548
549         /* Initialise memory range handling */
550         if (mem_range_softc.mr_op != NULL)
551                 mem_range_softc.mr_op->init(&mem_range_softc);
552
553         dev_ops_add(&mem_ops, 0xf0, 0);
554         make_dev(&mem_ops, 0, UID_ROOT, GID_KMEM, 0640, "mem");
555         make_dev(&mem_ops, 1, UID_ROOT, GID_KMEM, 0640, "kmem");
556         make_dev(&mem_ops, 2, UID_ROOT, GID_WHEEL, 0666, "null");
557         make_dev(&mem_ops, 3, UID_ROOT, GID_WHEEL, 0644, "random");
558         make_dev(&mem_ops, 4, UID_ROOT, GID_WHEEL, 0644, "urandom");
559         make_dev(&mem_ops, 12, UID_ROOT, GID_WHEEL, 0666, "zero");
560         make_dev(&mem_ops, 14, UID_ROOT, GID_WHEEL, 0600, "io");
561 }
562
563 SYSINIT(memdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,mem_drvinit,NULL)
564