| @@ -1,521 +1,548 @@ | | | @@ -1,521 +1,548 @@ |
1 | .\" $NetBSD: module.9,v 1.42.2.3 2018/03/11 00:44:32 pgoyette Exp $ | | 1 | .\" $NetBSD: module.9,v 1.42.2.4 2018/03/14 02:47:41 pgoyette Exp $ |
2 | .\" | | 2 | .\" |
3 | .\" Copyright (c) 2010 The NetBSD Foundation, Inc. | | 3 | .\" Copyright (c) 2010 The NetBSD Foundation, Inc. |
4 | .\" All rights reserved. | | 4 | .\" All rights reserved. |
5 | .\" | | 5 | .\" |
6 | .\" This code is derived from software contributed to The NetBSD Foundation | | 6 | .\" This code is derived from software contributed to The NetBSD Foundation |
7 | .\" by Andrew Doran. | | 7 | .\" by Andrew Doran. |
8 | .\" | | 8 | .\" |
9 | .\" Redistribution and use in source and binary forms, with or without | | 9 | .\" Redistribution and use in source and binary forms, with or without |
10 | .\" modification, are permitted provided that the following conditions | | 10 | .\" modification, are permitted provided that the following conditions |
11 | .\" are met: | | 11 | .\" are met: |
12 | .\" 1. Redistributions of source code must retain the above copyright | | 12 | .\" 1. Redistributions of source code must retain the above copyright |
13 | .\" notice, this list of conditions and the following disclaimer. | | 13 | .\" notice, this list of conditions and the following disclaimer. |
14 | .\" 2. Redistributions in binary form must reproduce the above copyright | | 14 | .\" 2. Redistributions in binary form must reproduce the above copyright |
15 | .\" notice, this list of conditions and the following disclaimer in the | | 15 | .\" notice, this list of conditions and the following disclaimer in the |
16 | .\" documentation and/or other materials provided with the distribution. | | 16 | .\" documentation and/or other materials provided with the distribution. |
17 | .\" | | 17 | .\" |
18 | .\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS | | 18 | .\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS |
19 | .\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | | 19 | .\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
20 | .\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | | 20 | .\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
21 | .\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS | | 21 | .\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS |
22 | .\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | | 22 | .\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
23 | .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | | 23 | .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
24 | .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | | 24 | .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
25 | .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | | 25 | .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
26 | .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | | 26 | .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
27 | .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | | 27 | .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
28 | .\" POSSIBILITY OF SUCH DAMAGE. | | 28 | .\" POSSIBILITY OF SUCH DAMAGE. |
29 | .\" | | 29 | .\" |
30 | .Dd December 16, 2017 | | 30 | .Dd March 14, 2018 |
31 | .Dt MODULE 9 | | 31 | .Dt MODULE 9 |
32 | .Os | | 32 | .Os |
33 | .Sh NAME | | 33 | .Sh NAME |
34 | .Nm module , | | 34 | .Nm module , |
35 | .Nm module_load , | | 35 | .Nm module_load , |
36 | .Nm module_autoload , | | 36 | .Nm module_autoload , |
37 | .Nm module_unload , | | 37 | .Nm module_unload , |
38 | .Nm module_init_class , | | 38 | .Nm module_init_class , |
39 | .Nm module_hold , | | 39 | .Nm module_hold , |
40 | .Nm module_rele , | | 40 | .Nm module_rele , |
41 | .Nm module_find_section | | 41 | .Nm module_find_section |
42 | .Nd kernel module loader | | 42 | .Nd kernel module loader |
43 | .Sh SYNOPSIS | | 43 | .Sh SYNOPSIS |
44 | .In sys/module.h | | 44 | .In sys/module.h |
45 | .Fn MODULE "class" "name" "required" | | 45 | .Fn MODULE "class" "name" "required" |
| | | 46 | .Fn MODULE_WITH_ALIAS "class" "name" "required" "aliases" |
46 | .Ft int | | 47 | .Ft int |
47 | .Fn module_load "const char *name" "int flags" "prop_dictionary_t props" \ | | 48 | .Fn module_load "const char *name" "int flags" "prop_dictionary_t props" \ |
48 | "modclass_t class" | | 49 | "modclass_t class" |
49 | .Ft int | | 50 | .Ft int |
50 | .Fn module_autoload "const char *name" "modclass_t class" | | 51 | .Fn module_autoload "const char *name" "modclass_t class" |
51 | .Ft int | | 52 | .Ft int |
52 | .Fn module_unload "const char *name" | | 53 | .Fn module_unload "const char *name" |
53 | .Ft void | | 54 | .Ft void |
54 | .Fn module_init_class "modclass_t class" | | 55 | .Fn module_init_class "modclass_t class" |
55 | .Ft int | | 56 | .Ft int |
56 | .Fn module_hold "const char *name" | | 57 | .Fn module_hold "const char *name" |
57 | .Ft void | | 58 | .Ft void |
58 | .Fn module_rele "const char *" | | 59 | .Fn module_rele "const char *" |
59 | .Ft int | | 60 | .Ft int |
60 | .Fn module_find_section "const char *" "void **" "size_t *" | | 61 | .Fn module_find_section "const char *" "void **" "size_t *" |
61 | .Ft void | | 62 | .Ft void |
62 | .Fn module_init "void" | | 63 | .Fn module_init "void" |
63 | .Ft void | | 64 | .Ft void |
64 | .Fn module_start_unload_thread "void" | | 65 | .Fn module_start_unload_thread "void" |
65 | .Ft void | | 66 | .Ft void |
66 | .Fn module_builtin_require_force "void" | | 67 | .Fn module_builtin_require_force "void" |
67 | .Ft void | | 68 | .Ft void |
68 | .Fn module_load_vfs_init "void" | | 69 | .Fn module_load_vfs_init "void" |
69 | .Sh DESCRIPTION | | 70 | .Sh DESCRIPTION |
70 | Modules are sections of code that can be independently linked and selectively | | 71 | Modules are sections of code that can be independently linked and selectively |
71 | loaded into or unloaded from a running kernel. | | 72 | loaded into or unloaded from a running kernel. |
72 | This provides a mechanism to update the module without having to relink | | 73 | This provides a mechanism to update the module without having to relink |
73 | the kernel and reboot. | | 74 | the kernel and reboot. |
74 | Modules can be loaded from within the kernel image, provided by the boot | | 75 | Modules can be loaded from within the kernel image, provided by the boot |
75 | loader, or loaded from the file system. | | 76 | loader, or loaded from the file system. |
76 | .Pp | | 77 | .Pp |
77 | The | | 78 | The |
78 | .Nm | | 79 | .Nm |
79 | subsystem includes two data types: | | 80 | subsystem includes two data types: |
80 | .Bl -enum -offset indent | | 81 | .Bl -enum -offset indent |
81 | .It | | 82 | .It |
82 | The | | 83 | The |
83 | .Vt module_t | | 84 | .Vt module_t |
84 | type provides storage to describe a module. | | 85 | type provides storage to describe a module. |
85 | .It | | 86 | .It |
86 | The | | 87 | The |
87 | .Vt modinfo_t | | 88 | .Vt modinfo_t |
88 | type resides within | | 89 | type resides within |
89 | .Vt module_t | | 90 | .Vt module_t |
90 | and contains module header info. | | 91 | and contains module header info. |
91 | .El | | 92 | .El |
92 | .Pp | | 93 | .Pp |
93 | The module subsystem is protected by the global | | 94 | The module subsystem is protected by the global |
94 | .Va kernconfig_mutex . | | 95 | .Va kernconfig_mutex . |
95 | .Sh FUNCTIONS | | 96 | .Sh FUNCTIONS |
96 | .Bl -tag -width abcd | | 97 | .Bl -tag -width abcd |
97 | .It Fn MODULE "class" "name" "required" | | 98 | .It Fn MODULE "class" "name" "required" |
| | | 99 | .It Fn MODULE_WITH_ALIASES "class" "name" "required" "aliases" |
98 | The | | 100 | The |
99 | .Fn MODULE | | 101 | .Fn MODULE |
100 | macro creates and initializes a | | 102 | and |
| | | 103 | .Fn MODULE_WITH_ALIASES |
| | | 104 | macros create and initialize a |
101 | .Vt modinfo_t | | 105 | .Vt modinfo_t |
102 | structure. | | 106 | structure. |
103 | The | | 107 | The |
104 | .Fa class | | 108 | .Fa class |
105 | argument identifies the class of module, and must be one of the following: | | 109 | argument identifies the class of module, and must be one of the following: |
106 | .Bl -tag -width MODULE_CLASS_SECMODEL -offset indent | | 110 | .Bl -tag -width MODULE_CLASS_SECMODEL -offset indent |
107 | .It Dv MODULE_CLASS_VFS | | 111 | .It Dv MODULE_CLASS_VFS |
108 | The module provide a virtual file system - see | | 112 | The module provide a virtual file system - see |
109 | .Xr vfs 9 | | 113 | .Xr vfs 9 |
110 | .It Dv MODULE_CLASS_DRIVER | | 114 | .It Dv MODULE_CLASS_DRIVER |
111 | The module is a device driver - see | | 115 | The module is a device driver - see |
112 | .Xr driver 9 | | 116 | .Xr driver 9 |
113 | .It Dv MODULE_CLASS_EXEC | | 117 | .It Dv MODULE_CLASS_EXEC |
114 | The module provides an alternate execution environment - see the various | | 118 | The module provides an alternate execution environment - see the various |
115 | .Dv COMPAT_xxx | | 119 | .Dv COMPAT_xxx |
116 | options in | | 120 | options in |
117 | .Xr options 4 | | 121 | .Xr options 4 |
118 | .It Dv MODULE_CLASS_SECMODEL | | 122 | .It Dv MODULE_CLASS_SECMODEL |
119 | The module provides a security model - see | | 123 | The module provides a security model - see |
120 | .Xr secmodel 9 | | 124 | .Xr secmodel 9 |
121 | .It Dv MODULE_CLASS_BUFQ | | 125 | .It Dv MODULE_CLASS_BUFQ |
122 | The module provides a buffer queue strategy - see | | 126 | The module provides a buffer queue strategy - see |
123 | .Xr bufq 9 | | 127 | .Xr bufq 9 |
124 | .It Dv MODULE_CLASS_MISC | | 128 | .It Dv MODULE_CLASS_MISC |
125 | The module provides miscellaneous kernel services | | 129 | The module provides miscellaneous kernel services |
126 | .El | | 130 | .El |
127 | .Pp | | 131 | .Pp |
128 | The | | 132 | The |
129 | .Fa name | | 133 | .Fa name |
130 | argument provides the name of the module. | | 134 | argument provides the name of the module. |
131 | Loaded modules, including those that are built-in to the kernel, must all | | 135 | Loaded modules, including those that are built-in to the kernel, must all |
132 | have unique names. | | 136 | have unique names. |
133 | .Pp | | 137 | .Pp |
134 | The | | 138 | The |
135 | .Fa required | | 139 | .Fa required |
136 | argument is a quoted string containing a comma-separated list of module | | 140 | argument is a quoted string containing a comma-separated list of module |
137 | names that are required by this module. | | 141 | names that are required by this module. |
138 | The list must not contain any white-space. | | 142 | The list must not contain any white-space. |
139 | When a module is loaded, all of its required modules are auto-loaded and | | 143 | When a module is loaded, all of its required modules are auto-loaded and |
140 | initialized before the module itself is loaded. | | 144 | initialized before the module itself is loaded. |
141 | Loading of required modules is a recursive operation. | | 145 | Loading of required modules is a recursive operation. |
142 | .Pp | | 146 | .Pp |
143 | If there are no required modules, this argument should be specified as | | 147 | The |
| | | 148 | .Fa aliases |
| | | 149 | argument is a pointer to a NULL-terminated constant array of constant |
| | | 150 | strings. |
| | | 151 | Each string in the array is an alternate name (i.e., an alias) of the |
| | | 152 | module. |
| | | 153 | Module names and aliases must all be unique; a module cannot be loaded |
| | | 154 | if its name or any of its aliases matches the name or an alias of any |
| | | 155 | already-loaded module. |
| | | 156 | (Module names are displayed by the |
| | | 157 | .Xr modstat 8 |
| | | 158 | utility, with an |
| | | 159 | .Dv A |
| | | 160 | in the flags column.) |
| | | 161 | .Pp |
| | | 162 | For example, |
| | | 163 | .Bd -literal |
| | | 164 | const char * const my_pets[] = { "dog", "cat", "bird", NULL }; |
| | | 165 | |
| | | 166 | MODULE_WITH_ALIASES(MODULE_CLASS_MISC, pets, NULL, &my_pets); |
| | | 167 | .Ed |
| | | 168 | .Pp |
| | | 169 | If there are no required modules or aliases, these argument should be |
| | | 170 | specified as |
144 | .Dv NULL . | | 171 | .Dv NULL . |
145 | .Pp | | 172 | .Pp |
146 | In addition to the explicit arguments, the | | 173 | In addition to the explicit arguments, the |
147 | .Fn MODULE | | 174 | .Fn MODULE |
148 | macro creates a reference to the module's | | 175 | macro creates a reference to the module's |
149 | .Fn modcmd | | 176 | .Fn modcmd |
150 | function. | | 177 | function. |
151 | This function is defined as: | | 178 | This function is defined as: |
152 | .Bl -tag -width modcmd -offset indent | | 179 | .Bl -tag -width modcmd -offset indent |
153 | .It Ft int | | 180 | .It Ft int |
154 | .Fn xxx_modcmd "modcmd_t cmd" "void *data" | | 181 | .Fn xxx_modcmd "modcmd_t cmd" "void *data" |
155 | .El | | 182 | .El |
156 | .Pp | | 183 | .Pp |
157 | (where xxx is the name of the module, from the | | 184 | (where xxx is the name of the module, from the |
158 | .Dv MODULE | | 185 | .Dv MODULE |
159 | macro). | | 186 | macro). |
160 | .Pp | | 187 | .Pp |
161 | The | | 188 | The |
162 | .Fa cmd | | 189 | .Fa cmd |
163 | argument requests one of the following operations: | | 190 | argument requests one of the following operations: |
164 | .Bl -tag -width MODULE_CMD_AUTOUNLOAD -offset indent | | 191 | .Bl -tag -width MODULE_CMD_AUTOUNLOAD -offset indent |
165 | .It Dv MODULE_CMD_INIT | | 192 | .It Dv MODULE_CMD_INIT |
166 | Perform module-specific initialization when the module is loaded. | | 193 | Perform module-specific initialization when the module is loaded. |
167 | .It Dv MODULE_CMD_FINI | | 194 | .It Dv MODULE_CMD_FINI |
168 | Perform module-specific clean-up before the module is unloaded. | | 195 | Perform module-specific clean-up before the module is unloaded. |
169 | .It Dv MODULE_CMD_AUTOUNLOAD | | 196 | .It Dv MODULE_CMD_AUTOUNLOAD |
170 | Notify the module that it is about to be unloaded. | | 197 | Notify the module that it is about to be unloaded. |
171 | .It Dv MODULE_CMD_STAT | | 198 | .It Dv MODULE_CMD_STAT |
172 | Request the module to provide status information (not currently implemented). | | 199 | Request the module to provide status information (not currently implemented). |
173 | .El | | 200 | .El |
174 | .Pp | | 201 | .Pp |
175 | All modules' | | 202 | All modules' |
176 | .Fn modcmd | | 203 | .Fn modcmd |
177 | functions must implement the | | 204 | functions must implement the |
178 | .Dv MODULE_CMD_INIT | | 205 | .Dv MODULE_CMD_INIT |
179 | and | | 206 | and |
180 | .Dv MODULE_CMD_FINI | | 207 | .Dv MODULE_CMD_FINI |
181 | commands. | | 208 | commands. |
182 | The other commands are optional, | | 209 | The other commands are optional, |
183 | and should return | | 210 | and should return |
184 | .Er ENOTTY | | 211 | .Er ENOTTY |
185 | if not implemented. | | 212 | if not implemented. |
186 | .Pp | | 213 | .Pp |
187 | For the | | 214 | For the |
188 | .Dv MODULE_CMD_INIT | | 215 | .Dv MODULE_CMD_INIT |
189 | command, the | | 216 | command, the |
190 | .Fa data | | 217 | .Fa data |
191 | argument is used to pass a pointer to the module's | | 218 | argument is used to pass a pointer to the module's |
192 | .Xr prop_dictionary 3 . | | 219 | .Xr prop_dictionary 3 . |
193 | For the | | 220 | For the |
194 | .Dv MODULE_CMD_STAT | | 221 | .Dv MODULE_CMD_STAT |
195 | command, the | | 222 | command, the |
196 | .Fa data | | 223 | .Fa data |
197 | argument points to a buffer where the status information should be placed. | | 224 | argument points to a buffer where the status information should be placed. |
198 | .Pp | | 225 | .Pp |
199 | The __link_set mechanism is used to enable the | | 226 | The __link_set mechanism is used to enable the |
200 | .Nm | | 227 | .Nm |
201 | subsystem to locate the | | 228 | subsystem to locate the |
202 | .Vt modinfo_t | | 229 | .Vt modinfo_t |
203 | structure. | | 230 | structure. |
204 | .It Fn module_load "name" "flags" "props" "class" | | 231 | .It Fn module_load "name" "flags" "props" "class" |
205 | Load a module, link it into the running kernel, and call the module's | | 232 | Load a module, link it into the running kernel, and call the module's |
206 | .Fn modcmd | | 233 | .Fn modcmd |
207 | routine with a | | 234 | routine with a |
208 | .Fa cmd | | 235 | .Fa cmd |
209 | argument of | | 236 | argument of |
210 | .Dv MODULE_CMD_INIT . | | 237 | .Dv MODULE_CMD_INIT . |
211 | If the specified module requires other modules, they are loaded first; if | | 238 | If the specified module requires other modules, they are loaded first; if |
212 | any required module cannot be loaded or if any of their | | 239 | any required module cannot be loaded or if any of their |
213 | .Fn modcmd | | 240 | .Fn modcmd |
214 | control routines returns a non-zero status, loading of this module and | | 241 | control routines returns a non-zero status, loading of this module and |
215 | the specific required module will fail. | | 242 | the specific required module will fail. |
216 | The required modules are marked for automatic unloading. | | 243 | The required modules are marked for automatic unloading. |
217 | Thus, if the loading of the module failed, the required modules will | | 244 | Thus, if the loading of the module failed, the required modules will |
218 | be automatically unloaded after a short delay. | | 245 | be automatically unloaded after a short delay. |
219 | .Pp | | 246 | .Pp |
220 | The loader will look first for a built-in module with the specified | | 247 | The loader will look first for a built-in module with the specified |
221 | .Fa name | | 248 | .Fa name |
222 | that has not been disabled (see | | 249 | that has not been disabled (see |
223 | .Fn module_unload | | 250 | .Fn module_unload |
224 | below). | | 251 | below). |
225 | If a built-in module with that | | 252 | If a built-in module with that |
226 | .Fa name | | 253 | .Fa name |
227 | is not found, the list of modules prepared by the boot loader is searched. | | 254 | is not found, the list of modules prepared by the boot loader is searched. |
228 | If the named module is still not found, an attempt is made to locate the | | 255 | If the named module is still not found, an attempt is made to locate the |
229 | module within the file system, provided it has been mounted by the | | 256 | module within the file system, provided it has been mounted by the |
230 | initialization code. | | 257 | initialization code. |
231 | .Pp | | 258 | .Pp |
232 | The | | 259 | The |
233 | .Fa flags | | 260 | .Fa flags |
234 | argument can include: | | 261 | argument can include: |
235 | .Bl -tag -width MODCTL_LOAD_FORCE -offset indent | | 262 | .Bl -tag -width MODCTL_LOAD_FORCE -offset indent |
236 | .It Dv MODCTL_NO_PROP | | 263 | .It Dv MODCTL_NO_PROP |
237 | When loading a module from the file system, do not attempt to locate a | | 264 | When loading a module from the file system, do not attempt to locate a |
238 | corresponding prop_dictionary file. | | 265 | corresponding prop_dictionary file. |
239 | .It Dv MODCTL_LOAD_FORCE | | 266 | .It Dv MODCTL_LOAD_FORCE |
240 | Force loading of disabled built-in modules and modules built for a | | 267 | Force loading of disabled built-in modules and modules built for a |
241 | different version of the operating system. | | 268 | different version of the operating system. |
242 | .El | | 269 | .El |
243 | .Pp | | 270 | .Pp |
244 | The | | 271 | The |
245 | .Fa props | | 272 | .Fa props |
246 | argument points to an externalized property list which is passed to the | | 273 | argument points to an externalized property list which is passed to the |
247 | module's | | 274 | module's |
248 | .Fn modcmd | | 275 | .Fn modcmd |
249 | routine. | | 276 | routine. |
250 | If a module is being loaded from the file system, and the | | 277 | If a module is being loaded from the file system, and the |
251 | .Dv MODCTL_NO_PROP | | 278 | .Dv MODCTL_NO_PROP |
252 | flag is not set, the system searches for a file with the same name as the | | 279 | flag is not set, the system searches for a file with the same name as the |
253 | module file, but with the suffix | | 280 | module file, but with the suffix |
254 | .Dq Pa .plist . | | 281 | .Dq Pa .plist . |
255 | If this file is found, the prop_dictionary it contains is loaded and | | 282 | If this file is found, the prop_dictionary it contains is loaded and |
256 | merged with the prop_dictionary from the | | 283 | merged with the prop_dictionary from the |
257 | .Fa props | | 284 | .Fa props |
258 | argument. | | 285 | argument. |
259 | .Pp | | 286 | .Pp |
260 | The | | 287 | The |
261 | .Fa class | | 288 | .Fa class |
262 | argument can be any of: | | 289 | argument can be any of: |
263 | .Pp | | 290 | .Pp |
264 | .Bl -tag -width MODULE_CLASS_SECMODEL -offset indent -compact | | 291 | .Bl -tag -width MODULE_CLASS_SECMODEL -offset indent -compact |
265 | .It Dv MODULE_CLASS_ANY | | 292 | .It Dv MODULE_CLASS_ANY |
266 | .It Dv MODULE_CLASS_DRIVER | | 293 | .It Dv MODULE_CLASS_DRIVER |
267 | Device driver | | 294 | Device driver |
268 | .It Dv MODULE_CLASS_EXEC | | 295 | .It Dv MODULE_CLASS_EXEC |
269 | Executable image handler | | 296 | Executable image handler |
270 | .It Dv MODULE_CLASS_MISC | | 297 | .It Dv MODULE_CLASS_MISC |
271 | Miscellaneous module | | 298 | Miscellaneous module |
272 | .It Dv MODULE_CLASS_SECMODEL | | 299 | .It Dv MODULE_CLASS_SECMODEL |
273 | Security model (see | | 300 | Security model (see |
274 | .Xr secmodel 9 | | 301 | .Xr secmodel 9 |
275 | for more details) | | 302 | for more details) |
276 | .It Dv MODULE_CLASS_VFS | | 303 | .It Dv MODULE_CLASS_VFS |
277 | Virtual file system | | 304 | Virtual file system |
278 | .El | | 305 | .El |
279 | .Pp | | 306 | .Pp |
280 | If the class is not | | 307 | If the class is not |
281 | .Dv MODULE_CLASS_ANY , | | 308 | .Dv MODULE_CLASS_ANY , |
282 | the class of the module being loaded | | 309 | the class of the module being loaded |
283 | must match the requested | | 310 | must match the requested |
284 | .Fa class . | | 311 | .Fa class . |
285 | Except when verifying a module's class when it is being loaded, module | | 312 | Except when verifying a module's class when it is being loaded, module |
286 | classes other than | | 313 | classes other than |
287 | .Dv MODULE_CLASS_SECMODEL | | 314 | .Dv MODULE_CLASS_SECMODEL |
288 | are transparent to the module subsystem. | | 315 | are transparent to the module subsystem. |
289 | They are provided only for the benefit of the subsystem's clients. | | 316 | They are provided only for the benefit of the subsystem's clients. |
290 | Modules with class | | 317 | Modules with class |
291 | .Dv MODULE_CLASS_SECMODEL | | 318 | .Dv MODULE_CLASS_SECMODEL |
292 | are automatically registered with | | 319 | are automatically registered with |
293 | .Fn secmodel_register | | 320 | .Fn secmodel_register |
294 | after being successfully loaded, and automatically deregistered with | | 321 | after being successfully loaded, and automatically deregistered with |
295 | .Fn secmodel_deregister | | 322 | .Fn secmodel_deregister |
296 | when being unloaded. | | 323 | when being unloaded. |
297 | .Pp | | 324 | .Pp |
298 | The | | 325 | The |
299 | .Fn module_load | | 326 | .Fn module_load |
300 | routine is primarily intended as the implementation of the | | 327 | routine is primarily intended as the implementation of the |
301 | .Dv MODCTL_LOAD | | 328 | .Dv MODCTL_LOAD |
302 | option of the | | 329 | option of the |
303 | .Xr modctl 2 | | 330 | .Xr modctl 2 |
304 | system call. | | 331 | system call. |
305 | .It Fn module_autoload "name" "class" | | 332 | .It Fn module_autoload "name" "class" |
306 | Auto-load a module, making it available for automatic unloading. | | 333 | Auto-load a module, making it available for automatic unloading. |
307 | The | | 334 | The |
308 | .Fa name | | 335 | .Fa name |
309 | and | | 336 | and |
310 | .Fa class | | 337 | .Fa class |
311 | arguments are the same as for the | | 338 | arguments are the same as for the |
312 | .Fn module_load | | 339 | .Fn module_load |
313 | routine. | | 340 | routine. |
314 | .Pp | | 341 | .Pp |
315 | The module subsystem uses a kernel thread to attempt to automatically | | 342 | The module subsystem uses a kernel thread to attempt to automatically |
316 | unload modules a short time (currently, 10 seconds) after being loaded by | | 343 | unload modules a short time (currently, 10 seconds) after being loaded by |
317 | .Fn module_autoload . | | 344 | .Fn module_autoload . |
318 | Before the module is unloaded, its | | 345 | Before the module is unloaded, its |
319 | .Fn modcmd | | 346 | .Fn modcmd |
320 | is called with the | | 347 | is called with the |
321 | .Fa cmd | | 348 | .Fa cmd |
322 | argument specified as | | 349 | argument specified as |
323 | .Dv MODULE_CMD_AUTOUNLOAD . | | 350 | .Dv MODULE_CMD_AUTOUNLOAD . |
324 | A module can prevent itself from being unloaded by returning a non-zero | | 351 | A module can prevent itself from being unloaded by returning a non-zero |
325 | value. | | 352 | value. |
326 | .Pp | | 353 | .Pp |
327 | The | | 354 | The |
328 | .Fn module_autoload | | 355 | .Fn module_autoload |
329 | function is intended for use by kernel components to locate and load optional | | 356 | function is intended for use by kernel components to locate and load optional |
330 | system components. | | 357 | system components. |
331 | The function is also used to load modules that are required by other modules. | | 358 | The function is also used to load modules that are required by other modules. |
332 | .Pp | | 359 | .Pp |
333 | The directory from which the module is loaded will be searched for a file | | 360 | The directory from which the module is loaded will be searched for a file |
334 | with the same name as the module file, but with the suffix | | 361 | with the same name as the module file, but with the suffix |
335 | .Dq Pa .plist . | | 362 | .Dq Pa .plist . |
336 | If this file is found, the prop_dictionary it contains will be loaded and | | 363 | If this file is found, the prop_dictionary it contains will be loaded and |
337 | passed to the module's | | 364 | passed to the module's |
338 | .Fn modcmd | | 365 | .Fn modcmd |
339 | routine. | | 366 | routine. |
340 | If this prop_dictionary contains a | | 367 | If this prop_dictionary contains a |
341 | .Dq Pa noautoload | | 368 | .Dq Pa noautoload |
342 | property which is set to | | 369 | property which is set to |
343 | .Dq Pa true | | 370 | .Dq Pa true |
344 | then the system will refuse to load the module. | | 371 | then the system will refuse to load the module. |
345 | .It Fn module_unload "name" | | 372 | .It Fn module_unload "name" |
346 | Unload a module. | | 373 | Unload a module. |
347 | If the module's reference count is non-zero, the function returns | | 374 | If the module's reference count is non-zero, the function returns |
348 | .Er EBUSY . | | 375 | .Er EBUSY . |
349 | Otherwise, the module's | | 376 | Otherwise, the module's |
350 | .Fn modcmd | | 377 | .Fn modcmd |
351 | routine is called with a | | 378 | routine is called with a |
352 | .Fa cmd | | 379 | .Fa cmd |
353 | argument of | | 380 | argument of |
354 | .Dv MODULE_CMD_FINI . | | 381 | .Dv MODULE_CMD_FINI . |
355 | If the | | 382 | If the |
356 | .Fn modcmd | | 383 | .Fn modcmd |
357 | routine returns with an error, then the error is returned to the caller | | 384 | routine returns with an error, then the error is returned to the caller |
358 | otherwise the module is unloaded. | | 385 | otherwise the module is unloaded. |
359 | .Pp | | 386 | .Pp |
360 | The reference counts of all modules that were required by this module are | | 387 | The reference counts of all modules that were required by this module are |
361 | decremented, but the required modules are not unloaded by the call to | | 388 | decremented, but the required modules are not unloaded by the call to |
362 | .Fn module_unload . | | 389 | .Fn module_unload . |
363 | Instead, the required modules may be unloaded by subsequent calls to | | 390 | Instead, the required modules may be unloaded by subsequent calls to |
364 | .Fn module_unload . | | 391 | .Fn module_unload . |
365 | .Pp | | 392 | .Pp |
366 | Unloading a built-in module causes the module to be marked as disabled. | | 393 | Unloading a built-in module causes the module to be marked as disabled. |
367 | This prevents the module from being re-loaded, except by the | | 394 | This prevents the module from being re-loaded, except by the |
368 | .Fn module_load | | 395 | .Fn module_load |
369 | function with the | | 396 | function with the |
370 | .Fa flags | | 397 | .Fa flags |
371 | argument set to | | 398 | argument set to |
372 | .Dv MODULE_FORCE_LOAD . | | 399 | .Dv MODULE_FORCE_LOAD . |
373 | .Pp | | 400 | .Pp |
374 | The | | 401 | The |
375 | .Fn module_unload | | 402 | .Fn module_unload |
376 | function may be called by the | | 403 | function may be called by the |
377 | .Xr modctl 2 | | 404 | .Xr modctl 2 |
378 | system call, by the module subsystem's internal auto-unload thread, or by | | 405 | system call, by the module subsystem's internal auto-unload thread, or by |
379 | other kernel facilities. | | 406 | other kernel facilities. |
380 | Generally, other kernel facilities should not be calling this function. | | 407 | Generally, other kernel facilities should not be calling this function. |
381 | .It Fn module_init_class "class" | | 408 | .It Fn module_init_class "class" |
382 | Load and initialize all available modules of the specified | | 409 | Load and initialize all available modules of the specified |
383 | .Fa class . | | 410 | .Fa class . |
384 | Any built-in modules that have not been disabled, and any modules provided | | 411 | Any built-in modules that have not been disabled, and any modules provided |
385 | by the boot loader are loaded. | | 412 | by the boot loader are loaded. |
386 | .It Fn module_hold "name" | | 413 | .It Fn module_hold "name" |
387 | Increment the reference count of a module. | | 414 | Increment the reference count of a module. |
388 | A module cannot be unloaded if its reference count is non-zero. | | 415 | A module cannot be unloaded if its reference count is non-zero. |
389 | .It Fn module_rele "name" | | 416 | .It Fn module_rele "name" |
390 | Decrement the reference count of a module. | | 417 | Decrement the reference count of a module. |
391 | .It Fn module_find_section "name" "addr" "size" | | 418 | .It Fn module_find_section "name" "addr" "size" |
392 | Find the start address and size of linker section | | 419 | Find the start address and size of linker section |
393 | .Ar name | | 420 | .Ar name |
394 | within a module. | | 421 | within a module. |
395 | The miniroot module uses this routine to find the address and size of the | | 422 | The miniroot module uses this routine to find the address and size of the |
396 | embedded file system image. | | 423 | embedded file system image. |
397 | This routine can only examine the linker data for the module that is | | 424 | This routine can only examine the linker data for the module that is |
398 | currently being initialized; it cannot examine data for any other module. | | 425 | currently being initialized; it cannot examine data for any other module. |
399 | .It Fn module_init "void" | | 426 | .It Fn module_init "void" |
400 | Initialize the module subsystem. | | 427 | Initialize the module subsystem. |
401 | Creates and initializes various data structures, locates all built-in | | 428 | Creates and initializes various data structures, locates all built-in |
402 | modules, and establishes the sub-system's | | 429 | modules, and establishes the sub-system's |
403 | .Xr sysctl 8 | | 430 | .Xr sysctl 8 |
404 | tree. | | 431 | tree. |
405 | .Fn module_init | | 432 | .Fn module_init |
406 | is called early in system initialization to facilitate use of security model | | 433 | is called early in system initialization to facilitate use of security model |
407 | modules. | | 434 | modules. |
408 | .It Fn module_start_unload_thread "void" | | 435 | .It Fn module_start_unload_thread "void" |
409 | Create the thread that attempts to automatically unload modules that were | | 436 | Create the thread that attempts to automatically unload modules that were |
410 | loaded via the | | 437 | loaded via the |
411 | .Fn module_autoload | | 438 | .Fn module_autoload |
412 | routine. | | 439 | routine. |
413 | The function is called only once, | | 440 | The function is called only once, |
414 | after the scheduler and timer functions are initialized. | | 441 | after the scheduler and timer functions are initialized. |
415 | .It Fn module_builtin_require_force "void" | | 442 | .It Fn module_builtin_require_force "void" |
416 | Mark as "disabled" any built-in modules that have not been successfully | | 443 | Mark as "disabled" any built-in modules that have not been successfully |
417 | initialized. | | 444 | initialized. |
418 | Modules marked "disabled" can only be loaded if the | | 445 | Modules marked "disabled" can only be loaded if the |
419 | .Dv MODCTL_LOAD_FORCE | | 446 | .Dv MODCTL_LOAD_FORCE |
420 | is specified. | | 447 | is specified. |
421 | .Fn module_builtin_require_force | | 448 | .Fn module_builtin_require_force |
422 | is called near the end of system initialization, after the | | 449 | is called near the end of system initialization, after the |
423 | .Xr init 8 | | 450 | .Xr init 8 |
424 | process is created. | | 451 | process is created. |
425 | .It Fn module_load_vfs_init | | 452 | .It Fn module_load_vfs_init |
426 | The module subsystem is initialized early, long before any file systems | | 453 | The module subsystem is initialized early, long before any file systems |
427 | are available. | | 454 | are available. |
428 | After the root file system is mounted, | | 455 | After the root file system is mounted, |
429 | .Fn module_load_vfs_init | | 456 | .Fn module_load_vfs_init |
430 | is used to enable loading modules from the file system. | | 457 | is used to enable loading modules from the file system. |
431 | Until this routine is called, modules can only be loaded if they were | | 458 | Until this routine is called, modules can only be loaded if they were |
432 | built-in to the kernel image or provided by the boot loader. | | 459 | built-in to the kernel image or provided by the boot loader. |
433 | .El | | 460 | .El |
434 | .Sh PROGRAMMING CONSIDERATIONS | | 461 | .Sh PROGRAMMING CONSIDERATIONS |
435 | The module subsystem is designed to be called recursively, but only within | | 462 | The module subsystem is designed to be called recursively, but only within |
436 | a single LWP. | | 463 | a single LWP. |
437 | This permits one module's | | 464 | This permits one module's |
438 | .Fn modcmd | | 465 | .Fn modcmd |
439 | routine to load or unload other modules. | | 466 | routine to load or unload other modules. |
440 | .Pp | | 467 | .Pp |
441 | Additional considerations: | | 468 | Additional considerations: |
442 | .Bl -bullet -offset indent | | 469 | .Bl -bullet -offset indent |
443 | .It | | 470 | .It |
444 | A module is not permitted to load or unload itself. | | 471 | A module is not permitted to load or unload itself. |
445 | Attempts to load or unload a module from within its own | | 472 | Attempts to load or unload a module from within its own |
446 | .Fn modcmd | | 473 | .Fn modcmd |
447 | routine will fail with | | 474 | routine will fail with |
448 | .Er EEXIST | | 475 | .Er EEXIST |
449 | or | | 476 | or |
450 | .Er EBUSY , | | 477 | .Er EBUSY , |
451 | respectively. | | 478 | respectively. |
452 | .It | | 479 | .It |
453 | Although a module can be loaded by using either | | 480 | Although a module can be loaded by using either |
454 | .Fn module_load | | 481 | .Fn module_load |
455 | or | | 482 | or |
456 | .Fn module_autoload , | | 483 | .Fn module_autoload , |
457 | it is not possible for the module's | | 484 | it is not possible for the module's |
458 | .Fn modcmd | | 485 | .Fn modcmd |
459 | routine to distinguish between the two methods. | | 486 | routine to distinguish between the two methods. |
460 | Any module which needs to ensure that it does not get auto-unloaded must | | 487 | Any module which needs to ensure that it does not get auto-unloaded must |
461 | either handle the | | 488 | either handle the |
462 | .Dv MODULE_CMD_AUTOUNLOAD | | 489 | .Dv MODULE_CMD_AUTOUNLOAD |
463 | command in its | | 490 | command in its |
464 | .Fn modcmd | | 491 | .Fn modcmd |
465 | routine, or use | | 492 | routine, or use |
466 | .Fn module_hold | | 493 | .Fn module_hold |
467 | to increment its reference count. | | 494 | to increment its reference count. |
468 | Note however that modules loaded manually with | | 495 | Note however that modules loaded manually with |
469 | .Xr modload 8 | | 496 | .Xr modload 8 |
470 | are never auto-unloaded. | | 497 | are never auto-unloaded. |
471 | .El | | 498 | .El |
472 | .Sh EXAMPLES | | 499 | .Sh EXAMPLES |
473 | A set of example modules is available in the | | 500 | A set of example modules is available in the |
474 | .Pa src/sys/modules/examples | | 501 | .Pa src/sys/modules/examples |
475 | directory hierarchy. | | 502 | directory hierarchy. |
476 | .Sh CODE REFERENCES | | 503 | .Sh CODE REFERENCES |
477 | The core of the kernel module implementation is in | | 504 | The core of the kernel module implementation is in |
478 | .Pa sys/kern/kern_module.c | | 505 | .Pa sys/kern/kern_module.c |
479 | and | | 506 | and |
480 | .Pa sys/kern/kern_module_vfs.c . | | 507 | .Pa sys/kern/kern_module_vfs.c . |
481 | .Pp | | 508 | .Pp |
482 | The routines for linking the module are in | | 509 | The routines for linking the module are in |
483 | .Pa sys/kern/subr_kobj.c . | | 510 | .Pa sys/kern/subr_kobj.c . |
484 | .Pp | | 511 | .Pp |
485 | The routines for reading a module from the file system are in | | 512 | The routines for reading a module from the file system are in |
486 | .Pa sys/kern/subr_kobj_vfs.c . | | 513 | .Pa sys/kern/subr_kobj_vfs.c . |
487 | .Pp | | 514 | .Pp |
488 | The header file | | 515 | The header file |
489 | .In sys/sys/module.h | | 516 | .In sys/sys/module.h |
490 | describes the public interface. | | 517 | describes the public interface. |
491 | .Pp | | 518 | .Pp |
492 | In addition, each architecture is expected to provide | | 519 | In addition, each architecture is expected to provide |
493 | .Fn kobj_machdep , | | 520 | .Fn kobj_machdep , |
494 | .Fn kobj_reloc , | | 521 | .Fn kobj_reloc , |
495 | and | | 522 | and |
496 | .Fn module_init_md . | | 523 | .Fn module_init_md . |
497 | .Fn kobj_machdep | | 524 | .Fn kobj_machdep |
498 | is for any machine dependent actions, such as flushing caches, that are | | 525 | is for any machine dependent actions, such as flushing caches, that are |
499 | needed when a module is loaded or unloaded. | | 526 | needed when a module is loaded or unloaded. |
500 | .Fn kobj_reloc | | 527 | .Fn kobj_reloc |
501 | deals with resolution of relocatable symbols. | | 528 | deals with resolution of relocatable symbols. |
502 | .Fn module_init_md | | 529 | .Fn module_init_md |
503 | is for finding modules passed in by the boot loader. | | 530 | is for finding modules passed in by the boot loader. |
504 | .Sh SEE ALSO | | 531 | .Sh SEE ALSO |
505 | .Xr modctl 2 , | | 532 | .Xr modctl 2 , |
506 | .Xr module 7 , | | 533 | .Xr module 7 , |
507 | .Xr intro 9lua | | 534 | .Xr intro 9lua |
508 | .Sh HISTORY | | 535 | .Sh HISTORY |
509 | The kernel module subsystem first appeared in | | 536 | The kernel module subsystem first appeared in |
510 | .Nx 5.0 . | | 537 | .Nx 5.0 . |
511 | It replaces the | | 538 | It replaces the |
512 | .Dq LKM | | 539 | .Dq LKM |
513 | subsystem from earlier releases. | | 540 | subsystem from earlier releases. |
514 | .Sh AUTHORS | | 541 | .Sh AUTHORS |
515 | .An -nosplit | | 542 | .An -nosplit |
516 | The | | 543 | The |
517 | .Nm | | 544 | .Nm |
518 | system was written by | | 545 | system was written by |
519 | .An Andrew Doran Aq Mt ad@NetBSD.org . | | 546 | .An Andrew Doran Aq Mt ad@NetBSD.org . |
520 | This manual page was written by | | 547 | This manual page was written by |
521 | .An Paul Goyette Aq Mt pgoyette@NetBSD.org . | | 548 | .An Paul Goyette Aq Mt pgoyette@NetBSD.org . |