[doc] Documented the module system.
[marionette.git] / doc / modules.txt
blob5ef2d13093b405f5b504aa72613640971e3da887
1 Modules
2 =======
4 Concepts
5 --------
7 A kernel module is an object file which can be loaded into the kernel at
8 run-time, rather than being compiled into the kernel directly.
10 Each kernel module has an entry point (`_start`) and can reference symbols in
11 the kernel, as well as symbols in other modules.
13 Modules are *trusted*. They run with full kernel privileges.
15 The Module Loader
16 -----------------
18 The module loader loads a kernel module and patches up the relocations for
19 execution. It also resolves references to kernel symbols, and symbols in other
20 modules.
22 When the module is ready to run, it calls the `_start` function of the module.
23 Note that the `_start` function is not given its own thread! It is expected to
24 call various kernel functions to install itself, and then return.
26 The `modinit` section
27 ---------------------
29 Kernel modules have a section called `modinit`. This section contains an array
30 of function pointers to initialisation functions. If you're familiar with the
31 `.ctors` sections of C++, `modinit` works in exactly the same way.
33 If a module is actually built as a kernel module, it is linked with
34 `kernel/module-entry.c`, which provides the entry point `_start` and calls each
35 function in the `modinit` section in turn.
37 This produces some desirable effects:
39 - Modules can be linked together into a supermodule, with no code changes. This
40   will merge .text, .data and .bss sections of the modules together and will
41   save memory that would otherwise be wasted due to alignment. For example, it
42   may be useful for a user to link together all the modules they know they need.
44 - Modules can even be linked statically into the kernel! The kernel can look in
45   its own `modinit` section for the module initialisation functions. This means
46   that modules can be compiled into the kernel, or compiled as separate modules,
47   very easily.
49 Problems
50 ~~~~~~~~
52 The `modinit` section does not take into account module dependencies. For
53 example, if module A uses module B, module B would have to be initialised before
54 A, although currently the initialisation order would depend on the order of the
55 linker arguments!