Home | History | Annotate | Line # | Download | only in kern
kern_lock.c revision 1.32.2.2
      1  1.32.2.2   thorpej /*	$NetBSD: kern_lock.c,v 1.32.2.2 2000/08/11 23:10:15 thorpej Exp $	*/
      2      1.19   thorpej 
      3      1.19   thorpej /*-
      4      1.28   thorpej  * Copyright (c) 1999, 2000 The NetBSD Foundation, Inc.
      5      1.19   thorpej  * All rights reserved.
      6      1.19   thorpej  *
      7      1.19   thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8      1.19   thorpej  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9      1.19   thorpej  * NASA Ames Research Center.
     10      1.19   thorpej  *
     11      1.19   thorpej  * This code is derived from software contributed to The NetBSD Foundation
     12      1.19   thorpej  * by Ross Harvey.
     13      1.19   thorpej  *
     14      1.19   thorpej  * Redistribution and use in source and binary forms, with or without
     15      1.19   thorpej  * modification, are permitted provided that the following conditions
     16      1.19   thorpej  * are met:
     17      1.19   thorpej  * 1. Redistributions of source code must retain the above copyright
     18      1.19   thorpej  *    notice, this list of conditions and the following disclaimer.
     19      1.19   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     20      1.19   thorpej  *    notice, this list of conditions and the following disclaimer in the
     21      1.19   thorpej  *    documentation and/or other materials provided with the distribution.
     22      1.19   thorpej  * 3. All advertising materials mentioning features or use of this software
     23      1.19   thorpej  *    must display the following acknowledgement:
     24      1.19   thorpej  *	This product includes software developed by the NetBSD
     25      1.19   thorpej  *	Foundation, Inc. and its contributors.
     26      1.19   thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     27      1.19   thorpej  *    contributors may be used to endorse or promote products derived
     28      1.19   thorpej  *    from this software without specific prior written permission.
     29      1.19   thorpej  *
     30      1.19   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     31      1.19   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     32      1.19   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     33      1.19   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     34      1.19   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     35      1.19   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     36      1.19   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     37      1.19   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     38      1.19   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     39      1.19   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     40      1.19   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     41      1.19   thorpej  */
     42       1.2      fvdl 
     43       1.1      fvdl /*
     44       1.1      fvdl  * Copyright (c) 1995
     45       1.1      fvdl  *	The Regents of the University of California.  All rights reserved.
     46       1.1      fvdl  *
     47       1.1      fvdl  * This code contains ideas from software contributed to Berkeley by
     48       1.1      fvdl  * Avadis Tevanian, Jr., Michael Wayne Young, and the Mach Operating
     49       1.1      fvdl  * System project at Carnegie-Mellon University.
     50       1.1      fvdl  *
     51       1.1      fvdl  * Redistribution and use in source and binary forms, with or without
     52       1.1      fvdl  * modification, are permitted provided that the following conditions
     53       1.1      fvdl  * are met:
     54       1.1      fvdl  * 1. Redistributions of source code must retain the above copyright
     55       1.1      fvdl  *    notice, this list of conditions and the following disclaimer.
     56       1.1      fvdl  * 2. Redistributions in binary form must reproduce the above copyright
     57       1.1      fvdl  *    notice, this list of conditions and the following disclaimer in the
     58       1.1      fvdl  *    documentation and/or other materials provided with the distribution.
     59       1.1      fvdl  * 3. All advertising materials mentioning features or use of this software
     60       1.1      fvdl  *    must display the following acknowledgement:
     61       1.1      fvdl  *	This product includes software developed by the University of
     62       1.1      fvdl  *	California, Berkeley and its contributors.
     63       1.1      fvdl  * 4. Neither the name of the University nor the names of its contributors
     64       1.1      fvdl  *    may be used to endorse or promote products derived from this software
     65       1.1      fvdl  *    without specific prior written permission.
     66       1.1      fvdl  *
     67       1.1      fvdl  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     68       1.1      fvdl  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     69       1.1      fvdl  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     70       1.1      fvdl  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     71       1.1      fvdl  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     72       1.1      fvdl  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     73       1.1      fvdl  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     74       1.1      fvdl  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     75       1.1      fvdl  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     76       1.1      fvdl  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     77       1.1      fvdl  * SUCH DAMAGE.
     78       1.1      fvdl  *
     79       1.1      fvdl  *	@(#)kern_lock.c	8.18 (Berkeley) 5/21/95
     80       1.1      fvdl  */
     81       1.7   thorpej 
     82      1.21   thorpej #include "opt_multiprocessor.h"
     83       1.7   thorpej #include "opt_lockdebug.h"
     84      1.18       chs #include "opt_ddb.h"
     85       1.1      fvdl 
     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.1      fvdl #include <machine/cpu.h>
     91       1.1      fvdl 
     92      1.28   thorpej #if defined(__HAVE_ATOMIC_OPERATIONS)
     93      1.28   thorpej #include <machine/atomic.h>
     94      1.28   thorpej #endif
     95      1.28   thorpej 
     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.25   thorpej void	lock_printf __P((const char *fmt, ...));
    106      1.25   thorpej 
    107      1.25   thorpej int	lock_debug_syslog = 0;	/* defaults to printf, but can be patched */
    108      1.25   thorpej #endif
    109      1.25   thorpej 
    110       1.1      fvdl /*
    111       1.1      fvdl  * Locking primitives implementation.
    112       1.1      fvdl  * Locks provide shared/exclusive sychronization.
    113       1.1      fvdl  */
    114       1.1      fvdl 
    115      1.21   thorpej #if defined(LOCKDEBUG) || defined(DIAGNOSTIC) /* { */
    116      1.21   thorpej #if defined(MULTIPROCESSOR) /* { */
    117      1.28   thorpej #if defined(__HAVE_ATOMIC_OPERATIONS) /* { */
    118      1.21   thorpej #define	COUNT_CPU(cpu_id, x)						\
    119      1.30   thorpej 	atomic_add_ulong(&curcpu()->ci_spin_locks, (x))
    120      1.28   thorpej #else
    121      1.28   thorpej #define	COUNT_CPU(cpu_id, x)	/* not safe */
    122      1.28   thorpej #endif /* __HAVE_ATOMIC_OPERATIONS */ /* } */
    123      1.21   thorpej #else
    124      1.21   thorpej u_long	spin_locks;
    125      1.21   thorpej #define	COUNT_CPU(cpu_id, x)	spin_locks += (x)
    126      1.21   thorpej #endif /* MULTIPROCESSOR */ /* } */
    127      1.21   thorpej 
    128      1.21   thorpej #define	COUNT(lkp, p, cpu_id, x)					\
    129      1.21   thorpej do {									\
    130      1.21   thorpej 	if ((lkp)->lk_flags & LK_SPIN)					\
    131      1.21   thorpej 		COUNT_CPU((cpu_id), (x));				\
    132      1.21   thorpej 	else								\
    133      1.21   thorpej 		(p)->p_locks += (x);					\
    134      1.30   thorpej } while (/*CONSTCOND*/0)
    135       1.1      fvdl #else
    136      1.22    mellon #define COUNT(lkp, p, cpu_id, x)
    137      1.21   thorpej #endif /* LOCKDEBUG || DIAGNOSTIC */ /* } */
    138       1.1      fvdl 
    139       1.1      fvdl /*
    140       1.1      fvdl  * Acquire a resource.
    141       1.1      fvdl  */
    142      1.23   thorpej #define ACQUIRE(lkp, error, extflags, drain, wanted)			\
    143      1.19   thorpej 	if ((extflags) & LK_SPIN) {					\
    144      1.19   thorpej 		int interlocked;					\
    145      1.19   thorpej 									\
    146      1.23   thorpej 		if ((drain) == 0)					\
    147      1.23   thorpej 			(lkp)->lk_waitcount++;				\
    148      1.19   thorpej 		for (interlocked = 1;;) {				\
    149      1.19   thorpej 			if (wanted) {					\
    150      1.19   thorpej 				if (interlocked) {			\
    151      1.19   thorpej 					simple_unlock(&(lkp)->lk_interlock); \
    152      1.19   thorpej 					interlocked = 0;		\
    153      1.19   thorpej 				}					\
    154      1.19   thorpej 			} else if (interlocked) {			\
    155      1.19   thorpej 				break;					\
    156      1.19   thorpej 			} else {					\
    157      1.19   thorpej 				simple_lock(&(lkp)->lk_interlock);	\
    158      1.19   thorpej 				interlocked = 1;			\
    159      1.19   thorpej 			}						\
    160      1.19   thorpej 		}							\
    161      1.23   thorpej 		if ((drain) == 0)					\
    162      1.23   thorpej 			(lkp)->lk_waitcount--;				\
    163      1.19   thorpej 		KASSERT((wanted) == 0);					\
    164      1.19   thorpej 		error = 0;	/* sanity */				\
    165      1.19   thorpej 	} else {							\
    166      1.19   thorpej 		for (error = 0; wanted; ) {				\
    167      1.23   thorpej 			if ((drain))					\
    168      1.23   thorpej 				(lkp)->lk_flags |= LK_WAITDRAIN;	\
    169      1.23   thorpej 			else						\
    170      1.23   thorpej 				(lkp)->lk_waitcount++;			\
    171      1.23   thorpej 			/* XXX Cast away volatile. */			\
    172      1.31   thorpej 			error = ltsleep((drain) ? &(lkp)->lk_flags :	\
    173      1.23   thorpej 			    (void *)(lkp), (lkp)->lk_prio,		\
    174      1.31   thorpej 			    (lkp)->lk_wmesg, (lkp)->lk_timo,		\
    175      1.31   thorpej 			    &(lkp)->lk_interlock);			\
    176      1.23   thorpej 			if ((drain) == 0)				\
    177      1.23   thorpej 				(lkp)->lk_waitcount--;			\
    178      1.19   thorpej 			if (error)					\
    179      1.19   thorpej 				break;					\
    180      1.19   thorpej 			if ((extflags) & LK_SLEEPFAIL) {		\
    181      1.19   thorpej 				error = ENOLCK;				\
    182      1.19   thorpej 				break;					\
    183      1.19   thorpej 			}						\
    184       1.1      fvdl 		}							\
    185       1.1      fvdl 	}
    186       1.1      fvdl 
    187      1.19   thorpej #define	SETHOLDER(lkp, pid, cpu_id)					\
    188      1.19   thorpej do {									\
    189      1.19   thorpej 	if ((lkp)->lk_flags & LK_SPIN)					\
    190      1.19   thorpej 		(lkp)->lk_cpu = cpu_id;					\
    191      1.19   thorpej 	else								\
    192      1.19   thorpej 		(lkp)->lk_lockholder = pid;				\
    193      1.30   thorpej } while (/*CONSTCOND*/0)
    194      1.19   thorpej 
    195      1.19   thorpej #define	WEHOLDIT(lkp, pid, cpu_id)					\
    196      1.19   thorpej 	(((lkp)->lk_flags & LK_SPIN) != 0 ?				\
    197      1.19   thorpej 	 ((lkp)->lk_cpu == (cpu_id)) : ((lkp)->lk_lockholder == (pid)))
    198      1.19   thorpej 
    199      1.23   thorpej #define	WAKEUP_WAITER(lkp)						\
    200      1.23   thorpej do {									\
    201      1.23   thorpej 	if (((lkp)->lk_flags & LK_SPIN) == 0 && (lkp)->lk_waitcount) {	\
    202      1.23   thorpej 		/* XXX Cast away volatile. */				\
    203      1.23   thorpej 		wakeup_one((void *)(lkp));				\
    204      1.23   thorpej 	}								\
    205      1.30   thorpej } while (/*CONSTCOND*/0)
    206      1.23   thorpej 
    207      1.21   thorpej #if defined(LOCKDEBUG) /* { */
    208      1.21   thorpej #if defined(MULTIPROCESSOR) /* { */
    209      1.21   thorpej struct simplelock spinlock_list_slock = SIMPLELOCK_INITIALIZER;
    210      1.21   thorpej 
    211      1.27   thorpej #define	SPINLOCK_LIST_LOCK()						\
    212      1.29  sommerfe 	__cpu_simple_lock(&spinlock_list_slock.lock_data)
    213      1.21   thorpej 
    214      1.27   thorpej #define	SPINLOCK_LIST_UNLOCK()						\
    215      1.29  sommerfe 	__cpu_simple_unlock(&spinlock_list_slock.lock_data)
    216      1.21   thorpej #else
    217      1.21   thorpej #define	SPINLOCK_LIST_LOCK()	/* nothing */
    218      1.21   thorpej 
    219      1.21   thorpej #define	SPINLOCK_LIST_UNLOCK()	/* nothing */
    220      1.21   thorpej #endif /* MULTIPROCESSOR */ /* } */
    221      1.21   thorpej 
    222      1.21   thorpej TAILQ_HEAD(, lock) spinlock_list =
    223      1.21   thorpej     TAILQ_HEAD_INITIALIZER(spinlock_list);
    224      1.21   thorpej 
    225      1.21   thorpej #define	HAVEIT(lkp)							\
    226      1.21   thorpej do {									\
    227      1.21   thorpej 	if ((lkp)->lk_flags & LK_SPIN) {				\
    228      1.21   thorpej 		int s = splhigh();					\
    229      1.21   thorpej 		SPINLOCK_LIST_LOCK();					\
    230      1.21   thorpej 		/* XXX Cast away volatile. */				\
    231      1.21   thorpej 		TAILQ_INSERT_TAIL(&spinlock_list, (struct lock *)(lkp),	\
    232      1.21   thorpej 		    lk_list);						\
    233      1.21   thorpej 		SPINLOCK_LIST_UNLOCK();					\
    234      1.21   thorpej 		splx(s);						\
    235      1.21   thorpej 	}								\
    236      1.30   thorpej } while (/*CONSTCOND*/0)
    237      1.21   thorpej 
    238      1.21   thorpej #define	DONTHAVEIT(lkp)							\
    239      1.21   thorpej do {									\
    240      1.21   thorpej 	if ((lkp)->lk_flags & LK_SPIN) {				\
    241      1.21   thorpej 		int s = splhigh();					\
    242      1.21   thorpej 		SPINLOCK_LIST_LOCK();					\
    243      1.21   thorpej 		/* XXX Cast away volatile. */				\
    244      1.21   thorpej 		TAILQ_REMOVE(&spinlock_list, (struct lock *)(lkp),	\
    245      1.21   thorpej 		    lk_list);						\
    246      1.21   thorpej 		SPINLOCK_LIST_UNLOCK();					\
    247      1.21   thorpej 		splx(s);						\
    248      1.21   thorpej 	}								\
    249      1.30   thorpej } while (/*CONSTCOND*/0)
    250      1.21   thorpej #else
    251      1.21   thorpej #define	HAVEIT(lkp)		/* nothing */
    252      1.21   thorpej 
    253      1.21   thorpej #define	DONTHAVEIT(lkp)		/* nothing */
    254      1.21   thorpej #endif /* LOCKDEBUG */ /* } */
    255      1.21   thorpej 
    256      1.25   thorpej #if defined(LOCKDEBUG)
    257      1.25   thorpej /*
    258      1.25   thorpej  * Lock debug printing routine; can be configured to print to console
    259      1.25   thorpej  * or log to syslog.
    260      1.25   thorpej  */
    261      1.25   thorpej void
    262      1.25   thorpej #ifdef __STDC__
    263      1.25   thorpej lock_printf(const char *fmt, ...)
    264      1.25   thorpej #else
    265      1.25   thorpej lock_printf(fmt, va_alist)
    266      1.25   thorpej 	char *fmt;
    267      1.25   thorpej 	va_dcl
    268      1.25   thorpej #endif
    269      1.25   thorpej {
    270      1.25   thorpej 	va_list ap;
    271      1.25   thorpej 
    272      1.25   thorpej 	va_start(ap, fmt);
    273      1.25   thorpej 	if (lock_debug_syslog)
    274      1.25   thorpej 		vlog(LOG_DEBUG, fmt, ap);
    275      1.25   thorpej 	else
    276      1.25   thorpej 		vprintf(fmt, ap);
    277      1.25   thorpej 	va_end(ap);
    278      1.25   thorpej }
    279      1.25   thorpej #endif /* LOCKDEBUG */
    280      1.25   thorpej 
    281       1.1      fvdl /*
    282       1.1      fvdl  * Initialize a lock; required before use.
    283       1.1      fvdl  */
    284       1.1      fvdl void
    285       1.1      fvdl lockinit(lkp, prio, wmesg, timo, flags)
    286       1.1      fvdl 	struct lock *lkp;
    287       1.1      fvdl 	int prio;
    288       1.4   mycroft 	const char *wmesg;
    289       1.1      fvdl 	int timo;
    290       1.1      fvdl 	int flags;
    291       1.1      fvdl {
    292       1.1      fvdl 
    293       1.8     perry 	memset(lkp, 0, sizeof(struct lock));
    294       1.1      fvdl 	simple_lock_init(&lkp->lk_interlock);
    295       1.1      fvdl 	lkp->lk_flags = flags & LK_EXTFLG_MASK;
    296      1.19   thorpej 	if (flags & LK_SPIN)
    297      1.19   thorpej 		lkp->lk_cpu = LK_NOCPU;
    298      1.19   thorpej 	else {
    299      1.19   thorpej 		lkp->lk_lockholder = LK_NOPROC;
    300      1.19   thorpej 		lkp->lk_prio = prio;
    301      1.19   thorpej 		lkp->lk_timo = timo;
    302      1.19   thorpej 	}
    303      1.19   thorpej 	lkp->lk_wmesg = wmesg;	/* just a name for spin locks */
    304       1.1      fvdl }
    305       1.1      fvdl 
    306       1.1      fvdl /*
    307       1.1      fvdl  * Determine the status of a lock.
    308       1.1      fvdl  */
    309       1.1      fvdl int
    310       1.1      fvdl lockstatus(lkp)
    311       1.1      fvdl 	struct lock *lkp;
    312       1.1      fvdl {
    313       1.1      fvdl 	int lock_type = 0;
    314       1.1      fvdl 
    315       1.1      fvdl 	simple_lock(&lkp->lk_interlock);
    316       1.1      fvdl 	if (lkp->lk_exclusivecount != 0)
    317       1.1      fvdl 		lock_type = LK_EXCLUSIVE;
    318       1.1      fvdl 	else if (lkp->lk_sharecount != 0)
    319       1.1      fvdl 		lock_type = LK_SHARED;
    320       1.1      fvdl 	simple_unlock(&lkp->lk_interlock);
    321       1.1      fvdl 	return (lock_type);
    322       1.1      fvdl }
    323  1.32.2.2   thorpej 
    324  1.32.2.2   thorpej #if defined(LOCKDEBUG) || defined(DIAGNOSTIC)
    325  1.32.2.2   thorpej /*
    326  1.32.2.2   thorpej  * Make sure no spin locks are held by a CPU that is about
    327  1.32.2.2   thorpej  * to context switch.
    328  1.32.2.2   thorpej  */
    329  1.32.2.2   thorpej void
    330  1.32.2.2   thorpej spinlock_switchcheck(void)
    331  1.32.2.2   thorpej {
    332  1.32.2.2   thorpej 	u_long cnt;
    333  1.32.2.2   thorpej 	int s;
    334  1.32.2.2   thorpej 
    335  1.32.2.2   thorpej 	s = splhigh();
    336  1.32.2.2   thorpej #if defined(MULTIPROCESSOR)
    337  1.32.2.2   thorpej 	cnt = curcpu()->ci_spin_locks;
    338  1.32.2.2   thorpej #else
    339  1.32.2.2   thorpej 	cnt = spin_locks;
    340  1.32.2.2   thorpej #endif
    341  1.32.2.2   thorpej 	splx(s);
    342  1.32.2.2   thorpej 
    343  1.32.2.2   thorpej 	if (cnt != 0)
    344  1.32.2.2   thorpej 		panic("spinlock_switchcheck: CPU %lu has %lu spin locks",
    345  1.32.2.2   thorpej 		    (u_long) cpu_number(), cnt);
    346  1.32.2.2   thorpej }
    347  1.32.2.2   thorpej #endif /* LOCKDEBUG || DIAGNOSTIC */
    348       1.1      fvdl 
    349       1.1      fvdl /*
    350      1.32  sommerfe  * XXX XXX kludge around another kludge..
    351      1.32  sommerfe  *
    352      1.32  sommerfe  * vfs_shutdown() may be called from interrupt context, either as a result
    353      1.32  sommerfe  * of a panic, or from the debugger.   It proceeds to call
    354      1.32  sommerfe  * sys_sync(&proc0, ...), pretending its running on behalf of proc0
    355      1.32  sommerfe  *
    356      1.32  sommerfe  * We would like to make an attempt to sync the filesystems in this case, so
    357      1.32  sommerfe  * if this happens, we treat attempts to acquire locks specially.
    358      1.32  sommerfe  * All locks are acquired on behalf of proc0.
    359      1.32  sommerfe  *
    360      1.32  sommerfe  * If we've already paniced, we don't block waiting for locks, but
    361      1.32  sommerfe  * just barge right ahead since we're already going down in flames.
    362      1.32  sommerfe  */
    363      1.32  sommerfe 
    364      1.32  sommerfe /*
    365       1.1      fvdl  * Set, change, or release a lock.
    366       1.1      fvdl  *
    367       1.1      fvdl  * Shared requests increment the shared count. Exclusive requests set the
    368       1.1      fvdl  * LK_WANT_EXCL flag (preventing further shared locks), and wait for already
    369       1.1      fvdl  * accepted shared locks and shared-to-exclusive upgrades to go away.
    370       1.1      fvdl  */
    371       1.1      fvdl int
    372       1.6      fvdl lockmgr(lkp, flags, interlkp)
    373       1.1      fvdl 	__volatile struct lock *lkp;
    374       1.1      fvdl 	u_int flags;
    375       1.1      fvdl 	struct simplelock *interlkp;
    376       1.1      fvdl {
    377       1.1      fvdl 	int error;
    378       1.1      fvdl 	pid_t pid;
    379       1.1      fvdl 	int extflags;
    380      1.24   thorpej 	cpuid_t cpu_id;
    381       1.6      fvdl 	struct proc *p = curproc;
    382      1.32  sommerfe 	int lock_shutdown_noblock = 0;
    383       1.1      fvdl 
    384       1.1      fvdl 	error = 0;
    385      1.19   thorpej 
    386       1.1      fvdl 	simple_lock(&lkp->lk_interlock);
    387       1.1      fvdl 	if (flags & LK_INTERLOCK)
    388       1.1      fvdl 		simple_unlock(interlkp);
    389       1.1      fvdl 	extflags = (flags | lkp->lk_flags) & LK_EXTFLG_MASK;
    390      1.19   thorpej 
    391      1.21   thorpej #ifdef DIAGNOSTIC /* { */
    392      1.19   thorpej 	/*
    393      1.19   thorpej 	 * Don't allow spins on sleep locks and don't allow sleeps
    394      1.19   thorpej 	 * on spin locks.
    395      1.19   thorpej 	 */
    396      1.19   thorpej 	if ((flags ^ lkp->lk_flags) & LK_SPIN)
    397      1.19   thorpej 		panic("lockmgr: sleep/spin mismatch\n");
    398      1.21   thorpej #endif /* } */
    399      1.19   thorpej 
    400      1.19   thorpej 	if (extflags & LK_SPIN)
    401      1.19   thorpej 		pid = LK_KERNPROC;
    402      1.19   thorpej 	else {
    403      1.32  sommerfe 		if (p == NULL) {
    404      1.32  sommerfe 			if (!doing_shutdown) {
    405      1.32  sommerfe #ifdef DIAGNOSTIC
    406      1.32  sommerfe 				panic("lockmgr: no context");
    407      1.32  sommerfe #endif
    408      1.32  sommerfe 			} else {
    409      1.32  sommerfe 				p = &proc0;
    410      1.32  sommerfe 				if (panicstr && (!(flags & LK_NOWAIT))) {
    411      1.32  sommerfe 					flags |= LK_NOWAIT;
    412      1.32  sommerfe 					lock_shutdown_noblock = 1;
    413      1.32  sommerfe 				}
    414      1.32  sommerfe 			}
    415      1.32  sommerfe 		}
    416      1.19   thorpej 		pid = p->p_pid;
    417      1.19   thorpej 	}
    418      1.24   thorpej 	cpu_id = cpu_number();
    419      1.19   thorpej 
    420       1.1      fvdl 	/*
    421       1.1      fvdl 	 * Once a lock has drained, the LK_DRAINING flag is set and an
    422       1.1      fvdl 	 * exclusive lock is returned. The only valid operation thereafter
    423       1.1      fvdl 	 * is a single release of that exclusive lock. This final release
    424       1.1      fvdl 	 * clears the LK_DRAINING flag and sets the LK_DRAINED flag. Any
    425       1.1      fvdl 	 * further requests of any sort will result in a panic. The bits
    426       1.1      fvdl 	 * selected for these two flags are chosen so that they will be set
    427       1.1      fvdl 	 * in memory that is freed (freed memory is filled with 0xdeadbeef).
    428       1.1      fvdl 	 * The final release is permitted to give a new lease on life to
    429       1.1      fvdl 	 * the lock by specifying LK_REENABLE.
    430       1.1      fvdl 	 */
    431       1.1      fvdl 	if (lkp->lk_flags & (LK_DRAINING|LK_DRAINED)) {
    432      1.28   thorpej #ifdef DIAGNOSTIC /* { */
    433       1.1      fvdl 		if (lkp->lk_flags & LK_DRAINED)
    434       1.1      fvdl 			panic("lockmgr: using decommissioned lock");
    435       1.1      fvdl 		if ((flags & LK_TYPE_MASK) != LK_RELEASE ||
    436      1.19   thorpej 		    WEHOLDIT(lkp, pid, cpu_id) == 0)
    437       1.1      fvdl 			panic("lockmgr: non-release on draining lock: %d\n",
    438       1.1      fvdl 			    flags & LK_TYPE_MASK);
    439      1.28   thorpej #endif /* DIAGNOSTIC */ /* } */
    440       1.1      fvdl 		lkp->lk_flags &= ~LK_DRAINING;
    441       1.1      fvdl 		if ((flags & LK_REENABLE) == 0)
    442       1.1      fvdl 			lkp->lk_flags |= LK_DRAINED;
    443       1.1      fvdl 	}
    444       1.1      fvdl 
    445       1.1      fvdl 	switch (flags & LK_TYPE_MASK) {
    446       1.1      fvdl 
    447       1.1      fvdl 	case LK_SHARED:
    448      1.19   thorpej 		if (WEHOLDIT(lkp, pid, cpu_id) == 0) {
    449       1.1      fvdl 			/*
    450       1.1      fvdl 			 * If just polling, check to see if we will block.
    451       1.1      fvdl 			 */
    452       1.1      fvdl 			if ((extflags & LK_NOWAIT) && (lkp->lk_flags &
    453       1.1      fvdl 			    (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE))) {
    454       1.1      fvdl 				error = EBUSY;
    455       1.1      fvdl 				break;
    456       1.1      fvdl 			}
    457       1.1      fvdl 			/*
    458       1.1      fvdl 			 * Wait for exclusive locks and upgrades to clear.
    459       1.1      fvdl 			 */
    460      1.23   thorpej 			ACQUIRE(lkp, error, extflags, 0, lkp->lk_flags &
    461       1.1      fvdl 			    (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE));
    462       1.1      fvdl 			if (error)
    463       1.1      fvdl 				break;
    464       1.1      fvdl 			lkp->lk_sharecount++;
    465      1.21   thorpej 			COUNT(lkp, p, cpu_id, 1);
    466       1.1      fvdl 			break;
    467       1.1      fvdl 		}
    468       1.1      fvdl 		/*
    469       1.1      fvdl 		 * We hold an exclusive lock, so downgrade it to shared.
    470       1.1      fvdl 		 * An alternative would be to fail with EDEADLK.
    471       1.1      fvdl 		 */
    472       1.1      fvdl 		lkp->lk_sharecount++;
    473      1.21   thorpej 		COUNT(lkp, p, cpu_id, 1);
    474       1.1      fvdl 		/* fall into downgrade */
    475       1.1      fvdl 
    476       1.1      fvdl 	case LK_DOWNGRADE:
    477      1.19   thorpej 		if (WEHOLDIT(lkp, pid, cpu_id) == 0 ||
    478      1.19   thorpej 		    lkp->lk_exclusivecount == 0)
    479       1.1      fvdl 			panic("lockmgr: not holding exclusive lock");
    480       1.1      fvdl 		lkp->lk_sharecount += lkp->lk_exclusivecount;
    481       1.1      fvdl 		lkp->lk_exclusivecount = 0;
    482      1.15      fvdl 		lkp->lk_recurselevel = 0;
    483       1.1      fvdl 		lkp->lk_flags &= ~LK_HAVE_EXCL;
    484      1.19   thorpej 		SETHOLDER(lkp, LK_NOPROC, LK_NOCPU);
    485      1.21   thorpej 		DONTHAVEIT(lkp);
    486      1.23   thorpej 		WAKEUP_WAITER(lkp);
    487       1.1      fvdl 		break;
    488       1.1      fvdl 
    489       1.1      fvdl 	case LK_EXCLUPGRADE:
    490       1.1      fvdl 		/*
    491       1.1      fvdl 		 * If another process is ahead of us to get an upgrade,
    492       1.1      fvdl 		 * then we want to fail rather than have an intervening
    493       1.1      fvdl 		 * exclusive access.
    494       1.1      fvdl 		 */
    495       1.1      fvdl 		if (lkp->lk_flags & LK_WANT_UPGRADE) {
    496       1.1      fvdl 			lkp->lk_sharecount--;
    497      1.21   thorpej 			COUNT(lkp, p, cpu_id, -1);
    498       1.1      fvdl 			error = EBUSY;
    499       1.1      fvdl 			break;
    500       1.1      fvdl 		}
    501       1.1      fvdl 		/* fall into normal upgrade */
    502       1.1      fvdl 
    503       1.1      fvdl 	case LK_UPGRADE:
    504       1.1      fvdl 		/*
    505       1.1      fvdl 		 * Upgrade a shared lock to an exclusive one. If another
    506       1.1      fvdl 		 * shared lock has already requested an upgrade to an
    507       1.1      fvdl 		 * exclusive lock, our shared lock is released and an
    508       1.1      fvdl 		 * exclusive lock is requested (which will be granted
    509       1.1      fvdl 		 * after the upgrade). If we return an error, the file
    510       1.1      fvdl 		 * will always be unlocked.
    511       1.1      fvdl 		 */
    512      1.19   thorpej 		if (WEHOLDIT(lkp, pid, cpu_id) || lkp->lk_sharecount <= 0)
    513       1.1      fvdl 			panic("lockmgr: upgrade exclusive lock");
    514       1.1      fvdl 		lkp->lk_sharecount--;
    515      1.21   thorpej 		COUNT(lkp, p, cpu_id, -1);
    516       1.1      fvdl 		/*
    517       1.1      fvdl 		 * If we are just polling, check to see if we will block.
    518       1.1      fvdl 		 */
    519       1.1      fvdl 		if ((extflags & LK_NOWAIT) &&
    520       1.1      fvdl 		    ((lkp->lk_flags & LK_WANT_UPGRADE) ||
    521       1.1      fvdl 		     lkp->lk_sharecount > 1)) {
    522       1.1      fvdl 			error = EBUSY;
    523       1.1      fvdl 			break;
    524       1.1      fvdl 		}
    525       1.1      fvdl 		if ((lkp->lk_flags & LK_WANT_UPGRADE) == 0) {
    526       1.1      fvdl 			/*
    527       1.1      fvdl 			 * We are first shared lock to request an upgrade, so
    528       1.1      fvdl 			 * request upgrade and wait for the shared count to
    529       1.1      fvdl 			 * drop to zero, then take exclusive lock.
    530       1.1      fvdl 			 */
    531       1.1      fvdl 			lkp->lk_flags |= LK_WANT_UPGRADE;
    532      1.23   thorpej 			ACQUIRE(lkp, error, extflags, 0, lkp->lk_sharecount);
    533       1.1      fvdl 			lkp->lk_flags &= ~LK_WANT_UPGRADE;
    534       1.1      fvdl 			if (error)
    535       1.1      fvdl 				break;
    536       1.1      fvdl 			lkp->lk_flags |= LK_HAVE_EXCL;
    537      1.19   thorpej 			SETHOLDER(lkp, pid, cpu_id);
    538      1.21   thorpej 			HAVEIT(lkp);
    539       1.1      fvdl 			if (lkp->lk_exclusivecount != 0)
    540       1.1      fvdl 				panic("lockmgr: non-zero exclusive count");
    541       1.1      fvdl 			lkp->lk_exclusivecount = 1;
    542      1.15      fvdl 			if (extflags & LK_SETRECURSE)
    543      1.15      fvdl 				lkp->lk_recurselevel = 1;
    544      1.21   thorpej 			COUNT(lkp, p, cpu_id, 1);
    545       1.1      fvdl 			break;
    546       1.1      fvdl 		}
    547       1.1      fvdl 		/*
    548       1.1      fvdl 		 * Someone else has requested upgrade. Release our shared
    549       1.1      fvdl 		 * lock, awaken upgrade requestor if we are the last shared
    550       1.1      fvdl 		 * lock, then request an exclusive lock.
    551       1.1      fvdl 		 */
    552      1.23   thorpej 		if (lkp->lk_sharecount == 0)
    553      1.23   thorpej 			WAKEUP_WAITER(lkp);
    554       1.1      fvdl 		/* fall into exclusive request */
    555       1.1      fvdl 
    556       1.1      fvdl 	case LK_EXCLUSIVE:
    557      1.19   thorpej 		if (WEHOLDIT(lkp, pid, cpu_id)) {
    558       1.1      fvdl 			/*
    559      1.19   thorpej 			 * Recursive lock.
    560       1.1      fvdl 			 */
    561      1.15      fvdl 			if ((extflags & LK_CANRECURSE) == 0 &&
    562      1.16  sommerfe 			     lkp->lk_recurselevel == 0) {
    563      1.16  sommerfe 				if (extflags & LK_RECURSEFAIL) {
    564      1.16  sommerfe 					error = EDEADLK;
    565      1.16  sommerfe 					break;
    566      1.16  sommerfe 				} else
    567      1.16  sommerfe 					panic("lockmgr: locking against myself");
    568      1.16  sommerfe 			}
    569       1.1      fvdl 			lkp->lk_exclusivecount++;
    570      1.15      fvdl 			if (extflags & LK_SETRECURSE &&
    571      1.15      fvdl 			    lkp->lk_recurselevel == 0)
    572      1.15      fvdl 				lkp->lk_recurselevel = lkp->lk_exclusivecount;
    573      1.21   thorpej 			COUNT(lkp, p, cpu_id, 1);
    574       1.1      fvdl 			break;
    575       1.1      fvdl 		}
    576       1.1      fvdl 		/*
    577       1.1      fvdl 		 * If we are just polling, check to see if we will sleep.
    578       1.1      fvdl 		 */
    579       1.1      fvdl 		if ((extflags & LK_NOWAIT) && ((lkp->lk_flags &
    580       1.1      fvdl 		     (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
    581       1.1      fvdl 		     lkp->lk_sharecount != 0)) {
    582       1.1      fvdl 			error = EBUSY;
    583       1.1      fvdl 			break;
    584       1.1      fvdl 		}
    585       1.1      fvdl 		/*
    586       1.1      fvdl 		 * Try to acquire the want_exclusive flag.
    587       1.1      fvdl 		 */
    588      1.23   thorpej 		ACQUIRE(lkp, error, extflags, 0, lkp->lk_flags &
    589       1.1      fvdl 		    (LK_HAVE_EXCL | LK_WANT_EXCL));
    590       1.1      fvdl 		if (error)
    591       1.1      fvdl 			break;
    592       1.1      fvdl 		lkp->lk_flags |= LK_WANT_EXCL;
    593       1.1      fvdl 		/*
    594       1.1      fvdl 		 * Wait for shared locks and upgrades to finish.
    595       1.1      fvdl 		 */
    596      1.23   thorpej 		ACQUIRE(lkp, error, extflags, 0, lkp->lk_sharecount != 0 ||
    597       1.1      fvdl 		       (lkp->lk_flags & LK_WANT_UPGRADE));
    598       1.1      fvdl 		lkp->lk_flags &= ~LK_WANT_EXCL;
    599       1.1      fvdl 		if (error)
    600       1.1      fvdl 			break;
    601       1.1      fvdl 		lkp->lk_flags |= LK_HAVE_EXCL;
    602      1.19   thorpej 		SETHOLDER(lkp, pid, cpu_id);
    603      1.21   thorpej 		HAVEIT(lkp);
    604       1.1      fvdl 		if (lkp->lk_exclusivecount != 0)
    605       1.1      fvdl 			panic("lockmgr: non-zero exclusive count");
    606       1.1      fvdl 		lkp->lk_exclusivecount = 1;
    607      1.15      fvdl 		if (extflags & LK_SETRECURSE)
    608      1.15      fvdl 			lkp->lk_recurselevel = 1;
    609      1.21   thorpej 		COUNT(lkp, p, cpu_id, 1);
    610       1.1      fvdl 		break;
    611       1.1      fvdl 
    612       1.1      fvdl 	case LK_RELEASE:
    613       1.1      fvdl 		if (lkp->lk_exclusivecount != 0) {
    614      1.19   thorpej 			if (WEHOLDIT(lkp, pid, cpu_id) == 0) {
    615      1.19   thorpej 				if (lkp->lk_flags & LK_SPIN) {
    616      1.19   thorpej 					panic("lockmgr: processor %lu, not "
    617      1.19   thorpej 					    "exclusive lock holder %lu "
    618      1.19   thorpej 					    "unlocking", cpu_id, lkp->lk_cpu);
    619      1.19   thorpej 				} else {
    620      1.19   thorpej 					panic("lockmgr: pid %d, not "
    621      1.19   thorpej 					    "exclusive lock holder %d "
    622      1.19   thorpej 					    "unlocking", pid,
    623      1.19   thorpej 					    lkp->lk_lockholder);
    624      1.19   thorpej 				}
    625      1.19   thorpej 			}
    626      1.15      fvdl 			if (lkp->lk_exclusivecount == lkp->lk_recurselevel)
    627      1.15      fvdl 				lkp->lk_recurselevel = 0;
    628       1.1      fvdl 			lkp->lk_exclusivecount--;
    629      1.21   thorpej 			COUNT(lkp, p, cpu_id, -1);
    630       1.1      fvdl 			if (lkp->lk_exclusivecount == 0) {
    631       1.1      fvdl 				lkp->lk_flags &= ~LK_HAVE_EXCL;
    632      1.19   thorpej 				SETHOLDER(lkp, LK_NOPROC, LK_NOCPU);
    633      1.21   thorpej 				DONTHAVEIT(lkp);
    634       1.1      fvdl 			}
    635       1.1      fvdl 		} else if (lkp->lk_sharecount != 0) {
    636       1.1      fvdl 			lkp->lk_sharecount--;
    637      1.21   thorpej 			COUNT(lkp, p, cpu_id, -1);
    638       1.1      fvdl 		}
    639      1.23   thorpej 		WAKEUP_WAITER(lkp);
    640       1.1      fvdl 		break;
    641       1.1      fvdl 
    642       1.1      fvdl 	case LK_DRAIN:
    643       1.1      fvdl 		/*
    644       1.1      fvdl 		 * Check that we do not already hold the lock, as it can
    645       1.1      fvdl 		 * never drain if we do. Unfortunately, we have no way to
    646       1.1      fvdl 		 * check for holding a shared lock, but at least we can
    647       1.1      fvdl 		 * check for an exclusive one.
    648       1.1      fvdl 		 */
    649      1.19   thorpej 		if (WEHOLDIT(lkp, pid, cpu_id))
    650       1.1      fvdl 			panic("lockmgr: draining against myself");
    651       1.1      fvdl 		/*
    652       1.1      fvdl 		 * If we are just polling, check to see if we will sleep.
    653       1.1      fvdl 		 */
    654       1.1      fvdl 		if ((extflags & LK_NOWAIT) && ((lkp->lk_flags &
    655       1.1      fvdl 		     (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
    656       1.1      fvdl 		     lkp->lk_sharecount != 0 || lkp->lk_waitcount != 0)) {
    657       1.1      fvdl 			error = EBUSY;
    658       1.1      fvdl 			break;
    659       1.1      fvdl 		}
    660      1.23   thorpej 		ACQUIRE(lkp, error, extflags, 1,
    661      1.23   thorpej 		    ((lkp->lk_flags &
    662      1.23   thorpej 		     (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
    663      1.23   thorpej 		     lkp->lk_sharecount != 0 ||
    664      1.23   thorpej 		     lkp->lk_waitcount != 0));
    665      1.23   thorpej 		if (error)
    666      1.23   thorpej 			break;
    667       1.1      fvdl 		lkp->lk_flags |= LK_DRAINING | LK_HAVE_EXCL;
    668      1.19   thorpej 		SETHOLDER(lkp, pid, cpu_id);
    669      1.21   thorpej 		HAVEIT(lkp);
    670       1.1      fvdl 		lkp->lk_exclusivecount = 1;
    671      1.15      fvdl 		/* XXX unlikely that we'd want this */
    672      1.15      fvdl 		if (extflags & LK_SETRECURSE)
    673      1.15      fvdl 			lkp->lk_recurselevel = 1;
    674      1.21   thorpej 		COUNT(lkp, p, cpu_id, 1);
    675       1.1      fvdl 		break;
    676       1.1      fvdl 
    677       1.1      fvdl 	default:
    678       1.1      fvdl 		simple_unlock(&lkp->lk_interlock);
    679       1.1      fvdl 		panic("lockmgr: unknown locktype request %d",
    680       1.1      fvdl 		    flags & LK_TYPE_MASK);
    681       1.1      fvdl 		/* NOTREACHED */
    682       1.1      fvdl 	}
    683      1.23   thorpej 	if ((lkp->lk_flags & (LK_WAITDRAIN|LK_SPIN)) == LK_WAITDRAIN &&
    684      1.23   thorpej 	    ((lkp->lk_flags &
    685      1.23   thorpej 	      (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) == 0 &&
    686       1.1      fvdl 	     lkp->lk_sharecount == 0 && lkp->lk_waitcount == 0)) {
    687       1.1      fvdl 		lkp->lk_flags &= ~LK_WAITDRAIN;
    688      1.20   thorpej 		wakeup_one((void *)&lkp->lk_flags);
    689       1.1      fvdl 	}
    690      1.32  sommerfe 	/*
    691      1.32  sommerfe 	 * Note that this panic will be a recursive panic, since
    692      1.32  sommerfe 	 * we only set lock_shutdown_noblock above if panicstr != NULL.
    693      1.32  sommerfe 	 */
    694      1.32  sommerfe 	if (error && lock_shutdown_noblock)
    695      1.32  sommerfe 		panic("lockmgr: deadlock (see previous panic)");
    696      1.32  sommerfe 
    697       1.1      fvdl 	simple_unlock(&lkp->lk_interlock);
    698       1.1      fvdl 	return (error);
    699       1.1      fvdl }
    700       1.1      fvdl 
    701       1.1      fvdl /*
    702       1.1      fvdl  * Print out information about state of a lock. Used by VOP_PRINT
    703       1.1      fvdl  * routines to display ststus about contained locks.
    704       1.1      fvdl  */
    705       1.2      fvdl void
    706       1.1      fvdl lockmgr_printinfo(lkp)
    707      1.19   thorpej 	__volatile struct lock *lkp;
    708       1.1      fvdl {
    709       1.1      fvdl 
    710       1.1      fvdl 	if (lkp->lk_sharecount)
    711       1.1      fvdl 		printf(" lock type %s: SHARED (count %d)", lkp->lk_wmesg,
    712       1.1      fvdl 		    lkp->lk_sharecount);
    713      1.19   thorpej 	else if (lkp->lk_flags & LK_HAVE_EXCL) {
    714      1.19   thorpej 		printf(" lock type %s: EXCL (count %d) by ",
    715      1.19   thorpej 		    lkp->lk_wmesg, lkp->lk_exclusivecount);
    716      1.19   thorpej 		if (lkp->lk_flags & LK_SPIN)
    717      1.19   thorpej 			printf("processor %lu", lkp->lk_cpu);
    718      1.19   thorpej 		else
    719      1.19   thorpej 			printf("pid %d", lkp->lk_lockholder);
    720      1.19   thorpej 	} else
    721      1.19   thorpej 		printf(" not locked");
    722      1.19   thorpej 	if ((lkp->lk_flags & LK_SPIN) == 0 && lkp->lk_waitcount > 0)
    723       1.1      fvdl 		printf(" with %d pending", lkp->lk_waitcount);
    724       1.1      fvdl }
    725       1.1      fvdl 
    726      1.21   thorpej #if defined(LOCKDEBUG) /* { */
    727      1.21   thorpej TAILQ_HEAD(, simplelock) simplelock_list =
    728      1.21   thorpej     TAILQ_HEAD_INITIALIZER(simplelock_list);
    729      1.21   thorpej 
    730      1.21   thorpej #if defined(MULTIPROCESSOR) /* { */
    731      1.21   thorpej struct simplelock simplelock_list_slock = SIMPLELOCK_INITIALIZER;
    732      1.21   thorpej 
    733      1.21   thorpej #define	SLOCK_LIST_LOCK()						\
    734      1.29  sommerfe 	__cpu_simple_lock(&simplelock_list_slock.lock_data)
    735      1.21   thorpej 
    736      1.21   thorpej #define	SLOCK_LIST_UNLOCK()						\
    737      1.29  sommerfe 	__cpu_simple_unlock(&simplelock_list_slock.lock_data)
    738      1.21   thorpej 
    739      1.28   thorpej #if defined(__HAVE_ATOMIC_OPERATIONS) /* { */
    740      1.21   thorpej #define	SLOCK_COUNT(x)							\
    741      1.28   thorpej 	atomic_add_ulong(&curcpu()->ci_simple_locks, (x))
    742      1.28   thorpej #else
    743      1.28   thorpej #define	SLOCK_COUNT(x)		/* not safe */
    744      1.28   thorpej #endif /* __HAVE_ATOMIC_OPERATIONS */ /* } */
    745      1.21   thorpej #else
    746      1.21   thorpej u_long simple_locks;
    747      1.21   thorpej 
    748      1.21   thorpej #define	SLOCK_LIST_LOCK()	/* nothing */
    749      1.21   thorpej 
    750      1.21   thorpej #define	SLOCK_LIST_UNLOCK()	/* nothing */
    751      1.21   thorpej 
    752      1.21   thorpej #define	SLOCK_COUNT(x)		simple_locks += (x)
    753      1.21   thorpej #endif /* MULTIPROCESSOR */ /* } */
    754      1.21   thorpej 
    755      1.21   thorpej #ifdef DDB /* { */
    756      1.18       chs int simple_lock_debugger = 0;
    757      1.21   thorpej #define	SLOCK_DEBUGGER()	if (simple_lock_debugger) Debugger()
    758      1.21   thorpej #else
    759      1.21   thorpej #define	SLOCK_DEBUGGER()	/* nothing */
    760      1.21   thorpej #endif /* } */
    761      1.21   thorpej 
    762      1.26  sommerfe #ifdef MULTIPROCESSOR
    763      1.26  sommerfe #define SLOCK_MP()		lock_printf("on cpu %d\n", cpu_number())
    764      1.26  sommerfe #else
    765      1.26  sommerfe #define SLOCK_MP()		/* nothing */
    766      1.26  sommerfe #endif
    767      1.26  sommerfe 
    768      1.21   thorpej #define	SLOCK_WHERE(str, alp, id, l)					\
    769      1.21   thorpej do {									\
    770      1.25   thorpej 	lock_printf(str);						\
    771      1.26  sommerfe 	lock_printf("lock: %p, currently at: %s:%d\n", (alp), (id), (l));		\
    772      1.26  sommerfe 	SLOCK_MP();							\
    773      1.21   thorpej 	if ((alp)->lock_file != NULL)					\
    774      1.25   thorpej 		lock_printf("last locked: %s:%d\n", (alp)->lock_file,	\
    775      1.21   thorpej 		    (alp)->lock_line);					\
    776      1.21   thorpej 	if ((alp)->unlock_file != NULL)					\
    777      1.25   thorpej 		lock_printf("last unlocked: %s:%d\n", (alp)->unlock_file, \
    778      1.21   thorpej 		    (alp)->unlock_line);				\
    779      1.21   thorpej 	SLOCK_DEBUGGER();						\
    780      1.30   thorpej } while (/*CONSTCOND*/0)
    781      1.12       chs 
    782       1.1      fvdl /*
    783       1.1      fvdl  * Simple lock functions so that the debugger can see from whence
    784       1.1      fvdl  * they are being called.
    785       1.1      fvdl  */
    786       1.1      fvdl void
    787       1.1      fvdl simple_lock_init(alp)
    788       1.1      fvdl 	struct simplelock *alp;
    789       1.1      fvdl {
    790      1.21   thorpej 
    791      1.21   thorpej #if defined(MULTIPROCESSOR) /* { */
    792      1.27   thorpej 	__cpu_simple_lock_init(&alp->lock_data);
    793      1.21   thorpej #else
    794      1.27   thorpej 	alp->lock_data = __SIMPLELOCK_UNLOCKED;
    795      1.21   thorpej #endif /* } */
    796       1.5       chs 	alp->lock_file = NULL;
    797       1.5       chs 	alp->lock_line = 0;
    798       1.5       chs 	alp->unlock_file = NULL;
    799       1.5       chs 	alp->unlock_line = 0;
    800      1.10   thorpej 	alp->lock_holder = 0;
    801       1.1      fvdl }
    802       1.1      fvdl 
    803       1.1      fvdl void
    804       1.1      fvdl _simple_lock(alp, id, l)
    805       1.1      fvdl 	__volatile struct simplelock *alp;
    806       1.1      fvdl 	const char *id;
    807       1.1      fvdl 	int l;
    808       1.1      fvdl {
    809      1.24   thorpej 	cpuid_t cpu_id = cpu_number();
    810      1.12       chs 	int s;
    811      1.12       chs 
    812      1.18       chs 	s = splhigh();
    813      1.21   thorpej 
    814      1.21   thorpej 	/*
    815      1.21   thorpej 	 * MULTIPROCESSOR case: This is `safe' since if it's not us, we
    816      1.21   thorpej 	 * don't take any action, and just fall into the normal spin case.
    817      1.21   thorpej 	 */
    818      1.27   thorpej 	if (alp->lock_data == __SIMPLELOCK_LOCKED) {
    819      1.21   thorpej #if defined(MULTIPROCESSOR) /* { */
    820      1.21   thorpej 		if (alp->lock_holder == cpu_id) {
    821      1.21   thorpej 			SLOCK_WHERE("simple_lock: locking against myself\n",
    822      1.21   thorpej 			    alp, id, l);
    823      1.21   thorpej 			goto out;
    824       1.1      fvdl 		}
    825      1.21   thorpej #else
    826      1.21   thorpej 		SLOCK_WHERE("simple_lock: lock held\n", alp, id, l);
    827      1.21   thorpej 		goto out;
    828      1.21   thorpej #endif /* MULTIPROCESSOR */ /* } */
    829       1.1      fvdl 	}
    830      1.21   thorpej 
    831      1.21   thorpej #if defined(MULTIPROCESSOR) /* { */
    832      1.21   thorpej 	/* Acquire the lock before modifying any fields. */
    833      1.27   thorpej 	__cpu_simple_lock(&alp->lock_data);
    834      1.21   thorpej #else
    835      1.27   thorpej 	alp->lock_data = __SIMPLELOCK_LOCKED;
    836      1.21   thorpej #endif /* } */
    837      1.21   thorpej 
    838       1.5       chs 	alp->lock_file = id;
    839       1.5       chs 	alp->lock_line = l;
    840      1.21   thorpej 	alp->lock_holder = cpu_id;
    841      1.21   thorpej 
    842      1.21   thorpej 	SLOCK_LIST_LOCK();
    843      1.21   thorpej 	/* XXX Cast away volatile */
    844      1.21   thorpej 	TAILQ_INSERT_TAIL(&simplelock_list, (struct simplelock *)alp, list);
    845      1.21   thorpej 	SLOCK_LIST_UNLOCK();
    846      1.21   thorpej 
    847      1.21   thorpej 	SLOCK_COUNT(1);
    848      1.21   thorpej 
    849      1.21   thorpej  out:
    850      1.18       chs 	splx(s);
    851       1.1      fvdl }
    852       1.1      fvdl 
    853       1.1      fvdl int
    854       1.1      fvdl _simple_lock_try(alp, id, l)
    855       1.1      fvdl 	__volatile struct simplelock *alp;
    856       1.1      fvdl 	const char *id;
    857       1.1      fvdl 	int l;
    858       1.1      fvdl {
    859      1.24   thorpej 	cpuid_t cpu_id = cpu_number();
    860      1.21   thorpej 	int s, rv = 0;
    861       1.1      fvdl 
    862      1.18       chs 	s = splhigh();
    863      1.21   thorpej 
    864      1.21   thorpej 	/*
    865      1.21   thorpej 	 * MULTIPROCESSOR case: This is `safe' since if it's not us, we
    866      1.21   thorpej 	 * don't take any action.
    867      1.21   thorpej 	 */
    868      1.21   thorpej #if defined(MULTIPROCESSOR) /* { */
    869      1.27   thorpej 	if ((rv = __cpu_simple_lock_try(&alp->lock_data)) == 0) {
    870      1.21   thorpej 		if (alp->lock_holder == cpu_id)
    871      1.21   thorpej 			SLOCK_WHERE("simple_lock_try: locking against myself\n",
    872      1.26  sommerfe 			    alp, id, l);
    873      1.21   thorpej 		goto out;
    874      1.21   thorpej 	}
    875      1.21   thorpej #else
    876      1.27   thorpej 	if (alp->lock_data == __SIMPLELOCK_LOCKED) {
    877      1.21   thorpej 		SLOCK_WHERE("simple_lock_try: lock held\n", alp, id, l);
    878      1.21   thorpej 		goto out;
    879      1.18       chs 	}
    880      1.27   thorpej 	alp->lock_data = __SIMPLELOCK_LOCKED;
    881      1.21   thorpej #endif /* MULTIPROCESSOR */ /* } */
    882      1.21   thorpej 
    883      1.21   thorpej 	/*
    884      1.21   thorpej 	 * At this point, we have acquired the lock.
    885      1.21   thorpej 	 */
    886      1.21   thorpej 
    887      1.21   thorpej 	rv = 1;
    888      1.18       chs 
    889       1.5       chs 	alp->lock_file = id;
    890       1.5       chs 	alp->lock_line = l;
    891      1.21   thorpej 	alp->lock_holder = cpu_id;
    892      1.21   thorpej 
    893      1.21   thorpej 	SLOCK_LIST_LOCK();
    894      1.21   thorpej 	/* XXX Cast away volatile. */
    895      1.21   thorpej 	TAILQ_INSERT_TAIL(&simplelock_list, (struct simplelock *)alp, list);
    896      1.21   thorpej 	SLOCK_LIST_UNLOCK();
    897      1.21   thorpej 
    898      1.21   thorpej 	SLOCK_COUNT(1);
    899      1.21   thorpej 
    900      1.21   thorpej  out:
    901      1.12       chs 	splx(s);
    902      1.21   thorpej 	return (rv);
    903       1.1      fvdl }
    904       1.1      fvdl 
    905       1.1      fvdl void
    906       1.1      fvdl _simple_unlock(alp, id, l)
    907       1.1      fvdl 	__volatile struct simplelock *alp;
    908       1.1      fvdl 	const char *id;
    909       1.1      fvdl 	int l;
    910       1.1      fvdl {
    911      1.12       chs 	int s;
    912       1.1      fvdl 
    913      1.18       chs 	s = splhigh();
    914      1.21   thorpej 
    915      1.21   thorpej 	/*
    916      1.21   thorpej 	 * MULTIPROCESSOR case: This is `safe' because we think we hold
    917      1.21   thorpej 	 * the lock, and if we don't, we don't take any action.
    918      1.21   thorpej 	 */
    919      1.27   thorpej 	if (alp->lock_data == __SIMPLELOCK_UNLOCKED) {
    920      1.21   thorpej 		SLOCK_WHERE("simple_unlock: lock not held\n",
    921      1.21   thorpej 		    alp, id, l);
    922      1.21   thorpej 		goto out;
    923      1.21   thorpej 	}
    924      1.21   thorpej 
    925      1.21   thorpej 	SLOCK_LIST_LOCK();
    926      1.21   thorpej 	TAILQ_REMOVE(&simplelock_list, alp, list);
    927      1.21   thorpej 	SLOCK_LIST_UNLOCK();
    928      1.21   thorpej 
    929      1.21   thorpej 	SLOCK_COUNT(-1);
    930      1.21   thorpej 
    931      1.21   thorpej 	alp->list.tqe_next = NULL;	/* sanity */
    932      1.21   thorpej 	alp->list.tqe_prev = NULL;	/* sanity */
    933      1.21   thorpej 
    934       1.5       chs 	alp->unlock_file = id;
    935       1.5       chs 	alp->unlock_line = l;
    936      1.21   thorpej 
    937      1.21   thorpej #if defined(MULTIPROCESSOR) /* { */
    938      1.26  sommerfe 	alp->lock_holder = LK_NOCPU;
    939      1.21   thorpej 	/* Now that we've modified all fields, release the lock. */
    940      1.27   thorpej 	__cpu_simple_unlock(&alp->lock_data);
    941      1.21   thorpej #else
    942      1.27   thorpej 	alp->lock_data = __SIMPLELOCK_UNLOCKED;
    943      1.21   thorpej #endif /* } */
    944      1.21   thorpej 
    945      1.21   thorpej  out:
    946      1.18       chs 	splx(s);
    947      1.12       chs }
    948      1.12       chs 
    949      1.12       chs void
    950      1.12       chs simple_lock_dump()
    951      1.12       chs {
    952      1.12       chs 	struct simplelock *alp;
    953      1.12       chs 	int s;
    954      1.12       chs 
    955      1.12       chs 	s = splhigh();
    956      1.21   thorpej 	SLOCK_LIST_LOCK();
    957      1.25   thorpej 	lock_printf("all simple locks:\n");
    958      1.21   thorpej 	for (alp = TAILQ_FIRST(&simplelock_list); alp != NULL;
    959      1.21   thorpej 	     alp = TAILQ_NEXT(alp, list)) {
    960      1.25   thorpej 		lock_printf("%p CPU %lu %s:%d\n", alp, alp->lock_holder,
    961      1.21   thorpej 		    alp->lock_file, alp->lock_line);
    962      1.12       chs 	}
    963      1.21   thorpej 	SLOCK_LIST_UNLOCK();
    964      1.12       chs 	splx(s);
    965      1.12       chs }
    966      1.12       chs 
    967      1.12       chs void
    968      1.12       chs simple_lock_freecheck(start, end)
    969      1.21   thorpej 	void *start, *end;
    970      1.12       chs {
    971      1.12       chs 	struct simplelock *alp;
    972      1.12       chs 	int s;
    973      1.12       chs 
    974      1.12       chs 	s = splhigh();
    975      1.21   thorpej 	SLOCK_LIST_LOCK();
    976      1.21   thorpej 	for (alp = TAILQ_FIRST(&simplelock_list); alp != NULL;
    977      1.21   thorpej 	     alp = TAILQ_NEXT(alp, list)) {
    978      1.12       chs 		if ((void *)alp >= start && (void *)alp < end) {
    979      1.25   thorpej 			lock_printf("freeing simple_lock %p CPU %lu %s:%d\n",
    980  1.32.2.1   thorpej 			    alp, alp->lock_holder, alp->lock_file,
    981  1.32.2.1   thorpej 			    alp->lock_line);
    982  1.32.2.1   thorpej 			SLOCK_DEBUGGER();
    983  1.32.2.1   thorpej 		}
    984  1.32.2.1   thorpej 	}
    985  1.32.2.1   thorpej 	SLOCK_LIST_UNLOCK();
    986  1.32.2.1   thorpej 	splx(s);
    987  1.32.2.1   thorpej }
    988  1.32.2.1   thorpej 
    989  1.32.2.1   thorpej void
    990  1.32.2.1   thorpej simple_lock_switchcheck(void)
    991  1.32.2.1   thorpej {
    992  1.32.2.1   thorpej 	struct simplelock *alp;
    993  1.32.2.1   thorpej 	cpuid_t cpu_id = cpu_number();
    994  1.32.2.1   thorpej 	int s;
    995  1.32.2.1   thorpej 
    996  1.32.2.1   thorpej 	s = splhigh();
    997  1.32.2.1   thorpej 	SLOCK_LIST_LOCK();
    998  1.32.2.1   thorpej 	for (alp = TAILQ_FIRST(&simplelock_list); alp != NULL;
    999  1.32.2.1   thorpej 	     alp = TAILQ_NEXT(alp, list)) {
   1000  1.32.2.1   thorpej 		if (alp->lock_holder == cpu_id) {
   1001  1.32.2.1   thorpej 			lock_printf("switching with held simple_lock %p "
   1002  1.32.2.1   thorpej 			    "CPU %lu %s:%s\n",
   1003      1.21   thorpej 			    alp, alp->lock_holder, alp->lock_file,
   1004      1.21   thorpej 			    alp->lock_line);
   1005      1.21   thorpej 			SLOCK_DEBUGGER();
   1006      1.12       chs 		}
   1007      1.12       chs 	}
   1008      1.21   thorpej 	SLOCK_LIST_UNLOCK();
   1009      1.12       chs 	splx(s);
   1010       1.1      fvdl }
   1011      1.21   thorpej #endif /* LOCKDEBUG */ /* } */
   1012