1 /* $NetBSD: kern_ras.c,v 1.3 2002/11/09 20:15:54 thorpej Exp $ */ 2 3 /*- 4 * Copyright (c) 2002 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Gregory McGarry. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 #include <sys/cdefs.h> 40 __KERNEL_RCSID(0, "$NetBSD: kern_ras.c,v 1.3 2002/11/09 20:15:54 thorpej Exp $"); 41 42 #include <sys/param.h> 43 #include <sys/lock.h> 44 #include <sys/systm.h> 45 #include <sys/pool.h> 46 #include <sys/proc.h> 47 #include <sys/ras.h> 48 49 #include <sys/mount.h> 50 #include <sys/syscallargs.h> 51 52 #include <uvm/uvm_extern.h> 53 54 #define MAX_RAS_PER_PROC 16 55 56 u_int ras_per_proc = MAX_RAS_PER_PROC; 57 58 #ifdef DEBUG 59 int ras_debug = 0; 60 #define DPRINTF(x) if (ras_debug) printf x 61 #else 62 #define DPRINTF(x) /* nothing */ 63 #endif 64 65 int ras_install(struct proc *, caddr_t, size_t); 66 int ras_purge(struct proc *, caddr_t, size_t); 67 68 extern struct pool ras_pool; 69 70 /* 71 * Check the specified address to see if it is within the 72 * sequence. If it is found, we return the restart address, 73 * otherwise we return -1. If we do perform a restart, we 74 * mark the sequence as hit. 75 */ 76 caddr_t 77 ras_lookup(struct proc *p, caddr_t addr) 78 { 79 struct ras *rp; 80 81 #ifdef DIAGNOSTIC 82 if (addr < (caddr_t)VM_MIN_ADDRESS || 83 addr > (caddr_t)VM_MAXUSER_ADDRESS) 84 return ((caddr_t)-1); 85 #endif 86 87 simple_lock(&p->p_raslock); 88 LIST_FOREACH(rp, &p->p_raslist, ras_list) { 89 if (addr > rp->ras_startaddr && addr < rp->ras_endaddr) { 90 rp->ras_hits++; 91 simple_unlock(&p->p_raslock); 92 #ifdef DIAGNOSTIC 93 DPRINTF(("RAS hit: p=%p %p\n", p, addr)); 94 #endif 95 return (rp->ras_startaddr); 96 } 97 } 98 simple_unlock(&p->p_raslock); 99 100 return ((caddr_t)-1); 101 } 102 103 /* 104 * During a fork, we copy all of the sequences from parent p1 to 105 * the child p2. 106 */ 107 int 108 ras_fork(struct proc *p1, struct proc *p2) 109 { 110 struct ras *rp, *nrp; 111 112 DPRINTF(("ras_fork: p1=%p, p2=%p, p1->p_nras=%d\n", 113 p1, p2, p1->p_nras)); 114 115 simple_lock(&p1->p_raslock); 116 LIST_FOREACH(rp, &p1->p_raslist, ras_list) { 117 nrp = pool_get(&ras_pool, PR_NOWAIT); 118 nrp->ras_startaddr = rp->ras_startaddr; 119 nrp->ras_endaddr = rp->ras_endaddr; 120 nrp->ras_hits = 0; 121 LIST_INSERT_HEAD(&p2->p_raslist, nrp, ras_list); 122 } 123 p2->p_nras = p1->p_nras; 124 simple_unlock(&p1->p_raslock); 125 126 return (0); 127 } 128 129 /* 130 * Nuke all sequences for this process. 131 */ 132 int 133 ras_purgeall(struct proc *p) 134 { 135 struct ras *rp; 136 137 simple_lock(&p->p_raslock); 138 while (!LIST_EMPTY(&p->p_raslist)) { 139 rp = LIST_FIRST(&p->p_raslist); 140 DPRINTF(("RAS %p-%p, hits %d\n", rp->ras_startaddr, 141 rp->ras_endaddr, rp->ras_hits)); 142 LIST_REMOVE(rp, ras_list); 143 pool_put(&ras_pool, rp); 144 } 145 p->p_nras = 0; 146 simple_unlock(&p->p_raslock); 147 148 return (0); 149 } 150 151 /* 152 * Install the new sequence. If it already exists, return 153 * an error. 154 */ 155 int 156 ras_install(struct proc *p, caddr_t addr, size_t len) 157 { 158 struct ras *rp; 159 caddr_t endaddr = addr + len; 160 161 if (addr < (caddr_t)VM_MIN_ADDRESS || 162 addr > (caddr_t)VM_MAXUSER_ADDRESS) 163 return (EINVAL); 164 165 if (len <= 0) 166 return (EINVAL); 167 168 if (p->p_nras >= ras_per_proc) 169 return (EINVAL); 170 171 simple_lock(&p->p_raslock); 172 LIST_FOREACH(rp, &p->p_raslist, ras_list) { 173 if ((addr > rp->ras_startaddr && addr < rp->ras_endaddr) || 174 (endaddr > rp->ras_startaddr && 175 endaddr < rp->ras_endaddr) || 176 (addr < rp->ras_startaddr && endaddr > rp->ras_endaddr)) { 177 simple_unlock(&p->p_raslock); 178 return (EINVAL); 179 } 180 } 181 rp = pool_get(&ras_pool, PR_NOWAIT); 182 rp->ras_startaddr = addr; 183 rp->ras_endaddr = endaddr; 184 rp->ras_hits = 0; 185 LIST_INSERT_HEAD(&p->p_raslist, rp, ras_list); 186 p->p_nras++; 187 simple_unlock(&p->p_raslock); 188 189 return (0); 190 } 191 192 /* 193 * Nuke the specified sequence. Both address and len must 194 * match, otherwise we return an error. 195 */ 196 int 197 ras_purge(struct proc *p, caddr_t addr, size_t len) 198 { 199 struct ras *rp; 200 caddr_t endaddr = addr + len; 201 int error = ESRCH; 202 203 simple_lock(&p->p_raslock); 204 LIST_FOREACH(rp, &p->p_raslist, ras_list) { 205 if (addr == rp->ras_startaddr && endaddr == rp->ras_endaddr) { 206 LIST_REMOVE(rp, ras_list); 207 pool_put(&ras_pool, rp); 208 p->p_nras--; 209 error = 0; 210 break; 211 } 212 } 213 simple_unlock(&p->p_raslock); 214 215 return (error); 216 } 217 218 /*ARGSUSED*/ 219 int 220 sys_rasctl(struct proc *p, void *v, register_t *retval) 221 { 222 223 #if defined(__HAVE_RAS) 224 225 struct sys_rasctl_args /* { 226 syscallarg(caddr_t) addr; 227 syscallarg(size_t) len; 228 syscallarg(int) op; 229 } */ *uap = v; 230 caddr_t addr; 231 size_t len; 232 int op; 233 int error; 234 235 /* 236 * first, extract syscall args from the uap. 237 */ 238 239 addr = (caddr_t)SCARG(uap, addr); 240 len = (size_t)SCARG(uap, len); 241 op = SCARG(uap, op); 242 243 DPRINTF(("sys_rasctl: p=%p addr=%p, len=%ld, op=0x%x\n", 244 p, addr, (long)len, op)); 245 246 switch (op) { 247 case RAS_INSTALL: 248 error = ras_install(p, addr, len); 249 break; 250 case RAS_PURGE: 251 error = ras_purge(p, addr, len); 252 break; 253 case RAS_PURGE_ALL: 254 error = ras_purgeall(p); 255 break; 256 default: 257 error = EINVAL; 258 break; 259 } 260 261 return (error); 262 263 #else 264 265 return (EOPNOTSUPP); 266 267 #endif 268 269 } 270