Home | History | Annotate | Line # | Download | only in kern
      1 /*	$NetBSD: kern_ras.c,v 1.44 2026/01/04 01:37:23 riastradh 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.44 2026/01/04 01:37:23 riastradh Exp $");
     34 
     35 #include <sys/param.h>
     36 #include <sys/types.h>
     37 
     38 #include <sys/kernel.h>
     39 #include <sys/kmem.h>
     40 #include <sys/proc.h>
     41 #include <sys/ras.h>
     42 #include <sys/sdt.h>
     43 #include <sys/syscallargs.h>
     44 #include <sys/systm.h>
     45 #include <sys/xcall.h>
     46 
     47 #include <uvm/uvm_extern.h>
     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 		xc_barrier(0);
     72 	}
     73 }
     74 
     75 /*
     76  * Check the specified address to see if it is within the
     77  * sequence.  If it is found, we return the restart address,
     78  * otherwise we return -1.  If we do perform a restart, we
     79  * mark the sequence as hit.
     80  *
     81  * No locking required: we disable preemption and ras_sync()
     82  * guarantees that individual entries are valid while we still
     83  * have visibility of them.
     84  */
     85 void *
     86 ras_lookup(struct proc *p, void *addr)
     87 {
     88 	struct ras *rp;
     89 	void *startaddr;
     90 	lwp_t *l;
     91 
     92 	startaddr = (void *)-1;
     93 	l = curlwp;
     94 
     95 	KPREEMPT_DISABLE(l);
     96 	for (rp = p->p_raslist; rp != NULL; rp = rp->ras_next) {
     97 		if (addr > rp->ras_startaddr && addr < rp->ras_endaddr) {
     98 			startaddr = rp->ras_startaddr;
     99 			DPRINTF(("RAS hit: p=%p %p\n", p, addr));
    100 			break;
    101 		}
    102 	}
    103 	KPREEMPT_ENABLE(l);
    104 
    105 	return startaddr;
    106 }
    107 
    108 /*
    109  * During a fork, we copy all of the sequences from parent p1 to
    110  * the child p2.
    111  *
    112  * No locking required as the parent must be paused.
    113  */
    114 int
    115 ras_fork(struct proc *p1, struct proc *p2)
    116 {
    117 	struct ras *rp, *nrp;
    118 
    119 	for (rp = p1->p_raslist; rp != NULL; rp = rp->ras_next) {
    120 		nrp = kmem_alloc(sizeof(*nrp), KM_SLEEP);
    121 		nrp->ras_startaddr = rp->ras_startaddr;
    122 		nrp->ras_endaddr = rp->ras_endaddr;
    123 		nrp->ras_next = p2->p_raslist;
    124 		p2->p_raslist = nrp;
    125 	}
    126 
    127 	DPRINTF(("ras_fork: p1=%p, p2=%p\n", p1, p2));
    128 
    129 	return 0;
    130 }
    131 
    132 /*
    133  * Nuke all sequences for this process.
    134  */
    135 int
    136 ras_purgeall(void)
    137 {
    138 	struct ras *rp, *nrp;
    139 	proc_t *p;
    140 
    141 	p = curproc;
    142 
    143 	if (p->p_raslist == NULL)
    144 		return 0;
    145 
    146 	mutex_enter(&p->p_auxlock);
    147 	if ((rp = p->p_raslist) != NULL) {
    148 		p->p_raslist = NULL;
    149 		ras_sync();
    150 		for(; rp != NULL; rp = nrp) {
    151 			nrp = rp->ras_next;
    152 			kmem_free(rp, sizeof(*rp));
    153 		}
    154 	}
    155 	mutex_exit(&p->p_auxlock);
    156 
    157 	return 0;
    158 }
    159 
    160 #if defined(__HAVE_RAS)
    161 
    162 /*
    163  * Install the new sequence.  If it already exists, return
    164  * an error.
    165  */
    166 static int
    167 ras_install(void *addr, size_t len)
    168 {
    169 	struct ras *rp;
    170 	struct ras *newrp;
    171 	void *endaddr;
    172 	int nras, error;
    173 	proc_t *p;
    174 
    175 	if (len == 0)
    176 		return SET_ERROR(EINVAL);
    177 
    178 	if ((uintptr_t)addr < VM_MIN_ADDRESS ||
    179 	    (uintptr_t)addr > VM_MAXUSER_ADDRESS)
    180 		return SET_ERROR(EINVAL);
    181 	if (len > VM_MAXUSER_ADDRESS - (uintptr_t)addr)
    182 		return SET_ERROR(EINVAL);
    183 	endaddr = (char *)addr + len;
    184 
    185 	newrp = kmem_alloc(sizeof(*newrp), KM_SLEEP);
    186 	newrp->ras_startaddr = addr;
    187 	newrp->ras_endaddr = endaddr;
    188 	error = 0;
    189 	nras = 0;
    190 	p = curproc;
    191 
    192 	mutex_enter(&p->p_auxlock);
    193 	for (rp = p->p_raslist; rp != NULL; rp = rp->ras_next) {
    194 		if (++nras >= ras_per_proc) {
    195 			error = SET_ERROR(EINVAL);
    196 			break;
    197 		}
    198 		if (addr < rp->ras_endaddr && endaddr > rp->ras_startaddr) {
    199 			error = SET_ERROR(EEXIST);
    200 			break;
    201 		}
    202 	}
    203 	if (rp == NULL) {
    204 		newrp->ras_next = p->p_raslist;
    205 		p->p_raslist = newrp;
    206 		ras_sync();
    207 	 	mutex_exit(&p->p_auxlock);
    208 	} else {
    209 	 	mutex_exit(&p->p_auxlock);
    210  		kmem_free(newrp, sizeof(*newrp));
    211 	}
    212 
    213 	return error;
    214 }
    215 
    216 /*
    217  * Nuke the specified sequence.  Both address and len must
    218  * match, otherwise we return an error.
    219  */
    220 static int
    221 ras_purge(void *addr, size_t len)
    222 {
    223 	struct ras *rp, **link;
    224 	proc_t *p;
    225 
    226 	p = curproc;
    227 
    228 	mutex_enter(&p->p_auxlock);
    229 	link = &p->p_raslist;
    230 	for (rp = *link; rp != NULL; link = &rp->ras_next, rp = *link) {
    231 		if (addr == rp->ras_startaddr &&
    232 		    (char *)rp->ras_endaddr - (char *)rp->ras_startaddr == len)
    233 			break;
    234 	}
    235 	if (rp != NULL) {
    236 		*link = rp->ras_next;
    237 		ras_sync();
    238 		mutex_exit(&p->p_auxlock);
    239 		kmem_free(rp, sizeof(*rp));
    240 		return 0;
    241 	} else {
    242 		mutex_exit(&p->p_auxlock);
    243 		return SET_ERROR(ESRCH);
    244 	}
    245 }
    246 
    247 #endif /* defined(__HAVE_RAS) */
    248 
    249 /*ARGSUSED*/
    250 int
    251 sys_rasctl(struct lwp *l, const struct sys_rasctl_args *uap, register_t *retval)
    252 {
    253 #if defined(__HAVE_RAS)
    254 	/* {
    255 		syscallarg(void *) addr;
    256 		syscallarg(size_t) len;
    257 		syscallarg(int) op;
    258 	} */
    259 	void *addr;
    260 	size_t len;
    261 	int op;
    262 	int error;
    263 
    264 	/*
    265 	 * first, extract syscall args from the uap.
    266 	 */
    267 
    268 	addr = (void *)SCARG(uap, addr);
    269 	len = (size_t)SCARG(uap, len);
    270 	op = SCARG(uap, op);
    271 
    272 	DPRINTF(("sys_rasctl: p=%p addr=%p, len=%ld, op=0x%x\n",
    273 	    curproc, addr, (long)len, op));
    274 
    275 	switch (op) {
    276 	case RAS_INSTALL:
    277 		error = ras_install(addr, len);
    278 		break;
    279 	case RAS_PURGE:
    280 		error = ras_purge(addr, len);
    281 		break;
    282 	case RAS_PURGE_ALL:
    283 		error = ras_purgeall();
    284 		break;
    285 	default:
    286 		error = SET_ERROR(EINVAL);
    287 		break;
    288 	}
    289 
    290 	return error;
    291 #else
    292 	return SET_ERROR(EOPNOTSUPP);
    293 #endif
    294 }
    295