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