forked from fuwensun/LinuxKernelAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path笔记
More file actions
234 lines (180 loc) · 6.44 KB
/
笔记
File metadata and controls
234 lines (180 loc) · 6.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
注意:
1,
fun() 要写成 fun(void)
2,
$ grep -r KERN_SOH /usr/src/linux-headers-4.15.0-22
新版
#define KERN_SOH "\001" /* ASCII Start Of Header */
#define KERN_SOH_ASCII '\001'
#define KERN_EMERG KERN_SOH "0" /* system is unusable */
#define KERN_ALERT KERN_SOH "1" /* action must be taken immediately */
#define KERN_CRIT KERN_SOH "2" /* critical conditions */
#define KERN_ERR KERN_SOH "3" /* error conditions */
#define KERN_WARNING KERN_SOH "4" /* warning conditions */
#define KERN_NOTICE KERN_SOH "5" /* normal but significant condition */
#define KERN_INFO KERN_SOH "6" /* informational */
#define KERN_DEBUG KERN_SOH "7" /* debug-level messages */
#define KERN_DEFAULT KERN_SOH "d" /* the default kernel loglevel */
旧版
#define KERN_EMERG "<0>" /* system is unusable */
#define KERN_ALERT "<1>" /* action must be taken immediately */
3,
申明和编码要分开
//warning!
ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]
struct module *fmodule = find_module(name);
//ok!
struct module *fmodule = 0;
fmodule = find_module(name);
4,
内核符号的导出,以变量、函数为最小单位。
GPL模块可以导出“非”GPL符号(使用它的模块不会被传染GPL)。
GPL模块可以导出GPL符号(使用它的模块会被传染GPL)。
EXPORT_SYMBOL(symbol_A);
EXPORT_SYMBOL_GPL(symbol_A_GPL);
EXPORT_SYMBOL_GPL_FUTURE(symbol_A_GPL_FUTURE);
5,
struct pid 进程描述符
pid_t 进程号
6,内核线程创建
cmm_thread_ptr = kthread_run(cmm_thread, NULL, "cmmthread");
if (!IS_ERR(cmm_thread_ptr))
return 0;
6,
pid_t <–> struct pid *
----------------------------------
pid_t pid_vnr(struct pid *pid)
struct pid *find_pid_ns(int nr, struct pid_namespace *ns);
struct pid *find_vpid(int nr)
struct pid *find_get_pid(int nr)
struct pid * –> struct task_struct *
----------------------------------
struct pid *get_task_pid(sturct task_struct *task, enum pid_type);
struct task_struct *pid_task(struct pid *pid, enum pid_type);
struct task_struct *get_pid_task(struct pid *pid, enum pid_type)
7,
pid_t pid_nr(struct pid *pid) //获取全局进程号
pid_t pid_vnr(struct pid *pid) //获取局部进程号
8,
废弃:
sleep_on_timeout is deprecated
新的:
prepare_to_wait(q, &wait, TASK_INTERRUPTIBLE);
schedule_timeout - sleep until timeout
9,
头文件可以包含头文件,参考linux/completion.h:
---------------------------------------------------
#ifndef __LINUX_COMPLETION_H
#define __LINUX_COMPLETION_H
/*
* (C) Copyright 2001 Linus Torvalds
*
* Atomic wait-for-completion handler data structures.
* See kernel/sched/completion.c for details.
*/
#include <linux/wait.h> <-------包含.h文件,这completion.h是一个完整的接口
struct completion {
unsigned int done;
wait_queue_head_t wait; <----在wait.h中定义
};.
.
.
----------------------------------------------------
10.用内核API初始化链表节点
INIT_LIST_HEAD(&data.entry);
-----------------------------------
wait_queue_entry_t data;
task = kthread_run(my_function,NULL,"XXX");
kpid = get_task_pid(task,PIDTYPE_PID);
pidt = pid_vnr(kpid);
init_waitqueue_head(&head);
//data.entry.next = &data.entry; //手工初始化链表节点,不推荐!
INIT_LIST_HEAD(&data.entry); //<-------sfw**用内核API初始化链表节点,推荐!
init_waitqueue_entry(&data,task);
prepare_to_wait(&head,&data,130); //sfw** 初始化为empty后才能成功加入队列
------------------------------------
11.查找内核符号
cat /proc/kallsyms | grep xxx
12.内存池技术
struct page *alloc_pages(gfp_t gfp_mask, unsigned int order) //原型
void *mempool_alloc_pages(gfp_t gfp_mask, void *pool_data) //封装
void *kmem_cache_alloc(struct kmem_cache *cachep, gfp_t flags) //原型
void *mempool_alloc_slab(gfp_t gfp_mask, void *pool_data) //封装
void *kmalloc(size_t size, gfp_t flags) //原型
void *mempool_kmalloc(gfp_t gfp_mask, void *pool_data) //封装
mempool_t *mempool_create(int min_nr, mempool_alloc_t *alloc_fn,mempool_free_t *free_fn, void *pool_data)
min_nr: 创建元素个数
alloc_fn: 后台分配函数
free_fn: 后台释放函数
pool_data:传给后台函数的参数,如:cache,size。
13.
------------------------
class_create()/device_create() --> mknod /dev/my_char_dev c 246 0
------------------------
register_chrdev()
cdev_alloc()
cdev_add()
unregister_chardev()
cdev_del()
cdev
------------------------
__class_create()
__class_register()
class_destroy()
class_unregister()
------------------------
device_create()
device_register()
device_initialize()
device_add()
device_destroy()
device_unregister()
device_del()
14.
命令:
make
sudo insmod __module_address.ko
sudo rmmod __module_address.ko
sudo dmesg -c
make && sudo insmod __module_address.ko && sudo rmmod __module_address.ko && sudo dmesg -c
modinfo xxx.ko
sudo insmod xxx.ko && sudo dmesg -c
sudo rmmod xxx.ko && sudo dmesg -c
grep -r KERN_SOH /usr/src/linux-headers-4.15.0-22
cat /proc/kallsyms | grep test_module
错误代码:
//sfw** errno-base.h
#define EPERM 1 /* Operation not permitted */
#define ENOENT 2 /* No such file or directory */
#define ESRCH 3 /* No such process */
#define EINTR 4 /* Interrupted system call */
#define EIO 5 /* I/O error */
#define ENXIO 6 /* No such device or address */
#define E2BIG 7 /* Argument list too long */
#define ENOEXEC 8 /* Exec format error */
#define EBADF 9 /* Bad file number */
#define ECHILD 10 /* No child processes */
#define EAGAIN 11 /* Try again */
#define ENOMEM 12 /* Out of memory */
#define EACCES 13 /* Permission denied */
#define EFAULT 14 /* Bad address */
#define ENOTBLK 15 /* Block device required */
#define EBUSY 16 /* Device or resource busy */
#define EEXIST 17 /* File exists */
#define EXDEV 18 /* Cross-device link */
#define ENODEV 19 /* No such device */
#define ENOTDIR 20 /* Not a directory */
#define EISDIR 21 /* Is a directory */
#define EINVAL 22 /* Invalid argument */
#define ENFILE 23 /* File table overflow */
#define EMFILE 24 /* Too many open files */
#define ENOTTY 25 /* Not a typewriter */
#define ETXTBSY 26 /* Text file busy */
#define EFBIG 27 /* File too large */
#define ENOSPC 28 /* No space left on device */
#define ESPIPE 29 /* Illegal seek */
#define EROFS 30 /* Read-only file system */
#define EMLINK 31 /* Too many links */
#define EPIPE 32 /* Broken pipe */
#define EDOM 33 /* Math argument out of domain of func */
#define ERANGE 34 /* Math result not representable */