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