kernel - Remove mplock shims from global tokens
[dragonfly.git] / sys / kern / subr_kobj.c
1 /*-
2  * Copyright (c) 2000 Doug Rabson
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/sys/kern/subr_kobj.c,v 1.4.2.1 2001/02/02 19:49:13 cg Exp $
27  * $DragonFly: src/sys/kern/subr_kobj.c,v 1.9 2007/04/30 07:18:54 dillon Exp $
28  */
29
30 #include <sys/param.h>
31 #include <sys/queue.h>
32 #include <sys/malloc.h>
33 #include <sys/kernel.h>
34 #include <sys/module.h>
35 #include <sys/errno.h>
36 #include <sys/thread.h>
37 #include <sys/thread2.h>
38 #ifndef TEST
39 #include <sys/systm.h>
40 #endif
41 #include <sys/kobj.h>
42
43 #ifdef TEST
44 #include "usertest.h"
45 #endif
46
47 static MALLOC_DEFINE(M_KOBJ, "kobj", "Kernel object structures");
48
49 static struct lwkt_token kobj_token;
50 static int kobj_next_id = 1;
51
52 static void
53 kobj_init_token(void *arg)
54 {
55         lwkt_token_init(&kobj_token, "kobj");
56 }
57
58 SYSINIT(kobj, SI_BOOT1_LOCK, SI_ORDER_ANY, kobj_init_token, NULL);
59
60 /*
61  * This method structure is used to initialise new caches. Since the
62  * desc pointer is NULL, it is guaranteed never to match any real
63  * descriptors.
64  */
65 static struct kobj_method null_method = {
66         0, 0,
67 };
68
69 int
70 kobj_error_method(void)
71 {
72         return ENXIO;
73 }
74
75 static void
76 kobj_register_method(struct kobjop_desc *desc)
77 {
78         if (desc->id == 0)
79                 desc->id = kobj_next_id++;
80 }
81
82 static void
83 kobj_unregister_method(struct kobjop_desc *desc)
84 {
85 }
86
87 static void
88 kobj_class_compile(kobj_class_t cls)
89 {
90         kobj_method_t *m;
91         kobj_ops_t ops;
92         int i;
93
94         /*
95          * Don't do anything if we are already compiled.
96          */
97         if (cls->ops)
98                 return;
99
100         /*
101          * Allocate space for the compiled ops table.
102          */
103         ops = kmalloc(sizeof(struct kobj_ops), M_KOBJ, M_INTWAIT);
104         for (i = 0; i < KOBJ_CACHE_SIZE; i++)
105                 ops->cache[i] = &null_method;
106         if (cls->ops) {
107                 /*
108                  * In case of preemption, another thread might have been faster,
109                  * but that's fine for us.
110                  */
111                 if (ops)
112                         kfree(ops, M_KOBJ);
113                 return;
114         }       
115
116         if (!ops)
117                 panic("kobj_compile_methods: out of memory");
118
119         ops->cls = cls;
120         cls->ops = ops;
121
122         /*
123          * Afterwards register any methods which need it.
124          */
125         for (m = cls->methods; m->desc; m++)
126                 kobj_register_method(m->desc);
127 }
128
129 static kobj_method_t *
130 kobj_lookup_method_class(kobj_class_t cls, kobjop_desc_t desc)
131 {
132         kobj_method_t *methods = cls->methods;
133         kobj_method_t *ce;
134
135         for (ce = methods; ce && ce->desc; ce++)
136                 if (ce->desc == desc)
137                         return(ce);
138
139         return(0);
140 }
141
142 static kobj_method_t *
143 kobj_lookup_method_mi(kobj_class_t cls, kobjop_desc_t desc)
144 {
145         kobj_method_t *ce;
146         kobj_class_t *basep;
147
148         ce = kobj_lookup_method_class(cls, desc);
149         if (ce)
150                 return(ce);
151
152         basep = cls->baseclasses;
153         if (basep) {
154                 for (; *basep; basep++) {
155                         ce = kobj_lookup_method_mi(*basep, desc);
156                         if (ce)
157                                 return(ce);
158                 }
159         }
160
161         return(0);
162 }
163
164 kobj_method_t*
165 kobj_lookup_method(kobj_class_t cls,
166                    kobj_method_t **cep,
167                    kobjop_desc_t desc)
168 {
169         kobj_method_t *ce;
170
171         ce = kobj_lookup_method_mi(cls, desc);
172         if (!ce)
173                 ce = desc->deflt;
174         *cep = ce;
175         return(ce);
176 }
177
178 /*
179  * This is called from the KOBJOPLOOKUP() macro in sys/kobj.h and
180  * replaces the original large body of the macro with a single
181  * procedure call.
182  */
183 kobjop_t
184 kobj_lookup_method_cache(kobj_class_t cls, kobj_method_t **cep,
185                          kobjop_desc_t desc)
186 {
187         kobj_method_t *ce;
188
189         cep = &cep[desc->id & (KOBJ_CACHE_SIZE-1)];
190         ce = *cep;
191         if (ce->desc != desc)
192                 ce = kobj_lookup_method(cls, cep, desc);
193         return(ce->func);
194 }
195
196 static void
197 kobj_class_free(kobj_class_t cls)
198 {
199         int i;
200         kobj_method_t *m;
201
202         /*
203          * Unregister any methods which are no longer used.
204          */
205         for (i = 0, m = cls->methods; m->desc; i++, m++)
206                 kobj_unregister_method(m->desc);
207
208         /*
209          * Free memory and clean up.
210          */
211         kfree(cls->ops, M_KOBJ);
212         cls->ops = 0;
213 }
214
215 void
216 kobj_class_instantiate(kobj_class_t cls)
217 {
218         lwkt_gettoken(&kobj_token);
219         crit_enter();
220
221         if (!cls->ops)
222                 kobj_class_compile(cls);
223         cls->refs++;
224
225         crit_exit();
226         lwkt_reltoken(&kobj_token);
227 }
228
229 void
230 kobj_class_uninstantiate(kobj_class_t cls)
231 {
232         lwkt_gettoken(&kobj_token);
233         crit_enter();
234
235         cls->refs--;
236         if (cls->refs == 0)
237                 kobj_class_free(cls);
238
239         crit_exit();
240         lwkt_reltoken(&kobj_token);
241 }
242
243 kobj_t
244 kobj_create(kobj_class_t cls,
245             struct malloc_type *mtype,
246             int mflags)
247 {
248         kobj_t obj;
249
250         /*
251          * Allocate and initialise the new object.
252          */
253         obj = kmalloc(cls->size, mtype, mflags | M_ZERO);
254         if (!obj)
255                 return 0;
256         kobj_init(obj, cls);
257
258         return obj;
259 }
260
261 void
262 kobj_init(kobj_t obj, kobj_class_t cls)
263 {
264         kobj_class_instantiate(cls);
265         obj->ops = cls->ops;
266 }
267
268 void
269 kobj_delete(kobj_t obj, struct malloc_type *mtype)
270 {
271         kobj_class_t cls = obj->ops->cls;
272
273         kobj_class_uninstantiate(cls);
274
275         obj->ops = 0;
276         if (mtype)
277                 kfree(obj, mtype);
278 }