root/arch/i386/i386/mem.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. mmopen
  2. mmclose
  3. mmrw
  4. mmmmap
  5. mmioctl
  6. mem_ioctl
  7. mem_range_attr_get
  8. mem_range_attr_set

    1 /*      $NetBSD: mem.c,v 1.31 1996/05/03 19:42:19 christos Exp $        */
    2 /*      $OpenBSD: mem.c,v 1.32 2006/12/29 13:04:37 pedro Exp $ */
    3 /*
    4  * Copyright (c) 1988 University of Utah.
    5  * Copyright (c) 1982, 1986, 1990, 1993
    6  *      The Regents of the University of California.  All rights reserved.
    7  *
    8  * This code is derived from software contributed to Berkeley by
    9  * the Systems Programming Group of the University of Utah Computer
   10  * Science Department.
   11  *
   12  * Redistribution and use in source and binary forms, with or without
   13  * modification, are permitted provided that the following conditions
   14  * are met:
   15  * 1. Redistributions of source code must retain the above copyright
   16  *    notice, this list of conditions and the following disclaimer.
   17  * 2. Redistributions in binary form must reproduce the above copyright
   18  *    notice, this list of conditions and the following disclaimer in the
   19  *    documentation and/or other materials provided with the distribution.
   20  * 3. Neither the name of the University nor the names of its contributors
   21  *    may be used to endorse or promote products derived from this software
   22  *    without specific prior written permission.
   23  *
   24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   34  * SUCH DAMAGE.
   35  *
   36  *      @(#)mem.c       8.3 (Berkeley) 1/12/94
   37  */
   38 
   39 /*
   40  * Memory special file
   41  */
   42 
   43 #include <sys/param.h>
   44 #include <sys/buf.h>
   45 #include <sys/systm.h>
   46 #include <sys/uio.h>
   47 #include <sys/ioccom.h>
   48 #include <sys/malloc.h>
   49 #include <sys/memrange.h>
   50 #include <sys/proc.h>
   51 #include <sys/fcntl.h>
   52 
   53 #include <machine/cpu.h>
   54 #include <machine/conf.h>
   55 
   56 #include <uvm/uvm_extern.h>
   57 
   58 #include "mtrr.h"
   59 
   60 extern char *vmmap;            /* poor name! */
   61 caddr_t zeropage;
   62 
   63 /* open counter for aperture */
   64 #ifdef APERTURE
   65 static int ap_open_count = 0;
   66 extern int allowaperture;
   67 
   68 #define VGA_START 0xA0000
   69 #define BIOS_END  0xFFFFF
   70 #endif
   71 
   72 #if NMTRR > 0
   73 struct mem_range_softc mem_range_softc;
   74 static int mem_ioctl(dev_t, u_long, caddr_t, int, struct proc *);
   75 #endif
   76 
   77 /*ARGSUSED*/
   78 int
   79 mmopen(dev_t dev, int flag, int mode, struct proc *p)
   80 {
   81 
   82         switch (minor(dev)) {
   83         case 0:
   84         case 1:
   85         case 2:
   86         case 12:
   87                 break;
   88 #ifdef APERTURE
   89         case 4:
   90                 if (suser(p, 0) != 0 || !allowaperture)
   91                         return (EPERM);
   92 
   93                 /* authorize only one simultaneous open() */
   94                 if (ap_open_count > 0)
   95                         return(EPERM);
   96                 ap_open_count++;
   97                 break;
   98 #endif
   99         default:
  100                 return (ENXIO);
  101         }
  102         return (0);
  103 }
  104 
  105 /*ARGSUSED*/
  106 int
  107 mmclose(dev_t dev, int flag, int mode, struct proc *p)
  108 {
  109 #ifdef APERTURE
  110         if (minor(dev) == 4)
  111                 ap_open_count--;
  112 #endif
  113         return (0);
  114 }
  115 
  116 /*ARGSUSED*/
  117 int
  118 mmrw(dev_t dev, struct uio *uio, int flags)
  119 {
  120         vaddr_t o, v;
  121         int c;
  122         struct iovec *iov;
  123         int error = 0;
  124         static int physlock;
  125 
  126         if (minor(dev) == 0) {
  127                 /* lock against other uses of shared vmmap */
  128                 while (physlock > 0) {
  129                         physlock++;
  130                         error = tsleep((caddr_t)&physlock, PZERO | PCATCH,
  131                             "mmrw", 0);
  132                         if (error)
  133                                 return (error);
  134                 }
  135                 physlock = 1;
  136         }
  137         while (uio->uio_resid > 0 && error == 0) {
  138                 iov = uio->uio_iov;
  139                 if (iov->iov_len == 0) {
  140                         uio->uio_iov++;
  141                         uio->uio_iovcnt--;
  142                         if (uio->uio_iovcnt < 0)
  143                                 panic("mmrw");
  144                         continue;
  145                 }
  146                 switch (minor(dev)) {
  147 
  148 /* minor device 0 is physical memory */
  149                 case 0:
  150                         v = uio->uio_offset;
  151                         pmap_enter(pmap_kernel(), (vaddr_t)vmmap,
  152                             trunc_page(v), uio->uio_rw == UIO_READ ?
  153                             VM_PROT_READ : VM_PROT_WRITE, PMAP_WIRED);
  154                         pmap_update(pmap_kernel());
  155                         o = uio->uio_offset & PGOFSET;
  156                         c = min(uio->uio_resid, (int)(NBPG - o));
  157                         error = uiomove((caddr_t)vmmap + o, c, uio);
  158                         pmap_remove(pmap_kernel(), (vaddr_t)vmmap,
  159                             (vaddr_t)vmmap + NBPG);
  160                         pmap_update(pmap_kernel());
  161                         continue;
  162 
  163 /* minor device 1 is kernel memory */
  164                 case 1:
  165                         v = uio->uio_offset;
  166                         c = min(iov->iov_len, MAXPHYS);
  167                         if (!uvm_kernacc((caddr_t)v, c,
  168                             uio->uio_rw == UIO_READ ? B_READ : B_WRITE))
  169                                 return (EFAULT);
  170                         error = uiomove((caddr_t)v, c, uio);
  171                         continue;
  172 
  173 /* minor device 2 is EOF/RATHOLE */
  174                 case 2:
  175                         if (uio->uio_rw == UIO_WRITE)
  176                                 uio->uio_resid = 0;
  177                         return (0);
  178 
  179 /* minor device 12 (/dev/zero) is source of nulls on read, rathole on write */
  180                 case 12:
  181                         if (uio->uio_rw == UIO_WRITE) {
  182                                 c = iov->iov_len;
  183                                 break;
  184                         }
  185                         if (zeropage == NULL) {
  186                                 zeropage = (caddr_t)
  187                                     malloc(PAGE_SIZE, M_TEMP, M_WAITOK);
  188                                 bzero(zeropage, PAGE_SIZE);
  189                         }
  190                         c = min(iov->iov_len, PAGE_SIZE);
  191                         error = uiomove(zeropage, c, uio);
  192                         continue;
  193 
  194                 default:
  195                         return (ENXIO);
  196                 }
  197                 (char *)iov->iov_base += c;
  198                 iov->iov_len -= c;
  199                 uio->uio_offset += c;
  200                 uio->uio_resid -= c;
  201         }
  202         if (minor(dev) == 0) {
  203                 if (physlock > 1)
  204                         wakeup((caddr_t)&physlock);
  205                 physlock = 0;
  206         }
  207         return (error);
  208 }
  209 
  210 paddr_t
  211 mmmmap(dev_t dev, off_t off, int prot)
  212 {
  213         struct proc *p = curproc;       /* XXX */
  214 
  215         switch (minor(dev)) {
  216 /* minor device 0 is physical memory */
  217         case 0:
  218                 if ((u_int)off > ctob(physmem) &&
  219                     suser(p, 0) != 0)
  220                         return -1;
  221                 return atop(off);
  222 
  223 #ifdef APERTURE
  224 /* minor device 4 is aperture driver */
  225         case 4:
  226                 switch (allowaperture) {
  227                 case 1:
  228                         /* Allow mapping of the VGA framebuffer & BIOS only */
  229                         if ((off >= VGA_START && off <= BIOS_END) ||
  230                             (unsigned)off > (unsigned)ctob(physmem))
  231                                 return atop(off);
  232                         else
  233                                 return -1;
  234                 case 2:
  235                         /* Allow mapping of the whole 1st megabyte
  236                            for x86emu */
  237                         if (off <= BIOS_END ||
  238                             (unsigned)off > (unsigned)ctob(physmem))
  239                                 return atop(off);
  240                         else
  241                                 return -1;
  242                 default:
  243                         return -1;
  244                 }
  245                         
  246 #endif
  247         default:
  248                 return -1;
  249         }
  250 }
  251 
  252 int
  253 mmioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct proc *p)
  254 {
  255 #if NMTRR > 0
  256         switch (minor(dev)) {
  257         case 0:
  258         case 4:
  259                 return mem_ioctl(dev, cmd, data, flags, p);
  260         }
  261 #endif
  262         return (ENODEV);
  263 }
  264 
  265 #if NMTRR > 0
  266 /*
  267  * Operations for changing memory attributes.
  268  *
  269  * This is basically just an ioctl shim for mem_range_attr_get
  270  * and mem_range_attr_set.
  271  */
  272 static int
  273 mem_ioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct proc *p)
  274 {
  275         int nd, error = 0;
  276         struct mem_range_op *mo = (struct mem_range_op *)data;
  277         struct mem_range_desc *md;
  278         
  279         /* is this for us? */
  280         if ((cmd != MEMRANGE_GET) &&
  281             (cmd != MEMRANGE_SET))
  282                 return (ENOTTY);
  283 
  284         /* any chance we can handle this? */
  285         if (mem_range_softc.mr_op == NULL)
  286                 return (EOPNOTSUPP);
  287 
  288         /* do we have any descriptors? */
  289         if (mem_range_softc.mr_ndesc == 0)
  290                 return (ENXIO);
  291 
  292         switch (cmd) {
  293         case MEMRANGE_GET:
  294                 nd = imin(mo->mo_arg[0], mem_range_softc.mr_ndesc);
  295                 if (nd > 0) {
  296                         md = (struct mem_range_desc *)
  297                                 malloc(nd * sizeof(struct mem_range_desc),
  298                                        M_MEMDESC, M_WAITOK);
  299                         error = mem_range_attr_get(md, &nd);
  300                         if (!error)
  301                                 error = copyout(md, mo->mo_desc,
  302                                         nd * sizeof(struct mem_range_desc));
  303                         free(md, M_MEMDESC);
  304                 } else {
  305                         nd = mem_range_softc.mr_ndesc;
  306                 }
  307                 mo->mo_arg[0] = nd;
  308                 break;
  309                 
  310         case MEMRANGE_SET:
  311                 md = malloc(sizeof(struct mem_range_desc), M_MEMDESC, M_WAITOK);
  312                 error = copyin(mo->mo_desc, md, sizeof(struct mem_range_desc));
  313                 /* clamp description string */
  314                 md->mr_owner[sizeof(md->mr_owner) - 1] = 0;
  315                 if (error == 0)
  316                         error = mem_range_attr_set(md, &mo->mo_arg[0]);
  317                 free(md, M_MEMDESC);
  318                 break;
  319         }
  320         return (error);
  321 }
  322 
  323 /*
  324  * Implementation-neutral, kernel-callable functions for manipulating
  325  * memory range attributes.
  326  */
  327 int
  328 mem_range_attr_get(struct mem_range_desc *mrd, int *arg)
  329 {
  330         /* can we handle this? */
  331         if (mem_range_softc.mr_op == NULL)
  332                 return (EOPNOTSUPP);
  333 
  334         if (*arg == 0) {
  335                 *arg = mem_range_softc.mr_ndesc;
  336         } else {
  337                 bcopy(mem_range_softc.mr_desc, mrd, (*arg) * sizeof(struct mem_range_desc));
  338         }
  339         return (0);
  340 }
  341 
  342 int
  343 mem_range_attr_set(struct mem_range_desc *mrd, int *arg)
  344 {
  345         /* can we handle this? */
  346         if (mem_range_softc.mr_op == NULL)
  347                 return (EOPNOTSUPP);
  348 
  349         return (mem_range_softc.mr_op->set(&mem_range_softc, mrd, arg));
  350 }
  351 
  352 #endif /* NMTRR > 0 */
  353 

/* [<][>][^][v][top][bottom][index][help] */