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. 9 * 10 * %sccs.include.redist.c% 11 * 12 * from: Utah $Hdr: mem.c 1.14 90/10/12$ 13 * 14 * @(#)mem.c 7.11 (Berkeley) 10/11/92 15 */ 16 17 /* 18 * Memory special file 19 */ 20 21 #include <sys/param.h> 22 #include <sys/conf.h> 23 #include <sys/buf.h> 24 #include <sys/systm.h> 25 #include <sys/malloc.h> 26 27 #include <machine/cpu.h> 28 29 #include <vm/vm_param.h> 30 #include <vm/lock.h> 31 #include <vm/vm_prot.h> 32 #include <vm/pmap.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 u_int lowram; 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 #ifndef DEBUG 62 /* allow reads only in RAM (except for DEBUG) */ 63 if (v >= 0xFFFFFFFC || v < lowram) 64 return (EFAULT); 65 #endif 66 pmap_enter(kernel_pmap, (vm_offset_t)vmmap, 67 trunc_page(v), uio->uio_rw == UIO_READ ? 68 VM_PROT_READ : VM_PROT_WRITE, TRUE); 69 o = (int)uio->uio_offset & PGOFSET; 70 c = (u_int)(NBPG - ((int)iov->iov_base & PGOFSET)); 71 c = min(c, (u_int)(NBPG - o)); 72 c = min(c, (u_int)iov->iov_len); 73 error = uiomove((caddr_t)&vmmap[o], (int)c, uio); 74 pmap_remove(kernel_pmap, (vm_offset_t)vmmap, 75 (vm_offset_t)&vmmap[NBPG]); 76 continue; 77 78 /* minor device 1 is kernel memory */ 79 case 1: 80 c = min(iov->iov_len, MAXPHYS); 81 if (!kernacc((caddr_t)uio->uio_offset, c, 82 uio->uio_rw == UIO_READ ? B_READ : B_WRITE)) 83 return (EFAULT); 84 error = uiomove((caddr_t)uio->uio_offset, (int)c, uio); 85 continue; 86 87 /* minor device 2 is EOF/RATHOLE */ 88 case 2: 89 if (uio->uio_rw == UIO_WRITE) 90 uio->uio_resid = 0; 91 return (0); 92 93 /* minor device 12 (/dev/zero) is source of nulls on read, rathole on write */ 94 case 12: 95 if (uio->uio_rw == UIO_WRITE) { 96 c = iov->iov_len; 97 break; 98 } 99 if (zbuf == NULL) { 100 zbuf = (caddr_t) 101 malloc(CLBYTES, M_TEMP, M_WAITOK); 102 bzero(zbuf, CLBYTES); 103 } 104 c = min(iov->iov_len, CLBYTES); 105 error = uiomove(zbuf, (int)c, uio); 106 continue; 107 108 default: 109 return (ENXIO); 110 } 111 if (error) 112 break; 113 iov->iov_base += c; 114 iov->iov_len -= c; 115 uio->uio_offset += c; 116 uio->uio_resid -= c; 117 } 118 if (zbuf) 119 free(zbuf, M_TEMP); 120 return (error); 121 } 122