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