1 /* $NetBSD: kern_ras.c,v 1.33 2008/06/09 11:49:40 ad Exp $ */
4 * Copyright (c) 2002, 2006, 2007, 2008 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Gregory McGarry, and by Andrew Doran.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: kern_ras.c,v 1.33 2008/06/09 11:49:40 ad Exp $");
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
42 #include <sys/savar.h>
43 #include <sys/xcall.h>
44 #include <sys/syscallargs.h>
46 #include <uvm/uvm_extern.h>
48 #define MAX_RAS_PER_PROC 16
50 u_int ras_per_proc
= MAX_RAS_PER_PROC
;
54 #define DPRINTF(x) if (ras_debug) printf x
56 #define DPRINTF(x) /* nothing */
60 * Force all CPUs through cpu_switchto(), waiting until complete.
61 * Context switching will drain the write buffer on the calling
68 /* No need to sync if exiting or single threaded. */
69 if (curproc
->p_nlwps
> 1 && ncpu
> 1) {
70 #ifdef NO_SOFTWARE_PATENTS
72 where
= xc_broadcast(0, (xcfunc_t
)nullop
, NULL
, NULL
);
78 * o preemption is disabled by the thread in
80 * o proc::p_raslist is only inspected with
81 * preemption disabled.
82 * o ras_lookup() plus loads reordered in advance
83 * will take no longer than 1/8s to complete.
85 const int delta
= hz
>> 3;
86 int target
= hardclock_ticks
+ delta
;
88 kpause("ras", false, delta
, NULL
);
89 } while (hardclock_ticks
< target
);
95 * Check the specified address to see if it is within the
96 * sequence. If it is found, we return the restart address,
97 * otherwise we return -1. If we do perform a restart, we
98 * mark the sequence as hit.
100 * No locking required: we disable preemption and ras_sync()
101 * guarantees that individual entries are valid while we still
102 * have visibility of them.
105 ras_lookup(struct proc
*p
, void *addr
)
111 startaddr
= (void *)-1;
115 for (rp
= p
->p_raslist
; rp
!= NULL
; rp
= rp
->ras_next
) {
116 if (addr
> rp
->ras_startaddr
&& addr
< rp
->ras_endaddr
) {
117 startaddr
= rp
->ras_startaddr
;
118 DPRINTF(("RAS hit: p=%p %p\n", p
, addr
));
128 * During a fork, we copy all of the sequences from parent p1 to
131 * No locking required as the parent must be paused.
134 ras_fork(struct proc
*p1
, struct proc
*p2
)
136 struct ras
*rp
, *nrp
;
138 for (rp
= p1
->p_raslist
; rp
!= NULL
; rp
= rp
->ras_next
) {
139 nrp
= kmem_alloc(sizeof(*nrp
), KM_SLEEP
);
140 nrp
->ras_startaddr
= rp
->ras_startaddr
;
141 nrp
->ras_endaddr
= rp
->ras_endaddr
;
142 nrp
->ras_next
= p2
->p_raslist
;
146 DPRINTF(("ras_fork: p1=%p, p2=%p\n", p1
, p2
));
152 * Nuke all sequences for this process.
157 struct ras
*rp
, *nrp
;
162 if (p
->p_raslist
== NULL
)
165 mutex_enter(&p
->p_auxlock
);
166 if ((rp
= p
->p_raslist
) != NULL
) {
169 for(; rp
!= NULL
; rp
= nrp
) {
171 kmem_free(rp
, sizeof(*rp
));
174 mutex_exit(&p
->p_auxlock
);
179 #if defined(__HAVE_RAS)
182 * Install the new sequence. If it already exists, return
186 ras_install(void *addr
, size_t len
)
194 endaddr
= (char *)addr
+ len
;
196 if (addr
< (void *)VM_MIN_ADDRESS
||
197 endaddr
> (void *)VM_MAXUSER_ADDRESS
)
203 newrp
= kmem_alloc(sizeof(*newrp
), KM_SLEEP
);
204 newrp
->ras_startaddr
= addr
;
205 newrp
->ras_endaddr
= endaddr
;
210 mutex_enter(&p
->p_auxlock
);
211 for (rp
= p
->p_raslist
; rp
!= NULL
; rp
= rp
->ras_next
) {
212 if (++nras
>= ras_per_proc
) {
216 if (addr
< rp
->ras_endaddr
&& endaddr
> rp
->ras_startaddr
) {
222 newrp
->ras_next
= p
->p_raslist
;
223 p
->p_raslist
= newrp
;
225 mutex_exit(&p
->p_auxlock
);
227 mutex_exit(&p
->p_auxlock
);
228 kmem_free(newrp
, sizeof(*newrp
));
235 * Nuke the specified sequence. Both address and len must
236 * match, otherwise we return an error.
239 ras_purge(void *addr
, size_t len
)
241 struct ras
*rp
, **link
;
245 endaddr
= (char *)addr
+ len
;
248 mutex_enter(&p
->p_auxlock
);
249 link
= &p
->p_raslist
;
250 for (rp
= *link
; rp
!= NULL
; link
= &rp
->ras_next
, rp
= *link
) {
251 if (addr
== rp
->ras_startaddr
&& endaddr
== rp
->ras_endaddr
)
255 *link
= rp
->ras_next
;
257 mutex_exit(&p
->p_auxlock
);
258 kmem_free(rp
, sizeof(*rp
));
261 mutex_exit(&p
->p_auxlock
);
266 #endif /* defined(__HAVE_RAS) */
270 sys_rasctl(struct lwp
*l
, const struct sys_rasctl_args
*uap
, register_t
*retval
)
273 #if defined(__HAVE_RAS)
275 syscallarg(void *) addr;
276 syscallarg(size_t) len;
285 * first, extract syscall args from the uap.
288 addr
= (void *)SCARG(uap
, addr
);
289 len
= (size_t)SCARG(uap
, len
);
292 DPRINTF(("sys_rasctl: p=%p addr=%p, len=%ld, op=0x%x\n",
293 curproc
, addr
, (long)len
, op
));
297 error
= ras_install(addr
, len
);
300 error
= ras_purge(addr
, len
);
303 error
= ras_purgeall();