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