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