The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/powerpc/powerpc/mem.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    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  * 4. Neither the name of the University nor the names of its contributors
   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  */
   38 
   39 #include <sys/cdefs.h>
   40 __FBSDID("$FreeBSD$");
   41 
   42 /*
   43  * Memory special file
   44  */
   45 
   46 #include <sys/param.h>
   47 #include <sys/conf.h>
   48 #include <sys/fcntl.h>
   49 #include <sys/kernel.h>
   50 #include <sys/lock.h>
   51 #include <sys/ioccom.h>
   52 #include <sys/malloc.h>
   53 #include <sys/memrange.h>
   54 #include <sys/module.h>
   55 #include <sys/mutex.h>
   56 #include <sys/proc.h>
   57 #include <sys/msgbuf.h>
   58 #include <sys/systm.h>
   59 #include <sys/signalvar.h>
   60 #include <sys/uio.h>
   61 
   62 #include <machine/md_var.h>
   63 #include <machine/vmparam.h>
   64 
   65 #include <vm/vm.h>
   66 #include <vm/pmap.h>
   67 #include <vm/vm_extern.h>
   68 #include <vm/vm_page.h>
   69 
   70 #include <machine/memdev.h>
   71 
   72 static void ppc_mrinit(struct mem_range_softc *);
   73 static int ppc_mrset(struct mem_range_softc *, struct mem_range_desc *, int *);
   74 
   75 MALLOC_DEFINE(M_MEMDESC, "memdesc", "memory range descriptors");
   76 
   77 static struct mem_range_ops ppc_mem_range_ops = {
   78         ppc_mrinit,
   79         ppc_mrset,
   80         NULL,
   81         NULL
   82 };
   83 struct mem_range_softc mem_range_softc = {
   84         &ppc_mem_range_ops,
   85         0, 0, NULL
   86 }; 
   87 
   88 /* ARGSUSED */
   89 int
   90 memrw(struct cdev *dev, struct uio *uio, int flags)
   91 {
   92         struct iovec *iov;
   93         int error = 0;
   94         vm_offset_t va, eva, off, v;
   95         vm_prot_t prot;
   96         struct vm_page m;
   97         vm_page_t marr;
   98         vm_size_t cnt;
   99 
  100         cnt = 0;
  101         error = 0;
  102 
  103         while (uio->uio_resid > 0 && !error) {
  104                 iov = uio->uio_iov;
  105                 if (iov->iov_len == 0) {
  106                         uio->uio_iov++;
  107                         uio->uio_iovcnt--;
  108                         if (uio->uio_iovcnt < 0)
  109                                 panic("memrw");
  110                         continue;
  111                 }
  112                 if (dev2unit(dev) == CDEV_MINOR_MEM) {
  113 kmem_direct_mapped:     v = uio->uio_offset;
  114 
  115                         off = uio->uio_offset & PAGE_MASK;
  116                         cnt = PAGE_SIZE - ((vm_offset_t)iov->iov_base &
  117                             PAGE_MASK);
  118                         cnt = min(cnt, PAGE_SIZE - off);
  119                         cnt = min(cnt, iov->iov_len);
  120 
  121                         if (mem_valid(v, cnt)) {
  122                                 error = EFAULT;
  123                                 break;
  124                         }
  125         
  126                         if (!pmap_dev_direct_mapped(v, cnt)) {
  127                                 error = uiomove((void *)v, cnt, uio);
  128                         } else {
  129                                 m.phys_addr = trunc_page(v);
  130                                 marr = &m;
  131                                 error = uiomove_fromphys(&marr, off, cnt, uio);
  132                         }
  133                 }
  134                 else if (dev2unit(dev) == CDEV_MINOR_KMEM) {
  135                         va = uio->uio_offset;
  136 
  137                         if ((va < VM_MIN_KERNEL_ADDRESS) || (va > virtual_end))
  138                                 goto kmem_direct_mapped;
  139 
  140                         va = trunc_page(uio->uio_offset);
  141                         eva = round_page(uio->uio_offset
  142                             + iov->iov_len);
  143 
  144                         /* 
  145                          * Make sure that all the pages are currently resident
  146                          * so that we don't create any zero-fill pages.
  147                          */
  148 
  149                         for (; va < eva; va += PAGE_SIZE)
  150                                 if (pmap_extract(kernel_pmap, va) == 0)
  151                                         return (EFAULT);
  152 
  153                         prot = (uio->uio_rw == UIO_READ)
  154                             ? VM_PROT_READ : VM_PROT_WRITE;
  155 
  156                         va = uio->uio_offset;
  157                         if (kernacc((void *) va, iov->iov_len, prot)
  158                             == FALSE)
  159                                 return (EFAULT);
  160 
  161                         error = uiomove((void *)va, iov->iov_len, uio);
  162 
  163                         continue;
  164                 }
  165         }
  166 
  167         return (error);
  168 }
  169 
  170 /*
  171  * allow user processes to MMAP some memory sections
  172  * instead of going through read/write
  173  */
  174 int
  175 memmmap(struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr,
  176     int prot, vm_memattr_t *memattr)
  177 {
  178         int i;
  179 
  180         if (dev2unit(dev) == CDEV_MINOR_MEM)
  181                 *paddr = offset;
  182         else
  183                 return (EFAULT);
  184 
  185         for (i = 0; i < mem_range_softc.mr_ndesc; i++) {
  186                 if (!(mem_range_softc.mr_desc[i].mr_flags & MDF_ACTIVE))
  187                         continue;
  188 
  189                 if (offset >= mem_range_softc.mr_desc[i].mr_base &&
  190                     offset < mem_range_softc.mr_desc[i].mr_base +
  191                     mem_range_softc.mr_desc[i].mr_len) {
  192                         switch (mem_range_softc.mr_desc[i].mr_flags &
  193                             MDF_ATTRMASK) {
  194                         case MDF_WRITEBACK:
  195                                 *memattr = VM_MEMATTR_WRITE_BACK;
  196                                 break;
  197                         case MDF_WRITECOMBINE:
  198                                 *memattr = VM_MEMATTR_WRITE_COMBINING;
  199                                 break;
  200                         case MDF_UNCACHEABLE:
  201                                 *memattr = VM_MEMATTR_UNCACHEABLE;
  202                                 break;
  203                         case MDF_WRITETHROUGH:
  204                                 *memattr = VM_MEMATTR_WRITE_THROUGH;
  205                                 break;
  206                         }
  207 
  208                         break;
  209                 }
  210         }
  211 
  212         return (0);
  213 }
  214 
  215 static void
  216 ppc_mrinit(struct mem_range_softc *sc)
  217 {
  218         sc->mr_cap = 0;
  219         sc->mr_ndesc = 8; /* XXX: Should be dynamically expandable */
  220         sc->mr_desc = malloc(sc->mr_ndesc * sizeof(struct mem_range_desc),
  221             M_MEMDESC, M_WAITOK | M_ZERO);
  222 }
  223 
  224 static int
  225 ppc_mrset(struct mem_range_softc *sc, struct mem_range_desc *desc, int *arg)
  226 {
  227         int i;
  228 
  229         switch(*arg) {
  230         case MEMRANGE_SET_UPDATE:
  231                 for (i = 0; i < sc->mr_ndesc; i++) {
  232                         if (!sc->mr_desc[i].mr_len) {
  233                                 sc->mr_desc[i] = *desc;
  234                                 sc->mr_desc[i].mr_flags |= MDF_ACTIVE;
  235                                 return (0);
  236                         }
  237                         if (sc->mr_desc[i].mr_base == desc->mr_base &&
  238                             sc->mr_desc[i].mr_len == desc->mr_len)
  239                                 return (EEXIST);
  240                 }
  241                 return (ENOSPC);
  242         case MEMRANGE_SET_REMOVE:
  243                 for (i = 0; i < sc->mr_ndesc; i++)
  244                         if (sc->mr_desc[i].mr_base == desc->mr_base &&
  245                             sc->mr_desc[i].mr_len == desc->mr_len) {
  246                                 bzero(&sc->mr_desc[i], sizeof(sc->mr_desc[i]));
  247                                 return (0);
  248                         }
  249                 return (ENOENT);
  250         default:
  251                 return (EOPNOTSUPP);
  252         }
  253 
  254         return (0);
  255 }
  256 
  257 /*
  258  * Operations for changing memory attributes.
  259  *
  260  * This is basically just an ioctl shim for mem_range_attr_get
  261  * and mem_range_attr_set.
  262  */
  263 /* ARGSUSED */
  264 int 
  265 memioctl(struct cdev *dev __unused, u_long cmd, caddr_t data, int flags,
  266     struct thread *td)
  267 {
  268         int nd, error = 0;
  269         struct mem_range_op *mo = (struct mem_range_op *)data;
  270         struct mem_range_desc *md;
  271         
  272         /* is this for us? */
  273         if ((cmd != MEMRANGE_GET) &&
  274             (cmd != MEMRANGE_SET))
  275                 return (ENOTTY);
  276 
  277         /* any chance we can handle this? */
  278         if (mem_range_softc.mr_op == NULL)
  279                 return (EOPNOTSUPP);
  280 
  281         /* do we have any descriptors? */
  282         if (mem_range_softc.mr_ndesc == 0)
  283                 return (ENXIO);
  284 
  285         switch (cmd) {
  286         case MEMRANGE_GET:
  287                 nd = imin(mo->mo_arg[0], mem_range_softc.mr_ndesc);
  288                 if (nd > 0) {
  289                         md = (struct mem_range_desc *)
  290                                 malloc(nd * sizeof(struct mem_range_desc),
  291                                        M_MEMDESC, M_WAITOK);
  292                         error = mem_range_attr_get(md, &nd);
  293                         if (!error)
  294                                 error = copyout(md, mo->mo_desc, 
  295                                         nd * sizeof(struct mem_range_desc));
  296                         free(md, M_MEMDESC);
  297                 }
  298                 else
  299                         nd = mem_range_softc.mr_ndesc;
  300                 mo->mo_arg[0] = nd;
  301                 break;
  302                 
  303         case MEMRANGE_SET:
  304                 md = (struct mem_range_desc *)malloc(sizeof(struct mem_range_desc),
  305                                                     M_MEMDESC, M_WAITOK);
  306                 error = copyin(mo->mo_desc, md, sizeof(struct mem_range_desc));
  307                 /* clamp description string */
  308                 md->mr_owner[sizeof(md->mr_owner) - 1] = 0;
  309                 if (error == 0)
  310                         error = mem_range_attr_set(md, &mo->mo_arg[0]);
  311                 free(md, M_MEMDESC);
  312                 break;
  313         }
  314         return (error);
  315 }
  316 

Cache object: 4755b1c37753594a382546277dbab7bd


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.