[BACK]Return to mem.c CVS log [TXT][DIR] Up to [local] / sys / arch / arm / arm

Annotation of sys/arch/arm/arm/mem.c, Revision 1.1.1.1

1.1       nbrk        1: /*     $OpenBSD: mem.c,v 1.6 2006/11/29 13:28:45 miod Exp $    */
                      2: /*     $NetBSD: mem.c,v 1.11 2003/10/16 12:02:58 jdolecek Exp $        */
                      3:
                      4: /*
                      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: /*
                     37:  * Copyright (c) 1988 University of Utah.
                     38:  *
                     39:  * This code is derived from software contributed to Berkeley by
                     40:  * the Systems Programming Group of the University of Utah Computer
                     41:  * Science Department.
                     42:  *
                     43:  * Redistribution and use in source and binary forms, with or without
                     44:  * modification, are permitted provided that the following conditions
                     45:  * are met:
                     46:  * 1. Redistributions of source code must retain the above copyright
                     47:  *    notice, this list of conditions and the following disclaimer.
                     48:  * 2. Redistributions in binary form must reproduce the above copyright
                     49:  *    notice, this list of conditions and the following disclaimer in the
                     50:  *    documentation and/or other materials provided with the distribution.
                     51:  * 3. All advertising materials mentioning features or use of this software
                     52:  *    must display the following acknowledgement:
                     53:  *     This product includes software developed by the University of
                     54:  *     California, Berkeley and its contributors.
                     55:  * 4. Neither the name of the University nor the names of its contributors
                     56:  *    may be used to endorse or promote products derived from this software
                     57:  *    without specific prior written permission.
                     58:  *
                     59:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     60:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     61:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     62:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     63:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     64:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     65:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     66:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     67:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     68:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     69:  * SUCH DAMAGE.
                     70:  */
                     71:
                     72: /*
                     73:  * Memory special file
                     74:  */
                     75:
                     76: #include <sys/param.h>
                     77: #include <sys/conf.h>
                     78: #include <sys/buf.h>
                     79: #include <sys/systm.h>
                     80: #include <sys/uio.h>
                     81: #include <sys/malloc.h>
                     82: #include <sys/proc.h>
                     83: #include <sys/fcntl.h>
                     84:
                     85: #include <machine/cpu.h>
                     86: #include <arm/conf.h>
                     87:
                     88: #include <uvm/uvm_extern.h>
                     89:
                     90: extern char *memhook;            /* poor name! */
                     91: caddr_t zeropage;
                     92: int physlock;
                     93:
                     94: /* open counter for aperture */
                     95: #ifdef APERTURE
                     96: static int ap_open_count = 0;
                     97: extern int allowaperture;
                     98: #endif
                     99:
                    100:
                    101: /*ARGSUSED*/
                    102: int
                    103: mmopen(dev, flag, mode, p)
                    104:        dev_t dev;
                    105:        int flag, mode;
                    106:        struct proc *p;
                    107: {
                    108:        switch (minor(dev)) {
                    109:                case 0:
                    110:                case 1:
                    111:                case 2:
                    112:                case 12:
                    113:                        break;
                    114: #ifdef APERTURE
                    115:        case 4:
                    116:                if (suser(p, 0) != 0 || !allowaperture)
                    117:                        return (EPERM);
                    118:
                    119:                /* authorize only one simultaneous open() */
                    120:                if (ap_open_count > 0)
                    121:                        return(EPERM);
                    122:                ap_open_count++;
                    123:                break;
                    124: #endif
                    125:                default:
                    126:                        return (ENXIO);
                    127:        }
                    128:        return (0);
                    129: }
                    130:
                    131: /*ARGSUSED*/
                    132: int
                    133: mmclose(dev, flag, mode, p)
                    134:        dev_t dev;
                    135:        int flag, mode;
                    136:        struct proc *p;
                    137: {
                    138: #ifdef APERTURE
                    139:        if (minor(dev) == 4)
                    140:                ap_open_count--;
                    141: #endif
                    142:        return (0);
                    143: }
                    144: #define DEV_MEM 0
                    145: #define DEV_KMEM 1
                    146: #define DEV_NULL 2
                    147: #define DEV_ZERO 12
                    148:
                    149: /*ARGSUSED*/
                    150: int
                    151: mmrw(dev, uio, flags)
                    152:        dev_t dev;
                    153:        struct uio *uio;
                    154:        int flags;
                    155: {
                    156:        register vaddr_t o, v;
                    157:        register int c;
                    158:        register struct iovec *iov;
                    159:        int error = 0;
                    160:        vm_prot_t prot;
                    161:
                    162:        if (minor(dev) == DEV_MEM) {
                    163:                /* lock against other uses of shared vmmap */
                    164:                while (physlock > 0) {
                    165:                        physlock++;
                    166:                        error = tsleep((caddr_t)&physlock, PZERO | PCATCH,
                    167:                            "mmrw", 0);
                    168:                        if (error)
                    169:                                return (error);
                    170:                }
                    171:                physlock = 1;
                    172:        }
                    173:        while (uio->uio_resid > 0 && error == 0) {
                    174:                iov = uio->uio_iov;
                    175:                if (iov->iov_len == 0) {
                    176:                        uio->uio_iov++;
                    177:                        uio->uio_iovcnt--;
                    178:                        if (uio->uio_iovcnt < 0)
                    179:                                panic("mmrw");
                    180:                        continue;
                    181:                }
                    182:                switch (minor(dev)) {
                    183:
                    184:                case DEV_MEM:
                    185:                        v = uio->uio_offset;
                    186:                        prot = uio->uio_rw == UIO_READ ? VM_PROT_READ :
                    187:                            VM_PROT_WRITE;
                    188:                        pmap_enter(pmap_kernel(), (vaddr_t)memhook,
                    189:                            trunc_page(v), prot, prot|PMAP_WIRED);
                    190:                        pmap_update(pmap_kernel());
                    191:                        o = uio->uio_offset & PGOFSET;
                    192:                        c = min(uio->uio_resid, (int)(PAGE_SIZE - o));
                    193:                        error = uiomove((caddr_t)memhook + o, c, uio);
                    194:                        pmap_remove(pmap_kernel(), (vaddr_t)memhook,
                    195:                            (vaddr_t)memhook + PAGE_SIZE);
                    196:                        pmap_update(pmap_kernel());
                    197:                        break;
                    198:
                    199:                case DEV_KMEM:
                    200:                        v = uio->uio_offset;
                    201:                        c = min(iov->iov_len, MAXPHYS);
                    202:                        if (!uvm_kernacc((caddr_t)v, c,
                    203:                            uio->uio_rw == UIO_READ ? B_READ : B_WRITE))
                    204:                                return (EFAULT);
                    205:                        error = uiomove((caddr_t)v, c, uio);
                    206:                        break;
                    207:
                    208:                case DEV_NULL:
                    209:                        if (uio->uio_rw == UIO_WRITE)
                    210:                                uio->uio_resid = 0;
                    211:                        return (0);
                    212:
                    213:                case DEV_ZERO:
                    214:                        if (uio->uio_rw == UIO_WRITE) {
                    215:                                uio->uio_resid = 0;
                    216:                                return (0);
                    217:                        }
                    218:                        if (zeropage == NULL) {
                    219:                                zeropage = (caddr_t)
                    220:                                    malloc(PAGE_SIZE, M_TEMP, M_WAITOK);
                    221:                                memset(zeropage, 0, PAGE_SIZE);
                    222:                        }
                    223:                        c = min(iov->iov_len, PAGE_SIZE);
                    224:                        error = uiomove(zeropage, c, uio);
                    225:                        break;
                    226:
                    227:                default:
                    228:                        return (ENXIO);
                    229:                }
                    230:        }
                    231:        if (minor(dev) == DEV_MEM) {
                    232: /*unlock:*/
                    233:                if (physlock > 1)
                    234:                        wakeup((caddr_t)&physlock);
                    235:                physlock = 0;
                    236:        }
                    237:        return (error);
                    238: }
                    239:
                    240: paddr_t
                    241: mmmmap(dev, off, prot)
                    242:        dev_t dev;
                    243:        off_t off;
                    244:        int prot;
                    245: {
                    246:        struct proc *p = curproc;       /* XXX */
                    247:
                    248:        /*
                    249:         * /dev/mem is the only one that makes sense through this
                    250:         * interface.  For /dev/kmem any physaddr we return here
                    251:         * could be transient and hence incorrect or invalid at
                    252:         * a later time.  /dev/null just doesn't make any sense
                    253:         * and /dev/zero is a hack that is handled via the default
                    254:         * pager in mmap().
                    255:         */
                    256:        if (minor(dev) != DEV_MEM)
                    257:                return (-1);
                    258:
                    259:        /* minor device 0 is physical memory */
                    260:
                    261:        if ((paddr_t)off >= ctob((paddr_t)physmem) &&
                    262:            suser(p, 0) != 0)
                    263:                return -1;
                    264:        return atop(off);
                    265: }
                    266: /*ARGSUSED*/
                    267: int
                    268: mmioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct proc *p)
                    269: {
                    270:        return (EOPNOTSUPP);
                    271: }
                    272:

CVSweb