Home | History | Annotate | Line # | Download | only in kern
kern_lock.c revision 1.115
      1 /*	$NetBSD: kern_lock.c,v 1.115 2007/06/15 20:59:38 ad Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999, 2000, 2006, 2007 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, and by Andrew Doran.
     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.115 2007/06/15 20:59:38 ad Exp $");
     80 
     81 #include "opt_multiprocessor.h"
     82 #include "opt_ddb.h"
     83 
     84 #define	__MUTEX_PRIVATE
     85 
     86 #include <sys/param.h>
     87 #include <sys/proc.h>
     88 #include <sys/lock.h>
     89 #include <sys/systm.h>
     90 #include <sys/lockdebug.h>
     91 
     92 #include <machine/cpu.h>
     93 #include <machine/stdarg.h>
     94 
     95 #include <dev/lockstat.h>
     96 
     97 #if defined(LOCKDEBUG)
     98 #include <sys/syslog.h>
     99 /*
    100  * note that stdarg.h and the ansi style va_start macro is used for both
    101  * ansi and traditional c compiles.
    102  * XXX: this requires that stdarg.h define: va_alist and va_dcl
    103  */
    104 #include <machine/stdarg.h>
    105 
    106 void	lock_printf(const char *fmt, ...)
    107     __attribute__((__format__(__printf__,1,2)));
    108 
    109 static int acquire(volatile struct lock **, int *, int, int, int, uintptr_t);
    110 
    111 int	lock_debug_syslog = 0;	/* defaults to printf, but can be patched */
    112 
    113 #ifdef DDB
    114 #include <ddb/ddbvar.h>
    115 #include <machine/db_machdep.h>
    116 #include <ddb/db_command.h>
    117 #include <ddb/db_interface.h>
    118 #endif
    119 #endif /* defined(LOCKDEBUG) */
    120 
    121 #if defined(MULTIPROCESSOR)
    122 int kernel_lock_id;
    123 __cpu_simple_lock_t kernel_lock;
    124 #endif
    125 
    126 /*
    127  * Locking primitives implementation.
    128  * Locks provide shared/exclusive synchronization.
    129  */
    130 
    131 #if defined(LOCKDEBUG) || defined(DIAGNOSTIC) /* { */
    132 #if defined(MULTIPROCESSOR) /* { */
    133 #define	COUNT_CPU(cpu_id, x)						\
    134 	curcpu()->ci_spin_locks += (x)
    135 #else
    136 u_long	spin_locks;
    137 #define	COUNT_CPU(cpu_id, x)	spin_locks += (x)
    138 #endif /* MULTIPROCESSOR */ /* } */
    139 
    140 #define	COUNT(lkp, l, cpu_id, x)					\
    141 do {									\
    142 	if ((lkp)->lk_flags & LK_SPIN)					\
    143 		COUNT_CPU((cpu_id), (x));				\
    144 	else								\
    145 		(l)->l_locks += (x);					\
    146 } while (/*CONSTCOND*/0)
    147 #else
    148 #define COUNT(lkp, p, cpu_id, x)
    149 #define COUNT_CPU(cpu_id, x)
    150 #endif /* LOCKDEBUG || DIAGNOSTIC */ /* } */
    151 
    152 #define	INTERLOCK_ACQUIRE(lkp, flags, s)				\
    153 do {									\
    154 	if ((flags) & LK_SPIN)						\
    155 		s = splhigh();						\
    156 	simple_lock(&(lkp)->lk_interlock);				\
    157 } while (/*CONSTCOND*/ 0)
    158 
    159 #define	INTERLOCK_RELEASE(lkp, flags, s)				\
    160 do {									\
    161 	simple_unlock(&(lkp)->lk_interlock);				\
    162 	if ((flags) & LK_SPIN)						\
    163 		splx(s);						\
    164 } while (/*CONSTCOND*/ 0)
    165 
    166 #ifdef DDB /* { */
    167 #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
    168 int simple_lock_debugger = 1;	/* more serious on MP */
    169 #else
    170 int simple_lock_debugger = 0;
    171 #endif
    172 #define	SLOCK_DEBUGGER()	if (simple_lock_debugger && db_onpanic) Debugger()
    173 #define	SLOCK_TRACE()							\
    174 	db_stack_trace_print((db_expr_t)__builtin_frame_address(0),	\
    175 	    true, 65535, "", lock_printf);
    176 #else
    177 #define	SLOCK_DEBUGGER()	/* nothing */
    178 #define	SLOCK_TRACE()		/* nothing */
    179 #endif /* } */
    180 
    181 #if defined(LOCKDEBUG)
    182 #if defined(DDB)
    183 #define	SPINLOCK_SPINCHECK_DEBUGGER	if (db_onpanic) Debugger()
    184 #else
    185 #define	SPINLOCK_SPINCHECK_DEBUGGER	/* nothing */
    186 #endif
    187 
    188 #define	SPINLOCK_SPINCHECK_DECL						\
    189 	/* 32-bits of count -- wrap constitutes a "spinout" */		\
    190 	uint32_t __spinc = 0
    191 
    192 #define	SPINLOCK_SPINCHECK						\
    193 do {									\
    194 	if (++__spinc == 0) {						\
    195 		lock_printf("LK_SPIN spinout, excl %d, share %d\n",	\
    196 		    lkp->lk_exclusivecount, lkp->lk_sharecount);	\
    197 		if (lkp->lk_exclusivecount)				\
    198 			lock_printf("held by CPU %lu\n",		\
    199 			    (u_long) lkp->lk_cpu);			\
    200 		if (lkp->lk_lock_file)					\
    201 			lock_printf("last locked at %s:%d\n",		\
    202 			    lkp->lk_lock_file, lkp->lk_lock_line);	\
    203 		if (lkp->lk_unlock_file)				\
    204 			lock_printf("last unlocked at %s:%d\n",		\
    205 			    lkp->lk_unlock_file, lkp->lk_unlock_line);	\
    206 		SLOCK_TRACE();						\
    207 		SPINLOCK_SPINCHECK_DEBUGGER;				\
    208 	}								\
    209 } while (/*CONSTCOND*/ 0)
    210 #else
    211 #define	SPINLOCK_SPINCHECK_DECL			/* nothing */
    212 #define	SPINLOCK_SPINCHECK			/* nothing */
    213 #endif /* LOCKDEBUG && DDB */
    214 
    215 #define	RETURN_ADDRESS		((uintptr_t)__builtin_return_address(0))
    216 
    217 /*
    218  * Acquire a resource.
    219  */
    220 static int
    221 acquire(volatile struct lock **lkpp, int *s, int extflags,
    222     int drain, int wanted, uintptr_t ra)
    223 {
    224 	int error;
    225 	volatile struct lock *lkp = *lkpp;
    226 	LOCKSTAT_TIMER(slptime);
    227 	LOCKSTAT_FLAG(lsflag);
    228 
    229 	KASSERT(drain || (wanted & LK_WAIT_NONZERO) == 0);
    230 
    231 	if (extflags & LK_SPIN) {
    232 		int interlocked;
    233 
    234 		SPINLOCK_SPINCHECK_DECL;
    235 
    236 		if (!drain) {
    237 			lkp->lk_waitcount++;
    238 			lkp->lk_flags |= LK_WAIT_NONZERO;
    239 		}
    240 		for (interlocked = 1;;) {
    241 			SPINLOCK_SPINCHECK;
    242 			if ((lkp->lk_flags & wanted) != 0) {
    243 				if (interlocked) {
    244 					INTERLOCK_RELEASE(lkp, LK_SPIN, *s);
    245 					interlocked = 0;
    246 				}
    247 				SPINLOCK_SPIN_HOOK;
    248 			} else if (interlocked) {
    249 				break;
    250 			} else {
    251 				INTERLOCK_ACQUIRE(lkp, LK_SPIN, *s);
    252 				interlocked = 1;
    253 			}
    254 		}
    255 		if (!drain) {
    256 			lkp->lk_waitcount--;
    257 			if (lkp->lk_waitcount == 0)
    258 				lkp->lk_flags &= ~LK_WAIT_NONZERO;
    259 		}
    260 		KASSERT((lkp->lk_flags & wanted) == 0);
    261 		error = 0;	/* sanity */
    262 	} else {
    263 		LOCKSTAT_ENTER(lsflag);
    264 
    265 		for (error = 0; (lkp->lk_flags & wanted) != 0; ) {
    266 			if (drain)
    267 				lkp->lk_flags |= LK_WAITDRAIN;
    268 			else {
    269 				lkp->lk_waitcount++;
    270 				lkp->lk_flags |= LK_WAIT_NONZERO;
    271 			}
    272 			/* XXX Cast away volatile. */
    273 			LOCKSTAT_START_TIMER(lsflag, slptime);
    274 			error = ltsleep(drain ?
    275 			    (volatile const void *)&lkp->lk_flags :
    276 			    (volatile const void *)lkp, lkp->lk_prio,
    277 			    lkp->lk_wmesg, lkp->lk_timo, &lkp->lk_interlock);
    278 			LOCKSTAT_STOP_TIMER(lsflag, slptime);
    279 			LOCKSTAT_EVENT_RA(lsflag, (void *)(uintptr_t)lkp,
    280 			    LB_LOCKMGR | LB_SLEEP1, 1, slptime, ra);
    281 			if (!drain) {
    282 				lkp->lk_waitcount--;
    283 				if (lkp->lk_waitcount == 0)
    284 					lkp->lk_flags &= ~LK_WAIT_NONZERO;
    285 			}
    286 			if (error)
    287 				break;
    288 			if (extflags & LK_SLEEPFAIL) {
    289 				error = ENOLCK;
    290 				break;
    291 			}
    292 			if (lkp->lk_newlock != NULL) {
    293 				simple_lock(&lkp->lk_newlock->lk_interlock);
    294 				simple_unlock(&lkp->lk_interlock);
    295 				if (lkp->lk_waitcount == 0)
    296 					wakeup(&lkp->lk_newlock);
    297 				*lkpp = lkp = lkp->lk_newlock;
    298 			}
    299 		}
    300 
    301 		LOCKSTAT_EXIT(lsflag);
    302 	}
    303 
    304 	return error;
    305 }
    306 
    307 #define	SETHOLDER(lkp, pid, lid, cpu_id)				\
    308 do {									\
    309 	if ((lkp)->lk_flags & LK_SPIN)					\
    310 		(lkp)->lk_cpu = cpu_id;					\
    311 	else {								\
    312 		(lkp)->lk_lockholder = pid;				\
    313 		(lkp)->lk_locklwp = lid;				\
    314 	}								\
    315 } while (/*CONSTCOND*/0)
    316 
    317 #define	WEHOLDIT(lkp, pid, lid, cpu_id)					\
    318 	(((lkp)->lk_flags & LK_SPIN) != 0 ?				\
    319 	 ((lkp)->lk_cpu == (cpu_id)) :					\
    320 	 ((lkp)->lk_lockholder == (pid) && (lkp)->lk_locklwp == (lid)))
    321 
    322 #define	WAKEUP_WAITER(lkp)						\
    323 do {									\
    324 	if (((lkp)->lk_flags & (LK_SPIN | LK_WAIT_NONZERO)) ==		\
    325 	    LK_WAIT_NONZERO) {						\
    326 		wakeup((lkp));						\
    327 	}								\
    328 } while (/*CONSTCOND*/0)
    329 
    330 #if defined(LOCKDEBUG) /* { */
    331 #if defined(MULTIPROCESSOR) /* { */
    332 struct simplelock spinlock_list_slock = SIMPLELOCK_INITIALIZER;
    333 
    334 #define	SPINLOCK_LIST_LOCK()						\
    335 	__cpu_simple_lock(&spinlock_list_slock.lock_data)
    336 
    337 #define	SPINLOCK_LIST_UNLOCK()						\
    338 	__cpu_simple_unlock(&spinlock_list_slock.lock_data)
    339 #else
    340 #define	SPINLOCK_LIST_LOCK()	/* nothing */
    341 
    342 #define	SPINLOCK_LIST_UNLOCK()	/* nothing */
    343 #endif /* MULTIPROCESSOR */ /* } */
    344 
    345 _TAILQ_HEAD(, struct lock, volatile) spinlock_list =
    346     TAILQ_HEAD_INITIALIZER(spinlock_list);
    347 
    348 #define	HAVEIT(lkp)							\
    349 do {									\
    350 	if ((lkp)->lk_flags & LK_SPIN) {				\
    351 		int sp = splhigh();					\
    352 		SPINLOCK_LIST_LOCK();					\
    353 		TAILQ_INSERT_TAIL(&spinlock_list, (lkp), lk_list);	\
    354 		SPINLOCK_LIST_UNLOCK();					\
    355 		splx(sp);						\
    356 	}								\
    357 } while (/*CONSTCOND*/0)
    358 
    359 #define	DONTHAVEIT(lkp)							\
    360 do {									\
    361 	if ((lkp)->lk_flags & LK_SPIN) {				\
    362 		int sp = splhigh();					\
    363 		SPINLOCK_LIST_LOCK();					\
    364 		TAILQ_REMOVE(&spinlock_list, (lkp), lk_list);		\
    365 		SPINLOCK_LIST_UNLOCK();					\
    366 		splx(sp);						\
    367 	}								\
    368 } while (/*CONSTCOND*/0)
    369 #else
    370 #define	HAVEIT(lkp)		/* nothing */
    371 
    372 #define	DONTHAVEIT(lkp)		/* nothing */
    373 #endif /* LOCKDEBUG */ /* } */
    374 
    375 #if defined(LOCKDEBUG)
    376 /*
    377  * Lock debug printing routine; can be configured to print to console
    378  * or log to syslog.
    379  */
    380 void
    381 lock_printf(const char *fmt, ...)
    382 {
    383 	char b[150];
    384 	va_list ap;
    385 
    386 	va_start(ap, fmt);
    387 	if (lock_debug_syslog)
    388 		vlog(LOG_DEBUG, fmt, ap);
    389 	else {
    390 		vsnprintf(b, sizeof(b), fmt, ap);
    391 		printf_nolog("%s", b);
    392 	}
    393 	va_end(ap);
    394 }
    395 #endif /* LOCKDEBUG */
    396 
    397 static void
    398 lockpanic(volatile struct lock *lkp, const char *fmt, ...)
    399 {
    400 	char s[150], b[150];
    401 #ifdef LOCKDEBUG
    402 	static const char *locktype[] = {
    403 	    "*0*", "shared", "exclusive", "upgrade", "exclupgrade",
    404 	    "downgrade", "release", "drain", "exclother", "*9*",
    405 	    "*10*", "*11*", "*12*", "*13*", "*14*", "*15*"
    406 	};
    407 #endif
    408 
    409 	va_list ap;
    410 	va_start(ap, fmt);
    411 	vsnprintf(s, sizeof(s), fmt, ap);
    412 	va_end(ap);
    413 	bitmask_snprintf(lkp->lk_flags, __LK_FLAG_BITS, b, sizeof(b));
    414 	panic("%s ("
    415 #ifdef LOCKDEBUG
    416 	    "type %s "
    417 #endif
    418 	    "flags %s, sharecount %d, exclusivecount %d, "
    419 	    "recurselevel %d, waitcount %d, wmesg %s"
    420 #ifdef LOCKDEBUG
    421 	    ", lock_file %s, unlock_file %s, lock_line %d, unlock_line %d"
    422 #endif
    423 	    ")\n",
    424 	    s,
    425 #ifdef LOCKDEBUG
    426 	    locktype[lkp->lk_flags & LK_TYPE_MASK],
    427 #endif
    428 	    b, lkp->lk_sharecount, lkp->lk_exclusivecount,
    429 	    lkp->lk_recurselevel, lkp->lk_waitcount, lkp->lk_wmesg
    430 #ifdef LOCKDEBUG
    431 	    , lkp->lk_lock_file, lkp->lk_unlock_file, lkp->lk_lock_line,
    432 	    lkp->lk_unlock_line
    433 #endif
    434 	);
    435 }
    436 
    437 /*
    438  * Transfer any waiting processes from one lock to another.
    439  */
    440 void
    441 transferlockers(struct lock *from, struct lock *to)
    442 {
    443 
    444 	KASSERT(from != to);
    445 	KASSERT((from->lk_flags & LK_WAITDRAIN) == 0);
    446 	if (from->lk_waitcount == 0)
    447 		return;
    448 	from->lk_newlock = to;
    449 	wakeup((void *)from);
    450 	tsleep((void *)&from->lk_newlock, from->lk_prio, "lkxfer", 0);
    451 	from->lk_newlock = NULL;
    452 	from->lk_flags &= ~(LK_WANT_EXCL | LK_WANT_UPGRADE);
    453 	KASSERT(from->lk_waitcount == 0);
    454 }
    455 
    456 
    457 /*
    458  * Initialize a lock; required before use.
    459  */
    460 void
    461 lockinit(struct lock *lkp, pri_t prio, const char *wmesg, int timo, int flags)
    462 {
    463 
    464 	memset(lkp, 0, sizeof(struct lock));
    465 	simple_lock_init(&lkp->lk_interlock);
    466 	lkp->lk_flags = flags & LK_EXTFLG_MASK;
    467 	if (flags & LK_SPIN)
    468 		lkp->lk_cpu = LK_NOCPU;
    469 	else {
    470 		lkp->lk_lockholder = LK_NOPROC;
    471 		lkp->lk_newlock = NULL;
    472 		lkp->lk_prio = prio;
    473 		lkp->lk_timo = timo;
    474 	}
    475 	lkp->lk_wmesg = wmesg;	/* just a name for spin locks */
    476 #if defined(LOCKDEBUG)
    477 	lkp->lk_lock_file = NULL;
    478 	lkp->lk_unlock_file = NULL;
    479 #endif
    480 }
    481 
    482 /*
    483  * Determine the status of a lock.
    484  */
    485 int
    486 lockstatus(struct lock *lkp)
    487 {
    488 	int s = 0; /* XXX: gcc */
    489 	int lock_type = 0;
    490 	struct lwp *l = curlwp; /* XXX */
    491 	pid_t pid;
    492 	lwpid_t lid;
    493 	cpuid_t cpu_num;
    494 
    495 	if ((lkp->lk_flags & LK_SPIN) || l == NULL) {
    496 		cpu_num = cpu_number();
    497 		pid = LK_KERNPROC;
    498 		lid = 0;
    499 	} else {
    500 		cpu_num = LK_NOCPU;
    501 		pid = l->l_proc->p_pid;
    502 		lid = l->l_lid;
    503 	}
    504 
    505 	INTERLOCK_ACQUIRE(lkp, lkp->lk_flags, s);
    506 	if (lkp->lk_exclusivecount != 0) {
    507 		if (WEHOLDIT(lkp, pid, lid, cpu_num))
    508 			lock_type = LK_EXCLUSIVE;
    509 		else
    510 			lock_type = LK_EXCLOTHER;
    511 	} else if (lkp->lk_sharecount != 0)
    512 		lock_type = LK_SHARED;
    513 	else if (lkp->lk_flags & (LK_WANT_EXCL | LK_WANT_UPGRADE))
    514 		lock_type = LK_EXCLOTHER;
    515 	INTERLOCK_RELEASE(lkp, lkp->lk_flags, s);
    516 	return (lock_type);
    517 }
    518 
    519 #if defined(LOCKDEBUG)
    520 /*
    521  * Make sure no spin locks are held by a CPU that is about
    522  * to context switch.
    523  */
    524 void
    525 spinlock_switchcheck(void)
    526 {
    527 	u_long cnt;
    528 	int s;
    529 
    530 	s = splhigh();
    531 #if defined(MULTIPROCESSOR)
    532 	cnt = curcpu()->ci_spin_locks;
    533 #else
    534 	cnt = spin_locks;
    535 #endif
    536 	splx(s);
    537 
    538 	if (cnt != 0)
    539 		panic("spinlock_switchcheck: CPU %lu has %lu spin locks",
    540 		    (u_long) cpu_number(), cnt);
    541 }
    542 #endif /* LOCKDEBUG */
    543 
    544 /*
    545  * Locks and IPLs (interrupt priority levels):
    546  *
    547  * Locks which may be taken from interrupt context must be handled
    548  * very carefully; you must spl to the highest IPL where the lock
    549  * is needed before acquiring the lock.
    550  *
    551  * It is also important to avoid deadlock, since certain (very high
    552  * priority) interrupts are often needed to keep the system as a whole
    553  * from deadlocking, and must not be blocked while you are spinning
    554  * waiting for a lower-priority lock.
    555  *
    556  * In addition, the lock-debugging hooks themselves need to use locks!
    557  *
    558  * A raw __cpu_simple_lock may be used from interrupts are long as it
    559  * is acquired and held at a single IPL.
    560  */
    561 
    562 /*
    563  * XXX XXX kludge around another kludge..
    564  *
    565  * vfs_shutdown() may be called from interrupt context, either as a result
    566  * of a panic, or from the debugger.   It proceeds to call
    567  * sys_sync(&proc0, ...), pretending its running on behalf of proc0
    568  *
    569  * We would like to make an attempt to sync the filesystems in this case, so
    570  * if this happens, we treat attempts to acquire locks specially.
    571  * All locks are acquired on behalf of proc0.
    572  *
    573  * If we've already paniced, we don't block waiting for locks, but
    574  * just barge right ahead since we're already going down in flames.
    575  */
    576 
    577 /*
    578  * Set, change, or release a lock.
    579  *
    580  * Shared requests increment the shared count. Exclusive requests set the
    581  * LK_WANT_EXCL flag (preventing further shared locks), and wait for already
    582  * accepted shared locks and shared-to-exclusive upgrades to go away.
    583  */
    584 int
    585 #if defined(LOCKDEBUG)
    586 _lockmgr(volatile struct lock *lkp, u_int flags,
    587     struct simplelock *interlkp, const char *file, int line)
    588 #else
    589 lockmgr(volatile struct lock *lkp, u_int flags,
    590     struct simplelock *interlkp)
    591 #endif
    592 {
    593 	int error;
    594 	pid_t pid;
    595 	lwpid_t lid;
    596 	int extflags;
    597 	cpuid_t cpu_num;
    598 	struct lwp *l = curlwp;
    599 	int lock_shutdown_noblock = 0;
    600 	int s = 0;
    601 
    602 	error = 0;
    603 
    604 	/* LK_RETRY is for vn_lock, not for lockmgr. */
    605 	KASSERT((flags & LK_RETRY) == 0);
    606 
    607 	INTERLOCK_ACQUIRE(lkp, lkp->lk_flags, s);
    608 	if (flags & LK_INTERLOCK)
    609 		simple_unlock(interlkp);
    610 	extflags = (flags | lkp->lk_flags) & LK_EXTFLG_MASK;
    611 
    612 #ifdef DIAGNOSTIC /* { */
    613 	/*
    614 	 * Don't allow spins on sleep locks and don't allow sleeps
    615 	 * on spin locks.
    616 	 */
    617 	if ((flags ^ lkp->lk_flags) & LK_SPIN)
    618 		lockpanic(lkp, "lockmgr: sleep/spin mismatch");
    619 #endif /* } */
    620 
    621 	if (extflags & LK_SPIN) {
    622 		pid = LK_KERNPROC;
    623 		lid = 0;
    624 	} else {
    625 		if (l == NULL) {
    626 			if (!doing_shutdown) {
    627 				panic("lockmgr: no context");
    628 			} else {
    629 				l = &lwp0;
    630 				if (panicstr && (!(flags & LK_NOWAIT))) {
    631 					flags |= LK_NOWAIT;
    632 					lock_shutdown_noblock = 1;
    633 				}
    634 			}
    635 		}
    636 		lid = l->l_lid;
    637 		pid = l->l_proc->p_pid;
    638 	}
    639 	cpu_num = cpu_number();
    640 
    641 	/*
    642 	 * Once a lock has drained, the LK_DRAINING flag is set and an
    643 	 * exclusive lock is returned. The only valid operation thereafter
    644 	 * is a single release of that exclusive lock. This final release
    645 	 * clears the LK_DRAINING flag and sets the LK_DRAINED flag. Any
    646 	 * further requests of any sort will result in a panic. The bits
    647 	 * selected for these two flags are chosen so that they will be set
    648 	 * in memory that is freed (freed memory is filled with 0xdeadbeef).
    649 	 * The final release is permitted to give a new lease on life to
    650 	 * the lock by specifying LK_REENABLE.
    651 	 */
    652 	if (lkp->lk_flags & (LK_DRAINING|LK_DRAINED)) {
    653 #ifdef DIAGNOSTIC /* { */
    654 		if (lkp->lk_flags & LK_DRAINED)
    655 			lockpanic(lkp, "lockmgr: using decommissioned lock");
    656 		if ((flags & LK_TYPE_MASK) != LK_RELEASE ||
    657 		    WEHOLDIT(lkp, pid, lid, cpu_num) == 0)
    658 			lockpanic(lkp, "lockmgr: non-release on draining lock: %d",
    659 			    flags & LK_TYPE_MASK);
    660 #endif /* DIAGNOSTIC */ /* } */
    661 		lkp->lk_flags &= ~LK_DRAINING;
    662 		if ((flags & LK_REENABLE) == 0)
    663 			lkp->lk_flags |= LK_DRAINED;
    664 	}
    665 
    666 	switch (flags & LK_TYPE_MASK) {
    667 
    668 	case LK_SHARED:
    669 		if (WEHOLDIT(lkp, pid, lid, cpu_num) == 0) {
    670 			/*
    671 			 * If just polling, check to see if we will block.
    672 			 */
    673 			if ((extflags & LK_NOWAIT) && (lkp->lk_flags &
    674 			    (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE))) {
    675 				error = EBUSY;
    676 				break;
    677 			}
    678 			/*
    679 			 * Wait for exclusive locks and upgrades to clear.
    680 			 */
    681 			error = acquire(&lkp, &s, extflags, 0,
    682 			    LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE,
    683 			    RETURN_ADDRESS);
    684 			if (error)
    685 				break;
    686 			lkp->lk_sharecount++;
    687 			lkp->lk_flags |= LK_SHARE_NONZERO;
    688 			COUNT(lkp, l, cpu_num, 1);
    689 			break;
    690 		}
    691 		/*
    692 		 * We hold an exclusive lock, so downgrade it to shared.
    693 		 * An alternative would be to fail with EDEADLK.
    694 		 */
    695 		lkp->lk_sharecount++;
    696 		lkp->lk_flags |= LK_SHARE_NONZERO;
    697 		COUNT(lkp, l, cpu_num, 1);
    698 		/* fall into downgrade */
    699 
    700 	case LK_DOWNGRADE:
    701 		if (WEHOLDIT(lkp, pid, lid, cpu_num) == 0 ||
    702 		    lkp->lk_exclusivecount == 0)
    703 			lockpanic(lkp, "lockmgr: not holding exclusive lock");
    704 		lkp->lk_sharecount += lkp->lk_exclusivecount;
    705 		lkp->lk_flags |= LK_SHARE_NONZERO;
    706 		lkp->lk_exclusivecount = 0;
    707 		lkp->lk_recurselevel = 0;
    708 		lkp->lk_flags &= ~LK_HAVE_EXCL;
    709 		SETHOLDER(lkp, LK_NOPROC, 0, LK_NOCPU);
    710 #if defined(LOCKDEBUG)
    711 		lkp->lk_unlock_file = file;
    712 		lkp->lk_unlock_line = line;
    713 #endif
    714 		DONTHAVEIT(lkp);
    715 		WAKEUP_WAITER(lkp);
    716 		break;
    717 
    718 	case LK_EXCLUPGRADE:
    719 		/*
    720 		 * If another process is ahead of us to get an upgrade,
    721 		 * then we want to fail rather than have an intervening
    722 		 * exclusive access.
    723 		 */
    724 		if (lkp->lk_flags & LK_WANT_UPGRADE) {
    725 			lkp->lk_sharecount--;
    726 			if (lkp->lk_sharecount == 0)
    727 				lkp->lk_flags &= ~LK_SHARE_NONZERO;
    728 			COUNT(lkp, l, cpu_num, -1);
    729 			error = EBUSY;
    730 			break;
    731 		}
    732 		/* fall into normal upgrade */
    733 
    734 	case LK_UPGRADE:
    735 		/*
    736 		 * Upgrade a shared lock to an exclusive one. If another
    737 		 * shared lock has already requested an upgrade to an
    738 		 * exclusive lock, our shared lock is released and an
    739 		 * exclusive lock is requested (which will be granted
    740 		 * after the upgrade). If we return an error, the file
    741 		 * will always be unlocked.
    742 		 */
    743 		if (WEHOLDIT(lkp, pid, lid, cpu_num) || lkp->lk_sharecount <= 0)
    744 			lockpanic(lkp, "lockmgr: upgrade exclusive lock");
    745 		lkp->lk_sharecount--;
    746 		if (lkp->lk_sharecount == 0)
    747 			lkp->lk_flags &= ~LK_SHARE_NONZERO;
    748 		COUNT(lkp, l, cpu_num, -1);
    749 		/*
    750 		 * If we are just polling, check to see if we will block.
    751 		 */
    752 		if ((extflags & LK_NOWAIT) &&
    753 		    ((lkp->lk_flags & LK_WANT_UPGRADE) ||
    754 		     lkp->lk_sharecount > 1)) {
    755 			error = EBUSY;
    756 			break;
    757 		}
    758 		if ((lkp->lk_flags & LK_WANT_UPGRADE) == 0) {
    759 			/*
    760 			 * We are first shared lock to request an upgrade, so
    761 			 * request upgrade and wait for the shared count to
    762 			 * drop to zero, then take exclusive lock.
    763 			 */
    764 			lkp->lk_flags |= LK_WANT_UPGRADE;
    765 			error = acquire(&lkp, &s, extflags, 0, LK_SHARE_NONZERO,
    766 			    RETURN_ADDRESS);
    767 			lkp->lk_flags &= ~LK_WANT_UPGRADE;
    768 			if (error) {
    769 				WAKEUP_WAITER(lkp);
    770 				break;
    771 			}
    772 			lkp->lk_flags |= LK_HAVE_EXCL;
    773 			SETHOLDER(lkp, pid, lid, cpu_num);
    774 #if defined(LOCKDEBUG)
    775 			lkp->lk_lock_file = file;
    776 			lkp->lk_lock_line = line;
    777 #endif
    778 			HAVEIT(lkp);
    779 			if (lkp->lk_exclusivecount != 0)
    780 				lockpanic(lkp, "lockmgr: non-zero exclusive count");
    781 			lkp->lk_exclusivecount = 1;
    782 			if (extflags & LK_SETRECURSE)
    783 				lkp->lk_recurselevel = 1;
    784 			COUNT(lkp, l, cpu_num, 1);
    785 			break;
    786 		}
    787 		/*
    788 		 * Someone else has requested upgrade. Release our shared
    789 		 * lock, awaken upgrade requestor if we are the last shared
    790 		 * lock, then request an exclusive lock.
    791 		 */
    792 		if (lkp->lk_sharecount == 0)
    793 			WAKEUP_WAITER(lkp);
    794 		/* fall into exclusive request */
    795 
    796 	case LK_EXCLUSIVE:
    797 		if (WEHOLDIT(lkp, pid, lid, cpu_num)) {
    798 			/*
    799 			 * Recursive lock.
    800 			 */
    801 			if ((extflags & LK_CANRECURSE) == 0 &&
    802 			     lkp->lk_recurselevel == 0) {
    803 				if (extflags & LK_RECURSEFAIL) {
    804 					error = EDEADLK;
    805 					break;
    806 				} else
    807 					lockpanic(lkp, "lockmgr: locking against myself");
    808 			}
    809 			lkp->lk_exclusivecount++;
    810 			if (extflags & LK_SETRECURSE &&
    811 			    lkp->lk_recurselevel == 0)
    812 				lkp->lk_recurselevel = lkp->lk_exclusivecount;
    813 			COUNT(lkp, l, cpu_num, 1);
    814 			break;
    815 		}
    816 		/*
    817 		 * If we are just polling, check to see if we will sleep.
    818 		 */
    819 		if ((extflags & LK_NOWAIT) && (lkp->lk_flags &
    820 		     (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE |
    821 		     LK_SHARE_NONZERO))) {
    822 			error = EBUSY;
    823 			break;
    824 		}
    825 		/*
    826 		 * Try to acquire the want_exclusive flag.
    827 		 */
    828 		error = acquire(&lkp, &s, extflags, 0,
    829 		    LK_HAVE_EXCL | LK_WANT_EXCL, RETURN_ADDRESS);
    830 		if (error)
    831 			break;
    832 		lkp->lk_flags |= LK_WANT_EXCL;
    833 		/*
    834 		 * Wait for shared locks and upgrades to finish.
    835 		 */
    836 		error = acquire(&lkp, &s, extflags, 0,
    837 		    LK_HAVE_EXCL | LK_WANT_UPGRADE | LK_SHARE_NONZERO,
    838 		    RETURN_ADDRESS);
    839 		lkp->lk_flags &= ~LK_WANT_EXCL;
    840 		if (error) {
    841 			WAKEUP_WAITER(lkp);
    842 			break;
    843 		}
    844 		lkp->lk_flags |= LK_HAVE_EXCL;
    845 		SETHOLDER(lkp, pid, lid, cpu_num);
    846 #if defined(LOCKDEBUG)
    847 		lkp->lk_lock_file = file;
    848 		lkp->lk_lock_line = line;
    849 #endif
    850 		HAVEIT(lkp);
    851 		if (lkp->lk_exclusivecount != 0)
    852 			lockpanic(lkp, "lockmgr: non-zero exclusive count");
    853 		lkp->lk_exclusivecount = 1;
    854 		if (extflags & LK_SETRECURSE)
    855 			lkp->lk_recurselevel = 1;
    856 		COUNT(lkp, l, cpu_num, 1);
    857 		break;
    858 
    859 	case LK_RELEASE:
    860 		if (lkp->lk_exclusivecount != 0) {
    861 			if (WEHOLDIT(lkp, pid, lid, cpu_num) == 0) {
    862 				if (lkp->lk_flags & LK_SPIN) {
    863 					lockpanic(lkp,
    864 					    "lockmgr: processor %lu, not "
    865 					    "exclusive lock holder %lu "
    866 					    "unlocking", cpu_num, lkp->lk_cpu);
    867 				} else {
    868 					lockpanic(lkp, "lockmgr: pid %d.%d, not "
    869 					    "exclusive lock holder %d.%d "
    870 					    "unlocking", pid, lid,
    871 					    lkp->lk_lockholder,
    872 					    lkp->lk_locklwp);
    873 				}
    874 			}
    875 			if (lkp->lk_exclusivecount == lkp->lk_recurselevel)
    876 				lkp->lk_recurselevel = 0;
    877 			lkp->lk_exclusivecount--;
    878 			COUNT(lkp, l, cpu_num, -1);
    879 			if (lkp->lk_exclusivecount == 0) {
    880 				lkp->lk_flags &= ~LK_HAVE_EXCL;
    881 				SETHOLDER(lkp, LK_NOPROC, 0, LK_NOCPU);
    882 #if defined(LOCKDEBUG)
    883 				lkp->lk_unlock_file = file;
    884 				lkp->lk_unlock_line = line;
    885 #endif
    886 				DONTHAVEIT(lkp);
    887 			}
    888 		} else if (lkp->lk_sharecount != 0) {
    889 			lkp->lk_sharecount--;
    890 			if (lkp->lk_sharecount == 0)
    891 				lkp->lk_flags &= ~LK_SHARE_NONZERO;
    892 			COUNT(lkp, l, cpu_num, -1);
    893 		}
    894 #ifdef DIAGNOSTIC
    895 		else
    896 			lockpanic(lkp, "lockmgr: release of unlocked lock!");
    897 #endif
    898 		WAKEUP_WAITER(lkp);
    899 		break;
    900 
    901 	case LK_DRAIN:
    902 		/*
    903 		 * Check that we do not already hold the lock, as it can
    904 		 * never drain if we do. Unfortunately, we have no way to
    905 		 * check for holding a shared lock, but at least we can
    906 		 * check for an exclusive one.
    907 		 */
    908 		if (WEHOLDIT(lkp, pid, lid, cpu_num))
    909 			lockpanic(lkp, "lockmgr: draining against myself");
    910 		/*
    911 		 * If we are just polling, check to see if we will sleep.
    912 		 */
    913 		if ((extflags & LK_NOWAIT) && (lkp->lk_flags &
    914 		     (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE |
    915 		     LK_SHARE_NONZERO | LK_WAIT_NONZERO))) {
    916 			error = EBUSY;
    917 			break;
    918 		}
    919 		error = acquire(&lkp, &s, extflags, 1,
    920 		    LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE |
    921 		    LK_SHARE_NONZERO | LK_WAIT_NONZERO,
    922 		    RETURN_ADDRESS);
    923 		if (error)
    924 			break;
    925 		lkp->lk_flags |= LK_DRAINING | LK_HAVE_EXCL;
    926 		SETHOLDER(lkp, pid, lid, cpu_num);
    927 #if defined(LOCKDEBUG)
    928 		lkp->lk_lock_file = file;
    929 		lkp->lk_lock_line = line;
    930 #endif
    931 		HAVEIT(lkp);
    932 		lkp->lk_exclusivecount = 1;
    933 		/* XXX unlikely that we'd want this */
    934 		if (extflags & LK_SETRECURSE)
    935 			lkp->lk_recurselevel = 1;
    936 		COUNT(lkp, l, cpu_num, 1);
    937 		break;
    938 
    939 	default:
    940 		INTERLOCK_RELEASE(lkp, lkp->lk_flags, s);
    941 		lockpanic(lkp, "lockmgr: unknown locktype request %d",
    942 		    flags & LK_TYPE_MASK);
    943 		/* NOTREACHED */
    944 	}
    945 	if ((lkp->lk_flags & (LK_WAITDRAIN|LK_SPIN)) == LK_WAITDRAIN &&
    946 	    ((lkp->lk_flags &
    947 	      (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE |
    948 	      LK_SHARE_NONZERO | LK_WAIT_NONZERO)) == 0)) {
    949 		lkp->lk_flags &= ~LK_WAITDRAIN;
    950 		wakeup(&lkp->lk_flags);
    951 	}
    952 	/*
    953 	 * Note that this panic will be a recursive panic, since
    954 	 * we only set lock_shutdown_noblock above if panicstr != NULL.
    955 	 */
    956 	if (error && lock_shutdown_noblock)
    957 		lockpanic(lkp, "lockmgr: deadlock (see previous panic)");
    958 
    959 	INTERLOCK_RELEASE(lkp, lkp->lk_flags, s);
    960 	return (error);
    961 }
    962 
    963 /*
    964  * For a recursive spinlock held one or more times by the current CPU,
    965  * release all N locks, and return N.
    966  * Intended for use in mi_switch() shortly before context switching.
    967  */
    968 
    969 int
    970 #if defined(LOCKDEBUG)
    971 _spinlock_release_all(volatile struct lock *lkp, const char *file, int line)
    972 #else
    973 spinlock_release_all(volatile struct lock *lkp)
    974 #endif
    975 {
    976 	int s, count;
    977 	cpuid_t cpu_num;
    978 
    979 	KASSERT(lkp->lk_flags & LK_SPIN);
    980 
    981 	INTERLOCK_ACQUIRE(lkp, LK_SPIN, s);
    982 
    983 	cpu_num = cpu_number();
    984 	count = lkp->lk_exclusivecount;
    985 
    986 	if (count != 0) {
    987 #ifdef DIAGNOSTIC
    988 		if (WEHOLDIT(lkp, 0, 0, cpu_num) == 0) {
    989 			lockpanic(lkp, "spinlock_release_all: processor %lu, not "
    990 			    "exclusive lock holder %lu "
    991 			    "unlocking", (long)cpu_num, lkp->lk_cpu);
    992 		}
    993 #endif
    994 		lkp->lk_recurselevel = 0;
    995 		lkp->lk_exclusivecount = 0;
    996 		COUNT_CPU(cpu_num, -count);
    997 		lkp->lk_flags &= ~LK_HAVE_EXCL;
    998 		SETHOLDER(lkp, LK_NOPROC, 0, LK_NOCPU);
    999 #if defined(LOCKDEBUG)
   1000 		lkp->lk_unlock_file = file;
   1001 		lkp->lk_unlock_line = line;
   1002 #endif
   1003 		DONTHAVEIT(lkp);
   1004 	}
   1005 #ifdef DIAGNOSTIC
   1006 	else if (lkp->lk_sharecount != 0)
   1007 		lockpanic(lkp, "spinlock_release_all: release of shared lock!");
   1008 	else
   1009 		lockpanic(lkp, "spinlock_release_all: release of unlocked lock!");
   1010 #endif
   1011 	INTERLOCK_RELEASE(lkp, LK_SPIN, s);
   1012 
   1013 	return (count);
   1014 }
   1015 
   1016 /*
   1017  * For a recursive spinlock held one or more times by the current CPU,
   1018  * release all N locks, and return N.
   1019  * Intended for use in mi_switch() right after resuming execution.
   1020  */
   1021 
   1022 void
   1023 #if defined(LOCKDEBUG)
   1024 _spinlock_acquire_count(volatile struct lock *lkp, int count,
   1025     const char *file, int line)
   1026 #else
   1027 spinlock_acquire_count(volatile struct lock *lkp, int count)
   1028 #endif
   1029 {
   1030 	int s, error;
   1031 	cpuid_t cpu_num;
   1032 
   1033 	KASSERT(lkp->lk_flags & LK_SPIN);
   1034 
   1035 	INTERLOCK_ACQUIRE(lkp, LK_SPIN, s);
   1036 
   1037 	cpu_num = cpu_number();
   1038 
   1039 #ifdef DIAGNOSTIC
   1040 	if (WEHOLDIT(lkp, LK_NOPROC, 0, cpu_num))
   1041 		lockpanic(lkp, "spinlock_acquire_count: processor %lu already holds lock", (long)cpu_num);
   1042 #endif
   1043 	/*
   1044 	 * Try to acquire the want_exclusive flag.
   1045 	 */
   1046 	error = acquire(&lkp, &s, LK_SPIN, 0, LK_HAVE_EXCL | LK_WANT_EXCL,
   1047 	    RETURN_ADDRESS);
   1048 	lkp->lk_flags |= LK_WANT_EXCL;
   1049 	/*
   1050 	 * Wait for shared locks and upgrades to finish.
   1051 	 */
   1052 	error = acquire(&lkp, &s, LK_SPIN, 0,
   1053 	    LK_HAVE_EXCL | LK_SHARE_NONZERO | LK_WANT_UPGRADE,
   1054 	    RETURN_ADDRESS);
   1055 	lkp->lk_flags &= ~LK_WANT_EXCL;
   1056 	lkp->lk_flags |= LK_HAVE_EXCL;
   1057 	SETHOLDER(lkp, LK_NOPROC, 0, cpu_num);
   1058 #if defined(LOCKDEBUG)
   1059 	lkp->lk_lock_file = file;
   1060 	lkp->lk_lock_line = line;
   1061 #endif
   1062 	HAVEIT(lkp);
   1063 	if (lkp->lk_exclusivecount != 0)
   1064 		lockpanic(lkp, "lockmgr: non-zero exclusive count");
   1065 	lkp->lk_exclusivecount = count;
   1066 	lkp->lk_recurselevel = 1;
   1067 	COUNT_CPU(cpu_num, count);
   1068 
   1069 	INTERLOCK_RELEASE(lkp, lkp->lk_flags, s);
   1070 }
   1071 
   1072 
   1073 
   1074 /*
   1075  * Print out information about state of a lock. Used by VOP_PRINT
   1076  * routines to display ststus about contained locks.
   1077  */
   1078 void
   1079 lockmgr_printinfo(volatile struct lock *lkp)
   1080 {
   1081 
   1082 	if (lkp->lk_sharecount)
   1083 		printf(" lock type %s: SHARED (count %d)", lkp->lk_wmesg,
   1084 		    lkp->lk_sharecount);
   1085 	else if (lkp->lk_flags & LK_HAVE_EXCL) {
   1086 		printf(" lock type %s: EXCL (count %d) by ",
   1087 		    lkp->lk_wmesg, lkp->lk_exclusivecount);
   1088 		if (lkp->lk_flags & LK_SPIN)
   1089 			printf("processor %lu", lkp->lk_cpu);
   1090 		else
   1091 			printf("pid %d.%d", lkp->lk_lockholder,
   1092 			    lkp->lk_locklwp);
   1093 	} else
   1094 		printf(" not locked");
   1095 	if ((lkp->lk_flags & LK_SPIN) == 0 && lkp->lk_waitcount > 0)
   1096 		printf(" with %d pending", lkp->lk_waitcount);
   1097 }
   1098 
   1099 #if defined(LOCKDEBUG) /* { */
   1100 _TAILQ_HEAD(, struct simplelock, volatile) simplelock_list =
   1101     TAILQ_HEAD_INITIALIZER(simplelock_list);
   1102 
   1103 #if defined(MULTIPROCESSOR) /* { */
   1104 struct simplelock simplelock_list_slock = SIMPLELOCK_INITIALIZER;
   1105 
   1106 #define	SLOCK_LIST_LOCK()						\
   1107 	__cpu_simple_lock(&simplelock_list_slock.lock_data)
   1108 
   1109 #define	SLOCK_LIST_UNLOCK()						\
   1110 	__cpu_simple_unlock(&simplelock_list_slock.lock_data)
   1111 
   1112 #define	SLOCK_COUNT(x)							\
   1113 	curcpu()->ci_simple_locks += (x)
   1114 #else
   1115 u_long simple_locks;
   1116 
   1117 #define	SLOCK_LIST_LOCK()	/* nothing */
   1118 
   1119 #define	SLOCK_LIST_UNLOCK()	/* nothing */
   1120 
   1121 #define	SLOCK_COUNT(x)		simple_locks += (x)
   1122 #endif /* MULTIPROCESSOR */ /* } */
   1123 
   1124 #ifdef MULTIPROCESSOR
   1125 #define SLOCK_MP()		lock_printf("on CPU %ld\n", 		\
   1126 				    (u_long) cpu_number())
   1127 #else
   1128 #define SLOCK_MP()		/* nothing */
   1129 #endif
   1130 
   1131 #define	SLOCK_WHERE(str, alp, id, l)					\
   1132 do {									\
   1133 	lock_printf("\n");						\
   1134 	lock_printf(str);						\
   1135 	lock_printf("lock: %p, currently at: %s:%d\n", (alp), (id), (l)); \
   1136 	SLOCK_MP();							\
   1137 	if ((alp)->lock_file != NULL)					\
   1138 		lock_printf("last locked: %s:%d\n", (alp)->lock_file,	\
   1139 		    (alp)->lock_line);					\
   1140 	if ((alp)->unlock_file != NULL)					\
   1141 		lock_printf("last unlocked: %s:%d\n", (alp)->unlock_file, \
   1142 		    (alp)->unlock_line);				\
   1143 	SLOCK_TRACE()							\
   1144 	SLOCK_DEBUGGER();						\
   1145 } while (/*CONSTCOND*/0)
   1146 
   1147 /*
   1148  * Simple lock functions so that the debugger can see from whence
   1149  * they are being called.
   1150  */
   1151 void
   1152 simple_lock_init(volatile struct simplelock *alp)
   1153 {
   1154 
   1155 #if defined(MULTIPROCESSOR) /* { */
   1156 	__cpu_simple_lock_init(&alp->lock_data);
   1157 #else
   1158 	alp->lock_data = __SIMPLELOCK_UNLOCKED;
   1159 #endif /* } */
   1160 	alp->lock_file = NULL;
   1161 	alp->lock_line = 0;
   1162 	alp->unlock_file = NULL;
   1163 	alp->unlock_line = 0;
   1164 	alp->lock_holder = LK_NOCPU;
   1165 }
   1166 
   1167 void
   1168 _simple_lock(volatile struct simplelock *alp, const char *id, int l)
   1169 {
   1170 	cpuid_t cpu_num = cpu_number();
   1171 	int s;
   1172 
   1173 	s = splhigh();
   1174 
   1175 	/*
   1176 	 * MULTIPROCESSOR case: This is `safe' since if it's not us, we
   1177 	 * don't take any action, and just fall into the normal spin case.
   1178 	 */
   1179 	if (alp->lock_data == __SIMPLELOCK_LOCKED) {
   1180 #if defined(MULTIPROCESSOR) /* { */
   1181 		if (alp->lock_holder == cpu_num) {
   1182 			SLOCK_WHERE("simple_lock: locking against myself\n",
   1183 			    alp, id, l);
   1184 			goto out;
   1185 		}
   1186 #else
   1187 		SLOCK_WHERE("simple_lock: lock held\n", alp, id, l);
   1188 		goto out;
   1189 #endif /* MULTIPROCESSOR */ /* } */
   1190 	}
   1191 
   1192 #if defined(MULTIPROCESSOR) /* { */
   1193 	/* Acquire the lock before modifying any fields. */
   1194 	splx(s);
   1195 	__cpu_simple_lock(&alp->lock_data);
   1196 	s = splhigh();
   1197 #else
   1198 	alp->lock_data = __SIMPLELOCK_LOCKED;
   1199 #endif /* } */
   1200 
   1201 	if (alp->lock_holder != LK_NOCPU) {
   1202 		SLOCK_WHERE("simple_lock: uninitialized lock\n",
   1203 		    alp, id, l);
   1204 	}
   1205 	alp->lock_file = id;
   1206 	alp->lock_line = l;
   1207 	alp->lock_holder = cpu_num;
   1208 
   1209 	SLOCK_LIST_LOCK();
   1210 	TAILQ_INSERT_TAIL(&simplelock_list, alp, list);
   1211 	SLOCK_LIST_UNLOCK();
   1212 
   1213 	SLOCK_COUNT(1);
   1214 
   1215  out:
   1216 	splx(s);
   1217 }
   1218 
   1219 int
   1220 _simple_lock_held(volatile struct simplelock *alp)
   1221 {
   1222 #if defined(MULTIPROCESSOR) || defined(DIAGNOSTIC)
   1223 	cpuid_t cpu_num = cpu_number();
   1224 #endif
   1225 	int s, locked = 0;
   1226 
   1227 	s = splhigh();
   1228 
   1229 #if defined(MULTIPROCESSOR)
   1230 	if (__cpu_simple_lock_try(&alp->lock_data) == 0)
   1231 		locked = (alp->lock_holder == cpu_num);
   1232 	else
   1233 		__cpu_simple_unlock(&alp->lock_data);
   1234 #else
   1235 	if (alp->lock_data == __SIMPLELOCK_LOCKED) {
   1236 		locked = 1;
   1237 		KASSERT(alp->lock_holder == cpu_num);
   1238 	}
   1239 #endif
   1240 
   1241 	splx(s);
   1242 
   1243 	return (locked);
   1244 }
   1245 
   1246 int
   1247 _simple_lock_try(volatile struct simplelock *alp, const char *id, int l)
   1248 {
   1249 	cpuid_t cpu_num = cpu_number();
   1250 	int s, rv = 0;
   1251 
   1252 	s = splhigh();
   1253 
   1254 	/*
   1255 	 * MULTIPROCESSOR case: This is `safe' since if it's not us, we
   1256 	 * don't take any action.
   1257 	 */
   1258 #if defined(MULTIPROCESSOR) /* { */
   1259 	if ((rv = __cpu_simple_lock_try(&alp->lock_data)) == 0) {
   1260 		if (alp->lock_holder == cpu_num)
   1261 			SLOCK_WHERE("simple_lock_try: locking against myself\n",
   1262 			    alp, id, l);
   1263 		goto out;
   1264 	}
   1265 #else
   1266 	if (alp->lock_data == __SIMPLELOCK_LOCKED) {
   1267 		SLOCK_WHERE("simple_lock_try: lock held\n", alp, id, l);
   1268 		goto out;
   1269 	}
   1270 	alp->lock_data = __SIMPLELOCK_LOCKED;
   1271 #endif /* MULTIPROCESSOR */ /* } */
   1272 
   1273 	/*
   1274 	 * At this point, we have acquired the lock.
   1275 	 */
   1276 
   1277 	rv = 1;
   1278 
   1279 	alp->lock_file = id;
   1280 	alp->lock_line = l;
   1281 	alp->lock_holder = cpu_num;
   1282 
   1283 	SLOCK_LIST_LOCK();
   1284 	TAILQ_INSERT_TAIL(&simplelock_list, alp, list);
   1285 	SLOCK_LIST_UNLOCK();
   1286 
   1287 	SLOCK_COUNT(1);
   1288 
   1289  out:
   1290 	splx(s);
   1291 	return (rv);
   1292 }
   1293 
   1294 void
   1295 _simple_unlock(volatile struct simplelock *alp, const char *id, int l)
   1296 {
   1297 	int s;
   1298 
   1299 	s = splhigh();
   1300 
   1301 	/*
   1302 	 * MULTIPROCESSOR case: This is `safe' because we think we hold
   1303 	 * the lock, and if we don't, we don't take any action.
   1304 	 */
   1305 	if (alp->lock_data == __SIMPLELOCK_UNLOCKED) {
   1306 		SLOCK_WHERE("simple_unlock: lock not held\n",
   1307 		    alp, id, l);
   1308 		goto out;
   1309 	}
   1310 
   1311 	SLOCK_LIST_LOCK();
   1312 	TAILQ_REMOVE(&simplelock_list, alp, list);
   1313 	SLOCK_LIST_UNLOCK();
   1314 
   1315 	SLOCK_COUNT(-1);
   1316 
   1317 	alp->list.tqe_next = NULL;	/* sanity */
   1318 	alp->list.tqe_prev = NULL;	/* sanity */
   1319 
   1320 	alp->unlock_file = id;
   1321 	alp->unlock_line = l;
   1322 
   1323 #if defined(MULTIPROCESSOR) /* { */
   1324 	alp->lock_holder = LK_NOCPU;
   1325 	/* Now that we've modified all fields, release the lock. */
   1326 	__cpu_simple_unlock(&alp->lock_data);
   1327 #else
   1328 	alp->lock_data = __SIMPLELOCK_UNLOCKED;
   1329 	KASSERT(alp->lock_holder == cpu_number());
   1330 	alp->lock_holder = LK_NOCPU;
   1331 #endif /* } */
   1332 
   1333  out:
   1334 	splx(s);
   1335 }
   1336 
   1337 void
   1338 simple_lock_dump(void)
   1339 {
   1340 	volatile struct simplelock *alp;
   1341 	int s;
   1342 
   1343 	s = splhigh();
   1344 	SLOCK_LIST_LOCK();
   1345 	lock_printf("all simple locks:\n");
   1346 	TAILQ_FOREACH(alp, &simplelock_list, list) {
   1347 		lock_printf("%p CPU %lu %s:%d\n", alp, alp->lock_holder,
   1348 		    alp->lock_file, alp->lock_line);
   1349 	}
   1350 	SLOCK_LIST_UNLOCK();
   1351 	splx(s);
   1352 }
   1353 
   1354 void
   1355 simple_lock_freecheck(void *start, void *end)
   1356 {
   1357 	volatile struct simplelock *alp;
   1358 	int s;
   1359 
   1360 	s = splhigh();
   1361 	SLOCK_LIST_LOCK();
   1362 	TAILQ_FOREACH(alp, &simplelock_list, list) {
   1363 		if ((volatile void *)alp >= start &&
   1364 		    (volatile void *)alp < end) {
   1365 			lock_printf("freeing simple_lock %p CPU %lu %s:%d\n",
   1366 			    alp, alp->lock_holder, alp->lock_file,
   1367 			    alp->lock_line);
   1368 			SLOCK_DEBUGGER();
   1369 		}
   1370 	}
   1371 	SLOCK_LIST_UNLOCK();
   1372 	splx(s);
   1373 }
   1374 
   1375 /*
   1376  * We must be holding exactly one lock: the spc_lock.
   1377  */
   1378 
   1379 void
   1380 simple_lock_switchcheck(void)
   1381 {
   1382 
   1383 	simple_lock_only_held(NULL, "switching");
   1384 }
   1385 
   1386 /*
   1387  * Drop into the debugger if lp isn't the only lock held.
   1388  * lp may be NULL.
   1389  */
   1390 void
   1391 simple_lock_only_held(volatile struct simplelock *lp, const char *where)
   1392 {
   1393 	volatile struct simplelock *alp;
   1394 	cpuid_t cpu_num = cpu_number();
   1395 	int s;
   1396 
   1397 	if (lp) {
   1398 		LOCK_ASSERT(simple_lock_held(lp));
   1399 	}
   1400 	s = splhigh();
   1401 	SLOCK_LIST_LOCK();
   1402 	TAILQ_FOREACH(alp, &simplelock_list, list) {
   1403 		if (alp == lp)
   1404 			continue;
   1405 		if (alp->lock_holder == cpu_num)
   1406 			break;
   1407 	}
   1408 	SLOCK_LIST_UNLOCK();
   1409 	splx(s);
   1410 
   1411 	if (alp != NULL) {
   1412 		lock_printf("\n%s with held simple_lock %p "
   1413 		    "CPU %lu %s:%d\n",
   1414 		    where, alp, alp->lock_holder, alp->lock_file,
   1415 		    alp->lock_line);
   1416 		SLOCK_TRACE();
   1417 		SLOCK_DEBUGGER();
   1418 	}
   1419 }
   1420 
   1421 /*
   1422  * Set to 1 by simple_lock_assert_*().
   1423  * Can be cleared from ddb to avoid a panic.
   1424  */
   1425 int slock_assert_will_panic;
   1426 
   1427 /*
   1428  * If the lock isn't held, print a traceback, optionally drop into the
   1429  *  debugger, then panic.
   1430  * The panic can be avoided by clearing slock_assert_with_panic from the
   1431  *  debugger.
   1432  */
   1433 void
   1434 _simple_lock_assert_locked(volatile struct simplelock *alp,
   1435     const char *lockname, const char *id, int l)
   1436 {
   1437 	if (simple_lock_held(alp) == 0) {
   1438 		slock_assert_will_panic = 1;
   1439 		lock_printf("%s lock not held\n", lockname);
   1440 		SLOCK_WHERE("lock not held", alp, id, l);
   1441 		if (slock_assert_will_panic)
   1442 			panic("%s: not locked", lockname);
   1443 	}
   1444 }
   1445 
   1446 void
   1447 _simple_lock_assert_unlocked(volatile struct simplelock *alp,
   1448     const char *lockname, const char *id, int l)
   1449 {
   1450 	if (simple_lock_held(alp)) {
   1451 		slock_assert_will_panic = 1;
   1452 		lock_printf("%s lock held\n", lockname);
   1453 		SLOCK_WHERE("lock held", alp, id, l);
   1454 		if (slock_assert_will_panic)
   1455 			panic("%s: locked", lockname);
   1456 	}
   1457 }
   1458 
   1459 void
   1460 assert_sleepable(struct simplelock *interlock, const char *msg)
   1461 {
   1462 
   1463 	if (CURCPU_IDLE_P()) {
   1464 		panic("assert_sleepable: idle");
   1465 	}
   1466 	simple_lock_only_held(interlock, msg);
   1467 }
   1468 
   1469 #endif /* LOCKDEBUG */ /* } */
   1470 
   1471 #if defined(MULTIPROCESSOR)
   1472 
   1473 /*
   1474  * Functions for manipulating the kernel_lock.  We put them here
   1475  * so that they show up in profiles.
   1476  */
   1477 
   1478 #define	_KERNEL_LOCK_ABORT(msg)						\
   1479     LOCKDEBUG_ABORT(kernel_lock_id, &kernel_lock, &_kernel_lock_ops,	\
   1480         __FUNCTION__, msg)
   1481 
   1482 #ifdef LOCKDEBUG
   1483 #define	_KERNEL_LOCK_ASSERT(cond)					\
   1484 do {									\
   1485 	if (!(cond))							\
   1486 		_KERNEL_LOCK_ABORT("assertion failed: " #cond);		\
   1487 } while (/* CONSTCOND */ 0)
   1488 #else
   1489 #define	_KERNEL_LOCK_ASSERT(cond)	/* nothing */
   1490 #endif
   1491 
   1492 void	_kernel_lock_dump(volatile void *);
   1493 
   1494 lockops_t _kernel_lock_ops = {
   1495 	"Kernel lock",
   1496 	0,
   1497 	_kernel_lock_dump
   1498 };
   1499 
   1500 /*
   1501  * Initialize the kernel lock.
   1502  */
   1503 void
   1504 _kernel_lock_init(void)
   1505 {
   1506 
   1507 	__cpu_simple_lock_init(&kernel_lock);
   1508 	kernel_lock_id = LOCKDEBUG_ALLOC(&kernel_lock, &_kernel_lock_ops);
   1509 }
   1510 
   1511 /*
   1512  * Print debugging information about the kernel lock.
   1513  */
   1514 void
   1515 _kernel_lock_dump(volatile void *junk)
   1516 {
   1517 	struct cpu_info *ci = curcpu();
   1518 
   1519 	(void)junk;
   1520 
   1521 	printf_nolog("curcpu holds : %18d wanted by: %#018lx\n",
   1522 	    ci->ci_biglock_count, (long)ci->ci_biglock_wanted);
   1523 }
   1524 
   1525 /*
   1526  * Acquire 'nlocks' holds on the kernel lock.  If 'l' is non-null, the
   1527  * acquisition is from process context.
   1528  */
   1529 void
   1530 _kernel_lock(int nlocks, struct lwp *l)
   1531 {
   1532 	struct cpu_info *ci = curcpu();
   1533 	LOCKSTAT_TIMER(spintime);
   1534 	LOCKSTAT_FLAG(lsflag);
   1535 	struct lwp *owant;
   1536 #ifdef LOCKDEBUG
   1537 	u_int spins;
   1538 #endif
   1539 	int s;
   1540 
   1541 	(void)l;
   1542 
   1543 	if (nlocks == 0)
   1544 		return;
   1545 	_KERNEL_LOCK_ASSERT(nlocks > 0);
   1546 
   1547 	s = splsched();	/* XXX splvm() */
   1548 
   1549 	if (ci->ci_biglock_count != 0) {
   1550 		_KERNEL_LOCK_ASSERT(kernel_lock == __SIMPLELOCK_LOCKED);
   1551 		ci->ci_biglock_count += nlocks;
   1552 		splx(s);
   1553 		return;
   1554 	}
   1555 
   1556 	LOCKDEBUG_WANTLOCK(kernel_lock_id,
   1557 	    (uintptr_t)__builtin_return_address(0), 0);
   1558 
   1559 	if (__cpu_simple_lock_try(&kernel_lock)) {
   1560 		ci->ci_biglock_count = nlocks;
   1561 		LOCKDEBUG_LOCKED(kernel_lock_id,
   1562 		    (uintptr_t)__builtin_return_address(0), 0);
   1563 		splx(s);
   1564 		return;
   1565 	}
   1566 
   1567 	LOCKSTAT_ENTER(lsflag);
   1568 	LOCKSTAT_START_TIMER(lsflag, spintime);
   1569 
   1570 	/*
   1571 	 * Before setting ci_biglock_wanted we must post a store
   1572 	 * fence (see kern_mutex.c).  This is accomplished by the
   1573 	 * __cpu_simple_lock_try() above.
   1574 	 */
   1575 	owant = ci->ci_biglock_wanted;
   1576 	ci->ci_biglock_wanted = curlwp;	/* XXXAD */
   1577 
   1578 #ifdef LOCKDEBUG
   1579 	spins = 0;
   1580 #endif
   1581 
   1582 	do {
   1583 		while (kernel_lock == __SIMPLELOCK_LOCKED) {
   1584 #ifdef LOCKDEBUG
   1585 			if (SPINLOCK_SPINOUT(spins))
   1586 				_KERNEL_LOCK_ABORT("spinout");
   1587 #endif
   1588 			splx(s);
   1589 			SPINLOCK_SPIN_HOOK;
   1590 			(void)splsched();	/* XXX splvm() */
   1591 		}
   1592 	} while (!__cpu_simple_lock_try(&kernel_lock));
   1593 
   1594 	ci->ci_biglock_wanted = owant;
   1595 	ci->ci_biglock_count += nlocks;
   1596 	LOCKSTAT_STOP_TIMER(lsflag, spintime);
   1597 	LOCKDEBUG_LOCKED(kernel_lock_id,
   1598 	    (uintptr_t)__builtin_return_address(0), 0);
   1599 	splx(s);
   1600 
   1601 	/*
   1602 	 * Again, another store fence is required (see kern_mutex.c).
   1603 	 */
   1604 	mb_write();
   1605 	if (owant == NULL) {
   1606 		LOCKSTAT_EVENT(lsflag, &kernel_lock, LB_KERNEL_LOCK | LB_SPIN,
   1607 		    1, spintime);
   1608 	}
   1609 	LOCKSTAT_EXIT(lsflag);
   1610 }
   1611 
   1612 /*
   1613  * Release 'nlocks' holds on the kernel lock.  If 'nlocks' is zero, release
   1614  * all holds.  If 'l' is non-null, the release is from process context.
   1615  */
   1616 void
   1617 _kernel_unlock(int nlocks, struct lwp *l, int *countp)
   1618 {
   1619 	struct cpu_info *ci = curcpu();
   1620 	u_int olocks;
   1621 	int s;
   1622 
   1623 	(void)l;
   1624 
   1625 	_KERNEL_LOCK_ASSERT(nlocks < 2);
   1626 
   1627 	olocks = ci->ci_biglock_count;
   1628 
   1629 	if (olocks == 0) {
   1630 		_KERNEL_LOCK_ASSERT(nlocks <= 0);
   1631 		if (countp != NULL)
   1632 			*countp = 0;
   1633 		return;
   1634 	}
   1635 
   1636 	_KERNEL_LOCK_ASSERT(kernel_lock == __SIMPLELOCK_LOCKED);
   1637 
   1638 	if (nlocks == 0)
   1639 		nlocks = olocks;
   1640 	else if (nlocks == -1) {
   1641 		nlocks = 1;
   1642 		_KERNEL_LOCK_ASSERT(olocks == 1);
   1643 	}
   1644 
   1645 	s = splsched();	/* XXX splvm() */
   1646 	if ((ci->ci_biglock_count -= nlocks) == 0) {
   1647 		LOCKDEBUG_UNLOCKED(kernel_lock_id,
   1648 		    (uintptr_t)__builtin_return_address(0), 0);
   1649 		__cpu_simple_unlock(&kernel_lock);
   1650 	}
   1651 	splx(s);
   1652 
   1653 	if (countp != NULL)
   1654 		*countp = olocks;
   1655 }
   1656 
   1657 #if defined(DEBUG)
   1658 /*
   1659  * Assert that the kernel lock is held.
   1660  */
   1661 void
   1662 _kernel_lock_assert_locked(void)
   1663 {
   1664 
   1665 	if (kernel_lock != __SIMPLELOCK_LOCKED ||
   1666 	    curcpu()->ci_biglock_count == 0)
   1667 		_KERNEL_LOCK_ABORT("not locked");
   1668 }
   1669 
   1670 void
   1671 _kernel_lock_assert_unlocked()
   1672 {
   1673 
   1674 	if (curcpu()->ci_biglock_count != 0)
   1675 		_KERNEL_LOCK_ABORT("locked");
   1676 }
   1677 #endif
   1678 
   1679 #endif	/* MULTIPROCESSOR || LOCKDEBUG */
   1680