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