| @@ -1,1239 +1,1239 @@ | | | @@ -1,1239 +1,1239 @@ |
1 | /* $NetBSD: kern_module.c,v 1.48 2009/06/09 20:35:02 jnemeth Exp $ */ | | 1 | /* $NetBSD: kern_module.c,v 1.49 2009/06/17 21:04:25 dyoung Exp $ */ |
2 | | | 2 | |
3 | /*- | | 3 | /*- |
4 | * Copyright (c) 2008 The NetBSD Foundation, Inc. | | 4 | * Copyright (c) 2008 The NetBSD Foundation, Inc. |
5 | * All rights reserved. | | 5 | * All rights reserved. |
6 | * | | 6 | * |
7 | * This code is derived from software developed for The NetBSD Foundation | | 7 | * This code is derived from software developed for The NetBSD Foundation |
8 | * by Andrew Doran. | | 8 | * by Andrew Doran. |
9 | * | | 9 | * |
10 | * Redistribution and use in source and binary forms, with or without | | 10 | * Redistribution and use in source and binary forms, with or without |
11 | * modification, are permitted provided that the following conditions | | 11 | * modification, are permitted provided that the following conditions |
12 | * are met: | | 12 | * are met: |
13 | * 1. Redistributions of source code must retain the above copyright | | 13 | * 1. Redistributions of source code must retain the above copyright |
14 | * notice, this list of conditions and the following disclaimer. | | 14 | * notice, this list of conditions and the following disclaimer. |
15 | * 2. Redistributions in binary form must reproduce the above copyright | | 15 | * 2. Redistributions in binary form must reproduce the above copyright |
16 | * notice, this list of conditions and the following disclaimer in the | | 16 | * notice, this list of conditions and the following disclaimer in the |
17 | * documentation and/or other materials provided with the distribution. | | 17 | * documentation and/or other materials provided with the distribution. |
18 | * | | 18 | * |
19 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS | | 19 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS |
20 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | | 20 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
21 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | | 21 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS | | 22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS |
23 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | | 23 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | | 24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | | 25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | | 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | | 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | | 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
29 | * POSSIBILITY OF SUCH DAMAGE. | | 29 | * POSSIBILITY OF SUCH DAMAGE. |
30 | */ | | 30 | */ |
31 | | | 31 | |
32 | /* | | 32 | /* |
33 | * Kernel module support. | | 33 | * Kernel module support. |
34 | */ | | 34 | */ |
35 | | | 35 | |
36 | #include <sys/cdefs.h> | | 36 | #include <sys/cdefs.h> |
37 | __KERNEL_RCSID(0, "$NetBSD: kern_module.c,v 1.48 2009/06/09 20:35:02 jnemeth Exp $"); | | 37 | __KERNEL_RCSID(0, "$NetBSD: kern_module.c,v 1.49 2009/06/17 21:04:25 dyoung Exp $"); |
38 | | | 38 | |
39 | #ifdef _KERNEL_OPT | | 39 | #ifdef _KERNEL_OPT |
40 | #include "opt_ddb.h" | | 40 | #include "opt_ddb.h" |
41 | #include "opt_modular.h" | | 41 | #include "opt_modular.h" |
42 | #endif | | 42 | #endif |
43 | | | 43 | |
44 | #include <sys/param.h> | | 44 | #include <sys/param.h> |
45 | #include <sys/systm.h> | | 45 | #include <sys/systm.h> |
46 | #include <sys/kernel.h> | | 46 | #include <sys/kernel.h> |
47 | #include <sys/fcntl.h> | | 47 | #include <sys/fcntl.h> |
48 | #include <sys/proc.h> | | 48 | #include <sys/proc.h> |
49 | #include <sys/kauth.h> | | 49 | #include <sys/kauth.h> |
50 | #include <sys/kobj.h> | | 50 | #include <sys/kobj.h> |
51 | #include <sys/kmem.h> | | 51 | #include <sys/kmem.h> |
52 | #include <sys/module.h> | | 52 | #include <sys/module.h> |
53 | #include <sys/kauth.h> | | 53 | #include <sys/kauth.h> |
54 | #include <sys/kthread.h> | | 54 | #include <sys/kthread.h> |
55 | #include <sys/sysctl.h> | | 55 | #include <sys/sysctl.h> |
56 | #include <sys/namei.h> | | 56 | #include <sys/namei.h> |
57 | #include <sys/lock.h> | | 57 | #include <sys/lock.h> |
58 | #include <sys/vnode.h> | | 58 | #include <sys/vnode.h> |
59 | #include <sys/stat.h> | | 59 | #include <sys/stat.h> |
60 | | | 60 | |
61 | #include <uvm/uvm_extern.h> | | 61 | #include <uvm/uvm_extern.h> |
62 | | | 62 | |
63 | #include <machine/stdarg.h> | | 63 | #include <machine/stdarg.h> |
64 | | | 64 | |
65 | struct vm_map *module_map; | | 65 | struct vm_map *module_map; |
66 | | | 66 | |
67 | struct modlist module_list = TAILQ_HEAD_INITIALIZER(module_list); | | 67 | struct modlist module_list = TAILQ_HEAD_INITIALIZER(module_list); |
68 | struct modlist module_bootlist = TAILQ_HEAD_INITIALIZER(module_bootlist); | | 68 | struct modlist module_bootlist = TAILQ_HEAD_INITIALIZER(module_bootlist); |
69 | static module_t *module_active; | | 69 | static module_t *module_active; |
70 | static char module_base[64]; | | 70 | static char module_base[64]; |
71 | static int module_verbose_on; | | 71 | static int module_verbose_on; |
72 | static int module_autoload_on = 1; | | 72 | static int module_autoload_on = 1; |
73 | u_int module_count; | | 73 | u_int module_count; |
74 | kmutex_t module_lock; | | 74 | kmutex_t module_lock; |
75 | u_int module_autotime = 10; | | 75 | u_int module_autotime = 10; |
76 | u_int module_gen = 1; | | 76 | u_int module_gen = 1; |
77 | static kcondvar_t module_thread_cv; | | 77 | static kcondvar_t module_thread_cv; |
78 | static kmutex_t module_thread_lock; | | 78 | static kmutex_t module_thread_lock; |
79 | static int module_thread_ticks; | | 79 | static int module_thread_ticks; |
80 | | | 80 | |
81 | /* Ensure that the kernel's link set isn't empty. */ | | 81 | /* Ensure that the kernel's link set isn't empty. */ |
82 | static modinfo_t module_dummy; | | 82 | static modinfo_t module_dummy; |
83 | __link_set_add_rodata(modules, module_dummy); | | 83 | __link_set_add_rodata(modules, module_dummy); |
84 | | | 84 | |
85 | static module_t *module_lookup(const char *); | | 85 | static module_t *module_lookup(const char *); |
86 | static int module_do_load(const char *, bool, int, prop_dictionary_t, | | 86 | static int module_do_load(const char *, bool, int, prop_dictionary_t, |
87 | module_t **, modclass_t class, bool); | | 87 | module_t **, modclass_t class, bool); |
88 | static int module_do_unload(const char *); | | 88 | static int module_do_unload(const char *); |
89 | static void module_error(const char *, ...) | | 89 | static void module_error(const char *, ...) |
90 | __attribute__((__format__(__printf__,1,2))); | | 90 | __attribute__((__format__(__printf__,1,2))); |
91 | static void module_print(const char *, ...) | | 91 | static void module_print(const char *, ...) |
92 | __attribute__((__format__(__printf__,1,2))); | | 92 | __attribute__((__format__(__printf__,1,2))); |
93 | static int module_do_builtin(const char *, module_t **); | | 93 | static int module_do_builtin(const char *, module_t **); |
94 | static int module_fetch_info(module_t *); | | 94 | static int module_fetch_info(module_t *); |
95 | static void module_thread(void *); | | 95 | static void module_thread(void *); |
96 | static int module_load_plist_file(const char *, const bool, void **, | | 96 | static int module_load_plist_file(const char *, const bool, void **, |
97 | size_t *); | | 97 | size_t *); |
98 | static bool module_merge_dicts(prop_dictionary_t, const prop_dictionary_t); | | 98 | static bool module_merge_dicts(prop_dictionary_t, const prop_dictionary_t); |
99 | | | 99 | |
100 | /* | | 100 | /* |
101 | * module_error: | | 101 | * module_error: |
102 | * | | 102 | * |
103 | * Utility function: log an error. | | 103 | * Utility function: log an error. |
104 | */ | | 104 | */ |
105 | static void | | 105 | static void |
106 | module_error(const char *fmt, ...) | | 106 | module_error(const char *fmt, ...) |
107 | { | | 107 | { |
108 | va_list ap; | | 108 | va_list ap; |
109 | | | 109 | |
110 | va_start(ap, fmt); | | 110 | va_start(ap, fmt); |
111 | printf("WARNING: module error: "); | | 111 | printf("WARNING: module error: "); |
112 | vprintf(fmt, ap); | | 112 | vprintf(fmt, ap); |
113 | printf("\n"); | | 113 | printf("\n"); |
114 | va_end(ap); | | 114 | va_end(ap); |
115 | } | | 115 | } |
116 | | | 116 | |
117 | /* | | 117 | /* |
118 | * module_print: | | 118 | * module_print: |
119 | * | | 119 | * |
120 | * Utility function: log verbose output. | | 120 | * Utility function: log verbose output. |
121 | */ | | 121 | */ |
122 | static void | | 122 | static void |
123 | module_print(const char *fmt, ...) | | 123 | module_print(const char *fmt, ...) |
124 | { | | 124 | { |
125 | va_list ap; | | 125 | va_list ap; |
126 | | | 126 | |
127 | if (module_verbose_on) { | | 127 | if (module_verbose_on) { |
128 | va_start(ap, fmt); | | 128 | va_start(ap, fmt); |
129 | printf("DEBUG: module: "); | | 129 | printf("DEBUG: module: "); |
130 | vprintf(fmt, ap); | | 130 | vprintf(fmt, ap); |
131 | printf("\n"); | | 131 | printf("\n"); |
132 | va_end(ap); | | 132 | va_end(ap); |
133 | } | | 133 | } |
134 | } | | 134 | } |
135 | | | 135 | |
136 | /* | | 136 | /* |
137 | * module_init: | | 137 | * module_init: |
138 | * | | 138 | * |
139 | * Initialize the module subsystem. | | 139 | * Initialize the module subsystem. |
140 | */ | | 140 | */ |
141 | void | | 141 | void |
142 | module_init(void) | | 142 | module_init(void) |
143 | { | | 143 | { |
144 | extern struct vm_map *module_map; | | 144 | extern struct vm_map *module_map; |
145 | int error; | | 145 | int error; |
146 | | | 146 | |
147 | if (module_map == NULL) { | | 147 | if (module_map == NULL) { |
148 | module_map = kernel_map; | | 148 | module_map = kernel_map; |
149 | } | | 149 | } |
150 | mutex_init(&module_lock, MUTEX_DEFAULT, IPL_NONE); | | 150 | mutex_init(&module_lock, MUTEX_DEFAULT, IPL_NONE); |
151 | cv_init(&module_thread_cv, "modunload"); | | 151 | cv_init(&module_thread_cv, "modunload"); |
152 | mutex_init(&module_thread_lock, MUTEX_DEFAULT, IPL_NONE); | | 152 | mutex_init(&module_thread_lock, MUTEX_DEFAULT, IPL_NONE); |
153 | #ifdef MODULAR /* XXX */ | | 153 | #ifdef MODULAR /* XXX */ |
154 | module_init_md(); | | 154 | module_init_md(); |
155 | #endif | | 155 | #endif |
156 | | | 156 | |
157 | #if __NetBSD_Version__ / 1000000 % 100 == 99 /* -current */ | | 157 | #if __NetBSD_Version__ / 1000000 % 100 == 99 /* -current */ |
158 | snprintf(module_base, sizeof(module_base), "/stand/%s/%s/modules", | | 158 | snprintf(module_base, sizeof(module_base), "/stand/%s/%s/modules", |
159 | machine, osrelease); | | 159 | machine, osrelease); |
160 | #else /* release */ | | 160 | #else /* release */ |
161 | snprintf(module_base, sizeof(module_base), "/stand/%s/%d.%d/modules", | | 161 | snprintf(module_base, sizeof(module_base), "/stand/%s/%d.%d/modules", |
162 | machine, __NetBSD_Version__ / 100000000, | | 162 | machine, __NetBSD_Version__ / 100000000, |
163 | __NetBSD_Version__ / 1000000 % 100); | | 163 | __NetBSD_Version__ / 1000000 % 100); |
164 | #endif | | 164 | #endif |
165 | | | 165 | |
166 | error = kthread_create(PRI_VM, KTHREAD_MPSAFE, NULL, module_thread, | | 166 | error = kthread_create(PRI_VM, KTHREAD_MPSAFE, NULL, module_thread, |
167 | NULL, NULL, "modunload"); | | 167 | NULL, NULL, "modunload"); |
168 | if (error != 0) | | 168 | if (error != 0) |
169 | panic("module_init: %d", error); | | 169 | panic("module_init: %d", error); |
170 | } | | 170 | } |
171 | | | 171 | |
172 | SYSCTL_SETUP(sysctl_module_setup, "sysctl module setup") | | 172 | SYSCTL_SETUP(sysctl_module_setup, "sysctl module setup") |
173 | { | | 173 | { |
174 | const struct sysctlnode *node = NULL; | | 174 | const struct sysctlnode *node = NULL; |
175 | | | 175 | |
176 | sysctl_createv(clog, 0, NULL, NULL, | | 176 | sysctl_createv(clog, 0, NULL, NULL, |
177 | CTLFLAG_PERMANENT, | | 177 | CTLFLAG_PERMANENT, |
178 | CTLTYPE_NODE, "kern", NULL, | | 178 | CTLTYPE_NODE, "kern", NULL, |
179 | NULL, 0, NULL, 0, | | 179 | NULL, 0, NULL, 0, |
180 | CTL_KERN, CTL_EOL); | | 180 | CTL_KERN, CTL_EOL); |
181 | sysctl_createv(clog, 0, NULL, &node, | | 181 | sysctl_createv(clog, 0, NULL, &node, |
182 | CTLFLAG_PERMANENT, | | 182 | CTLFLAG_PERMANENT, |
183 | CTLTYPE_NODE, "module", | | 183 | CTLTYPE_NODE, "module", |
184 | SYSCTL_DESCR("Module options"), | | 184 | SYSCTL_DESCR("Module options"), |
185 | NULL, 0, NULL, 0, | | 185 | NULL, 0, NULL, 0, |
186 | CTL_KERN, CTL_CREATE, CTL_EOL); | | 186 | CTL_KERN, CTL_CREATE, CTL_EOL); |
187 | | | 187 | |
188 | if (node == NULL) | | 188 | if (node == NULL) |
189 | return; | | 189 | return; |
190 | | | 190 | |
191 | sysctl_createv(clog, 0, &node, NULL, | | 191 | sysctl_createv(clog, 0, &node, NULL, |
192 | CTLFLAG_PERMANENT | CTLFLAG_READWRITE, | | 192 | CTLFLAG_PERMANENT | CTLFLAG_READWRITE, |
193 | CTLTYPE_INT, "autoload", | | 193 | CTLTYPE_INT, "autoload", |
194 | SYSCTL_DESCR("Enable automatic load of modules"), | | 194 | SYSCTL_DESCR("Enable automatic load of modules"), |
195 | NULL, 0, &module_autoload_on, 0, | | 195 | NULL, 0, &module_autoload_on, 0, |
196 | CTL_CREATE, CTL_EOL); | | 196 | CTL_CREATE, CTL_EOL); |
197 | sysctl_createv(clog, 0, &node, NULL, | | 197 | sysctl_createv(clog, 0, &node, NULL, |
198 | CTLFLAG_PERMANENT | CTLFLAG_READWRITE, | | 198 | CTLFLAG_PERMANENT | CTLFLAG_READWRITE, |
199 | CTLTYPE_INT, "verbose", | | 199 | CTLTYPE_INT, "verbose", |
200 | SYSCTL_DESCR("Enable verbose output"), | | 200 | SYSCTL_DESCR("Enable verbose output"), |
201 | NULL, 0, &module_verbose_on, 0, | | 201 | NULL, 0, &module_verbose_on, 0, |
202 | CTL_CREATE, CTL_EOL); | | 202 | CTL_CREATE, CTL_EOL); |
203 | } | | 203 | } |
204 | | | 204 | |
205 | /* | | 205 | /* |
206 | * module_init_class: | | 206 | * module_init_class: |
207 | * | | 207 | * |
208 | * Initialize all built-in and pre-loaded modules of the | | 208 | * Initialize all built-in and pre-loaded modules of the |
209 | * specified class. | | 209 | * specified class. |
210 | */ | | 210 | */ |
211 | void | | 211 | void |
212 | module_init_class(modclass_t class) | | 212 | module_init_class(modclass_t class) |
213 | { | | 213 | { |
214 | __link_set_decl(modules, modinfo_t); | | 214 | __link_set_decl(modules, modinfo_t); |
215 | modinfo_t *const *mip, *mi; | | 215 | modinfo_t *const *mip, *mi; |
216 | module_t *mod; | | 216 | module_t *mod; |
217 | | | 217 | |
218 | mutex_enter(&module_lock); | | 218 | mutex_enter(&module_lock); |
219 | /* | | 219 | /* |
220 | * Builtins first. These can't depend on pre-loaded modules. | | 220 | * Builtins first. These can't depend on pre-loaded modules. |
221 | */ | | 221 | */ |
222 | __link_set_foreach(mip, modules) { | | 222 | __link_set_foreach(mip, modules) { |
223 | mi = *mip; | | 223 | mi = *mip; |
224 | if (mi == &module_dummy) { | | 224 | if (mi == &module_dummy) { |
225 | continue; | | 225 | continue; |
226 | } | | 226 | } |
227 | if (class != MODULE_CLASS_ANY && class != mi->mi_class) { | | 227 | if (class != MODULE_CLASS_ANY && class != mi->mi_class) { |
228 | continue; | | 228 | continue; |
229 | } | | 229 | } |
230 | (void)module_do_builtin(mi->mi_name, NULL); | | 230 | (void)module_do_builtin(mi->mi_name, NULL); |
231 | } | | 231 | } |
232 | /* | | 232 | /* |
233 | * Now preloaded modules. These will be pulled off the | | 233 | * Now preloaded modules. These will be pulled off the |
234 | * list as we call module_do_load(); | | 234 | * list as we call module_do_load(); |
235 | */ | | 235 | */ |
236 | do { | | 236 | do { |
237 | TAILQ_FOREACH(mod, &module_bootlist, mod_chain) { | | 237 | TAILQ_FOREACH(mod, &module_bootlist, mod_chain) { |
238 | mi = mod->mod_info; | | 238 | mi = mod->mod_info; |
239 | if (class != MODULE_CLASS_ANY && | | 239 | if (class != MODULE_CLASS_ANY && |
240 | class != mi->mi_class) | | 240 | class != mi->mi_class) |
241 | continue; | | 241 | continue; |
242 | module_do_load(mi->mi_name, false, 0, NULL, NULL, | | 242 | module_do_load(mi->mi_name, false, 0, NULL, NULL, |
243 | class, false); | | 243 | class, false); |
244 | break; | | 244 | break; |
245 | } | | 245 | } |
246 | } while (mod != NULL); | | 246 | } while (mod != NULL); |
247 | mutex_exit(&module_lock); | | 247 | mutex_exit(&module_lock); |
248 | } | | 248 | } |
249 | | | 249 | |
250 | /* | | 250 | /* |
251 | * module_compatible: | | 251 | * module_compatible: |
252 | * | | 252 | * |
253 | * Return true if the two supplied kernel versions are said to | | 253 | * Return true if the two supplied kernel versions are said to |
254 | * have the same binary interface for kernel code. The entire | | 254 | * have the same binary interface for kernel code. The entire |
255 | * version is signficant for the development tree (-current), | | 255 | * version is signficant for the development tree (-current), |
256 | * major and minor versions are significant for official | | 256 | * major and minor versions are significant for official |
257 | * releases of the system. | | 257 | * releases of the system. |
258 | */ | | 258 | */ |
259 | bool | | 259 | bool |
260 | module_compatible(int v1, int v2) | | 260 | module_compatible(int v1, int v2) |
261 | { | | 261 | { |
262 | | | 262 | |
263 | #if __NetBSD_Version__ / 1000000 % 100 == 99 /* -current */ | | 263 | #if __NetBSD_Version__ / 1000000 % 100 == 99 /* -current */ |
264 | return v1 == v2; | | 264 | return v1 == v2; |
265 | #else /* release */ | | 265 | #else /* release */ |
266 | return abs(v1 - v2) < 10000; | | 266 | return abs(v1 - v2) < 10000; |
267 | #endif | | 267 | #endif |
268 | } | | 268 | } |
269 | | | 269 | |
270 | /* | | 270 | /* |
271 | * module_load: | | 271 | * module_load: |
272 | * | | 272 | * |
273 | * Load a single module from the file system. | | 273 | * Load a single module from the file system. |
274 | */ | | 274 | */ |
275 | int | | 275 | int |
276 | module_load(const char *filename, int flags, prop_dictionary_t props, | | 276 | module_load(const char *filename, int flags, prop_dictionary_t props, |
277 | modclass_t class) | | 277 | modclass_t class) |
278 | { | | 278 | { |
279 | int error; | | 279 | int error; |
280 | | | 280 | |
281 | /* Authorize. */ | | 281 | /* Authorize. */ |
282 | error = kauth_authorize_system(kauth_cred_get(), KAUTH_SYSTEM_MODULE, | | 282 | error = kauth_authorize_system(kauth_cred_get(), KAUTH_SYSTEM_MODULE, |
283 | 0, (void *)(uintptr_t)MODCTL_LOAD, NULL, NULL); | | 283 | 0, (void *)(uintptr_t)MODCTL_LOAD, NULL, NULL); |
284 | if (error != 0) { | | 284 | if (error != 0) { |
285 | return error; | | 285 | return error; |
286 | } | | 286 | } |
287 | | | 287 | |
288 | mutex_enter(&module_lock); | | 288 | mutex_enter(&module_lock); |
289 | error = module_do_load(filename, false, flags, props, NULL, class, | | 289 | error = module_do_load(filename, false, flags, props, NULL, class, |
290 | false); | | 290 | false); |
291 | mutex_exit(&module_lock); | | 291 | mutex_exit(&module_lock); |
292 | | | 292 | |
293 | return error; | | 293 | return error; |
294 | } | | 294 | } |
295 | | | 295 | |
296 | /* | | 296 | /* |
297 | * module_autoload: | | 297 | * module_autoload: |
298 | * | | 298 | * |
299 | * Load a single module from the file system, system initiated. | | 299 | * Load a single module from the file system, system initiated. |
300 | */ | | 300 | */ |
301 | int | | 301 | int |
302 | module_autoload(const char *filename, modclass_t class) | | 302 | module_autoload(const char *filename, modclass_t class) |
303 | { | | 303 | { |
304 | int error; | | 304 | int error; |
305 | | | 305 | |
306 | KASSERT(mutex_owned(&module_lock)); | | 306 | KASSERT(mutex_owned(&module_lock)); |
307 | | | 307 | |
308 | /* Nothing if the user has disabled it. */ | | 308 | /* Nothing if the user has disabled it. */ |
309 | if (!module_autoload_on) { | | 309 | if (!module_autoload_on) { |
310 | return EPERM; | | 310 | return EPERM; |
311 | } | | 311 | } |
312 | | | 312 | |
313 | /* Disallow path seperators and magic symlinks. */ | | 313 | /* Disallow path seperators and magic symlinks. */ |
314 | if (strchr(filename, '/') != NULL || strchr(filename, '@') != NULL || | | 314 | if (strchr(filename, '/') != NULL || strchr(filename, '@') != NULL || |
315 | strchr(filename, '.') != NULL) { | | 315 | strchr(filename, '.') != NULL) { |
316 | return EPERM; | | 316 | return EPERM; |
317 | } | | 317 | } |
318 | | | 318 | |
319 | /* Authorize. */ | | 319 | /* Authorize. */ |
320 | error = kauth_authorize_system(kauth_cred_get(), KAUTH_SYSTEM_MODULE, | | 320 | error = kauth_authorize_system(kauth_cred_get(), KAUTH_SYSTEM_MODULE, |
321 | 0, (void *)(uintptr_t)MODCTL_LOAD, (void *)(uintptr_t)1, NULL); | | 321 | 0, (void *)(uintptr_t)MODCTL_LOAD, (void *)(uintptr_t)1, NULL); |
322 | if (error != 0) { | | 322 | if (error != 0) { |
323 | return error; | | 323 | return error; |
324 | } | | 324 | } |
325 | | | 325 | |
326 | return module_do_load(filename, false, 0, NULL, NULL, class, true); | | 326 | return module_do_load(filename, false, 0, NULL, NULL, class, true); |
327 | } | | 327 | } |
328 | | | 328 | |
329 | /* | | 329 | /* |
330 | * module_unload: | | 330 | * module_unload: |
331 | * | | 331 | * |
332 | * Find and unload a module by name. | | 332 | * Find and unload a module by name. |
333 | */ | | 333 | */ |
334 | int | | 334 | int |
335 | module_unload(const char *name) | | 335 | module_unload(const char *name) |
336 | { | | 336 | { |
337 | int error; | | 337 | int error; |
338 | | | 338 | |
339 | /* Authorize. */ | | 339 | /* Authorize. */ |
340 | error = kauth_authorize_system(kauth_cred_get(), KAUTH_SYSTEM_MODULE, | | 340 | error = kauth_authorize_system(kauth_cred_get(), KAUTH_SYSTEM_MODULE, |
341 | 0, (void *)(uintptr_t)MODCTL_UNLOAD, NULL, NULL); | | 341 | 0, (void *)(uintptr_t)MODCTL_UNLOAD, NULL, NULL); |
342 | if (error != 0) { | | 342 | if (error != 0) { |
343 | return error; | | 343 | return error; |
344 | } | | 344 | } |
345 | | | 345 | |
346 | mutex_enter(&module_lock); | | 346 | mutex_enter(&module_lock); |
347 | error = module_do_unload(name); | | 347 | error = module_do_unload(name); |
348 | mutex_exit(&module_lock); | | 348 | mutex_exit(&module_lock); |
349 | | | 349 | |
350 | return error; | | 350 | return error; |
351 | } | | 351 | } |
352 | | | 352 | |
353 | /* | | 353 | /* |
354 | * module_lookup: | | 354 | * module_lookup: |
355 | * | | 355 | * |
356 | * Look up a module by name. | | 356 | * Look up a module by name. |
357 | */ | | 357 | */ |
358 | module_t * | | 358 | module_t * |
359 | module_lookup(const char *name) | | 359 | module_lookup(const char *name) |
360 | { | | 360 | { |
361 | module_t *mod; | | 361 | module_t *mod; |
362 | | | 362 | |
363 | KASSERT(mutex_owned(&module_lock)); | | 363 | KASSERT(mutex_owned(&module_lock)); |
364 | | | 364 | |
365 | TAILQ_FOREACH(mod, &module_list, mod_chain) { | | 365 | TAILQ_FOREACH(mod, &module_list, mod_chain) { |
366 | if (strcmp(mod->mod_info->mi_name, name) == 0) { | | 366 | if (strcmp(mod->mod_info->mi_name, name) == 0) { |
367 | break; | | 367 | break; |
368 | } | | 368 | } |
369 | } | | 369 | } |
370 | | | 370 | |
371 | return mod; | | 371 | return mod; |
372 | } | | 372 | } |
373 | | | 373 | |
374 | /* | | 374 | /* |
375 | * module_hold: | | 375 | * module_hold: |
376 | * | | 376 | * |
377 | * Add a single reference to a module. It's the caller's | | 377 | * Add a single reference to a module. It's the caller's |
378 | * responsibility to ensure that the reference is dropped | | 378 | * responsibility to ensure that the reference is dropped |
379 | * later. | | 379 | * later. |
380 | */ | | 380 | */ |
381 | int | | 381 | int |
382 | module_hold(const char *name) | | 382 | module_hold(const char *name) |
383 | { | | 383 | { |
384 | module_t *mod; | | 384 | module_t *mod; |
385 | | | 385 | |
386 | mutex_enter(&module_lock); | | 386 | mutex_enter(&module_lock); |
387 | mod = module_lookup(name); | | 387 | mod = module_lookup(name); |
388 | if (mod == NULL) { | | 388 | if (mod == NULL) { |
389 | mutex_exit(&module_lock); | | 389 | mutex_exit(&module_lock); |
390 | return ENOENT; | | 390 | return ENOENT; |
391 | } | | 391 | } |
392 | mod->mod_refcnt++; | | 392 | mod->mod_refcnt++; |
393 | mutex_exit(&module_lock); | | 393 | mutex_exit(&module_lock); |
394 | | | 394 | |
395 | return 0; | | 395 | return 0; |
396 | } | | 396 | } |
397 | | | 397 | |
398 | /* | | 398 | /* |
399 | * module_rele: | | 399 | * module_rele: |
400 | * | | 400 | * |
401 | * Release a reference acquired with module_hold(). | | 401 | * Release a reference acquired with module_hold(). |
402 | */ | | 402 | */ |
403 | void | | 403 | void |
404 | module_rele(const char *name) | | 404 | module_rele(const char *name) |
405 | { | | 405 | { |
406 | module_t *mod; | | 406 | module_t *mod; |
407 | | | 407 | |
408 | mutex_enter(&module_lock); | | 408 | mutex_enter(&module_lock); |
409 | mod = module_lookup(name); | | 409 | mod = module_lookup(name); |
410 | if (mod == NULL) { | | 410 | if (mod == NULL) { |
411 | mutex_exit(&module_lock); | | 411 | mutex_exit(&module_lock); |
412 | panic("module_rele: gone"); | | 412 | panic("module_rele: gone"); |
413 | } | | 413 | } |
414 | mod->mod_refcnt--; | | 414 | mod->mod_refcnt--; |
415 | mutex_exit(&module_lock); | | 415 | mutex_exit(&module_lock); |
416 | } | | 416 | } |
417 | | | 417 | |
418 | /* | | 418 | /* |
419 | * module_enqueue: | | 419 | * module_enqueue: |
420 | * | | 420 | * |
421 | * Put a module onto the global list and update counters. | | 421 | * Put a module onto the global list and update counters. |
422 | */ | | 422 | */ |
423 | static void | | 423 | static void |
424 | module_enqueue(module_t *mod) | | 424 | module_enqueue(module_t *mod) |
425 | { | | 425 | { |
426 | int i; | | 426 | int i; |
427 | | | 427 | |
428 | /* | | 428 | /* |
429 | * If there are requisite modules, put at the head of the queue. | | 429 | * If there are requisite modules, put at the head of the queue. |
430 | * This is so that autounload can unload requisite modules with | | 430 | * This is so that autounload can unload requisite modules with |
431 | * only one pass through the queue. | | 431 | * only one pass through the queue. |
432 | */ | | 432 | */ |
433 | if (mod->mod_nrequired) { | | 433 | if (mod->mod_nrequired) { |
434 | TAILQ_INSERT_HEAD(&module_list, mod, mod_chain); | | 434 | TAILQ_INSERT_HEAD(&module_list, mod, mod_chain); |
435 | | | 435 | |
436 | /* Add references to the requisite modules. */ | | 436 | /* Add references to the requisite modules. */ |
437 | for (i = 0; i < mod->mod_nrequired; i++) { | | 437 | for (i = 0; i < mod->mod_nrequired; i++) { |
438 | KASSERT(mod->mod_required[i] != NULL); | | 438 | KASSERT(mod->mod_required[i] != NULL); |
439 | mod->mod_required[i]->mod_refcnt++; | | 439 | mod->mod_required[i]->mod_refcnt++; |
440 | } | | 440 | } |
441 | } else { | | 441 | } else { |
442 | TAILQ_INSERT_TAIL(&module_list, mod, mod_chain); | | 442 | TAILQ_INSERT_TAIL(&module_list, mod, mod_chain); |
443 | } | | 443 | } |
444 | module_count++; | | 444 | module_count++; |
445 | module_gen++; | | 445 | module_gen++; |
446 | } | | 446 | } |
447 | | | 447 | |
448 | /* | | 448 | /* |
449 | * module_do_builtin: | | 449 | * module_do_builtin: |
450 | * | | 450 | * |
451 | * Initialize a single module from the list of modules that are | | 451 | * Initialize a single module from the list of modules that are |
452 | * built into the kernel (linked into the kernel image). | | 452 | * built into the kernel (linked into the kernel image). |
453 | */ | | 453 | */ |
454 | static int | | 454 | static int |
455 | module_do_builtin(const char *name, module_t **modp) | | 455 | module_do_builtin(const char *name, module_t **modp) |
456 | { | | 456 | { |
457 | __link_set_decl(modules, modinfo_t); | | 457 | __link_set_decl(modules, modinfo_t); |
458 | modinfo_t *const *mip; | | 458 | modinfo_t *const *mip; |
459 | const char *p, *s; | | 459 | const char *p, *s; |
460 | char buf[MAXMODNAME]; | | 460 | char buf[MAXMODNAME]; |
461 | modinfo_t *mi; | | 461 | modinfo_t *mi; |
462 | module_t *mod, *mod2; | | 462 | module_t *mod, *mod2; |
463 | size_t len; | | 463 | size_t len; |
464 | int error; | | 464 | int error; |
465 | | | 465 | |
466 | KASSERT(mutex_owned(&module_lock)); | | 466 | KASSERT(mutex_owned(&module_lock)); |
467 | | | 467 | |
468 | /* | | 468 | /* |
469 | * Check to see if already loaded. | | 469 | * Check to see if already loaded. |
470 | */ | | 470 | */ |
471 | if ((mod = module_lookup(name)) != NULL) { | | 471 | if ((mod = module_lookup(name)) != NULL) { |
472 | if (modp != NULL) { | | 472 | if (modp != NULL) { |
473 | *modp = mod; | | 473 | *modp = mod; |
474 | } | | 474 | } |
475 | return 0; | | 475 | return 0; |
476 | } | | 476 | } |
477 | | | 477 | |
478 | /* | | 478 | /* |
479 | * Search the list to see if we have a module by this name. | | 479 | * Search the list to see if we have a module by this name. |
480 | */ | | 480 | */ |
481 | error = ENOENT; | | 481 | error = ENOENT; |
482 | __link_set_foreach(mip, modules) { | | 482 | __link_set_foreach(mip, modules) { |
483 | mi = *mip; | | 483 | mi = *mip; |
484 | if (mi == &module_dummy) { | | 484 | if (mi == &module_dummy) { |
485 | continue; | | 485 | continue; |
486 | } | | 486 | } |
487 | if (strcmp(mi->mi_name, name) == 0) { | | 487 | if (strcmp(mi->mi_name, name) == 0) { |
488 | error = 0; | | 488 | error = 0; |
489 | break; | | 489 | break; |
490 | } | | 490 | } |
491 | } | | 491 | } |
492 | if (error != 0) { | | 492 | if (error != 0) { |
493 | module_error("can't find `%s'", name); | | 493 | module_error("can't find `%s'", name); |
494 | return error; | | 494 | return error; |
495 | } | | 495 | } |
496 | | | 496 | |
497 | /* | | 497 | /* |
498 | * Initialize pre-requisites. | | 498 | * Initialize pre-requisites. |
499 | */ | | 499 | */ |
500 | mod = kmem_zalloc(sizeof(*mod), KM_SLEEP); | | 500 | mod = kmem_zalloc(sizeof(*mod), KM_SLEEP); |
501 | if (mod == NULL) { | | 501 | if (mod == NULL) { |
502 | module_error("out of memory for `%s'", name); | | 502 | module_error("out of memory for `%s'", name); |
503 | return ENOMEM; | | 503 | return ENOMEM; |
504 | } | | 504 | } |
505 | if (modp != NULL) { | | 505 | if (modp != NULL) { |
506 | *modp = mod; | | 506 | *modp = mod; |
507 | } | | 507 | } |
508 | if (mi->mi_required != NULL) { | | 508 | if (mi->mi_required != NULL) { |
509 | for (s = mi->mi_required; *s != '\0'; s = p) { | | 509 | for (s = mi->mi_required; *s != '\0'; s = p) { |
510 | if (*s == ',') | | 510 | if (*s == ',') |
511 | s++; | | 511 | s++; |
512 | p = s; | | 512 | p = s; |
513 | while (*p != '\0' && *p != ',') | | 513 | while (*p != '\0' && *p != ',') |
514 | p++; | | 514 | p++; |
515 | len = min(p - s + 1, sizeof(buf)); | | 515 | len = min(p - s + 1, sizeof(buf)); |
516 | strlcpy(buf, s, len); | | 516 | strlcpy(buf, s, len); |
517 | if (buf[0] == '\0') | | 517 | if (buf[0] == '\0') |
518 | break; | | 518 | break; |
519 | if (mod->mod_nrequired == MAXMODDEPS - 1) { | | 519 | if (mod->mod_nrequired == MAXMODDEPS - 1) { |
520 | module_error("too many required modules"); | | 520 | module_error("too many required modules"); |
521 | kmem_free(mod, sizeof(*mod)); | | 521 | kmem_free(mod, sizeof(*mod)); |
522 | return EINVAL; | | 522 | return EINVAL; |
523 | } | | 523 | } |
524 | error = module_do_builtin(buf, &mod2); | | 524 | error = module_do_builtin(buf, &mod2); |
525 | if (error != 0) { | | 525 | if (error != 0) { |
526 | kmem_free(mod, sizeof(*mod)); | | 526 | kmem_free(mod, sizeof(*mod)); |
527 | return error; | | 527 | return error; |
528 | } | | 528 | } |
529 | mod->mod_required[mod->mod_nrequired++] = mod2; | | 529 | mod->mod_required[mod->mod_nrequired++] = mod2; |
530 | } | | 530 | } |
531 | } | | 531 | } |
532 | | | 532 | |
533 | /* | | 533 | /* |
534 | * Try to initialize the module. | | 534 | * Try to initialize the module. |
535 | */ | | 535 | */ |
536 | KASSERT(module_active == NULL); | | 536 | KASSERT(module_active == NULL); |
537 | module_active = mod; | | 537 | module_active = mod; |
538 | error = (*mi->mi_modcmd)(MODULE_CMD_INIT, NULL); | | 538 | error = (*mi->mi_modcmd)(MODULE_CMD_INIT, NULL); |
539 | module_active = NULL; | | 539 | module_active = NULL; |
540 | if (error != 0) { | | 540 | if (error != 0) { |
541 | module_error("builtin module `%s' " | | 541 | module_error("builtin module `%s' " |
542 | "failed to init", mi->mi_name); | | 542 | "failed to init", mi->mi_name); |
543 | kmem_free(mod, sizeof(*mod)); | | 543 | kmem_free(mod, sizeof(*mod)); |
544 | return error; | | 544 | return error; |
545 | } | | 545 | } |
546 | mod->mod_info = mi; | | 546 | mod->mod_info = mi; |
547 | mod->mod_source = MODULE_SOURCE_KERNEL; | | 547 | mod->mod_source = MODULE_SOURCE_KERNEL; |
548 | module_enqueue(mod); | | 548 | module_enqueue(mod); |
549 | return 0; | | 549 | return 0; |
550 | } | | 550 | } |
551 | | | 551 | |
552 | /* | | 552 | /* |
553 | * module_do_load: | | 553 | * module_do_load: |
554 | * | | 554 | * |
555 | * Helper routine: load a module from the file system, or one | | 555 | * Helper routine: load a module from the file system, or one |
556 | * pushed by the boot loader. | | 556 | * pushed by the boot loader. |
557 | */ | | 557 | */ |
558 | static int | | 558 | static int |
559 | module_do_load(const char *name, bool isdep, int flags, | | 559 | module_do_load(const char *name, bool isdep, int flags, |
560 | prop_dictionary_t props, module_t **modp, modclass_t class, | | 560 | prop_dictionary_t props, module_t **modp, modclass_t class, |
561 | bool autoload) | | 561 | bool autoload) |
562 | { | | 562 | { |
563 | static TAILQ_HEAD(,module) pending = TAILQ_HEAD_INITIALIZER(pending); | | 563 | static TAILQ_HEAD(,module) pending = TAILQ_HEAD_INITIALIZER(pending); |
564 | static int depth; | | 564 | static int depth; |
565 | const int maxdepth = 6; | | 565 | const int maxdepth = 6; |
566 | modinfo_t *mi; | | 566 | modinfo_t *mi; |
567 | module_t *mod, *mod2; | | 567 | module_t *mod, *mod2; |
568 | prop_dictionary_t filedict; | | 568 | prop_dictionary_t filedict; |
569 | void *plist; | | 569 | void *plist; |
570 | char buf[MAXMODNAME], *path; | | 570 | char buf[MAXMODNAME], *path; |
571 | const char *s, *p; | | 571 | const char *s, *p; |
572 | int error; | | 572 | int error; |
573 | size_t len, plistlen; | | 573 | size_t len, plistlen; |
574 | bool nochroot; | | 574 | bool nochroot; |
575 | | | 575 | |
576 | KASSERT(mutex_owned(&module_lock)); | | 576 | KASSERT(mutex_owned(&module_lock)); |
577 | | | 577 | |
578 | filedict = NULL; | | 578 | filedict = NULL; |
579 | path = NULL; | | 579 | path = NULL; |
580 | error = 0; | | 580 | error = 0; |
581 | nochroot = false; | | 581 | nochroot = false; |
582 | | | 582 | |
583 | /* | | 583 | /* |
584 | * Avoid recursing too far. | | 584 | * Avoid recursing too far. |
585 | */ | | 585 | */ |
586 | if (++depth > maxdepth) { | | 586 | if (++depth > maxdepth) { |
587 | module_error("too many required modules"); | | 587 | module_error("too many required modules"); |
588 | depth--; | | 588 | depth--; |
589 | return EMLINK; | | 589 | return EMLINK; |
590 | } | | 590 | } |
591 | | | 591 | |
592 | /* | | 592 | /* |
593 | * Load the module and link. Before going to the file system, | | 593 | * Load the module and link. Before going to the file system, |
594 | * scan the list of modules loaded by the boot loader. Just | | 594 | * scan the list of modules loaded by the boot loader. Just |
595 | * before init is started the list of modules loaded at boot | | 595 | * before init is started the list of modules loaded at boot |
596 | * will be purged. Before init is started we can assume that | | 596 | * will be purged. Before init is started we can assume that |
597 | * `name' is a module name and not a path name. | | 597 | * `name' is a module name and not a path name. |
598 | */ | | 598 | */ |
599 | TAILQ_FOREACH(mod, &module_bootlist, mod_chain) { | | 599 | TAILQ_FOREACH(mod, &module_bootlist, mod_chain) { |
600 | if (strcmp(mod->mod_info->mi_name, name) == 0) { | | 600 | if (strcmp(mod->mod_info->mi_name, name) == 0) { |
601 | TAILQ_REMOVE(&module_bootlist, mod, mod_chain); | | 601 | TAILQ_REMOVE(&module_bootlist, mod, mod_chain); |
602 | break; | | 602 | break; |
603 | } | | 603 | } |
604 | } | | 604 | } |
605 | if (mod != NULL) { | | 605 | if (mod != NULL) { |
606 | TAILQ_INSERT_TAIL(&pending, mod, mod_chain); | | 606 | TAILQ_INSERT_TAIL(&pending, mod, mod_chain); |
607 | } else { | | 607 | } else { |
608 | /* | | 608 | /* |
609 | * If a requisite module, check to see if it is | | 609 | * If a requisite module, check to see if it is |
610 | * already present. | | 610 | * already present. |
611 | */ | | 611 | */ |
612 | if (isdep) { | | 612 | if (isdep) { |
613 | TAILQ_FOREACH(mod, &module_list, mod_chain) { | | 613 | TAILQ_FOREACH(mod, &module_list, mod_chain) { |
614 | if (strcmp(mod->mod_info->mi_name, name) == 0) { | | 614 | if (strcmp(mod->mod_info->mi_name, name) == 0) { |
615 | break; | | 615 | break; |
616 | } | | 616 | } |
617 | } | | 617 | } |
618 | if (mod != NULL) { | | 618 | if (mod != NULL) { |
619 | if (modp != NULL) { | | 619 | if (modp != NULL) { |
620 | *modp = mod; | | 620 | *modp = mod; |
621 | } | | 621 | } |
622 | depth--; | | 622 | depth--; |
623 | return 0; | | 623 | return 0; |
624 | } | | 624 | } |
625 | } | | 625 | } |
626 | mod = kmem_zalloc(sizeof(*mod), KM_SLEEP); | | 626 | mod = kmem_zalloc(sizeof(*mod), KM_SLEEP); |
627 | if (mod == NULL) { | | 627 | if (mod == NULL) { |
628 | module_error("out of memory for `%s'", name); | | 628 | module_error("out of memory for `%s'", name); |
629 | depth--; | | 629 | depth--; |
630 | return ENOMEM; | | 630 | return ENOMEM; |
631 | } | | 631 | } |
632 | path = PNBUF_GET(); | | 632 | path = PNBUF_GET(); |
633 | if (!autoload) { | | 633 | if (!autoload) { |
634 | nochroot = false; | | 634 | nochroot = false; |
635 | snprintf(path, MAXPATHLEN, "%s", name); | | 635 | snprintf(path, MAXPATHLEN, "%s", name); |
636 | error = kobj_load_file(&mod->mod_kobj, path, nochroot); | | 636 | error = kobj_load_file(&mod->mod_kobj, path, nochroot); |
637 | } | | 637 | } |
638 | if (autoload || (error == ENOENT)) { | | 638 | if (autoload || (error == ENOENT)) { |
639 | nochroot = true; | | 639 | nochroot = true; |
640 | snprintf(path, MAXPATHLEN, "%s/%s/%s.kmod", | | 640 | snprintf(path, MAXPATHLEN, "%s/%s/%s.kmod", |
641 | module_base, name, name); | | 641 | module_base, name, name); |
642 | error = kobj_load_file(&mod->mod_kobj, path, nochroot); | | 642 | error = kobj_load_file(&mod->mod_kobj, path, nochroot); |
643 | } | | 643 | } |
644 | if (error != 0) { | | 644 | if (error != 0) { |
645 | kmem_free(mod, sizeof(*mod)); | | 645 | kmem_free(mod, sizeof(*mod)); |
646 | depth--; | | 646 | depth--; |
647 | PNBUF_PUT(path); | | 647 | PNBUF_PUT(path); |
648 | if (autoload) { | | 648 | if (autoload) { |
649 | module_print("Cannot load kernel object `%s'" | | 649 | module_print("Cannot load kernel object `%s'" |
650 | " error=%d", name, error); | | 650 | " error=%d", name, error); |
651 | } else { | | 651 | } else { |
652 | module_error("Cannot load kernel object `%s'" | | 652 | module_error("Cannot load kernel object `%s'" |
653 | " error=%d", name, error); | | 653 | " error=%d", name, error); |
654 | } | | 654 | } |
655 | return error; | | 655 | return error; |
656 | } | | 656 | } |
657 | TAILQ_INSERT_TAIL(&pending, mod, mod_chain); | | 657 | TAILQ_INSERT_TAIL(&pending, mod, mod_chain); |
658 | mod->mod_source = MODULE_SOURCE_FILESYS; | | 658 | mod->mod_source = MODULE_SOURCE_FILESYS; |
659 | error = module_fetch_info(mod); | | 659 | error = module_fetch_info(mod); |
660 | if (error != 0) { | | 660 | if (error != 0) { |
661 | module_error("cannot fetch module info for `%s'", | | 661 | module_error("cannot fetch module info for `%s'", |
662 | name); | | 662 | name); |
663 | goto fail; | | 663 | goto fail; |
664 | } | | 664 | } |
665 | } | | 665 | } |
666 | | | 666 | |
667 | /* | | 667 | /* |
668 | * Check compatibility. | | 668 | * Check compatibility. |
669 | */ | | 669 | */ |
670 | mi = mod->mod_info; | | 670 | mi = mod->mod_info; |
671 | if (strlen(mi->mi_name) >= MAXMODNAME) { | | 671 | if (strlen(mi->mi_name) >= MAXMODNAME) { |
672 | error = EINVAL; | | 672 | error = EINVAL; |
673 | module_error("module name `%s' too long", mi->mi_name); | | 673 | module_error("module name `%s' too long", mi->mi_name); |
674 | goto fail; | | 674 | goto fail; |
675 | } | | 675 | } |
676 | if (!module_compatible(mi->mi_version, __NetBSD_Version__)) { | | 676 | if (!module_compatible(mi->mi_version, __NetBSD_Version__)) { |
677 | module_error("module built for `%d', system `%d'", | | 677 | module_error("module built for `%d', system `%d'", |
678 | mi->mi_version, __NetBSD_Version__); | | 678 | mi->mi_version, __NetBSD_Version__); |
679 | if ((flags & MODCTL_LOAD_FORCE) != 0) { | | 679 | if ((flags & MODCTL_LOAD_FORCE) != 0) { |
680 | module_error("forced load, system may be unstable"); | | 680 | module_error("forced load, system may be unstable"); |
681 | } else { | | 681 | } else { |
682 | error = EPROGMISMATCH; | | 682 | error = EPROGMISMATCH; |
683 | goto fail; | | 683 | goto fail; |
684 | } | | 684 | } |
685 | } | | 685 | } |
686 | | | 686 | |
687 | /* | | 687 | /* |
688 | * If a specific kind of module was requested, ensure that we have | | 688 | * If a specific kind of module was requested, ensure that we have |
689 | * a match. | | 689 | * a match. |
690 | */ | | 690 | */ |
691 | if (class != MODULE_CLASS_ANY && class != mi->mi_class) { | | 691 | if (class != MODULE_CLASS_ANY && class != mi->mi_class) { |
692 | module_print("incompatible module class for `%s' (%d != %d)", | | 692 | module_print("incompatible module class for `%s' (%d != %d)", |
693 | name, class, mi->mi_class); | | 693 | name, class, mi->mi_class); |
694 | error = ENOENT; | | 694 | error = ENOENT; |
695 | goto fail; | | 695 | goto fail; |
696 | } | | 696 | } |
697 | | | 697 | |
698 | /* | | 698 | /* |
699 | * If loading a dependency, `name' is a plain module name. | | 699 | * If loading a dependency, `name' is a plain module name. |
700 | * The name must match. | | 700 | * The name must match. |
701 | */ | | 701 | */ |
702 | if (isdep && strcmp(mi->mi_name, name) != 0) { | | 702 | if (isdep && strcmp(mi->mi_name, name) != 0) { |
703 | module_error("dependency name mismatch (`%s' != `%s')", | | 703 | module_error("dependency name mismatch (`%s' != `%s')", |
704 | name, mi->mi_name); | | 704 | name, mi->mi_name); |
705 | error = ENOENT; | | 705 | error = ENOENT; |
706 | goto fail; | | 706 | goto fail; |
707 | } | | 707 | } |
708 | | | 708 | |
709 | /* | | 709 | /* |
710 | * Check to see if the module is already loaded. If so, we may | | 710 | * Check to see if the module is already loaded. If so, we may |
711 | * have been recursively called to handle a dependency, so be sure | | 711 | * have been recursively called to handle a dependency, so be sure |
712 | * to set modp. | | 712 | * to set modp. |
713 | */ | | 713 | */ |
714 | if ((mod2 = module_lookup(mi->mi_name)) != NULL) { | | 714 | if ((mod2 = module_lookup(mi->mi_name)) != NULL) { |
715 | if (modp != NULL) | | 715 | if (modp != NULL) |
716 | *modp = mod2; | | 716 | *modp = mod2; |
717 | module_print("module `%s' already loaded", mi->mi_name); | | 717 | module_print("module `%s' already loaded", mi->mi_name); |
718 | error = EEXIST; | | 718 | error = EEXIST; |
719 | goto fail; | | 719 | goto fail; |
720 | } | | 720 | } |
721 | | | 721 | |
722 | /* | | 722 | /* |
723 | * Block circular dependencies. | | 723 | * Block circular dependencies. |
724 | */ | | 724 | */ |
725 | TAILQ_FOREACH(mod2, &pending, mod_chain) { | | 725 | TAILQ_FOREACH(mod2, &pending, mod_chain) { |
726 | if (mod == mod2) { | | 726 | if (mod == mod2) { |
727 | continue; | | 727 | continue; |
728 | } | | 728 | } |
729 | if (strcmp(mod2->mod_info->mi_name, mi->mi_name) == 0) { | | 729 | if (strcmp(mod2->mod_info->mi_name, mi->mi_name) == 0) { |
730 | error = EDEADLK; | | 730 | error = EDEADLK; |
731 | module_error("circular dependency detected for `%s'", | | 731 | module_error("circular dependency detected for `%s'", |
732 | mi->mi_name); | | 732 | mi->mi_name); |
733 | goto fail; | | 733 | goto fail; |
734 | } | | 734 | } |
735 | } | | 735 | } |
736 | | | 736 | |
737 | /* | | 737 | /* |
738 | * Now try to load any requisite modules. | | 738 | * Now try to load any requisite modules. |
739 | */ | | 739 | */ |
740 | if (mi->mi_required != NULL) { | | 740 | if (mi->mi_required != NULL) { |
741 | for (s = mi->mi_required; *s != '\0'; s = p) { | | 741 | for (s = mi->mi_required; *s != '\0'; s = p) { |
742 | if (*s == ',') | | 742 | if (*s == ',') |
743 | s++; | | 743 | s++; |
744 | p = s; | | 744 | p = s; |
745 | while (*p != '\0' && *p != ',') | | 745 | while (*p != '\0' && *p != ',') |
746 | p++; | | 746 | p++; |
747 | len = p - s + 1; | | 747 | len = p - s + 1; |
748 | if (len >= MAXMODNAME) { | | 748 | if (len >= MAXMODNAME) { |
749 | error = EINVAL; | | 749 | error = EINVAL; |
750 | module_error("required module name `%s'" | | 750 | module_error("required module name `%s'" |
751 | " too long", mi->mi_required); | | 751 | " too long", mi->mi_required); |
752 | goto fail; | | 752 | goto fail; |
753 | } | | 753 | } |
754 | strlcpy(buf, s, len); | | 754 | strlcpy(buf, s, len); |
755 | if (buf[0] == '\0') | | 755 | if (buf[0] == '\0') |
756 | break; | | 756 | break; |
757 | if (mod->mod_nrequired == MAXMODDEPS - 1) { | | 757 | if (mod->mod_nrequired == MAXMODDEPS - 1) { |
758 | error = EINVAL; | | 758 | error = EINVAL; |
759 | module_error("too many required modules (%d)", | | 759 | module_error("too many required modules (%d)", |
760 | mod->mod_nrequired); | | 760 | mod->mod_nrequired); |
761 | goto fail; | | 761 | goto fail; |
762 | } | | 762 | } |
763 | if (strcmp(buf, mi->mi_name) == 0) { | | 763 | if (strcmp(buf, mi->mi_name) == 0) { |
764 | error = EDEADLK; | | 764 | error = EDEADLK; |
765 | module_error("self-dependency detected for " | | 765 | module_error("self-dependency detected for " |
766 | "`%s'", mi->mi_name); | | 766 | "`%s'", mi->mi_name); |
767 | goto fail; | | 767 | goto fail; |
768 | } | | 768 | } |
769 | error = module_do_load(buf, true, flags, NULL, | | 769 | error = module_do_load(buf, true, flags, NULL, |
770 | &mod->mod_required[mod->mod_nrequired++], | | 770 | &mod->mod_required[mod->mod_nrequired++], |
771 | MODULE_CLASS_ANY, true); | | 771 | MODULE_CLASS_ANY, true); |
772 | if (error != 0) | | 772 | if (error != 0) |
773 | goto fail; | | 773 | goto fail; |
774 | } | | 774 | } |
775 | } | | 775 | } |
776 | | | 776 | |
777 | /* | | 777 | /* |
778 | * We loaded all needed modules successfully: perform global | | 778 | * We loaded all needed modules successfully: perform global |
779 | * relocations and initialize. | | 779 | * relocations and initialize. |
780 | */ | | 780 | */ |
781 | error = kobj_affix(mod->mod_kobj, mi->mi_name); | | 781 | error = kobj_affix(mod->mod_kobj, mi->mi_name); |
782 | if (error != 0) { | | 782 | if (error != 0) { |
783 | /* Cannot touch 'mi' as the module is now gone. */ | | 783 | /* Cannot touch 'mi' as the module is now gone. */ |
784 | module_error("unable to affix module `%s'", name); | | 784 | module_error("unable to affix module `%s'", name); |
785 | goto fail2; | | 785 | goto fail2; |
786 | } | | 786 | } |
787 | | | 787 | |
788 | /* | | 788 | /* |
789 | * Load and process <module>.prop if it exists. | | 789 | * Load and process <module>.prop if it exists. |
790 | */ | | 790 | */ |
791 | if (((flags & MODCTL_NO_PROP) == 0) && | | 791 | if (((flags & MODCTL_NO_PROP) == 0) && |
792 | (mod->mod_source == MODULE_SOURCE_FILESYS)) { | | 792 | (mod->mod_source == MODULE_SOURCE_FILESYS)) { |
793 | error = module_load_plist_file(path, nochroot, &plist, | | 793 | error = module_load_plist_file(path, nochroot, &plist, |
794 | &plistlen); | | 794 | &plistlen); |
795 | if (error != 0) { | | 795 | if (error != 0) { |
796 | module_print("plist load returned error %d for `%s'", | | 796 | module_print("plist load returned error %d for `%s'", |
797 | error, path); | | 797 | error, path); |
798 | } else { | | 798 | } else { |
799 | filedict = prop_dictionary_internalize(plist); | | 799 | filedict = prop_dictionary_internalize(plist); |
800 | if (filedict == NULL) { | | 800 | if (filedict == NULL) { |
801 | error = EINVAL; | | 801 | error = EINVAL; |
802 | } else if (!module_merge_dicts(filedict, props)) { | | 802 | } else if (!module_merge_dicts(filedict, props)) { |
803 | error = EINVAL; | | 803 | error = EINVAL; |
804 | prop_object_release(filedict); | | 804 | prop_object_release(filedict); |
805 | filedict = NULL; | | 805 | filedict = NULL; |
806 | } | | 806 | } |
807 | } | | 807 | } |
808 | if (plist != NULL) { | | 808 | if (plist != NULL) { |
809 | kmem_free(plist, PAGE_SIZE); | | 809 | kmem_free(plist, PAGE_SIZE); |
810 | } | | 810 | } |
811 | if ((error != 0) && (error != ENOENT)) { | | 811 | if ((error != 0) && (error != ENOENT)) { |
812 | goto fail; | | 812 | goto fail; |
813 | } | | 813 | } |
814 | } | | 814 | } |
815 | | | 815 | |
816 | KASSERT(module_active == NULL); | | 816 | KASSERT(module_active == NULL); |
817 | module_active = mod; | | 817 | module_active = mod; |
818 | error = (*mi->mi_modcmd)(MODULE_CMD_INIT, (filedict != NULL) ? | | 818 | error = (*mi->mi_modcmd)(MODULE_CMD_INIT, (filedict != NULL) ? |
819 | filedict : props); /* props will have been merged with filedict */ | | 819 | filedict : props); /* props will have been merged with filedict */ |
820 | module_active = NULL; | | 820 | module_active = NULL; |
821 | if (filedict != NULL) { | | 821 | if (filedict != NULL) { |
822 | prop_object_release(filedict); | | 822 | prop_object_release(filedict); |
823 | } | | 823 | } |
824 | if (error != 0) { | | 824 | if (error != 0) { |
825 | module_error("modcmd function returned error %d for `%s'", | | 825 | module_error("modcmd function returned error %d for `%s'", |
826 | error, mi->mi_name); | | 826 | error, mi->mi_name); |
827 | goto fail; | | 827 | goto fail; |
828 | } | | 828 | } |
829 | | | 829 | |
830 | /* | | 830 | /* |
831 | * Good, the module loaded successfully. Put it onto the | | 831 | * Good, the module loaded successfully. Put it onto the |
832 | * list and add references to its requisite modules. | | 832 | * list and add references to its requisite modules. |
833 | */ | | 833 | */ |
834 | TAILQ_REMOVE(&pending, mod, mod_chain); | | 834 | TAILQ_REMOVE(&pending, mod, mod_chain); |
835 | module_enqueue(mod); | | 835 | module_enqueue(mod); |
836 | if (modp != NULL) { | | 836 | if (modp != NULL) { |
837 | *modp = mod; | | 837 | *modp = mod; |
838 | } | | 838 | } |
839 | if (autoload) { | | 839 | if (autoload) { |
840 | /* | | 840 | /* |
841 | * Arrange to try unloading the module after | | 841 | * Arrange to try unloading the module after |
842 | * a short delay. | | 842 | * a short delay. |
843 | */ | | 843 | */ |
844 | mod->mod_autotime = time_second + module_autotime; | | 844 | mod->mod_autotime = time_second + module_autotime; |
845 | module_thread_kick(); | | 845 | module_thread_kick(); |
846 | } | | 846 | } |
847 | depth--; | | 847 | depth--; |
848 | if (path != NULL) | | 848 | if (path != NULL) |
849 | PNBUF_PUT(path); | | 849 | PNBUF_PUT(path); |
850 | return 0; | | 850 | return 0; |
851 | | | 851 | |
852 | fail: | | 852 | fail: |
853 | kobj_unload(mod->mod_kobj); | | 853 | kobj_unload(mod->mod_kobj); |
854 | fail2: | | 854 | fail2: |
855 | TAILQ_REMOVE(&pending, mod, mod_chain); | | 855 | TAILQ_REMOVE(&pending, mod, mod_chain); |
856 | kmem_free(mod, sizeof(*mod)); | | 856 | kmem_free(mod, sizeof(*mod)); |
857 | depth--; | | 857 | depth--; |
858 | if (path != NULL) | | 858 | if (path != NULL) |
859 | PNBUF_PUT(path); | | 859 | PNBUF_PUT(path); |
860 | return error; | | 860 | return error; |
861 | } | | 861 | } |
862 | | | 862 | |
863 | /* | | 863 | /* |
864 | * module_do_unload: | | 864 | * module_do_unload: |
865 | * | | 865 | * |
866 | * Helper routine: do the dirty work of unloading a module. | | 866 | * Helper routine: do the dirty work of unloading a module. |
867 | */ | | 867 | */ |
868 | static int | | 868 | static int |
869 | module_do_unload(const char *name) | | 869 | module_do_unload(const char *name) |
870 | { | | 870 | { |
871 | module_t *mod; | | 871 | module_t *mod; |
872 | int error; | | 872 | int error; |
873 | u_int i; | | 873 | u_int i; |
874 | | | 874 | |
875 | KASSERT(mutex_owned(&module_lock)); | | 875 | KASSERT(mutex_owned(&module_lock)); |
876 | | | 876 | |
877 | mod = module_lookup(name); | | 877 | mod = module_lookup(name); |
878 | if (mod == NULL) { | | 878 | if (mod == NULL) { |
879 | module_error("module `%s' not found", name); | | 879 | module_error("module `%s' not found", name); |
880 | return ENOENT; | | 880 | return ENOENT; |
881 | } | | 881 | } |
882 | if (mod->mod_refcnt != 0 || mod->mod_source == MODULE_SOURCE_KERNEL) { | | 882 | if (mod->mod_refcnt != 0 || mod->mod_source == MODULE_SOURCE_KERNEL) { |
883 | module_print("module `%s' busy", name); | | 883 | module_print("module `%s' busy", name); |
884 | return EBUSY; | | 884 | return EBUSY; |
885 | } | | 885 | } |
886 | KASSERT(module_active == NULL); | | 886 | KASSERT(module_active == NULL); |
887 | module_active = mod; | | 887 | module_active = mod; |
888 | error = (*mod->mod_info->mi_modcmd)(MODULE_CMD_FINI, NULL); | | 888 | error = (*mod->mod_info->mi_modcmd)(MODULE_CMD_FINI, NULL); |
889 | module_active = NULL; | | 889 | module_active = NULL; |
890 | if (error != 0) { | | 890 | if (error != 0) { |
891 | module_print("cannot unload module `%s' error=%d", name, | | 891 | module_print("cannot unload module `%s' error=%d", name, |
892 | error); | | 892 | error); |
893 | return error; | | 893 | return error; |
894 | } | | 894 | } |
895 | module_count--; | | 895 | module_count--; |
896 | TAILQ_REMOVE(&module_list, mod, mod_chain); | | 896 | TAILQ_REMOVE(&module_list, mod, mod_chain); |
897 | for (i = 0; i < mod->mod_nrequired; i++) { | | 897 | for (i = 0; i < mod->mod_nrequired; i++) { |
898 | mod->mod_required[i]->mod_refcnt--; | | 898 | mod->mod_required[i]->mod_refcnt--; |
899 | } | | 899 | } |
900 | if (mod->mod_kobj != NULL) { | | 900 | if (mod->mod_kobj != NULL) { |
901 | kobj_unload(mod->mod_kobj); | | 901 | kobj_unload(mod->mod_kobj); |
902 | } | | 902 | } |
903 | kmem_free(mod, sizeof(*mod)); | | 903 | kmem_free(mod, sizeof(*mod)); |
904 | module_gen++; | | 904 | module_gen++; |
905 | | | 905 | |
906 | return 0; | | 906 | return 0; |
907 | } | | 907 | } |
908 | | | 908 | |
909 | /* | | 909 | /* |
910 | * module_prime: | | 910 | * module_prime: |
911 | * | | 911 | * |
912 | * Push a module loaded by the bootloader onto our internal | | 912 | * Push a module loaded by the bootloader onto our internal |
913 | * list. | | 913 | * list. |
914 | */ | | 914 | */ |
915 | int | | 915 | int |
916 | module_prime(void *base, size_t size) | | 916 | module_prime(void *base, size_t size) |
917 | { | | 917 | { |
918 | module_t *mod; | | 918 | module_t *mod; |
919 | int error; | | 919 | int error; |
920 | | | 920 | |
921 | mod = kmem_zalloc(sizeof(*mod), KM_SLEEP); | | 921 | mod = kmem_zalloc(sizeof(*mod), KM_SLEEP); |
922 | if (mod == NULL) { | | 922 | if (mod == NULL) { |
923 | return ENOMEM; | | 923 | return ENOMEM; |
924 | } | | 924 | } |
925 | mod->mod_source = MODULE_SOURCE_BOOT; | | 925 | mod->mod_source = MODULE_SOURCE_BOOT; |
926 | | | 926 | |
927 | error = kobj_load_mem(&mod->mod_kobj, base, size); | | 927 | error = kobj_load_mem(&mod->mod_kobj, base, size); |
928 | if (error != 0) { | | 928 | if (error != 0) { |
929 | kmem_free(mod, sizeof(*mod)); | | 929 | kmem_free(mod, sizeof(*mod)); |
930 | module_error("unable to load object pushed by boot loader"); | | 930 | module_error("unable to load object pushed by boot loader"); |
931 | return error; | | 931 | return error; |
932 | } | | 932 | } |
933 | error = module_fetch_info(mod); | | 933 | error = module_fetch_info(mod); |
934 | if (error != 0) { | | 934 | if (error != 0) { |
935 | kobj_unload(mod->mod_kobj); | | 935 | kobj_unload(mod->mod_kobj); |
936 | kmem_free(mod, sizeof(*mod)); | | 936 | kmem_free(mod, sizeof(*mod)); |
937 | module_error("unable to load object pushed by boot loader"); | | 937 | module_error("unable to load object pushed by boot loader"); |
938 | return error; | | 938 | return error; |
939 | } | | 939 | } |
940 | | | 940 | |
941 | TAILQ_INSERT_TAIL(&module_bootlist, mod, mod_chain); | | 941 | TAILQ_INSERT_TAIL(&module_bootlist, mod, mod_chain); |
942 | | | 942 | |
943 | return 0; | | 943 | return 0; |
944 | } | | 944 | } |
945 | | | 945 | |
946 | /* | | 946 | /* |
947 | * module_fetch_into: | | 947 | * module_fetch_into: |
948 | * | | 948 | * |
949 | * Fetch modinfo record from a loaded module. | | 949 | * Fetch modinfo record from a loaded module. |
950 | */ | | 950 | */ |
951 | static int | | 951 | static int |
952 | module_fetch_info(module_t *mod) | | 952 | module_fetch_info(module_t *mod) |
953 | { | | 953 | { |
954 | int error; | | 954 | int error; |
955 | void *addr; | | 955 | void *addr; |
956 | size_t size; | | 956 | size_t size; |
957 | | | 957 | |
958 | /* | | 958 | /* |
959 | * Find module info record and check compatibility. | | 959 | * Find module info record and check compatibility. |
960 | */ | | 960 | */ |
961 | error = kobj_find_section(mod->mod_kobj, "link_set_modules", | | 961 | error = kobj_find_section(mod->mod_kobj, "link_set_modules", |
962 | &addr, &size); | | 962 | &addr, &size); |
963 | if (error != 0) { | | 963 | if (error != 0) { |
964 | module_error("`link_set_modules' section not present"); | | 964 | module_error("`link_set_modules' section not present"); |
965 | return error; | | 965 | return error; |
966 | } | | 966 | } |
967 | if (size != sizeof(modinfo_t **)) { | | 967 | if (size != sizeof(modinfo_t **)) { |
968 | module_error("`link_set_modules' section wrong size"); | | 968 | module_error("`link_set_modules' section wrong size"); |
969 | return error; | | 969 | return error; |
970 | } | | 970 | } |
971 | mod->mod_info = *(modinfo_t **)addr; | | 971 | mod->mod_info = *(modinfo_t **)addr; |
972 | | | 972 | |
973 | return 0; | | 973 | return 0; |
974 | } | | 974 | } |
975 | | | 975 | |
976 | /* | | 976 | /* |
977 | * module_find_section: | | 977 | * module_find_section: |
978 | * | | 978 | * |
979 | * Allows a module that is being initialized to look up a section | | 979 | * Allows a module that is being initialized to look up a section |
980 | * within its ELF object. | | 980 | * within its ELF object. |
981 | */ | | 981 | */ |
982 | int | | 982 | int |
983 | module_find_section(const char *name, void **addr, size_t *size) | | 983 | module_find_section(const char *name, void **addr, size_t *size) |
984 | { | | 984 | { |
985 | | | 985 | |
986 | KASSERT(mutex_owned(&module_lock)); | | 986 | KASSERT(mutex_owned(&module_lock)); |
987 | KASSERT(module_active != NULL); | | 987 | KASSERT(module_active != NULL); |
988 | | | 988 | |
989 | return kobj_find_section(module_active->mod_kobj, name, addr, size); | | 989 | return kobj_find_section(module_active->mod_kobj, name, addr, size); |
990 | } | | 990 | } |
991 | | | 991 | |
992 | /* | | 992 | /* |
993 | * module_thread: | | 993 | * module_thread: |
994 | * | | 994 | * |
995 | * Automatically unload modules. We try once to unload autoloaded | | 995 | * Automatically unload modules. We try once to unload autoloaded |
996 | * modules after module_autotime seconds. If the system is under | | 996 | * modules after module_autotime seconds. If the system is under |
997 | * severe memory pressure, we'll try unloading all modules. | | 997 | * severe memory pressure, we'll try unloading all modules. |
998 | */ | | 998 | */ |
999 | static void | | 999 | static void |
1000 | module_thread(void *cookie) | | 1000 | module_thread(void *cookie) |
1001 | { | | 1001 | { |
1002 | module_t *mod, *next; | | 1002 | module_t *mod, *next; |
1003 | modinfo_t *mi; | | 1003 | modinfo_t *mi; |
1004 | int error; | | 1004 | int error; |
1005 | | | 1005 | |
1006 | for (;;) { | | 1006 | for (;;) { |
1007 | mutex_enter(&module_lock); | | 1007 | mutex_enter(&module_lock); |
1008 | for (mod = TAILQ_FIRST(&module_list); mod != NULL; mod = next) { | | 1008 | for (mod = TAILQ_FIRST(&module_list); mod != NULL; mod = next) { |
1009 | next = TAILQ_NEXT(mod, mod_chain); | | 1009 | next = TAILQ_NEXT(mod, mod_chain); |
1010 | if (uvmexp.free < uvmexp.freemin) { | | 1010 | if (uvmexp.free < uvmexp.freemin) { |
1011 | module_thread_ticks = hz; | | 1011 | module_thread_ticks = hz; |
1012 | } else if (mod->mod_autotime == 0) { | | 1012 | } else if (mod->mod_autotime == 0) { |
1013 | continue; | | 1013 | continue; |
1014 | } else if (time_second < mod->mod_autotime) { | | 1014 | } else if (time_second < mod->mod_autotime) { |
1015 | module_thread_ticks = hz; | | 1015 | module_thread_ticks = hz; |
1016 | continue; | | 1016 | continue; |
1017 | } else { | | 1017 | } else { |
1018 | mod->mod_autotime = 0; | | 1018 | mod->mod_autotime = 0; |
1019 | } | | 1019 | } |
1020 | /* | | 1020 | /* |
1021 | * If this module wants to avoid autounload then | | 1021 | * If this module wants to avoid autounload then |
1022 | * skip it. Some modules can ping-pong in and out | | 1022 | * skip it. Some modules can ping-pong in and out |
1023 | * because their use is transient but often. | | 1023 | * because their use is transient but often. |
1024 | * Example: exec_script. | | 1024 | * Example: exec_script. |
1025 | */ | | 1025 | */ |
1026 | mi = mod->mod_info; | | 1026 | mi = mod->mod_info; |
1027 | error = (*mi->mi_modcmd)(MODULE_CMD_AUTOUNLOAD, NULL); | | 1027 | error = (*mi->mi_modcmd)(MODULE_CMD_AUTOUNLOAD, NULL); |
1028 | if (error == 0 || error == ENOTTY) { | | 1028 | if (error == 0 || error == ENOTTY) { |
1029 | (void)module_do_unload(mi->mi_name); | | 1029 | (void)module_do_unload(mi->mi_name); |
1030 | } | | 1030 | } |
1031 | } | | 1031 | } |
1032 | mutex_exit(&module_lock); | | 1032 | mutex_exit(&module_lock); |
1033 | | | 1033 | |
1034 | mutex_enter(&module_thread_lock); | | 1034 | mutex_enter(&module_thread_lock); |
1035 | (void)cv_timedwait(&module_thread_cv, &module_thread_lock, | | 1035 | (void)cv_timedwait(&module_thread_cv, &module_thread_lock, |
1036 | module_thread_ticks); | | 1036 | module_thread_ticks); |
1037 | module_thread_ticks = 0; | | 1037 | module_thread_ticks = 0; |
1038 | mutex_exit(&module_thread_lock); | | 1038 | mutex_exit(&module_thread_lock); |
1039 | } | | 1039 | } |
1040 | } | | 1040 | } |
1041 | | | 1041 | |
1042 | /* | | 1042 | /* |
1043 | * module_thread: | | 1043 | * module_thread: |
1044 | * | | 1044 | * |
1045 | * Kick the module thread into action, perhaps because the | | 1045 | * Kick the module thread into action, perhaps because the |
1046 | * system is low on memory. | | 1046 | * system is low on memory. |
1047 | */ | | 1047 | */ |
1048 | void | | 1048 | void |
1049 | module_thread_kick(void) | | 1049 | module_thread_kick(void) |
1050 | { | | 1050 | { |
1051 | | | 1051 | |
1052 | mutex_enter(&module_thread_lock); | | 1052 | mutex_enter(&module_thread_lock); |
1053 | module_thread_ticks = hz; | | 1053 | module_thread_ticks = hz; |
1054 | cv_broadcast(&module_thread_cv); | | 1054 | cv_broadcast(&module_thread_cv); |
1055 | mutex_exit(&module_thread_lock); | | 1055 | mutex_exit(&module_thread_lock); |
1056 | } | | 1056 | } |
1057 | | | 1057 | |
1058 | #ifdef DDB | | 1058 | #ifdef DDB |
1059 | /* | | 1059 | /* |
1060 | * module_whatis: | | 1060 | * module_whatis: |
1061 | * | | 1061 | * |
1062 | * Helper routine for DDB. | | 1062 | * Helper routine for DDB. |
1063 | */ | | 1063 | */ |
1064 | void | | 1064 | void |
1065 | module_whatis(uintptr_t addr, void (*pr)(const char *, ...)) | | 1065 | module_whatis(uintptr_t addr, void (*pr)(const char *, ...)) |
1066 | { | | 1066 | { |
1067 | module_t *mod; | | 1067 | module_t *mod; |
1068 | size_t msize; | | 1068 | size_t msize; |
1069 | vaddr_t maddr; | | 1069 | vaddr_t maddr; |
1070 | | | 1070 | |
1071 | TAILQ_FOREACH(mod, &module_list, mod_chain) { | | 1071 | TAILQ_FOREACH(mod, &module_list, mod_chain) { |
1072 | if (mod->mod_kobj == NULL) { | | 1072 | if (mod->mod_kobj == NULL) { |
1073 | continue; | | 1073 | continue; |
1074 | } | | 1074 | } |
1075 | kobj_stat(mod->mod_kobj, &maddr, &msize); | | 1075 | if (kobj_stat(mod->mod_kobj, &maddr, &msize) != 0) |
| | | 1076 | continue; |
1076 | if (addr < maddr || addr >= maddr + msize) { | | 1077 | if (addr < maddr || addr >= maddr + msize) { |
1077 | continue; | | 1078 | continue; |
1078 | } | | 1079 | } |
1079 | (*pr)("%p is %p+%zu, in kernel module `%s'\n", | | 1080 | (*pr)("%p is %p+%zu, in kernel module `%s'\n", |
1080 | (void *)addr, (void *)maddr, | | 1081 | (void *)addr, (void *)maddr, |
1081 | (size_t)(addr - maddr), mod->mod_info->mi_name); | | 1082 | (size_t)(addr - maddr), mod->mod_info->mi_name); |
1082 | } | | 1083 | } |
1083 | } | | 1084 | } |
1084 | | | 1085 | |
1085 | /* | | 1086 | /* |
1086 | * module_print_list: | | 1087 | * module_print_list: |
1087 | * | | 1088 | * |
1088 | * Helper routine for DDB. | | 1089 | * Helper routine for DDB. |
1089 | */ | | 1090 | */ |
1090 | void | | 1091 | void |
1091 | module_print_list(void (*pr)(const char *, ...)) | | 1092 | module_print_list(void (*pr)(const char *, ...)) |
1092 | { | | 1093 | { |
1093 | const char *src; | | 1094 | const char *src; |
1094 | module_t *mod; | | 1095 | module_t *mod; |
1095 | size_t msize; | | 1096 | size_t msize; |
1096 | vaddr_t maddr; | | 1097 | vaddr_t maddr; |
1097 | | | 1098 | |
1098 | (*pr)("%16s %16s %8s %8s\n", "NAME", "TEXT/DATA", "SIZE", "SOURCE"); | | 1099 | (*pr)("%16s %16s %8s %8s\n", "NAME", "TEXT/DATA", "SIZE", "SOURCE"); |
1099 | | | 1100 | |
1100 | TAILQ_FOREACH(mod, &module_list, mod_chain) { | | 1101 | TAILQ_FOREACH(mod, &module_list, mod_chain) { |
1101 | switch (mod->mod_source) { | | 1102 | switch (mod->mod_source) { |
1102 | case MODULE_SOURCE_KERNEL: | | 1103 | case MODULE_SOURCE_KERNEL: |
1103 | src = "builtin"; | | 1104 | src = "builtin"; |
1104 | break; | | 1105 | break; |
1105 | case MODULE_SOURCE_FILESYS: | | 1106 | case MODULE_SOURCE_FILESYS: |
1106 | src = "filesys"; | | 1107 | src = "filesys"; |
1107 | break; | | 1108 | break; |
1108 | case MODULE_SOURCE_BOOT: | | 1109 | case MODULE_SOURCE_BOOT: |
1109 | src = "boot"; | | 1110 | src = "boot"; |
1110 | break; | | 1111 | break; |
1111 | default: | | 1112 | default: |
1112 | src = "unknown"; | | 1113 | src = "unknown"; |
1113 | break; | | 1114 | break; |
1114 | } | | 1115 | } |
1115 | if (mod->mod_kobj != NULL) { | | 1116 | if (mod->mod_kobj == NULL) { |
1116 | kobj_stat(mod->mod_kobj, &maddr, &msize); | | | |
1117 | } else { | | | |
1118 | maddr = 0; | | 1117 | maddr = 0; |
1119 | msize = 0; | | 1118 | msize = 0; |
1120 | } | | 1119 | } else if (kobj_stat(mod->mod_kobj, &maddr, &msize) != 0) |
| | | 1120 | continue; |
1121 | (*pr)("%16s %16lx %8ld %8s\n", mod->mod_info->mi_name, | | 1121 | (*pr)("%16s %16lx %8ld %8s\n", mod->mod_info->mi_name, |
1122 | (long)maddr, (long)msize, src); | | 1122 | (long)maddr, (long)msize, src); |
1123 | } | | 1123 | } |
1124 | } | | 1124 | } |
1125 | #endif /* DDB */ | | 1125 | #endif /* DDB */ |
1126 | | | 1126 | |
1127 | /* | | 1127 | /* |
1128 | * module_load_plist_file: | | 1128 | * module_load_plist_file: |
1129 | * | | 1129 | * |
1130 | * Load a plist located in the file system into memory. | | 1130 | * Load a plist located in the file system into memory. |
1131 | */ | | 1131 | */ |
1132 | static int | | 1132 | static int |
1133 | module_load_plist_file(const char *modpath, const bool nochroot, | | 1133 | module_load_plist_file(const char *modpath, const bool nochroot, |
1134 | void **basep, size_t *length) | | 1134 | void **basep, size_t *length) |
1135 | { | | 1135 | { |
1136 | struct nameidata nd; | | 1136 | struct nameidata nd; |
1137 | struct stat sb; | | 1137 | struct stat sb; |
1138 | void *base; | | 1138 | void *base; |
1139 | char *proppath; | | 1139 | char *proppath; |
1140 | size_t resid; | | 1140 | size_t resid; |
1141 | int error, pathlen; | | 1141 | int error, pathlen; |
1142 | | | 1142 | |
1143 | base = NULL; | | 1143 | base = NULL; |
1144 | *length = 0; | | 1144 | *length = 0; |
1145 | | | 1145 | |
1146 | proppath = PNBUF_GET(); | | 1146 | proppath = PNBUF_GET(); |
1147 | strcpy(proppath, modpath); | | 1147 | strcpy(proppath, modpath); |
1148 | pathlen = strlen(proppath); | | 1148 | pathlen = strlen(proppath); |
1149 | if ((pathlen >= 5) && (strcmp(&proppath[pathlen - 5], ".kmod") == 0)) { | | 1149 | if ((pathlen >= 5) && (strcmp(&proppath[pathlen - 5], ".kmod") == 0)) { |
1150 | strcpy(&proppath[pathlen - 5], ".prop"); | | 1150 | strcpy(&proppath[pathlen - 5], ".prop"); |
1151 | } else if (pathlen < MAXPATHLEN - 5) { | | 1151 | } else if (pathlen < MAXPATHLEN - 5) { |
1152 | strcat(proppath, ".prop"); | | 1152 | strcat(proppath, ".prop"); |
1153 | } else { | | 1153 | } else { |
1154 | error = ENOENT; | | 1154 | error = ENOENT; |
1155 | goto out1; | | 1155 | goto out1; |
1156 | } | | 1156 | } |
1157 | | | 1157 | |
1158 | NDINIT(&nd, LOOKUP, FOLLOW | (nochroot ? NOCHROOT : 0), | | 1158 | NDINIT(&nd, LOOKUP, FOLLOW | (nochroot ? NOCHROOT : 0), |
1159 | UIO_SYSSPACE, proppath); | | 1159 | UIO_SYSSPACE, proppath); |
1160 | | | 1160 | |
1161 | error = namei(&nd); | | 1161 | error = namei(&nd); |
1162 | if (error != 0) { | | 1162 | if (error != 0) { |
1163 | goto out1; | | 1163 | goto out1; |
1164 | } | | 1164 | } |
1165 | | | 1165 | |
1166 | error = vn_stat(nd.ni_vp, &sb); | | 1166 | error = vn_stat(nd.ni_vp, &sb); |
1167 | if (sb.st_size >= (PAGE_SIZE - 1)) { /* leave space for term \0 */ | | 1167 | if (sb.st_size >= (PAGE_SIZE - 1)) { /* leave space for term \0 */ |
1168 | error = EINVAL; | | 1168 | error = EINVAL; |
1169 | } | | 1169 | } |
1170 | if (error != 0) { | | 1170 | if (error != 0) { |
1171 | goto out1; | | 1171 | goto out1; |
1172 | } | | 1172 | } |
1173 | | | 1173 | |
1174 | error = vn_open(&nd, FREAD, 0); | | 1174 | error = vn_open(&nd, FREAD, 0); |
1175 | if (error != 0) { | | 1175 | if (error != 0) { |
1176 | goto out1; | | 1176 | goto out1; |
1177 | } | | 1177 | } |
1178 | | | 1178 | |
1179 | base = kmem_alloc(PAGE_SIZE, KM_SLEEP); | | 1179 | base = kmem_alloc(PAGE_SIZE, KM_SLEEP); |
1180 | if (base == NULL) { | | 1180 | if (base == NULL) { |
1181 | error = ENOMEM; | | 1181 | error = ENOMEM; |
1182 | goto out; | | 1182 | goto out; |
1183 | } | | 1183 | } |
1184 | | | 1184 | |
1185 | error = vn_rdwr(UIO_READ, nd.ni_vp, base, sb.st_size, 0, | | 1185 | error = vn_rdwr(UIO_READ, nd.ni_vp, base, sb.st_size, 0, |
1186 | UIO_SYSSPACE, IO_NODELOCKED, curlwp->l_cred, &resid, curlwp); | | 1186 | UIO_SYSSPACE, IO_NODELOCKED, curlwp->l_cred, &resid, curlwp); |
1187 | *((uint8_t *)base + sb.st_size) = '\0'; | | 1187 | *((uint8_t *)base + sb.st_size) = '\0'; |
1188 | if (error == 0 && resid != 0) { | | 1188 | if (error == 0 && resid != 0) { |
1189 | error = EINVAL; | | 1189 | error = EINVAL; |
1190 | } | | 1190 | } |
1191 | if (error != 0) { | | 1191 | if (error != 0) { |
1192 | kmem_free(base, PAGE_SIZE); | | 1192 | kmem_free(base, PAGE_SIZE); |
1193 | base = NULL; | | 1193 | base = NULL; |
1194 | } | | 1194 | } |
1195 | *length = sb.st_size; | | 1195 | *length = sb.st_size; |
1196 | | | 1196 | |
1197 | out: | | 1197 | out: |
1198 | VOP_UNLOCK(nd.ni_vp, 0); | | 1198 | VOP_UNLOCK(nd.ni_vp, 0); |
1199 | vn_close(nd.ni_vp, FREAD, kauth_cred_get()); | | 1199 | vn_close(nd.ni_vp, FREAD, kauth_cred_get()); |
1200 | | | 1200 | |
1201 | out1: | | 1201 | out1: |
1202 | PNBUF_PUT(proppath); | | 1202 | PNBUF_PUT(proppath); |
1203 | *basep = base; | | 1203 | *basep = base; |
1204 | return error; | | 1204 | return error; |
1205 | } | | 1205 | } |
1206 | | | 1206 | |
1207 | static bool | | 1207 | static bool |
1208 | module_merge_dicts(prop_dictionary_t existing_dict, | | 1208 | module_merge_dicts(prop_dictionary_t existing_dict, |
1209 | const prop_dictionary_t new_dict) | | 1209 | const prop_dictionary_t new_dict) |
1210 | { | | 1210 | { |
1211 | prop_dictionary_keysym_t props_keysym; | | 1211 | prop_dictionary_keysym_t props_keysym; |
1212 | prop_object_iterator_t props_iter; | | 1212 | prop_object_iterator_t props_iter; |
1213 | prop_object_t props_obj; | | 1213 | prop_object_t props_obj; |
1214 | const char *props_key; | | 1214 | const char *props_key; |
1215 | bool error; | | 1215 | bool error; |
1216 | | | 1216 | |
1217 | error = false; | | 1217 | error = false; |
1218 | props_iter = prop_dictionary_iterator(new_dict); | | 1218 | props_iter = prop_dictionary_iterator(new_dict); |
1219 | if (props_iter == NULL) { | | 1219 | if (props_iter == NULL) { |
1220 | return false; | | 1220 | return false; |
1221 | } | | 1221 | } |
1222 | | | 1222 | |
1223 | while ((props_obj = prop_object_iterator_next(props_iter)) != NULL) { | | 1223 | while ((props_obj = prop_object_iterator_next(props_iter)) != NULL) { |
1224 | props_keysym = (prop_dictionary_keysym_t)props_obj; | | 1224 | props_keysym = (prop_dictionary_keysym_t)props_obj; |
1225 | props_key = prop_dictionary_keysym_cstring_nocopy(props_keysym); | | 1225 | props_key = prop_dictionary_keysym_cstring_nocopy(props_keysym); |
1226 | props_obj = prop_dictionary_get_keysym(new_dict, props_keysym); | | 1226 | props_obj = prop_dictionary_get_keysym(new_dict, props_keysym); |
1227 | if ((props_obj == NULL) || !prop_dictionary_set(existing_dict, | | 1227 | if ((props_obj == NULL) || !prop_dictionary_set(existing_dict, |
1228 | props_key, props_obj)) { | | 1228 | props_key, props_obj)) { |
1229 | error = true; | | 1229 | error = true; |
1230 | goto out; | | 1230 | goto out; |
1231 | } | | 1231 | } |
1232 | } | | 1232 | } |
1233 | error = false; | | 1233 | error = false; |
1234 | | | 1234 | |
1235 | out: | | 1235 | out: |
1236 | prop_object_iterator_release(props_iter); | | 1236 | prop_object_iterator_release(props_iter); |
1237 | | | 1237 | |
1238 | return !error; | | 1238 | return !error; |
1239 | } | | 1239 | } |