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