kern_ras.c revision 1.38.18.1 1 1.38.18.1 martin /* $NetBSD: kern_ras.c,v 1.38.18.1 2020/04/13 08:05:03 martin Exp $ */
2 1.1 gmcgarry
3 1.1 gmcgarry /*-
4 1.31 ad * Copyright (c) 2002, 2006, 2007, 2008 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.21 ad * 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 *
19 1.1 gmcgarry * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 gmcgarry * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 gmcgarry * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 gmcgarry * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 gmcgarry * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 gmcgarry * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 gmcgarry * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 gmcgarry * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 gmcgarry * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 gmcgarry * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 gmcgarry * POSSIBILITY OF SUCH DAMAGE.
30 1.1 gmcgarry */
31 1.1 gmcgarry
32 1.1 gmcgarry #include <sys/cdefs.h>
33 1.38.18.1 martin __KERNEL_RCSID(0, "$NetBSD: kern_ras.c,v 1.38.18.1 2020/04/13 08:05:03 martin Exp $");
34 1.1 gmcgarry
35 1.1 gmcgarry #include <sys/param.h>
36 1.1 gmcgarry #include <sys/systm.h>
37 1.21 ad #include <sys/kernel.h>
38 1.31 ad #include <sys/kmem.h>
39 1.1 gmcgarry #include <sys/proc.h>
40 1.1 gmcgarry #include <sys/ras.h>
41 1.21 ad #include <sys/xcall.h>
42 1.1 gmcgarry #include <sys/syscallargs.h>
43 1.1 gmcgarry
44 1.1 gmcgarry #include <uvm/uvm_extern.h>
45 1.1 gmcgarry
46 1.1 gmcgarry #define MAX_RAS_PER_PROC 16
47 1.1 gmcgarry
48 1.3 thorpej u_int ras_per_proc = MAX_RAS_PER_PROC;
49 1.1 gmcgarry
50 1.1 gmcgarry #ifdef DEBUG
51 1.1 gmcgarry int ras_debug = 0;
52 1.1 gmcgarry #define DPRINTF(x) if (ras_debug) printf x
53 1.1 gmcgarry #else
54 1.1 gmcgarry #define DPRINTF(x) /* nothing */
55 1.1 gmcgarry #endif
56 1.1 gmcgarry
57 1.1 gmcgarry /*
58 1.21 ad * Force all CPUs through cpu_switchto(), waiting until complete.
59 1.23 ad * Context switching will drain the write buffer on the calling
60 1.23 ad * CPU.
61 1.21 ad */
62 1.21 ad static void
63 1.21 ad ras_sync(void)
64 1.21 ad {
65 1.21 ad
66 1.21 ad /* No need to sync if exiting or single threaded. */
67 1.21 ad if (curproc->p_nlwps > 1 && ncpu > 1) {
68 1.38.18.1 martin xc_barrier(0);
69 1.21 ad }
70 1.21 ad }
71 1.21 ad
72 1.21 ad /*
73 1.1 gmcgarry * Check the specified address to see if it is within the
74 1.1 gmcgarry * sequence. If it is found, we return the restart address,
75 1.1 gmcgarry * otherwise we return -1. If we do perform a restart, we
76 1.1 gmcgarry * mark the sequence as hit.
77 1.21 ad *
78 1.21 ad * No locking required: we disable preemption and ras_sync()
79 1.21 ad * guarantees that individual entries are valid while we still
80 1.21 ad * have visibility of them.
81 1.1 gmcgarry */
82 1.17 christos void *
83 1.17 christos ras_lookup(struct proc *p, void *addr)
84 1.1 gmcgarry {
85 1.1 gmcgarry struct ras *rp;
86 1.17 christos void *startaddr;
87 1.29 ad lwp_t *l;
88 1.16 ad
89 1.17 christos startaddr = (void *)-1;
90 1.29 ad l = curlwp;
91 1.1 gmcgarry
92 1.29 ad KPREEMPT_DISABLE(l);
93 1.21 ad for (rp = p->p_raslist; rp != NULL; rp = rp->ras_next) {
94 1.1 gmcgarry if (addr > rp->ras_startaddr && addr < rp->ras_endaddr) {
95 1.16 ad startaddr = rp->ras_startaddr;
96 1.1 gmcgarry DPRINTF(("RAS hit: p=%p %p\n", p, addr));
97 1.16 ad break;
98 1.1 gmcgarry }
99 1.1 gmcgarry }
100 1.29 ad KPREEMPT_ENABLE(l);
101 1.1 gmcgarry
102 1.21 ad return startaddr;
103 1.1 gmcgarry }
104 1.1 gmcgarry
105 1.1 gmcgarry /*
106 1.1 gmcgarry * During a fork, we copy all of the sequences from parent p1 to
107 1.1 gmcgarry * the child p2.
108 1.21 ad *
109 1.21 ad * No locking required as the parent must be paused.
110 1.1 gmcgarry */
111 1.1 gmcgarry int
112 1.1 gmcgarry ras_fork(struct proc *p1, struct proc *p2)
113 1.1 gmcgarry {
114 1.1 gmcgarry struct ras *rp, *nrp;
115 1.9 yamt
116 1.21 ad for (rp = p1->p_raslist; rp != NULL; rp = rp->ras_next) {
117 1.31 ad nrp = kmem_alloc(sizeof(*nrp), KM_SLEEP);
118 1.1 gmcgarry nrp->ras_startaddr = rp->ras_startaddr;
119 1.1 gmcgarry nrp->ras_endaddr = rp->ras_endaddr;
120 1.27 dsl nrp->ras_next = p2->p_raslist;
121 1.21 ad p2->p_raslist = nrp;
122 1.9 yamt }
123 1.9 yamt
124 1.22 ad DPRINTF(("ras_fork: p1=%p, p2=%p\n", p1, p2));
125 1.1 gmcgarry
126 1.21 ad return 0;
127 1.1 gmcgarry }
128 1.1 gmcgarry
129 1.1 gmcgarry /*
130 1.1 gmcgarry * Nuke all sequences for this process.
131 1.1 gmcgarry */
132 1.1 gmcgarry int
133 1.21 ad ras_purgeall(void)
134 1.1 gmcgarry {
135 1.21 ad struct ras *rp, *nrp;
136 1.21 ad proc_t *p;
137 1.21 ad
138 1.21 ad p = curproc;
139 1.1 gmcgarry
140 1.32 ad if (p->p_raslist == NULL)
141 1.33 ad return 0;
142 1.32 ad
143 1.25 ad mutex_enter(&p->p_auxlock);
144 1.21 ad if ((rp = p->p_raslist) != NULL) {
145 1.21 ad p->p_raslist = NULL;
146 1.21 ad ras_sync();
147 1.21 ad for(; rp != NULL; rp = nrp) {
148 1.21 ad nrp = rp->ras_next;
149 1.31 ad kmem_free(rp, sizeof(*rp));
150 1.21 ad }
151 1.1 gmcgarry }
152 1.25 ad mutex_exit(&p->p_auxlock);
153 1.1 gmcgarry
154 1.21 ad return 0;
155 1.1 gmcgarry }
156 1.1 gmcgarry
157 1.12 hannken #if defined(__HAVE_RAS)
158 1.12 hannken
159 1.36 martin #if __GNUC_PREREQ__(4, 8)
160 1.36 martin #define __WARNING_PUSH_LESS_NULL_PTR _Pragma("GCC diagnostic push") _Pragma("GCC diagnostic ignored \"-Wextra\"")
161 1.36 martin #define __WARNING_POP_LESS_NULL_PTR _Pragma("GCC diagnostic pop")
162 1.36 martin #else
163 1.36 martin #define __WARNING_PUSH_LESS_NULL_PTR
164 1.36 martin #define __WARNING_POP_LESS_NULL_PTR
165 1.36 martin #endif
166 1.36 martin
167 1.1 gmcgarry /*
168 1.1 gmcgarry * Install the new sequence. If it already exists, return
169 1.1 gmcgarry * an error.
170 1.1 gmcgarry */
171 1.11 thorpej static int
172 1.21 ad ras_install(void *addr, size_t len)
173 1.1 gmcgarry {
174 1.1 gmcgarry struct ras *rp;
175 1.8 yamt struct ras *newrp;
176 1.21 ad void *endaddr;
177 1.21 ad int nras, error;
178 1.21 ad proc_t *p;
179 1.21 ad
180 1.37 maxv if (len == 0)
181 1.37 maxv return EINVAL;
182 1.37 maxv
183 1.21 ad endaddr = (char *)addr + len;
184 1.1 gmcgarry
185 1.37 maxv /* Do not warn about < NULL pointer comparison */
186 1.36 martin __WARNING_PUSH_LESS_NULL_PTR
187 1.37 maxv if (addr < (void *)VM_MIN_ADDRESS || addr > (void *)VM_MAXUSER_ADDRESS)
188 1.37 maxv return EINVAL;
189 1.38 maxv if (endaddr > (void *)VM_MAXUSER_ADDRESS)
190 1.38 maxv return EINVAL;
191 1.37 maxv if (endaddr < addr)
192 1.37 maxv return EINVAL;
193 1.36 martin __WARNING_POP_LESS_NULL_PTR
194 1.1 gmcgarry
195 1.31 ad newrp = kmem_alloc(sizeof(*newrp), KM_SLEEP);
196 1.21 ad newrp->ras_startaddr = addr;
197 1.21 ad newrp->ras_endaddr = endaddr;
198 1.21 ad error = 0;
199 1.21 ad nras = 0;
200 1.21 ad p = curproc;
201 1.21 ad
202 1.25 ad mutex_enter(&p->p_auxlock);
203 1.21 ad for (rp = p->p_raslist; rp != NULL; rp = rp->ras_next) {
204 1.19 thorpej if (++nras >= ras_per_proc) {
205 1.21 ad error = EINVAL;
206 1.21 ad break;
207 1.1 gmcgarry }
208 1.19 thorpej if (addr < rp->ras_endaddr && endaddr > rp->ras_startaddr) {
209 1.21 ad error = EEXIST;
210 1.21 ad break;
211 1.19 thorpej }
212 1.1 gmcgarry }
213 1.21 ad if (rp == NULL) {
214 1.21 ad newrp->ras_next = p->p_raslist;
215 1.21 ad p->p_raslist = newrp;
216 1.21 ad ras_sync();
217 1.25 ad mutex_exit(&p->p_auxlock);
218 1.21 ad } else {
219 1.25 ad mutex_exit(&p->p_auxlock);
220 1.31 ad kmem_free(newrp, sizeof(*newrp));
221 1.8 yamt }
222 1.1 gmcgarry
223 1.21 ad return error;
224 1.1 gmcgarry }
225 1.1 gmcgarry
226 1.1 gmcgarry /*
227 1.1 gmcgarry * Nuke the specified sequence. Both address and len must
228 1.1 gmcgarry * match, otherwise we return an error.
229 1.1 gmcgarry */
230 1.11 thorpej static int
231 1.21 ad ras_purge(void *addr, size_t len)
232 1.1 gmcgarry {
233 1.21 ad struct ras *rp, **link;
234 1.21 ad void *endaddr;
235 1.21 ad proc_t *p;
236 1.21 ad
237 1.21 ad endaddr = (char *)addr + len;
238 1.21 ad p = curproc;
239 1.21 ad
240 1.25 ad mutex_enter(&p->p_auxlock);
241 1.21 ad link = &p->p_raslist;
242 1.21 ad for (rp = *link; rp != NULL; link = &rp->ras_next, rp = *link) {
243 1.21 ad if (addr == rp->ras_startaddr && endaddr == rp->ras_endaddr)
244 1.1 gmcgarry break;
245 1.1 gmcgarry }
246 1.16 ad if (rp != NULL) {
247 1.21 ad *link = rp->ras_next;
248 1.21 ad ras_sync();
249 1.25 ad mutex_exit(&p->p_auxlock);
250 1.31 ad kmem_free(rp, sizeof(*rp));
251 1.21 ad return 0;
252 1.21 ad } else {
253 1.25 ad mutex_exit(&p->p_auxlock);
254 1.21 ad return ESRCH;
255 1.16 ad }
256 1.1 gmcgarry }
257 1.1 gmcgarry
258 1.12 hannken #endif /* defined(__HAVE_RAS) */
259 1.12 hannken
260 1.1 gmcgarry /*ARGSUSED*/
261 1.1 gmcgarry int
262 1.24 dsl sys_rasctl(struct lwp *l, const struct sys_rasctl_args *uap, register_t *retval)
263 1.1 gmcgarry {
264 1.1 gmcgarry #if defined(__HAVE_RAS)
265 1.24 dsl /* {
266 1.17 christos syscallarg(void *) addr;
267 1.1 gmcgarry syscallarg(size_t) len;
268 1.1 gmcgarry syscallarg(int) op;
269 1.24 dsl } */
270 1.17 christos void *addr;
271 1.1 gmcgarry size_t len;
272 1.1 gmcgarry int op;
273 1.1 gmcgarry int error;
274 1.1 gmcgarry
275 1.1 gmcgarry /*
276 1.1 gmcgarry * first, extract syscall args from the uap.
277 1.1 gmcgarry */
278 1.1 gmcgarry
279 1.17 christos addr = (void *)SCARG(uap, addr);
280 1.1 gmcgarry len = (size_t)SCARG(uap, len);
281 1.1 gmcgarry op = SCARG(uap, op);
282 1.1 gmcgarry
283 1.2 thorpej DPRINTF(("sys_rasctl: p=%p addr=%p, len=%ld, op=0x%x\n",
284 1.21 ad curproc, addr, (long)len, op));
285 1.1 gmcgarry
286 1.1 gmcgarry switch (op) {
287 1.1 gmcgarry case RAS_INSTALL:
288 1.21 ad error = ras_install(addr, len);
289 1.1 gmcgarry break;
290 1.1 gmcgarry case RAS_PURGE:
291 1.21 ad error = ras_purge(addr, len);
292 1.1 gmcgarry break;
293 1.1 gmcgarry case RAS_PURGE_ALL:
294 1.21 ad error = ras_purgeall();
295 1.1 gmcgarry break;
296 1.1 gmcgarry default:
297 1.1 gmcgarry error = EINVAL;
298 1.1 gmcgarry break;
299 1.1 gmcgarry }
300 1.1 gmcgarry
301 1.1 gmcgarry return (error);
302 1.1 gmcgarry #else
303 1.1 gmcgarry return (EOPNOTSUPP);
304 1.1 gmcgarry #endif
305 1.1 gmcgarry }
306