Upload
scott-edwards
View
217
Download
0
Embed Size (px)
Citation preview
Kernel Modules
Giving your Linux more pop since 1995
History
• Introduced in Linux version 1.2 (1995)
• At the time, drivers were already designed to be somewhat modular, so not a lot of work was needed to revamp one driver for use as a LKM. There are a lot of drivers, though...
• By 2000, most drivers and “module-able” things are converted.
What are Modules
• A Module is a object file that contains code to extend the functionality of the base kernel.
• Modules are used to add support for new hardware and file systems.
• Also used to add new system calls and executable interpreters.
Why are they Important?
• If modules did not exist then adding new features to the kernel would be nearly impossible.
• Remember the systemCall() project???
Pros and Cons
• Pros: – Time savings – Portability– Memory savings– Security (PAM)
• Cons– No safeguards– Security (rootkits)
Modules vs. Applications
• Modules are really a part of the kernel and so run in kernel space.
• Apps just do their tasks and end. Modules sit in the kernel space waiting to be run.
• Apps can be linked to outside libraries; modules can only "see" in the kernel and other loaded modules.
Module Loading
• Kernel Symbol Table: references kernel functions that modules can use.
• When loaded, unresolved symbols in module are linked to system table
• EXPORT_SYMBOL() allows specified symbol to be used by kernel and other modules.
• Use cat /proc/kallsyms to get a list of all exported kernel symbols.
Image from: http://www.xml.com/ldd/chapter/book/ch02.html
Module HOW-TO
• Some useful commands– insmod insert module into kernel
– rmmod remove kernel module
– lsmod show currently loaded modules
• The above work, but are dumb. Instead, use modprobe [-r] which is aware of module dependencies and config settings.
• Use depmod to find module dependencies. File location: /lib/modules/version/modules.dep
Building the Module
• The steps taken depend on whether you want your module in the kernel tree or not
• Living in the tree– The LKD book explains this well with the Fish
Master example (pg 281)
– Quickly: put module in drivers/ directory, edit/create Makefile so module is built, make Kconfig file if you have config options for module.
Example Files
config SOME_OPTIONtristate “Enable some option”default nhelp A file like this must be with your module.
Go to Kconfig above this and add: source “this/directory/Kconfig”
Kconfig:
Makefile:#in module directoryobj-$(CONFIG_SOME_OPTION) += myModule.omyModule-objs := leroy.o jenkins.o
#edit makefile above module directoryobj-m += directory/
• Makefile goes with module source.
• The difference is in the compilation:– make -C /kernel/source/location SUBDIRS=$PWD modules
– /kernel/source/location is the location of your kernel source tree
– Be sure that your kernel source is configured and built
And now for an example...
Building Outside Tree
#similar to before#Note the := rather than += on next lineobj-m := hellomod.ohellomod-objs := goodbye.o hi.o
References
• The Linux Kernel Module Programming Guide
http://www.faqs.org/docs/kernel/
• Wikipedia: Linux Kernel
http://en.wikipedia.org/wiki/Linux_kernel
• Wikipedia: Linux Modules
http://en.wikipedia.org/wiki/Module_(Linux)
• Linux Device Drivers: Building and Running Modules
http://www.xml.com/ldd/chapter/book/ch02.html
• Linux Loadable Kernel Module HOWTO
http://www.tldp.org/HOWTO/Module-HOWTO/
• Linux PAM page
http://www.kernel.org/pub/linux/libs/pam/whatispam.html
• Linux Kernel Development. Ch. 16: Modules. pp.279-289
• Linux Kernel Cross-Reference
http://lxr.linux.no/