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