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