xref: /netbsd-src/share/man/man9/module.9 (revision 7ce51081c72a7abd8226164886a921c009714c51)
1*7ce51081Sriastradh.\"	$NetBSD: module.9,v 1.54 2022/08/12 15:43:38 riastradh Exp $
231f0dbbbSpgoyette.\"
331f0dbbbSpgoyette.\" Copyright (c) 2010 The NetBSD Foundation, Inc.
431f0dbbbSpgoyette.\" All rights reserved.
531f0dbbbSpgoyette.\"
631f0dbbbSpgoyette.\" This code is derived from software contributed to The NetBSD Foundation
731f0dbbbSpgoyette.\" by Andrew Doran.
831f0dbbbSpgoyette.\"
931f0dbbbSpgoyette.\" Redistribution and use in source and binary forms, with or without
1031f0dbbbSpgoyette.\" modification, are permitted provided that the following conditions
1131f0dbbbSpgoyette.\" are met:
1231f0dbbbSpgoyette.\" 1. Redistributions of source code must retain the above copyright
1331f0dbbbSpgoyette.\"    notice, this list of conditions and the following disclaimer.
1431f0dbbbSpgoyette.\" 2. Redistributions in binary form must reproduce the above copyright
1531f0dbbbSpgoyette.\"    notice, this list of conditions and the following disclaimer in the
1631f0dbbbSpgoyette.\"    documentation and/or other materials provided with the distribution.
1731f0dbbbSpgoyette.\"
1831f0dbbbSpgoyette.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
1931f0dbbbSpgoyette.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2031f0dbbbSpgoyette.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2131f0dbbbSpgoyette.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2231f0dbbbSpgoyette.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2331f0dbbbSpgoyette.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2431f0dbbbSpgoyette.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2531f0dbbbSpgoyette.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2631f0dbbbSpgoyette.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2731f0dbbbSpgoyette.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2831f0dbbbSpgoyette.\" POSSIBILITY OF SUCH DAMAGE.
2931f0dbbbSpgoyette.\"
307e0a9c14Spgoyette.Dd July 21, 2021
3131f0dbbbSpgoyette.Dt MODULE 9
3231f0dbbbSpgoyette.Os
3331f0dbbbSpgoyette.Sh NAME
3431f0dbbbSpgoyette.Nm module ,
3531f0dbbbSpgoyette.Nm module_load ,
3631f0dbbbSpgoyette.Nm module_autoload ,
3731f0dbbbSpgoyette.Nm module_unload ,
3831f0dbbbSpgoyette.Nm module_init_class ,
3931f0dbbbSpgoyette.Nm module_hold ,
406241e2c4Spgoyette.Nm module_rele ,
4143518000Spgoyette.Nm module_find_section ,
4243518000Spgoyette.Nm module_kernel ,
4343518000Spgoyette.Nm module_name ,
44208f2b06Spgoyette.Nm module_source ,
45208f2b06Spgoyette.Nm module_register_callbacks ,
4662ec288fSpgoyette.Nm module_unregister_callbacks ,
4762ec288fSpgoyette.Nm module_specific_key_create ,
4862ec288fSpgoyette.Nm module_specific_key_delete ,
4962ec288fSpgoyette.Nm module_getspecific ,
5062ec288fSpgoyette.Nm module_setspecific
5131f0dbbbSpgoyette.Nd kernel module loader
5231f0dbbbSpgoyette.Sh SYNOPSIS
5331f0dbbbSpgoyette.In sys/module.h
5431f0dbbbSpgoyette.Fn MODULE "class" "name" "required"
5531f0dbbbSpgoyette.Ft int
56fb03fbedSpgoyette.Fn module_load "const char *name" "int flags" "prop_dictionary_t props" \
57fb03fbedSpgoyette"modclass_t class"
5831f0dbbbSpgoyette.Ft int
5931f0dbbbSpgoyette.Fn module_autoload "const char *name" "modclass_t class"
6031f0dbbbSpgoyette.Ft int
6131f0dbbbSpgoyette.Fn module_unload "const char *name"
6231f0dbbbSpgoyette.Ft void
6331f0dbbbSpgoyette.Fn module_init_class "modclass_t class"
647e0a9c14Spgoyette.Ft void
6543518000Spgoyette.Fn module_hold "module_t *module"
6631f0dbbbSpgoyette.Ft void
6743518000Spgoyette.Fn module_rele "module_t *module"
686241e2c4Spgoyette.Ft int
696241e2c4Spgoyette.Fn module_find_section "const char *" "void **" "size_t *"
7043518000Spgoyette.Ft "module_t *"
7143518000Spgoyette.Fn module_kernel "void"
7243518000Spgoyette.Ft "const char *"
7343518000Spgoyette.Fn module_name "struct module *module"
7443518000Spgoyette.Ft modsrc_t
7543518000Spgoyette.Fn module_source "struct module *module"
767a8c7dc7Spgoyette.Ft void
777a8c7dc7Spgoyette.Fn module_init "void"
787a8c7dc7Spgoyette.Ft void
797a8c7dc7Spgoyette.Fn module_start_unload_thread "void"
807a8c7dc7Spgoyette.Ft void
817a8c7dc7Spgoyette.Fn module_builtin_require_force "void"
827a8c7dc7Spgoyette.Ft void
837a8c7dc7Spgoyette.Fn module_load_vfs_init "void"
8467554cc5Spgoyette.Ft "void *"
8567554cc5Spgoyette.Fn module_register_callbacks "void (*)(struct module *)" \
8667554cc5Spgoyette"void (*unload)(struct module *)"
8767554cc5Spgoyette.Ft void
8867554cc5Spgoyette.Fn module_unregister_callbacks "void *"
8962ec288fSpgoyette.Ft specificdata_key_t
9062ec288fSpgoyette.Fn module_specific_key_create "specificdata_key_t *keyp" \
9162ec288fSpgoyette"specificdata_dtor_t dtor"
9262ec288fSpgoyette.Ft void
9362ec288fSpgoyette.Fn module_specific_key_delete "specificdata_key_t key"
9462ec288fSpgoyette.Ft "void *"
9562ec288fSpgoyette.Fn module_getspecific "module_t *mod" "specificdata_key_t key"
9662ec288fSpgoyette.Ft "void *"
9762ec288fSpgoyette.Fn module_setspecific "module_t *mod" "specificdata_key_t key" "void *data"
9831f0dbbbSpgoyette.Sh DESCRIPTION
99da5cba29SjruohoModules are sections of code that can be independently linked and selectively
10031f0dbbbSpgoyetteloaded into or unloaded from a running kernel.
10131f0dbbbSpgoyetteThis provides a mechanism to update the module without having to relink
10231f0dbbbSpgoyettethe kernel and reboot.
103fb03fbedSpgoyetteModules can be loaded from within the kernel image, provided by the boot
10431f0dbbbSpgoyetteloader, or loaded from the file system.
10531f0dbbbSpgoyette.Pp
1067c6859f1SpgoyetteThe
1077c6859f1Spgoyette.Nm
1087c6859f1Spgoyettesubsystem includes two data types:
109da5cba29Sjruoho.Bl -enum -offset indent
110da5cba29Sjruoho.It
11131f0dbbbSpgoyetteThe
11231f0dbbbSpgoyette.Vt module_t
11331f0dbbbSpgoyettetype provides storage to describe a module.
114da5cba29Sjruoho.It
11531f0dbbbSpgoyetteThe
11631f0dbbbSpgoyette.Vt modinfo_t
1177c6859f1Spgoyettetype resides within
1187c6859f1Spgoyette.Vt module_t
1197c6859f1Spgoyetteand contains module header info.
120da5cba29Sjruoho.El
1214a743ad4Spgoyette.Pp
1227c6859f1SpgoyetteThe module subsystem is protected by the global
1237c6859f1Spgoyette.Va kernconfig_mutex .
12431f0dbbbSpgoyette.Sh FUNCTIONS
12531f0dbbbSpgoyette.Bl -tag -width abcd
12631f0dbbbSpgoyette.It Fn MODULE "class" "name" "required"
12731f0dbbbSpgoyetteThe
12831f0dbbbSpgoyette.Fn MODULE
129f43feba0Swizmacro creates and initializes a
13031f0dbbbSpgoyette.Vt modinfo_t
13131f0dbbbSpgoyettestructure.
1329500538cSpgoyetteThe
1339500538cSpgoyette.Fa class
134f1af096dSpgoyetteargument identifies the class of module, and must be one of the following
135f1af096dSpgoyette(note that
136f1af096dSpgoyette.Dv MODULE_CLASS_ANY
137f1af096dSpgoyetteshould not be used here):
1385c1f1a6dSpgoyette.Bl -tag -width MODULE_CLASS_SECMODEL -offset indent
1399500538cSpgoyette.It Dv MODULE_CLASS_VFS
1409500538cSpgoyetteThe module provide a virtual file system - see
1419500538cSpgoyette.Xr vfs 9
1429500538cSpgoyette.It Dv MODULE_CLASS_DRIVER
1439500538cSpgoyetteThe module is a device driver - see
1449500538cSpgoyette.Xr driver 9
1459500538cSpgoyette.It Dv MODULE_CLASS_EXEC
1469500538cSpgoyetteThe module provides an alternate execution environment - see the various
1479500538cSpgoyette.Dv COMPAT_xxx
1489500538cSpgoyetteoptions in
149d00a8be0Swiz.Xr options 4
1509500538cSpgoyette.It Dv MODULE_CLASS_SECMODEL
1519500538cSpgoyetteThe module provides a security model - see
1529500538cSpgoyette.Xr secmodel 9
1539500538cSpgoyette.It Dv MODULE_CLASS_BUFQ
1549500538cSpgoyetteThe module provides a buffer queue strategy - see
1559500538cSpgoyette.Xr bufq 9
1569500538cSpgoyette.It Dv MODULE_CLASS_MISC
1579500538cSpgoyetteThe module provides miscellaneous kernel services
1589500538cSpgoyette.El
1599500538cSpgoyette.Pp
1609500538cSpgoyetteThe
1619500538cSpgoyette.Fa name
1629500538cSpgoyetteargument provides the name of the module.
1639500538cSpgoyetteLoaded modules, including those that are built-in to the kernel, must all
1649500538cSpgoyettehave unique names.
1659500538cSpgoyette.Pp
1669500538cSpgoyetteThe
1679500538cSpgoyette.Fa required
1685c4820f0Spgoyetteargument is a quoted string containing a comma-separated list of module
1695c4820f0Spgoyettenames that are required by this module.
1709500538cSpgoyetteThe list must not contain any white-space.
1719500538cSpgoyetteWhen a module is loaded, all of its required modules are auto-loaded and
1729500538cSpgoyetteinitialized before the module itself is loaded.
1739500538cSpgoyetteLoading of required modules is a recursive operation.
1749500538cSpgoyette.Pp
1755c4820f0SpgoyetteIf there are no required modules, this argument should be specified as
1765c4820f0Spgoyette.Dv NULL .
1775c4820f0Spgoyette.Pp
178fb03fbedSpgoyetteIn addition to the explicit arguments, the
179fb03fbedSpgoyette.Fn MODULE
180fb03fbedSpgoyettemacro creates a reference to the module's
181fb03fbedSpgoyette.Fn modcmd
182fb03fbedSpgoyettefunction.
183da5cba29SjruohoThis function is defined as:
184da5cba29Sjruoho.Bl -tag -width modcmd -offset indent
18543518000Spgoyette.It Ft modcmd_t
1865c4820f0Spgoyette.Fn xxx_modcmd "modcmd_t cmd" "void *data"
187fb03fbedSpgoyette.El
188fb03fbedSpgoyette.Pp
1895c4820f0Spgoyette(where xxx is the name of the module, from the
1905c4820f0Spgoyette.Dv MODULE
1915c4820f0Spgoyettemacro).
1925c4820f0Spgoyette.Pp
193fb03fbedSpgoyetteThe
194fb03fbedSpgoyette.Fa cmd
195fb03fbedSpgoyetteargument requests one of the following operations:
196da5cba29Sjruoho.Bl -tag -width MODULE_CMD_AUTOUNLOAD -offset indent
197da5cba29Sjruoho.It Dv MODULE_CMD_INIT
198da5cba29SjruohoPerform module-specific initialization when the module is loaded.
199da5cba29Sjruoho.It Dv MODULE_CMD_FINI
200da5cba29SjruohoPerform module-specific clean-up before the module is unloaded.
201da5cba29Sjruoho.It Dv MODULE_CMD_AUTOUNLOAD
202da5cba29SjruohoNotify the module that it is about to be unloaded.
203da5cba29Sjruoho.It Dv MODULE_CMD_STAT
204da5cba29SjruohoRequest the module to provide status information (not currently implemented).
205fb03fbedSpgoyette.El
206fb03fbedSpgoyette.Pp
207fb03fbedSpgoyetteAll modules'
208fb03fbedSpgoyette.Fn modcmd
209fb03fbedSpgoyettefunctions must implement the
210fb03fbedSpgoyette.Dv MODULE_CMD_INIT
211fb03fbedSpgoyetteand
212fb03fbedSpgoyette.Dv MODULE_CMD_FINI
213da5cba29Sjruohocommands.
214da5cba29SjruohoThe other commands are optional,
215da5cba29Sjruohoand should return
216da5cba29Sjruoho.Er ENOTTY
217da5cba29Sjruohoif not implemented.
218fb03fbedSpgoyette.Pp
219fb03fbedSpgoyetteFor the
220fb03fbedSpgoyette.Dv MODULE_CMD_INIT
221fb03fbedSpgoyettecommand, the
222fb03fbedSpgoyette.Fa data
223f792e9e4Spgoyetteargument is used to pass a pointer to the module's
224da5cba29Sjruoho.Xr prop_dictionary 3 .
225da5cba29SjruohoFor the
226fb03fbedSpgoyette.Dv MODULE_CMD_STAT
227fb03fbedSpgoyettecommand, the
228fb03fbedSpgoyette.Fa data
229fb03fbedSpgoyetteargument points to a buffer where the status information should be placed.
230fb03fbedSpgoyette.Pp
23131f0dbbbSpgoyetteThe __link_set mechanism is used to enable the
23231f0dbbbSpgoyette.Nm
233f792e9e4Spgoyettesubsystem to locate the
234f792e9e4Spgoyette.Vt modinfo_t
235f792e9e4Spgoyettestructure.
23631f0dbbbSpgoyette.It Fn module_load "name" "flags" "props" "class"
237fb03fbedSpgoyetteLoad a module, link it into the running kernel, and call the module's
23831f0dbbbSpgoyette.Fn modcmd
239fb03fbedSpgoyetteroutine with a
240fb03fbedSpgoyette.Fa cmd
241fb03fbedSpgoyetteargument of
242fb03fbedSpgoyette.Dv MODULE_CMD_INIT .
24331f0dbbbSpgoyetteIf the specified module requires other modules, they are loaded first; if
24444b6cc43Spgoyetteany required module cannot be loaded or if any of their
24531f0dbbbSpgoyette.Fn modcmd
2464de9b94cSjnemethcontrol routines returns a non-zero status, loading of this module and
2474de9b94cSjnemeththe specific required module will fail.
2484de9b94cSjnemethThe required modules are marked for automatic unloading.
2494de9b94cSjnemethThus, if the loading of the module failed, the required modules will
2504de9b94cSjnemethbe automatically unloaded after a short delay.
25131f0dbbbSpgoyette.Pp
25231f0dbbbSpgoyetteThe loader will look first for a built-in module with the specified
25331f0dbbbSpgoyette.Fa name
25431f0dbbbSpgoyettethat has not been disabled (see
25531f0dbbbSpgoyette.Fn module_unload
25631f0dbbbSpgoyettebelow).
25731f0dbbbSpgoyetteIf a built-in module with that
25831f0dbbbSpgoyette.Fa name
25931f0dbbbSpgoyetteis not found, the list of modules prepared by the boot loader is searched.
26031f0dbbbSpgoyetteIf the named module is still not found, an attempt is made to locate the
261b7e3c0fcSjymmodule within the file system, provided it has been mounted by the
262b7e3c0fcSjyminitialization code.
26331f0dbbbSpgoyette.Pp
26431f0dbbbSpgoyetteThe
26531f0dbbbSpgoyette.Fa flags
266da5cba29Sjruohoargument can include:
267da5cba29Sjruoho.Bl -tag -width MODCTL_LOAD_FORCE -offset indent
268da5cba29Sjruoho.It Dv MODCTL_NO_PROP
269ffe6b7cdSwizWhen loading a module from the file system, do not attempt to locate a
270f792e9e4Spgoyettecorresponding prop_dictionary file.
271da5cba29Sjruoho.It Dv MODCTL_LOAD_FORCE
272f792e9e4SpgoyetteForce loading of disabled built-in modules and modules built for a
273f792e9e4Spgoyettedifferent version of the operating system.
27431f0dbbbSpgoyette.El
27531f0dbbbSpgoyette.Pp
27631f0dbbbSpgoyetteThe
27731f0dbbbSpgoyette.Fa props
27831f0dbbbSpgoyetteargument points to an externalized property list which is passed to the
27931f0dbbbSpgoyettemodule's
28031f0dbbbSpgoyette.Fn modcmd
28131f0dbbbSpgoyetteroutine.
282ffe6b7cdSwizIf a module is being loaded from the file system, and the
283f792e9e4Spgoyette.Dv MODCTL_NO_PROP
284f792e9e4Spgoyetteflag is not set, the system searches for a file with the same name as the
285032d0f9aSwizmodule file, but with the suffix
28622405528Sjnemeth.Dq Pa .plist .
287f792e9e4SpgoyetteIf this file is found, the prop_dictionary it contains is loaded and
2884de9b94cSjnemethmerged with the prop_dictionary from the
2894de9b94cSjnemeth.Fa props
2904de9b94cSjnemethargument.
29131f0dbbbSpgoyette.Pp
29231f0dbbbSpgoyetteThe
29331f0dbbbSpgoyette.Fa class
294da5cba29Sjruohoargument can be any of:
295da5cba29Sjruoho.Pp
296f792e9e4Spgoyette.Bl -tag -width MODULE_CLASS_SECMODEL -offset indent -compact
297da5cba29Sjruoho.It Dv MODULE_CLASS_ANY
298da5cba29Sjruoho.It Dv MODULE_CLASS_DRIVER
299f792e9e4SpgoyetteDevice driver
300da5cba29Sjruoho.It Dv MODULE_CLASS_EXEC
301f792e9e4SpgoyetteExecutable image handler
302f792e9e4Spgoyette.It Dv MODULE_CLASS_MISC
303f792e9e4SpgoyetteMiscellaneous module
304da5cba29Sjruoho.It Dv MODULE_CLASS_SECMODEL
305f792e9e4SpgoyetteSecurity model (see
306f792e9e4Spgoyette.Xr secmodel 9
307f792e9e4Spgoyettefor more details)
308f792e9e4Spgoyette.It Dv MODULE_CLASS_VFS
309f792e9e4SpgoyetteVirtual file system
310da5cba29Sjruoho.El
311da5cba29Sjruoho.Pp
312fb03fbedSpgoyetteIf the class is not
313fb03fbedSpgoyette.Dv MODULE_CLASS_ANY ,
314fb03fbedSpgoyettethe class of the module being loaded
315da5cba29Sjruohomust match the requested
316da5cba29Sjruoho.Fa class .
317f792e9e4SpgoyetteExcept when verifying a module's class when it is being loaded, module
318c6f8bef8Spgoyetteclasses other than
319c6f8bef8Spgoyette.Dv MODULE_CLASS_SECMODEL
320c6f8bef8Spgoyetteare transparent to the module subsystem.
321f792e9e4SpgoyetteThey are provided only for the benefit of the subsystem's clients.
322c6f8bef8SpgoyetteModules with class
323c6f8bef8Spgoyette.Dv MODULE_CLASS_SECMODEL
324c6f8bef8Spgoyetteare automatically registered with
325c6f8bef8Spgoyette.Fn secmodel_register
3263a12eae4Sjnemethafter being successfully loaded, and automatically deregistered with
3273a12eae4Sjnemeth.Fn secmodel_deregister
3283a12eae4Sjnemethwhen being unloaded.
32931f0dbbbSpgoyette.Pp
33031f0dbbbSpgoyetteThe
33131f0dbbbSpgoyette.Fn module_load
332fb03fbedSpgoyetteroutine is primarily intended as the implementation of the
333fb03fbedSpgoyette.Dv MODCTL_LOAD
33431f0dbbbSpgoyetteoption of the
33531f0dbbbSpgoyette.Xr modctl 2
33631f0dbbbSpgoyettesystem call.
33731f0dbbbSpgoyette.It Fn module_autoload "name" "class"
338fb03fbedSpgoyetteAuto-load a module, making it available for automatic unloading.
33931f0dbbbSpgoyetteThe
34031f0dbbbSpgoyette.Fa name
34131f0dbbbSpgoyetteand
34231f0dbbbSpgoyette.Fa class
34331f0dbbbSpgoyettearguments are the same as for the
34431f0dbbbSpgoyette.Fn module_load
34531f0dbbbSpgoyetteroutine.
34631f0dbbbSpgoyette.Pp
347fb03fbedSpgoyetteThe module subsystem uses a kernel thread to attempt to automatically
348fb03fbedSpgoyetteunload modules a short time (currently, 10 seconds) after being loaded by
349fb03fbedSpgoyette.Fn module_autoload .
350*7ce51081SriastradhBefore the module is unloaded by this thread, its
35131f0dbbbSpgoyette.Fn modcmd
352da5cba29Sjruohois called with the
353fb03fbedSpgoyette.Fa cmd
354da5cba29Sjruohoargument specified as
355fb03fbedSpgoyette.Dv MODULE_CMD_AUTOUNLOAD .
35631f0dbbbSpgoyetteA module can prevent itself from being unloaded by returning a non-zero
35731f0dbbbSpgoyettevalue.
358*7ce51081SriastradhException: If
359*7ce51081Sriastradh.Li kern.module.autounload_unsafe
360*7ce51081Sriastradhis set, a module that returns
361*7ce51081Sriastradh.Er ENOTTY ,
362*7ce51081Sriastradhmeaning it does not understand the command, may still be autounloaded.
36331f0dbbbSpgoyette.Pp
364da5cba29SjruohoThe
36531f0dbbbSpgoyette.Fn module_autoload
366da5cba29Sjruohofunction is intended for use by kernel components to locate and load optional
36731f0dbbbSpgoyettesystem components.
368da5cba29SjruohoThe function is also used to load modules that are required by other modules.
36920fdec76Sjnemeth.Pp
37020fdec76SjnemethThe directory from which the module is loaded will be searched for a file
37120fdec76Sjnemethwith the same name as the module file, but with the suffix
37220fdec76Sjnemeth.Dq Pa .plist .
37320fdec76SjnemethIf this file is found, the prop_dictionary it contains will be loaded and
37420fdec76Sjnemethpassed to the module's
37520fdec76Sjnemeth.Fn modcmd
37620fdec76Sjnemethroutine.
37720fdec76SjnemethIf this prop_dictionary contains a
37820fdec76Sjnemeth.Dq Pa noautoload
37920fdec76Sjnemethproperty which is set to
38020fdec76Sjnemeth.Dq Pa true
38120fdec76Sjnemeththen the system will refuse to load the module.
38231f0dbbbSpgoyette.It Fn module_unload "name"
38331f0dbbbSpgoyetteUnload a module.
384da5cba29SjruohoIf the module's reference count is non-zero, the function returns
385da5cba29Sjruoho.Er EBUSY .
38631f0dbbbSpgoyetteOtherwise, the module's
38731f0dbbbSpgoyette.Fn modcmd
388fb03fbedSpgoyetteroutine is called with a
389fb03fbedSpgoyette.Fa cmd
390fb03fbedSpgoyetteargument of
3914de9b94cSjnemeth.Dv MODULE_CMD_FINI .
3924de9b94cSjnemethIf the
3934de9b94cSjnemeth.Fn modcmd
3944de9b94cSjnemethroutine returns with an error, then the error is returned to the caller
3954de9b94cSjnemethotherwise the module is unloaded.
39631f0dbbbSpgoyette.Pp
397da5cba29SjruohoThe reference counts of all modules that were required by this module are
398da5cba29Sjruohodecremented, but the required modules are not unloaded by the call to
399da5cba29Sjruoho.Fn module_unload .
400da5cba29SjruohoInstead, the required modules may be unloaded by subsequent calls to
401da5cba29Sjruoho.Fn module_unload .
402da5cba29Sjruoho.Pp
403da5cba29SjruohoUnloading a built-in module causes the module to be marked as disabled.
40431f0dbbbSpgoyetteThis prevents the module from being re-loaded, except by the
40531f0dbbbSpgoyette.Fn module_load
40631f0dbbbSpgoyettefunction with the
40731f0dbbbSpgoyette.Fa flags
408fb03fbedSpgoyetteargument set to
409fb03fbedSpgoyette.Dv MODULE_FORCE_LOAD .
41031f0dbbbSpgoyette.Pp
411da5cba29SjruohoThe
41231f0dbbbSpgoyette.Fn module_unload
413da5cba29Sjruohofunction may be called by the
41431f0dbbbSpgoyette.Xr modctl 2
41531f0dbbbSpgoyettesystem call, by the module subsystem's internal auto-unload thread, or by
41631f0dbbbSpgoyetteother kernel facilities.
4174de9b94cSjnemethGenerally, other kernel facilities should not be calling this function.
41831f0dbbbSpgoyette.It Fn module_init_class "class"
41931f0dbbbSpgoyetteLoad and initialize all available modules of the specified
42031f0dbbbSpgoyette.Fa class .
42131f0dbbbSpgoyetteAny built-in modules that have not been disabled, and any modules provided
42231f0dbbbSpgoyetteby the boot loader are loaded.
42343518000Spgoyette.It Fn module_hold "module"
42431f0dbbbSpgoyetteIncrement the reference count of a module.
425fb03fbedSpgoyetteA module cannot be unloaded if its reference count is non-zero.
42643518000Spgoyette.It Fn module_rele "module"
42731f0dbbbSpgoyetteDecrement the reference count of a module.
4286241e2c4Spgoyette.It Fn module_find_section "name" "addr" "size"
4296241e2c4SpgoyetteFind the start address and size of linker section
4306241e2c4Spgoyette.Ar name
4316241e2c4Spgoyettewithin a module.
4326241e2c4SpgoyetteThe miniroot module uses this routine to find the address and size of the
4336241e2c4Spgoyetteembedded file system image.
4346241e2c4SpgoyetteThis routine can only examine the linker data for the module that is
4356241e2c4Spgoyettecurrently being initialized;  it cannot examine data for any other module.
43643518000Spgoyette.It Fn module_kernel "void"
43743518000SpgoyetteReturns a pointer to a
43843518000Spgoyette.Ft module_t
43943518000Spgoyettestructure describing the kernel module.
44043518000Spgoyette.It Fn module_name module
44143518000SpgoyetteReturns a pointer to the module's name.
44243518000Spgoyette.It Fn module_source module
44343518000SpgoyetteReturns the source of the module, one of
44443518000Spgoyette.Dv MODULE_SOURCE_KERNEL ,
44543518000Spgoyette.Dv MODULE_SOURCE_BOOT ,
44643518000Spgoyetteor
44743518000Spgoyette.Dv MODULE_SOURCE_FILESYS .
4487a8c7dc7Spgoyette.It Fn module_init "void"
4497a8c7dc7SpgoyetteInitialize the module subsystem.
4507a8c7dc7SpgoyetteCreates and initializes various data structures, locates all built-in
4517a8c7dc7Spgoyettemodules, and establishes the sub-system's
4527a8c7dc7Spgoyette.Xr sysctl 8
4537a8c7dc7Spgoyettetree.
4547a8c7dc7Spgoyette.Fn module_init
4557a8c7dc7Spgoyetteis called early in system initialization to facilitate use of security model
4567a8c7dc7Spgoyettemodules.
4577a8c7dc7Spgoyette.It Fn module_start_unload_thread "void"
4587a8c7dc7SpgoyetteCreate the thread that attempts to automatically unload modules that were
4597a8c7dc7Spgoyetteloaded via the
4607a8c7dc7Spgoyette.Fn module_autoload
4617a8c7dc7Spgoyetteroutine.
462dfca43fdSjruohoThe function is called only once,
463dfca43fdSjruohoafter the scheduler and timer functions are initialized.
4647a8c7dc7Spgoyette.It Fn module_builtin_require_force "void"
4657a8c7dc7SpgoyetteMark as "disabled" any built-in modules that have not been successfully
466ffe6b7cdSwizinitialized.
4677a8c7dc7SpgoyetteModules marked "disabled" can only be loaded if the
4687a8c7dc7Spgoyette.Dv MODCTL_LOAD_FORCE
4697a8c7dc7Spgoyetteis specified.
4707a8c7dc7Spgoyette.Fn module_builtin_require_force
4717a8c7dc7Spgoyetteis called near the end of system initialization, after the
4727a8c7dc7Spgoyette.Xr init 8
4737a8c7dc7Spgoyetteprocess is created.
47467554cc5Spgoyette.It Fn module_load_vfs_init "void"
475ffe6b7cdSwizThe module subsystem is initialized early, long before any file systems
4767a8c7dc7Spgoyetteare available.
477ffe6b7cdSwizAfter the root file system is mounted,
47867554cc5Spgoyette.Fn module_load_vfs_init "void"
479ffe6b7cdSwizis used to enable loading modules from the file system.
4807a8c7dc7SpgoyetteUntil this routine is called, modules can only be loaded if they were
4817a8c7dc7Spgoyettebuilt-in to the kernel image or provided by the boot loader.
48267554cc5Spgoyette.It Fn module_register_callbacks "void (*load)(struct module *)" \
48367554cc5Spgoyette"void (*unload)(struct module *)"
48467554cc5SpgoyetteRegister a new set of callbacks.
48567554cc5SpgoyetteThe
48667554cc5Spgoyette.Fa load
48767554cc5Spgoyettecallback is invoked after any module (including this module) is
48867554cc5Spgoyettesuccessfully loaded; the
48967554cc5Spgoyette.Fa unload
49067554cc5Spgoyettecallback is invoked before any module is unloaded.
49167554cc5SpgoyetteEach load or unload event can result in multiple invocations of the
49267554cc5Spgoyettecallback routines.
49367554cc5SpgoyetteAn opaque cookie is returned which can be passed to
49467554cc5Spgoyette.Fn module_unregister_callbacks .
49567554cc5Spgoyette.It Fn module_unregister_callbacks "void *opaque"
49667554cc5SpgoyetteUnregister a set of callback routines previously registered with
49767554cc5Spgoyette.Fn module_register_callbacks .
49867554cc5SpgoyetteThe
49967554cc5Spgoyette.Fa opaque
50067554cc5Spgoyetteargument should be the return value from the previous
50167554cc5Spgoyette.Fn module_register_callbacks
50267554cc5Spgoyettecall.
50356d01054Spgoyette.It Fn module_specific_key_create "specificdata_key_t *keyp" \
50462ec288fSpgoyette"specificdata_dtor_t dtor"
50562ec288fSpgoyetteCreates a new specificdata_key for use within the
50662ec288fSpgoyette.Nm
50762ec288fSpgoyettedomain.
50862ec288fSpgoyetteThe key identifier is returned in
50962ec288fSpgoyette.Fa keyp .
51056d01054Spgoyette.It Fn module_specific_key_delete "specificdata_key_t key"
51162ec288fSpgoyetteDeletes the specified specificdata_key
51262ec288fSpgoyette.Fa key
51362ec288fSpgoyettefrom the
5149a08b6a9Swiz.Nm
5159a08b6a9Swizdomain.
51656d01054Spgoyette.It Fn module_getspecific "module_t *mod" "specificdata_key_t key"
51762ec288fSpgoyetteRetrieves the value associated with
51862ec288fSpgoyette.Fa key
51962ec288fSpgoyettefrom module
52062ec288fSpgoyette.Fa mod .
52156d01054Spgoyette.It Fn module_setspecific "module_t *mod" "specificdata_key_t key" "void *data"
52262ec288fSpgoyetteStores
52362ec288fSpgoyette.Fa data
52462ec288fSpgoyetteas the value associated with
52562ec288fSpgoyette.Fa key
52662ec288fSpgoyettefor module
52762ec288fSpgoyette.Fa mod .
52831f0dbbbSpgoyette.El
5294a743ad4Spgoyette.Sh PROGRAMMING CONSIDERATIONS
5304a743ad4SpgoyetteThe module subsystem is designed to be called recursively, but only within
531dfca43fdSjruohoa single LWP.
5324a743ad4SpgoyetteThis permits one module's
5334a743ad4Spgoyette.Fn modcmd
5344a743ad4Spgoyetteroutine to load or unload other modules.
5354a743ad4Spgoyette.Pp
536dfca43fdSjruohoAdditional considerations:
537dfca43fdSjruoho.Bl -bullet -offset indent
538dfca43fdSjruoho.It
5394a743ad4SpgoyetteA module is not permitted to load or unload itself.
5404a743ad4SpgoyetteAttempts to load or unload a module from within its own
5414a743ad4Spgoyette.Fn modcmd
5424a743ad4Spgoyetteroutine will fail with
5434a743ad4Spgoyette.Er EEXIST
5444a743ad4Spgoyetteor
545dfca43fdSjruoho.Er EBUSY ,
5464a743ad4Spgoyetterespectively.
547dfca43fdSjruoho.It
548dfca43fdSjruohoAlthough a module can be loaded by using either
54931f0dbbbSpgoyette.Fn module_load
5504a743ad4Spgoyetteor
551dfca43fdSjruoho.Fn module_autoload ,
5524a743ad4Spgoyetteit is not possible for the module's
5534a743ad4Spgoyette.Fn modcmd
5544a743ad4Spgoyetteroutine to distinguish between the two methods.
5554a743ad4SpgoyetteAny module which needs to ensure that it does not get auto-unloaded must
5564a743ad4Spgoyetteeither handle the
5574a743ad4Spgoyette.Dv MODULE_CMD_AUTOUNLOAD
5584a743ad4Spgoyettecommand in its
5594a743ad4Spgoyette.Fn modcmd
5604a743ad4Spgoyetteroutine, or use
5614a743ad4Spgoyette.Fn module_hold
5624a743ad4Spgoyetteto increment its reference count.
563288ab9d2SjruohoNote however that modules loaded manually with
564288ab9d2Sjruoho.Xr modload 8
565288ab9d2Sjruohoare never auto-unloaded.
566dfca43fdSjruoho.El
56708966c2eSwiz.Sh EXAMPLES
56808966c2eSwizA set of example modules is available in the
569cadd54e5Spgoyette.Pa src/sys/modules/examples
57008966c2eSwizdirectory hierarchy.
57131f0dbbbSpgoyette.Sh CODE REFERENCES
57231f0dbbbSpgoyetteThe core of the kernel module implementation is in
57331f0dbbbSpgoyette.Pa sys/kern/kern_module.c
57431f0dbbbSpgoyetteand
575fe39a07bSwiz.Pa sys/kern/kern_module_vfs.c .
57631f0dbbbSpgoyette.Pp
5774de9b94cSjnemethThe routines for linking the module are in
5784de9b94cSjnemeth.Pa sys/kern/subr_kobj.c .
5794de9b94cSjnemeth.Pp
5804de9b94cSjnemethThe routines for reading a module from the file system are in
5814de9b94cSjnemeth.Pa sys/kern/subr_kobj_vfs.c .
5824de9b94cSjnemeth.Pp
58331f0dbbbSpgoyetteThe header file
584da5cba29Sjruoho.In sys/sys/module.h
58531f0dbbbSpgoyettedescribes the public interface.
5864de9b94cSjnemeth.Pp
5874de9b94cSjnemethIn addition, each architecture is expected to provide
5884de9b94cSjnemeth.Fn kobj_machdep ,
5894de9b94cSjnemeth.Fn kobj_reloc ,
5904de9b94cSjnemethand
5914de9b94cSjnemeth.Fn module_init_md .
5924de9b94cSjnemeth.Fn kobj_machdep
5934de9b94cSjnemethis for any machine dependent actions, such as flushing caches, that are
5944de9b94cSjnemethneeded when a module is loaded or unloaded.
5954de9b94cSjnemeth.Fn kobj_reloc
5964de9b94cSjnemethdeals with resolution of relocatable symbols.
5974de9b94cSjnemeth.Fn module_init_md
5984de9b94cSjnemethis for finding modules passed in by the boot loader.
59931f0dbbbSpgoyette.Sh SEE ALSO
60009458121Spgoyette.Xr modctl 2 ,
60109f3122fSpgoyette.Xr module 7 ,
602b1b8d817Spgoyette.Xr specificdata 9 ,
60309f3122fSpgoyette.Xr intro 9lua
604efa04f97Swiz.Sh HISTORY
605efa04f97SwizThe kernel module subsystem first appeared in
606efa04f97Swiz.Nx 5.0 .
607efa04f97SwizIt replaces the
608d00a8be0Swiz.Dq LKM
609efa04f97Swizsubsystem from earlier releases.
610f385c3daSpgoyette.Sh AUTHORS
611f385c3daSpgoyette.An -nosplit
612f385c3daSpgoyetteThe
613f385c3daSpgoyette.Nm
614f385c3daSpgoyettesystem was written by
615a5684d07Swiz.An Andrew Doran Aq Mt ad@NetBSD.org .
616f385c3daSpgoyetteThis manual page was written by
617a5684d07Swiz.An Paul Goyette Aq Mt pgoyette@NetBSD.org .
618