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