Thu Jul 22 01:38:45 2021 UTC ()
module_hold() returns void, not int

XXX module_hold() and module_rele() should probably not be exposed, since
XXX they really aren't MP-safe.


(pgoyette)
diff -r1.52 -r1.53 src/share/man/man9/module.9

cvs diff -r1.52 -r1.53 src/share/man/man9/module.9 (switch to unified diff)

--- src/share/man/man9/module.9 2019/04/07 22:32:10 1.52
+++ src/share/man/man9/module.9 2021/07/22 01:38:45 1.53
@@ -1,612 +1,612 @@ @@ -1,612 +1,612 @@
1.\" $NetBSD: module.9,v 1.52 2019/04/07 22:32:10 pgoyette Exp $ 1.\" $NetBSD: module.9,v 1.53 2021/07/22 01:38:45 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 April 8, 2019 30.Dd July 21, 2021
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.Nm module_kernel , 42.Nm module_kernel ,
43.Nm module_name , 43.Nm module_name ,
44.Nm module_source , 44.Nm module_source ,
45.Nm module_register_callbacks , 45.Nm module_register_callbacks ,
46.Nm module_unregister_callbacks , 46.Nm module_unregister_callbacks ,
47.Nm module_specific_key_create , 47.Nm module_specific_key_create ,
48.Nm module_specific_key_delete , 48.Nm module_specific_key_delete ,
49.Nm module_getspecific , 49.Nm module_getspecific ,
50.Nm module_setspecific 50.Nm module_setspecific
51.Nd kernel module loader 51.Nd kernel module loader
52.Sh SYNOPSIS 52.Sh SYNOPSIS
53.In sys/module.h 53.In sys/module.h
54.Fn MODULE "class" "name" "required" 54.Fn MODULE "class" "name" "required"
55.Ft int 55.Ft int
56.Fn module_load "const char *name" "int flags" "prop_dictionary_t props" \ 56.Fn module_load "const char *name" "int flags" "prop_dictionary_t props" \
57"modclass_t class" 57"modclass_t class"
58.Ft int 58.Ft int
59.Fn module_autoload "const char *name" "modclass_t class" 59.Fn module_autoload "const char *name" "modclass_t class"
60.Ft int 60.Ft int
61.Fn module_unload "const char *name" 61.Fn module_unload "const char *name"
62.Ft void 62.Ft void
63.Fn module_init_class "modclass_t class" 63.Fn module_init_class "modclass_t class"
64.Ft int 64.Ft void
65.Fn module_hold "module_t *module" 65.Fn module_hold "module_t *module"
66.Ft void 66.Ft void
67.Fn module_rele "module_t *module" 67.Fn module_rele "module_t *module"
68.Ft int 68.Ft int
69.Fn module_find_section "const char *" "void **" "size_t *" 69.Fn module_find_section "const char *" "void **" "size_t *"
70.Ft "module_t *" 70.Ft "module_t *"
71.Fn module_kernel "void" 71.Fn module_kernel "void"
72.Ft "const char *" 72.Ft "const char *"
73.Fn module_name "struct module *module" 73.Fn module_name "struct module *module"
74.Ft modsrc_t 74.Ft modsrc_t
75.Fn module_source "struct module *module" 75.Fn module_source "struct module *module"
76.Ft void 76.Ft void
77.Fn module_init "void" 77.Fn module_init "void"
78.Ft void 78.Ft void
79.Fn module_start_unload_thread "void" 79.Fn module_start_unload_thread "void"
80.Ft void 80.Ft void
81.Fn module_builtin_require_force "void" 81.Fn module_builtin_require_force "void"
82.Ft void 82.Ft void
83.Fn module_load_vfs_init "void" 83.Fn module_load_vfs_init "void"
84.Ft "void *" 84.Ft "void *"
85.Fn module_register_callbacks "void (*)(struct module *)" \ 85.Fn module_register_callbacks "void (*)(struct module *)" \
86"void (*unload)(struct module *)" 86"void (*unload)(struct module *)"
87.Ft void 87.Ft void
88.Fn module_unregister_callbacks "void *" 88.Fn module_unregister_callbacks "void *"
89.Ft specificdata_key_t 89.Ft specificdata_key_t
90.Fn module_specific_key_create "specificdata_key_t *keyp" \ 90.Fn module_specific_key_create "specificdata_key_t *keyp" \
91"specificdata_dtor_t dtor" 91"specificdata_dtor_t dtor"
92.Ft void 92.Ft void
93.Fn module_specific_key_delete "specificdata_key_t key" 93.Fn module_specific_key_delete "specificdata_key_t key"
94.Ft "void *" 94.Ft "void *"
95.Fn module_getspecific "module_t *mod" "specificdata_key_t key" 95.Fn module_getspecific "module_t *mod" "specificdata_key_t key"
96.Ft "void *" 96.Ft "void *"
97.Fn module_setspecific "module_t *mod" "specificdata_key_t key" "void *data" 97.Fn module_setspecific "module_t *mod" "specificdata_key_t key" "void *data"
98.Sh DESCRIPTION 98.Sh DESCRIPTION
99Modules are sections of code that can be independently linked and selectively 99Modules are sections of code that can be independently linked and selectively
100loaded into or unloaded from a running kernel. 100loaded into or unloaded from a running kernel.
101This provides a mechanism to update the module without having to relink 101This provides a mechanism to update the module without having to relink
102the kernel and reboot. 102the kernel and reboot.
103Modules can be loaded from within the kernel image, provided by the boot 103Modules can be loaded from within the kernel image, provided by the boot
104loader, or loaded from the file system. 104loader, or loaded from the file system.
105.Pp 105.Pp
106The 106The
107.Nm 107.Nm
108subsystem includes two data types: 108subsystem includes two data types:
109.Bl -enum -offset indent 109.Bl -enum -offset indent
110.It 110.It
111The 111The
112.Vt module_t 112.Vt module_t
113type provides storage to describe a module. 113type provides storage to describe a module.
114.It 114.It
115The 115The
116.Vt modinfo_t 116.Vt modinfo_t
117type resides within 117type resides within
118.Vt module_t 118.Vt module_t
119and contains module header info. 119and contains module header info.
120.El 120.El
121.Pp 121.Pp
122The module subsystem is protected by the global 122The module subsystem is protected by the global
123.Va kernconfig_mutex . 123.Va kernconfig_mutex .
124.Sh FUNCTIONS 124.Sh FUNCTIONS
125.Bl -tag -width abcd 125.Bl -tag -width abcd
126.It Fn MODULE "class" "name" "required" 126.It Fn MODULE "class" "name" "required"
127The 127The
128.Fn MODULE 128.Fn MODULE
129macro creates and initializes a 129macro creates and initializes a
130.Vt modinfo_t 130.Vt modinfo_t
131structure. 131structure.
132The 132The
133.Fa class 133.Fa class
134argument identifies the class of module, and must be one of the following 134argument identifies the class of module, and must be one of the following
135(note that 135(note that
136.Dv MODULE_CLASS_ANY 136.Dv MODULE_CLASS_ANY
137should not be used here): 137should not be used here):
138.Bl -tag -width MODULE_CLASS_SECMODEL -offset indent 138.Bl -tag -width MODULE_CLASS_SECMODEL -offset indent
139.It Dv MODULE_CLASS_VFS 139.It Dv MODULE_CLASS_VFS
140The module provide a virtual file system - see 140The module provide a virtual file system - see
141.Xr vfs 9 141.Xr vfs 9
142.It Dv MODULE_CLASS_DRIVER 142.It Dv MODULE_CLASS_DRIVER
143The module is a device driver - see 143The module is a device driver - see
144.Xr driver 9 144.Xr driver 9
145.It Dv MODULE_CLASS_EXEC 145.It Dv MODULE_CLASS_EXEC
146The module provides an alternate execution environment - see the various 146The module provides an alternate execution environment - see the various
147.Dv COMPAT_xxx 147.Dv COMPAT_xxx
148options in 148options in
149.Xr options 4 149.Xr options 4
150.It Dv MODULE_CLASS_SECMODEL 150.It Dv MODULE_CLASS_SECMODEL
151The module provides a security model - see 151The module provides a security model - see
152.Xr secmodel 9 152.Xr secmodel 9
153.It Dv MODULE_CLASS_BUFQ 153.It Dv MODULE_CLASS_BUFQ
154The module provides a buffer queue strategy - see 154The module provides a buffer queue strategy - see
155.Xr bufq 9 155.Xr bufq 9
156.It Dv MODULE_CLASS_MISC 156.It Dv MODULE_CLASS_MISC
157The module provides miscellaneous kernel services 157The module provides miscellaneous kernel services
158.El 158.El
159.Pp 159.Pp
160The 160The
161.Fa name 161.Fa name
162argument provides the name of the module. 162argument provides the name of the module.
163Loaded modules, including those that are built-in to the kernel, must all 163Loaded modules, including those that are built-in to the kernel, must all
164have unique names. 164have unique names.
165.Pp 165.Pp
166The 166The
167.Fa required 167.Fa required
168argument is a quoted string containing a comma-separated list of module 168argument is a quoted string containing a comma-separated list of module
169names that are required by this module. 169names that are required by this module.
170The list must not contain any white-space. 170The list must not contain any white-space.
171When a module is loaded, all of its required modules are auto-loaded and 171When a module is loaded, all of its required modules are auto-loaded and
172initialized before the module itself is loaded. 172initialized before the module itself is loaded.
173Loading of required modules is a recursive operation. 173Loading of required modules is a recursive operation.
174.Pp 174.Pp
175If there are no required modules, this argument should be specified as 175If there are no required modules, this argument should be specified as
176.Dv NULL . 176.Dv NULL .
177.Pp 177.Pp
178In addition to the explicit arguments, the 178In addition to the explicit arguments, the
179.Fn MODULE 179.Fn MODULE
180macro creates a reference to the module's 180macro creates a reference to the module's
181.Fn modcmd 181.Fn modcmd
182function. 182function.
183This function is defined as: 183This function is defined as:
184.Bl -tag -width modcmd -offset indent 184.Bl -tag -width modcmd -offset indent
185.It Ft modcmd_t 185.It Ft modcmd_t
186.Fn xxx_modcmd "modcmd_t cmd" "void *data" 186.Fn xxx_modcmd "modcmd_t cmd" "void *data"
187.El 187.El
188.Pp 188.Pp
189(where xxx is the name of the module, from the 189(where xxx is the name of the module, from the
190.Dv MODULE 190.Dv MODULE
191macro). 191macro).
192.Pp 192.Pp
193The 193The
194.Fa cmd 194.Fa cmd
195argument requests one of the following operations: 195argument requests one of the following operations:
196.Bl -tag -width MODULE_CMD_AUTOUNLOAD -offset indent 196.Bl -tag -width MODULE_CMD_AUTOUNLOAD -offset indent
197.It Dv MODULE_CMD_INIT 197.It Dv MODULE_CMD_INIT
198Perform module-specific initialization when the module is loaded. 198Perform module-specific initialization when the module is loaded.
199.It Dv MODULE_CMD_FINI 199.It Dv MODULE_CMD_FINI
200Perform module-specific clean-up before the module is unloaded. 200Perform module-specific clean-up before the module is unloaded.
201.It Dv MODULE_CMD_AUTOUNLOAD 201.It Dv MODULE_CMD_AUTOUNLOAD
202Notify the module that it is about to be unloaded. 202Notify the module that it is about to be unloaded.
203.It Dv MODULE_CMD_STAT 203.It Dv MODULE_CMD_STAT
204Request the module to provide status information (not currently implemented). 204Request the module to provide status information (not currently implemented).
205.El 205.El
206.Pp 206.Pp
207All modules' 207All modules'
208.Fn modcmd 208.Fn modcmd
209functions must implement the 209functions must implement the
210.Dv MODULE_CMD_INIT 210.Dv MODULE_CMD_INIT
211and 211and
212.Dv MODULE_CMD_FINI 212.Dv MODULE_CMD_FINI
213commands. 213commands.
214The other commands are optional, 214The other commands are optional,
215and should return 215and should return
216.Er ENOTTY 216.Er ENOTTY
217if not implemented. 217if not implemented.
218.Pp 218.Pp
219For the 219For the
220.Dv MODULE_CMD_INIT 220.Dv MODULE_CMD_INIT
221command, the 221command, the
222.Fa data 222.Fa data
223argument is used to pass a pointer to the module's 223argument is used to pass a pointer to the module's
224.Xr prop_dictionary 3 . 224.Xr prop_dictionary 3 .
225For the 225For the
226.Dv MODULE_CMD_STAT 226.Dv MODULE_CMD_STAT
227command, the 227command, the
228.Fa data 228.Fa data
229argument points to a buffer where the status information should be placed. 229argument points to a buffer where the status information should be placed.
230.Pp 230.Pp
231The __link_set mechanism is used to enable the 231The __link_set mechanism is used to enable the
232.Nm 232.Nm
233subsystem to locate the 233subsystem to locate the
234.Vt modinfo_t 234.Vt modinfo_t
235structure. 235structure.
236.It Fn module_load "name" "flags" "props" "class" 236.It Fn module_load "name" "flags" "props" "class"
237Load a module, link it into the running kernel, and call the module's 237Load a module, link it into the running kernel, and call the module's
238.Fn modcmd 238.Fn modcmd
239routine with a 239routine with a
240.Fa cmd 240.Fa cmd
241argument of 241argument of
242.Dv MODULE_CMD_INIT . 242.Dv MODULE_CMD_INIT .
243If the specified module requires other modules, they are loaded first; if 243If the specified module requires other modules, they are loaded first; if
244any required module cannot be loaded or if any of their 244any required module cannot be loaded or if any of their
245.Fn modcmd 245.Fn modcmd
246control routines returns a non-zero status, loading of this module and 246control routines returns a non-zero status, loading of this module and
247the specific required module will fail. 247the specific required module will fail.
248The required modules are marked for automatic unloading. 248The required modules are marked for automatic unloading.
249Thus, if the loading of the module failed, the required modules will 249Thus, if the loading of the module failed, the required modules will
250be automatically unloaded after a short delay. 250be automatically unloaded after a short delay.
251.Pp 251.Pp
252The loader will look first for a built-in module with the specified 252The loader will look first for a built-in module with the specified
253.Fa name 253.Fa name
254that has not been disabled (see 254that has not been disabled (see
255.Fn module_unload 255.Fn module_unload
256below). 256below).
257If a built-in module with that 257If a built-in module with that
258.Fa name 258.Fa name
259is not found, the list of modules prepared by the boot loader is searched. 259is not found, the list of modules prepared by the boot loader is searched.
260If the named module is still not found, an attempt is made to locate the 260If the named module is still not found, an attempt is made to locate the
261module within the file system, provided it has been mounted by the 261module within the file system, provided it has been mounted by the
262initialization code. 262initialization code.
263.Pp 263.Pp
264The 264The
265.Fa flags 265.Fa flags
266argument can include: 266argument can include:
267.Bl -tag -width MODCTL_LOAD_FORCE -offset indent 267.Bl -tag -width MODCTL_LOAD_FORCE -offset indent
268.It Dv MODCTL_NO_PROP 268.It Dv MODCTL_NO_PROP
269When loading a module from the file system, do not attempt to locate a 269When loading a module from the file system, do not attempt to locate a
270corresponding prop_dictionary file. 270corresponding prop_dictionary file.
271.It Dv MODCTL_LOAD_FORCE 271.It Dv MODCTL_LOAD_FORCE
272Force loading of disabled built-in modules and modules built for a 272Force loading of disabled built-in modules and modules built for a
273different version of the operating system. 273different version of the operating system.
274.El 274.El
275.Pp 275.Pp
276The 276The
277.Fa props 277.Fa props
278argument points to an externalized property list which is passed to the 278argument points to an externalized property list which is passed to the
279module's 279module's
280.Fn modcmd 280.Fn modcmd
281routine. 281routine.
282If a module is being loaded from the file system, and the 282If a module is being loaded from the file system, and the
283.Dv MODCTL_NO_PROP 283.Dv MODCTL_NO_PROP
284flag is not set, the system searches for a file with the same name as the 284flag is not set, the system searches for a file with the same name as the
285module file, but with the suffix 285module file, but with the suffix
286.Dq Pa .plist . 286.Dq Pa .plist .
287If this file is found, the prop_dictionary it contains is loaded and 287If this file is found, the prop_dictionary it contains is loaded and
288merged with the prop_dictionary from the 288merged with the prop_dictionary from the
289.Fa props 289.Fa props
290argument. 290argument.
291.Pp 291.Pp
292The 292The
293.Fa class 293.Fa class
294argument can be any of: 294argument can be any of:
295.Pp 295.Pp
296.Bl -tag -width MODULE_CLASS_SECMODEL -offset indent -compact 296.Bl -tag -width MODULE_CLASS_SECMODEL -offset indent -compact
297.It Dv MODULE_CLASS_ANY 297.It Dv MODULE_CLASS_ANY
298.It Dv MODULE_CLASS_DRIVER 298.It Dv MODULE_CLASS_DRIVER
299Device driver 299Device driver
300.It Dv MODULE_CLASS_EXEC 300.It Dv MODULE_CLASS_EXEC
301Executable image handler 301Executable image handler
302.It Dv MODULE_CLASS_MISC 302.It Dv MODULE_CLASS_MISC
303Miscellaneous module 303Miscellaneous module
304.It Dv MODULE_CLASS_SECMODEL 304.It Dv MODULE_CLASS_SECMODEL
305Security model (see 305Security model (see
306.Xr secmodel 9 306.Xr secmodel 9
307for more details) 307for more details)
308.It Dv MODULE_CLASS_VFS 308.It Dv MODULE_CLASS_VFS
309Virtual file system 309Virtual file system
310.El 310.El
311.Pp 311.Pp
312If the class is not 312If the class is not
313.Dv MODULE_CLASS_ANY , 313.Dv MODULE_CLASS_ANY ,
314the class of the module being loaded 314the class of the module being loaded
315must match the requested 315must match the requested
316.Fa class . 316.Fa class .
317Except when verifying a module's class when it is being loaded, module 317Except when verifying a module's class when it is being loaded, module
318classes other than 318classes other than
319.Dv MODULE_CLASS_SECMODEL 319.Dv MODULE_CLASS_SECMODEL
320are transparent to the module subsystem. 320are transparent to the module subsystem.
321They are provided only for the benefit of the subsystem's clients. 321They are provided only for the benefit of the subsystem's clients.
322Modules with class 322Modules with class
323.Dv MODULE_CLASS_SECMODEL 323.Dv MODULE_CLASS_SECMODEL
324are automatically registered with 324are automatically registered with
325.Fn secmodel_register 325.Fn secmodel_register
326after being successfully loaded, and automatically deregistered with 326after being successfully loaded, and automatically deregistered with
327.Fn secmodel_deregister 327.Fn secmodel_deregister
328when being unloaded. 328when being unloaded.
329.Pp 329.Pp
330The 330The
331.Fn module_load 331.Fn module_load
332routine is primarily intended as the implementation of the 332routine is primarily intended as the implementation of the
333.Dv MODCTL_LOAD 333.Dv MODCTL_LOAD
334option of the 334option of the
335.Xr modctl 2 335.Xr modctl 2
336system call. 336system call.
337.It Fn module_autoload "name" "class" 337.It Fn module_autoload "name" "class"
338Auto-load a module, making it available for automatic unloading. 338Auto-load a module, making it available for automatic unloading.
339The 339The
340.Fa name 340.Fa name
341and 341and
342.Fa class 342.Fa class
343arguments are the same as for the 343arguments are the same as for the
344.Fn module_load 344.Fn module_load
345routine. 345routine.
346.Pp 346.Pp
347The module subsystem uses a kernel thread to attempt to automatically 347The module subsystem uses a kernel thread to attempt to automatically
348unload modules a short time (currently, 10 seconds) after being loaded by 348unload modules a short time (currently, 10 seconds) after being loaded by
349.Fn module_autoload . 349.Fn module_autoload .
350Before the module is unloaded, its 350Before the module is unloaded, its
351.Fn modcmd 351.Fn modcmd
352is called with the 352is called with the
353.Fa cmd 353.Fa cmd
354argument specified as 354argument specified as
355.Dv MODULE_CMD_AUTOUNLOAD . 355.Dv MODULE_CMD_AUTOUNLOAD .
356A module can prevent itself from being unloaded by returning a non-zero 356A module can prevent itself from being unloaded by returning a non-zero
357value. 357value.
358.Pp 358.Pp
359The 359The
360.Fn module_autoload 360.Fn module_autoload
361function is intended for use by kernel components to locate and load optional 361function is intended for use by kernel components to locate and load optional
362system components. 362system components.
363The function is also used to load modules that are required by other modules. 363The function is also used to load modules that are required by other modules.
364.Pp 364.Pp
365The directory from which the module is loaded will be searched for a file 365The directory from which the module is loaded will be searched for a file
366with the same name as the module file, but with the suffix 366with the same name as the module file, but with the suffix
367.Dq Pa .plist . 367.Dq Pa .plist .
368If this file is found, the prop_dictionary it contains will be loaded and 368If this file is found, the prop_dictionary it contains will be loaded and
369passed to the module's 369passed to the module's
370.Fn modcmd 370.Fn modcmd
371routine. 371routine.
372If this prop_dictionary contains a 372If this prop_dictionary contains a
373.Dq Pa noautoload 373.Dq Pa noautoload
374property which is set to 374property which is set to
375.Dq Pa true 375.Dq Pa true
376then the system will refuse to load the module. 376then the system will refuse to load the module.
377.It Fn module_unload "name" 377.It Fn module_unload "name"
378Unload a module. 378Unload a module.
379If the module's reference count is non-zero, the function returns 379If the module's reference count is non-zero, the function returns
380.Er EBUSY . 380.Er EBUSY .
381Otherwise, the module's 381Otherwise, the module's
382.Fn modcmd 382.Fn modcmd
383routine is called with a 383routine is called with a
384.Fa cmd 384.Fa cmd
385argument of 385argument of
386.Dv MODULE_CMD_FINI . 386.Dv MODULE_CMD_FINI .
387If the 387If the
388.Fn modcmd 388.Fn modcmd
389routine returns with an error, then the error is returned to the caller 389routine returns with an error, then the error is returned to the caller
390otherwise the module is unloaded. 390otherwise the module is unloaded.
391.Pp 391.Pp
392The reference counts of all modules that were required by this module are 392The reference counts of all modules that were required by this module are
393decremented, but the required modules are not unloaded by the call to 393decremented, but the required modules are not unloaded by the call to
394.Fn module_unload . 394.Fn module_unload .
395Instead, the required modules may be unloaded by subsequent calls to 395Instead, the required modules may be unloaded by subsequent calls to
396.Fn module_unload . 396.Fn module_unload .
397.Pp 397.Pp
398Unloading a built-in module causes the module to be marked as disabled. 398Unloading a built-in module causes the module to be marked as disabled.
399This prevents the module from being re-loaded, except by the 399This prevents the module from being re-loaded, except by the
400.Fn module_load 400.Fn module_load
401function with the 401function with the
402.Fa flags 402.Fa flags
403argument set to 403argument set to
404.Dv MODULE_FORCE_LOAD . 404.Dv MODULE_FORCE_LOAD .
405.Pp 405.Pp
406The 406The
407.Fn module_unload 407.Fn module_unload
408function may be called by the 408function may be called by the
409.Xr modctl 2 409.Xr modctl 2
410system call, by the module subsystem's internal auto-unload thread, or by 410system call, by the module subsystem's internal auto-unload thread, or by
411other kernel facilities. 411other kernel facilities.
412Generally, other kernel facilities should not be calling this function. 412Generally, other kernel facilities should not be calling this function.
413.It Fn module_init_class "class" 413.It Fn module_init_class "class"
414Load and initialize all available modules of the specified 414Load and initialize all available modules of the specified
415.Fa class . 415.Fa class .
416Any built-in modules that have not been disabled, and any modules provided 416Any built-in modules that have not been disabled, and any modules provided
417by the boot loader are loaded. 417by the boot loader are loaded.
418.It Fn module_hold "module" 418.It Fn module_hold "module"
419Increment the reference count of a module. 419Increment the reference count of a module.
420A module cannot be unloaded if its reference count is non-zero. 420A module cannot be unloaded if its reference count is non-zero.
421.It Fn module_rele "module" 421.It Fn module_rele "module"
422Decrement the reference count of a module. 422Decrement the reference count of a module.
423.It Fn module_find_section "name" "addr" "size" 423.It Fn module_find_section "name" "addr" "size"
424Find the start address and size of linker section 424Find the start address and size of linker section
425.Ar name 425.Ar name
426within a module. 426within a module.
427The miniroot module uses this routine to find the address and size of the 427The miniroot module uses this routine to find the address and size of the
428embedded file system image. 428embedded file system image.
429This routine can only examine the linker data for the module that is 429This routine can only examine the linker data for the module that is
430currently being initialized; it cannot examine data for any other module. 430currently being initialized; it cannot examine data for any other module.
431.It Fn module_kernel "void" 431.It Fn module_kernel "void"
432Returns a pointer to a 432Returns a pointer to a
433.Ft module_t 433.Ft module_t
434structure describing the kernel module. 434structure describing the kernel module.
435.It Fn module_name module 435.It Fn module_name module
436Returns a pointer to the module's name. 436Returns a pointer to the module's name.
437.It Fn module_source module 437.It Fn module_source module
438Returns the source of the module, one of 438Returns the source of the module, one of
439.Dv MODULE_SOURCE_KERNEL , 439.Dv MODULE_SOURCE_KERNEL ,
440.Dv MODULE_SOURCE_BOOT , 440.Dv MODULE_SOURCE_BOOT ,
441or 441or
442.Dv MODULE_SOURCE_FILESYS . 442.Dv MODULE_SOURCE_FILESYS .
443.It Fn module_init "void" 443.It Fn module_init "void"
444Initialize the module subsystem. 444Initialize the module subsystem.
445Creates and initializes various data structures, locates all built-in 445Creates and initializes various data structures, locates all built-in
446modules, and establishes the sub-system's 446modules, and establishes the sub-system's
447.Xr sysctl 8 447.Xr sysctl 8
448tree. 448tree.
449.Fn module_init 449.Fn module_init
450is called early in system initialization to facilitate use of security model 450is called early in system initialization to facilitate use of security model
451modules. 451modules.
452.It Fn module_start_unload_thread "void" 452.It Fn module_start_unload_thread "void"
453Create the thread that attempts to automatically unload modules that were 453Create the thread that attempts to automatically unload modules that were
454loaded via the 454loaded via the
455.Fn module_autoload 455.Fn module_autoload
456routine. 456routine.
457The function is called only once, 457The function is called only once,
458after the scheduler and timer functions are initialized. 458after the scheduler and timer functions are initialized.
459.It Fn module_builtin_require_force "void" 459.It Fn module_builtin_require_force "void"
460Mark as "disabled" any built-in modules that have not been successfully 460Mark as "disabled" any built-in modules that have not been successfully
461initialized. 461initialized.
462Modules marked "disabled" can only be loaded if the 462Modules marked "disabled" can only be loaded if the
463.Dv MODCTL_LOAD_FORCE 463.Dv MODCTL_LOAD_FORCE
464is specified. 464is specified.
465.Fn module_builtin_require_force 465.Fn module_builtin_require_force
466is called near the end of system initialization, after the 466is called near the end of system initialization, after the
467.Xr init 8 467.Xr init 8
468process is created. 468process is created.
469.It Fn module_load_vfs_init "void" 469.It Fn module_load_vfs_init "void"
470The module subsystem is initialized early, long before any file systems 470The module subsystem is initialized early, long before any file systems
471are available. 471are available.
472After the root file system is mounted, 472After the root file system is mounted,
473.Fn module_load_vfs_init "void" 473.Fn module_load_vfs_init "void"
474is used to enable loading modules from the file system. 474is used to enable loading modules from the file system.
475Until this routine is called, modules can only be loaded if they were 475Until this routine is called, modules can only be loaded if they were
476built-in to the kernel image or provided by the boot loader. 476built-in to the kernel image or provided by the boot loader.
477.It Fn module_register_callbacks "void (*load)(struct module *)" \ 477.It Fn module_register_callbacks "void (*load)(struct module *)" \
478"void (*unload)(struct module *)" 478"void (*unload)(struct module *)"
479Register a new set of callbacks. 479Register a new set of callbacks.
480The 480The
481.Fa load 481.Fa load
482callback is invoked after any module (including this module) is 482callback is invoked after any module (including this module) is
483successfully loaded; the 483successfully loaded; the
484.Fa unload 484.Fa unload
485callback is invoked before any module is unloaded. 485callback is invoked before any module is unloaded.
486Each load or unload event can result in multiple invocations of the 486Each load or unload event can result in multiple invocations of the
487callback routines. 487callback routines.
488An opaque cookie is returned which can be passed to 488An opaque cookie is returned which can be passed to
489.Fn module_unregister_callbacks . 489.Fn module_unregister_callbacks .
490.It Fn module_unregister_callbacks "void *opaque" 490.It Fn module_unregister_callbacks "void *opaque"
491Unregister a set of callback routines previously registered with 491Unregister a set of callback routines previously registered with
492.Fn module_register_callbacks . 492.Fn module_register_callbacks .
493The 493The
494.Fa opaque 494.Fa opaque
495argument should be the return value from the previous 495argument should be the return value from the previous
496.Fn module_register_callbacks 496.Fn module_register_callbacks
497call. 497call.
498.It Fn module_specific_key_create "specificdata_key_t *keyp" \ 498.It Fn module_specific_key_create "specificdata_key_t *keyp" \
499"specificdata_dtor_t dtor" 499"specificdata_dtor_t dtor"
500Creates a new specificdata_key for use within the 500Creates a new specificdata_key for use within the
501.Nm 501.Nm
502domain. 502domain.
503The key identifier is returned in 503The key identifier is returned in
504.Fa keyp . 504.Fa keyp .
505.It Fn module_specific_key_delete "specificdata_key_t key" 505.It Fn module_specific_key_delete "specificdata_key_t key"
506Deletes the specified specificdata_key 506Deletes the specified specificdata_key
507.Fa key 507.Fa key
508from the 508from the
509.Nm 509.Nm
510domain. 510domain.
511.It Fn module_getspecific "module_t *mod" "specificdata_key_t key" 511.It Fn module_getspecific "module_t *mod" "specificdata_key_t key"
512Retrieves the value associated with 512Retrieves the value associated with
513.Fa key 513.Fa key
514from module 514from module
515.Fa mod . 515.Fa mod .
516.It Fn module_setspecific "module_t *mod" "specificdata_key_t key" "void *data" 516.It Fn module_setspecific "module_t *mod" "specificdata_key_t key" "void *data"
517Stores 517Stores
518.Fa data 518.Fa data
519as the value associated with 519as the value associated with
520.Fa key 520.Fa key
521for module 521for module
522.Fa mod . 522.Fa mod .
523.El 523.El
524.Sh PROGRAMMING CONSIDERATIONS 524.Sh PROGRAMMING CONSIDERATIONS
525The module subsystem is designed to be called recursively, but only within 525The module subsystem is designed to be called recursively, but only within
526a single LWP. 526a single LWP.
527This permits one module's 527This permits one module's
528.Fn modcmd 528.Fn modcmd
529routine to load or unload other modules. 529routine to load or unload other modules.
530.Pp 530.Pp
531Additional considerations: 531Additional considerations:
532.Bl -bullet -offset indent 532.Bl -bullet -offset indent
533.It 533.It
534A module is not permitted to load or unload itself. 534A module is not permitted to load or unload itself.
535Attempts to load or unload a module from within its own 535Attempts to load or unload a module from within its own
536.Fn modcmd 536.Fn modcmd
537routine will fail with 537routine will fail with
538.Er EEXIST 538.Er EEXIST
539or 539or
540.Er EBUSY , 540.Er EBUSY ,
541respectively. 541respectively.
542.It 542.It
543Although a module can be loaded by using either 543Although a module can be loaded by using either
544.Fn module_load 544.Fn module_load
545or 545or
546.Fn module_autoload , 546.Fn module_autoload ,
547it is not possible for the module's 547it is not possible for the module's
548.Fn modcmd 548.Fn modcmd
549routine to distinguish between the two methods. 549routine to distinguish between the two methods.
550Any module which needs to ensure that it does not get auto-unloaded must 550Any module which needs to ensure that it does not get auto-unloaded must
551either handle the 551either handle the
552.Dv MODULE_CMD_AUTOUNLOAD 552.Dv MODULE_CMD_AUTOUNLOAD
553command in its 553command in its
554.Fn modcmd 554.Fn modcmd
555routine, or use 555routine, or use
556.Fn module_hold 556.Fn module_hold
557to increment its reference count. 557to increment its reference count.
558Note however that modules loaded manually with 558Note however that modules loaded manually with
559.Xr modload 8 559.Xr modload 8
560are never auto-unloaded. 560are never auto-unloaded.
561.El 561.El
562.Sh EXAMPLES 562.Sh EXAMPLES
563A set of example modules is available in the 563A set of example modules is available in the
564.Pa src/sys/modules/examples 564.Pa src/sys/modules/examples
565directory hierarchy. 565directory hierarchy.
566.Sh CODE REFERENCES 566.Sh CODE REFERENCES
567The core of the kernel module implementation is in 567The core of the kernel module implementation is in
568.Pa sys/kern/kern_module.c 568.Pa sys/kern/kern_module.c
569and 569and
570.Pa sys/kern/kern_module_vfs.c . 570.Pa sys/kern/kern_module_vfs.c .
571.Pp 571.Pp
572The routines for linking the module are in 572The routines for linking the module are in
573.Pa sys/kern/subr_kobj.c . 573.Pa sys/kern/subr_kobj.c .
574.Pp 574.Pp
575The routines for reading a module from the file system are in 575The routines for reading a module from the file system are in
576.Pa sys/kern/subr_kobj_vfs.c . 576.Pa sys/kern/subr_kobj_vfs.c .
577.Pp 577.Pp
578The header file 578The header file
579.In sys/sys/module.h 579.In sys/sys/module.h
580describes the public interface. 580describes the public interface.
581.Pp 581.Pp
582In addition, each architecture is expected to provide 582In addition, each architecture is expected to provide
583.Fn kobj_machdep , 583.Fn kobj_machdep ,
584.Fn kobj_reloc , 584.Fn kobj_reloc ,
585and 585and
586.Fn module_init_md . 586.Fn module_init_md .
587.Fn kobj_machdep 587.Fn kobj_machdep
588is for any machine dependent actions, such as flushing caches, that are 588is for any machine dependent actions, such as flushing caches, that are
589needed when a module is loaded or unloaded. 589needed when a module is loaded or unloaded.
590.Fn kobj_reloc 590.Fn kobj_reloc
591deals with resolution of relocatable symbols. 591deals with resolution of relocatable symbols.
592.Fn module_init_md 592.Fn module_init_md
593is for finding modules passed in by the boot loader. 593is for finding modules passed in by the boot loader.
594.Sh SEE ALSO 594.Sh SEE ALSO
595.Xr modctl 2 , 595.Xr modctl 2 ,
596.Xr module 7 , 596.Xr module 7 ,
597.Xr specificdata 9 , 597.Xr specificdata 9 ,
598.Xr intro 9lua 598.Xr intro 9lua
599.Sh HISTORY 599.Sh HISTORY
600The kernel module subsystem first appeared in 600The kernel module subsystem first appeared in
601.Nx 5.0 . 601.Nx 5.0 .
602It replaces the 602It replaces the
603.Dq LKM 603.Dq LKM
604subsystem from earlier releases. 604subsystem from earlier releases.
605.Sh AUTHORS 605.Sh AUTHORS
606.An -nosplit 606.An -nosplit
607The 607The
608.Nm 608.Nm
609system was written by 609system was written by
610.An Andrew Doran Aq Mt ad@NetBSD.org . 610.An Andrew Doran Aq Mt ad@NetBSD.org .
611This manual page was written by 611This manual page was written by
612.An Paul Goyette Aq Mt pgoyette@NetBSD.org . 612.An Paul Goyette Aq Mt pgoyette@NetBSD.org .