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