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