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

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

1.1       nbrk        1: /*     $OpenBSD: mem.c,v 1.6 2007/01/15 23:19:05 jsg Exp $ */
                      2: /*
                      3:  * Copyright (c) 1988 University of Utah.
                      4:  * Copyright (c) 1982, 1986, 1990, 1993
                      5:  *     The Regents of the University of California.  All rights reserved.
                      6:  *
                      7:  * This code is derived from software contributed to Berkeley by
                      8:  * the Systems Programming Group of the University of Utah Computer
                      9:  * Science Department.
                     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:  *     @(#)mem.c       8.3 (Berkeley) 1/12/94
                     40:  */
                     41:
                     42: /*
                     43:  * Memory special file
                     44:  */
                     45:
                     46: #include <sys/param.h>
                     47: #include <sys/buf.h>
                     48: #include <sys/systm.h>
                     49: #include <sys/uio.h>
                     50: #include <sys/ioccom.h>
                     51: #include <sys/malloc.h>
                     52: #include <sys/memrange.h>
                     53: #include <sys/proc.h>
                     54: #include <sys/fcntl.h>
                     55:
                     56: #include <machine/cpu.h>
                     57: #include <machine/conf.h>
                     58:
                     59: #include <uvm/uvm_extern.h>
                     60:
                     61: caddr_t zeropage;
                     62: extern int start, end, etext;
                     63:
                     64: /* open counter for aperture */
                     65: #ifdef APERTURE
                     66: static int ap_open_count = 0;
                     67: extern int allowaperture;
                     68:
                     69: #define VGA_START 0xA0000
                     70: #define BIOS_END  0xFFFFF
                     71: #endif
                     72:
                     73: /*ARGSUSED*/
                     74: int
                     75: mmopen(dev_t dev, int flag, int mode, struct proc *p)
                     76: {
                     77:        switch (minor(dev)) {
                     78:        case 0:
                     79:        case 1:
                     80:        case 2:
                     81:        case 12:
                     82:                break;
                     83: #ifdef APERTURE
                     84:        case 4:
                     85:                if (suser(p, 0) != 0 || !allowaperture)
                     86:                        return (EPERM);
                     87:
                     88:                /* authorize only one simultaneous open() */
                     89:                if (ap_open_count > 0)
                     90:                        return(EPERM);
                     91:                ap_open_count++;
                     92:                break;
                     93: #endif
                     94:        default:
                     95:                return (ENXIO);
                     96:        }
                     97:        return (0);
                     98: }
                     99:
                    100: /*ARGSUSED*/
                    101: int
                    102: mmclose(dev_t dev, int flag, int mode, struct proc *p)
                    103: {
                    104: #ifdef APERTURE
                    105:        if (minor(dev) == 4)
                    106:                ap_open_count--;
                    107: #endif
                    108:        return (0);
                    109: }
                    110:
                    111: /*ARGSUSED*/
                    112: int
                    113: mmrw(dev_t dev, struct uio *uio, int flags)
                    114: {
                    115:        extern vaddr_t kern_end;
                    116:        vaddr_t v;
                    117:        int c;
                    118:        struct iovec *iov;
                    119:        int error = 0;
                    120:
                    121:        while (uio->uio_resid > 0 && error == 0) {
                    122:                iov = uio->uio_iov;
                    123:                if (iov->iov_len == 0) {
                    124:                        uio->uio_iov++;
                    125:                        uio->uio_iovcnt--;
                    126:                        if (uio->uio_iovcnt < 0)
                    127:                                panic("mmrw");
                    128:                        continue;
                    129:                }
                    130:                switch (minor(dev)) {
                    131:
                    132: /* minor device 0 is physical memory */
                    133:                case 0:
                    134:                        v = PMAP_DIRECT_MAP(uio->uio_offset);
                    135:                        error = uiomove((caddr_t)v, uio->uio_resid, uio);
                    136:                        continue;
                    137:
                    138: /* minor device 1 is kernel memory */
                    139:                case 1:
                    140:                        v = uio->uio_offset;
                    141:                        c = min(iov->iov_len, MAXPHYS);
                    142:                        if (v >= (vaddr_t)&start && v < kern_end) {
                    143:                                 if (v < (vaddr_t)&etext &&
                    144:                                     uio->uio_rw == UIO_WRITE)
                    145:                                         return EFAULT;
                    146:                         } else if ((!uvm_kernacc((caddr_t)v, c,
                    147:                            uio->uio_rw == UIO_READ ? B_READ : B_WRITE)) &&
                    148:                            (v < PMAP_DIRECT_BASE && v > PMAP_DIRECT_END))
                    149:                                return (EFAULT);
                    150:                        error = uiomove((caddr_t)v, c, uio);
                    151:                        continue;
                    152:
                    153: /* minor device 2 is EOF/RATHOLE */
                    154:                case 2:
                    155:                        if (uio->uio_rw == UIO_WRITE)
                    156:                                uio->uio_resid = 0;
                    157:                        return (0);
                    158:
                    159: /* minor device 12 (/dev/zero) is source of nulls on read, rathole on write */
                    160:                case 12:
                    161:                        if (uio->uio_rw == UIO_WRITE) {
                    162:                                c = iov->iov_len;
                    163:                                break;
                    164:                        }
                    165:                        if (zeropage == NULL) {
                    166:                                zeropage = (caddr_t)
                    167:                                    malloc(PAGE_SIZE, M_TEMP, M_WAITOK);
                    168:                                bzero(zeropage, PAGE_SIZE);
                    169:                        }
                    170:                        c = min(iov->iov_len, PAGE_SIZE);
                    171:                        error = uiomove(zeropage, c, uio);
                    172:                        continue;
                    173:
                    174:                default:
                    175:                        return (ENXIO);
                    176:                }
                    177:                iov->iov_base += c;
                    178:                iov->iov_len -= c;
                    179:                uio->uio_offset += c;
                    180:                uio->uio_resid -= c;
                    181:        }
                    182:
                    183:        return (error);
                    184: }
                    185:
                    186: paddr_t
                    187: mmmmap(dev_t dev, off_t off, int prot)
                    188: {
                    189:        struct proc *p = curproc;       /* XXX */
                    190:
                    191:        switch (minor(dev)) {
                    192: /* minor device 0 is physical memory */
                    193:        case 0:
                    194:                if ((paddr_t)off > (paddr_t)ctob(physmem) && suser(p, 0) != 0)
                    195:                        return -1;
                    196:                return atop(off);
                    197:
                    198: #ifdef APERTURE
                    199: /* minor device 4 is aperture driver */
                    200:        case 4:
                    201:                switch (allowaperture) {
                    202:                case 1:
                    203:                        /* Allow mapping of the VGA framebuffer & BIOS only */
                    204:                        if ((off >= VGA_START && off <= BIOS_END) ||
                    205:                            (unsigned)off > (unsigned)ctob(physmem))
                    206:                                return atop(off);
                    207:                        else
                    208:                                return -1;
                    209:                case 2:
                    210:                        /* Allow mapping of the whole 1st megabyte
                    211:                           for x86emu */
                    212:                        if (off <= BIOS_END ||
                    213:                            (unsigned)off > (unsigned)ctob(physmem))
                    214:                                return atop(off);
                    215:                        else
                    216:                                return -1;
                    217:                default:
                    218:                        return -1;
                    219:                }
                    220:
                    221: #endif
                    222:        default:
                    223:                return -1;
                    224:        }
                    225: }
                    226:
                    227: int
                    228: mmioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct proc *p)
                    229: {
                    230:        return (ENODEV);
                    231: }
                    232:

CVSweb