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