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