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  * SPDX-License-Identifier: BSD-3-Clause
    3  *
    4  * Copyright (c) 1988 University of Utah.
    5  * Copyright (c) 1982, 1986, 1990 The Regents of the University of California.
    6  * 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, and code derived from software contributed to
   11  * Berkeley by William Jolitz.
   12  *
   13  * Redistribution and use in source and binary forms, with or without
   14  * modification, are permitted provided that the following conditions
   15  * are met:
   16  * 1. Redistributions of source code must retain the above copyright
   17  *    notice, this list of conditions and the following disclaimer.
   18  * 2. Redistributions in binary form must reproduce the above copyright
   19  *    notice, this list of conditions and the following disclaimer in the
   20  *    documentation and/or other materials provided with the distribution.
   21  * 3. Neither the name of the University nor the names of its contributors
   22  *    may be used to endorse or promote products derived from this software
   23  *    without specific prior written permission.
   24  *
   25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   35  * SUCH DAMAGE.
   36  *
   37  *      from: Utah $Hdr: mem.c 1.13 89/10/08$
   38  *      from: @(#)mem.c 7.2 (Berkeley) 5/9/91
   39  */
   40 
   41 #include <sys/cdefs.h>
   42 __FBSDID("$FreeBSD: releng/12.0/sys/powerpc/powerpc/mem.c 330610 2018-03-07 17:08:07Z nwhitehorn $");
   43 
   44 /*
   45  * Memory special file
   46  */
   47 
   48 #include <sys/param.h>
   49 #include <sys/conf.h>
   50 #include <sys/fcntl.h>
   51 #include <sys/kernel.h>
   52 #include <sys/lock.h>
   53 #include <sys/ioccom.h>
   54 #include <sys/malloc.h>
   55 #include <sys/memrange.h>
   56 #include <sys/module.h>
   57 #include <sys/mutex.h>
   58 #include <sys/proc.h>
   59 #include <sys/msgbuf.h>
   60 #include <sys/systm.h>
   61 #include <sys/signalvar.h>
   62 #include <sys/uio.h>
   63 
   64 #include <machine/md_var.h>
   65 #include <machine/vmparam.h>
   66 
   67 #include <vm/vm.h>
   68 #include <vm/pmap.h>
   69 #include <vm/vm_extern.h>
   70 #include <vm/vm_page.h>
   71 
   72 #include <machine/memdev.h>
   73 
   74 static void ppc_mrinit(struct mem_range_softc *);
   75 static int ppc_mrset(struct mem_range_softc *, struct mem_range_desc *, int *);
   76 
   77 MALLOC_DEFINE(M_MEMDESC, "memdesc", "memory range descriptors");
   78 
   79 static struct mem_range_ops ppc_mem_range_ops = {
   80         ppc_mrinit,
   81         ppc_mrset,
   82         NULL,
   83         NULL
   84 };
   85 struct mem_range_softc mem_range_softc = {
   86         &ppc_mem_range_ops,
   87         0, 0, NULL
   88 }; 
   89 
   90 /* ARGSUSED */
   91 int
   92 memrw(struct cdev *dev, struct uio *uio, int flags)
   93 {
   94         struct iovec *iov;
   95         int error = 0;
   96         vm_offset_t va, eva, off, v;
   97         vm_prot_t prot;
   98         struct vm_page m;
   99         vm_page_t marr;
  100         vm_size_t cnt;
  101 
  102         cnt = 0;
  103         error = 0;
  104 
  105         while (uio->uio_resid > 0 && !error) {
  106                 iov = uio->uio_iov;
  107                 if (iov->iov_len == 0) {
  108                         uio->uio_iov++;
  109                         uio->uio_iovcnt--;
  110                         if (uio->uio_iovcnt < 0)
  111                                 panic("memrw");
  112                         continue;
  113                 }
  114                 if (dev2unit(dev) == CDEV_MINOR_MEM) {
  115 kmem_direct_mapped:     v = uio->uio_offset;
  116 
  117                         off = uio->uio_offset & PAGE_MASK;
  118                         cnt = PAGE_SIZE - ((vm_offset_t)iov->iov_base &
  119                             PAGE_MASK);
  120                         cnt = min(cnt, PAGE_SIZE - off);
  121                         cnt = min(cnt, iov->iov_len);
  122 
  123                         if (mem_valid(v, cnt)) {
  124                                 error = EFAULT;
  125                                 break;
  126                         }
  127         
  128                         if (hw_direct_map && !pmap_dev_direct_mapped(v, cnt)) {
  129                                 error = uiomove((void *)PHYS_TO_DMAP(v), cnt,
  130                                     uio);
  131                         } else {
  132                                 m.phys_addr = trunc_page(v);
  133                                 marr = &m;
  134                                 error = uiomove_fromphys(&marr, off, cnt, uio);
  135                         }
  136                 }
  137                 else if (dev2unit(dev) == CDEV_MINOR_KMEM) {
  138                         va = uio->uio_offset;
  139 
  140                         if ((va < VM_MIN_KERNEL_ADDRESS) || (va > virtual_end))
  141                                 goto kmem_direct_mapped;
  142 
  143                         va = trunc_page(uio->uio_offset);
  144                         eva = round_page(uio->uio_offset
  145                             + iov->iov_len);
  146 
  147                         /* 
  148                          * Make sure that all the pages are currently resident
  149                          * so that we don't create any zero-fill pages.
  150                          */
  151 
  152                         for (; va < eva; va += PAGE_SIZE)
  153                                 if (pmap_extract(kernel_pmap, va) == 0)
  154                                         return (EFAULT);
  155 
  156                         prot = (uio->uio_rw == UIO_READ)
  157                             ? VM_PROT_READ : VM_PROT_WRITE;
  158 
  159                         va = uio->uio_offset;
  160                         if (kernacc((void *) va, iov->iov_len, prot)
  161                             == FALSE)
  162                                 return (EFAULT);
  163 
  164                         error = uiomove((void *)va, iov->iov_len, uio);
  165 
  166                         continue;
  167                 }
  168         }
  169 
  170         return (error);
  171 }
  172 
  173 /*
  174  * allow user processes to MMAP some memory sections
  175  * instead of going through read/write
  176  */
  177 int
  178 memmmap(struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr,
  179     int prot, vm_memattr_t *memattr)
  180 {
  181         int i;
  182 
  183         if (dev2unit(dev) == CDEV_MINOR_MEM)
  184                 *paddr = offset;
  185         else
  186                 return (EFAULT);
  187 
  188         for (i = 0; i < mem_range_softc.mr_ndesc; i++) {
  189                 if (!(mem_range_softc.mr_desc[i].mr_flags & MDF_ACTIVE))
  190                         continue;
  191 
  192                 if (offset >= mem_range_softc.mr_desc[i].mr_base &&
  193                     offset < mem_range_softc.mr_desc[i].mr_base +
  194                     mem_range_softc.mr_desc[i].mr_len) {
  195                         switch (mem_range_softc.mr_desc[i].mr_flags &
  196                             MDF_ATTRMASK) {
  197                         case MDF_WRITEBACK:
  198                                 *memattr = VM_MEMATTR_WRITE_BACK;
  199                                 break;
  200                         case MDF_WRITECOMBINE:
  201                                 *memattr = VM_MEMATTR_WRITE_COMBINING;
  202                                 break;
  203                         case MDF_UNCACHEABLE:
  204                                 *memattr = VM_MEMATTR_UNCACHEABLE;
  205                                 break;
  206                         case MDF_WRITETHROUGH:
  207                                 *memattr = VM_MEMATTR_WRITE_THROUGH;
  208                                 break;
  209                         }
  210 
  211                         break;
  212                 }
  213         }
  214 
  215         return (0);
  216 }
  217 
  218 static void
  219 ppc_mrinit(struct mem_range_softc *sc)
  220 {
  221         sc->mr_cap = 0;
  222         sc->mr_ndesc = 8; /* XXX: Should be dynamically expandable */
  223         sc->mr_desc = malloc(sc->mr_ndesc * sizeof(struct mem_range_desc),
  224             M_MEMDESC, M_WAITOK | M_ZERO);
  225 }
  226 
  227 static int
  228 ppc_mrset(struct mem_range_softc *sc, struct mem_range_desc *desc, int *arg)
  229 {
  230         int i;
  231 
  232         switch(*arg) {
  233         case MEMRANGE_SET_UPDATE:
  234                 for (i = 0; i < sc->mr_ndesc; i++) {
  235                         if (!sc->mr_desc[i].mr_len) {
  236                                 sc->mr_desc[i] = *desc;
  237                                 sc->mr_desc[i].mr_flags |= MDF_ACTIVE;
  238                                 return (0);
  239                         }
  240                         if (sc->mr_desc[i].mr_base == desc->mr_base &&
  241                             sc->mr_desc[i].mr_len == desc->mr_len)
  242                                 return (EEXIST);
  243                 }
  244                 return (ENOSPC);
  245         case MEMRANGE_SET_REMOVE:
  246                 for (i = 0; i < sc->mr_ndesc; i++)
  247                         if (sc->mr_desc[i].mr_base == desc->mr_base &&
  248                             sc->mr_desc[i].mr_len == desc->mr_len) {
  249                                 bzero(&sc->mr_desc[i], sizeof(sc->mr_desc[i]));
  250                                 return (0);
  251                         }
  252                 return (ENOENT);
  253         default:
  254                 return (EOPNOTSUPP);
  255         }
  256 
  257         return (0);
  258 }
  259 
  260 /*
  261  * Operations for changing memory attributes.
  262  *
  263  * This is basically just an ioctl shim for mem_range_attr_get
  264  * and mem_range_attr_set.
  265  */
  266 /* ARGSUSED */
  267 int 
  268 memioctl(struct cdev *dev __unused, u_long cmd, caddr_t data, int flags,
  269     struct thread *td)
  270 {
  271         int nd, error = 0;
  272         struct mem_range_op *mo = (struct mem_range_op *)data;
  273         struct mem_range_desc *md;
  274         
  275         /* is this for us? */
  276         if ((cmd != MEMRANGE_GET) &&
  277             (cmd != MEMRANGE_SET))
  278                 return (ENOTTY);
  279 
  280         /* any chance we can handle this? */
  281         if (mem_range_softc.mr_op == NULL)
  282                 return (EOPNOTSUPP);
  283 
  284         /* do we have any descriptors? */
  285         if (mem_range_softc.mr_ndesc == 0)
  286                 return (ENXIO);
  287 
  288         switch (cmd) {
  289         case MEMRANGE_GET:
  290                 nd = imin(mo->mo_arg[0], mem_range_softc.mr_ndesc);
  291                 if (nd > 0) {
  292                         md = (struct mem_range_desc *)
  293                                 malloc(nd * sizeof(struct mem_range_desc),
  294                                        M_MEMDESC, M_WAITOK);
  295                         error = mem_range_attr_get(md, &nd);
  296                         if (!error)
  297                                 error = copyout(md, mo->mo_desc, 
  298                                         nd * sizeof(struct mem_range_desc));
  299                         free(md, M_MEMDESC);
  300                 }
  301                 else
  302                         nd = mem_range_softc.mr_ndesc;
  303                 mo->mo_arg[0] = nd;
  304                 break;
  305                 
  306         case MEMRANGE_SET:
  307                 md = (struct mem_range_desc *)malloc(sizeof(struct mem_range_desc),
  308                                                     M_MEMDESC, M_WAITOK);
  309                 error = copyin(mo->mo_desc, md, sizeof(struct mem_range_desc));
  310                 /* clamp description string */
  311                 md->mr_owner[sizeof(md->mr_owner) - 1] = 0;
  312                 if (error == 0)
  313                         error = mem_range_attr_set(md, &mo->mo_arg[0]);
  314                 free(md, M_MEMDESC);
  315                 break;
  316         }
  317         return (error);
  318 }
  319 

Cache object: c165fdf771d2906a3fd4608ad4c647cb


[ 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.