1 /* $NetBSD: kern_ras.c,v 1.30 2008/04/28 20:24:03 martin 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 * 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. 30 */ 31 32 #include <sys/cdefs.h> 33 __KERNEL_RCSID(0, "$NetBSD: kern_ras.c,v 1.30 2008/04/28 20:24:03 martin Exp $"); 34 35 #include <sys/param.h> 36 #include <sys/systm.h> 37 #include <sys/kernel.h> 38 #include <sys/pool.h> 39 #include <sys/proc.h> 40 #include <sys/ras.h> 41 #include <sys/xcall.h> 42 #include <sys/syscallargs.h> 43 44 #include <uvm/uvm_extern.h> 45 46 POOL_INIT(ras_pool, sizeof(struct ras), 0, 0, 0, "raspl", 47 &pool_allocator_nointr, IPL_NONE); 48 49 #define MAX_RAS_PER_PROC 16 50 51 u_int ras_per_proc = MAX_RAS_PER_PROC; 52 53 #ifdef DEBUG 54 int ras_debug = 0; 55 #define DPRINTF(x) if (ras_debug) printf x 56 #else 57 #define DPRINTF(x) /* nothing */ 58 #endif 59 60 /* 61 * Force all CPUs through cpu_switchto(), waiting until complete. 62 * Context switching will drain the write buffer on the calling 63 * CPU. 64 */ 65 static void 66 ras_sync(void) 67 { 68 69 /* No need to sync if exiting or single threaded. */ 70 if (curproc->p_nlwps > 1 && ncpu > 1) { 71 #ifdef NO_SOFTWARE_PATENTS 72 uint64_t where; 73 where = xc_broadcast(0, (xcfunc_t)nullop, NULL, NULL); 74 xc_wait(where); 75 #else 76 /* 77 * Assumptions: 78 * 79 * o preemption is disabled by the thread in 80 * ras_lookup(). 81 * o proc::p_raslist is only inspected with 82 * preemption disabled. 83 * o ras_lookup() plus loads reordered in advance 84 * will take no longer than 1/8s to complete. 85 */ 86 const int delta = hz >> 3; 87 int target = hardclock_ticks + delta; 88 do { 89 kpause("ras", false, delta, NULL); 90 } while (hardclock_ticks < target); 91 #endif 92 } 93 } 94 95 /* 96 * Check the specified address to see if it is within the 97 * sequence. If it is found, we return the restart address, 98 * otherwise we return -1. If we do perform a restart, we 99 * mark the sequence as hit. 100 * 101 * No locking required: we disable preemption and ras_sync() 102 * guarantees that individual entries are valid while we still 103 * have visibility of them. 104 */ 105 void * 106 ras_lookup(struct proc *p, void *addr) 107 { 108 struct ras *rp; 109 void *startaddr; 110 lwp_t *l; 111 112 startaddr = (void *)-1; 113 l = curlwp; 114 115 KPREEMPT_DISABLE(l); 116 for (rp = p->p_raslist; rp != NULL; rp = rp->ras_next) { 117 if (addr > rp->ras_startaddr && addr < rp->ras_endaddr) { 118 startaddr = rp->ras_startaddr; 119 DPRINTF(("RAS hit: p=%p %p\n", p, addr)); 120 break; 121 } 122 } 123 KPREEMPT_ENABLE(l); 124 125 return startaddr; 126 } 127 128 /* 129 * During a fork, we copy all of the sequences from parent p1 to 130 * the child p2. 131 * 132 * No locking required as the parent must be paused. 133 */ 134 int 135 ras_fork(struct proc *p1, struct proc *p2) 136 { 137 struct ras *rp, *nrp; 138 139 for (rp = p1->p_raslist; rp != NULL; rp = rp->ras_next) { 140 nrp = pool_get(&ras_pool, PR_WAITOK); 141 nrp->ras_startaddr = rp->ras_startaddr; 142 nrp->ras_endaddr = rp->ras_endaddr; 143 nrp->ras_next = p2->p_raslist; 144 p2->p_raslist = nrp; 145 } 146 147 DPRINTF(("ras_fork: p1=%p, p2=%p\n", p1, p2)); 148 149 return 0; 150 } 151 152 /* 153 * Nuke all sequences for this process. 154 */ 155 int 156 ras_purgeall(void) 157 { 158 struct ras *rp, *nrp; 159 proc_t *p; 160 161 p = curproc; 162 163 mutex_enter(&p->p_auxlock); 164 if ((rp = p->p_raslist) != NULL) { 165 p->p_raslist = NULL; 166 ras_sync(); 167 for(; rp != NULL; rp = nrp) { 168 nrp = rp->ras_next; 169 pool_put(&ras_pool, rp); 170 } 171 } 172 mutex_exit(&p->p_auxlock); 173 174 return 0; 175 } 176 177 #if defined(__HAVE_RAS) 178 179 /* 180 * Install the new sequence. If it already exists, return 181 * an error. 182 */ 183 static int 184 ras_install(void *addr, size_t len) 185 { 186 struct ras *rp; 187 struct ras *newrp; 188 void *endaddr; 189 int nras, error; 190 proc_t *p; 191 192 endaddr = (char *)addr + len; 193 194 if (addr < (void *)VM_MIN_ADDRESS || 195 endaddr > (void *)VM_MAXUSER_ADDRESS) 196 return (EINVAL); 197 198 if (len <= 0) 199 return (EINVAL); 200 201 newrp = pool_get(&ras_pool, PR_WAITOK); 202 newrp->ras_startaddr = addr; 203 newrp->ras_endaddr = endaddr; 204 error = 0; 205 nras = 0; 206 p = curproc; 207 208 mutex_enter(&p->p_auxlock); 209 for (rp = p->p_raslist; rp != NULL; rp = rp->ras_next) { 210 if (++nras >= ras_per_proc) { 211 error = EINVAL; 212 break; 213 } 214 if (addr < rp->ras_endaddr && endaddr > rp->ras_startaddr) { 215 error = EEXIST; 216 break; 217 } 218 } 219 if (rp == NULL) { 220 newrp->ras_next = p->p_raslist; 221 p->p_raslist = newrp; 222 ras_sync(); 223 mutex_exit(&p->p_auxlock); 224 } else { 225 mutex_exit(&p->p_auxlock); 226 pool_put(&ras_pool, newrp); 227 } 228 229 return error; 230 } 231 232 /* 233 * Nuke the specified sequence. Both address and len must 234 * match, otherwise we return an error. 235 */ 236 static int 237 ras_purge(void *addr, size_t len) 238 { 239 struct ras *rp, **link; 240 void *endaddr; 241 proc_t *p; 242 243 endaddr = (char *)addr + len; 244 p = curproc; 245 246 mutex_enter(&p->p_auxlock); 247 link = &p->p_raslist; 248 for (rp = *link; rp != NULL; link = &rp->ras_next, rp = *link) { 249 if (addr == rp->ras_startaddr && endaddr == rp->ras_endaddr) 250 break; 251 } 252 if (rp != NULL) { 253 *link = rp->ras_next; 254 ras_sync(); 255 mutex_exit(&p->p_auxlock); 256 pool_put(&ras_pool, rp); 257 return 0; 258 } else { 259 mutex_exit(&p->p_auxlock); 260 return ESRCH; 261 } 262 } 263 264 #endif /* defined(__HAVE_RAS) */ 265 266 /*ARGSUSED*/ 267 int 268 sys_rasctl(struct lwp *l, const struct sys_rasctl_args *uap, register_t *retval) 269 { 270 271 #if defined(__HAVE_RAS) 272 /* { 273 syscallarg(void *) addr; 274 syscallarg(size_t) len; 275 syscallarg(int) op; 276 } */ 277 void *addr; 278 size_t len; 279 int op; 280 int error; 281 282 /* 283 * first, extract syscall args from the uap. 284 */ 285 286 addr = (void *)SCARG(uap, addr); 287 len = (size_t)SCARG(uap, len); 288 op = SCARG(uap, op); 289 290 DPRINTF(("sys_rasctl: p=%p addr=%p, len=%ld, op=0x%x\n", 291 curproc, addr, (long)len, op)); 292 293 switch (op) { 294 case RAS_INSTALL: 295 error = ras_install(addr, len); 296 break; 297 case RAS_PURGE: 298 error = ras_purge(addr, len); 299 break; 300 case RAS_PURGE_ALL: 301 error = ras_purgeall(); 302 break; 303 default: 304 error = EINVAL; 305 break; 306 } 307 308 return (error); 309 310 #else 311 312 return (EOPNOTSUPP); 313 314 #endif 315 316 } 317