Deleted Added
full compact
kern_module.c (83366) kern_module.c (90963)
1/*-
2 * Copyright (c) 1997 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 unchanged lines hidden (view full) ---

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 *
1/*-
2 * Copyright (c) 1997 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 unchanged lines hidden (view full) ---

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: head/sys/kern/kern_module.c 83366 2001-09-12 08:38:13Z julian $
26 * $FreeBSD: head/sys/kern/kern_module.c 90963 2002-02-20 14:30:02Z arr $
27 */
28
29#include <sys/param.h>
30#include <sys/kernel.h>
31#include <sys/systm.h>
32#include <sys/eventhandler.h>
33#include <sys/malloc.h>
34#include <sys/sysproto.h>
35#include <sys/sysent.h>
36#include <sys/module.h>
37#include <sys/linker.h>
38#include <sys/proc.h>
39#include <sys/lock.h>
40#include <sys/mutex.h>
41
42static MALLOC_DEFINE(M_MODULE, "module", "module data structures");
43
44typedef TAILQ_HEAD(, module) modulelist_t;
45struct module {
27 */
28
29#include <sys/param.h>
30#include <sys/kernel.h>
31#include <sys/systm.h>
32#include <sys/eventhandler.h>
33#include <sys/malloc.h>
34#include <sys/sysproto.h>
35#include <sys/sysent.h>
36#include <sys/module.h>
37#include <sys/linker.h>
38#include <sys/proc.h>
39#include <sys/lock.h>
40#include <sys/mutex.h>
41
42static MALLOC_DEFINE(M_MODULE, "module", "module data structures");
43
44typedef TAILQ_HEAD(, module) modulelist_t;
45struct module {
46 TAILQ_ENTRY(module) link; /* chain together all modules */
47 TAILQ_ENTRY(module) flink; /* all modules in a file */
48 struct linker_file* file; /* file which contains this module */
49 int refs; /* reference count */
50 int id; /* unique id number */
51 char *name; /* module name */
52 modeventhand_t handler; /* event handler */
53 void *arg; /* argument for handler */
54 modspecific_t data; /* module specific data */
46 TAILQ_ENTRY(module) link; /* chain together all modules */
47 TAILQ_ENTRY(module) flink; /* all modules in a file */
48 struct linker_file *file; /* file which contains this module */
49 int refs; /* reference count */
50 int id; /* unique id number */
51 char *name; /* module name */
52 modeventhand_t handler; /* event handler */
53 void *arg; /* argument for handler */
54 modspecific_t data; /* module specific data */
55};
56
55};
56
57#define MOD_EVENT(mod, type) (mod)->handler((mod), (type), (mod)->arg)
57#define MOD_EVENT(mod, type) (mod)->handler((mod), (type), (mod)->arg)
58
59static modulelist_t modules;
60static int nextid = 1;
58
59static modulelist_t modules;
60static int nextid = 1;
61static void module_shutdown(void *, int);
61
62
62static void module_shutdown(void*, int);
63
64static int
63static int
65modevent_nop(module_t mod, int what, void* arg)
64modevent_nop(module_t mod, int what, void *arg)
66{
67 return 0;
68}
69
70
71static void
65{
66 return 0;
67}
68
69
70static void
72module_init(void* arg)
71module_init(void *arg)
73{
72{
74 TAILQ_INIT(&modules);
75 EVENTHANDLER_REGISTER(shutdown_post_sync, module_shutdown, NULL,
76 SHUTDOWN_PRI_DEFAULT);
73
74 TAILQ_INIT(&modules);
75 EVENTHANDLER_REGISTER(shutdown_post_sync, module_shutdown, NULL,
76 SHUTDOWN_PRI_DEFAULT);
77}
78
77}
78
79SYSINIT(module, SI_SUB_KLD, SI_ORDER_FIRST, module_init, 0);
79SYSINIT(module, SI_SUB_KLD, SI_ORDER_FIRST, module_init, 0)
80
81static void
80
81static void
82module_shutdown(void* arg1, int arg2)
82module_shutdown(void *arg1, int arg2)
83{
83{
84 module_t mod;
84 module_t mod;
85
85
86 TAILQ_FOREACH(mod, &modules, link)
87 MOD_EVENT(mod, MOD_SHUTDOWN);
86 TAILQ_FOREACH(mod, &modules, link)
87 MOD_EVENT(mod, MOD_SHUTDOWN);
88}
89
90void
91module_register_init(const void *arg)
92{
88}
89
90void
91module_register_init(const void *arg)
92{
93 const moduledata_t* data = (const moduledata_t*) arg;
94 int error;
95 module_t mod;
93 const moduledata_t *data = (const moduledata_t *)arg;
94 int error;
95 module_t mod;
96
96
97 mod = module_lookupbyname(data->name);
98 if (mod == NULL)
99 panic("module_register_init: module named %s not found\n", data->name);
100 error = MOD_EVENT(mod, MOD_LOAD);
101 if (error) {
102 MOD_EVENT(mod, MOD_UNLOAD);
103 module_release(mod);
104 printf("module_register_init: MOD_LOAD (%s, %lx, %p) error %d\n",
105 data->name, (u_long)(uintfptr_t)data->evhand, data->priv, error);
106 }
97 mod = module_lookupbyname(data->name);
98 if (mod == NULL)
99 panic("module_register_init: module named %s not found\n",
100 data->name);
101 error = MOD_EVENT(mod, MOD_LOAD);
102 if (error) {
103 MOD_EVENT(mod, MOD_UNLOAD);
104 module_release(mod);
105 printf(
106 "module_register_init: MOD_LOAD (%s, %lx, %p) error %d\n",
107 data->name, (u_long)(uintfptr_t)data->evhand, data->priv,
108 error);
109 }
107}
108
109int
110module_register(const moduledata_t *data, linker_file_t container)
111{
110}
111
112int
113module_register(const moduledata_t *data, linker_file_t container)
114{
112 size_t namelen;
113 module_t newmod;
115 size_t namelen;
116 module_t newmod;
114
117
115 newmod = module_lookupbyname(data->name);
116 if (newmod != NULL) {
117 printf("module_register: module %s already exists!\n", data->name);
118 return EEXIST;
119 }
120 namelen = strlen(data->name) + 1;
121 newmod = (module_t) malloc(sizeof(struct module) + namelen,
122 M_MODULE, M_WAITOK);
123 if (newmod == 0)
124 return ENOMEM;
118 newmod = module_lookupbyname(data->name);
119 if (newmod != NULL) {
120 printf("module_register: module %s already exists!\n",
121 data->name);
122 return EEXIST;
123 }
124 namelen = strlen(data->name) + 1;
125 newmod = malloc(sizeof(struct module) + namelen, M_MODULE, M_WAITOK);
126 if (newmod == NULL)
127 return ENOMEM;
128 newmod->refs = 1;
129 newmod->id = nextid++;
130 newmod->name = (char *)(newmod + 1);
131 strcpy(newmod->name, data->name);
132 newmod->handler = data->evhand ? data->evhand : modevent_nop;
133 newmod->arg = data->priv;
134 bzero(&newmod->data, sizeof(newmod->data));
135 TAILQ_INSERT_TAIL(&modules, newmod, link);
125
136
126 newmod->refs = 1;
127 newmod->id = nextid++;
128 newmod->name = (char *) (newmod + 1);
129 strcpy(newmod->name, data->name);
130 newmod->handler = data->evhand ? data->evhand : modevent_nop;
131 newmod->arg = data->priv;
132 bzero(&newmod->data, sizeof(newmod->data));
133 TAILQ_INSERT_TAIL(&modules, newmod, link);
134
135 if (container)
136 TAILQ_INSERT_TAIL(&container->modules, newmod, flink);
137 newmod->file = container;
138
139 return 0;
137 if (container)
138 TAILQ_INSERT_TAIL(&container->modules, newmod, flink);
139 newmod->file = container;
140 return 0;
140}
141
142void
143module_reference(module_t mod)
144{
141}
142
143void
144module_reference(module_t mod)
145{
145 MOD_DPF(REFS, ("module_reference: before, refs=%d\n", mod->refs));
146
146
147 mod->refs++;
147 MOD_DPF(REFS, ("module_reference: before, refs=%d\n", mod->refs));
148 mod->refs++;
148}
149
150void
151module_release(module_t mod)
152{
149}
150
151void
152module_release(module_t mod)
153{
153 if (mod->refs <= 0)
154 panic("module_release: bad reference count");
155
154
156 MOD_DPF(REFS, ("module_release: before, refs=%d\n", mod->refs));
155 if (mod->refs <= 0)
156 panic("module_release: bad reference count");
157
157
158 mod->refs--;
159 if (mod->refs == 0) {
160 TAILQ_REMOVE(&modules, mod, link);
161 if (mod->file) {
162 TAILQ_REMOVE(&mod->file->modules, mod, flink);
158 MOD_DPF(REFS, ("module_release: before, refs=%d\n", mod->refs));
159
160 mod->refs--;
161 if (mod->refs == 0) {
162 TAILQ_REMOVE(&modules, mod, link);
163 if (mod->file)
164 TAILQ_REMOVE(&mod->file->modules, mod, flink);
165 free(mod, M_MODULE);
163 }
166 }
164 free(mod, M_MODULE);
165 }
166}
167
168module_t
167}
168
169module_t
169module_lookupbyname(const char* name)
170module_lookupbyname(const char *name)
170{
171{
171 module_t mod;
172 module_t mod;
173 int err;
172
174
173 TAILQ_FOREACH(mod, &modules, link) {
174 if (!strcmp(mod->name, name))
175 return mod;
176 }
177
178 return 0;
175 TAILQ_FOREACH(mod, &modules, link) {
176 err = strcmp(mod->name, name);
177 if (err == 0)
178 return mod;
179 }
180 return 0;
179}
180
181module_t
182module_lookupbyid(int modid)
183{
181}
182
183module_t
184module_lookupbyid(int modid)
185{
184 module_t mod;
186 module_t mod;
185
187
186 TAILQ_FOREACH(mod, &modules, link) {
187 if (mod->id == modid)
188 return mod;
189 }
190
191 return 0;
188 TAILQ_FOREACH(mod, &modules, link) {
189 if (mod->id == modid)
190 return mod;
191 }
192 return 0;
192}
193
194int
195module_unload(module_t mod)
196{
193}
194
195int
196module_unload(module_t mod)
197{
197 return MOD_EVENT(mod, MOD_UNLOAD);
198
199 return MOD_EVENT(mod, MOD_UNLOAD);
198}
199
200int
201module_getid(module_t mod)
202{
200}
201
202int
203module_getid(module_t mod)
204{
203 return mod->id;
205
206 return mod->id;
204}
205
206module_t
207module_getfnext(module_t mod)
208{
207}
208
209module_t
210module_getfnext(module_t mod)
211{
209 return TAILQ_NEXT(mod, flink);
212
213 return TAILQ_NEXT(mod, flink);
210}
211
212void
213module_setspecific(module_t mod, modspecific_t *datap)
214{
214}
215
216void
217module_setspecific(module_t mod, modspecific_t *datap)
218{
215 mod->data = *datap;
219
220 mod->data = *datap;
216}
217
218/*
219 * Syscalls.
220 */
221/*
222 * MPSAFE
223 */
224int
225modnext(struct thread *td, struct modnext_args *uap)
226{
221}
222
223/*
224 * Syscalls.
225 */
226/*
227 * MPSAFE
228 */
229int
230modnext(struct thread *td, struct modnext_args *uap)
231{
227 module_t mod;
228 int error = 0;
232 module_t mod;
233 int error = 0;
229
234
230 mtx_lock(&Giant);
235 mtx_lock(&Giant);
231
236
232 td->td_retval[0] = -1;
233 if (SCARG(uap, modid) == 0) {
234 mod = TAILQ_FIRST(&modules);
235 if (mod)
236 td->td_retval[0] = mod->id;
237 td->td_retval[0] = -1;
238 if (SCARG(uap, modid) == 0) {
239 mod = TAILQ_FIRST(&modules);
240 if (mod)
241 td->td_retval[0] = mod->id;
242 else
243 error = ENOENT;
244 goto done2;
245 }
246 mod = module_lookupbyid(SCARG(uap, modid));
247 if (mod == NULL) {
248 error = ENOENT;
249 goto done2;
250 }
251 if (TAILQ_NEXT(mod, link))
252 td->td_retval[0] = TAILQ_NEXT(mod, link)->id;
237 else
253 else
238 error = ENOENT;
239 goto done2;
240 }
241
242 mod = module_lookupbyid(SCARG(uap, modid));
243 if (mod == NULL) {
244 error = ENOENT;
245 goto done2;
246 }
247
248 if (TAILQ_NEXT(mod, link))
249 td->td_retval[0] = TAILQ_NEXT(mod, link)->id;
250 else
251 td->td_retval[0] = 0;
254 td->td_retval[0] = 0;
252done2:
255done2:
253 mtx_unlock(&Giant);
254 return (error);
256 mtx_unlock(&Giant);
257 return (error);
255}
256
257/*
258 * MPSAFE
259 */
260int
261modfnext(struct thread *td, struct modfnext_args *uap)
262{
258}
259
260/*
261 * MPSAFE
262 */
263int
264modfnext(struct thread *td, struct modfnext_args *uap)
265{
263 module_t mod;
264 int error;
266 module_t mod;
267 int error;
265
268
266 td->td_retval[0] = -1;
269 td->td_retval[0] = -1;
267
270
268 mtx_lock(&Giant);
271 mtx_lock(&Giant);
269
272
270 mod = module_lookupbyid(SCARG(uap, modid));
271 if (mod == NULL) {
272 error = ENOENT;
273 } else {
274 error = 0;
275 if (TAILQ_NEXT(mod, flink))
276 td->td_retval[0] = TAILQ_NEXT(mod, flink)->id;
277 else
278 td->td_retval[0] = 0;
279 }
280 mtx_unlock(&Giant);
281 return (error);
273 mod = module_lookupbyid(SCARG(uap, modid));
274 if (mod == NULL) {
275 error = ENOENT;
276 } else {
277 error = 0;
278 if (TAILQ_NEXT(mod, flink))
279 td->td_retval[0] = TAILQ_NEXT(mod, flink)->id;
280 else
281 td->td_retval[0] = 0;
282 }
283 mtx_unlock(&Giant);
284 return (error);
282}
283
284struct module_stat_v1 {
285}
286
287struct module_stat_v1 {
285 int version; /* set to sizeof(struct module_stat) */
286 char name[MAXMODNAME];
287 int refs;
288 int id;
288 int version; /* set to sizeof(struct module_stat) */
289 char name[MAXMODNAME];
290 int refs;
291 int id;
289};
290
291/*
292 * MPSAFE
293 */
294int
295modstat(struct thread *td, struct modstat_args *uap)
296{
292};
293
294/*
295 * MPSAFE
296 */
297int
298modstat(struct thread *td, struct modstat_args *uap)
299{
297 module_t mod;
298 int error = 0;
299 int namelen;
300 int version;
301 struct module_stat* stat;
300 module_t mod;
301 int error = 0;
302 int namelen;
303 int version;
304 struct module_stat *stat;
302
305
303 mtx_lock(&Giant);
306 mtx_lock(&Giant);
304
307
305 mod = module_lookupbyid(SCARG(uap, modid));
306 if (mod == NULL) {
307 error = ENOENT;
308 goto out;
309 }
308 mod = module_lookupbyid(SCARG(uap, modid));
309 if (mod == NULL) {
310 error = ENOENT;
311 goto out;
312 }
313 stat = SCARG(uap, stat);
310
314
311 stat = SCARG(uap, stat);
315 /*
316 * Check the version of the user's structure.
317 */
318 if ((error = copyin(&stat->version, &version, sizeof(version))) != 0)
319 goto out;
320 if (version != sizeof(struct module_stat_v1)
321 && version != sizeof(struct module_stat)) {
322 error = EINVAL;
323 goto out;
324 }
325 namelen = strlen(mod->name) + 1;
326 if (namelen > MAXMODNAME)
327 namelen = MAXMODNAME;
328 if ((error = copyout(mod->name, &stat->name[0], namelen)) != 0)
329 goto out;
312
330
313 /*
314 * Check the version of the user's structure.
315 */
316 if ((error = copyin(&stat->version, &version, sizeof(version))) != 0)
317 goto out;
318 if (version != sizeof(struct module_stat_v1)
319 && version != sizeof(struct module_stat)) {
320 error = EINVAL;
321 goto out;
322 }
331 if ((error = copyout(&mod->refs, &stat->refs, sizeof(int))) != 0)
332 goto out;
333 if ((error = copyout(&mod->id, &stat->id, sizeof(int))) != 0)
334 goto out;
323
335
324 namelen = strlen(mod->name) + 1;
325 if (namelen > MAXMODNAME)
326 namelen = MAXMODNAME;
327 if ((error = copyout(mod->name, &stat->name[0], namelen)) != 0)
328 goto out;
329
330 if ((error = copyout(&mod->refs, &stat->refs, sizeof(int))) != 0)
331 goto out;
332 if ((error = copyout(&mod->id, &stat->id, sizeof(int))) != 0)
333 goto out;
334
335 /*
336 * >v1 stat includes module data.
337 */
338 if (version == sizeof(struct module_stat)) {
339 if ((error = copyout(&mod->data, &stat->data, sizeof(mod->data))) != 0)
340 goto out;
341 }
342
343 td->td_retval[0] = 0;
344
336 /*
337 * >v1 stat includes module data.
338 */
339 if (version == sizeof(struct module_stat)) {
340 if ((error = copyout(&mod->data, &stat->data,
341 sizeof(mod->data))) != 0)
342 goto out;
343 }
344 td->td_retval[0] = 0;
345out:
345out:
346 mtx_unlock(&Giant);
347 return error;
346 mtx_unlock(&Giant);
347 return error;
348}
349
350/*
351 * MPSAFE
352 */
353int
354modfind(struct thread *td, struct modfind_args *uap)
355{
348}
349
350/*
351 * MPSAFE
352 */
353int
354modfind(struct thread *td, struct modfind_args *uap)
355{
356 int error = 0;
357 char name[MAXMODNAME];
358 module_t mod;
356 int error = 0;
357 char name[MAXMODNAME];
358 module_t mod;
359
359
360 if ((error = copyinstr(SCARG(uap, name), name, sizeof name, 0)) != 0)
361 goto out;
360 if ((error = copyinstr(SCARG(uap, name), name, sizeof name, 0)) != 0)
361 goto out;
362
362
363 mtx_lock(&Giant);
364 mod = module_lookupbyname(name);
365 if (mod == NULL)
366 error = ENOENT;
367 else
368 td->td_retval[0] = mod->id;
369 mtx_unlock(&Giant);
363 mtx_lock(&Giant);
364 mod = module_lookupbyname(name);
365 if (mod == NULL)
366 error = ENOENT;
367 else
368 td->td_retval[0] = mod->id;
369 mtx_unlock(&Giant);
370out:
370out:
371 return error;
371 return error;
372}
372}