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