Home | History | Annotate | Line # | Download | only in kern
kern_lock.c revision 1.22
      1  1.22    mellon /*	$NetBSD: kern_lock.c,v 1.22 1999/07/28 01:59:46 mellon Exp $	*/
      2  1.19   thorpej 
      3  1.19   thorpej /*-
      4  1.19   thorpej  * Copyright (c) 1999 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.1      fvdl /*
     93   1.1      fvdl  * Locking primitives implementation.
     94   1.1      fvdl  * Locks provide shared/exclusive sychronization.
     95   1.1      fvdl  */
     96   1.1      fvdl 
     97  1.21   thorpej #if defined(LOCKDEBUG) || defined(DIAGNOSTIC) /* { */
     98  1.21   thorpej #if defined(MULTIPROCESSOR) /* { */
     99  1.21   thorpej #define	COUNT_CPU(cpu_id, x)						\
    100  1.21   thorpej 	/* atomic_add_ulong(&curcpu().ci_spin_locks, (x)) */
    101  1.21   thorpej #else
    102  1.21   thorpej u_long	spin_locks;
    103  1.21   thorpej #define	COUNT_CPU(cpu_id, x)	spin_locks += (x)
    104  1.21   thorpej #endif /* MULTIPROCESSOR */ /* } */
    105  1.21   thorpej 
    106  1.21   thorpej #define	COUNT(lkp, p, cpu_id, x)					\
    107  1.21   thorpej do {									\
    108  1.21   thorpej 	if ((lkp)->lk_flags & LK_SPIN)					\
    109  1.21   thorpej 		COUNT_CPU((cpu_id), (x));				\
    110  1.21   thorpej 	else								\
    111  1.21   thorpej 		(p)->p_locks += (x);					\
    112  1.21   thorpej } while (0)
    113   1.1      fvdl #else
    114  1.22    mellon #define COUNT(lkp, p, cpu_id, x)
    115  1.21   thorpej #endif /* LOCKDEBUG || DIAGNOSTIC */ /* } */
    116   1.1      fvdl 
    117   1.1      fvdl /*
    118   1.1      fvdl  * Acquire a resource.
    119   1.1      fvdl  */
    120   1.1      fvdl #define ACQUIRE(lkp, error, extflags, wanted)				\
    121  1.19   thorpej 	if ((extflags) & LK_SPIN) {					\
    122  1.19   thorpej 		int interlocked;					\
    123  1.19   thorpej 									\
    124  1.19   thorpej 		for (interlocked = 1;;) {				\
    125  1.19   thorpej 			if (wanted) {					\
    126  1.19   thorpej 				if (interlocked) {			\
    127  1.19   thorpej 					simple_unlock(&(lkp)->lk_interlock); \
    128  1.19   thorpej 					interlocked = 0;		\
    129  1.19   thorpej 				}					\
    130  1.19   thorpej 			} else if (interlocked) {			\
    131  1.19   thorpej 				break;					\
    132  1.19   thorpej 			} else {					\
    133  1.19   thorpej 				simple_lock(&(lkp)->lk_interlock);	\
    134  1.19   thorpej 				interlocked = 1;			\
    135  1.19   thorpej 			}						\
    136  1.19   thorpej 		}							\
    137  1.19   thorpej 		KASSERT((wanted) == 0);					\
    138  1.19   thorpej 		error = 0;	/* sanity */				\
    139  1.19   thorpej 	} else {							\
    140  1.19   thorpej 		for (error = 0; wanted; ) {				\
    141  1.19   thorpej 			(lkp)->lk_waitcount++;				\
    142  1.19   thorpej 			simple_unlock(&(lkp)->lk_interlock);		\
    143  1.19   thorpej 			error = tsleep((void *)lkp, (lkp)->lk_prio,	\
    144  1.19   thorpej 			    (lkp)->lk_wmesg, (lkp)->lk_timo);		\
    145  1.19   thorpej 			simple_lock(&(lkp)->lk_interlock);		\
    146  1.19   thorpej 			(lkp)->lk_waitcount--;				\
    147  1.19   thorpej 			if (error)					\
    148  1.19   thorpej 				break;					\
    149  1.19   thorpej 			if ((extflags) & LK_SLEEPFAIL) {		\
    150  1.19   thorpej 				error = ENOLCK;				\
    151  1.19   thorpej 				break;					\
    152  1.19   thorpej 			}						\
    153   1.1      fvdl 		}							\
    154   1.1      fvdl 	}
    155   1.1      fvdl 
    156  1.19   thorpej #define	SETHOLDER(lkp, pid, cpu_id)					\
    157  1.19   thorpej do {									\
    158  1.19   thorpej 	if ((lkp)->lk_flags & LK_SPIN)					\
    159  1.19   thorpej 		(lkp)->lk_cpu = cpu_id;					\
    160  1.19   thorpej 	else								\
    161  1.19   thorpej 		(lkp)->lk_lockholder = pid;				\
    162  1.19   thorpej } while (0)
    163  1.19   thorpej 
    164  1.19   thorpej #define	WEHOLDIT(lkp, pid, cpu_id)					\
    165  1.19   thorpej 	(((lkp)->lk_flags & LK_SPIN) != 0 ?				\
    166  1.19   thorpej 	 ((lkp)->lk_cpu == (cpu_id)) : ((lkp)->lk_lockholder == (pid)))
    167  1.19   thorpej 
    168  1.21   thorpej #if defined(LOCKDEBUG) /* { */
    169  1.21   thorpej #if defined(MULTIPROCESSOR) /* { */
    170  1.21   thorpej struct simplelock spinlock_list_slock = SIMPLELOCK_INITIALIZER;
    171  1.21   thorpej 
    172  1.21   thorpej #define	SPINLOCK_LIST_LOCK()	cpu_simple_lock(&spinlock_list_slock)
    173  1.21   thorpej 
    174  1.21   thorpej #define	SPINLOCK_LIST_UNLOCK()	cpu_simple_unlock(&spinlock_list_slock)
    175  1.21   thorpej #else
    176  1.21   thorpej #define	SPINLOCK_LIST_LOCK()	/* nothing */
    177  1.21   thorpej 
    178  1.21   thorpej #define	SPINLOCK_LIST_UNLOCK()	/* nothing */
    179  1.21   thorpej #endif /* MULTIPROCESSOR */ /* } */
    180  1.21   thorpej 
    181  1.21   thorpej TAILQ_HEAD(, lock) spinlock_list =
    182  1.21   thorpej     TAILQ_HEAD_INITIALIZER(spinlock_list);
    183  1.21   thorpej 
    184  1.21   thorpej #define	HAVEIT(lkp)							\
    185  1.21   thorpej do {									\
    186  1.21   thorpej 	if ((lkp)->lk_flags & LK_SPIN) {				\
    187  1.21   thorpej 		int s = splhigh();					\
    188  1.21   thorpej 		SPINLOCK_LIST_LOCK();					\
    189  1.21   thorpej 		/* XXX Cast away volatile. */				\
    190  1.21   thorpej 		TAILQ_INSERT_TAIL(&spinlock_list, (struct lock *)(lkp),	\
    191  1.21   thorpej 		    lk_list);						\
    192  1.21   thorpej 		SPINLOCK_LIST_UNLOCK();					\
    193  1.21   thorpej 		splx(s);						\
    194  1.21   thorpej 	}								\
    195  1.21   thorpej } while (0)
    196  1.21   thorpej 
    197  1.21   thorpej #define	DONTHAVEIT(lkp)							\
    198  1.21   thorpej do {									\
    199  1.21   thorpej 	if ((lkp)->lk_flags & LK_SPIN) {				\
    200  1.21   thorpej 		int s = splhigh();					\
    201  1.21   thorpej 		SPINLOCK_LIST_LOCK();					\
    202  1.21   thorpej 		/* XXX Cast away volatile. */				\
    203  1.21   thorpej 		TAILQ_REMOVE(&spinlock_list, (struct lock *)(lkp),	\
    204  1.21   thorpej 		    lk_list);						\
    205  1.21   thorpej 		SPINLOCK_LIST_UNLOCK();					\
    206  1.21   thorpej 		splx(s);						\
    207  1.21   thorpej 	}								\
    208  1.21   thorpej } while (0)
    209  1.21   thorpej #else
    210  1.21   thorpej #define	HAVEIT(lkp)		/* nothing */
    211  1.21   thorpej 
    212  1.21   thorpej #define	DONTHAVEIT(lkp)		/* nothing */
    213  1.21   thorpej #endif /* LOCKDEBUG */ /* } */
    214  1.21   thorpej 
    215   1.1      fvdl /*
    216   1.1      fvdl  * Initialize a lock; required before use.
    217   1.1      fvdl  */
    218   1.1      fvdl void
    219   1.1      fvdl lockinit(lkp, prio, wmesg, timo, flags)
    220   1.1      fvdl 	struct lock *lkp;
    221   1.1      fvdl 	int prio;
    222   1.4   mycroft 	const char *wmesg;
    223   1.1      fvdl 	int timo;
    224   1.1      fvdl 	int flags;
    225   1.1      fvdl {
    226   1.1      fvdl 
    227   1.8     perry 	memset(lkp, 0, sizeof(struct lock));
    228   1.1      fvdl 	simple_lock_init(&lkp->lk_interlock);
    229   1.1      fvdl 	lkp->lk_flags = flags & LK_EXTFLG_MASK;
    230  1.19   thorpej 	if (flags & LK_SPIN)
    231  1.19   thorpej 		lkp->lk_cpu = LK_NOCPU;
    232  1.19   thorpej 	else {
    233  1.19   thorpej 		lkp->lk_lockholder = LK_NOPROC;
    234  1.19   thorpej 		lkp->lk_prio = prio;
    235  1.19   thorpej 		lkp->lk_timo = timo;
    236  1.19   thorpej 	}
    237  1.19   thorpej 	lkp->lk_wmesg = wmesg;	/* just a name for spin locks */
    238   1.1      fvdl }
    239   1.1      fvdl 
    240   1.1      fvdl /*
    241   1.1      fvdl  * Determine the status of a lock.
    242   1.1      fvdl  */
    243   1.1      fvdl int
    244   1.1      fvdl lockstatus(lkp)
    245   1.1      fvdl 	struct lock *lkp;
    246   1.1      fvdl {
    247   1.1      fvdl 	int lock_type = 0;
    248   1.1      fvdl 
    249   1.1      fvdl 	simple_lock(&lkp->lk_interlock);
    250   1.1      fvdl 	if (lkp->lk_exclusivecount != 0)
    251   1.1      fvdl 		lock_type = LK_EXCLUSIVE;
    252   1.1      fvdl 	else if (lkp->lk_sharecount != 0)
    253   1.1      fvdl 		lock_type = LK_SHARED;
    254   1.1      fvdl 	simple_unlock(&lkp->lk_interlock);
    255   1.1      fvdl 	return (lock_type);
    256   1.1      fvdl }
    257   1.1      fvdl 
    258   1.1      fvdl /*
    259   1.1      fvdl  * Set, change, or release a lock.
    260   1.1      fvdl  *
    261   1.1      fvdl  * Shared requests increment the shared count. Exclusive requests set the
    262   1.1      fvdl  * LK_WANT_EXCL flag (preventing further shared locks), and wait for already
    263   1.1      fvdl  * accepted shared locks and shared-to-exclusive upgrades to go away.
    264   1.1      fvdl  */
    265   1.1      fvdl int
    266   1.6      fvdl lockmgr(lkp, flags, interlkp)
    267   1.1      fvdl 	__volatile struct lock *lkp;
    268   1.1      fvdl 	u_int flags;
    269   1.1      fvdl 	struct simplelock *interlkp;
    270   1.1      fvdl {
    271   1.1      fvdl 	int error;
    272   1.1      fvdl 	pid_t pid;
    273   1.1      fvdl 	int extflags;
    274  1.19   thorpej 	u_long cpu_id;
    275   1.6      fvdl 	struct proc *p = curproc;
    276   1.1      fvdl 
    277   1.1      fvdl 	error = 0;
    278  1.19   thorpej 
    279   1.1      fvdl 	simple_lock(&lkp->lk_interlock);
    280   1.1      fvdl 	if (flags & LK_INTERLOCK)
    281   1.1      fvdl 		simple_unlock(interlkp);
    282   1.1      fvdl 	extflags = (flags | lkp->lk_flags) & LK_EXTFLG_MASK;
    283  1.19   thorpej 
    284  1.21   thorpej #ifdef DIAGNOSTIC /* { */
    285  1.19   thorpej 	/*
    286  1.19   thorpej 	 * Don't allow spins on sleep locks and don't allow sleeps
    287  1.19   thorpej 	 * on spin locks.
    288  1.19   thorpej 	 */
    289  1.19   thorpej 	if ((flags ^ lkp->lk_flags) & LK_SPIN)
    290  1.19   thorpej 		panic("lockmgr: sleep/spin mismatch\n");
    291  1.21   thorpej #endif /* } */
    292  1.19   thorpej 
    293  1.19   thorpej 	if (extflags & LK_SPIN)
    294  1.19   thorpej 		pid = LK_KERNPROC;
    295  1.19   thorpej 	else {
    296  1.21   thorpej #ifdef DIAGNOSTIC /* { */
    297  1.19   thorpej 		if (p == NULL)
    298  1.19   thorpej 			panic("lockmgr: no context");
    299  1.21   thorpej #endif /* } */
    300  1.19   thorpej 		pid = p->p_pid;
    301  1.19   thorpej 	}
    302  1.19   thorpej 	cpu_id = 0;			/* XXX cpu_number() XXX */
    303  1.19   thorpej 
    304  1.21   thorpej #ifdef DIAGNOSTIC /* { */
    305   1.1      fvdl 	/*
    306   1.1      fvdl 	 * Once a lock has drained, the LK_DRAINING flag is set and an
    307   1.1      fvdl 	 * exclusive lock is returned. The only valid operation thereafter
    308   1.1      fvdl 	 * is a single release of that exclusive lock. This final release
    309   1.1      fvdl 	 * clears the LK_DRAINING flag and sets the LK_DRAINED flag. Any
    310   1.1      fvdl 	 * further requests of any sort will result in a panic. The bits
    311   1.1      fvdl 	 * selected for these two flags are chosen so that they will be set
    312   1.1      fvdl 	 * in memory that is freed (freed memory is filled with 0xdeadbeef).
    313   1.1      fvdl 	 * The final release is permitted to give a new lease on life to
    314   1.1      fvdl 	 * the lock by specifying LK_REENABLE.
    315   1.1      fvdl 	 */
    316   1.1      fvdl 	if (lkp->lk_flags & (LK_DRAINING|LK_DRAINED)) {
    317   1.1      fvdl 		if (lkp->lk_flags & LK_DRAINED)
    318   1.1      fvdl 			panic("lockmgr: using decommissioned lock");
    319   1.1      fvdl 		if ((flags & LK_TYPE_MASK) != LK_RELEASE ||
    320  1.19   thorpej 		    WEHOLDIT(lkp, pid, cpu_id) == 0)
    321   1.1      fvdl 			panic("lockmgr: non-release on draining lock: %d\n",
    322   1.1      fvdl 			    flags & LK_TYPE_MASK);
    323   1.1      fvdl 		lkp->lk_flags &= ~LK_DRAINING;
    324   1.1      fvdl 		if ((flags & LK_REENABLE) == 0)
    325   1.1      fvdl 			lkp->lk_flags |= LK_DRAINED;
    326   1.1      fvdl 	}
    327  1.21   thorpej #endif /* DIAGNOSTIC */ /* } */
    328   1.1      fvdl 
    329   1.1      fvdl 	switch (flags & LK_TYPE_MASK) {
    330   1.1      fvdl 
    331   1.1      fvdl 	case LK_SHARED:
    332  1.19   thorpej 		if (WEHOLDIT(lkp, pid, cpu_id) == 0) {
    333   1.1      fvdl 			/*
    334   1.1      fvdl 			 * If just polling, check to see if we will block.
    335   1.1      fvdl 			 */
    336   1.1      fvdl 			if ((extflags & LK_NOWAIT) && (lkp->lk_flags &
    337   1.1      fvdl 			    (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE))) {
    338   1.1      fvdl 				error = EBUSY;
    339   1.1      fvdl 				break;
    340   1.1      fvdl 			}
    341   1.1      fvdl 			/*
    342   1.1      fvdl 			 * Wait for exclusive locks and upgrades to clear.
    343   1.1      fvdl 			 */
    344   1.1      fvdl 			ACQUIRE(lkp, error, extflags, lkp->lk_flags &
    345   1.1      fvdl 			    (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE));
    346   1.1      fvdl 			if (error)
    347   1.1      fvdl 				break;
    348   1.1      fvdl 			lkp->lk_sharecount++;
    349  1.21   thorpej 			COUNT(lkp, p, cpu_id, 1);
    350   1.1      fvdl 			break;
    351   1.1      fvdl 		}
    352   1.1      fvdl 		/*
    353   1.1      fvdl 		 * We hold an exclusive lock, so downgrade it to shared.
    354   1.1      fvdl 		 * An alternative would be to fail with EDEADLK.
    355   1.1      fvdl 		 */
    356   1.1      fvdl 		lkp->lk_sharecount++;
    357  1.21   thorpej 		COUNT(lkp, p, cpu_id, 1);
    358   1.1      fvdl 		/* fall into downgrade */
    359   1.1      fvdl 
    360   1.1      fvdl 	case LK_DOWNGRADE:
    361  1.19   thorpej 		if (WEHOLDIT(lkp, pid, cpu_id) == 0 ||
    362  1.19   thorpej 		    lkp->lk_exclusivecount == 0)
    363   1.1      fvdl 			panic("lockmgr: not holding exclusive lock");
    364   1.1      fvdl 		lkp->lk_sharecount += lkp->lk_exclusivecount;
    365   1.1      fvdl 		lkp->lk_exclusivecount = 0;
    366  1.15      fvdl 		lkp->lk_recurselevel = 0;
    367   1.1      fvdl 		lkp->lk_flags &= ~LK_HAVE_EXCL;
    368  1.19   thorpej 		SETHOLDER(lkp, LK_NOPROC, LK_NOCPU);
    369  1.21   thorpej 		DONTHAVEIT(lkp);
    370   1.1      fvdl 		if (lkp->lk_waitcount)
    371  1.20   thorpej 			wakeup_one((void *)lkp);
    372   1.1      fvdl 		break;
    373   1.1      fvdl 
    374   1.1      fvdl 	case LK_EXCLUPGRADE:
    375   1.1      fvdl 		/*
    376   1.1      fvdl 		 * If another process is ahead of us to get an upgrade,
    377   1.1      fvdl 		 * then we want to fail rather than have an intervening
    378   1.1      fvdl 		 * exclusive access.
    379   1.1      fvdl 		 */
    380   1.1      fvdl 		if (lkp->lk_flags & LK_WANT_UPGRADE) {
    381   1.1      fvdl 			lkp->lk_sharecount--;
    382  1.21   thorpej 			COUNT(lkp, p, cpu_id, -1);
    383   1.1      fvdl 			error = EBUSY;
    384   1.1      fvdl 			break;
    385   1.1      fvdl 		}
    386   1.1      fvdl 		/* fall into normal upgrade */
    387   1.1      fvdl 
    388   1.1      fvdl 	case LK_UPGRADE:
    389   1.1      fvdl 		/*
    390   1.1      fvdl 		 * Upgrade a shared lock to an exclusive one. If another
    391   1.1      fvdl 		 * shared lock has already requested an upgrade to an
    392   1.1      fvdl 		 * exclusive lock, our shared lock is released and an
    393   1.1      fvdl 		 * exclusive lock is requested (which will be granted
    394   1.1      fvdl 		 * after the upgrade). If we return an error, the file
    395   1.1      fvdl 		 * will always be unlocked.
    396   1.1      fvdl 		 */
    397  1.19   thorpej 		if (WEHOLDIT(lkp, pid, cpu_id) || lkp->lk_sharecount <= 0)
    398   1.1      fvdl 			panic("lockmgr: upgrade exclusive lock");
    399   1.1      fvdl 		lkp->lk_sharecount--;
    400  1.21   thorpej 		COUNT(lkp, p, cpu_id, -1);
    401   1.1      fvdl 		/*
    402   1.1      fvdl 		 * If we are just polling, check to see if we will block.
    403   1.1      fvdl 		 */
    404   1.1      fvdl 		if ((extflags & LK_NOWAIT) &&
    405   1.1      fvdl 		    ((lkp->lk_flags & LK_WANT_UPGRADE) ||
    406   1.1      fvdl 		     lkp->lk_sharecount > 1)) {
    407   1.1      fvdl 			error = EBUSY;
    408   1.1      fvdl 			break;
    409   1.1      fvdl 		}
    410   1.1      fvdl 		if ((lkp->lk_flags & LK_WANT_UPGRADE) == 0) {
    411   1.1      fvdl 			/*
    412   1.1      fvdl 			 * We are first shared lock to request an upgrade, so
    413   1.1      fvdl 			 * request upgrade and wait for the shared count to
    414   1.1      fvdl 			 * drop to zero, then take exclusive lock.
    415   1.1      fvdl 			 */
    416   1.1      fvdl 			lkp->lk_flags |= LK_WANT_UPGRADE;
    417   1.1      fvdl 			ACQUIRE(lkp, error, extflags, lkp->lk_sharecount);
    418   1.1      fvdl 			lkp->lk_flags &= ~LK_WANT_UPGRADE;
    419   1.1      fvdl 			if (error)
    420   1.1      fvdl 				break;
    421   1.1      fvdl 			lkp->lk_flags |= LK_HAVE_EXCL;
    422  1.19   thorpej 			SETHOLDER(lkp, pid, cpu_id);
    423  1.21   thorpej 			HAVEIT(lkp);
    424   1.1      fvdl 			if (lkp->lk_exclusivecount != 0)
    425   1.1      fvdl 				panic("lockmgr: non-zero exclusive count");
    426   1.1      fvdl 			lkp->lk_exclusivecount = 1;
    427  1.15      fvdl 			if (extflags & LK_SETRECURSE)
    428  1.15      fvdl 				lkp->lk_recurselevel = 1;
    429  1.21   thorpej 			COUNT(lkp, p, cpu_id, 1);
    430   1.1      fvdl 			break;
    431   1.1      fvdl 		}
    432   1.1      fvdl 		/*
    433   1.1      fvdl 		 * Someone else has requested upgrade. Release our shared
    434   1.1      fvdl 		 * lock, awaken upgrade requestor if we are the last shared
    435   1.1      fvdl 		 * lock, then request an exclusive lock.
    436   1.1      fvdl 		 */
    437   1.1      fvdl 		if (lkp->lk_sharecount == 0 && lkp->lk_waitcount)
    438  1.20   thorpej 			wakeup_one((void *)lkp);
    439   1.1      fvdl 		/* fall into exclusive request */
    440   1.1      fvdl 
    441   1.1      fvdl 	case LK_EXCLUSIVE:
    442  1.19   thorpej 		if (WEHOLDIT(lkp, pid, cpu_id)) {
    443   1.1      fvdl 			/*
    444  1.19   thorpej 			 * Recursive lock.
    445   1.1      fvdl 			 */
    446  1.15      fvdl 			if ((extflags & LK_CANRECURSE) == 0 &&
    447  1.16  sommerfe 			     lkp->lk_recurselevel == 0) {
    448  1.16  sommerfe 				if (extflags & LK_RECURSEFAIL) {
    449  1.16  sommerfe 					error = EDEADLK;
    450  1.16  sommerfe 					break;
    451  1.16  sommerfe 				} else
    452  1.16  sommerfe 					panic("lockmgr: locking against myself");
    453  1.16  sommerfe 			}
    454   1.1      fvdl 			lkp->lk_exclusivecount++;
    455  1.15      fvdl 			if (extflags & LK_SETRECURSE &&
    456  1.15      fvdl 			    lkp->lk_recurselevel == 0)
    457  1.15      fvdl 				lkp->lk_recurselevel = lkp->lk_exclusivecount;
    458  1.21   thorpej 			COUNT(lkp, p, cpu_id, 1);
    459   1.1      fvdl 			break;
    460   1.1      fvdl 		}
    461   1.1      fvdl 		/*
    462   1.1      fvdl 		 * If we are just polling, check to see if we will sleep.
    463   1.1      fvdl 		 */
    464   1.1      fvdl 		if ((extflags & LK_NOWAIT) && ((lkp->lk_flags &
    465   1.1      fvdl 		     (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
    466   1.1      fvdl 		     lkp->lk_sharecount != 0)) {
    467   1.1      fvdl 			error = EBUSY;
    468   1.1      fvdl 			break;
    469   1.1      fvdl 		}
    470   1.1      fvdl 		/*
    471   1.1      fvdl 		 * Try to acquire the want_exclusive flag.
    472   1.1      fvdl 		 */
    473   1.1      fvdl 		ACQUIRE(lkp, error, extflags, lkp->lk_flags &
    474   1.1      fvdl 		    (LK_HAVE_EXCL | LK_WANT_EXCL));
    475   1.1      fvdl 		if (error)
    476   1.1      fvdl 			break;
    477   1.1      fvdl 		lkp->lk_flags |= LK_WANT_EXCL;
    478   1.1      fvdl 		/*
    479   1.1      fvdl 		 * Wait for shared locks and upgrades to finish.
    480   1.1      fvdl 		 */
    481   1.1      fvdl 		ACQUIRE(lkp, error, extflags, lkp->lk_sharecount != 0 ||
    482   1.1      fvdl 		       (lkp->lk_flags & LK_WANT_UPGRADE));
    483   1.1      fvdl 		lkp->lk_flags &= ~LK_WANT_EXCL;
    484   1.1      fvdl 		if (error)
    485   1.1      fvdl 			break;
    486   1.1      fvdl 		lkp->lk_flags |= LK_HAVE_EXCL;
    487  1.19   thorpej 		SETHOLDER(lkp, pid, cpu_id);
    488  1.21   thorpej 		HAVEIT(lkp);
    489   1.1      fvdl 		if (lkp->lk_exclusivecount != 0)
    490   1.1      fvdl 			panic("lockmgr: non-zero exclusive count");
    491   1.1      fvdl 		lkp->lk_exclusivecount = 1;
    492  1.15      fvdl 		if (extflags & LK_SETRECURSE)
    493  1.15      fvdl 			lkp->lk_recurselevel = 1;
    494  1.21   thorpej 		COUNT(lkp, p, cpu_id, 1);
    495   1.1      fvdl 		break;
    496   1.1      fvdl 
    497   1.1      fvdl 	case LK_RELEASE:
    498   1.1      fvdl 		if (lkp->lk_exclusivecount != 0) {
    499  1.19   thorpej 			if (WEHOLDIT(lkp, pid, cpu_id) == 0) {
    500  1.19   thorpej 				if (lkp->lk_flags & LK_SPIN) {
    501  1.19   thorpej 					panic("lockmgr: processor %lu, not "
    502  1.19   thorpej 					    "exclusive lock holder %lu "
    503  1.19   thorpej 					    "unlocking", cpu_id, lkp->lk_cpu);
    504  1.19   thorpej 				} else {
    505  1.19   thorpej 					panic("lockmgr: pid %d, not "
    506  1.19   thorpej 					    "exclusive lock holder %d "
    507  1.19   thorpej 					    "unlocking", pid,
    508  1.19   thorpej 					    lkp->lk_lockholder);
    509  1.19   thorpej 				}
    510  1.19   thorpej 			}
    511  1.15      fvdl 			if (lkp->lk_exclusivecount == lkp->lk_recurselevel)
    512  1.15      fvdl 				lkp->lk_recurselevel = 0;
    513   1.1      fvdl 			lkp->lk_exclusivecount--;
    514  1.21   thorpej 			COUNT(lkp, p, cpu_id, -1);
    515   1.1      fvdl 			if (lkp->lk_exclusivecount == 0) {
    516   1.1      fvdl 				lkp->lk_flags &= ~LK_HAVE_EXCL;
    517  1.19   thorpej 				SETHOLDER(lkp, LK_NOPROC, LK_NOCPU);
    518  1.21   thorpej 				DONTHAVEIT(lkp);
    519   1.1      fvdl 			}
    520   1.1      fvdl 		} else if (lkp->lk_sharecount != 0) {
    521   1.1      fvdl 			lkp->lk_sharecount--;
    522  1.21   thorpej 			COUNT(lkp, p, cpu_id, -1);
    523   1.1      fvdl 		}
    524   1.1      fvdl 		if (lkp->lk_waitcount)
    525  1.20   thorpej 			wakeup_one((void *)lkp);
    526   1.1      fvdl 		break;
    527   1.1      fvdl 
    528   1.1      fvdl 	case LK_DRAIN:
    529   1.1      fvdl 		/*
    530   1.1      fvdl 		 * Check that we do not already hold the lock, as it can
    531   1.1      fvdl 		 * never drain if we do. Unfortunately, we have no way to
    532   1.1      fvdl 		 * check for holding a shared lock, but at least we can
    533   1.1      fvdl 		 * check for an exclusive one.
    534   1.1      fvdl 		 */
    535  1.19   thorpej 		if (WEHOLDIT(lkp, pid, cpu_id))
    536   1.1      fvdl 			panic("lockmgr: draining against myself");
    537   1.1      fvdl 		/*
    538   1.1      fvdl 		 * If we are just polling, check to see if we will sleep.
    539   1.1      fvdl 		 */
    540   1.1      fvdl 		if ((extflags & LK_NOWAIT) && ((lkp->lk_flags &
    541   1.1      fvdl 		     (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
    542   1.1      fvdl 		     lkp->lk_sharecount != 0 || lkp->lk_waitcount != 0)) {
    543   1.1      fvdl 			error = EBUSY;
    544   1.1      fvdl 			break;
    545   1.1      fvdl 		}
    546  1.19   thorpej 		if (lkp->lk_flags & LK_SPIN) {
    547  1.19   thorpej 			ACQUIRE(lkp, error, extflags,
    548  1.19   thorpej 			    ((lkp->lk_flags &
    549  1.19   thorpej 			     (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
    550  1.19   thorpej 			     lkp->lk_sharecount != 0 ||
    551  1.19   thorpej 			     lkp->lk_waitcount != 0));
    552  1.19   thorpej 		} else {
    553  1.19   thorpej 			/*
    554  1.19   thorpej 			 * This is just a special cause of the sleep case
    555  1.19   thorpej 			 * in ACQUIRE().  We set WANTDRAIN instead of
    556  1.19   thorpej 			 * incrementing waitcount.
    557  1.19   thorpej 			 */
    558  1.19   thorpej 			for (error = 0; ((lkp->lk_flags &
    559  1.19   thorpej 			     (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
    560  1.19   thorpej 			     lkp->lk_sharecount != 0 ||
    561  1.19   thorpej 			     lkp->lk_waitcount != 0); ) {
    562  1.19   thorpej 				lkp->lk_flags |= LK_WAITDRAIN;
    563  1.19   thorpej 				simple_unlock(&lkp->lk_interlock);
    564  1.19   thorpej 				if ((error = tsleep((void *)&lkp->lk_flags,
    565  1.19   thorpej 				    lkp->lk_prio, lkp->lk_wmesg, lkp->lk_timo)))
    566  1.19   thorpej 					return (error);
    567  1.19   thorpej 				if ((extflags) & LK_SLEEPFAIL)
    568  1.19   thorpej 					return (ENOLCK);
    569  1.19   thorpej 				simple_lock(&lkp->lk_interlock);
    570  1.19   thorpej 			}
    571   1.1      fvdl 		}
    572   1.1      fvdl 		lkp->lk_flags |= LK_DRAINING | LK_HAVE_EXCL;
    573  1.19   thorpej 		SETHOLDER(lkp, pid, cpu_id);
    574  1.21   thorpej 		HAVEIT(lkp);
    575   1.1      fvdl 		lkp->lk_exclusivecount = 1;
    576  1.15      fvdl 		/* XXX unlikely that we'd want this */
    577  1.15      fvdl 		if (extflags & LK_SETRECURSE)
    578  1.15      fvdl 			lkp->lk_recurselevel = 1;
    579  1.21   thorpej 		COUNT(lkp, p, cpu_id, 1);
    580   1.1      fvdl 		break;
    581   1.1      fvdl 
    582   1.1      fvdl 	default:
    583   1.1      fvdl 		simple_unlock(&lkp->lk_interlock);
    584   1.1      fvdl 		panic("lockmgr: unknown locktype request %d",
    585   1.1      fvdl 		    flags & LK_TYPE_MASK);
    586   1.1      fvdl 		/* NOTREACHED */
    587   1.1      fvdl 	}
    588   1.1      fvdl 	if ((lkp->lk_flags & LK_WAITDRAIN) && ((lkp->lk_flags &
    589   1.1      fvdl 	     (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) == 0 &&
    590   1.1      fvdl 	     lkp->lk_sharecount == 0 && lkp->lk_waitcount == 0)) {
    591   1.1      fvdl 		lkp->lk_flags &= ~LK_WAITDRAIN;
    592  1.20   thorpej 		wakeup_one((void *)&lkp->lk_flags);
    593   1.1      fvdl 	}
    594   1.1      fvdl 	simple_unlock(&lkp->lk_interlock);
    595   1.1      fvdl 	return (error);
    596   1.1      fvdl }
    597   1.1      fvdl 
    598   1.1      fvdl /*
    599   1.1      fvdl  * Print out information about state of a lock. Used by VOP_PRINT
    600   1.1      fvdl  * routines to display ststus about contained locks.
    601   1.1      fvdl  */
    602   1.2      fvdl void
    603   1.1      fvdl lockmgr_printinfo(lkp)
    604  1.19   thorpej 	__volatile struct lock *lkp;
    605   1.1      fvdl {
    606   1.1      fvdl 
    607   1.1      fvdl 	if (lkp->lk_sharecount)
    608   1.1      fvdl 		printf(" lock type %s: SHARED (count %d)", lkp->lk_wmesg,
    609   1.1      fvdl 		    lkp->lk_sharecount);
    610  1.19   thorpej 	else if (lkp->lk_flags & LK_HAVE_EXCL) {
    611  1.19   thorpej 		printf(" lock type %s: EXCL (count %d) by ",
    612  1.19   thorpej 		    lkp->lk_wmesg, lkp->lk_exclusivecount);
    613  1.19   thorpej 		if (lkp->lk_flags & LK_SPIN)
    614  1.19   thorpej 			printf("processor %lu", lkp->lk_cpu);
    615  1.19   thorpej 		else
    616  1.19   thorpej 			printf("pid %d", lkp->lk_lockholder);
    617  1.19   thorpej 	} else
    618  1.19   thorpej 		printf(" not locked");
    619  1.19   thorpej 	if ((lkp->lk_flags & LK_SPIN) == 0 && lkp->lk_waitcount > 0)
    620   1.1      fvdl 		printf(" with %d pending", lkp->lk_waitcount);
    621   1.1      fvdl }
    622   1.1      fvdl 
    623  1.21   thorpej #if defined(LOCKDEBUG) /* { */
    624  1.21   thorpej TAILQ_HEAD(, simplelock) simplelock_list =
    625  1.21   thorpej     TAILQ_HEAD_INITIALIZER(simplelock_list);
    626  1.21   thorpej 
    627  1.21   thorpej #if defined(MULTIPROCESSOR) /* { */
    628  1.21   thorpej struct simplelock simplelock_list_slock = SIMPLELOCK_INITIALIZER;
    629  1.21   thorpej 
    630  1.21   thorpej #define	SLOCK_LIST_LOCK()						\
    631  1.21   thorpej 	cpu_simple_lock(&simplelock_list_slock)
    632  1.21   thorpej 
    633  1.21   thorpej #define	SLOCK_LIST_UNLOCK()						\
    634  1.21   thorpej 	cpu_simple_unlock(&simplelock_list_slock)
    635  1.21   thorpej 
    636  1.21   thorpej #define	SLOCK_COUNT(x)							\
    637  1.21   thorpej 	/* atomic_add_ulong(&curcpu()->ci_simple_locks, (x)) */
    638  1.21   thorpej #else
    639  1.21   thorpej u_long simple_locks;
    640  1.21   thorpej 
    641  1.21   thorpej #define	SLOCK_LIST_LOCK()	/* nothing */
    642  1.21   thorpej 
    643  1.21   thorpej #define	SLOCK_LIST_UNLOCK()	/* nothing */
    644  1.21   thorpej 
    645  1.21   thorpej #define	SLOCK_COUNT(x)		simple_locks += (x)
    646  1.21   thorpej #endif /* MULTIPROCESSOR */ /* } */
    647  1.21   thorpej 
    648  1.21   thorpej #ifdef DDB /* { */
    649  1.18       chs int simple_lock_debugger = 0;
    650  1.21   thorpej #define	SLOCK_DEBUGGER()	if (simple_lock_debugger) Debugger()
    651  1.21   thorpej #else
    652  1.21   thorpej #define	SLOCK_DEBUGGER()	/* nothing */
    653  1.21   thorpej #endif /* } */
    654  1.21   thorpej 
    655  1.21   thorpej #define	SLOCK_WHERE(str, alp, id, l)					\
    656  1.21   thorpej do {									\
    657  1.21   thorpej 	printf(str);							\
    658  1.21   thorpej 	printf("currently at: %s:%d\n", (id), (l));			\
    659  1.21   thorpej 	if ((alp)->lock_file != NULL)					\
    660  1.21   thorpej 		printf("last locked: %s:%d\n", (alp)->lock_file,	\
    661  1.21   thorpej 		    (alp)->lock_line);					\
    662  1.21   thorpej 	if ((alp)->unlock_file != NULL)					\
    663  1.21   thorpej 		printf("last unlocked: %s:%d\n", (alp)->unlock_file,	\
    664  1.21   thorpej 		    (alp)->unlock_line);				\
    665  1.21   thorpej 	SLOCK_DEBUGGER();						\
    666  1.21   thorpej } while (0)
    667  1.12       chs 
    668   1.1      fvdl /*
    669   1.1      fvdl  * Simple lock functions so that the debugger can see from whence
    670   1.1      fvdl  * they are being called.
    671   1.1      fvdl  */
    672   1.1      fvdl void
    673   1.1      fvdl simple_lock_init(alp)
    674   1.1      fvdl 	struct simplelock *alp;
    675   1.1      fvdl {
    676  1.21   thorpej 
    677  1.21   thorpej #if defined(MULTIPROCESSOR) /* { */
    678  1.21   thorpej 	cpu_simple_lock_init(alp);
    679  1.21   thorpej #else
    680  1.21   thorpej 	alp->lock_data = SIMPLELOCK_UNLOCKED;
    681  1.21   thorpej #endif /* } */
    682   1.5       chs 	alp->lock_file = NULL;
    683   1.5       chs 	alp->lock_line = 0;
    684   1.5       chs 	alp->unlock_file = NULL;
    685   1.5       chs 	alp->unlock_line = 0;
    686  1.10   thorpej 	alp->lock_holder = 0;
    687   1.1      fvdl }
    688   1.1      fvdl 
    689   1.1      fvdl void
    690   1.1      fvdl _simple_lock(alp, id, l)
    691   1.1      fvdl 	__volatile struct simplelock *alp;
    692   1.1      fvdl 	const char *id;
    693   1.1      fvdl 	int l;
    694   1.1      fvdl {
    695  1.21   thorpej 	u_long cpu_id = 0 /* XXX cpu_number() XXX */;
    696  1.12       chs 	int s;
    697  1.12       chs 
    698  1.18       chs 	s = splhigh();
    699  1.21   thorpej 
    700  1.21   thorpej 	/*
    701  1.21   thorpej 	 * MULTIPROCESSOR case: This is `safe' since if it's not us, we
    702  1.21   thorpej 	 * don't take any action, and just fall into the normal spin case.
    703  1.21   thorpej 	 */
    704  1.21   thorpej 	if (alp->lock_data == SIMPLELOCK_LOCKED) {
    705  1.21   thorpej #if defined(MULTIPROCESSOR) /* { */
    706  1.21   thorpej 		if (alp->lock_holder == cpu_id) {
    707  1.21   thorpej 			SLOCK_WHERE("simple_lock: locking against myself\n",
    708  1.21   thorpej 			    alp, id, l);
    709  1.21   thorpej 			goto out;
    710   1.1      fvdl 		}
    711  1.21   thorpej #else
    712  1.21   thorpej 		SLOCK_WHERE("simple_lock: lock held\n", alp, id, l);
    713  1.21   thorpej 		goto out;
    714  1.21   thorpej #endif /* MULTIPROCESSOR */ /* } */
    715   1.1      fvdl 	}
    716  1.21   thorpej 
    717  1.21   thorpej #if defined(MULTIPROCESSOR) /* { */
    718  1.21   thorpej 	/* Acquire the lock before modifying any fields. */
    719  1.21   thorpej 	cpu_simple_lock(alp);
    720  1.21   thorpej #else
    721  1.21   thorpej 	alp->lock_data = SIMPLELOCK_LOCKED;
    722  1.21   thorpej #endif /* } */
    723  1.21   thorpej 
    724   1.5       chs 	alp->lock_file = id;
    725   1.5       chs 	alp->lock_line = l;
    726  1.21   thorpej 	alp->lock_holder = cpu_id;
    727  1.21   thorpej 
    728  1.21   thorpej 	SLOCK_LIST_LOCK();
    729  1.21   thorpej 	/* XXX Cast away volatile */
    730  1.21   thorpej 	TAILQ_INSERT_TAIL(&simplelock_list, (struct simplelock *)alp, list);
    731  1.21   thorpej 	SLOCK_LIST_UNLOCK();
    732  1.21   thorpej 
    733  1.21   thorpej 	SLOCK_COUNT(1);
    734  1.21   thorpej 
    735  1.21   thorpej  out:
    736  1.18       chs 	splx(s);
    737   1.1      fvdl }
    738   1.1      fvdl 
    739   1.1      fvdl int
    740   1.1      fvdl _simple_lock_try(alp, id, l)
    741   1.1      fvdl 	__volatile struct simplelock *alp;
    742   1.1      fvdl 	const char *id;
    743   1.1      fvdl 	int l;
    744   1.1      fvdl {
    745  1.21   thorpej 	u_long cpu_id = 0 /* XXX cpu_number() XXX */;
    746  1.21   thorpej 	int s, rv = 0;
    747   1.1      fvdl 
    748  1.18       chs 	s = splhigh();
    749  1.21   thorpej 
    750  1.21   thorpej 	/*
    751  1.21   thorpej 	 * MULTIPROCESSOR case: This is `safe' since if it's not us, we
    752  1.21   thorpej 	 * don't take any action.
    753  1.21   thorpej 	 */
    754  1.21   thorpej #if defined(MULTIPROCESSOR) /* { */
    755  1.21   thorpej 	if ((rv = cpu_simple_lock_try(alp)) == 0) {
    756  1.21   thorpej 		if (alp->lock_holder == cpu_id)
    757  1.21   thorpej 			SLOCK_WHERE("simple_lock_try: locking against myself\n",
    758  1.21   thorpej 			    alp, id l);
    759  1.21   thorpej 		goto out;
    760  1.21   thorpej 	}
    761  1.21   thorpej #else
    762  1.21   thorpej 	if (alp->lock_data == SIMPLELOCK_LOCKED) {
    763  1.21   thorpej 		SLOCK_WHERE("simple_lock_try: lock held\n", alp, id, l);
    764  1.21   thorpej 		goto out;
    765  1.18       chs 	}
    766  1.21   thorpej 	alp->lock_data = SIMPLELOCK_LOCKED;
    767  1.21   thorpej #endif /* MULTIPROCESSOR */ /* } */
    768  1.21   thorpej 
    769  1.21   thorpej 	/*
    770  1.21   thorpej 	 * At this point, we have acquired the lock.
    771  1.21   thorpej 	 */
    772  1.21   thorpej 
    773  1.21   thorpej 	rv = 1;
    774  1.18       chs 
    775   1.5       chs 	alp->lock_file = id;
    776   1.5       chs 	alp->lock_line = l;
    777  1.21   thorpej 	alp->lock_holder = cpu_id;
    778  1.21   thorpej 
    779  1.21   thorpej 	SLOCK_LIST_LOCK();
    780  1.21   thorpej 	/* XXX Cast away volatile. */
    781  1.21   thorpej 	TAILQ_INSERT_TAIL(&simplelock_list, (struct simplelock *)alp, list);
    782  1.21   thorpej 	SLOCK_LIST_UNLOCK();
    783  1.21   thorpej 
    784  1.21   thorpej 	SLOCK_COUNT(1);
    785  1.21   thorpej 
    786  1.21   thorpej  out:
    787  1.12       chs 	splx(s);
    788  1.21   thorpej 	return (rv);
    789   1.1      fvdl }
    790   1.1      fvdl 
    791   1.1      fvdl void
    792   1.1      fvdl _simple_unlock(alp, id, l)
    793   1.1      fvdl 	__volatile struct simplelock *alp;
    794   1.1      fvdl 	const char *id;
    795   1.1      fvdl 	int l;
    796   1.1      fvdl {
    797  1.12       chs 	int s;
    798   1.1      fvdl 
    799  1.18       chs 	s = splhigh();
    800  1.21   thorpej 
    801  1.21   thorpej 	/*
    802  1.21   thorpej 	 * MULTIPROCESSOR case: This is `safe' because we think we hold
    803  1.21   thorpej 	 * the lock, and if we don't, we don't take any action.
    804  1.21   thorpej 	 */
    805  1.21   thorpej 	if (alp->lock_data == SIMPLELOCK_UNLOCKED) {
    806  1.21   thorpej 		SLOCK_WHERE("simple_unlock: lock not held\n",
    807  1.21   thorpej 		    alp, id, l);
    808  1.21   thorpej 		goto out;
    809  1.21   thorpej 	}
    810  1.21   thorpej 
    811  1.21   thorpej 	SLOCK_LIST_LOCK();
    812  1.21   thorpej 	TAILQ_REMOVE(&simplelock_list, alp, list);
    813  1.21   thorpej 	SLOCK_LIST_UNLOCK();
    814  1.21   thorpej 
    815  1.21   thorpej 	SLOCK_COUNT(-1);
    816  1.21   thorpej 
    817  1.21   thorpej 	alp->list.tqe_next = NULL;	/* sanity */
    818  1.21   thorpej 	alp->list.tqe_prev = NULL;	/* sanity */
    819  1.21   thorpej 
    820   1.5       chs 	alp->unlock_file = id;
    821   1.5       chs 	alp->unlock_line = l;
    822  1.21   thorpej 
    823  1.21   thorpej #if defined(MULTIPROCESSOR) /* { */
    824  1.21   thorpej 	/* Now that we've modified all fields, release the lock. */
    825  1.21   thorpej 	cpu_simple_unlock(alp);
    826  1.21   thorpej #else
    827  1.21   thorpej 	alp->lock_data = SIMPLELOCK_UNLOCKED;
    828  1.21   thorpej #endif /* } */
    829  1.21   thorpej 
    830  1.21   thorpej  out:
    831  1.18       chs 	splx(s);
    832  1.12       chs }
    833  1.12       chs 
    834  1.12       chs void
    835  1.12       chs simple_lock_dump()
    836  1.12       chs {
    837  1.12       chs 	struct simplelock *alp;
    838  1.12       chs 	int s;
    839  1.12       chs 
    840  1.12       chs 	s = splhigh();
    841  1.21   thorpej 	SLOCK_LIST_LOCK();
    842  1.12       chs 	printf("all simple locks:\n");
    843  1.21   thorpej 	for (alp = TAILQ_FIRST(&simplelock_list); alp != NULL;
    844  1.21   thorpej 	     alp = TAILQ_NEXT(alp, list)) {
    845  1.21   thorpej 		printf("%p CPU %lu %s:%d\n", alp, alp->lock_holder,
    846  1.21   thorpej 		    alp->lock_file, alp->lock_line);
    847  1.12       chs 	}
    848  1.21   thorpej 	SLOCK_LIST_UNLOCK();
    849  1.12       chs 	splx(s);
    850  1.12       chs }
    851  1.12       chs 
    852  1.12       chs void
    853  1.12       chs simple_lock_freecheck(start, end)
    854  1.21   thorpej 	void *start, *end;
    855  1.12       chs {
    856  1.12       chs 	struct simplelock *alp;
    857  1.12       chs 	int s;
    858  1.12       chs 
    859  1.12       chs 	s = splhigh();
    860  1.21   thorpej 	SLOCK_LIST_LOCK();
    861  1.21   thorpej 	for (alp = TAILQ_FIRST(&simplelock_list); alp != NULL;
    862  1.21   thorpej 	     alp = TAILQ_NEXT(alp, list)) {
    863  1.12       chs 		if ((void *)alp >= start && (void *)alp < end) {
    864  1.21   thorpej 			printf("freeing simple_lock %p CPU %lu %s:%d\n",
    865  1.21   thorpej 			    alp, alp->lock_holder, alp->lock_file,
    866  1.21   thorpej 			    alp->lock_line);
    867  1.21   thorpej 			SLOCK_DEBUGGER();
    868  1.12       chs 		}
    869  1.12       chs 	}
    870  1.21   thorpej 	SLOCK_LIST_UNLOCK();
    871  1.12       chs 	splx(s);
    872   1.1      fvdl }
    873  1.21   thorpej #endif /* LOCKDEBUG */ /* } */
    874