xref: /netbsd-src/sys/kern/kern_ras.c (revision d710132b4b8ce7f7cccaaf660cb16aa16b4077a0)
1 /*	$NetBSD: kern_ras.c,v 1.4 2003/01/18 10:06:29 thorpej Exp $	*/
2 
3 /*-
4  * Copyright (c) 2002 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.
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.4 2003/01/18 10:06:29 thorpej Exp $");
41 
42 #include <sys/param.h>
43 #include <sys/lock.h>
44 #include <sys/systm.h>
45 #include <sys/pool.h>
46 #include <sys/proc.h>
47 #include <sys/ras.h>
48 #include <sys/sa.h>
49 #include <sys/savar.h>
50 
51 #include <sys/mount.h>
52 #include <sys/syscallargs.h>
53 
54 #include <uvm/uvm_extern.h>
55 
56 #define MAX_RAS_PER_PROC	16
57 
58 u_int ras_per_proc = MAX_RAS_PER_PROC;
59 
60 #ifdef DEBUG
61 int ras_debug = 0;
62 #define DPRINTF(x)	if (ras_debug) printf x
63 #else
64 #define DPRINTF(x)	/* nothing */
65 #endif
66 
67 int ras_install(struct proc *, caddr_t, size_t);
68 int ras_purge(struct proc *, caddr_t, size_t);
69 
70 extern struct pool ras_pool;
71 
72 /*
73  * Check the specified address to see if it is within the
74  * sequence.  If it is found, we return the restart address,
75  * otherwise we return -1.  If we do perform a restart, we
76  * mark the sequence as hit.
77  */
78 caddr_t
79 ras_lookup(struct proc *p, caddr_t addr)
80 {
81 	struct ras *rp;
82 
83 #ifdef DIAGNOSTIC
84 	if (addr < (caddr_t)VM_MIN_ADDRESS ||
85 	    addr > (caddr_t)VM_MAXUSER_ADDRESS)
86 		return ((caddr_t)-1);
87 #endif
88 
89 	simple_lock(&p->p_raslock);
90 	LIST_FOREACH(rp, &p->p_raslist, ras_list) {
91 		if (addr > rp->ras_startaddr && addr < rp->ras_endaddr) {
92 			rp->ras_hits++;
93 			simple_unlock(&p->p_raslock);
94 #ifdef DIAGNOSTIC
95 			DPRINTF(("RAS hit: p=%p %p\n", p, addr));
96 #endif
97 			return (rp->ras_startaddr);
98 		}
99 	}
100 	simple_unlock(&p->p_raslock);
101 
102 	return ((caddr_t)-1);
103 }
104 
105 /*
106  * During a fork, we copy all of the sequences from parent p1 to
107  * the child p2.
108  */
109 int
110 ras_fork(struct proc *p1, struct proc *p2)
111 {
112 	struct ras *rp, *nrp;
113 
114 	DPRINTF(("ras_fork: p1=%p, p2=%p, p1->p_nras=%d\n",
115 	    p1, p2, p1->p_nras));
116 
117 	simple_lock(&p1->p_raslock);
118 	LIST_FOREACH(rp, &p1->p_raslist, ras_list) {
119 		nrp = pool_get(&ras_pool, PR_NOWAIT);
120 		nrp->ras_startaddr = rp->ras_startaddr;
121 		nrp->ras_endaddr = rp->ras_endaddr;
122 		nrp->ras_hits = 0;
123 		LIST_INSERT_HEAD(&p2->p_raslist, nrp, ras_list);
124 	}
125 	p2->p_nras = p1->p_nras;
126 	simple_unlock(&p1->p_raslock);
127 
128 	return (0);
129 }
130 
131 /*
132  * Nuke all sequences for this process.
133  */
134 int
135 ras_purgeall(struct proc *p)
136 {
137 	struct ras *rp;
138 
139 	simple_lock(&p->p_raslock);
140 	while (!LIST_EMPTY(&p->p_raslist)) {
141 		rp = LIST_FIRST(&p->p_raslist);
142                 DPRINTF(("RAS %p-%p, hits %d\n", rp->ras_startaddr,
143                     rp->ras_endaddr, rp->ras_hits));
144 		LIST_REMOVE(rp, ras_list);
145 		pool_put(&ras_pool, rp);
146 	}
147 	p->p_nras = 0;
148 	simple_unlock(&p->p_raslock);
149 
150 	return (0);
151 }
152 
153 /*
154  * Install the new sequence.  If it already exists, return
155  * an error.
156  */
157 int
158 ras_install(struct proc *p, caddr_t addr, size_t len)
159 {
160 	struct ras *rp;
161 	caddr_t endaddr = addr + len;
162 
163 	if (addr < (caddr_t)VM_MIN_ADDRESS ||
164 	    addr > (caddr_t)VM_MAXUSER_ADDRESS)
165 		return (EINVAL);
166 
167 	if (len <= 0)
168 		return (EINVAL);
169 
170 	if (p->p_nras >= ras_per_proc)
171 		return (EINVAL);
172 
173 	simple_lock(&p->p_raslock);
174 	LIST_FOREACH(rp, &p->p_raslist, ras_list) {
175 		if ((addr > rp->ras_startaddr && addr < rp->ras_endaddr) ||
176 		    (endaddr > rp->ras_startaddr &&
177 			 endaddr < rp->ras_endaddr) ||
178 		    (addr < rp->ras_startaddr && endaddr > rp->ras_endaddr)) {
179 			simple_unlock(&p->p_raslock);
180 			return (EINVAL);
181 		}
182 	}
183 	rp = pool_get(&ras_pool, PR_NOWAIT);
184 	rp->ras_startaddr = addr;
185 	rp->ras_endaddr = endaddr;
186 	rp->ras_hits = 0;
187 	LIST_INSERT_HEAD(&p->p_raslist, rp, ras_list);
188 	p->p_nras++;
189 	simple_unlock(&p->p_raslock);
190 
191 	return (0);
192 }
193 
194 /*
195  * Nuke the specified sequence.  Both address and len must
196  * match, otherwise we return an error.
197  */
198 int
199 ras_purge(struct proc *p, caddr_t addr, size_t len)
200 {
201 	struct ras *rp;
202 	caddr_t endaddr = addr + len;
203 	int error = ESRCH;
204 
205 	simple_lock(&p->p_raslock);
206 	LIST_FOREACH(rp, &p->p_raslist, ras_list) {
207 		if (addr == rp->ras_startaddr && endaddr == rp->ras_endaddr) {
208 			LIST_REMOVE(rp, ras_list);
209 			pool_put(&ras_pool, rp);
210 			p->p_nras--;
211 			error = 0;
212 			break;
213 		}
214 	}
215 	simple_unlock(&p->p_raslock);
216 
217 	return (error);
218 }
219 
220 /*ARGSUSED*/
221 int
222 sys_rasctl(struct lwp *l, void *v, register_t *retval)
223 {
224 
225 #if defined(__HAVE_RAS)
226 
227 	struct sys_rasctl_args /* {
228 		syscallarg(caddr_t) addr;
229 		syscallarg(size_t) len;
230 		syscallarg(int) op;
231 	} */ *uap = v;
232 	struct proc *p = l->l_proc;
233 	caddr_t addr;
234 	size_t len;
235 	int op;
236 	int error;
237 
238 	/*
239 	 * first, extract syscall args from the uap.
240 	 */
241 
242 	addr = (caddr_t)SCARG(uap, addr);
243 	len = (size_t)SCARG(uap, len);
244 	op = SCARG(uap, op);
245 
246 	DPRINTF(("sys_rasctl: p=%p addr=%p, len=%ld, op=0x%x\n",
247 	    p, addr, (long)len, op));
248 
249 	switch (op) {
250 	case RAS_INSTALL:
251 		error = ras_install(p, addr, len);
252 		break;
253 	case RAS_PURGE:
254 		error = ras_purge(p, addr, len);
255 		break;
256 	case RAS_PURGE_ALL:
257 		error = ras_purgeall(p);
258 		break;
259 	default:
260 		error = EINVAL;
261 		break;
262 	}
263 
264 	return (error);
265 
266 #else
267 
268 	return (EOPNOTSUPP);
269 
270 #endif
271 
272 }
273