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