xref: /netbsd/sys/sys/module.h (revision 6550d01e)
1 /*	$NetBSD: module.h,v 1.25 2010/08/21 13:17:32 pgoyette Exp $	*/
2 
3 /*-
4  * Copyright (c) 2008 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #ifndef _SYS_MODULE_H_
30 #define _SYS_MODULE_H_
31 
32 #include <sys/types.h>
33 #include <sys/param.h>
34 #include <sys/cdefs.h>
35 #include <sys/queue.h>
36 #include <sys/uio.h>
37 
38 #define	MAXMODNAME	32
39 #define	MAXMODDEPS	10
40 
41 /* Module classes, provided only for system boot and cosmetic purposes. */
42 typedef enum modclass {
43 	MODULE_CLASS_ANY,
44 	MODULE_CLASS_MISC,
45 	MODULE_CLASS_VFS,
46 	MODULE_CLASS_DRIVER,
47 	MODULE_CLASS_EXEC,
48 	MODULE_CLASS_SECMODEL
49 } modclass_t;
50 
51 /* Module sources: where did it come from? */
52 typedef enum modsrc {
53 	MODULE_SOURCE_KERNEL,
54 	MODULE_SOURCE_BOOT,
55 	MODULE_SOURCE_FILESYS
56 } modsrc_t;
57 
58 /* Commands passed to module control routine. */
59 typedef enum modcmd {
60 	MODULE_CMD_INIT,		/* mandatory */
61 	MODULE_CMD_FINI,		/* mandatory */
62 	MODULE_CMD_STAT,		/* optional */
63 	MODULE_CMD_AUTOUNLOAD,		/* optional */
64 } modcmd_t;
65 
66 #ifdef _KERNEL
67 
68 #include <sys/mutex.h>
69 
70 #include <prop/proplib.h>
71 
72 /* Module header structure. */
73 typedef struct modinfo {
74 	u_int		mi_version;
75 	modclass_t	mi_class;
76 	int		(*mi_modcmd)(modcmd_t, void *);
77 	const char	*mi_name;
78 	const char	*mi_required;
79 } const modinfo_t;
80 
81 /* Per module information, maintained by kern_module.c */
82 typedef struct module {
83 	u_int			mod_refcnt;
84 	const modinfo_t		*mod_info;
85 	struct kobj		*mod_kobj;
86 	TAILQ_ENTRY(module)	mod_chain;
87 	struct module		*mod_required[MAXMODDEPS];
88 	u_int			mod_nrequired;
89 	modsrc_t		mod_source;
90 	time_t			mod_autotime;
91 	void 			*mod_ctf;
92 	u_int			mod_fbtentries;	/* DTrace FBT entrie count */
93 	int			mod_flags;
94 #define MODFLG_MUST_FORCE	0x01
95 
96 } module_t;
97 
98 /*
99  * Per-module linkage.  Loadable modules have a `link_set_modules' section
100  * containing only one entry, pointing to the module's modinfo_t record.
101  * For the kernel, `link_set_modules' can contain multiple entries and
102  * records all modules built into the kernel at link time.
103  */
104 #define	MODULE(class, name, required)				\
105 static int name##_modcmd(modcmd_t, void *);			\
106 static const modinfo_t name##_modinfo = {			\
107 	.mi_version = __NetBSD_Version__,			\
108 	.mi_class = (class),					\
109 	.mi_modcmd = name##_modcmd,				\
110 	.mi_name = #name,					\
111 	.mi_required = (required)				\
112 }; 								\
113 __link_set_add_rodata(modules, name##_modinfo);
114 
115 TAILQ_HEAD(modlist, module);
116 
117 extern struct vm_map	*module_map;
118 extern u_int		module_count;
119 extern u_int		module_builtinlist;
120 extern struct modlist	module_list;
121 extern struct modlist	module_builtins;
122 extern u_int		module_gen;
123 
124 void	module_init(void);
125 void	module_start_unload_thread(void);
126 void	module_builtin_require_force(void);
127 void	module_init_md(void);
128 void	module_init_class(modclass_t);
129 int	module_prime(void *, size_t);
130 
131 bool	module_compatible(int, int);
132 int	module_load(const char *, int, prop_dictionary_t, modclass_t);
133 int	module_builtin_add(modinfo_t * const *, size_t, bool);
134 int	module_builtin_remove(modinfo_t *, bool);
135 int	module_autoload(const char *, modclass_t);
136 int	module_unload(const char *);
137 int	module_hold(const char *);
138 void	module_rele(const char *);
139 int	module_find_section(const char *, void **, size_t *);
140 void	module_thread_kick(void);
141 void	module_load_vfs_init(void);
142 
143 void	module_whatis(uintptr_t, void (*)(const char *, ...));
144 void	module_print_list(void (*)(const char *, ...));
145 
146 #ifdef _MODULE_INTERNAL
147 extern
148 int	(*module_load_vfs_vec)(const char *, int, bool, module_t *,
149 			       prop_dictionary_t *);
150 int	module_load_vfs(const char *, int, bool, module_t *,
151 			prop_dictionary_t *);
152 void	module_error(const char *, ...)
153 			     __attribute__((__format__(__printf__,1,2)));
154 void	module_print(const char *, ...)
155 			     __attribute__((__format__(__printf__,1,2)));
156 #endif /* _MODULE_INTERNAL */
157 
158 #define MODULE_BASE_SIZE 64
159 extern char	module_base[MODULE_BASE_SIZE];
160 
161 #else	/* _KERNEL */
162 
163 #include <stdint.h>
164 
165 #endif	/* _KERNEL */
166 
167 typedef struct modctl_load {
168 	const char *ml_filename;
169 
170 #define MODCTL_NO_PROP		0x2
171 #define MODCTL_LOAD_FORCE	0x1
172 	int ml_flags;
173 
174 	const char *ml_props;
175 	size_t ml_propslen;
176 } modctl_load_t;
177 
178 typedef enum modctl {
179 	MODCTL_LOAD,		/* modctl_load_t *ml */
180 	MODCTL_UNLOAD,		/* char *name */
181 	MODCTL_STAT		/* struct iovec *buffer */
182 } modctl_t;
183 
184 /*
185  * This structure intentionally has the same layout for 32 and 64
186  * bit builds.
187  */
188 typedef struct modstat {
189 	char		ms_name[MAXMODNAME];
190 	char		ms_required[MAXMODNAME * MAXMODDEPS];
191 	uint64_t	ms_addr;
192 	modsrc_t	ms_source;
193 	modclass_t	ms_class;
194 	u_int		ms_size;
195 	u_int		ms_refcnt;
196 	u_int		ms_reserved[4];
197 } modstat_t;
198 
199 int	modctl(int, void *);
200 
201 #endif	/* !_SYS_MODULE_H_ */
202