1 /* 2 * Copyright (c) 1988 University of Utah. 3 * Copyright (c) 1992 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 Ralph Campbell. 9 * 10 * %sccs.include.redist.c% 11 * 12 * from: Utah $Hdr: mem.c 1.14 90/10/12$ 13 * 14 * @(#)mem.c 7.2 (Berkeley) 02/29/92 15 */ 16 17 /* 18 * Memory special file 19 */ 20 21 #include "param.h" 22 #include "conf.h" 23 #include "buf.h" 24 #include "systm.h" 25 #include "malloc.h" 26 27 #include "../include/cpu.h" 28 29 #include "vm/vm_param.h" 30 #include "vm/lock.h" 31 #include "vm/pmap.h" 32 #include "vm/vm_prot.h" 33 34 /*ARGSUSED*/ 35 mmrw(dev, uio, flags) 36 dev_t dev; 37 struct uio *uio; 38 int flags; 39 { 40 register int o; 41 register u_int c, v; 42 register struct iovec *iov; 43 int error = 0; 44 caddr_t zbuf = NULL; 45 extern vm_offset_t avail_end; 46 47 while (uio->uio_resid > 0 && error == 0) { 48 iov = uio->uio_iov; 49 if (iov->iov_len == 0) { 50 uio->uio_iov++; 51 uio->uio_iovcnt--; 52 if (uio->uio_iovcnt < 0) 53 panic("mmrw"); 54 continue; 55 } 56 switch (minor(dev)) { 57 58 /* minor device 0 is physical memory */ 59 case 0: 60 v = uio->uio_offset; 61 c = iov->iov_len; 62 if (v + c >= physmem) 63 return (EFAULT); 64 error = uiomove((caddr_t)(MACH_CACHED_MEMORY_ADDR + v), 65 (int)c, uio); 66 continue; 67 68 /* minor device 1 is kernel memory */ 69 case 1: 70 if (uio->uio_offset < MACH_CACHED_MEMORY_ADDR) 71 return (EFAULT); 72 c = iov->iov_len; 73 if (uio->uio_offset + c <= avail_end || 74 uio->uio_offset >= MACH_KSEG2_ADDR && 75 kernacc((caddr_t)uio->uio_offset, c, 76 uio->uio_rw == UIO_READ ? B_READ : B_WRITE)) { 77 error = uiomove((caddr_t)uio->uio_offset, 78 (int)c, uio); 79 continue; 80 } 81 return (EFAULT); 82 83 /* minor device 2 is EOF/RATHOLE */ 84 case 2: 85 if (uio->uio_rw == UIO_WRITE) 86 uio->uio_resid = 0; 87 return (0); 88 89 /* minor device 12 (/dev/zero) is source of nulls on read, rathole on write */ 90 case 12: 91 if (uio->uio_rw == UIO_WRITE) { 92 c = iov->iov_len; 93 break; 94 } 95 if (zbuf == NULL) { 96 zbuf = (caddr_t) 97 malloc(CLBYTES, M_TEMP, M_WAITOK); 98 bzero(zbuf, CLBYTES); 99 } 100 c = MIN(iov->iov_len, CLBYTES); 101 error = uiomove(zbuf, (int)c, uio); 102 continue; 103 104 default: 105 return (ENXIO); 106 } 107 if (error) 108 break; 109 iov->iov_base += c; 110 iov->iov_len -= c; 111 uio->uio_offset += c; 112 uio->uio_resid -= c; 113 } 114 if (zbuf) 115 free(zbuf, M_TEMP); 116 return (error); 117 } 118