xref: /netbsd-src/sys/kern/kern_cfglock.c (revision 4a743ad47da19f6d12b34b0eb7eeb6ecab1caffe)
1*4a743ad4Spgoyette /*	$NetBSD: kern_cfglock.c,v 1.1 2010/08/21 13:17:31 pgoyette Exp $ */
2*4a743ad4Spgoyette 
3*4a743ad4Spgoyette /*-
4*4a743ad4Spgoyette  * Copyright (c) 2002, 2006, 2007, 2008, 2009 The NetBSD Foundation, Inc.
5*4a743ad4Spgoyette  * All rights reserved.
6*4a743ad4Spgoyette  *
7*4a743ad4Spgoyette  * This code is derived from software contributed to The NetBSD Foundation
8*4a743ad4Spgoyette  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9*4a743ad4Spgoyette  * NASA Ames Research Center, and by Andrew Doran.
10*4a743ad4Spgoyette  *
11*4a743ad4Spgoyette  * Redistribution and use in source and binary forms, with or without
12*4a743ad4Spgoyette  * modification, are permitted provided that the following conditions
13*4a743ad4Spgoyette  * are met:
14*4a743ad4Spgoyette  * 1. Redistributions of source code must retain the above copyright
15*4a743ad4Spgoyette  *    notice, this list of conditions and the following disclaimer.
16*4a743ad4Spgoyette  * 2. Redistributions in binary form must reproduce the above copyright
17*4a743ad4Spgoyette  *    notice, this list of conditions and the following disclaimer in the
18*4a743ad4Spgoyette  *    documentation and/or other materials provided with the distribution.
19*4a743ad4Spgoyette  *
20*4a743ad4Spgoyette  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21*4a743ad4Spgoyette  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22*4a743ad4Spgoyette  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23*4a743ad4Spgoyette  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24*4a743ad4Spgoyette  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25*4a743ad4Spgoyette  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26*4a743ad4Spgoyette  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27*4a743ad4Spgoyette  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28*4a743ad4Spgoyette  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29*4a743ad4Spgoyette  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30*4a743ad4Spgoyette  * POSSIBILITY OF SUCH DAMAGE.
31*4a743ad4Spgoyette  */
32*4a743ad4Spgoyette 
33*4a743ad4Spgoyette #include <sys/cdefs.h>
34*4a743ad4Spgoyette __KERNEL_RCSID(0, "$NetBSD: kern_cfglock.c,v 1.1 2010/08/21 13:17:31 pgoyette Exp $");
35*4a743ad4Spgoyette 
36*4a743ad4Spgoyette #include <sys/param.h>
37*4a743ad4Spgoyette #include <sys/cpu.h>
38*4a743ad4Spgoyette #include <sys/mutex.h>
39*4a743ad4Spgoyette #include <sys/lwp.h>
40*4a743ad4Spgoyette #include <sys/systm.h>
41*4a743ad4Spgoyette 
42*4a743ad4Spgoyette static kmutex_t kernconfig_mutex;
43*4a743ad4Spgoyette static lwp_t *kernconfig_lwp;
44*4a743ad4Spgoyette static int kernconfig_recurse;
45*4a743ad4Spgoyette 
46*4a743ad4Spgoyette /*
47*4a743ad4Spgoyette  * Functions for manipulating the kernel configuration lock.  This
48*4a743ad4Spgoyette  * recursive lock should be used to protect all additions and removals
49*4a743ad4Spgoyette  * of kernel functionality, such as device configuration and loading
50*4a743ad4Spgoyette  * of modular kernel components.
51*4a743ad4Spgoyette  */
52*4a743ad4Spgoyette 
53*4a743ad4Spgoyette void
kernconfig_lock_init(void)54*4a743ad4Spgoyette kernconfig_lock_init(void)
55*4a743ad4Spgoyette {
56*4a743ad4Spgoyette 
57*4a743ad4Spgoyette 	mutex_init(&kernconfig_mutex, MUTEX_DEFAULT, IPL_NONE);
58*4a743ad4Spgoyette 	kernconfig_lwp = NULL;
59*4a743ad4Spgoyette 	kernconfig_recurse = 0;
60*4a743ad4Spgoyette }
61*4a743ad4Spgoyette 
62*4a743ad4Spgoyette void
kernconfig_lock(void)63*4a743ad4Spgoyette kernconfig_lock(void)
64*4a743ad4Spgoyette {
65*4a743ad4Spgoyette 	lwp_t	*my_lwp;
66*4a743ad4Spgoyette 
67*4a743ad4Spgoyette 	/*
68*4a743ad4Spgoyette 	 * It's OK to check this unlocked, since it could only be set to
69*4a743ad4Spgoyette 	 * curlwp by the current thread itself, and not by an interrupt
70*4a743ad4Spgoyette 	 * or any other LWP.
71*4a743ad4Spgoyette 	 */
72*4a743ad4Spgoyette 	KASSERT(!cpu_intr_p());
73*4a743ad4Spgoyette 	my_lwp = curlwp;
74*4a743ad4Spgoyette 	if (kernconfig_lwp == my_lwp) {
75*4a743ad4Spgoyette 		kernconfig_recurse++;
76*4a743ad4Spgoyette 		KASSERT(kernconfig_recurse > 1);
77*4a743ad4Spgoyette 	} else {
78*4a743ad4Spgoyette 		mutex_enter(&kernconfig_mutex);
79*4a743ad4Spgoyette 		kernconfig_lwp = my_lwp;
80*4a743ad4Spgoyette 		kernconfig_recurse = 1;
81*4a743ad4Spgoyette 	}
82*4a743ad4Spgoyette }
83*4a743ad4Spgoyette 
84*4a743ad4Spgoyette void
kernconfig_unlock(void)85*4a743ad4Spgoyette kernconfig_unlock(void)
86*4a743ad4Spgoyette {
87*4a743ad4Spgoyette 
88*4a743ad4Spgoyette 	KASSERT(kernconfig_is_held());
89*4a743ad4Spgoyette 	KASSERT(kernconfig_recurse != 0);
90*4a743ad4Spgoyette 	if (--kernconfig_recurse == 0) {
91*4a743ad4Spgoyette 		kernconfig_lwp = NULL;
92*4a743ad4Spgoyette 		mutex_exit(&kernconfig_mutex);
93*4a743ad4Spgoyette 	}
94*4a743ad4Spgoyette }
95*4a743ad4Spgoyette 
96*4a743ad4Spgoyette bool
kernconfig_is_held(void)97*4a743ad4Spgoyette kernconfig_is_held(void)
98*4a743ad4Spgoyette {
99*4a743ad4Spgoyette 
100*4a743ad4Spgoyette 	return mutex_owned(&kernconfig_mutex);
101*4a743ad4Spgoyette }
102