Table of Contents
Foreword..............................................................................................................................................................1
1. Authorship...........................................................................................................................................1
2. Versioning and Notes...........................................................................................................................1
3. Acknowledgements..............................................................................................................................1
Chapter 1. Introduction......................................................................................................................................2
1.1. What Is A Kernel Module?...............................................................................................................2
1.2. How Do Modules Get Into The Kernel?...........................................................................................2
1.2.1. Before We Begin.....................................................................................................................3
Chapter 2. Hello World......................................................................................................................................5
2.1. Hello, World (part 1): The Simplest Module....................................................................................5
2.1.1. Introducing printk().................................................................................................................6
2.2. Compiling Kernel Modules..............................................................................................................6
2.3. Hello World (part 2)..........................................................................................................................7
2.4. Hello World (part 3): The __init and __exit Macros........................................................................8
2.5. Hello World (part 4): Licensing and Module Documentation..........................................................9
2.6. Passing Command Line Arguments to a Module...........................................................................10
2.7. Modules Spanning Multiple Files...................................................................................................12
2.8. Building modules for a precompiled kernel....................................................................................14
Chapter 3. Preliminaries..................................................................................................................................16
3.1. Modules vs Programs......................................................................................................................16
3.1.1. How modules begin and end.................................................................................................16
3.1.2. Functions available to modules.............................................................................................16
3.1.3. User Space vs Kernel Space..................................................................................................17
3.1.4. Name Space...........................................................................................................................17
3.1.5. Code space.............................................................................................................................18
3.1.6. Device Drivers.......................................................................................................................18
Chapter 4. Character Device Files..................................................................................................................21
4.1. Character Device Drivers................................................................................................................21
4.1.1. The file_operations Structure................................................................................................21
4.1.2. The file structure...................................................................................................................22
4.1.3. Registering A Device............................................................................................................22
4.1.4. Unregistering A Device.........................................................................................................23
4.1.5. chardev.c................................................................................................................................23
4.1.6. Writing Modules for Multiple Kernel Versions....................................................................26
Chapter 5. The /proc File System....................................................................................................................28
5.1. The /proc File System.....................................................................................................................28
Chapter 6. Using /proc For Input....................................................................................................................31
6.1. Using /proc For Input......................................................................................................................31
Chapter 7. Talking To Device Files.................................................................................................................35
7.1. Talking to Device Files (writes and IOCTLs)}..............................................................................35
The Linux Kernel Module Programming Guide
i