Linux Kernel Structure
January 25, 2002
Yongguang Zhang
Today's Lecture
- Kernel Responsibility
- Kernel Organization
- Kernel Modules
- Project 2
Q&A
- Have everybody turned in their project 1?
- Any problem with project 1?
Running User-Mode-Linux
- Kill Your Processes if your UML hangs
- Check with ps -x | grep ./linux
- Kill each process by kill pid
- Sometimes you need to kill -9 pid
Kernel Responsibility
- Kernel Mode and User Mode
- Modern CPU architecture:
- Supervisor mode: execute any instructions
- User mode: execute non-privileged instructions, in bounded address space
- Kernel's Responsibilities
- Managing machine's resources (hardware, data structures)
Kernel/User Space Partition
- Strict separation of responsibility
- Strictly-defined interface between kernel and user space
- Next lecture: kernel/user space interaction
Kernel Organization
- Monolithic kernel
- Everything in a single executable piece of software
- Every data structure/function is available to every other function
- But it does not imply that Linux kernel has no structure
- Q: What is the name of the other type of kernel structure
(that you have learned in CS372)?
Monolithic vs Microkernel
- Problem with Monolithic Kernel
- Kernel Getting Bigger and Bigger
- New functionality
- New hardware
- Linux Solution: Module
- Unessential kernel functions implemented as a dynamic loadable modules
Basic Concept
- Modular Programming
- Compiling and Linking
- Dynamic loading vs static linking
- Symbol relocation
- Q: what is symbol?
- Think examples of user space
- Shared library (libxxx.so vs libxxx.a)
- Dynamic linker/loader (ld.so)
Linux Modules
- How Module Works
- Follow some conventions (i.e. special macros)
- Compiled into an .o file
- With special compiler option
- Loaded into a running kernel (if so demanded)
- Dynamic loading (symbol relocation)
- Unloaded it later (if unused)
Symbols
- Kernel Symbol Tables
- The list of symbols that are made available for reference by modules
- To include a symbol in the list, call
- Module Symbol Tables
- All global symbols are exported
- To find out the list of symbols
Module Dependency
- If A references symbols in B, B must be loaded first
- Tools for helping dependency
/sbin/depmod -- find out the dependency for all modules under
/lib/modules/version/
Module Commands
- /sbin/insmod - load a module into the kernel
- /sbin/rmmod - unload a module
- /sbin/lsmod - list all the loaded module
- /sbin/modprobe - automatically load required module
according to the dependency
Writing Module
- Includes
#include <linux/kernel.h>
#include <linux/module.h>
- Compile option:
gcc -c -Ilinux/include -Wall -D__KERNEL__ -DMODULE foo.c
- Functions:
#ifdef MODULE
int init_module() { .... }
void cleanup_module() { .... }
#endif
More about Modules
- Kernel must be configured to support kernel module
- Kernel must be compiled first!
- You will need the linux/include directory
in your kernel source tree
- Symbols, version number
- Many kernel modules can be statically linked
- You can configure a module to be dynamic loadable
Homework
- Read Appendix B (Understanding Linux Kernel)
- Explore module configuration
- Do project 2
Project 2
- Do Exercise 4 in "Kernel Projects for Linux", Page 97-106
- Follow the book by yourself
- Write the code and test it out
- Due: Feb 1, Fri (one week from today)
© 2002 Yongguang Zhang