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 * 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 * from: Utah $Hdr: mem.c 1.13 89/10/08$ 40 * from: @(#)mem.c 7.2 (Berkeley) 5/9/91 41 * $Id: mem.c,v 1.8 1994/05/25 08:54:24 rgrimes Exp $ 42 */ 43 44 /* 45 * Memory special file 46 */ 47 48 #include <sys/param.h> 49 #include <sys/conf.h> 50 #include <sys/buf.h> 51 #include <sys/systm.h> 52 #include <sys/uio.h> 53 #include <sys/malloc.h> 54 #include <sys/proc.h> 55 56 #include <machine/cpu.h> 57 #include <machine/psl.h> 58 59 #include <vm/vm.h> 60 #include <vm/vm_param.h> 61 #include <vm/lock.h> 62 #include <vm/vm_prot.h> 63 #include <vm/pmap.h> 64 65 extern char *ptvmmap; /* poor name! */ 66 /*ARGSUSED*/ 67 int 68 mmclose(dev, uio, flags) 69 dev_t dev; 70 struct uio *uio; 71 int flags; 72 { 73 struct trapframe *fp; 74 75 switch (minor(dev)) { 76 case 14: 77 fp = (struct trapframe *)curproc->p_md.md_regs; 78 fp->tf_eflags &= ~PSL_IOPL; 79 break; 80 default: 81 break; 82 } 83 return(0); 84 } 85 /*ARGSUSED*/ 86 int 87 mmopen(dev, uio, flags) 88 dev_t dev; 89 struct uio *uio; 90 int flags; 91 { 92 struct trapframe *fp; 93 94 switch (minor(dev)) { 95 case 14: 96 fp = (struct trapframe *)curproc->p_md.md_regs; 97 fp->tf_eflags |= PSL_IOPL; 98 break; 99 default: 100 break; 101 } 102 return(0); 103 } 104 /*ARGSUSED*/ 105 int 106 mmrw(dev, uio, flags) 107 dev_t dev; 108 struct uio *uio; 109 int flags; 110 { 111 register int o; 112 register u_int c, v; 113 register struct iovec *iov; 114 int error = 0; 115 caddr_t zbuf = NULL; 116 117 while (uio->uio_resid > 0 && error == 0) { 118 iov = uio->uio_iov; 119 if (iov->iov_len == 0) { 120 uio->uio_iov++; 121 uio->uio_iovcnt--; 122 if (uio->uio_iovcnt < 0) 123 panic("mmrw"); 124 continue; 125 } 126 switch (minor(dev)) { 127 128 /* minor device 0 is physical memory */ 129 case 0: 130 v = uio->uio_offset; 131 pmap_enter(kernel_pmap, (vm_offset_t)ptvmmap, v, 132 uio->uio_rw == UIO_READ ? VM_PROT_READ : VM_PROT_WRITE, 133 TRUE); 134 o = (int)uio->uio_offset & PGOFSET; 135 c = (u_int)(NBPG - ((int)iov->iov_base & PGOFSET)); 136 c = min(c, (u_int)(NBPG - o)); 137 c = min(c, (u_int)iov->iov_len); 138 error = uiomove((caddr_t)&ptvmmap[o], (int)c, uio); 139 pmap_remove(kernel_pmap, (vm_offset_t)ptvmmap, 140 (vm_offset_t)&ptvmmap[NBPG]); 141 continue; 142 143 /* minor device 1 is kernel memory */ 144 case 1: 145 c = iov->iov_len; 146 if (!kernacc((caddr_t)(int)uio->uio_offset, c, 147 uio->uio_rw == UIO_READ ? B_READ : B_WRITE)) 148 return(EFAULT); 149 error = uiomove((caddr_t)(int)uio->uio_offset, (int)c, uio); 150 continue; 151 152 /* minor device 2 is EOF/RATHOLE */ 153 case 2: 154 if (uio->uio_rw == UIO_READ) 155 return (0); 156 c = iov->iov_len; 157 break; 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 (zbuf == NULL) { 166 zbuf = (caddr_t) 167 malloc(CLBYTES, M_TEMP, M_WAITOK); 168 bzero(zbuf, CLBYTES); 169 } 170 c = min(iov->iov_len, CLBYTES); 171 error = uiomove(zbuf, (int)c, uio); 172 continue; 173 174 #ifdef notyet 175 /* 386 I/O address space (/dev/ioport[bwl]) is a read/write access to seperate 176 i/o device address bus, different than memory bus. Semantics here are 177 very different than ordinary read/write, as if iov_len is a multiple 178 an implied string move from a single port will be done. Note that lseek 179 must be used to set the port number reliably. */ 180 case 14: 181 if (iov->iov_len == 1) { 182 u_char tmp; 183 tmp = inb(uio->uio_offset); 184 error = uiomove (&tmp, iov->iov_len, uio); 185 } else { 186 if (!useracc((caddr_t)iov->iov_base, 187 iov->iov_len, uio->uio_rw)) 188 return (EFAULT); 189 insb(uio->uio_offset, iov->iov_base, 190 iov->iov_len); 191 } 192 break; 193 case 15: 194 if (iov->iov_len == sizeof (short)) { 195 u_short tmp; 196 tmp = inw(uio->uio_offset); 197 error = uiomove (&tmp, iov->iov_len, uio); 198 } else { 199 if (!useracc((caddr_t)iov->iov_base, 200 iov->iov_len, uio->uio_rw)) 201 return (EFAULT); 202 insw(uio->uio_offset, iov->iov_base, 203 iov->iov_len/ sizeof (short)); 204 } 205 break; 206 case 16: 207 if (iov->iov_len == sizeof (long)) { 208 u_long tmp; 209 tmp = inl(uio->uio_offset); 210 error = uiomove (&tmp, iov->iov_len, uio); 211 } else { 212 if (!useracc((caddr_t)iov->iov_base, 213 iov->iov_len, uio->uio_rw)) 214 return (EFAULT); 215 insl(uio->uio_offset, iov->iov_base, 216 iov->iov_len/ sizeof (long)); 217 } 218 break; 219 #endif 220 221 default: 222 return (ENXIO); 223 } 224 if (error) 225 break; 226 iov->iov_base += c; 227 iov->iov_len -= c; 228 uio->uio_offset += c; 229 uio->uio_resid -= c; 230 } 231 if (zbuf) 232 free(zbuf, M_TEMP); 233 return (error); 234 } 235 236 237 238 239 /*******************************************************\ 240 * allow user processes to MMAP some memory sections * 241 * instead of going through read/write * 242 \*******************************************************/ 243 int memmmap(dev_t dev, int offset, int nprot) 244 { 245 switch (minor(dev)) 246 { 247 248 /* minor device 0 is physical memory */ 249 case 0: 250 return i386_btop(offset); 251 252 /* minor device 1 is kernel memory */ 253 case 1: 254 return i386_btop(vtophys(offset)); 255 256 default: 257 return -1; 258 } 259 } 260 261