没有合适的资源?快使用搜索试试~ 我知道了~
LInux内核模块开发
资源推荐
资源详情
资源评论
The Linux Kernel Module Programming Guide
Peter Jay Salzman, Michael Burian, Ori Pomerantz, Bob Mottram, Jim Huang
October 8, 2023
Peter Jay Salzman, Michael Burian,
Ori Pomerantz, Bob Mottram,
Jim Huang
Contents
1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.1 Authorship . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.2 Acknowledgements . . . . . . . . . . . . . . . . . . . . . . 4
1.3 What Is A Kernel Module? . . . . . . . . . . . . . . . . . 5
1.4 Kernel module package . . . . . . . . . . . . . . . . . . . 5
1.5 What Modules are in my Kernel? . . . . . . . . . . . . . . 5
1.6 Is there a need to download and compile the kernel? . . . 6
1.7 Before We Begin . . . . . . . . . . . . . . . . . . . . . . . 6
2 Headers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
4 Hello World . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
4.1 The Simplest Module . . . . . . . . . . . . . . . . . . . . 8
4.2 Hello and Goodbye . . . . . . . . . . . . . . . . . . . . . . 12
4.3 The __init and __exit Macros . . . . . . . . . . . . . . 13
4.4 Licensing and Module Documentation . . . . . . . . . . . 14
4.5 Passing Command Line Arguments to a Module . . . . . 15
4.6 Modules Spanning Multiple Files . . . . . . . . . . . . . . 18
4.7 Building modules for a precompiled kernel . . . . . . . . . 19
5 Preliminaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
5.1 How modules begin and end . . . . . . . . . . . . . . . . . 21
5.2 Functions available to modules . . . . . . . . . . . . . . . 22
5.3 User Space vs Kernel Space . . . . . . . . . . . . . . . . . 23
5.4 Name Space . . . . . . . . . . . . . . . . . . . . . . . . . . 23
5.5 Code space . . . . . . . . . . . . . . . . . . . . . . . . . . 24
5.6 Device Drivers . . . . . . . . . . . . . . . . . . . . . . . . 24
6 Character Device drivers . . . . . . . . . . . . . . . . . . . . . . . 26
6.1 The file_operations Structure . . . . . . . . . . . . . . . . 26
6.2 The file structure . . . . . . . . . . . . . . . . . . . . . . . 28
2
6.3 Registering A Device . . . . . . . . . . . . . . . . . . . . . 28
6.4 Unregistering A Device . . . . . . . . . . . . . . . . . . . 30
6.5 chardev.c . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
6.6 Writing Modules for Multiple Kernel Versions . . . . . . . 34
7 The /proc File System . . . . . . . . . . . . . . . . . . . . . . . . 35
7.1 The proc_ops Structure . . . . . . . . . . . . . . . . . . . 37
7.2 Read and Write a /proc File . . . . . . . . . . . . . . . . 37
7.3 Manage /proc file with standard filesystem . . . . . . . . 39
7.4 Manage /proc file with seq_file . . . . . . . . . . . . . . . 42
8 sysfs: Interacting with your module . . . . . . . . . . . . . . . . . 45
9 Talking To Device Files . . . . . . . . . . . . . . . . . . . . . . . 48
10 System Calls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
11 Blocking Processes and threads . . . . . . . . . . . . . . . . . . . 68
11.1 Sleep . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
11.2 Completions . . . . . . . . . . . . . . . . . . . . . . . . . 74
12 Avoiding Collisions and Deadlocks . . . . . . . . . . . . . . . . . 76
12.1 Mutex . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
12.2 Spinlocks . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
12.3 Read and write locks . . . . . . . . . . . . . . . . . . . . . 79
12.4 Atomic operations . . . . . . . . . . . . . . . . . . . . . . 80
13 Replacing Print Macros . . . . . . . . . . . . . . . . . . . . . . . 82
13.1 Replacement . . . . . . . . . . . . . . . . . . . . . . . . . 82
13.2 Flashing keyboard LEDs . . . . . . . . . . . . . . . . . . . 84
14 Scheduling Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
14.1 Tasklets . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
14.2 Work queues . . . . . . . . . . . . . . . . . . . . . . . . . 88
15 Interrupt Handlers . . . . . . . . . . . . . . . . . . . . . . . . . . 89
15.1 Interrupt Handlers . . . . . . . . . . . . . . . . . . . . . . 89
15.2 Detecting button presses . . . . . . . . . . . . . . . . . . . 90
15.3 Bottom Half . . . . . . . . . . . . . . . . . . . . . . . . . 93
16 Crypto . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
16.1 Hash functions . . . . . . . . . . . . . . . . . . . . . . . . 97
16.2 Symmetric key encryption . . . . . . . . . . . . . . . . . . 98
17 Virtual Input Device Driver . . . . . . . . . . . . . . . . . . . . . 102
18 Standardizing the interfaces: The Device Model . . . . . . . . . . 114
19 Optimizations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
19.1 Likely and Unlikely conditions . . . . . . . . . . . . . . . 116
19.2 Static keys . . . . . . . . . . . . . . . . . . . . . . . . . . 116
20 Common Pitfalls . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
20.1 Using standard libraries . . . . . . . . . . . . . . . . . . . 121
20.2 Disabling interrupts . . . . . . . . . . . . . . . . . . . . . 121
21 Where To Go From Here? . . . . . . . . . . . . . . . . . . . . . . 121
1 Introduction
The Linux Kernel Module Programming Guide is a free book; you may repro-
duce and/or modify it under the terms of the Open Software License, version
3.0.
This book is distributed in the hope that it would be useful, but without
any warranty, without even the implied warranty of merchantability or fitness
for a particular purpose.
The author encourages wide distribution of this book for personal or com-
mercial use, provided the above copyright notice remains intact and the method
adheres to the provisions of the Open Software License. In summary, you may
copy and distribute this book free of charge or for a profit. No explicit permis-
sion is required from the author for reproduction of this book in any medium,
physical or electronic.
Derivative works and translations of this document must be placed un-
der the Open Software License, and the original copyright notice must remain
intact. If you have contributed new material to this book, you must make
the material and source code available for your revisions. Please make revi-
sions and updates available directly to the document maintainer, Jim Huang
<jserv@ccns.ncku.edu.tw>. This will allow for the merging of updates and
provide consistent revisions to the Linux community.
If you publish or distribute this book commercially, donations, royalties,
and/or printed copies are greatly appreciated by the author and the Linux
Documentation Project (LDP). Contributing in this way shows your support for
free software and the LDP. If you have questions or comments, please contact
the address above.
1.1 Authorship
The Linux Kernel Module Programming Guide was initially authored by Ori
Pomerantz for Linux v2.2. As the Linux kernel evolved, Ori’s availability to
maintain the document diminished. Consequently, Peter Jay Salzman assumed
the role of maintainer and updated the guide for Linux v2.4. Similar constraints
arose for Peter when tracking developments in Linux v2.6, leading to Michael
Burian joining as a co-maintainer to bring the guide up to speed with Linux v2.6.
Bob Mottram contributed to the guide by updating examples for Linux v3.8 and
later. Jim Huang then undertook the task of updating the guide for recent Linux
versions (v5.0 and beyond), along with revising the LaTeX document.
1.2 Acknowledgements
The following people have contributed corrections or good suggestions:
Andy Shevchenko, Arush Sharma, Benno Bielmeier, Bob Lee, Brad Baker,
Che-Chia Chang, Chih-En Lin, Chih-Hsuan Yang, Chih-Yu Chen, Ching-Hua
(Vivian) Lin, Chin Yik Ming, Cyril Brulebois, Daniele Paolo Scarpazza, David
剩余122页未读,继续阅读
资源评论
zdlinux
- 粉丝: 151
- 资源: 13
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功