2005-02-27 00:34:55 +03:00
|
|
|
/* $NetBSD: kern_ras.c,v 1.10 2005/02/26 21:34:55 perry Exp $ */
|
2002-08-28 11:16:33 +04:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 2002 The NetBSD Foundation, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by Gregory McGarry.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the NetBSD
|
|
|
|
* Foundation, Inc. and its contributors.
|
|
|
|
* 4. Neither the name of The NetBSD Foundation nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
2005-02-27 00:34:55 +03:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: kern_ras.c,v 1.10 2005/02/26 21:34:55 perry Exp $");
|
2002-08-28 11:16:33 +04:00
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/lock.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/pool.h>
|
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/ras.h>
|
2003-01-18 13:06:22 +03:00
|
|
|
#include <sys/sa.h>
|
|
|
|
#include <sys/savar.h>
|
2002-08-28 11:16:33 +04:00
|
|
|
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/syscallargs.h>
|
|
|
|
|
|
|
|
#include <uvm/uvm_extern.h>
|
|
|
|
|
|
|
|
#define MAX_RAS_PER_PROC 16
|
|
|
|
|
2002-11-09 23:06:07 +03:00
|
|
|
u_int ras_per_proc = MAX_RAS_PER_PROC;
|
2002-08-28 11:16:33 +04:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
int ras_debug = 0;
|
|
|
|
#define DPRINTF(x) if (ras_debug) printf x
|
|
|
|
#else
|
|
|
|
#define DPRINTF(x) /* nothing */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int ras_install(struct proc *, caddr_t, size_t);
|
|
|
|
int ras_purge(struct proc *, caddr_t, size_t);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check the specified address to see if it is within the
|
|
|
|
* sequence. If it is found, we return the restart address,
|
|
|
|
* otherwise we return -1. If we do perform a restart, we
|
|
|
|
* mark the sequence as hit.
|
|
|
|
*/
|
|
|
|
caddr_t
|
|
|
|
ras_lookup(struct proc *p, caddr_t addr)
|
|
|
|
{
|
|
|
|
struct ras *rp;
|
|
|
|
|
|
|
|
#ifdef DIAGNOSTIC
|
|
|
|
if (addr < (caddr_t)VM_MIN_ADDRESS ||
|
|
|
|
addr > (caddr_t)VM_MAXUSER_ADDRESS)
|
|
|
|
return ((caddr_t)-1);
|
|
|
|
#endif
|
|
|
|
|
2003-11-04 13:33:15 +03:00
|
|
|
simple_lock(&p->p_lock);
|
2002-08-28 11:16:33 +04:00
|
|
|
LIST_FOREACH(rp, &p->p_raslist, ras_list) {
|
|
|
|
if (addr > rp->ras_startaddr && addr < rp->ras_endaddr) {
|
|
|
|
rp->ras_hits++;
|
2003-11-04 13:33:15 +03:00
|
|
|
simple_unlock(&p->p_lock);
|
2002-08-28 11:16:33 +04:00
|
|
|
#ifdef DIAGNOSTIC
|
|
|
|
DPRINTF(("RAS hit: p=%p %p\n", p, addr));
|
|
|
|
#endif
|
|
|
|
return (rp->ras_startaddr);
|
|
|
|
}
|
|
|
|
}
|
2003-11-04 13:33:15 +03:00
|
|
|
simple_unlock(&p->p_lock);
|
2002-08-28 11:16:33 +04:00
|
|
|
|
|
|
|
return ((caddr_t)-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* During a fork, we copy all of the sequences from parent p1 to
|
|
|
|
* the child p2.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
ras_fork(struct proc *p1, struct proc *p2)
|
|
|
|
{
|
|
|
|
struct ras *rp, *nrp;
|
2004-04-01 06:37:42 +04:00
|
|
|
int nras;
|
2002-08-28 11:16:33 +04:00
|
|
|
|
2004-04-01 06:37:42 +04:00
|
|
|
again:
|
|
|
|
/*
|
|
|
|
* first, try to shortcut.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (LIST_EMPTY(&p1->p_raslist))
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* count entries.
|
|
|
|
*/
|
|
|
|
|
|
|
|
nras = 0;
|
2003-11-04 13:33:15 +03:00
|
|
|
simple_lock(&p1->p_lock);
|
2004-04-01 06:37:42 +04:00
|
|
|
LIST_FOREACH(rp, &p1->p_raslist, ras_list)
|
2003-11-04 13:33:15 +03:00
|
|
|
nras++;
|
2004-04-01 06:37:42 +04:00
|
|
|
simple_unlock(&p1->p_lock);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* allocate entries.
|
|
|
|
*/
|
|
|
|
|
|
|
|
for ( ; nras > 0; nras--) {
|
2004-03-26 01:08:33 +03:00
|
|
|
nrp = pool_get(&ras_pool, PR_WAITOK);
|
2002-08-28 11:16:33 +04:00
|
|
|
nrp->ras_hits = 0;
|
|
|
|
LIST_INSERT_HEAD(&p2->p_raslist, nrp, ras_list);
|
|
|
|
}
|
2004-04-01 06:37:42 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* copy entries.
|
|
|
|
*/
|
|
|
|
|
|
|
|
simple_lock(&p1->p_lock);
|
|
|
|
nrp = LIST_FIRST(&p2->p_raslist);
|
|
|
|
LIST_FOREACH(rp, &p1->p_raslist, ras_list) {
|
|
|
|
if (nrp == NULL)
|
|
|
|
break;
|
|
|
|
nrp->ras_startaddr = rp->ras_startaddr;
|
|
|
|
nrp->ras_endaddr = rp->ras_endaddr;
|
|
|
|
nrp = LIST_NEXT(nrp, ras_list);
|
|
|
|
}
|
2003-11-04 13:33:15 +03:00
|
|
|
simple_unlock(&p1->p_lock);
|
|
|
|
|
2004-04-01 06:37:42 +04:00
|
|
|
/*
|
|
|
|
* if we lose a race, retry.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (rp != NULL || nrp != NULL) {
|
|
|
|
ras_purgeall(p2);
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
|
2003-11-04 13:33:15 +03:00
|
|
|
DPRINTF(("ras_fork: p1=%p, p2=%p, nras=%d\n", p1, p2, nras));
|
2002-08-28 11:16:33 +04:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Nuke all sequences for this process.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
ras_purgeall(struct proc *p)
|
|
|
|
{
|
|
|
|
struct ras *rp;
|
|
|
|
|
2003-11-04 13:33:15 +03:00
|
|
|
simple_lock(&p->p_lock);
|
2002-08-28 11:16:33 +04:00
|
|
|
while (!LIST_EMPTY(&p->p_raslist)) {
|
|
|
|
rp = LIST_FIRST(&p->p_raslist);
|
2005-02-27 00:34:55 +03:00
|
|
|
DPRINTF(("RAS %p-%p, hits %d\n", rp->ras_startaddr,
|
2002-08-28 11:16:33 +04:00
|
|
|
rp->ras_endaddr, rp->ras_hits));
|
|
|
|
LIST_REMOVE(rp, ras_list);
|
|
|
|
pool_put(&ras_pool, rp);
|
|
|
|
}
|
2003-11-04 13:33:15 +03:00
|
|
|
simple_unlock(&p->p_lock);
|
2002-08-28 11:16:33 +04:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Install the new sequence. If it already exists, return
|
|
|
|
* an error.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
ras_install(struct proc *p, caddr_t addr, size_t len)
|
|
|
|
{
|
|
|
|
struct ras *rp;
|
2004-04-01 05:49:04 +04:00
|
|
|
struct ras *newrp;
|
2002-08-28 11:16:33 +04:00
|
|
|
caddr_t endaddr = addr + len;
|
2003-11-04 13:33:15 +03:00
|
|
|
int nras = 0;
|
2002-08-28 11:16:33 +04:00
|
|
|
|
|
|
|
if (addr < (caddr_t)VM_MIN_ADDRESS ||
|
2003-11-04 13:33:15 +03:00
|
|
|
endaddr > (caddr_t)VM_MAXUSER_ADDRESS)
|
2002-08-28 11:16:33 +04:00
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
if (len <= 0)
|
|
|
|
return (EINVAL);
|
|
|
|
|
2004-04-01 05:49:04 +04:00
|
|
|
newrp = NULL;
|
|
|
|
again:
|
2003-11-04 13:33:15 +03:00
|
|
|
simple_lock(&p->p_lock);
|
2002-08-28 11:16:33 +04:00
|
|
|
LIST_FOREACH(rp, &p->p_raslist, ras_list) {
|
2003-11-04 13:33:15 +03:00
|
|
|
if (++nras >= ras_per_proc ||
|
|
|
|
(addr < rp->ras_endaddr && endaddr > rp->ras_startaddr)) {
|
|
|
|
simple_unlock(&p->p_lock);
|
2002-08-28 11:16:33 +04:00
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
}
|
2004-04-01 05:49:04 +04:00
|
|
|
if (newrp == NULL) {
|
|
|
|
simple_unlock(&p->p_lock);
|
|
|
|
newrp = pool_get(&ras_pool, PR_WAITOK);
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
newrp->ras_startaddr = addr;
|
|
|
|
newrp->ras_endaddr = endaddr;
|
|
|
|
newrp->ras_hits = 0;
|
|
|
|
LIST_INSERT_HEAD(&p->p_raslist, newrp, ras_list);
|
2003-11-04 13:33:15 +03:00
|
|
|
simple_unlock(&p->p_lock);
|
2002-08-28 11:16:33 +04:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Nuke the specified sequence. Both address and len must
|
|
|
|
* match, otherwise we return an error.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
ras_purge(struct proc *p, caddr_t addr, size_t len)
|
|
|
|
{
|
|
|
|
struct ras *rp;
|
|
|
|
caddr_t endaddr = addr + len;
|
|
|
|
int error = ESRCH;
|
|
|
|
|
2003-11-04 13:33:15 +03:00
|
|
|
simple_lock(&p->p_lock);
|
2002-08-28 11:16:33 +04:00
|
|
|
LIST_FOREACH(rp, &p->p_raslist, ras_list) {
|
|
|
|
if (addr == rp->ras_startaddr && endaddr == rp->ras_endaddr) {
|
|
|
|
LIST_REMOVE(rp, ras_list);
|
|
|
|
pool_put(&ras_pool, rp);
|
|
|
|
error = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2003-11-04 13:33:15 +03:00
|
|
|
simple_unlock(&p->p_lock);
|
2002-08-28 11:16:33 +04:00
|
|
|
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*ARGSUSED*/
|
|
|
|
int
|
2003-01-18 13:06:22 +03:00
|
|
|
sys_rasctl(struct lwp *l, void *v, register_t *retval)
|
2002-08-28 11:16:33 +04:00
|
|
|
{
|
|
|
|
|
|
|
|
#if defined(__HAVE_RAS)
|
|
|
|
|
|
|
|
struct sys_rasctl_args /* {
|
|
|
|
syscallarg(caddr_t) addr;
|
|
|
|
syscallarg(size_t) len;
|
|
|
|
syscallarg(int) op;
|
|
|
|
} */ *uap = v;
|
2003-01-18 13:06:22 +03:00
|
|
|
struct proc *p = l->l_proc;
|
2002-08-28 11:16:33 +04:00
|
|
|
caddr_t addr;
|
|
|
|
size_t len;
|
|
|
|
int op;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* first, extract syscall args from the uap.
|
|
|
|
*/
|
|
|
|
|
|
|
|
addr = (caddr_t)SCARG(uap, addr);
|
|
|
|
len = (size_t)SCARG(uap, len);
|
|
|
|
op = SCARG(uap, op);
|
|
|
|
|
2002-10-03 05:42:43 +04:00
|
|
|
DPRINTF(("sys_rasctl: p=%p addr=%p, len=%ld, op=0x%x\n",
|
|
|
|
p, addr, (long)len, op));
|
2002-08-28 11:16:33 +04:00
|
|
|
|
|
|
|
switch (op) {
|
|
|
|
case RAS_INSTALL:
|
|
|
|
error = ras_install(p, addr, len);
|
|
|
|
break;
|
|
|
|
case RAS_PURGE:
|
|
|
|
error = ras_purge(p, addr, len);
|
|
|
|
break;
|
|
|
|
case RAS_PURGE_ALL:
|
|
|
|
error = ras_purgeall(p);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error = EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (error);
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
return (EOPNOTSUPP);
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|