Annotation of sys/arch/mac68k/mac68k/mem.c, Revision 1.1
1.1 ! nbrk 1: /* $OpenBSD: mem.c,v 1.20 2005/10/23 19:00:26 martin Exp $ */
! 2: /* $NetBSD: mem.c,v 1.22 1999/03/27 00:30:07 mycroft Exp $ */
! 3:
! 4: /*
! 5: * Copyright (c) 1988 University of Utah.
! 6: * Copyright (c) 1982, 1986, 1990, 1993
! 7: * The Regents of the University of California. All rights reserved.
! 8: *
! 9: * This code is derived from software contributed to Berkeley by
! 10: * the Systems Programming Group of the University of Utah Computer
! 11: * Science Department.
! 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: * @(#)mem.c 8.3 (Berkeley) 1/12/94
! 38: */
! 39:
! 40: /*
! 41: * Memory special file
! 42: */
! 43:
! 44: #include <sys/param.h>
! 45: #include <sys/conf.h>
! 46: #include <sys/buf.h>
! 47: #include <sys/proc.h>
! 48: #include <sys/systm.h>
! 49: #include <sys/uio.h>
! 50: #include <sys/malloc.h>
! 51:
! 52: #include <machine/cpu.h>
! 53:
! 54: #include <uvm/uvm_extern.h>
! 55:
! 56: extern u_long maxaddr;
! 57:
! 58: static caddr_t devzeropage;
! 59:
! 60: #define mmread mmrw
! 61: #define mmwrite mmrw
! 62: cdev_decl(mm);
! 63:
! 64: /*ARGSUSED*/
! 65: int
! 66: mmopen(dev, flag, mode, p)
! 67: dev_t dev;
! 68: int flag, mode;
! 69: struct proc *p;
! 70: {
! 71:
! 72: switch (minor(dev)) {
! 73: case 0:
! 74: case 1:
! 75: case 2:
! 76: case 12:
! 77: return (0);
! 78: default:
! 79: return (ENXIO);
! 80: }
! 81: }
! 82:
! 83: /*ARGSUSED*/
! 84: int
! 85: mmclose(dev, flag, mode, p)
! 86: dev_t dev;
! 87: int flag, mode;
! 88: struct proc *p;
! 89: {
! 90:
! 91: return (0);
! 92: }
! 93:
! 94: /*ARGSUSED*/
! 95: int
! 96: mmrw(dev, uio, flags)
! 97: dev_t dev;
! 98: struct uio *uio;
! 99: int flags;
! 100: {
! 101: vaddr_t o, v;
! 102: int c;
! 103: struct iovec *iov;
! 104: int error = 0;
! 105: static int physlock;
! 106: vm_prot_t prot;
! 107:
! 108: if (minor(dev) == 0) {
! 109: /* lock against other uses of shared vmmap */
! 110: while (physlock > 0) {
! 111: physlock++;
! 112: error = tsleep((caddr_t)&physlock, PZERO | PCATCH,
! 113: "mmrw", 0);
! 114: if (error)
! 115: return (error);
! 116: }
! 117: physlock = 1;
! 118: }
! 119: while (uio->uio_resid > 0 && error == 0) {
! 120: iov = uio->uio_iov;
! 121: if (iov->iov_len == 0) {
! 122: uio->uio_iov++;
! 123: uio->uio_iovcnt--;
! 124: if (uio->uio_iovcnt < 0)
! 125: panic("mmrw");
! 126: continue;
! 127: }
! 128: switch (minor(dev)) {
! 129:
! 130: /* minor device 0 is physical memory */
! 131: case 0:
! 132: v = uio->uio_offset;
! 133:
! 134: /*
! 135: * Only allow reads in physical RAM.
! 136: */
! 137: if (v >= maxaddr || v < 0) {
! 138: error = EFAULT;
! 139: goto unlock;
! 140: }
! 141:
! 142: prot = uio->uio_rw == UIO_READ ? VM_PROT_READ :
! 143: VM_PROT_WRITE;
! 144: pmap_enter(pmap_kernel(), (vaddr_t)vmmap,
! 145: trunc_page(v), prot, prot|PMAP_WIRED);
! 146: pmap_update(pmap_kernel());
! 147: o = m68k_page_offset(uio->uio_offset);
! 148: c = min(uio->uio_resid, (int)(NBPG - o));
! 149: error = uiomove((caddr_t)vmmap + o, c, uio);
! 150: pmap_remove(pmap_kernel(), (vaddr_t)vmmap,
! 151: (vaddr_t)vmmap + NBPG);
! 152: pmap_update(pmap_kernel());
! 153: continue;
! 154:
! 155: /* minor device 1 is kernel memory */
! 156: case 1:
! 157: v = uio->uio_offset;
! 158: c = min(iov->iov_len, MAXPHYS);
! 159: if (!uvm_kernacc((caddr_t)v, c,
! 160: uio->uio_rw == UIO_READ ? B_READ : B_WRITE))
! 161: return (EFAULT);
! 162: error = uiomove((caddr_t)v, c, uio);
! 163: continue;
! 164:
! 165: /* minor device 2 is EOF/RATHOLE */
! 166: case 2:
! 167: if (uio->uio_rw == UIO_WRITE)
! 168: uio->uio_resid = 0;
! 169: return (0);
! 170:
! 171: /* minor device 12 (/dev/zero) is source of nulls on read, rathole on write */
! 172: case 12:
! 173: if (uio->uio_rw == UIO_WRITE) {
! 174: c = iov->iov_len;
! 175: break;
! 176: }
! 177:
! 178: /*
! 179: * On the first call, allocate and zero a page
! 180: * of memory for use with /dev/zero.
! 181: */
! 182: if (devzeropage == NULL) {
! 183: devzeropage = (caddr_t)
! 184: malloc(PAGE_SIZE, M_TEMP, M_WAITOK);
! 185: bzero(devzeropage, PAGE_SIZE);
! 186: }
! 187: c = min(iov->iov_len, PAGE_SIZE);
! 188: error = uiomove(devzeropage, c, uio);
! 189: continue;
! 190:
! 191: default:
! 192: return (ENXIO);
! 193: }
! 194: if (error)
! 195: break;
! 196: iov->iov_base = (caddr_t)iov->iov_base + c;
! 197: iov->iov_len -= c;
! 198: uio->uio_offset += c;
! 199: uio->uio_resid -= c;
! 200: }
! 201: if (minor(dev) == 0) {
! 202: unlock:
! 203: if (physlock > 1)
! 204: wakeup((caddr_t)&physlock);
! 205: physlock = 0;
! 206: }
! 207: return (error);
! 208: }
! 209:
! 210: paddr_t
! 211: mmmmap(dev, off, prot)
! 212: dev_t dev;
! 213: off_t off;
! 214: int prot;
! 215: {
! 216: /*
! 217: * /dev/mem is the only one that makes sense through this
! 218: * interface. For /dev/kmem any physaddr we return here
! 219: * could be transient and hence incorrect or invalid at
! 220: * a later time. /dev/null just doesn't make any sense
! 221: * and /dev/zero is a hack that is handled via the default
! 222: * pager in mmap().
! 223: */
! 224: if (minor(dev) != 0)
! 225: return (-1);
! 226:
! 227: /*
! 228: * Only allow access to physical RAM.
! 229: */
! 230: if ((u_int)off >= maxaddr)
! 231: return (-1);
! 232:
! 233: return (atop((u_int)off));
! 234: }
! 235:
! 236: int
! 237: mmioctl(dev, cmd, data, flags, p)
! 238: dev_t dev;
! 239: u_long cmd;
! 240: caddr_t data;
! 241: int flags;
! 242: struct proc *p;
! 243: {
! 244: return (EOPNOTSUPP);
! 245: }
CVSweb