Notes: Linux device driver

Kernel module:
@ lsmod: 顯示系統中載入的所有模組以及其相依關係,實際上是讀取/proc/modules的訊息後在進分析後的結果。

@Kernel中以經載入的module可以在/sys/module下查看其資訊。

@modprobe指令會將安裝該module所相依的module同時安裝,卸載時的指令為modprobe -r "module name",同樣會將相依的module一並卸載。

@modinfo <module name>: Get the module information, include author, description, supported parameter, and vermagic.

@The program structure of the linux kernel module:
- Module load function
- Module remove function
- Module license 
- Module parameter (*)
- Module export signal (*)
- Module author information (*)
*: Means may not required, but the module license is needed. The module load and remove function, in general, we need this to init and remove our module.

@When module init got error, the user program could print out the "perror" and do translate to make it readable.

@__init: 有__init標籤的API在LINKER階段會被放在.init.text區段而且在.initcall.init區段中保存一個fun pointer,當kernel初始化的時候會透過fun pointer呼叫__init API。在初始化完之後init區段會被釋放。

@/proc/kallsyms: 對應到kernel symbol table. 記錄了symbol以及symbol的記憶體位置, 可以從這找到export出去的變數

@When the kernel start to use the module, kernel will call try_module_get(dev->owner) to increase the counter, and use the module_put(dev->owner) to decrease the counter

@Due to char device has no disk file system as his upper layer, the file operation would provide by device driver directly

@

Linux File System and Device File System:
@About the open function:
int open(const char* pathname, int flags);
int open(const char* pathname, int flags, mode_t mode);
目前提供兩種形式的open函數,其中flag參數可以參考下表:
O_RDONLY: Open a file with Read Only attribute.
O_WRONLY: Open a file with Write Only attribute.
O_RDWR: Open a file with R/W attribute.
O_APPEND: Open a file with append attribute.
O_CREAT: Create a file.
O_EXEC: It will cause an error when using the O_CREAT, but the file exist.
O_NOBLOCK: Open a file with Non-Blocking method.
O_TRUNC: Delete the file if the file exist.

如果使用了O_CREAT flag後,那就表示使用第二個function,此時就要看一下mode參數了:
S_IRUSR: User擁有Read權限。
S_IWUSR: User擁有Write權限
S_IXUSR: User擁有Execute權限
S_IRWXU:  User擁有Read, Write, Execute權限

S_IRGRP: Group擁有Read權限
S_IWGRP: Group擁有Write權限
S_IXGRP: Group擁有Execute權限
S_IRWXG:  Group擁有Read, Write, Execute權限

S_IROTH: Others擁有Read權限
S_IWOTH: Others擁有Write權限
S_IXOTH: Others擁有Execute權限
S_IRWXO:  Others擁有Read, Write, Execute權限

S_ISUID: 設定User execute ID.
S_ISGID: 設定Group execute ID.


@About udev:
策略和機制,Mechanism and Policy,兩者必須分開實作以提高系統彈性。Mechanism通常是指變動性小,相對於Policy而言是固定的部分。而Policy則是變動較大,相較於Mechanism而言是可以經常性改動的部分。
    好比在CPU中提供了Security Mechanism,機制中需使用到timer和interrupt,此時的timer和interrupt就是機制。該如何實現timer和interrupt就是Policy的部分,就像是timer要是定多長的時間,Interrupt如何觸發。
     這兩個名詞也和Kernel扯上關係,在Linux中,有著User Space和Kernel Space的分別,在早期Linux 2.4時期的裝置檔案系統(Device File System)使用devfs,這個檔案系統是被加入到Kernel Space的,之後在Linux 2.6時被udev取代了,而udev移到了User Space了。被取代的理由也跟Mechanism和Policy有關係,Mechanism通常實作在Kernel Space,而Policy則是在User Space。由於發現devfs實作的部分可以被抽離到User Space上去,因此才被直接實作在User Space的udev給取代掉,避免在Kernel Space中有經常性的大更改。

@About sysfs:
和proc fs同為虛擬的file system,儲存在ram上。


留言

熱門文章