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