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