1 /* $NetBSD: kern_ras.c,v 1.12 2005/11/25 11:29:48 hannken 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.12 2005/11/25 11:29:48 hannken 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 #include <sys/sa.h> 49 #include <sys/savar.h> 50 51 #include <sys/mount.h> 52 #include <sys/syscallargs.h> 53 54 #include <uvm/uvm_extern.h> 55 56 #define MAX_RAS_PER_PROC 16 57 58 u_int ras_per_proc = MAX_RAS_PER_PROC; 59 60 #ifdef DEBUG 61 int ras_debug = 0; 62 #define DPRINTF(x) if (ras_debug) printf x 63 #else 64 #define DPRINTF(x) /* nothing */ 65 #endif 66 67 /* 68 * Check the specified address to see if it is within the 69 * sequence. If it is found, we return the restart address, 70 * otherwise we return -1. If we do perform a restart, we 71 * mark the sequence as hit. 72 */ 73 caddr_t 74 ras_lookup(struct proc *p, caddr_t addr) 75 { 76 struct ras *rp; 77 78 #ifdef DIAGNOSTIC 79 if (addr < (caddr_t)VM_MIN_ADDRESS || 80 addr > (caddr_t)VM_MAXUSER_ADDRESS) 81 return ((caddr_t)-1); 82 #endif 83 84 simple_lock(&p->p_lock); 85 LIST_FOREACH(rp, &p->p_raslist, ras_list) { 86 if (addr > rp->ras_startaddr && addr < rp->ras_endaddr) { 87 rp->ras_hits++; 88 simple_unlock(&p->p_lock); 89 #ifdef DIAGNOSTIC 90 DPRINTF(("RAS hit: p=%p %p\n", p, addr)); 91 #endif 92 return (rp->ras_startaddr); 93 } 94 } 95 simple_unlock(&p->p_lock); 96 97 return ((caddr_t)-1); 98 } 99 100 /* 101 * During a fork, we copy all of the sequences from parent p1 to 102 * the child p2. 103 */ 104 int 105 ras_fork(struct proc *p1, struct proc *p2) 106 { 107 struct ras *rp, *nrp; 108 int nras; 109 110 again: 111 /* 112 * first, try to shortcut. 113 */ 114 115 if (LIST_EMPTY(&p1->p_raslist)) 116 return (0); 117 118 /* 119 * count entries. 120 */ 121 122 nras = 0; 123 simple_lock(&p1->p_lock); 124 LIST_FOREACH(rp, &p1->p_raslist, ras_list) 125 nras++; 126 simple_unlock(&p1->p_lock); 127 128 /* 129 * allocate entries. 130 */ 131 132 for ( ; nras > 0; nras--) { 133 nrp = pool_get(&ras_pool, PR_WAITOK); 134 nrp->ras_hits = 0; 135 LIST_INSERT_HEAD(&p2->p_raslist, nrp, ras_list); 136 } 137 138 /* 139 * copy entries. 140 */ 141 142 simple_lock(&p1->p_lock); 143 nrp = LIST_FIRST(&p2->p_raslist); 144 LIST_FOREACH(rp, &p1->p_raslist, ras_list) { 145 if (nrp == NULL) 146 break; 147 nrp->ras_startaddr = rp->ras_startaddr; 148 nrp->ras_endaddr = rp->ras_endaddr; 149 nrp = LIST_NEXT(nrp, ras_list); 150 } 151 simple_unlock(&p1->p_lock); 152 153 /* 154 * if we lose a race, retry. 155 */ 156 157 if (rp != NULL || nrp != NULL) { 158 ras_purgeall(p2); 159 goto again; 160 } 161 162 DPRINTF(("ras_fork: p1=%p, p2=%p, nras=%d\n", p1, p2, nras)); 163 164 return (0); 165 } 166 167 /* 168 * Nuke all sequences for this process. 169 */ 170 int 171 ras_purgeall(struct proc *p) 172 { 173 struct ras *rp; 174 175 simple_lock(&p->p_lock); 176 while (!LIST_EMPTY(&p->p_raslist)) { 177 rp = LIST_FIRST(&p->p_raslist); 178 DPRINTF(("RAS %p-%p, hits %d\n", rp->ras_startaddr, 179 rp->ras_endaddr, rp->ras_hits)); 180 LIST_REMOVE(rp, ras_list); 181 pool_put(&ras_pool, rp); 182 } 183 simple_unlock(&p->p_lock); 184 185 return (0); 186 } 187 188 #if defined(__HAVE_RAS) 189 190 /* 191 * Install the new sequence. If it already exists, return 192 * an error. 193 */ 194 static int 195 ras_install(struct proc *p, caddr_t addr, size_t len) 196 { 197 struct ras *rp; 198 struct ras *newrp; 199 caddr_t endaddr = addr + len; 200 int nras = 0; 201 202 if (addr < (caddr_t)VM_MIN_ADDRESS || 203 endaddr > (caddr_t)VM_MAXUSER_ADDRESS) 204 return (EINVAL); 205 206 if (len <= 0) 207 return (EINVAL); 208 209 newrp = NULL; 210 again: 211 simple_lock(&p->p_lock); 212 LIST_FOREACH(rp, &p->p_raslist, ras_list) { 213 if (++nras >= ras_per_proc || 214 (addr < rp->ras_endaddr && endaddr > rp->ras_startaddr)) { 215 simple_unlock(&p->p_lock); 216 return (EINVAL); 217 } 218 } 219 if (newrp == NULL) { 220 simple_unlock(&p->p_lock); 221 newrp = pool_get(&ras_pool, PR_WAITOK); 222 goto again; 223 } 224 newrp->ras_startaddr = addr; 225 newrp->ras_endaddr = endaddr; 226 newrp->ras_hits = 0; 227 LIST_INSERT_HEAD(&p->p_raslist, newrp, ras_list); 228 simple_unlock(&p->p_lock); 229 230 return (0); 231 } 232 233 /* 234 * Nuke the specified sequence. Both address and len must 235 * match, otherwise we return an error. 236 */ 237 static int 238 ras_purge(struct proc *p, caddr_t addr, size_t len) 239 { 240 struct ras *rp; 241 caddr_t endaddr = addr + len; 242 int error = ESRCH; 243 244 simple_lock(&p->p_lock); 245 LIST_FOREACH(rp, &p->p_raslist, ras_list) { 246 if (addr == rp->ras_startaddr && endaddr == rp->ras_endaddr) { 247 LIST_REMOVE(rp, ras_list); 248 pool_put(&ras_pool, rp); 249 error = 0; 250 break; 251 } 252 } 253 simple_unlock(&p->p_lock); 254 255 return (error); 256 } 257 258 #endif /* defined(__HAVE_RAS) */ 259 260 /*ARGSUSED*/ 261 int 262 sys_rasctl(struct lwp *l, void *v, register_t *retval) 263 { 264 265 #if defined(__HAVE_RAS) 266 267 struct sys_rasctl_args /* { 268 syscallarg(caddr_t) addr; 269 syscallarg(size_t) len; 270 syscallarg(int) op; 271 } */ *uap = v; 272 struct proc *p = l->l_proc; 273 caddr_t addr; 274 size_t len; 275 int op; 276 int error; 277 278 /* 279 * first, extract syscall args from the uap. 280 */ 281 282 addr = (caddr_t)SCARG(uap, addr); 283 len = (size_t)SCARG(uap, len); 284 op = SCARG(uap, op); 285 286 DPRINTF(("sys_rasctl: p=%p addr=%p, len=%ld, op=0x%x\n", 287 p, addr, (long)len, op)); 288 289 switch (op) { 290 case RAS_INSTALL: 291 error = ras_install(p, addr, len); 292 break; 293 case RAS_PURGE: 294 error = ras_purge(p, addr, len); 295 break; 296 case RAS_PURGE_ALL: 297 error = ras_purgeall(p); 298 break; 299 default: 300 error = EINVAL; 301 break; 302 } 303 304 return (error); 305 306 #else 307 308 return (EOPNOTSUPP); 309 310 #endif 311 312 } 313