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 * $FreeBSD$ 42 */ 43 44 /* 45 * Memory special file 46 */ 47 48 #include <sys/param.h> 49 #include <sys/conf.h> 50 #include <sys/fcntl.h> 51 #include <sys/ioccom.h> 52 #include <sys/kernel.h> 53 #include <sys/lock.h> 54 #include <sys/malloc.h> 55 #include <sys/memrange.h> 56 #include <sys/mutex.h> 57 #include <sys/proc.h> 58 #include <sys/signalvar.h> 59 #include <sys/systm.h> 60 #include <sys/uio.h> 61 62 #include <machine/db_machdep.h> 63 #include <machine/frame.h> 64 #include <machine/psl.h> 65 #include <machine/specialreg.h> 66 67 #include <vm/vm.h> 68 #include <vm/pmap.h> 69 #include <vm/vm_extern.h> 70 71 static dev_t memdev, kmemdev, iodev; 72 73 static d_open_t mmopen; 74 static d_close_t mmclose; 75 static d_read_t mmrw; 76 static d_ioctl_t mmioctl; 77 static d_mmap_t memmmap; 78 79 #define CDEV_MAJOR 2 80 static struct cdevsw mem_cdevsw = { 81 /* open */ mmopen, 82 /* close */ mmclose, 83 /* read */ mmrw, 84 /* write */ mmrw, 85 /* ioctl */ mmioctl, 86 /* poll */ (d_poll_t *)seltrue, 87 /* mmap */ memmmap, 88 /* strategy */ nostrategy, 89 /* name */ "mem", 90 /* maj */ CDEV_MAJOR, 91 /* dump */ nodump, 92 /* psize */ nopsize, 93 /* flags */ D_MEM, 94 }; 95 96 MALLOC_DEFINE(M_MEMDESC, "memdesc", "memory range descriptors"); 97 98 struct mem_range_softc mem_range_softc; 99 100 static int 101 mmclose(dev_t dev, int flags, int fmt, struct proc *p) 102 { 103 switch (minor(dev)) { 104 case 14: 105 p->p_frame->tf_eflags &= ~PSL_IOPL; 106 } 107 return (0); 108 } 109 110 static int 111 mmopen(dev_t dev, int flags, int fmt, struct proc *p) 112 { 113 int error; 114 115 switch (minor(dev)) { 116 case 0: 117 case 1: 118 if ((flags & FWRITE) && securelevel > 0) 119 return (EPERM); 120 break; 121 case 14: 122 error = suser(p); 123 if (error != 0) 124 return (error); 125 if (securelevel > 0) 126 return (EPERM); 127 p->p_frame->tf_eflags |= PSL_IOPL; 128 break; 129 } 130 return (0); 131 } 132 133 /*ARGSUSED*/ 134 static int 135 mmrw(dev_t dev, struct uio *uio, int flags) 136 { 137 int o; 138 u_int c = 0, v; 139 struct iovec *iov; 140 int error = 0; 141 vm_offset_t addr, eaddr; 142 143 while (uio->uio_resid > 0 && error == 0) { 144 iov = uio->uio_iov; 145 if (iov->iov_len == 0) { 146 uio->uio_iov++; 147 uio->uio_iovcnt--; 148 if (uio->uio_iovcnt < 0) 149 panic("mmrw"); 150 continue; 151 } 152 switch (minor(dev)) { 153 154 /* minor device 0 is physical memory */ 155 case 0: 156 v = uio->uio_offset; 157 v &= ~PAGE_MASK; 158 mtx_lock(&vm_mtx); 159 pmap_kenter((vm_offset_t)ptvmmap, v); 160 mtx_unlock(&vm_mtx); 161 o = (int)uio->uio_offset & PAGE_MASK; 162 c = (u_int)(PAGE_SIZE - ((int)iov->iov_base & PAGE_MASK)); 163 c = min(c, (u_int)(PAGE_SIZE - o)); 164 c = min(c, (u_int)iov->iov_len); 165 error = uiomove((caddr_t)&ptvmmap[o], (int)c, uio); 166 mtx_lock(&vm_mtx); 167 pmap_kremove((vm_offset_t)ptvmmap); 168 mtx_unlock(&vm_mtx); 169 continue; 170 171 /* minor device 1 is kernel memory */ 172 case 1: 173 c = iov->iov_len; 174 175 /* 176 * Make sure that all of the pages are currently resident so 177 * that we don't create any zero-fill pages. 178 */ 179 addr = trunc_page(uio->uio_offset); 180 eaddr = round_page(uio->uio_offset + c); 181 182 if (addr < (vm_offset_t)VADDR(PTDPTDI, 0)) 183 return EFAULT; 184 if (eaddr >= (vm_offset_t)VADDR(APTDPTDI, 0)) 185 return EFAULT; 186 mtx_lock(&vm_mtx); 187 for (; addr < eaddr; addr += PAGE_SIZE) 188 if (pmap_extract(kernel_pmap, addr) == 0) { 189 mtx_unlock(&vm_mtx); 190 return EFAULT; 191 } 192 193 if (!kernacc((caddr_t)(int)uio->uio_offset, c, 194 uio->uio_rw == UIO_READ ? 195 VM_PROT_READ : VM_PROT_WRITE)) { 196 mtx_unlock(&vm_mtx); 197 return (EFAULT); 198 } 199 mtx_unlock(&vm_mtx); 200 error = uiomove((caddr_t)(int)uio->uio_offset, (int)c, uio); 201 continue; 202 } 203 204 if (error) 205 break; 206 iov->iov_base += c; 207 iov->iov_len -= c; 208 uio->uio_offset += c; 209 uio->uio_resid -= c; 210 } 211 return (error); 212 } 213 214 /*******************************************************\ 215 * allow user processes to MMAP some memory sections * 216 * instead of going through read/write * 217 \*******************************************************/ 218 static int 219 memmmap(dev_t dev, vm_offset_t offset, int prot) 220 { 221 switch (minor(dev)) 222 { 223 224 /* minor device 0 is physical memory */ 225 case 0: 226 return i386_btop(offset); 227 228 /* minor device 1 is kernel memory */ 229 case 1: 230 return i386_btop(vtophys(offset)); 231 232 default: 233 return -1; 234 } 235 } 236 237 /* 238 * Operations for changing memory attributes. 239 * 240 * This is basically just an ioctl shim for mem_range_attr_get 241 * and mem_range_attr_set. 242 */ 243 static int 244 mmioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct proc *p) 245 { 246 int nd, error = 0; 247 struct mem_range_op *mo = (struct mem_range_op *)data; 248 struct mem_range_desc *md; 249 250 /* is this for us? */ 251 if ((cmd != MEMRANGE_GET) && 252 (cmd != MEMRANGE_SET)) 253 return (ENOTTY); 254 255 /* any chance we can handle this? */ 256 if (mem_range_softc.mr_op == NULL) 257 return (EOPNOTSUPP); 258 259 /* do we have any descriptors? */ 260 if (mem_range_softc.mr_ndesc == 0) 261 return (ENXIO); 262 263 switch (cmd) { 264 case MEMRANGE_GET: 265 nd = imin(mo->mo_arg[0], mem_range_softc.mr_ndesc); 266 if (nd > 0) { 267 md = (struct mem_range_desc *) 268 malloc(nd * sizeof(struct mem_range_desc), 269 M_MEMDESC, M_WAITOK); 270 error = mem_range_attr_get(md, &nd); 271 if (!error) 272 error = copyout(md, mo->mo_desc, 273 nd * sizeof(struct mem_range_desc)); 274 free(md, M_MEMDESC); 275 } else { 276 nd = mem_range_softc.mr_ndesc; 277 } 278 mo->mo_arg[0] = nd; 279 break; 280 281 case MEMRANGE_SET: 282 md = (struct mem_range_desc *)malloc(sizeof(struct mem_range_desc), 283 M_MEMDESC, M_WAITOK); 284 error = copyin(mo->mo_desc, md, sizeof(struct mem_range_desc)); 285 /* clamp description string */ 286 md->mr_owner[sizeof(md->mr_owner) - 1] = 0; 287 if (error == 0) 288 error = mem_range_attr_set(md, &mo->mo_arg[0]); 289 free(md, M_MEMDESC); 290 break; 291 } 292 return (error); 293 } 294 295 /* 296 * Implementation-neutral, kernel-callable functions for manipulating 297 * memory range attributes. 298 */ 299 int 300 mem_range_attr_get(struct mem_range_desc *mrd, int *arg) 301 { 302 /* can we handle this? */ 303 if (mem_range_softc.mr_op == NULL) 304 return (EOPNOTSUPP); 305 306 if (*arg == 0) { 307 *arg = mem_range_softc.mr_ndesc; 308 } 309 else { 310 bcopy(mem_range_softc.mr_desc, mrd, 311 (*arg) * sizeof(struct mem_range_desc)); 312 } 313 return (0); 314 } 315 316 int 317 mem_range_attr_set(struct mem_range_desc *mrd, int *arg) 318 { 319 /* can we handle this? */ 320 if (mem_range_softc.mr_op == NULL) 321 return (EOPNOTSUPP); 322 323 return (mem_range_softc.mr_op->set(&mem_range_softc, mrd, arg)); 324 } 325 326 #ifdef SMP 327 void 328 mem_range_AP_init(void) 329 { 330 if (mem_range_softc.mr_op && mem_range_softc.mr_op->initAP) 331 return (mem_range_softc.mr_op->initAP(&mem_range_softc)); 332 } 333 #endif 334 335 static int 336 mem_modevent(module_t mod, int type, void *data) 337 { 338 switch(type) { 339 case MOD_LOAD: 340 if (bootverbose) 341 printf("mem: <memory & I/O>\n"); 342 /* Initialise memory range handling */ 343 if (mem_range_softc.mr_op != NULL) 344 mem_range_softc.mr_op->init(&mem_range_softc); 345 346 memdev = make_dev(&mem_cdevsw, 0, UID_ROOT, GID_KMEM, 347 0640, "mem"); 348 kmemdev = make_dev(&mem_cdevsw, 1, UID_ROOT, GID_KMEM, 349 0640, "kmem"); 350 iodev = make_dev(&mem_cdevsw, 14, UID_ROOT, GID_WHEEL, 351 0600, "io"); 352 return 0; 353 354 case MOD_UNLOAD: 355 destroy_dev(memdev); 356 destroy_dev(kmemdev); 357 destroy_dev(iodev); 358 return 0; 359 360 case MOD_SHUTDOWN: 361 return 0; 362 363 default: 364 return EOPNOTSUPP; 365 } 366 } 367 368 DEV_MODULE(mem, mem_modevent, NULL); 369