Home | History | Annotate | Line # | Download | only in kern
kern_lock.c revision 1.51.2.7
      1 /*	$NetBSD: kern_lock.c,v 1.51.2.7 2001/11/27 03:17:18 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999, 2000 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  * NASA Ames Research Center.
     10  *
     11  * This code is derived from software contributed to The NetBSD Foundation
     12  * by Ross Harvey.
     13  *
     14  * Redistribution and use in source and binary forms, with or without
     15  * modification, are permitted provided that the following conditions
     16  * are met:
     17  * 1. Redistributions of source code must retain the above copyright
     18  *    notice, this list of conditions and the following disclaimer.
     19  * 2. Redistributions in binary form must reproduce the above copyright
     20  *    notice, this list of conditions and the following disclaimer in the
     21  *    documentation and/or other materials provided with the distribution.
     22  * 3. All advertising materials mentioning features or use of this software
     23  *    must display the following acknowledgement:
     24  *	This product includes software developed by the NetBSD
     25  *	Foundation, Inc. and its contributors.
     26  * 4. Neither the name of The NetBSD Foundation nor the names of its
     27  *    contributors may be used to endorse or promote products derived
     28  *    from this software without specific prior written permission.
     29  *
     30  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     31  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     32  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     33  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     34  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     35  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     36  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     37  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     38  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     39  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     40  * POSSIBILITY OF SUCH DAMAGE.
     41  */
     42 
     43 /*
     44  * Copyright (c) 1995
     45  *	The Regents of the University of California.  All rights reserved.
     46  *
     47  * This code contains ideas from software contributed to Berkeley by
     48  * Avadis Tevanian, Jr., Michael Wayne Young, and the Mach Operating
     49  * System project at Carnegie-Mellon University.
     50  *
     51  * Redistribution and use in source and binary forms, with or without
     52  * modification, are permitted provided that the following conditions
     53  * are met:
     54  * 1. Redistributions of source code must retain the above copyright
     55  *    notice, this list of conditions and the following disclaimer.
     56  * 2. Redistributions in binary form must reproduce the above copyright
     57  *    notice, this list of conditions and the following disclaimer in the
     58  *    documentation and/or other materials provided with the distribution.
     59  * 3. All advertising materials mentioning features or use of this software
     60  *    must display the following acknowledgement:
     61  *	This product includes software developed by the University of
     62  *	California, Berkeley and its contributors.
     63  * 4. Neither the name of the University nor the names of its contributors
     64  *    may be used to endorse or promote products derived from this software
     65  *    without specific prior written permission.
     66  *
     67  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     68  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     69  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     70  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     71  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     72  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     73  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     74  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     75  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     76  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     77  * SUCH DAMAGE.
     78  *
     79  *	@(#)kern_lock.c	8.18 (Berkeley) 5/21/95
     80  */
     81 
     82 #include <sys/cdefs.h>
     83 __KERNEL_RCSID(0, "$NetBSD: kern_lock.c,v 1.51.2.7 2001/11/27 03:17:18 thorpej Exp $");
     84 
     85 #include "opt_multiprocessor.h"
     86 #include "opt_lockdebug.h"
     87 #include "opt_ddb.h"
     88 
     89 #include <sys/param.h>
     90 #include <sys/lwp.h>
     91 #include <sys/proc.h>
     92 #include <sys/lock.h>
     93 #include <sys/systm.h>
     94 #include <machine/cpu.h>
     95 
     96 #if defined(LOCKDEBUG)
     97 #include <sys/syslog.h>
     98 /*
     99  * note that stdarg.h and the ansi style va_start macro is used for both
    100  * ansi and traditional c compiles.
    101  * XXX: this requires that stdarg.h define: va_alist and va_dcl
    102  */
    103 #include <machine/stdarg.h>
    104 
    105 void	lock_printf(const char *fmt, ...)
    106     __attribute__((__format__(__printf__,1,2)));
    107 
    108 int	lock_debug_syslog = 0;	/* defaults to printf, but can be patched */
    109 
    110 #ifdef DDB
    111 #include <ddb/ddbvar.h>
    112 #include <machine/db_machdep.h>
    113 #include <ddb/db_command.h>
    114 #include <ddb/db_interface.h>
    115 #endif
    116 #endif
    117 
    118 /*
    119  * Locking primitives implementation.
    120  * Locks provide shared/exclusive synchronization.
    121  */
    122 
    123 #if defined(LOCKDEBUG) || defined(DIAGNOSTIC) /* { */
    124 #if defined(MULTIPROCESSOR) /* { */
    125 #define	COUNT_CPU(cpu_id, x)						\
    126 	curcpu()->ci_spin_locks += (x)
    127 #else
    128 u_long	spin_locks;
    129 #define	COUNT_CPU(cpu_id, x)	spin_locks += (x)
    130 #endif /* MULTIPROCESSOR */ /* } */
    131 
    132 #define	COUNT(lkp, l, cpu_id, x)					\
    133 do {									\
    134 	if ((lkp)->lk_flags & LK_SPIN)					\
    135 		COUNT_CPU((cpu_id), (x));				\
    136 	else								\
    137 		(l)->l_locks += (x);					\
    138 } while (/*CONSTCOND*/0)
    139 #else
    140 #define COUNT(lkp, p, cpu_id, x)
    141 #define COUNT_CPU(cpu_id, x)
    142 #endif /* LOCKDEBUG || DIAGNOSTIC */ /* } */
    143 
    144 #ifndef SPINLOCK_SPIN_HOOK		/* from <machine/lock.h> */
    145 #define	SPINLOCK_SPIN_HOOK		/* nothing */
    146 #endif
    147 
    148 #define	INTERLOCK_ACQUIRE(lkp, flags, s)				\
    149 do {									\
    150 	if ((flags) & LK_SPIN)						\
    151 		s = splsched();						\
    152 	simple_lock(&(lkp)->lk_interlock);				\
    153 } while (0)
    154 
    155 #define	INTERLOCK_RELEASE(lkp, flags, s)				\
    156 do {									\
    157 	simple_unlock(&(lkp)->lk_interlock);				\
    158 	if ((flags) & LK_SPIN)						\
    159 		splx(s);						\
    160 } while (0)
    161 
    162 #if defined(LOCKDEBUG)
    163 #if defined(DDB)
    164 #define	SPINLOCK_SPINCHECK_DEBUGGER	Debugger()
    165 #else
    166 #define	SPINLOCK_SPINCHECK_DEBUGGER	/* nothing */
    167 #endif
    168 
    169 #define	SPINLOCK_SPINCHECK_DECL						\
    170 	/* 32-bits of count -- wrap constitutes a "spinout" */		\
    171 	uint32_t __spinc = 0
    172 
    173 #define	SPINLOCK_SPINCHECK						\
    174 do {									\
    175 	if (++__spinc == 0) {						\
    176 		printf("LK_SPIN spinout, excl %d, share %d\n",		\
    177 		    lkp->lk_exclusivecount, lkp->lk_sharecount);	\
    178 		if (lkp->lk_exclusivecount)				\
    179 			printf("held by CPU %lu\n",			\
    180 			    (u_long) lkp->lk_cpu);			\
    181 		if (lkp->lk_lock_file)					\
    182 			printf("last locked at %s:%d\n",		\
    183 			    lkp->lk_lock_file, lkp->lk_lock_line);	\
    184 		if (lkp->lk_unlock_file)				\
    185 			printf("last unlocked at %s:%d\n",		\
    186 			    lkp->lk_unlock_file, lkp->lk_unlock_line);	\
    187 		SPINLOCK_SPINCHECK_DEBUGGER;				\
    188 	}								\
    189 } while (0)
    190 #else
    191 #define	SPINLOCK_SPINCHECK_DECL			/* nothing */
    192 #define	SPINLOCK_SPINCHECK			/* nothing */
    193 #endif /* LOCKDEBUG && DDB */
    194 
    195 /*
    196  * Acquire a resource.
    197  */
    198 #define ACQUIRE(lkp, error, extflags, drain, wanted)			\
    199 	if ((extflags) & LK_SPIN) {					\
    200 		int interlocked;					\
    201 		SPINLOCK_SPINCHECK_DECL;				\
    202 									\
    203 		if ((drain) == 0)					\
    204 			(lkp)->lk_waitcount++;				\
    205 		for (interlocked = 1;;) {				\
    206 			SPINLOCK_SPINCHECK;				\
    207 			if (wanted) {					\
    208 				if (interlocked) {			\
    209 					INTERLOCK_RELEASE((lkp),	\
    210 					    LK_SPIN, s);		\
    211 					interlocked = 0;		\
    212 				}					\
    213 				SPINLOCK_SPIN_HOOK;			\
    214 			} else if (interlocked) {			\
    215 				break;					\
    216 			} else {					\
    217 				INTERLOCK_ACQUIRE((lkp), LK_SPIN, s);	\
    218 				interlocked = 1;			\
    219 			}						\
    220 		}							\
    221 		if ((drain) == 0)					\
    222 			(lkp)->lk_waitcount--;				\
    223 		KASSERT((wanted) == 0);					\
    224 		error = 0;	/* sanity */				\
    225 	} else {							\
    226 		for (error = 0; wanted; ) {				\
    227 			if ((drain))					\
    228 				(lkp)->lk_flags |= LK_WAITDRAIN;	\
    229 			else						\
    230 				(lkp)->lk_waitcount++;			\
    231 			/* XXX Cast away volatile. */			\
    232 			error = ltsleep((drain) ?			\
    233 			    (void *)&(lkp)->lk_flags :			\
    234 			    (void *)(lkp), (lkp)->lk_prio,		\
    235 			    (lkp)->lk_wmesg, (lkp)->lk_timo,		\
    236 			    &(lkp)->lk_interlock);			\
    237 			if ((drain) == 0)				\
    238 				(lkp)->lk_waitcount--;			\
    239 			if (error)					\
    240 				break;					\
    241 			if ((extflags) & LK_SLEEPFAIL) {		\
    242 				error = ENOLCK;				\
    243 				break;					\
    244 			}						\
    245 		}							\
    246 	}
    247 
    248 #define	SETHOLDER(lkp, pid, lid, cpu_id)				\
    249 do {									\
    250 	if ((lkp)->lk_flags & LK_SPIN)					\
    251 		(lkp)->lk_cpu = cpu_id;					\
    252 	else {								\
    253 		(lkp)->lk_lockholder = pid;				\
    254 		(lkp)->lk_locklwp = lid;				\
    255 	}								\
    256 } while (/*CONSTCOND*/0)
    257 
    258 #define	WEHOLDIT(lkp, pid, lid, cpu_id)					\
    259 	(((lkp)->lk_flags & LK_SPIN) != 0 ?				\
    260 	 ((lkp)->lk_cpu == (cpu_id)) :					\
    261 	 ((lkp)->lk_lockholder == (pid) && (lkp)->lk_locklwp == (lid)))
    262 
    263 #define	WAKEUP_WAITER(lkp)						\
    264 do {									\
    265 	if (((lkp)->lk_flags & LK_SPIN) == 0 && (lkp)->lk_waitcount) {	\
    266 		/* XXX Cast away volatile. */				\
    267 		wakeup((void *)(lkp));					\
    268 	}								\
    269 } while (/*CONSTCOND*/0)
    270 
    271 #if defined(LOCKDEBUG) /* { */
    272 #if defined(MULTIPROCESSOR) /* { */
    273 struct simplelock spinlock_list_slock = SIMPLELOCK_INITIALIZER;
    274 
    275 #define	SPINLOCK_LIST_LOCK()						\
    276 	__cpu_simple_lock(&spinlock_list_slock.lock_data)
    277 
    278 #define	SPINLOCK_LIST_UNLOCK()						\
    279 	__cpu_simple_unlock(&spinlock_list_slock.lock_data)
    280 #else
    281 #define	SPINLOCK_LIST_LOCK()	/* nothing */
    282 
    283 #define	SPINLOCK_LIST_UNLOCK()	/* nothing */
    284 #endif /* MULTIPROCESSOR */ /* } */
    285 
    286 TAILQ_HEAD(, lock) spinlock_list =
    287     TAILQ_HEAD_INITIALIZER(spinlock_list);
    288 
    289 #define	HAVEIT(lkp)							\
    290 do {									\
    291 	if ((lkp)->lk_flags & LK_SPIN) {				\
    292 		int s = spllock();					\
    293 		SPINLOCK_LIST_LOCK();					\
    294 		/* XXX Cast away volatile. */				\
    295 		TAILQ_INSERT_TAIL(&spinlock_list, (struct lock *)(lkp),	\
    296 		    lk_list);						\
    297 		SPINLOCK_LIST_UNLOCK();					\
    298 		splx(s);						\
    299 	}								\
    300 } while (/*CONSTCOND*/0)
    301 
    302 #define	DONTHAVEIT(lkp)							\
    303 do {									\
    304 	if ((lkp)->lk_flags & LK_SPIN) {				\
    305 		int s = spllock();					\
    306 		SPINLOCK_LIST_LOCK();					\
    307 		/* XXX Cast away volatile. */				\
    308 		TAILQ_REMOVE(&spinlock_list, (struct lock *)(lkp),	\
    309 		    lk_list);						\
    310 		SPINLOCK_LIST_UNLOCK();					\
    311 		splx(s);						\
    312 	}								\
    313 } while (/*CONSTCOND*/0)
    314 #else
    315 #define	HAVEIT(lkp)		/* nothing */
    316 
    317 #define	DONTHAVEIT(lkp)		/* nothing */
    318 #endif /* LOCKDEBUG */ /* } */
    319 
    320 #if defined(LOCKDEBUG)
    321 /*
    322  * Lock debug printing routine; can be configured to print to console
    323  * or log to syslog.
    324  */
    325 void
    326 lock_printf(const char *fmt, ...)
    327 {
    328 	va_list ap;
    329 
    330 	va_start(ap, fmt);
    331 	if (lock_debug_syslog)
    332 		vlog(LOG_DEBUG, fmt, ap);
    333 	else
    334 		vprintf(fmt, ap);
    335 	va_end(ap);
    336 }
    337 #endif /* LOCKDEBUG */
    338 
    339 /*
    340  * Initialize a lock; required before use.
    341  */
    342 void
    343 lockinit(struct lock *lkp, int prio, const char *wmesg, int timo, int flags)
    344 {
    345 
    346 	memset(lkp, 0, sizeof(struct lock));
    347 	simple_lock_init(&lkp->lk_interlock);
    348 	lkp->lk_flags = flags & LK_EXTFLG_MASK;
    349 	if (flags & LK_SPIN)
    350 		lkp->lk_cpu = LK_NOCPU;
    351 	else {
    352 		lkp->lk_lockholder = LK_NOPROC;
    353 		lkp->lk_prio = prio;
    354 		lkp->lk_timo = timo;
    355 	}
    356 	lkp->lk_wmesg = wmesg;	/* just a name for spin locks */
    357 #if defined(LOCKDEBUG)
    358 	lkp->lk_lock_file = NULL;
    359 	lkp->lk_unlock_file = NULL;
    360 #endif
    361 }
    362 
    363 /*
    364  * Determine the status of a lock.
    365  */
    366 int
    367 lockstatus(struct lock *lkp)
    368 {
    369 	int s, lock_type = 0;
    370 
    371 	INTERLOCK_ACQUIRE(lkp, lkp->lk_flags, s);
    372 	if (lkp->lk_exclusivecount != 0)
    373 		lock_type = LK_EXCLUSIVE;
    374 	else if (lkp->lk_sharecount != 0)
    375 		lock_type = LK_SHARED;
    376 	INTERLOCK_RELEASE(lkp, lkp->lk_flags, s);
    377 	return (lock_type);
    378 }
    379 
    380 #if defined(LOCKDEBUG) || defined(DIAGNOSTIC)
    381 /*
    382  * Make sure no spin locks are held by a CPU that is about
    383  * to context switch.
    384  */
    385 void
    386 spinlock_switchcheck(void)
    387 {
    388 	u_long cnt;
    389 	int s;
    390 
    391 	s = spllock();
    392 #if defined(MULTIPROCESSOR)
    393 	cnt = curcpu()->ci_spin_locks;
    394 #else
    395 	cnt = spin_locks;
    396 #endif
    397 	splx(s);
    398 
    399 	if (cnt != 0)
    400 		panic("spinlock_switchcheck: CPU %lu has %lu spin locks",
    401 		    (u_long) cpu_number(), cnt);
    402 }
    403 #endif /* LOCKDEBUG || DIAGNOSTIC */
    404 
    405 /*
    406  * Locks and IPLs (interrupt priority levels):
    407  *
    408  * Locks which may be taken from interrupt context must be handled
    409  * very carefully; you must spl to the highest IPL where the lock
    410  * is needed before acquiring the lock.
    411  *
    412  * It is also important to avoid deadlock, since certain (very high
    413  * priority) interrupts are often needed to keep the system as a whole
    414  * from deadlocking, and must not be blocked while you are spinning
    415  * waiting for a lower-priority lock.
    416  *
    417  * In addition, the lock-debugging hooks themselves need to use locks!
    418  *
    419  * A raw __cpu_simple_lock may be used from interrupts are long as it
    420  * is acquired and held at a single IPL.
    421  *
    422  * A simple_lock (which is a __cpu_simple_lock wrapped with some
    423  * debugging hooks) may be used at or below spllock(), which is
    424  * typically at or just below splhigh() (i.e. blocks everything
    425  * but certain machine-dependent extremely high priority interrupts).
    426  *
    427  * spinlockmgr spinlocks should be used at or below splsched().
    428  *
    429  * Some platforms may have interrupts of higher priority than splsched(),
    430  * including hard serial interrupts, inter-processor interrupts, and
    431  * kernel debugger traps.
    432  */
    433 
    434 /*
    435  * XXX XXX kludge around another kludge..
    436  *
    437  * vfs_shutdown() may be called from interrupt context, either as a result
    438  * of a panic, or from the debugger.   It proceeds to call
    439  * sys_sync(&proc0, ...), pretending its running on behalf of proc0
    440  *
    441  * We would like to make an attempt to sync the filesystems in this case, so
    442  * if this happens, we treat attempts to acquire locks specially.
    443  * All locks are acquired on behalf of proc0.
    444  *
    445  * If we've already paniced, we don't block waiting for locks, but
    446  * just barge right ahead since we're already going down in flames.
    447  */
    448 
    449 /*
    450  * Set, change, or release a lock.
    451  *
    452  * Shared requests increment the shared count. Exclusive requests set the
    453  * LK_WANT_EXCL flag (preventing further shared locks), and wait for already
    454  * accepted shared locks and shared-to-exclusive upgrades to go away.
    455  */
    456 int
    457 #if defined(LOCKDEBUG)
    458 _lockmgr(__volatile struct lock *lkp, u_int flags,
    459     struct simplelock *interlkp, const char *file, int line)
    460 #else
    461 lockmgr(__volatile struct lock *lkp, u_int flags,
    462     struct simplelock *interlkp)
    463 #endif
    464 {
    465 	int error;
    466 	pid_t pid;
    467 	lwpid_t lid;
    468 	int extflags;
    469 	cpuid_t cpu_id;
    470 	struct lwp *l = curproc;
    471 	int lock_shutdown_noblock = 0;
    472 	int s;
    473 
    474 	error = 0;
    475 
    476 	INTERLOCK_ACQUIRE(lkp, lkp->lk_flags, s);
    477 	if (flags & LK_INTERLOCK)
    478 		simple_unlock(interlkp);
    479 	extflags = (flags | lkp->lk_flags) & LK_EXTFLG_MASK;
    480 
    481 #ifdef DIAGNOSTIC /* { */
    482 	/*
    483 	 * Don't allow spins on sleep locks and don't allow sleeps
    484 	 * on spin locks.
    485 	 */
    486 	if ((flags ^ lkp->lk_flags) & LK_SPIN)
    487 		panic("lockmgr: sleep/spin mismatch\n");
    488 #endif /* } */
    489 
    490 	if (extflags & LK_SPIN) {
    491 		pid = LK_KERNPROC;
    492 		lid = 0;
    493 	} else {
    494 		if (l == NULL) {
    495 			if (!doing_shutdown) {
    496 #ifdef DIAGNOSTIC
    497 				panic("lockmgr: no context");
    498 #endif
    499 			} else {
    500 				l = &lwp0;
    501 				if (panicstr && (!(flags & LK_NOWAIT))) {
    502 					flags |= LK_NOWAIT;
    503 					lock_shutdown_noblock = 1;
    504 				}
    505 			}
    506 		}
    507 		lid = l->l_lid;
    508 		pid = l->l_proc->p_pid;
    509 	}
    510 	cpu_id = cpu_number();
    511 
    512 	/*
    513 	 * Once a lock has drained, the LK_DRAINING flag is set and an
    514 	 * exclusive lock is returned. The only valid operation thereafter
    515 	 * is a single release of that exclusive lock. This final release
    516 	 * clears the LK_DRAINING flag and sets the LK_DRAINED flag. Any
    517 	 * further requests of any sort will result in a panic. The bits
    518 	 * selected for these two flags are chosen so that they will be set
    519 	 * in memory that is freed (freed memory is filled with 0xdeadbeef).
    520 	 * The final release is permitted to give a new lease on life to
    521 	 * the lock by specifying LK_REENABLE.
    522 	 */
    523 	if (lkp->lk_flags & (LK_DRAINING|LK_DRAINED)) {
    524 #ifdef DIAGNOSTIC /* { */
    525 		if (lkp->lk_flags & LK_DRAINED)
    526 			panic("lockmgr: using decommissioned lock");
    527 		if ((flags & LK_TYPE_MASK) != LK_RELEASE ||
    528 		    WEHOLDIT(lkp, pid, lid, cpu_id) == 0)
    529 			panic("lockmgr: non-release on draining lock: %d\n",
    530 			    flags & LK_TYPE_MASK);
    531 #endif /* DIAGNOSTIC */ /* } */
    532 		lkp->lk_flags &= ~LK_DRAINING;
    533 		if ((flags & LK_REENABLE) == 0)
    534 			lkp->lk_flags |= LK_DRAINED;
    535 	}
    536 
    537 	switch (flags & LK_TYPE_MASK) {
    538 
    539 	case LK_SHARED:
    540 		if (WEHOLDIT(lkp, pid, lid, cpu_id) == 0) {
    541 			/*
    542 			 * If just polling, check to see if we will block.
    543 			 */
    544 			if ((extflags & LK_NOWAIT) && (lkp->lk_flags &
    545 			    (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE))) {
    546 				error = EBUSY;
    547 				break;
    548 			}
    549 			/*
    550 			 * Wait for exclusive locks and upgrades to clear.
    551 			 */
    552 			ACQUIRE(lkp, error, extflags, 0, lkp->lk_flags &
    553 			    (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE));
    554 			if (error)
    555 				break;
    556 			lkp->lk_sharecount++;
    557 			COUNT(lkp, l, cpu_id, 1);
    558 			break;
    559 		}
    560 		/*
    561 		 * We hold an exclusive lock, so downgrade it to shared.
    562 		 * An alternative would be to fail with EDEADLK.
    563 		 */
    564 		lkp->lk_sharecount++;
    565 		COUNT(lkp, l, cpu_id, 1);
    566 		/* fall into downgrade */
    567 
    568 	case LK_DOWNGRADE:
    569 		if (WEHOLDIT(lkp, pid, lid, cpu_id) == 0 ||
    570 		    lkp->lk_exclusivecount == 0)
    571 			panic("lockmgr: not holding exclusive lock");
    572 		lkp->lk_sharecount += lkp->lk_exclusivecount;
    573 		lkp->lk_exclusivecount = 0;
    574 		lkp->lk_recurselevel = 0;
    575 		lkp->lk_flags &= ~LK_HAVE_EXCL;
    576 		SETHOLDER(lkp, LK_NOPROC, 0, LK_NOCPU);
    577 #if defined(LOCKDEBUG)
    578 		lkp->lk_unlock_file = file;
    579 		lkp->lk_unlock_line = line;
    580 #endif
    581 		DONTHAVEIT(lkp);
    582 		WAKEUP_WAITER(lkp);
    583 		break;
    584 
    585 	case LK_EXCLUPGRADE:
    586 		/*
    587 		 * If another process is ahead of us to get an upgrade,
    588 		 * then we want to fail rather than have an intervening
    589 		 * exclusive access.
    590 		 */
    591 		if (lkp->lk_flags & LK_WANT_UPGRADE) {
    592 			lkp->lk_sharecount--;
    593 			COUNT(lkp, l, cpu_id, -1);
    594 			error = EBUSY;
    595 			break;
    596 		}
    597 		/* fall into normal upgrade */
    598 
    599 	case LK_UPGRADE:
    600 		/*
    601 		 * Upgrade a shared lock to an exclusive one. If another
    602 		 * shared lock has already requested an upgrade to an
    603 		 * exclusive lock, our shared lock is released and an
    604 		 * exclusive lock is requested (which will be granted
    605 		 * after the upgrade). If we return an error, the file
    606 		 * will always be unlocked.
    607 		 */
    608 		if (WEHOLDIT(lkp, pid, lid, cpu_id) || lkp->lk_sharecount <= 0)
    609 			panic("lockmgr: upgrade exclusive lock");
    610 		lkp->lk_sharecount--;
    611 		COUNT(lkp, l, cpu_id, -1);
    612 		/*
    613 		 * If we are just polling, check to see if we will block.
    614 		 */
    615 		if ((extflags & LK_NOWAIT) &&
    616 		    ((lkp->lk_flags & LK_WANT_UPGRADE) ||
    617 		     lkp->lk_sharecount > 1)) {
    618 			error = EBUSY;
    619 			break;
    620 		}
    621 		if ((lkp->lk_flags & LK_WANT_UPGRADE) == 0) {
    622 			/*
    623 			 * We are first shared lock to request an upgrade, so
    624 			 * request upgrade and wait for the shared count to
    625 			 * drop to zero, then take exclusive lock.
    626 			 */
    627 			lkp->lk_flags |= LK_WANT_UPGRADE;
    628 			ACQUIRE(lkp, error, extflags, 0, lkp->lk_sharecount);
    629 			lkp->lk_flags &= ~LK_WANT_UPGRADE;
    630 			if (error)
    631 				break;
    632 			lkp->lk_flags |= LK_HAVE_EXCL;
    633 			SETHOLDER(lkp, pid, lid, cpu_id);
    634 #if defined(LOCKDEBUG)
    635 			lkp->lk_lock_file = file;
    636 			lkp->lk_lock_line = line;
    637 #endif
    638 			HAVEIT(lkp);
    639 			if (lkp->lk_exclusivecount != 0)
    640 				panic("lockmgr: non-zero exclusive count");
    641 			lkp->lk_exclusivecount = 1;
    642 			if (extflags & LK_SETRECURSE)
    643 				lkp->lk_recurselevel = 1;
    644 			COUNT(lkp, l, cpu_id, 1);
    645 			break;
    646 		}
    647 		/*
    648 		 * Someone else has requested upgrade. Release our shared
    649 		 * lock, awaken upgrade requestor if we are the last shared
    650 		 * lock, then request an exclusive lock.
    651 		 */
    652 		if (lkp->lk_sharecount == 0)
    653 			WAKEUP_WAITER(lkp);
    654 		/* fall into exclusive request */
    655 
    656 	case LK_EXCLUSIVE:
    657 		if (WEHOLDIT(lkp, pid, lid, cpu_id)) {
    658 			/*
    659 			 * Recursive lock.
    660 			 */
    661 			if ((extflags & LK_CANRECURSE) == 0 &&
    662 			     lkp->lk_recurselevel == 0) {
    663 				if (extflags & LK_RECURSEFAIL) {
    664 					error = EDEADLK;
    665 					break;
    666 				} else
    667 					panic("lockmgr: locking against myself");
    668 			}
    669 			lkp->lk_exclusivecount++;
    670 			if (extflags & LK_SETRECURSE &&
    671 			    lkp->lk_recurselevel == 0)
    672 				lkp->lk_recurselevel = lkp->lk_exclusivecount;
    673 			COUNT(lkp, l, cpu_id, 1);
    674 			break;
    675 		}
    676 		/*
    677 		 * If we are just polling, check to see if we will sleep.
    678 		 */
    679 		if ((extflags & LK_NOWAIT) && ((lkp->lk_flags &
    680 		     (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
    681 		     lkp->lk_sharecount != 0)) {
    682 			error = EBUSY;
    683 			break;
    684 		}
    685 		/*
    686 		 * Try to acquire the want_exclusive flag.
    687 		 */
    688 		ACQUIRE(lkp, error, extflags, 0, lkp->lk_flags &
    689 		    (LK_HAVE_EXCL | LK_WANT_EXCL));
    690 		if (error)
    691 			break;
    692 		lkp->lk_flags |= LK_WANT_EXCL;
    693 		/*
    694 		 * Wait for shared locks and upgrades to finish.
    695 		 */
    696 		ACQUIRE(lkp, error, extflags, 0, lkp->lk_sharecount != 0 ||
    697 		       (lkp->lk_flags & LK_WANT_UPGRADE));
    698 		lkp->lk_flags &= ~LK_WANT_EXCL;
    699 		if (error)
    700 			break;
    701 		lkp->lk_flags |= LK_HAVE_EXCL;
    702 		SETHOLDER(lkp, pid, lid, cpu_id);
    703 #if defined(LOCKDEBUG)
    704 		lkp->lk_lock_file = file;
    705 		lkp->lk_lock_line = line;
    706 #endif
    707 		HAVEIT(lkp);
    708 		if (lkp->lk_exclusivecount != 0)
    709 			panic("lockmgr: non-zero exclusive count");
    710 		lkp->lk_exclusivecount = 1;
    711 		if (extflags & LK_SETRECURSE)
    712 			lkp->lk_recurselevel = 1;
    713 		COUNT(lkp, l, cpu_id, 1);
    714 		break;
    715 
    716 	case LK_RELEASE:
    717 		if (lkp->lk_exclusivecount != 0) {
    718 			if (WEHOLDIT(lkp, pid, lid, cpu_id) == 0) {
    719 				if (lkp->lk_flags & LK_SPIN) {
    720 					panic("lockmgr: processor %lu, not "
    721 					    "exclusive lock holder %lu "
    722 					    "unlocking", cpu_id, lkp->lk_cpu);
    723 				} else {
    724 					panic("lockmgr: pid %d, not "
    725 					    "exclusive lock holder %d "
    726 					    "unlocking", pid,
    727 					    lkp->lk_lockholder);
    728 				}
    729 			}
    730 			if (lkp->lk_exclusivecount == lkp->lk_recurselevel)
    731 				lkp->lk_recurselevel = 0;
    732 			lkp->lk_exclusivecount--;
    733 			COUNT(lkp, l, cpu_id, -1);
    734 			if (lkp->lk_exclusivecount == 0) {
    735 				lkp->lk_flags &= ~LK_HAVE_EXCL;
    736 				SETHOLDER(lkp, LK_NOPROC, 0, LK_NOCPU);
    737 #if defined(LOCKDEBUG)
    738 				lkp->lk_unlock_file = file;
    739 				lkp->lk_unlock_line = line;
    740 #endif
    741 				DONTHAVEIT(lkp);
    742 			}
    743 		} else if (lkp->lk_sharecount != 0) {
    744 			lkp->lk_sharecount--;
    745 			COUNT(lkp, l, cpu_id, -1);
    746 		}
    747 #ifdef DIAGNOSTIC
    748 		else
    749 			panic("lockmgr: release of unlocked lock!");
    750 #endif
    751 		WAKEUP_WAITER(lkp);
    752 		break;
    753 
    754 	case LK_DRAIN:
    755 		/*
    756 		 * Check that we do not already hold the lock, as it can
    757 		 * never drain if we do. Unfortunately, we have no way to
    758 		 * check for holding a shared lock, but at least we can
    759 		 * check for an exclusive one.
    760 		 */
    761 		if (WEHOLDIT(lkp, pid, lid, cpu_id))
    762 			panic("lockmgr: draining against myself");
    763 		/*
    764 		 * If we are just polling, check to see if we will sleep.
    765 		 */
    766 		if ((extflags & LK_NOWAIT) && ((lkp->lk_flags &
    767 		     (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
    768 		     lkp->lk_sharecount != 0 || lkp->lk_waitcount != 0)) {
    769 			error = EBUSY;
    770 			break;
    771 		}
    772 		ACQUIRE(lkp, error, extflags, 1,
    773 		    ((lkp->lk_flags &
    774 		     (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
    775 		     lkp->lk_sharecount != 0 ||
    776 		     lkp->lk_waitcount != 0));
    777 		if (error)
    778 			break;
    779 		lkp->lk_flags |= LK_DRAINING | LK_HAVE_EXCL;
    780 		SETHOLDER(lkp, pid, lid, cpu_id);
    781 #if defined(LOCKDEBUG)
    782 		lkp->lk_lock_file = file;
    783 		lkp->lk_lock_line = line;
    784 #endif
    785 		HAVEIT(lkp);
    786 		lkp->lk_exclusivecount = 1;
    787 		/* XXX unlikely that we'd want this */
    788 		if (extflags & LK_SETRECURSE)
    789 			lkp->lk_recurselevel = 1;
    790 		COUNT(lkp, l, cpu_id, 1);
    791 		break;
    792 
    793 	default:
    794 		INTERLOCK_RELEASE(lkp, lkp->lk_flags, s);
    795 		panic("lockmgr: unknown locktype request %d",
    796 		    flags & LK_TYPE_MASK);
    797 		/* NOTREACHED */
    798 	}
    799 	if ((lkp->lk_flags & (LK_WAITDRAIN|LK_SPIN)) == LK_WAITDRAIN &&
    800 	    ((lkp->lk_flags &
    801 	      (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) == 0 &&
    802 	     lkp->lk_sharecount == 0 && lkp->lk_waitcount == 0)) {
    803 		lkp->lk_flags &= ~LK_WAITDRAIN;
    804 		wakeup((void *)&lkp->lk_flags);
    805 	}
    806 	/*
    807 	 * Note that this panic will be a recursive panic, since
    808 	 * we only set lock_shutdown_noblock above if panicstr != NULL.
    809 	 */
    810 	if (error && lock_shutdown_noblock)
    811 		panic("lockmgr: deadlock (see previous panic)");
    812 
    813 	INTERLOCK_RELEASE(lkp, lkp->lk_flags, s);
    814 	return (error);
    815 }
    816 
    817 /*
    818  * For a recursive spinlock held one or more times by the current CPU,
    819  * release all N locks, and return N.
    820  * Intended for use in mi_switch() shortly before context switching.
    821  */
    822 
    823 int
    824 #if defined(LOCKDEBUG)
    825 _spinlock_release_all(__volatile struct lock *lkp, const char *file, int line)
    826 #else
    827 spinlock_release_all(__volatile struct lock *lkp)
    828 #endif
    829 {
    830 	int s, count;
    831 	cpuid_t cpu_id;
    832 
    833 	KASSERT(lkp->lk_flags & LK_SPIN);
    834 
    835 	INTERLOCK_ACQUIRE(lkp, LK_SPIN, s);
    836 
    837 	cpu_id = cpu_number();
    838 	count = lkp->lk_exclusivecount;
    839 
    840 	if (count != 0) {
    841 #ifdef DIAGNOSTIC
    842 		if (WEHOLDIT(lkp, 0, 0, cpu_id) == 0) {
    843 			panic("spinlock_release_all: processor %lu, not "
    844 			    "exclusive lock holder %lu "
    845 			    "unlocking", (long)cpu_id, lkp->lk_cpu);
    846 		}
    847 #endif
    848 		lkp->lk_recurselevel = 0;
    849 		lkp->lk_exclusivecount = 0;
    850 		COUNT_CPU(cpu_id, -count);
    851 		lkp->lk_flags &= ~LK_HAVE_EXCL;
    852 		SETHOLDER(lkp, LK_NOPROC, 0, LK_NOCPU);
    853 #if defined(LOCKDEBUG)
    854 		lkp->lk_unlock_file = file;
    855 		lkp->lk_unlock_line = line;
    856 #endif
    857 		DONTHAVEIT(lkp);
    858 	}
    859 #ifdef DIAGNOSTIC
    860 	else if (lkp->lk_sharecount != 0)
    861 		panic("spinlock_release_all: release of shared lock!");
    862 	else
    863 		panic("spinlock_release_all: release of unlocked lock!");
    864 #endif
    865 	INTERLOCK_RELEASE(lkp, LK_SPIN, s);
    866 
    867 	return (count);
    868 }
    869 
    870 /*
    871  * For a recursive spinlock held one or more times by the current CPU,
    872  * release all N locks, and return N.
    873  * Intended for use in mi_switch() right after resuming execution.
    874  */
    875 
    876 void
    877 #if defined(LOCKDEBUG)
    878 _spinlock_acquire_count(__volatile struct lock *lkp, int count,
    879     const char *file, int line)
    880 #else
    881 spinlock_acquire_count(__volatile struct lock *lkp, int count)
    882 #endif
    883 {
    884 	int s, error;
    885 	cpuid_t cpu_id;
    886 
    887 	KASSERT(lkp->lk_flags & LK_SPIN);
    888 
    889 	INTERLOCK_ACQUIRE(lkp, LK_SPIN, s);
    890 
    891 	cpu_id = cpu_number();
    892 
    893 #ifdef DIAGNOSTIC
    894 	if (WEHOLDIT(lkp, LK_NOPROC, 0, cpu_id))
    895 		panic("spinlock_acquire_count: processor %lu already holds lock\n", (long)cpu_id);
    896 #endif
    897 	/*
    898 	 * Try to acquire the want_exclusive flag.
    899 	 */
    900 	ACQUIRE(lkp, error, LK_SPIN, 0, lkp->lk_flags &
    901 	    (LK_HAVE_EXCL | LK_WANT_EXCL));
    902 	lkp->lk_flags |= LK_WANT_EXCL;
    903 	/*
    904 	 * Wait for shared locks and upgrades to finish.
    905 	 */
    906 	ACQUIRE(lkp, error, LK_SPIN, 0, lkp->lk_sharecount != 0 ||
    907 	    (lkp->lk_flags & LK_WANT_UPGRADE));
    908 	lkp->lk_flags &= ~LK_WANT_EXCL;
    909 	lkp->lk_flags |= LK_HAVE_EXCL;
    910 	SETHOLDER(lkp, LK_NOPROC, 0, cpu_id);
    911 #if defined(LOCKDEBUG)
    912 	lkp->lk_lock_file = file;
    913 	lkp->lk_lock_line = line;
    914 #endif
    915 	HAVEIT(lkp);
    916 	if (lkp->lk_exclusivecount != 0)
    917 		panic("lockmgr: non-zero exclusive count");
    918 	lkp->lk_exclusivecount = count;
    919 	lkp->lk_recurselevel = 1;
    920 	COUNT_CPU(cpu_id, count);
    921 
    922 	INTERLOCK_RELEASE(lkp, lkp->lk_flags, s);
    923 }
    924 
    925 
    926 
    927 /*
    928  * Print out information about state of a lock. Used by VOP_PRINT
    929  * routines to display ststus about contained locks.
    930  */
    931 void
    932 lockmgr_printinfo(__volatile struct lock *lkp)
    933 {
    934 
    935 	if (lkp->lk_sharecount)
    936 		printf(" lock type %s: SHARED (count %d)", lkp->lk_wmesg,
    937 		    lkp->lk_sharecount);
    938 	else if (lkp->lk_flags & LK_HAVE_EXCL) {
    939 		printf(" lock type %s: EXCL (count %d) by ",
    940 		    lkp->lk_wmesg, lkp->lk_exclusivecount);
    941 		if (lkp->lk_flags & LK_SPIN)
    942 			printf("processor %lu", lkp->lk_cpu);
    943 		else
    944 			printf("pid %d.%d", lkp->lk_lockholder,
    945 			    lkp->lk_locklwp);
    946 	} else
    947 		printf(" not locked");
    948 	if ((lkp->lk_flags & LK_SPIN) == 0 && lkp->lk_waitcount > 0)
    949 		printf(" with %d pending", lkp->lk_waitcount);
    950 }
    951 
    952 #if defined(LOCKDEBUG) /* { */
    953 TAILQ_HEAD(, simplelock) simplelock_list =
    954     TAILQ_HEAD_INITIALIZER(simplelock_list);
    955 
    956 #if defined(MULTIPROCESSOR) /* { */
    957 struct simplelock simplelock_list_slock = SIMPLELOCK_INITIALIZER;
    958 
    959 #define	SLOCK_LIST_LOCK()						\
    960 	__cpu_simple_lock(&simplelock_list_slock.lock_data)
    961 
    962 #define	SLOCK_LIST_UNLOCK()						\
    963 	__cpu_simple_unlock(&simplelock_list_slock.lock_data)
    964 
    965 #define	SLOCK_COUNT(x)							\
    966 	curcpu()->ci_simple_locks += (x)
    967 #else
    968 u_long simple_locks;
    969 
    970 #define	SLOCK_LIST_LOCK()	/* nothing */
    971 
    972 #define	SLOCK_LIST_UNLOCK()	/* nothing */
    973 
    974 #define	SLOCK_COUNT(x)		simple_locks += (x)
    975 #endif /* MULTIPROCESSOR */ /* } */
    976 
    977 #ifdef DDB /* { */
    978 #ifdef MULTIPROCESSOR
    979 int simple_lock_debugger = 1;	/* more serious on MP */
    980 #else
    981 int simple_lock_debugger = 0;
    982 #endif
    983 #define	SLOCK_DEBUGGER()	if (simple_lock_debugger) Debugger()
    984 #define	SLOCK_TRACE()							\
    985 	db_stack_trace_print((db_expr_t)__builtin_frame_address(0),	\
    986 	    TRUE, 65535, "", printf);
    987 #else
    988 #define	SLOCK_DEBUGGER()	/* nothing */
    989 #define	SLOCK_TRACE()		/* nothing */
    990 #endif /* } */
    991 
    992 #ifdef MULTIPROCESSOR
    993 #define SLOCK_MP()		lock_printf("on cpu %ld\n", 		\
    994 				    (u_long) cpu_number())
    995 #else
    996 #define SLOCK_MP()		/* nothing */
    997 #endif
    998 
    999 #define	SLOCK_WHERE(str, alp, id, l)					\
   1000 do {									\
   1001 	lock_printf("\n");						\
   1002 	lock_printf(str);						\
   1003 	lock_printf("lock: %p, currently at: %s:%d\n", (alp), (id), (l)); \
   1004 	SLOCK_MP();							\
   1005 	if ((alp)->lock_file != NULL)					\
   1006 		lock_printf("last locked: %s:%d\n", (alp)->lock_file,	\
   1007 		    (alp)->lock_line);					\
   1008 	if ((alp)->unlock_file != NULL)					\
   1009 		lock_printf("last unlocked: %s:%d\n", (alp)->unlock_file, \
   1010 		    (alp)->unlock_line);				\
   1011 	SLOCK_TRACE()							\
   1012 	SLOCK_DEBUGGER();						\
   1013 } while (/*CONSTCOND*/0)
   1014 
   1015 /*
   1016  * Simple lock functions so that the debugger can see from whence
   1017  * they are being called.
   1018  */
   1019 void
   1020 simple_lock_init(struct simplelock *alp)
   1021 {
   1022 
   1023 #if defined(MULTIPROCESSOR) /* { */
   1024 	__cpu_simple_lock_init(&alp->lock_data);
   1025 #else
   1026 	alp->lock_data = __SIMPLELOCK_UNLOCKED;
   1027 #endif /* } */
   1028 	alp->lock_file = NULL;
   1029 	alp->lock_line = 0;
   1030 	alp->unlock_file = NULL;
   1031 	alp->unlock_line = 0;
   1032 	alp->lock_holder = LK_NOCPU;
   1033 }
   1034 
   1035 void
   1036 _simple_lock(__volatile struct simplelock *alp, const char *id, int l)
   1037 {
   1038 	cpuid_t cpu_id = cpu_number();
   1039 	int s;
   1040 
   1041 	s = spllock();
   1042 
   1043 	/*
   1044 	 * MULTIPROCESSOR case: This is `safe' since if it's not us, we
   1045 	 * don't take any action, and just fall into the normal spin case.
   1046 	 */
   1047 	if (alp->lock_data == __SIMPLELOCK_LOCKED) {
   1048 #if defined(MULTIPROCESSOR) /* { */
   1049 		if (alp->lock_holder == cpu_id) {
   1050 			SLOCK_WHERE("simple_lock: locking against myself\n",
   1051 			    alp, id, l);
   1052 			goto out;
   1053 		}
   1054 #else
   1055 		SLOCK_WHERE("simple_lock: lock held\n", alp, id, l);
   1056 		goto out;
   1057 #endif /* MULTIPROCESSOR */ /* } */
   1058 	}
   1059 
   1060 #if defined(MULTIPROCESSOR) /* { */
   1061 	/* Acquire the lock before modifying any fields. */
   1062 	__cpu_simple_lock(&alp->lock_data);
   1063 #else
   1064 	alp->lock_data = __SIMPLELOCK_LOCKED;
   1065 #endif /* } */
   1066 
   1067 	if (alp->lock_holder != LK_NOCPU) {
   1068 		SLOCK_WHERE("simple_lock: uninitialized lock\n",
   1069 		    alp, id, l);
   1070 	}
   1071 	alp->lock_file = id;
   1072 	alp->lock_line = l;
   1073 	alp->lock_holder = cpu_id;
   1074 
   1075 	SLOCK_LIST_LOCK();
   1076 	/* XXX Cast away volatile */
   1077 	TAILQ_INSERT_TAIL(&simplelock_list, (struct simplelock *)alp, list);
   1078 	SLOCK_LIST_UNLOCK();
   1079 
   1080 	SLOCK_COUNT(1);
   1081 
   1082  out:
   1083 	splx(s);
   1084 }
   1085 
   1086 int
   1087 _simple_lock_held(__volatile struct simplelock *alp)
   1088 {
   1089 #if defined(MULTIPROCESSOR) || defined(DIAGNOSTIC)
   1090 	cpuid_t cpu_id = cpu_number();
   1091 #endif
   1092 	int s, locked = 0;
   1093 
   1094 	s = spllock();
   1095 
   1096 #if defined(MULTIPROCESSOR)
   1097 	if (__cpu_simple_lock_try(&alp->lock_data) == 0)
   1098 		locked = (alp->lock_holder == cpu_id);
   1099 	else
   1100 		__cpu_simple_unlock(&alp->lock_data);
   1101 #else
   1102 	if (alp->lock_data == __SIMPLELOCK_LOCKED) {
   1103 		locked = 1;
   1104 		KASSERT(alp->lock_holder == cpu_id);
   1105 	}
   1106 #endif
   1107 
   1108 	splx(s);
   1109 
   1110 	return (locked);
   1111 }
   1112 
   1113 int
   1114 _simple_lock_try(__volatile struct simplelock *alp, const char *id, int l)
   1115 {
   1116 	cpuid_t cpu_id = cpu_number();
   1117 	int s, rv = 0;
   1118 
   1119 	s = spllock();
   1120 
   1121 	/*
   1122 	 * MULTIPROCESSOR case: This is `safe' since if it's not us, we
   1123 	 * don't take any action.
   1124 	 */
   1125 #if defined(MULTIPROCESSOR) /* { */
   1126 	if ((rv = __cpu_simple_lock_try(&alp->lock_data)) == 0) {
   1127 		if (alp->lock_holder == cpu_id)
   1128 			SLOCK_WHERE("simple_lock_try: locking against myself\n",
   1129 			    alp, id, l);
   1130 		goto out;
   1131 	}
   1132 #else
   1133 	if (alp->lock_data == __SIMPLELOCK_LOCKED) {
   1134 		SLOCK_WHERE("simple_lock_try: lock held\n", alp, id, l);
   1135 		goto out;
   1136 	}
   1137 	alp->lock_data = __SIMPLELOCK_LOCKED;
   1138 #endif /* MULTIPROCESSOR */ /* } */
   1139 
   1140 	/*
   1141 	 * At this point, we have acquired the lock.
   1142 	 */
   1143 
   1144 	rv = 1;
   1145 
   1146 	alp->lock_file = id;
   1147 	alp->lock_line = l;
   1148 	alp->lock_holder = cpu_id;
   1149 
   1150 	SLOCK_LIST_LOCK();
   1151 	/* XXX Cast away volatile. */
   1152 	TAILQ_INSERT_TAIL(&simplelock_list, (struct simplelock *)alp, list);
   1153 	SLOCK_LIST_UNLOCK();
   1154 
   1155 	SLOCK_COUNT(1);
   1156 
   1157  out:
   1158 	splx(s);
   1159 	return (rv);
   1160 }
   1161 
   1162 void
   1163 _simple_unlock(__volatile struct simplelock *alp, const char *id, int l)
   1164 {
   1165 	int s;
   1166 
   1167 	s = spllock();
   1168 
   1169 	/*
   1170 	 * MULTIPROCESSOR case: This is `safe' because we think we hold
   1171 	 * the lock, and if we don't, we don't take any action.
   1172 	 */
   1173 	if (alp->lock_data == __SIMPLELOCK_UNLOCKED) {
   1174 		SLOCK_WHERE("simple_unlock: lock not held\n",
   1175 		    alp, id, l);
   1176 		goto out;
   1177 	}
   1178 
   1179 	SLOCK_LIST_LOCK();
   1180 	TAILQ_REMOVE(&simplelock_list, alp, list);
   1181 	SLOCK_LIST_UNLOCK();
   1182 
   1183 	SLOCK_COUNT(-1);
   1184 
   1185 	alp->list.tqe_next = NULL;	/* sanity */
   1186 	alp->list.tqe_prev = NULL;	/* sanity */
   1187 
   1188 	alp->unlock_file = id;
   1189 	alp->unlock_line = l;
   1190 
   1191 #if defined(MULTIPROCESSOR) /* { */
   1192 	alp->lock_holder = LK_NOCPU;
   1193 	/* Now that we've modified all fields, release the lock. */
   1194 	__cpu_simple_unlock(&alp->lock_data);
   1195 #else
   1196 	alp->lock_data = __SIMPLELOCK_UNLOCKED;
   1197 	KASSERT(alp->lock_holder == cpu_number());
   1198 	alp->lock_holder = LK_NOCPU;
   1199 #endif /* } */
   1200 
   1201  out:
   1202 	splx(s);
   1203 }
   1204 
   1205 void
   1206 simple_lock_dump(void)
   1207 {
   1208 	struct simplelock *alp;
   1209 	int s;
   1210 
   1211 	s = spllock();
   1212 	SLOCK_LIST_LOCK();
   1213 	lock_printf("all simple locks:\n");
   1214 	TAILQ_FOREACH(alp, &simplelock_list, list) {
   1215 		lock_printf("%p CPU %lu %s:%d\n", alp, alp->lock_holder,
   1216 		    alp->lock_file, alp->lock_line);
   1217 	}
   1218 	SLOCK_LIST_UNLOCK();
   1219 	splx(s);
   1220 }
   1221 
   1222 void
   1223 simple_lock_freecheck(void *start, void *end)
   1224 {
   1225 	struct simplelock *alp;
   1226 	int s;
   1227 
   1228 	s = spllock();
   1229 	SLOCK_LIST_LOCK();
   1230 	TAILQ_FOREACH(alp, &simplelock_list, list) {
   1231 		if ((void *)alp >= start && (void *)alp < end) {
   1232 			lock_printf("freeing simple_lock %p CPU %lu %s:%d\n",
   1233 			    alp, alp->lock_holder, alp->lock_file,
   1234 			    alp->lock_line);
   1235 			SLOCK_DEBUGGER();
   1236 		}
   1237 	}
   1238 	SLOCK_LIST_UNLOCK();
   1239 	splx(s);
   1240 }
   1241 
   1242 /*
   1243  * We must be holding exactly one lock: the sched_lock.
   1244  */
   1245 
   1246 void
   1247 simple_lock_switchcheck(void)
   1248 {
   1249 
   1250 	simple_lock_only_held(&sched_lock, "switching");
   1251 }
   1252 
   1253 void
   1254 simple_lock_only_held(volatile struct simplelock *lp, const char *where)
   1255 {
   1256 	struct simplelock *alp;
   1257 	cpuid_t cpu_id = cpu_number();
   1258 	int s;
   1259 
   1260 	if (lp) {
   1261 		LOCK_ASSERT(simple_lock_held(lp));
   1262 	}
   1263 	s = spllock();
   1264 	SLOCK_LIST_LOCK();
   1265 	TAILQ_FOREACH(alp, &simplelock_list, list) {
   1266 		if (alp == lp)
   1267 			continue;
   1268 		if (alp->lock_holder == cpu_id)
   1269 			break;
   1270 	}
   1271 	SLOCK_LIST_UNLOCK();
   1272 	splx(s);
   1273 
   1274 	if (alp != NULL) {
   1275 		lock_printf("\n%s with held simple_lock %p "
   1276 		    "CPU %lu %s:%d\n",
   1277 		    where, alp, alp->lock_holder, alp->lock_file,
   1278 		    alp->lock_line);
   1279 		SLOCK_TRACE();
   1280 		SLOCK_DEBUGGER();
   1281 	}
   1282 }
   1283 #endif /* LOCKDEBUG */ /* } */
   1284