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