Home | History | Annotate | Line # | Download | only in kern
kern_lock.c revision 1.51.2.9
      1 /*	$NetBSD: kern_lock.c,v 1.51.2.9 2002/06/24 22:10:45 nathanw 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.9 2002/06/24 22:10:45 nathanw 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 = curlwp;
    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 				panic("lockmgr: no context");
    497 			} else {
    498 				l = &lwp0;
    499 				if (panicstr && (!(flags & LK_NOWAIT))) {
    500 					flags |= LK_NOWAIT;
    501 					lock_shutdown_noblock = 1;
    502 				}
    503 			}
    504 		}
    505 		lid = l->l_lid;
    506 		pid = l->l_proc->p_pid;
    507 	}
    508 	cpu_id = cpu_number();
    509 
    510 	/*
    511 	 * Once a lock has drained, the LK_DRAINING flag is set and an
    512 	 * exclusive lock is returned. The only valid operation thereafter
    513 	 * is a single release of that exclusive lock. This final release
    514 	 * clears the LK_DRAINING flag and sets the LK_DRAINED flag. Any
    515 	 * further requests of any sort will result in a panic. The bits
    516 	 * selected for these two flags are chosen so that they will be set
    517 	 * in memory that is freed (freed memory is filled with 0xdeadbeef).
    518 	 * The final release is permitted to give a new lease on life to
    519 	 * the lock by specifying LK_REENABLE.
    520 	 */
    521 	if (lkp->lk_flags & (LK_DRAINING|LK_DRAINED)) {
    522 #ifdef DIAGNOSTIC /* { */
    523 		if (lkp->lk_flags & LK_DRAINED)
    524 			panic("lockmgr: using decommissioned lock");
    525 		if ((flags & LK_TYPE_MASK) != LK_RELEASE ||
    526 		    WEHOLDIT(lkp, pid, lid, cpu_id) == 0)
    527 			panic("lockmgr: non-release on draining lock: %d\n",
    528 			    flags & LK_TYPE_MASK);
    529 #endif /* DIAGNOSTIC */ /* } */
    530 		lkp->lk_flags &= ~LK_DRAINING;
    531 		if ((flags & LK_REENABLE) == 0)
    532 			lkp->lk_flags |= LK_DRAINED;
    533 	}
    534 
    535 	switch (flags & LK_TYPE_MASK) {
    536 
    537 	case LK_SHARED:
    538 		if (WEHOLDIT(lkp, pid, lid, cpu_id) == 0) {
    539 			/*
    540 			 * If just polling, check to see if we will block.
    541 			 */
    542 			if ((extflags & LK_NOWAIT) && (lkp->lk_flags &
    543 			    (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE))) {
    544 				error = EBUSY;
    545 				break;
    546 			}
    547 			/*
    548 			 * Wait for exclusive locks and upgrades to clear.
    549 			 */
    550 			ACQUIRE(lkp, error, extflags, 0, lkp->lk_flags &
    551 			    (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE));
    552 			if (error)
    553 				break;
    554 			lkp->lk_sharecount++;
    555 			COUNT(lkp, l, cpu_id, 1);
    556 			break;
    557 		}
    558 		/*
    559 		 * We hold an exclusive lock, so downgrade it to shared.
    560 		 * An alternative would be to fail with EDEADLK.
    561 		 */
    562 		lkp->lk_sharecount++;
    563 		COUNT(lkp, l, cpu_id, 1);
    564 		/* fall into downgrade */
    565 
    566 	case LK_DOWNGRADE:
    567 		if (WEHOLDIT(lkp, pid, lid, cpu_id) == 0 ||
    568 		    lkp->lk_exclusivecount == 0)
    569 			panic("lockmgr: not holding exclusive lock");
    570 		lkp->lk_sharecount += lkp->lk_exclusivecount;
    571 		lkp->lk_exclusivecount = 0;
    572 		lkp->lk_recurselevel = 0;
    573 		lkp->lk_flags &= ~LK_HAVE_EXCL;
    574 		SETHOLDER(lkp, LK_NOPROC, 0, LK_NOCPU);
    575 #if defined(LOCKDEBUG)
    576 		lkp->lk_unlock_file = file;
    577 		lkp->lk_unlock_line = line;
    578 #endif
    579 		DONTHAVEIT(lkp);
    580 		WAKEUP_WAITER(lkp);
    581 		break;
    582 
    583 	case LK_EXCLUPGRADE:
    584 		/*
    585 		 * If another process is ahead of us to get an upgrade,
    586 		 * then we want to fail rather than have an intervening
    587 		 * exclusive access.
    588 		 */
    589 		if (lkp->lk_flags & LK_WANT_UPGRADE) {
    590 			lkp->lk_sharecount--;
    591 			COUNT(lkp, l, cpu_id, -1);
    592 			error = EBUSY;
    593 			break;
    594 		}
    595 		/* fall into normal upgrade */
    596 
    597 	case LK_UPGRADE:
    598 		/*
    599 		 * Upgrade a shared lock to an exclusive one. If another
    600 		 * shared lock has already requested an upgrade to an
    601 		 * exclusive lock, our shared lock is released and an
    602 		 * exclusive lock is requested (which will be granted
    603 		 * after the upgrade). If we return an error, the file
    604 		 * will always be unlocked.
    605 		 */
    606 		if (WEHOLDIT(lkp, pid, lid, cpu_id) || lkp->lk_sharecount <= 0)
    607 			panic("lockmgr: upgrade exclusive lock");
    608 		lkp->lk_sharecount--;
    609 		COUNT(lkp, l, cpu_id, -1);
    610 		/*
    611 		 * If we are just polling, check to see if we will block.
    612 		 */
    613 		if ((extflags & LK_NOWAIT) &&
    614 		    ((lkp->lk_flags & LK_WANT_UPGRADE) ||
    615 		     lkp->lk_sharecount > 1)) {
    616 			error = EBUSY;
    617 			break;
    618 		}
    619 		if ((lkp->lk_flags & LK_WANT_UPGRADE) == 0) {
    620 			/*
    621 			 * We are first shared lock to request an upgrade, so
    622 			 * request upgrade and wait for the shared count to
    623 			 * drop to zero, then take exclusive lock.
    624 			 */
    625 			lkp->lk_flags |= LK_WANT_UPGRADE;
    626 			ACQUIRE(lkp, error, extflags, 0, lkp->lk_sharecount);
    627 			lkp->lk_flags &= ~LK_WANT_UPGRADE;
    628 			if (error)
    629 				break;
    630 			lkp->lk_flags |= LK_HAVE_EXCL;
    631 			SETHOLDER(lkp, pid, lid, cpu_id);
    632 #if defined(LOCKDEBUG)
    633 			lkp->lk_lock_file = file;
    634 			lkp->lk_lock_line = line;
    635 #endif
    636 			HAVEIT(lkp);
    637 			if (lkp->lk_exclusivecount != 0)
    638 				panic("lockmgr: non-zero exclusive count");
    639 			lkp->lk_exclusivecount = 1;
    640 			if (extflags & LK_SETRECURSE)
    641 				lkp->lk_recurselevel = 1;
    642 			COUNT(lkp, l, cpu_id, 1);
    643 			break;
    644 		}
    645 		/*
    646 		 * Someone else has requested upgrade. Release our shared
    647 		 * lock, awaken upgrade requestor if we are the last shared
    648 		 * lock, then request an exclusive lock.
    649 		 */
    650 		if (lkp->lk_sharecount == 0)
    651 			WAKEUP_WAITER(lkp);
    652 		/* fall into exclusive request */
    653 
    654 	case LK_EXCLUSIVE:
    655 		if (WEHOLDIT(lkp, pid, lid, cpu_id)) {
    656 			/*
    657 			 * Recursive lock.
    658 			 */
    659 			if ((extflags & LK_CANRECURSE) == 0 &&
    660 			     lkp->lk_recurselevel == 0) {
    661 				if (extflags & LK_RECURSEFAIL) {
    662 					error = EDEADLK;
    663 					break;
    664 				} else
    665 					panic("lockmgr: locking against myself");
    666 			}
    667 			lkp->lk_exclusivecount++;
    668 			if (extflags & LK_SETRECURSE &&
    669 			    lkp->lk_recurselevel == 0)
    670 				lkp->lk_recurselevel = lkp->lk_exclusivecount;
    671 			COUNT(lkp, l, cpu_id, 1);
    672 			break;
    673 		}
    674 		/*
    675 		 * If we are just polling, check to see if we will sleep.
    676 		 */
    677 		if ((extflags & LK_NOWAIT) && ((lkp->lk_flags &
    678 		     (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
    679 		     lkp->lk_sharecount != 0)) {
    680 			error = EBUSY;
    681 			break;
    682 		}
    683 		/*
    684 		 * Try to acquire the want_exclusive flag.
    685 		 */
    686 		ACQUIRE(lkp, error, extflags, 0, lkp->lk_flags &
    687 		    (LK_HAVE_EXCL | LK_WANT_EXCL));
    688 		if (error)
    689 			break;
    690 		lkp->lk_flags |= LK_WANT_EXCL;
    691 		/*
    692 		 * Wait for shared locks and upgrades to finish.
    693 		 */
    694 		ACQUIRE(lkp, error, extflags, 0, lkp->lk_sharecount != 0 ||
    695 		       (lkp->lk_flags & LK_WANT_UPGRADE));
    696 		lkp->lk_flags &= ~LK_WANT_EXCL;
    697 		if (error)
    698 			break;
    699 		lkp->lk_flags |= LK_HAVE_EXCL;
    700 		SETHOLDER(lkp, pid, lid, cpu_id);
    701 #if defined(LOCKDEBUG)
    702 		lkp->lk_lock_file = file;
    703 		lkp->lk_lock_line = line;
    704 #endif
    705 		HAVEIT(lkp);
    706 		if (lkp->lk_exclusivecount != 0)
    707 			panic("lockmgr: non-zero exclusive count");
    708 		lkp->lk_exclusivecount = 1;
    709 		if (extflags & LK_SETRECURSE)
    710 			lkp->lk_recurselevel = 1;
    711 		COUNT(lkp, l, cpu_id, 1);
    712 		break;
    713 
    714 	case LK_RELEASE:
    715 		if (lkp->lk_exclusivecount != 0) {
    716 			if (WEHOLDIT(lkp, pid, lid, cpu_id) == 0) {
    717 				if (lkp->lk_flags & LK_SPIN) {
    718 					panic("lockmgr: processor %lu, not "
    719 					    "exclusive lock holder %lu "
    720 					    "unlocking", cpu_id, lkp->lk_cpu);
    721 				} else {
    722 					panic("lockmgr: pid %d, not "
    723 					    "exclusive lock holder %d "
    724 					    "unlocking", pid,
    725 					    lkp->lk_lockholder);
    726 				}
    727 			}
    728 			if (lkp->lk_exclusivecount == lkp->lk_recurselevel)
    729 				lkp->lk_recurselevel = 0;
    730 			lkp->lk_exclusivecount--;
    731 			COUNT(lkp, l, cpu_id, -1);
    732 			if (lkp->lk_exclusivecount == 0) {
    733 				lkp->lk_flags &= ~LK_HAVE_EXCL;
    734 				SETHOLDER(lkp, LK_NOPROC, 0, LK_NOCPU);
    735 #if defined(LOCKDEBUG)
    736 				lkp->lk_unlock_file = file;
    737 				lkp->lk_unlock_line = line;
    738 #endif
    739 				DONTHAVEIT(lkp);
    740 			}
    741 		} else if (lkp->lk_sharecount != 0) {
    742 			lkp->lk_sharecount--;
    743 			COUNT(lkp, l, cpu_id, -1);
    744 		}
    745 #ifdef DIAGNOSTIC
    746 		else
    747 			panic("lockmgr: release of unlocked lock!");
    748 #endif
    749 		WAKEUP_WAITER(lkp);
    750 		break;
    751 
    752 	case LK_DRAIN:
    753 		/*
    754 		 * Check that we do not already hold the lock, as it can
    755 		 * never drain if we do. Unfortunately, we have no way to
    756 		 * check for holding a shared lock, but at least we can
    757 		 * check for an exclusive one.
    758 		 */
    759 		if (WEHOLDIT(lkp, pid, lid, cpu_id))
    760 			panic("lockmgr: draining against myself");
    761 		/*
    762 		 * If we are just polling, check to see if we will sleep.
    763 		 */
    764 		if ((extflags & LK_NOWAIT) && ((lkp->lk_flags &
    765 		     (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
    766 		     lkp->lk_sharecount != 0 || lkp->lk_waitcount != 0)) {
    767 			error = EBUSY;
    768 			break;
    769 		}
    770 		ACQUIRE(lkp, error, extflags, 1,
    771 		    ((lkp->lk_flags &
    772 		     (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
    773 		     lkp->lk_sharecount != 0 ||
    774 		     lkp->lk_waitcount != 0));
    775 		if (error)
    776 			break;
    777 		lkp->lk_flags |= LK_DRAINING | LK_HAVE_EXCL;
    778 		SETHOLDER(lkp, pid, lid, cpu_id);
    779 #if defined(LOCKDEBUG)
    780 		lkp->lk_lock_file = file;
    781 		lkp->lk_lock_line = line;
    782 #endif
    783 		HAVEIT(lkp);
    784 		lkp->lk_exclusivecount = 1;
    785 		/* XXX unlikely that we'd want this */
    786 		if (extflags & LK_SETRECURSE)
    787 			lkp->lk_recurselevel = 1;
    788 		COUNT(lkp, l, cpu_id, 1);
    789 		break;
    790 
    791 	default:
    792 		INTERLOCK_RELEASE(lkp, lkp->lk_flags, s);
    793 		panic("lockmgr: unknown locktype request %d",
    794 		    flags & LK_TYPE_MASK);
    795 		/* NOTREACHED */
    796 	}
    797 	if ((lkp->lk_flags & (LK_WAITDRAIN|LK_SPIN)) == LK_WAITDRAIN &&
    798 	    ((lkp->lk_flags &
    799 	      (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) == 0 &&
    800 	     lkp->lk_sharecount == 0 && lkp->lk_waitcount == 0)) {
    801 		lkp->lk_flags &= ~LK_WAITDRAIN;
    802 		wakeup((void *)&lkp->lk_flags);
    803 	}
    804 	/*
    805 	 * Note that this panic will be a recursive panic, since
    806 	 * we only set lock_shutdown_noblock above if panicstr != NULL.
    807 	 */
    808 	if (error && lock_shutdown_noblock)
    809 		panic("lockmgr: deadlock (see previous panic)");
    810 
    811 	INTERLOCK_RELEASE(lkp, lkp->lk_flags, s);
    812 	return (error);
    813 }
    814 
    815 /*
    816  * For a recursive spinlock held one or more times by the current CPU,
    817  * release all N locks, and return N.
    818  * Intended for use in mi_switch() shortly before context switching.
    819  */
    820 
    821 int
    822 #if defined(LOCKDEBUG)
    823 _spinlock_release_all(__volatile struct lock *lkp, const char *file, int line)
    824 #else
    825 spinlock_release_all(__volatile struct lock *lkp)
    826 #endif
    827 {
    828 	int s, count;
    829 	cpuid_t cpu_id;
    830 
    831 	KASSERT(lkp->lk_flags & LK_SPIN);
    832 
    833 	INTERLOCK_ACQUIRE(lkp, LK_SPIN, s);
    834 
    835 	cpu_id = cpu_number();
    836 	count = lkp->lk_exclusivecount;
    837 
    838 	if (count != 0) {
    839 #ifdef DIAGNOSTIC
    840 		if (WEHOLDIT(lkp, 0, 0, cpu_id) == 0) {
    841 			panic("spinlock_release_all: processor %lu, not "
    842 			    "exclusive lock holder %lu "
    843 			    "unlocking", (long)cpu_id, lkp->lk_cpu);
    844 		}
    845 #endif
    846 		lkp->lk_recurselevel = 0;
    847 		lkp->lk_exclusivecount = 0;
    848 		COUNT_CPU(cpu_id, -count);
    849 		lkp->lk_flags &= ~LK_HAVE_EXCL;
    850 		SETHOLDER(lkp, LK_NOPROC, 0, LK_NOCPU);
    851 #if defined(LOCKDEBUG)
    852 		lkp->lk_unlock_file = file;
    853 		lkp->lk_unlock_line = line;
    854 #endif
    855 		DONTHAVEIT(lkp);
    856 	}
    857 #ifdef DIAGNOSTIC
    858 	else if (lkp->lk_sharecount != 0)
    859 		panic("spinlock_release_all: release of shared lock!");
    860 	else
    861 		panic("spinlock_release_all: release of unlocked lock!");
    862 #endif
    863 	INTERLOCK_RELEASE(lkp, LK_SPIN, s);
    864 
    865 	return (count);
    866 }
    867 
    868 /*
    869  * For a recursive spinlock held one or more times by the current CPU,
    870  * release all N locks, and return N.
    871  * Intended for use in mi_switch() right after resuming execution.
    872  */
    873 
    874 void
    875 #if defined(LOCKDEBUG)
    876 _spinlock_acquire_count(__volatile struct lock *lkp, int count,
    877     const char *file, int line)
    878 #else
    879 spinlock_acquire_count(__volatile struct lock *lkp, int count)
    880 #endif
    881 {
    882 	int s, error;
    883 	cpuid_t cpu_id;
    884 
    885 	KASSERT(lkp->lk_flags & LK_SPIN);
    886 
    887 	INTERLOCK_ACQUIRE(lkp, LK_SPIN, s);
    888 
    889 	cpu_id = cpu_number();
    890 
    891 #ifdef DIAGNOSTIC
    892 	if (WEHOLDIT(lkp, LK_NOPROC, 0, cpu_id))
    893 		panic("spinlock_acquire_count: processor %lu already holds lock\n", (long)cpu_id);
    894 #endif
    895 	/*
    896 	 * Try to acquire the want_exclusive flag.
    897 	 */
    898 	ACQUIRE(lkp, error, LK_SPIN, 0, lkp->lk_flags &
    899 	    (LK_HAVE_EXCL | LK_WANT_EXCL));
    900 	lkp->lk_flags |= LK_WANT_EXCL;
    901 	/*
    902 	 * Wait for shared locks and upgrades to finish.
    903 	 */
    904 	ACQUIRE(lkp, error, LK_SPIN, 0, lkp->lk_sharecount != 0 ||
    905 	    (lkp->lk_flags & LK_WANT_UPGRADE));
    906 	lkp->lk_flags &= ~LK_WANT_EXCL;
    907 	lkp->lk_flags |= LK_HAVE_EXCL;
    908 	SETHOLDER(lkp, LK_NOPROC, 0, cpu_id);
    909 #if defined(LOCKDEBUG)
    910 	lkp->lk_lock_file = file;
    911 	lkp->lk_lock_line = line;
    912 #endif
    913 	HAVEIT(lkp);
    914 	if (lkp->lk_exclusivecount != 0)
    915 		panic("lockmgr: non-zero exclusive count");
    916 	lkp->lk_exclusivecount = count;
    917 	lkp->lk_recurselevel = 1;
    918 	COUNT_CPU(cpu_id, count);
    919 
    920 	INTERLOCK_RELEASE(lkp, lkp->lk_flags, s);
    921 }
    922 
    923 
    924 
    925 /*
    926  * Print out information about state of a lock. Used by VOP_PRINT
    927  * routines to display ststus about contained locks.
    928  */
    929 void
    930 lockmgr_printinfo(__volatile struct lock *lkp)
    931 {
    932 
    933 	if (lkp->lk_sharecount)
    934 		printf(" lock type %s: SHARED (count %d)", lkp->lk_wmesg,
    935 		    lkp->lk_sharecount);
    936 	else if (lkp->lk_flags & LK_HAVE_EXCL) {
    937 		printf(" lock type %s: EXCL (count %d) by ",
    938 		    lkp->lk_wmesg, lkp->lk_exclusivecount);
    939 		if (lkp->lk_flags & LK_SPIN)
    940 			printf("processor %lu", lkp->lk_cpu);
    941 		else
    942 			printf("pid %d.%d", lkp->lk_lockholder,
    943 			    lkp->lk_locklwp);
    944 	} else
    945 		printf(" not locked");
    946 	if ((lkp->lk_flags & LK_SPIN) == 0 && lkp->lk_waitcount > 0)
    947 		printf(" with %d pending", lkp->lk_waitcount);
    948 }
    949 
    950 #if defined(LOCKDEBUG) /* { */
    951 TAILQ_HEAD(, simplelock) simplelock_list =
    952     TAILQ_HEAD_INITIALIZER(simplelock_list);
    953 
    954 #if defined(MULTIPROCESSOR) /* { */
    955 struct simplelock simplelock_list_slock = SIMPLELOCK_INITIALIZER;
    956 
    957 #define	SLOCK_LIST_LOCK()						\
    958 	__cpu_simple_lock(&simplelock_list_slock.lock_data)
    959 
    960 #define	SLOCK_LIST_UNLOCK()						\
    961 	__cpu_simple_unlock(&simplelock_list_slock.lock_data)
    962 
    963 #define	SLOCK_COUNT(x)							\
    964 	curcpu()->ci_simple_locks += (x)
    965 #else
    966 u_long simple_locks;
    967 
    968 #define	SLOCK_LIST_LOCK()	/* nothing */
    969 
    970 #define	SLOCK_LIST_UNLOCK()	/* nothing */
    971 
    972 #define	SLOCK_COUNT(x)		simple_locks += (x)
    973 #endif /* MULTIPROCESSOR */ /* } */
    974 
    975 #ifdef DDB /* { */
    976 #ifdef MULTIPROCESSOR
    977 int simple_lock_debugger = 1;	/* more serious on MP */
    978 #else
    979 int simple_lock_debugger = 0;
    980 #endif
    981 #define	SLOCK_DEBUGGER()	if (simple_lock_debugger) Debugger()
    982 #define	SLOCK_TRACE()							\
    983 	db_stack_trace_print((db_expr_t)__builtin_frame_address(0),	\
    984 	    TRUE, 65535, "", printf);
    985 #else
    986 #define	SLOCK_DEBUGGER()	/* nothing */
    987 #define	SLOCK_TRACE()		/* nothing */
    988 #endif /* } */
    989 
    990 #ifdef MULTIPROCESSOR
    991 #define SLOCK_MP()		lock_printf("on cpu %ld\n", 		\
    992 				    (u_long) cpu_number())
    993 #else
    994 #define SLOCK_MP()		/* nothing */
    995 #endif
    996 
    997 #define	SLOCK_WHERE(str, alp, id, l)					\
    998 do {									\
    999 	lock_printf("\n");						\
   1000 	lock_printf(str);						\
   1001 	lock_printf("lock: %p, currently at: %s:%d\n", (alp), (id), (l)); \
   1002 	SLOCK_MP();							\
   1003 	if ((alp)->lock_file != NULL)					\
   1004 		lock_printf("last locked: %s:%d\n", (alp)->lock_file,	\
   1005 		    (alp)->lock_line);					\
   1006 	if ((alp)->unlock_file != NULL)					\
   1007 		lock_printf("last unlocked: %s:%d\n", (alp)->unlock_file, \
   1008 		    (alp)->unlock_line);				\
   1009 	SLOCK_TRACE()							\
   1010 	SLOCK_DEBUGGER();						\
   1011 } while (/*CONSTCOND*/0)
   1012 
   1013 /*
   1014  * Simple lock functions so that the debugger can see from whence
   1015  * they are being called.
   1016  */
   1017 void
   1018 simple_lock_init(struct simplelock *alp)
   1019 {
   1020 
   1021 #if defined(MULTIPROCESSOR) /* { */
   1022 	__cpu_simple_lock_init(&alp->lock_data);
   1023 #else
   1024 	alp->lock_data = __SIMPLELOCK_UNLOCKED;
   1025 #endif /* } */
   1026 	alp->lock_file = NULL;
   1027 	alp->lock_line = 0;
   1028 	alp->unlock_file = NULL;
   1029 	alp->unlock_line = 0;
   1030 	alp->lock_holder = LK_NOCPU;
   1031 }
   1032 
   1033 void
   1034 _simple_lock(__volatile struct simplelock *alp, const char *id, int l)
   1035 {
   1036 	cpuid_t cpu_id = cpu_number();
   1037 	int s;
   1038 
   1039 	s = spllock();
   1040 
   1041 	/*
   1042 	 * MULTIPROCESSOR case: This is `safe' since if it's not us, we
   1043 	 * don't take any action, and just fall into the normal spin case.
   1044 	 */
   1045 	if (alp->lock_data == __SIMPLELOCK_LOCKED) {
   1046 #if defined(MULTIPROCESSOR) /* { */
   1047 		if (alp->lock_holder == cpu_id) {
   1048 			SLOCK_WHERE("simple_lock: locking against myself\n",
   1049 			    alp, id, l);
   1050 			goto out;
   1051 		}
   1052 #else
   1053 		SLOCK_WHERE("simple_lock: lock held\n", alp, id, l);
   1054 		goto out;
   1055 #endif /* MULTIPROCESSOR */ /* } */
   1056 	}
   1057 
   1058 #if defined(MULTIPROCESSOR) /* { */
   1059 	/* Acquire the lock before modifying any fields. */
   1060 	__cpu_simple_lock(&alp->lock_data);
   1061 #else
   1062 	alp->lock_data = __SIMPLELOCK_LOCKED;
   1063 #endif /* } */
   1064 
   1065 	if (alp->lock_holder != LK_NOCPU) {
   1066 		SLOCK_WHERE("simple_lock: uninitialized lock\n",
   1067 		    alp, id, l);
   1068 	}
   1069 	alp->lock_file = id;
   1070 	alp->lock_line = l;
   1071 	alp->lock_holder = cpu_id;
   1072 
   1073 	SLOCK_LIST_LOCK();
   1074 	/* XXX Cast away volatile */
   1075 	TAILQ_INSERT_TAIL(&simplelock_list, (struct simplelock *)alp, list);
   1076 	SLOCK_LIST_UNLOCK();
   1077 
   1078 	SLOCK_COUNT(1);
   1079 
   1080  out:
   1081 	splx(s);
   1082 }
   1083 
   1084 int
   1085 _simple_lock_held(__volatile struct simplelock *alp)
   1086 {
   1087 #if defined(MULTIPROCESSOR) || defined(DIAGNOSTIC)
   1088 	cpuid_t cpu_id = cpu_number();
   1089 #endif
   1090 	int s, locked = 0;
   1091 
   1092 	s = spllock();
   1093 
   1094 #if defined(MULTIPROCESSOR)
   1095 	if (__cpu_simple_lock_try(&alp->lock_data) == 0)
   1096 		locked = (alp->lock_holder == cpu_id);
   1097 	else
   1098 		__cpu_simple_unlock(&alp->lock_data);
   1099 #else
   1100 	if (alp->lock_data == __SIMPLELOCK_LOCKED) {
   1101 		locked = 1;
   1102 		KASSERT(alp->lock_holder == cpu_id);
   1103 	}
   1104 #endif
   1105 
   1106 	splx(s);
   1107 
   1108 	return (locked);
   1109 }
   1110 
   1111 int
   1112 _simple_lock_try(__volatile struct simplelock *alp, const char *id, int l)
   1113 {
   1114 	cpuid_t cpu_id = cpu_number();
   1115 	int s, rv = 0;
   1116 
   1117 	s = spllock();
   1118 
   1119 	/*
   1120 	 * MULTIPROCESSOR case: This is `safe' since if it's not us, we
   1121 	 * don't take any action.
   1122 	 */
   1123 #if defined(MULTIPROCESSOR) /* { */
   1124 	if ((rv = __cpu_simple_lock_try(&alp->lock_data)) == 0) {
   1125 		if (alp->lock_holder == cpu_id)
   1126 			SLOCK_WHERE("simple_lock_try: locking against myself\n",
   1127 			    alp, id, l);
   1128 		goto out;
   1129 	}
   1130 #else
   1131 	if (alp->lock_data == __SIMPLELOCK_LOCKED) {
   1132 		SLOCK_WHERE("simple_lock_try: lock held\n", alp, id, l);
   1133 		goto out;
   1134 	}
   1135 	alp->lock_data = __SIMPLELOCK_LOCKED;
   1136 #endif /* MULTIPROCESSOR */ /* } */
   1137 
   1138 	/*
   1139 	 * At this point, we have acquired the lock.
   1140 	 */
   1141 
   1142 	rv = 1;
   1143 
   1144 	alp->lock_file = id;
   1145 	alp->lock_line = l;
   1146 	alp->lock_holder = cpu_id;
   1147 
   1148 	SLOCK_LIST_LOCK();
   1149 	/* XXX Cast away volatile. */
   1150 	TAILQ_INSERT_TAIL(&simplelock_list, (struct simplelock *)alp, list);
   1151 	SLOCK_LIST_UNLOCK();
   1152 
   1153 	SLOCK_COUNT(1);
   1154 
   1155  out:
   1156 	splx(s);
   1157 	return (rv);
   1158 }
   1159 
   1160 void
   1161 _simple_unlock(__volatile struct simplelock *alp, const char *id, int l)
   1162 {
   1163 	int s;
   1164 
   1165 	s = spllock();
   1166 
   1167 	/*
   1168 	 * MULTIPROCESSOR case: This is `safe' because we think we hold
   1169 	 * the lock, and if we don't, we don't take any action.
   1170 	 */
   1171 	if (alp->lock_data == __SIMPLELOCK_UNLOCKED) {
   1172 		SLOCK_WHERE("simple_unlock: lock not held\n",
   1173 		    alp, id, l);
   1174 		goto out;
   1175 	}
   1176 
   1177 	SLOCK_LIST_LOCK();
   1178 	TAILQ_REMOVE(&simplelock_list, alp, list);
   1179 	SLOCK_LIST_UNLOCK();
   1180 
   1181 	SLOCK_COUNT(-1);
   1182 
   1183 	alp->list.tqe_next = NULL;	/* sanity */
   1184 	alp->list.tqe_prev = NULL;	/* sanity */
   1185 
   1186 	alp->unlock_file = id;
   1187 	alp->unlock_line = l;
   1188 
   1189 #if defined(MULTIPROCESSOR) /* { */
   1190 	alp->lock_holder = LK_NOCPU;
   1191 	/* Now that we've modified all fields, release the lock. */
   1192 	__cpu_simple_unlock(&alp->lock_data);
   1193 #else
   1194 	alp->lock_data = __SIMPLELOCK_UNLOCKED;
   1195 	KASSERT(alp->lock_holder == cpu_number());
   1196 	alp->lock_holder = LK_NOCPU;
   1197 #endif /* } */
   1198 
   1199  out:
   1200 	splx(s);
   1201 }
   1202 
   1203 void
   1204 simple_lock_dump(void)
   1205 {
   1206 	struct simplelock *alp;
   1207 	int s;
   1208 
   1209 	s = spllock();
   1210 	SLOCK_LIST_LOCK();
   1211 	lock_printf("all simple locks:\n");
   1212 	TAILQ_FOREACH(alp, &simplelock_list, list) {
   1213 		lock_printf("%p CPU %lu %s:%d\n", alp, alp->lock_holder,
   1214 		    alp->lock_file, alp->lock_line);
   1215 	}
   1216 	SLOCK_LIST_UNLOCK();
   1217 	splx(s);
   1218 }
   1219 
   1220 void
   1221 simple_lock_freecheck(void *start, void *end)
   1222 {
   1223 	struct simplelock *alp;
   1224 	int s;
   1225 
   1226 	s = spllock();
   1227 	SLOCK_LIST_LOCK();
   1228 	TAILQ_FOREACH(alp, &simplelock_list, list) {
   1229 		if ((void *)alp >= start && (void *)alp < end) {
   1230 			lock_printf("freeing simple_lock %p CPU %lu %s:%d\n",
   1231 			    alp, alp->lock_holder, alp->lock_file,
   1232 			    alp->lock_line);
   1233 			SLOCK_DEBUGGER();
   1234 		}
   1235 	}
   1236 	SLOCK_LIST_UNLOCK();
   1237 	splx(s);
   1238 }
   1239 
   1240 /*
   1241  * We must be holding exactly one lock: the sched_lock.
   1242  */
   1243 
   1244 void
   1245 simple_lock_switchcheck(void)
   1246 {
   1247 
   1248 	simple_lock_only_held(&sched_lock, "switching");
   1249 }
   1250 
   1251 void
   1252 simple_lock_only_held(volatile struct simplelock *lp, const char *where)
   1253 {
   1254 	struct simplelock *alp;
   1255 	cpuid_t cpu_id = cpu_number();
   1256 	int s;
   1257 
   1258 	if (lp) {
   1259 		LOCK_ASSERT(simple_lock_held(lp));
   1260 	}
   1261 	s = spllock();
   1262 	SLOCK_LIST_LOCK();
   1263 	TAILQ_FOREACH(alp, &simplelock_list, list) {
   1264 		if (alp == lp)
   1265 			continue;
   1266 		if (alp->lock_holder == cpu_id)
   1267 			break;
   1268 	}
   1269 	SLOCK_LIST_UNLOCK();
   1270 	splx(s);
   1271 
   1272 	if (alp != NULL) {
   1273 		lock_printf("\n%s with held simple_lock %p "
   1274 		    "CPU %lu %s:%d\n",
   1275 		    where, alp, alp->lock_holder, alp->lock_file,
   1276 		    alp->lock_line);
   1277 		SLOCK_TRACE();
   1278 		SLOCK_DEBUGGER();
   1279 	}
   1280 }
   1281 #endif /* LOCKDEBUG */ /* } */
   1282 
   1283 #if defined(MULTIPROCESSOR)
   1284 /*
   1285  * Functions for manipulating the kernel_lock.  We put them here
   1286  * so that they show up in profiles.
   1287  */
   1288 
   1289 struct lock kernel_lock;
   1290 
   1291 void
   1292 _kernel_lock_init(void)
   1293 {
   1294 
   1295 	spinlockinit(&kernel_lock, "klock", 0);
   1296 }
   1297 
   1298 /*
   1299  * Acquire/release the kernel lock.  Intended for use in the scheduler
   1300  * and the lower half of the kernel.
   1301  */
   1302 void
   1303 _kernel_lock(int flag)
   1304 {
   1305 
   1306 	SCHED_ASSERT_UNLOCKED();
   1307 	spinlockmgr(&kernel_lock, flag, 0);
   1308 }
   1309 
   1310 void
   1311 _kernel_unlock(void)
   1312 {
   1313 
   1314 	spinlockmgr(&kernel_lock, LK_RELEASE, 0);
   1315 }
   1316 
   1317 /*
   1318  * Acquire/release the kernel_lock on behalf of a process.  Intended for
   1319  * use in the top half of the kernel.
   1320  */
   1321 void
   1322 _kernel_proc_lock(struct lwp *l)
   1323 {
   1324 
   1325 	SCHED_ASSERT_UNLOCKED();
   1326 	spinlockmgr(&kernel_lock, LK_EXCLUSIVE, 0);
   1327 	l->l_flag |= P_BIGLOCK;
   1328 }
   1329 
   1330 void
   1331 _kernel_proc_unlock(struct lwp *l)
   1332 {
   1333 
   1334 	l->l_flag &= ~P_BIGLOCK;
   1335 	spinlockmgr(&kernel_lock, LK_RELEASE, 0);
   1336 }
   1337 #endif /* MULTIPROCESSOR */
   1338