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