2008-01-05 00:17:40 +03:00
|
|
|
/* $NetBSD: kern_ras.c,v 1.26 2008/01/04 21:18:10 ad Exp $ */
|
2002-08-28 11:16:33 +04:00
|
|
|
|
|
|
|
/*-
|
2007-10-24 18:50:38 +04:00
|
|
|
* Copyright (c) 2002, 2006, 2007 The NetBSD Foundation, Inc.
|
2002-08-28 11:16:33 +04:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
2007-10-24 18:50:38 +04:00
|
|
|
* by Gregory McGarry, and by Andrew Doran.
|
2002-08-28 11:16:33 +04:00
|
|
|
*
|
|
|
|
* 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>
|
2008-01-05 00:17:40 +03:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: kern_ras.c,v 1.26 2008/01/04 21:18:10 ad Exp $");
|
2002-08-28 11:16:33 +04:00
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
2007-10-24 18:50:38 +04:00
|
|
|
#include <sys/kernel.h>
|
2002-08-28 11:16:33 +04:00
|
|
|
#include <sys/pool.h>
|
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/ras.h>
|
2007-10-24 18:50:38 +04:00
|
|
|
#include <sys/xcall.h>
|
2002-08-28 11:16:33 +04:00
|
|
|
#include <sys/syscallargs.h>
|
|
|
|
|
|
|
|
#include <uvm/uvm_extern.h>
|
|
|
|
|
2006-10-08 08:28:44 +04:00
|
|
|
POOL_INIT(ras_pool, sizeof(struct ras), 0, 0, 0, "raspl",
|
2007-03-12 21:18:22 +03:00
|
|
|
&pool_allocator_nointr, IPL_NONE);
|
2006-10-08 08:28:44 +04:00
|
|
|
|
2002-08-28 11:16:33 +04:00
|
|
|
#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
|
|
|
|
|
2007-10-24 18:50:38 +04:00
|
|
|
/*
|
|
|
|
* Force all CPUs through cpu_switchto(), waiting until complete.
|
2007-10-26 21:28:37 +04:00
|
|
|
* Context switching will drain the write buffer on the calling
|
|
|
|
* CPU.
|
2007-10-24 18:50:38 +04:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
ras_sync(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
/* No need to sync if exiting or single threaded. */
|
|
|
|
if (curproc->p_nlwps > 1 && ncpu > 1) {
|
|
|
|
#ifdef NO_SOFTWARE_PATENTS
|
|
|
|
uint64_t where;
|
|
|
|
where = xc_broadcast(0, (xcfunc_t)nullop, NULL, NULL);
|
|
|
|
xc_wait(where);
|
|
|
|
#else
|
2007-10-26 21:28:37 +04:00
|
|
|
/*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
* o preemption is disabled by the thread in
|
|
|
|
* ras_lookup().
|
|
|
|
* o proc::p_raslist is only inspected with
|
|
|
|
* preemption disabled.
|
|
|
|
* o ras_lookup() plus loads reordered in advance
|
|
|
|
* will take no longer than 1/8s to complete.
|
|
|
|
*/
|
|
|
|
const int delta = hz >> 3;
|
|
|
|
int target = hardclock_ticks + delta;
|
|
|
|
do {
|
|
|
|
kpause("ras", false, delta, NULL);
|
|
|
|
} while (hardclock_ticks < target);
|
2007-10-24 18:50:38 +04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-08-28 11:16:33 +04:00
|
|
|
/*
|
|
|
|
* 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.
|
2007-10-24 18:50:38 +04:00
|
|
|
*
|
|
|
|
* No locking required: we disable preemption and ras_sync()
|
|
|
|
* guarantees that individual entries are valid while we still
|
|
|
|
* have visibility of them.
|
2002-08-28 11:16:33 +04:00
|
|
|
*/
|
2007-03-04 08:59:00 +03:00
|
|
|
void *
|
|
|
|
ras_lookup(struct proc *p, void *addr)
|
2002-08-28 11:16:33 +04:00
|
|
|
{
|
|
|
|
struct ras *rp;
|
2007-03-04 08:59:00 +03:00
|
|
|
void *startaddr;
|
2007-02-10 00:55:00 +03:00
|
|
|
|
2007-03-04 08:59:00 +03:00
|
|
|
startaddr = (void *)-1;
|
2002-08-28 11:16:33 +04:00
|
|
|
|
2007-10-24 18:50:38 +04:00
|
|
|
crit_enter();
|
|
|
|
for (rp = p->p_raslist; rp != NULL; rp = rp->ras_next) {
|
2002-08-28 11:16:33 +04:00
|
|
|
if (addr > rp->ras_startaddr && addr < rp->ras_endaddr) {
|
2007-02-10 00:55:00 +03:00
|
|
|
startaddr = rp->ras_startaddr;
|
2002-08-28 11:16:33 +04:00
|
|
|
DPRINTF(("RAS hit: p=%p %p\n", p, addr));
|
2007-02-10 00:55:00 +03:00
|
|
|
break;
|
2002-08-28 11:16:33 +04:00
|
|
|
}
|
|
|
|
}
|
2007-10-24 18:50:38 +04:00
|
|
|
crit_exit();
|
2002-08-28 11:16:33 +04:00
|
|
|
|
2007-10-24 18:50:38 +04:00
|
|
|
return startaddr;
|
2002-08-28 11:16:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* During a fork, we copy all of the sequences from parent p1 to
|
|
|
|
* the child p2.
|
2007-10-24 18:50:38 +04:00
|
|
|
*
|
|
|
|
* No locking required as the parent must be paused.
|
2002-08-28 11:16:33 +04:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
ras_fork(struct proc *p1, struct proc *p2)
|
|
|
|
{
|
|
|
|
struct ras *rp, *nrp;
|
|
|
|
|
2007-10-24 18:50:38 +04:00
|
|
|
for (rp = p1->p_raslist; rp != NULL; rp = rp->ras_next) {
|
2004-03-26 01:08:33 +03:00
|
|
|
nrp = pool_get(&ras_pool, PR_WAITOK);
|
2004-04-01 06:37:42 +04:00
|
|
|
nrp->ras_startaddr = rp->ras_startaddr;
|
|
|
|
nrp->ras_endaddr = rp->ras_endaddr;
|
2007-10-24 18:50:38 +04:00
|
|
|
nrp = p2->p_raslist;
|
|
|
|
p2->p_raslist = nrp;
|
2004-04-01 06:37:42 +04:00
|
|
|
}
|
|
|
|
|
2007-10-25 01:50:09 +04:00
|
|
|
DPRINTF(("ras_fork: p1=%p, p2=%p\n", p1, p2));
|
2002-08-28 11:16:33 +04:00
|
|
|
|
2007-10-24 18:50:38 +04:00
|
|
|
return 0;
|
2002-08-28 11:16:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Nuke all sequences for this process.
|
|
|
|
*/
|
|
|
|
int
|
2007-10-24 18:50:38 +04:00
|
|
|
ras_purgeall(void)
|
2002-08-28 11:16:33 +04:00
|
|
|
{
|
2007-10-24 18:50:38 +04:00
|
|
|
struct ras *rp, *nrp;
|
|
|
|
proc_t *p;
|
2002-08-28 11:16:33 +04:00
|
|
|
|
2007-10-24 18:50:38 +04:00
|
|
|
p = curproc;
|
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
mutex_enter(&p->p_auxlock);
|
2007-10-24 18:50:38 +04:00
|
|
|
if ((rp = p->p_raslist) != NULL) {
|
|
|
|
p->p_raslist = NULL;
|
|
|
|
ras_sync();
|
|
|
|
for(; rp != NULL; rp = nrp) {
|
|
|
|
nrp = rp->ras_next;
|
|
|
|
pool_put(&ras_pool, rp);
|
|
|
|
}
|
2002-08-28 11:16:33 +04:00
|
|
|
}
|
2008-01-02 14:48:20 +03:00
|
|
|
mutex_exit(&p->p_auxlock);
|
2002-08-28 11:16:33 +04:00
|
|
|
|
2007-10-24 18:50:38 +04:00
|
|
|
return 0;
|
2002-08-28 11:16:33 +04:00
|
|
|
}
|
|
|
|
|
2005-11-25 14:29:48 +03:00
|
|
|
#if defined(__HAVE_RAS)
|
|
|
|
|
2002-08-28 11:16:33 +04:00
|
|
|
/*
|
|
|
|
* Install the new sequence. If it already exists, return
|
|
|
|
* an error.
|
|
|
|
*/
|
2005-11-24 21:36:50 +03:00
|
|
|
static int
|
2007-10-24 18:50:38 +04:00
|
|
|
ras_install(void *addr, size_t len)
|
2002-08-28 11:16:33 +04:00
|
|
|
{
|
|
|
|
struct ras *rp;
|
2004-04-01 05:49:04 +04:00
|
|
|
struct ras *newrp;
|
2007-10-24 18:50:38 +04:00
|
|
|
void *endaddr;
|
|
|
|
int nras, error;
|
|
|
|
proc_t *p;
|
|
|
|
|
|
|
|
endaddr = (char *)addr + len;
|
2002-08-28 11:16:33 +04:00
|
|
|
|
2007-03-04 08:59:00 +03:00
|
|
|
if (addr < (void *)VM_MIN_ADDRESS ||
|
|
|
|
endaddr > (void *)VM_MAXUSER_ADDRESS)
|
2002-08-28 11:16:33 +04:00
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
if (len <= 0)
|
|
|
|
return (EINVAL);
|
|
|
|
|
2007-10-24 18:50:38 +04:00
|
|
|
newrp = pool_get(&ras_pool, PR_WAITOK);
|
|
|
|
newrp->ras_startaddr = addr;
|
|
|
|
newrp->ras_endaddr = endaddr;
|
|
|
|
error = 0;
|
|
|
|
nras = 0;
|
|
|
|
p = curproc;
|
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
mutex_enter(&p->p_auxlock);
|
2007-10-24 18:50:38 +04:00
|
|
|
for (rp = p->p_raslist; rp != NULL; rp = rp->ras_next) {
|
2007-03-13 07:44:06 +03:00
|
|
|
if (++nras >= ras_per_proc) {
|
2007-10-24 18:50:38 +04:00
|
|
|
error = EINVAL;
|
|
|
|
break;
|
2002-08-28 11:16:33 +04:00
|
|
|
}
|
2007-03-13 07:44:06 +03:00
|
|
|
if (addr < rp->ras_endaddr && endaddr > rp->ras_startaddr) {
|
2007-10-24 18:50:38 +04:00
|
|
|
error = EEXIST;
|
|
|
|
break;
|
2007-03-13 07:44:06 +03:00
|
|
|
}
|
2002-08-28 11:16:33 +04:00
|
|
|
}
|
2007-10-24 18:50:38 +04:00
|
|
|
if (rp == NULL) {
|
|
|
|
newrp->ras_next = p->p_raslist;
|
|
|
|
p->p_raslist = newrp;
|
|
|
|
ras_sync();
|
2008-01-02 14:48:20 +03:00
|
|
|
mutex_exit(&p->p_auxlock);
|
2007-10-24 18:50:38 +04:00
|
|
|
} else {
|
2008-01-02 14:48:20 +03:00
|
|
|
mutex_exit(&p->p_auxlock);
|
2007-10-24 18:50:38 +04:00
|
|
|
pool_put(&ras_pool, newrp);
|
2004-04-01 05:49:04 +04:00
|
|
|
}
|
2002-08-28 11:16:33 +04:00
|
|
|
|
2007-10-24 18:50:38 +04:00
|
|
|
return error;
|
2002-08-28 11:16:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Nuke the specified sequence. Both address and len must
|
|
|
|
* match, otherwise we return an error.
|
|
|
|
*/
|
2005-11-24 21:36:50 +03:00
|
|
|
static int
|
2007-10-24 18:50:38 +04:00
|
|
|
ras_purge(void *addr, size_t len)
|
2002-08-28 11:16:33 +04:00
|
|
|
{
|
2007-10-24 18:50:38 +04:00
|
|
|
struct ras *rp, **link;
|
|
|
|
void *endaddr;
|
|
|
|
proc_t *p;
|
2002-08-28 11:16:33 +04:00
|
|
|
|
2007-10-24 18:50:38 +04:00
|
|
|
endaddr = (char *)addr + len;
|
|
|
|
p = curproc;
|
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
mutex_enter(&p->p_auxlock);
|
2007-10-24 18:50:38 +04:00
|
|
|
link = &p->p_raslist;
|
|
|
|
for (rp = *link; rp != NULL; link = &rp->ras_next, rp = *link) {
|
|
|
|
if (addr == rp->ras_startaddr && endaddr == rp->ras_endaddr)
|
2002-08-28 11:16:33 +04:00
|
|
|
break;
|
|
|
|
}
|
2007-02-10 00:55:00 +03:00
|
|
|
if (rp != NULL) {
|
2007-10-24 18:50:38 +04:00
|
|
|
*link = rp->ras_next;
|
|
|
|
ras_sync();
|
2008-01-02 14:48:20 +03:00
|
|
|
mutex_exit(&p->p_auxlock);
|
2007-02-10 00:55:00 +03:00
|
|
|
pool_put(&ras_pool, rp);
|
2007-10-24 18:50:38 +04:00
|
|
|
return 0;
|
|
|
|
} else {
|
2008-01-02 14:48:20 +03:00
|
|
|
mutex_exit(&p->p_auxlock);
|
2007-10-24 18:50:38 +04:00
|
|
|
return ESRCH;
|
2007-02-10 00:55:00 +03:00
|
|
|
}
|
2002-08-28 11:16:33 +04:00
|
|
|
}
|
|
|
|
|
2005-11-25 14:29:48 +03:00
|
|
|
#endif /* defined(__HAVE_RAS) */
|
|
|
|
|
2002-08-28 11:16:33 +04:00
|
|
|
/*ARGSUSED*/
|
|
|
|
int
|
2007-12-21 02:02:38 +03:00
|
|
|
sys_rasctl(struct lwp *l, const struct sys_rasctl_args *uap, register_t *retval)
|
2002-08-28 11:16:33 +04:00
|
|
|
{
|
|
|
|
|
|
|
|
#if defined(__HAVE_RAS)
|
2007-12-21 02:02:38 +03:00
|
|
|
/* {
|
2007-03-04 08:59:00 +03:00
|
|
|
syscallarg(void *) addr;
|
2002-08-28 11:16:33 +04:00
|
|
|
syscallarg(size_t) len;
|
|
|
|
syscallarg(int) op;
|
2007-12-21 02:02:38 +03:00
|
|
|
} */
|
2007-03-04 08:59:00 +03:00
|
|
|
void *addr;
|
2002-08-28 11:16:33 +04:00
|
|
|
size_t len;
|
|
|
|
int op;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* first, extract syscall args from the uap.
|
|
|
|
*/
|
|
|
|
|
2007-03-04 08:59:00 +03:00
|
|
|
addr = (void *)SCARG(uap, addr);
|
2002-08-28 11:16:33 +04:00
|
|
|
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",
|
2007-10-24 18:50:38 +04:00
|
|
|
curproc, addr, (long)len, op));
|
2002-08-28 11:16:33 +04:00
|
|
|
|
|
|
|
switch (op) {
|
|
|
|
case RAS_INSTALL:
|
2007-10-24 18:50:38 +04:00
|
|
|
error = ras_install(addr, len);
|
2002-08-28 11:16:33 +04:00
|
|
|
break;
|
|
|
|
case RAS_PURGE:
|
2007-10-24 18:50:38 +04:00
|
|
|
error = ras_purge(addr, len);
|
2002-08-28 11:16:33 +04:00
|
|
|
break;
|
|
|
|
case RAS_PURGE_ALL:
|
2007-10-24 18:50:38 +04:00
|
|
|
error = ras_purgeall();
|
2002-08-28 11:16:33 +04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error = EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (error);
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
return (EOPNOTSUPP);
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|