No empty .Rs/.Re
[netbsd-mini2440.git] / sys / kern / kern_ras.c
blob8f64ffe27448a5a24dde6ef7010fa058a8318eeb
1 /* $NetBSD: kern_ras.c,v 1.33 2008/06/09 11:49:40 ad Exp $ */
3 /*-
4 * Copyright (c) 2002, 2006, 2007, 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
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
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.
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>
38 #include <sys/kmem.h>
39 #include <sys/proc.h>
40 #include <sys/ras.h>
41 #include <sys/sa.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;
52 #ifdef DEBUG
53 int ras_debug = 0;
54 #define DPRINTF(x) if (ras_debug) printf x
55 #else
56 #define DPRINTF(x) /* nothing */
57 #endif
60 * Force all CPUs through cpu_switchto(), waiting until complete.
61 * Context switching will drain the write buffer on the calling
62 * CPU.
64 static void
65 ras_sync(void)
68 /* No need to sync if exiting or single threaded. */
69 if (curproc->p_nlwps > 1 && ncpu > 1) {
70 #ifdef NO_SOFTWARE_PATENTS
71 uint64_t where;
72 where = xc_broadcast(0, (xcfunc_t)nullop, NULL, NULL);
73 xc_wait(where);
74 #else
76 * Assumptions:
78 * o preemption is disabled by the thread in
79 * ras_lookup().
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;
87 do {
88 kpause("ras", false, delta, NULL);
89 } while (hardclock_ticks < target);
90 #endif
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.
104 void *
105 ras_lookup(struct proc *p, void *addr)
107 struct ras *rp;
108 void *startaddr;
109 lwp_t *l;
111 startaddr = (void *)-1;
112 l = curlwp;
114 KPREEMPT_DISABLE(l);
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));
119 break;
122 KPREEMPT_ENABLE(l);
124 return startaddr;
128 * During a fork, we copy all of the sequences from parent p1 to
129 * the child p2.
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;
143 p2->p_raslist = nrp;
146 DPRINTF(("ras_fork: p1=%p, p2=%p\n", p1, p2));
148 return 0;
152 * Nuke all sequences for this process.
155 ras_purgeall(void)
157 struct ras *rp, *nrp;
158 proc_t *p;
160 p = curproc;
162 if (p->p_raslist == NULL)
163 return 0;
165 mutex_enter(&p->p_auxlock);
166 if ((rp = p->p_raslist) != NULL) {
167 p->p_raslist = NULL;
168 ras_sync();
169 for(; rp != NULL; rp = nrp) {
170 nrp = rp->ras_next;
171 kmem_free(rp, sizeof(*rp));
174 mutex_exit(&p->p_auxlock);
176 return 0;
179 #if defined(__HAVE_RAS)
182 * Install the new sequence. If it already exists, return
183 * an error.
185 static int
186 ras_install(void *addr, size_t len)
188 struct ras *rp;
189 struct ras *newrp;
190 void *endaddr;
191 int nras, error;
192 proc_t *p;
194 endaddr = (char *)addr + len;
196 if (addr < (void *)VM_MIN_ADDRESS ||
197 endaddr > (void *)VM_MAXUSER_ADDRESS)
198 return (EINVAL);
200 if (len <= 0)
201 return (EINVAL);
203 newrp = kmem_alloc(sizeof(*newrp), KM_SLEEP);
204 newrp->ras_startaddr = addr;
205 newrp->ras_endaddr = endaddr;
206 error = 0;
207 nras = 0;
208 p = curproc;
210 mutex_enter(&p->p_auxlock);
211 for (rp = p->p_raslist; rp != NULL; rp = rp->ras_next) {
212 if (++nras >= ras_per_proc) {
213 error = EINVAL;
214 break;
216 if (addr < rp->ras_endaddr && endaddr > rp->ras_startaddr) {
217 error = EEXIST;
218 break;
221 if (rp == NULL) {
222 newrp->ras_next = p->p_raslist;
223 p->p_raslist = newrp;
224 ras_sync();
225 mutex_exit(&p->p_auxlock);
226 } else {
227 mutex_exit(&p->p_auxlock);
228 kmem_free(newrp, sizeof(*newrp));
231 return error;
235 * Nuke the specified sequence. Both address and len must
236 * match, otherwise we return an error.
238 static int
239 ras_purge(void *addr, size_t len)
241 struct ras *rp, **link;
242 void *endaddr;
243 proc_t *p;
245 endaddr = (char *)addr + len;
246 p = curproc;
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)
252 break;
254 if (rp != NULL) {
255 *link = rp->ras_next;
256 ras_sync();
257 mutex_exit(&p->p_auxlock);
258 kmem_free(rp, sizeof(*rp));
259 return 0;
260 } else {
261 mutex_exit(&p->p_auxlock);
262 return ESRCH;
266 #endif /* defined(__HAVE_RAS) */
268 /*ARGSUSED*/
270 sys_rasctl(struct lwp *l, const struct sys_rasctl_args *uap, register_t *retval)
273 #if defined(__HAVE_RAS)
274 /* {
275 syscallarg(void *) addr;
276 syscallarg(size_t) len;
277 syscallarg(int) op;
278 } */
279 void *addr;
280 size_t len;
281 int op;
282 int error;
285 * first, extract syscall args from the uap.
288 addr = (void *)SCARG(uap, addr);
289 len = (size_t)SCARG(uap, len);
290 op = SCARG(uap, op);
292 DPRINTF(("sys_rasctl: p=%p addr=%p, len=%ld, op=0x%x\n",
293 curproc, addr, (long)len, op));
295 switch (op) {
296 case RAS_INSTALL:
297 error = ras_install(addr, len);
298 break;
299 case RAS_PURGE:
300 error = ras_purge(addr, len);
301 break;
302 case RAS_PURGE_ALL:
303 error = ras_purgeall();
304 break;
305 default:
306 error = EINVAL;
307 break;
310 return (error);
312 #else
314 return (EOPNOTSUPP);
316 #endif