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