| Commit | Line | Data |
|---|---|---|
| 984263bc MD |
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 $ | |
| ba39e2e0 | 27 | * $DragonFly: src/sys/kern/subr_kobj.c,v 1.9 2007/04/30 07:18:54 dillon Exp $ |
| 984263bc MD |
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> | |
| 0486864b JS |
36 | #include <sys/thread.h> |
| 37 | #include <sys/thread2.h> | |
| 984263bc MD |
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 | #ifdef KOBJ_STATS | |
| 50 | ||
| 51 | #include <sys/sysctl.h> | |
| 52 | ||
| e6e9601e JR |
53 | u_int kobj_lookup_hits; |
| 54 | u_int kobj_lookup_misses; | |
| 984263bc | 55 | |
| e6e9601e | 56 | SYSCTL_UINT(_kern, OID_AUTO, kobj_hits, CTLFLAG_RD, |
| 984263bc | 57 | &kobj_lookup_hits, 0, "") |
| e6e9601e | 58 | SYSCTL_UINT(_kern, OID_AUTO, kobj_misses, CTLFLAG_RD, |
| 984263bc MD |
59 | &kobj_lookup_misses, 0, "") |
| 60 | ||
| 61 | #endif | |
| 62 | ||
| 0486864b | 63 | static struct lwkt_token kobj_token; |
| 984263bc MD |
64 | static int kobj_next_id = 1; |
| 65 | ||
| 0486864b JS |
66 | static void |
| 67 | kobj_init_token(void *arg) | |
| 68 | { | |
| 3b998fa9 | 69 | lwkt_token_init(&kobj_token, 1); |
| 0486864b JS |
70 | } |
| 71 | ||
| ba39e2e0 | 72 | SYSINIT(kobj, SI_BOOT1_LOCK, SI_ORDER_ANY, kobj_init_token, NULL); |
| 0486864b | 73 | |
| b3dd25cb JS |
74 | /* |
| 75 | * This method structure is used to initialise new caches. Since the | |
| 76 | * desc pointer is NULL, it is guaranteed never to match any real | |
| 77 | * descriptors. | |
| 78 | */ | |
| 79 | static struct kobj_method null_method = { | |
| 80 | 0, 0, | |
| 81 | }; | |
| 82 | ||
| 83 | int | |
| 984263bc MD |
84 | kobj_error_method(void) |
| 85 | { | |
| 86 | return ENXIO; | |
| 87 | } | |
| 88 | ||
| 89 | static void | |
| 90 | kobj_register_method(struct kobjop_desc *desc) | |
| 91 | { | |
| 92 | if (desc->id == 0) | |
| 93 | desc->id = kobj_next_id++; | |
| 94 | } | |
| 95 | ||
| 96 | static void | |
| 97 | kobj_unregister_method(struct kobjop_desc *desc) | |
| 98 | { | |
| 99 | } | |
| 100 | ||
| 101 | static void | |
| b4f5425e | 102 | kobj_class_compile(kobj_class_t cls) |
| 984263bc MD |
103 | { |
| 104 | kobj_method_t *m; | |
| 0486864b | 105 | kobj_ops_t ops; |
| b3dd25cb | 106 | int i; |
| 984263bc MD |
107 | |
| 108 | /* | |
| 109 | * Don't do anything if we are already compiled. | |
| 110 | */ | |
| 111 | if (cls->ops) | |
| 112 | return; | |
| 113 | ||
| 114 | /* | |
| 984263bc MD |
115 | * Allocate space for the compiled ops table. |
| 116 | */ | |
| efda3bd0 | 117 | ops = kmalloc(sizeof(struct kobj_ops), M_KOBJ, M_INTWAIT); |
| b3dd25cb JS |
118 | for (i = 0; i < KOBJ_CACHE_SIZE; i++) |
| 119 | ops->cache[i] = &null_method; | |
| 0486864b JS |
120 | if (cls->ops) { |
| 121 | /* | |
| 122 | * In case of preemption, another thread might have been faster, | |
| 123 | * but that's fine for us. | |
| 124 | */ | |
| 125 | if (ops) | |
| efda3bd0 | 126 | kfree(ops, M_KOBJ); |
| 0486864b JS |
127 | return; |
| 128 | } | |
| 129 | ||
| 130 | if (!ops) | |
| 984263bc | 131 | panic("kobj_compile_methods: out of memory"); |
| 0486864b JS |
132 | |
| 133 | ops->cls = cls; | |
| 134 | cls->ops = ops; | |
| 984263bc | 135 | |
| 984263bc | 136 | /* |
| b4f5425e | 137 | * Afterwards register any methods which need it. |
| 984263bc | 138 | */ |
| b4f5425e JS |
139 | for (m = cls->methods; m->desc; m++) |
| 140 | kobj_register_method(m->desc); | |
| 984263bc MD |
141 | } |
| 142 | ||
| b3dd25cb JS |
143 | static kobj_method_t * |
| 144 | kobj_lookup_method_class(kobj_class_t cls, kobjop_desc_t desc) | |
| 145 | { | |
| 146 | kobj_method_t *methods = cls->methods; | |
| 147 | kobj_method_t *ce; | |
| 148 | ||
| 149 | for (ce = methods; ce && ce->desc; ce++) | |
| 150 | if (ce->desc == desc) | |
| 151 | return(ce); | |
| 152 | ||
| 153 | return(0); | |
| 154 | } | |
| 155 | ||
| 156 | static kobj_method_t * | |
| 157 | kobj_lookup_method_mi(kobj_class_t cls, kobjop_desc_t desc) | |
| 984263bc | 158 | { |
| b3dd25cb JS |
159 | kobj_method_t *ce; |
| 160 | kobj_class_t *basep; | |
| 161 | ||
| 162 | ce = kobj_lookup_method_class(cls, desc); | |
| 163 | if (ce) | |
| 164 | return(ce); | |
| 165 | ||
| 166 | basep = cls->baseclasses; | |
| 167 | if (basep) { | |
| 168 | for (; *basep; basep++) { | |
| 169 | ce = kobj_lookup_method_mi(*basep, desc); | |
| 170 | if (ce) | |
| 171 | return(ce); | |
| 984263bc MD |
172 | } |
| 173 | } | |
| b3dd25cb JS |
174 | |
| 175 | return(0); | |
| 176 | } | |
| 177 | ||
| 178 | kobj_method_t* | |
| 179 | kobj_lookup_method(kobj_class_t cls, | |
| 180 | kobj_method_t **cep, | |
| 181 | kobjop_desc_t desc) | |
| 182 | { | |
| 183 | kobj_method_t *ce; | |
| 184 | ||
| 185 | #ifdef KOBJ_STATS | |
| 186 | /* | |
| 187 | * Correct for the 'hit' assumption in KOBJOPLOOKUP and record | |
| 188 | * a 'miss'. | |
| 189 | */ | |
| 190 | kobj_lookup_hits--; | |
| 191 | kobj_lookup_misses--; | |
| 192 | #endif | |
| 193 | ||
| 194 | ce = kobj_lookup_method_mi(cls, desc); | |
| 195 | if (!ce) | |
| 196 | ce = desc->deflt; | |
| 197 | *cep = ce; | |
| 198 | return(ce); | |
| 984263bc MD |
199 | } |
| 200 | ||
| b4f5425e | 201 | static void |
| 984263bc MD |
202 | kobj_class_free(kobj_class_t cls) |
| 203 | { | |
| 204 | int i; | |
| 205 | kobj_method_t *m; | |
| 206 | ||
| 207 | /* | |
| 208 | * Unregister any methods which are no longer used. | |
| 209 | */ | |
| 210 | for (i = 0, m = cls->methods; m->desc; i++, m++) | |
| 211 | kobj_unregister_method(m->desc); | |
| 212 | ||
| 213 | /* | |
| 214 | * Free memory and clean up. | |
| 215 | */ | |
| efda3bd0 | 216 | kfree(cls->ops, M_KOBJ); |
| 984263bc MD |
217 | cls->ops = 0; |
| 218 | } | |
| 219 | ||
| b4f5425e JS |
220 | void |
| 221 | kobj_class_instantiate(kobj_class_t cls) | |
| 222 | { | |
| 3b998fa9 | 223 | lwkt_gettoken(&kobj_token); |
| 0486864b JS |
224 | crit_enter(); |
| 225 | ||
| b4f5425e JS |
226 | if (!cls->ops) |
| 227 | kobj_class_compile(cls); | |
| 228 | cls->refs++; | |
| 0486864b JS |
229 | |
| 230 | crit_exit(); | |
| 3b998fa9 | 231 | lwkt_reltoken(&kobj_token); |
| b4f5425e JS |
232 | } |
| 233 | ||
| 234 | void | |
| 235 | kobj_class_uninstantiate(kobj_class_t cls) | |
| 236 | { | |
| 3b998fa9 | 237 | lwkt_gettoken(&kobj_token); |
| 0486864b JS |
238 | crit_enter(); |
| 239 | ||
| b4f5425e JS |
240 | cls->refs--; |
| 241 | if (cls->refs == 0) | |
| 242 | kobj_class_free(cls); | |
| 0486864b JS |
243 | |
| 244 | crit_exit(); | |
| 3b998fa9 | 245 | lwkt_reltoken(&kobj_token); |
| b4f5425e JS |
246 | } |
| 247 | ||
| 984263bc MD |
248 | kobj_t |
| 249 | kobj_create(kobj_class_t cls, | |
| 250 | struct malloc_type *mtype, | |
| 251 | int mflags) | |
| 252 | { | |
| 253 | kobj_t obj; | |
| 254 | ||
| 255 | /* | |
| 256 | * Allocate and initialise the new object. | |
| 257 | */ | |
| efda3bd0 | 258 | obj = kmalloc(cls->size, mtype, mflags | M_ZERO); |
| 984263bc MD |
259 | if (!obj) |
| 260 | return 0; | |
| 261 | kobj_init(obj, cls); | |
| 262 | ||
| 263 | return obj; | |
| 264 | } | |
| 265 | ||
| 266 | void | |
| 267 | kobj_init(kobj_t obj, kobj_class_t cls) | |
| 268 | { | |
| b4f5425e | 269 | kobj_class_instantiate(cls); |
| 984263bc | 270 | obj->ops = cls->ops; |
| 984263bc MD |
271 | } |
| 272 | ||
| 273 | void | |
| 274 | kobj_delete(kobj_t obj, struct malloc_type *mtype) | |
| 275 | { | |
| 276 | kobj_class_t cls = obj->ops->cls; | |
| 277 | ||
| b4f5425e | 278 | kobj_class_uninstantiate(cls); |
| 984263bc MD |
279 | |
| 280 | obj->ops = 0; | |
| 281 | if (mtype) | |
| efda3bd0 | 282 | kfree(obj, mtype); |
| 984263bc | 283 | } |