Home | History | Annotate | Line # | Download | only in kern
kern_ras.c revision 1.30
      1  1.30    martin /*	$NetBSD: kern_ras.c,v 1.30 2008/04/28 20:24:03 martin Exp $	*/
      2   1.1  gmcgarry 
      3   1.1  gmcgarry /*-
      4  1.21        ad  * Copyright (c) 2002, 2006, 2007 The NetBSD Foundation, Inc.
      5   1.1  gmcgarry  * All rights reserved.
      6   1.1  gmcgarry  *
      7   1.1  gmcgarry  * This code is derived from software contributed to The NetBSD Foundation
      8  1.21        ad  * by Gregory McGarry, and by Andrew Doran.
      9   1.1  gmcgarry  *
     10   1.1  gmcgarry  * Redistribution and use in source and binary forms, with or without
     11   1.1  gmcgarry  * modification, are permitted provided that the following conditions
     12   1.1  gmcgarry  * are met:
     13   1.1  gmcgarry  * 1. Redistributions of source code must retain the above copyright
     14   1.1  gmcgarry  *    notice, this list of conditions and the following disclaimer.
     15   1.1  gmcgarry  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1  gmcgarry  *    notice, this list of conditions and the following disclaimer in the
     17   1.1  gmcgarry  *    documentation and/or other materials provided with the distribution.
     18   1.1  gmcgarry  *
     19   1.1  gmcgarry  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.1  gmcgarry  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.1  gmcgarry  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.1  gmcgarry  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.1  gmcgarry  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.1  gmcgarry  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.1  gmcgarry  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.1  gmcgarry  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.1  gmcgarry  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.1  gmcgarry  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.1  gmcgarry  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1  gmcgarry  */
     31   1.1  gmcgarry 
     32   1.1  gmcgarry #include <sys/cdefs.h>
     33  1.30    martin __KERNEL_RCSID(0, "$NetBSD: kern_ras.c,v 1.30 2008/04/28 20:24:03 martin Exp $");
     34   1.1  gmcgarry 
     35   1.1  gmcgarry #include <sys/param.h>
     36   1.1  gmcgarry #include <sys/systm.h>
     37  1.21        ad #include <sys/kernel.h>
     38   1.1  gmcgarry #include <sys/pool.h>
     39   1.1  gmcgarry #include <sys/proc.h>
     40   1.1  gmcgarry #include <sys/ras.h>
     41  1.21        ad #include <sys/xcall.h>
     42   1.1  gmcgarry #include <sys/syscallargs.h>
     43   1.1  gmcgarry 
     44   1.1  gmcgarry #include <uvm/uvm_extern.h>
     45   1.1  gmcgarry 
     46  1.13   thorpej POOL_INIT(ras_pool, sizeof(struct ras), 0, 0, 0, "raspl",
     47  1.18        ad     &pool_allocator_nointr, IPL_NONE);
     48  1.13   thorpej 
     49   1.1  gmcgarry #define MAX_RAS_PER_PROC	16
     50   1.1  gmcgarry 
     51   1.3   thorpej u_int ras_per_proc = MAX_RAS_PER_PROC;
     52   1.1  gmcgarry 
     53   1.1  gmcgarry #ifdef DEBUG
     54   1.1  gmcgarry int ras_debug = 0;
     55   1.1  gmcgarry #define DPRINTF(x)	if (ras_debug) printf x
     56   1.1  gmcgarry #else
     57   1.1  gmcgarry #define DPRINTF(x)	/* nothing */
     58   1.1  gmcgarry #endif
     59   1.1  gmcgarry 
     60   1.1  gmcgarry /*
     61  1.21        ad  * Force all CPUs through cpu_switchto(), waiting until complete.
     62  1.23        ad  * Context switching will drain the write buffer on the calling
     63  1.23        ad  * CPU.
     64  1.21        ad  */
     65  1.21        ad static void
     66  1.21        ad ras_sync(void)
     67  1.21        ad {
     68  1.21        ad 
     69  1.21        ad 	/* No need to sync if exiting or single threaded. */
     70  1.21        ad 	if (curproc->p_nlwps > 1 && ncpu > 1) {
     71  1.21        ad #ifdef NO_SOFTWARE_PATENTS
     72  1.21        ad 		uint64_t where;
     73  1.21        ad 		where = xc_broadcast(0, (xcfunc_t)nullop, NULL, NULL);
     74  1.21        ad 		xc_wait(where);
     75  1.21        ad #else
     76  1.23        ad 		/*
     77  1.23        ad 		 * Assumptions:
     78  1.23        ad 		 *
     79  1.23        ad 		 * o preemption is disabled by the thread in
     80  1.23        ad 		 *   ras_lookup().
     81  1.23        ad 		 * o proc::p_raslist is only inspected with
     82  1.23        ad 		 *   preemption disabled.
     83  1.23        ad 		 * o ras_lookup() plus loads reordered in advance
     84  1.23        ad 		 *   will take no longer than 1/8s to complete.
     85  1.23        ad 		 */
     86  1.23        ad 		const int delta = hz >> 3;
     87  1.23        ad 		int target = hardclock_ticks + delta;
     88  1.23        ad 		do {
     89  1.23        ad 			kpause("ras", false, delta, NULL);
     90  1.23        ad 		} while (hardclock_ticks < target);
     91  1.21        ad #endif
     92  1.21        ad 	}
     93  1.21        ad }
     94  1.21        ad 
     95  1.21        ad /*
     96   1.1  gmcgarry  * Check the specified address to see if it is within the
     97   1.1  gmcgarry  * sequence.  If it is found, we return the restart address,
     98   1.1  gmcgarry  * otherwise we return -1.  If we do perform a restart, we
     99   1.1  gmcgarry  * mark the sequence as hit.
    100  1.21        ad  *
    101  1.21        ad  * No locking required: we disable preemption and ras_sync()
    102  1.21        ad  * guarantees that individual entries are valid while we still
    103  1.21        ad  * have visibility of them.
    104   1.1  gmcgarry  */
    105  1.17  christos void *
    106  1.17  christos ras_lookup(struct proc *p, void *addr)
    107   1.1  gmcgarry {
    108   1.1  gmcgarry 	struct ras *rp;
    109  1.17  christos 	void *startaddr;
    110  1.29        ad 	lwp_t *l;
    111  1.16        ad 
    112  1.17  christos 	startaddr = (void *)-1;
    113  1.29        ad 	l = curlwp;
    114   1.1  gmcgarry 
    115  1.29        ad 	KPREEMPT_DISABLE(l);
    116  1.21        ad 	for (rp = p->p_raslist; rp != NULL; rp = rp->ras_next) {
    117   1.1  gmcgarry 		if (addr > rp->ras_startaddr && addr < rp->ras_endaddr) {
    118  1.16        ad 			startaddr = rp->ras_startaddr;
    119   1.1  gmcgarry 			DPRINTF(("RAS hit: p=%p %p\n", p, addr));
    120  1.16        ad 			break;
    121   1.1  gmcgarry 		}
    122   1.1  gmcgarry 	}
    123  1.29        ad 	KPREEMPT_ENABLE(l);
    124   1.1  gmcgarry 
    125  1.21        ad 	return startaddr;
    126   1.1  gmcgarry }
    127   1.1  gmcgarry 
    128   1.1  gmcgarry /*
    129   1.1  gmcgarry  * During a fork, we copy all of the sequences from parent p1 to
    130   1.1  gmcgarry  * the child p2.
    131  1.21        ad  *
    132  1.21        ad  * No locking required as the parent must be paused.
    133   1.1  gmcgarry  */
    134   1.1  gmcgarry int
    135   1.1  gmcgarry ras_fork(struct proc *p1, struct proc *p2)
    136   1.1  gmcgarry {
    137   1.1  gmcgarry 	struct ras *rp, *nrp;
    138   1.9      yamt 
    139  1.21        ad 	for (rp = p1->p_raslist; rp != NULL; rp = rp->ras_next) {
    140   1.7     pooka 		nrp = pool_get(&ras_pool, PR_WAITOK);
    141   1.1  gmcgarry 		nrp->ras_startaddr = rp->ras_startaddr;
    142   1.1  gmcgarry 		nrp->ras_endaddr = rp->ras_endaddr;
    143  1.27       dsl 		nrp->ras_next = p2->p_raslist;
    144  1.21        ad 		p2->p_raslist = nrp;
    145   1.9      yamt 	}
    146   1.9      yamt 
    147  1.22        ad 	DPRINTF(("ras_fork: p1=%p, p2=%p\n", p1, p2));
    148   1.1  gmcgarry 
    149  1.21        ad 	return 0;
    150   1.1  gmcgarry }
    151   1.1  gmcgarry 
    152   1.1  gmcgarry /*
    153   1.1  gmcgarry  * Nuke all sequences for this process.
    154   1.1  gmcgarry  */
    155   1.1  gmcgarry int
    156  1.21        ad ras_purgeall(void)
    157   1.1  gmcgarry {
    158  1.21        ad 	struct ras *rp, *nrp;
    159  1.21        ad 	proc_t *p;
    160  1.21        ad 
    161  1.21        ad 	p = curproc;
    162   1.1  gmcgarry 
    163  1.25        ad 	mutex_enter(&p->p_auxlock);
    164  1.21        ad 	if ((rp = p->p_raslist) != NULL) {
    165  1.21        ad 		p->p_raslist = NULL;
    166  1.21        ad 		ras_sync();
    167  1.21        ad 		for(; rp != NULL; rp = nrp) {
    168  1.21        ad 			nrp = rp->ras_next;
    169  1.21        ad 			pool_put(&ras_pool, rp);
    170  1.21        ad 		}
    171   1.1  gmcgarry 	}
    172  1.25        ad 	mutex_exit(&p->p_auxlock);
    173   1.1  gmcgarry 
    174  1.21        ad 	return 0;
    175   1.1  gmcgarry }
    176   1.1  gmcgarry 
    177  1.12   hannken #if defined(__HAVE_RAS)
    178  1.12   hannken 
    179   1.1  gmcgarry /*
    180   1.1  gmcgarry  * Install the new sequence.  If it already exists, return
    181   1.1  gmcgarry  * an error.
    182   1.1  gmcgarry  */
    183  1.11   thorpej static int
    184  1.21        ad ras_install(void *addr, size_t len)
    185   1.1  gmcgarry {
    186   1.1  gmcgarry 	struct ras *rp;
    187   1.8      yamt 	struct ras *newrp;
    188  1.21        ad 	void *endaddr;
    189  1.21        ad 	int nras, error;
    190  1.21        ad 	proc_t *p;
    191  1.21        ad 
    192  1.21        ad 	endaddr = (char *)addr + len;
    193   1.1  gmcgarry 
    194  1.17  christos 	if (addr < (void *)VM_MIN_ADDRESS ||
    195  1.17  christos 	    endaddr > (void *)VM_MAXUSER_ADDRESS)
    196   1.1  gmcgarry 		return (EINVAL);
    197   1.1  gmcgarry 
    198   1.1  gmcgarry 	if (len <= 0)
    199   1.1  gmcgarry 		return (EINVAL);
    200   1.1  gmcgarry 
    201  1.21        ad 	newrp = pool_get(&ras_pool, PR_WAITOK);
    202  1.21        ad 	newrp->ras_startaddr = addr;
    203  1.21        ad 	newrp->ras_endaddr = endaddr;
    204  1.21        ad 	error = 0;
    205  1.21        ad 	nras = 0;
    206  1.21        ad 	p = curproc;
    207  1.21        ad 
    208  1.25        ad 	mutex_enter(&p->p_auxlock);
    209  1.21        ad 	for (rp = p->p_raslist; rp != NULL; rp = rp->ras_next) {
    210  1.19   thorpej 		if (++nras >= ras_per_proc) {
    211  1.21        ad 			error = EINVAL;
    212  1.21        ad 			break;
    213   1.1  gmcgarry 		}
    214  1.19   thorpej 		if (addr < rp->ras_endaddr && endaddr > rp->ras_startaddr) {
    215  1.21        ad 			error = EEXIST;
    216  1.21        ad 			break;
    217  1.19   thorpej 		}
    218   1.1  gmcgarry 	}
    219  1.21        ad 	if (rp == NULL) {
    220  1.21        ad 		newrp->ras_next = p->p_raslist;
    221  1.21        ad 		p->p_raslist = newrp;
    222  1.21        ad 		ras_sync();
    223  1.25        ad 	 	mutex_exit(&p->p_auxlock);
    224  1.21        ad 	} else {
    225  1.25        ad 	 	mutex_exit(&p->p_auxlock);
    226  1.21        ad  		pool_put(&ras_pool, newrp);
    227   1.8      yamt 	}
    228   1.1  gmcgarry 
    229  1.21        ad 	return error;
    230   1.1  gmcgarry }
    231   1.1  gmcgarry 
    232   1.1  gmcgarry /*
    233   1.1  gmcgarry  * Nuke the specified sequence.  Both address and len must
    234   1.1  gmcgarry  * match, otherwise we return an error.
    235   1.1  gmcgarry  */
    236  1.11   thorpej static int
    237  1.21        ad ras_purge(void *addr, size_t len)
    238   1.1  gmcgarry {
    239  1.21        ad 	struct ras *rp, **link;
    240  1.21        ad 	void *endaddr;
    241  1.21        ad 	proc_t *p;
    242  1.21        ad 
    243  1.21        ad 	endaddr = (char *)addr + len;
    244  1.21        ad 	p = curproc;
    245  1.21        ad 
    246  1.25        ad 	mutex_enter(&p->p_auxlock);
    247  1.21        ad 	link = &p->p_raslist;
    248  1.21        ad 	for (rp = *link; rp != NULL; link = &rp->ras_next, rp = *link) {
    249  1.21        ad 		if (addr == rp->ras_startaddr && endaddr == rp->ras_endaddr)
    250   1.1  gmcgarry 			break;
    251   1.1  gmcgarry 	}
    252  1.16        ad 	if (rp != NULL) {
    253  1.21        ad 		*link = rp->ras_next;
    254  1.21        ad 		ras_sync();
    255  1.25        ad 		mutex_exit(&p->p_auxlock);
    256  1.16        ad 		pool_put(&ras_pool, rp);
    257  1.21        ad 		return 0;
    258  1.21        ad 	} else {
    259  1.25        ad 		mutex_exit(&p->p_auxlock);
    260  1.21        ad 		return ESRCH;
    261  1.16        ad 	}
    262   1.1  gmcgarry }
    263   1.1  gmcgarry 
    264  1.12   hannken #endif /* defined(__HAVE_RAS) */
    265  1.12   hannken 
    266   1.1  gmcgarry /*ARGSUSED*/
    267   1.1  gmcgarry int
    268  1.24       dsl sys_rasctl(struct lwp *l, const struct sys_rasctl_args *uap, register_t *retval)
    269   1.1  gmcgarry {
    270   1.1  gmcgarry 
    271   1.1  gmcgarry #if defined(__HAVE_RAS)
    272  1.24       dsl 	/* {
    273  1.17  christos 		syscallarg(void *) addr;
    274   1.1  gmcgarry 		syscallarg(size_t) len;
    275   1.1  gmcgarry 		syscallarg(int) op;
    276  1.24       dsl 	} */
    277  1.17  christos 	void *addr;
    278   1.1  gmcgarry 	size_t len;
    279   1.1  gmcgarry 	int op;
    280   1.1  gmcgarry 	int error;
    281   1.1  gmcgarry 
    282   1.1  gmcgarry 	/*
    283   1.1  gmcgarry 	 * first, extract syscall args from the uap.
    284   1.1  gmcgarry 	 */
    285   1.1  gmcgarry 
    286  1.17  christos 	addr = (void *)SCARG(uap, addr);
    287   1.1  gmcgarry 	len = (size_t)SCARG(uap, len);
    288   1.1  gmcgarry 	op = SCARG(uap, op);
    289   1.1  gmcgarry 
    290   1.2   thorpej 	DPRINTF(("sys_rasctl: p=%p addr=%p, len=%ld, op=0x%x\n",
    291  1.21        ad 	    curproc, addr, (long)len, op));
    292   1.1  gmcgarry 
    293   1.1  gmcgarry 	switch (op) {
    294   1.1  gmcgarry 	case RAS_INSTALL:
    295  1.21        ad 		error = ras_install(addr, len);
    296   1.1  gmcgarry 		break;
    297   1.1  gmcgarry 	case RAS_PURGE:
    298  1.21        ad 		error = ras_purge(addr, len);
    299   1.1  gmcgarry 		break;
    300   1.1  gmcgarry 	case RAS_PURGE_ALL:
    301  1.21        ad 		error = ras_purgeall();
    302   1.1  gmcgarry 		break;
    303   1.1  gmcgarry 	default:
    304   1.1  gmcgarry 		error = EINVAL;
    305   1.1  gmcgarry 		break;
    306   1.1  gmcgarry 	}
    307   1.1  gmcgarry 
    308   1.1  gmcgarry 	return (error);
    309   1.1  gmcgarry 
    310   1.1  gmcgarry #else
    311   1.1  gmcgarry 
    312   1.1  gmcgarry 	return (EOPNOTSUPP);
    313   1.1  gmcgarry 
    314   1.1  gmcgarry #endif
    315   1.1  gmcgarry 
    316   1.1  gmcgarry }
    317