summaryrefslogtreecommitdiffstats
path: root/sys/kern/kern_module.c
blob: 540be5323852ff32c7235e68166756bedc8f4d1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*-
 * Copyright (c) 1997 Doug Rabson
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $FreeBSD$
 */

#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/systm.h>
#include <sys/eventhandler.h>
#include <sys/malloc.h>
#include <sys/sysproto.h>
#include <sys/sysent.h>
#include <sys/module.h>
#include <sys/linker.h>
#include <sys/proc.h>

MALLOC_DEFINE(M_MODULE, "module", "module data structures");

typedef TAILQ_HEAD(, struct module) modulelist_t;
struct module {
    TAILQ_ENTRY(struct module)	link;		/* chain together all modules */
    TAILQ_ENTRY(struct module)	flink;		/* all modules in a file */
    struct linker_file*	file;		/* file which contains this module */
    int			refs;		/* reference count */
    int			id;		/* unique id number */
    char		*name;		/* module name */
    modeventhand_t	handler;	/* event handler */
    void		*arg;		/* argument for handler */
    modspecific_t	data;		/* module specific data */
};

#define MOD_EVENT(mod, type) (mod)->handler((mod), (type), (mod)->arg)

static modulelist_t modules;
static int nextid = 1;

static void module_shutdown(void*, int);

static int
modevent_nop(module_t mod, int what, void* arg)
{
	return 0;
}


static void
module_init(void* arg)
{
    TAILQ_INIT(&modules);
    EVENTHANDLER_REGISTER(shutdown_post_sync, module_shutdown, NULL,
			  SHUTDOWN_PRI_DEFAULT);
}

SYSINIT(module, SI_SUB_KLD, SI_ORDER_FIRST, module_init, 0);

static void
module_shutdown(void* arg1, int arg2)
{
    module_t mod;

    for (mod = TAILQ_FIRST(&modules); mod; mod = TAILQ_NEXT(mod, link))
	MOD_EVENT(mod, MOD_SHUTDOWN);
}

void
module_register_init(const void *arg)
{
    const moduledata_t* data = (const moduledata_t*) arg;
    int error;
    module_t mod;

    mod = module_lookupbyname(data->name);
    if (mod == NULL)
	panic("module_register_init: module named %s not found\n", data->name);
    error = MOD_EVENT(mod, MOD_LOAD);
    if (error) {
	MOD_EVENT(mod, MOD_UNLOAD);
	module_release(mod);
	printf("module_register_init: MOD_LOAD (%s, %lx, %p) error %d\n",
	       data->name, (u_long)(uintfptr_t)data->evhand, data->priv, error);
    }
}

int
module_register(const moduledata_t *data, linker_file_t container)
{
    size_t namelen;
    module_t newmod;

    newmod = module_lookupbyname(data->name);
    if (newmod != NULL) {
	printf("module_register: module %s already exists!\n", data->name);
	return EEXIST;
    }
    namelen = strlen(data->name) + 1;
    newmod = (module_t) malloc(sizeof(struct module) + namelen,
			       M_MODULE, M_WAITOK);
    if (newmod == 0)
	return ENOMEM;

    newmod->refs = 1;
    newmod->id = nextid++;
    newmod->name = (char *) (newmod + 1);
    strcpy(newmod->name, data->name);
    newmod->handler = data->evhand ? data->evhand : modevent_nop;
    newmod->arg = data->priv;
    bzero(&newmod->data, sizeof(newmod->data));
    TAILQ_INSERT_TAIL(&modules, newmod, link);

    if (container)
	TAILQ_INSERT_TAIL(&container->modules, newmod, flink);
    newmod->file = container;

    return 0;
}

void
module_reference(module_t mod)
{
    MOD_DPF(REFS, ("module_reference: before, refs=%d\n", mod->refs));

    mod->refs++;
}

void
module_release(module_t mod)
{
    if (mod->refs <= 0)
	panic("module_release: bad reference count");

    MOD_DPF(REFS, ("module_release: before, refs=%d\n", mod->refs));

    mod->refs--;
    if (mod->refs == 0) {
	TAILQ_REMOVE(&modules, mod, link);
	if (mod->file) {
	    TAILQ_REMOVE(&mod->file->modules, mod, flink);
	}
	free(mod, M_MODULE);
    }
}

module_t
module_lookupbyname(const char* name)
{
    module_t mod;

    for (mod = TAILQ_FIRST(&modules); mod; mod = TAILQ_NEXT(mod, link)) {
	if (!strcmp(mod->name, name))
	    return mod;
    }

    return 0;
}

module_t
module_lookupbyid(int modid)
{
    module_t mod;

    for (mod = TAILQ_FIRST(&modules); mod; mod = TAILQ_NEXT(mod, link)) {
	if (mod->id == modid)
	    return mod;
    }

    return 0;
}

int
module_unload(module_t mod)
{
    return MOD_EVENT(mod, MOD_UNLOAD);
}

int
module_getid(module_t mod)
{
    return mod->id;
}

module_t
module_getfnext(module_t mod)
{
    return TAILQ_NEXT(mod, flink);
}

void
module_setspecific(module_t mod, modspecific_t *datap)
{
    mod->data = *datap;
}

/*
 * Syscalls.
 */
int
modnext(struct proc* p, struct modnext_args* uap)
{
    module_t mod;

    p->p_retval[0] = -1;
    if (SCARG(uap, modid) == 0) {
	mod = TAILQ_FIRST(&modules);
	if (mod) {
	    p->p_retval[0] = mod->id;
	    return 0;
	} else
	    return ENOENT;
    }

    mod = module_lookupbyid(SCARG(uap, modid));
    if (!mod)
	return ENOENT;

    if (TAILQ_NEXT(mod, link))
	p->p_retval[0] = TAILQ_NEXT(mod, link)->id;
    else
	p->p_retval[0] = 0;
    return 0;
}

int
modfnext(struct proc* p, struct modfnext_args* uap)
{
    module_t mod;

    p->p_retval[0] = -1;

    mod = module_lookupbyid(SCARG(uap, modid));
    if (!mod)
	return ENOENT;

    if (TAILQ_NEXT(mod, flink))
	p->p_retval[0] = TAILQ_NEXT(mod, flink)->id;
    else
	p->p_retval[0] = 0;
    return 0;
}

struct module_stat_v1 {
    int		version;	/* set to sizeof(struct module_stat) */
    char	name[MAXMODNAME];
    int		refs;
    int		id;
};

int
modstat(struct proc* p, struct modstat_args* uap)
{
    module_t mod;
    int error = 0;
    int namelen;
    int version;
    struct module_stat* stat;

    mod = module_lookupbyid(SCARG(uap, modid));
    if (!mod)
	return ENOENT;

    stat = SCARG(uap, stat);

    /*
     * Check the version of the user's structure.
     */
    if ((error = copyin(&stat->version, &version, sizeof(version))) != 0)
	goto out;
    if (version != sizeof(struct module_stat_v1)
	&& version != sizeof(struct module_stat)) {
	error = EINVAL;
	goto out;
    }

    namelen = strlen(mod->name) + 1;
    if (namelen > MAXMODNAME)
	namelen = MAXMODNAME;
    if ((error = copyout(mod->name, &stat->name[0], namelen)) != 0)
	goto out;

    if ((error = copyout(&mod->refs, &stat->refs, sizeof(int))) != 0)
	goto out;
    if ((error = copyout(&mod->id, &stat->id, sizeof(int))) != 0)
	goto out;

    /*
     * >v1 stat includes module data.
     */
    if (version == sizeof(struct module_stat)) {
	if ((error = copyout(&mod->data, &stat->data, sizeof(mod->data))) != 0)
	    goto out;
    }

    p->p_retval[0] = 0;

out:
    return error;
}

int
modfind(struct proc* p, struct modfind_args* uap)
{
    int error = 0;
    char name[MAXMODNAME];
    module_t mod;

    if ((error = copyinstr(SCARG(uap, name), name, sizeof name, 0)) != 0)
	goto out;

    mod = module_lookupbyname(name);
    if (!mod)
	error = ENOENT;
    else
	p->p_retval[0] = mod->id;

out:
    return error;
}
OpenPOWER on IntegriCloud