0% found this document useful (0 votes)
22 views14 pages

03 Linux Modules

dede

Uploaded by

Lionel Auroux
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views14 pages

03 Linux Modules

dede

Uploaded by

Lionel Auroux
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Linux -

Modules and
misc devices

Lionel Auroux

Modules

Linux - Modules and misc devices

Lionel Auroux

2017-09-29

Lionel Auroux Linux - Modules and misc devices 2017-09-29 1 / 14


Linux -
Modules and
misc devices

Lionel Auroux

Modules

Modules

Lionel Auroux Linux - Modules and misc devices 2017-09-29 2 / 14


Why modules?
Linux -
Modules and
misc devices

Lionel Auroux

Modules

Modules make it easy to develop drivers without rebooting.


Keeps the kernel image small and versatile.
Reduce boot time: don’t spend time initializing drivers,
devices and kernel features you don’t need now.

Lionel Auroux Linux - Modules and misc devices 2017-09-29 3 / 14


What is a module?
Linux -
Modules and
misc devices

Lionel Auroux
$ file hello.ko
hello.ko: ELF 64-bit LSB relocatable, x86-64,
Modules

version 1 (SYSV), not stripped


$ modinfo ./hello.ko
filename: /home/halfr/hello/./hello.ko
author: Rémi Audebert
description: Hello module
license: GPL
depends:
vermagic: 4.0.3-1-ARCH SMP preempt mod_unload
modversions

Lionel Auroux Linux - Modules and misc devices 2017-09-29 4 / 14


Build system
Linux -
Modules and
misc devices

Lionel Auroux

Modules The Linux build system is complex, yet is very easy to use,
and almost always produce the desired result.
Compiling a module is trivial:

Makefile
obj-m := hello.o

Build
$ make -C ~/linux M=`pwd` modules

Lionel Auroux Linux - Modules and misc devices 2017-09-29 5 / 14


Build system
Linux -
Modules and
misc devices

Lionel Auroux

Modules
The Linux build system is complex, yet is very easy to use,
and almost always produce the desired result.
Compiling a module is trivial:

Makefile
obj-m := hello.o
hello-objs := hello-file1.o hello-file2.o

Build
$ make -C ~/linux M=`pwd` modules

Lionel Auroux Linux - Modules and misc devices 2017-09-29 6 / 14


Build system
Linux -
Modules and
misc devices

Lionel Auroux

Modules Using a variable from Kconfig:

Kconfig
config HELLO
tristate "Build with the hello support?"

Makefile
obj-$(CONFIG_HELLO) := hello.o
hello-objs := hello-file1.o hello-file2.o

Lionel Auroux Linux - Modules and misc devices 2017-09-29 7 / 14


Generated files
Linux -
Modules and
misc devices

Lionel Auroux

Modules hello.o: The compiled module.


hello.mod.{c,o}: Additional sections and metadata to
be linked in the module
hello.ko: The linked module.
Module.symvers: Symbol versions
modules.order: Lists modules that appears in the
Makefile, used in case of duplicate module match by
modprobe.
.tmp_versions and .*.cmd: Miscelaneous files.

Lionel Auroux Linux - Modules and misc devices 2017-09-29 8 / 14


Cryptographic signing of modules
Linux -
Modules and
misc devices

Lionel Auroux

Modules
Since Linux 3.7
Requires CONFIG_MODULE_SIG=y.
Cryptographically signs modules during installation (make
modules_install)
Uses RSA and SHA-{1,224,256,384,512}.
The private key is only needed during the build, after which
it can be deleted or stored securely.
The public key gets built into the kernel so that it can be
used to check the signatures as the modules are loaded.
See public keys in /proc/keys.

Lionel Auroux Linux - Modules and misc devices 2017-09-29 9 / 14


Inserting a module
Linux -
Modules and
misc devices

Lionel Auroux
Userspace tools: insmod and modprobe.
Modules
modprobe will try to insert other modules so that all
symbol are resolvable.
They use the init_module(2) syscall, which performs
(roughly):
signature checks (if enabled)
mernel memory allocation
module .text section copy
license and version checks
symbol resolving using the kernel symbol table
sysfs and internal registrations

Lionel Auroux Linux - Modules and misc devices 2017-09-29 10 / 14


Unloading a module
Linux -
Modules and
misc devices

Lionel Auroux

Modules

Userspace tools: rmmod and modprobe -r


Uses delete_module(2).
Won’t work if the kernel thinks the module is still in use:
The module is a dependency of another module.
A file descriptor is owned by this module.
...

Lionel Auroux Linux - Modules and misc devices 2017-09-29 11 / 14


Versions
Linux -
Modules and
misc devices

Lionel Auroux

Modules
A module has to be recompiled for each version fo the
kernel that you want to link it to.
If CONFIG_MODVERSIONS=y, a simple ABI consistency
check is performed over the prototypes of exported symbols.
When building a module, a CRC is computed for each
exported symbol, and stored in Module.symvers and in
the generated module.mod.c.
Versions checks can be bypassed, but it is not wise to do so.

Lionel Auroux Linux - Modules and misc devices 2017-09-29 12 / 14


modalias
Linux -
Modules and What peripherals does a module handle? modalias!
misc devices

Lionel Auroux cat /sys/devices/pci0000:00/0000:00:1d.0/usb4/[...]


Modules usb:v05E3p0608d8537dc09dsc00dp01ic09isc00ip00in00

usb is the device class, the rest is class-specific (vendor id,


device id, etc.)
A module defines a MODULE_ALIAS("usb:...") that
pattern-matches the modalias class-specific string.
When a new device is detected, the module loader is called
with the modalias string and loads the matching module.
See devices supported by modalias: less
/lib/modules/$(uname -r)/modules.alias
Example:

alias pci:v00001002d000099A4sv*sd*bc*sc*i* radeon


Lionel Auroux Linux - Modules and misc devices 2017-09-29 13 / 14
Parameters
Linux -
Modules and Parameters are typed key=value settings.
misc devices
They are defined as such:
Lionel Auroux

Modules static char *bde = "Test.";


static int votes = 1;
module_param(bde, charp, S_IRUGO)
module_param(votes, int, S_IRUGO)

Available types: bool, invbool, charp (memory is


auto-allocated), int, long, short, uint, ulong, ushort
Arrays are also supported:

module_param_array(name,type,num,perm);

The parameters will appear in


/sys/module/MODULE/parameters/PARAM.
The final field controls the permission value of this file.
Lionel Auroux Linux - Modules and misc devices 2017-09-29 14 / 14

You might also like