2007年4月21日 星期六

如何在Linux 2.6的kernel上,編譯出module?

----先進行環境設定(以後可以略過這一步驟)
1.先安裝libncurses5-dev套件。
#>apt-get install libncurses5-dev
2.下載kernel source至/usr/src目錄中。
#>cd /usr/src
#>ftp ftp.kernel.org
username:anonymous
password:(void)
ftp> cd pub/linux/kernel/v2.6/
ftp> binary
ftp> get linux-2.6.xxxxx.tar.gz
ftp> bye
3.將kernel source解開。
#>tar xzvf linux-2.6.xxxxx.tar.gz
4.編譯kernel source。
#>cd linux-2.6.xxxxx
#>make menuconfig
#>make
4.做一個Symbolic Link的目錄
#>cd /lib/modules/2.6.xxxxx/
#>ln -s /usr/src/linux-2.6.xxxxx build

----撰寫及安裝程式
5.撰寫模組程式,別忘了下列兩個最基本的函式。
int init_module(void);
void cleanup_module(void);
.....
6.撰寫Makefile檔。(使用2.6版kernel source的編譯設定)
obj-m += xxxxx.o

all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
7.編譯程式。
#>make
8.它將會產出xxxxx.ko(xxxxx.ko是Linux 2.6版模組預設的附檔名,在Linux 2.4版時是xxxxx.o)。
9.將其與Linux Kernel連結。
#>insmod xxxxx.ko
10.檢查其是否確實存在。
#>lsmod | grep --color "xxxxx"
11.將其移出Linux Kernel。
#>rmmod xxxxx

printk( ) 的Priority

寫Driver時,可以以printk( )來顯示訊息進行debug,當其priority數值小於console_loglevel時,就會被顯示出來。其相關的priority數值定義如下:
#define KERN_EMERG "<0>" /* system is unusable */
#define KERN_ALERT "<1>" /* action must be taken immediately */
#define KERN_CRIT "<2>" /* critical conditions */
#define KERN_ERR "<3>" /* error conditions */
#define KERN_WARNING "<4>" /* warning conditions */
#define KERN_NOTICE "<5>" /* normal but significant condition */
#define KERN_INFO "<6>" /* informational */
#define KERN_DEBUG "<7>" /* debug-level messages */

2007年4月14日 星期六

2007年4月11日 星期三

Precise Exception與Imprecise Exception的差異

Exception可分為二類:一者為precise exception,另一者為imprecise exception。
.precise exception指的是當某一個指令(會觸發exception的指令)仍在處理器pipeline執行的過程中,即發出change control flow的event,並且進入exception handler。Ex:指令格式錯誤時,所發出的Instruction Exception。
.imprecise exception指的是當某一個指令(會觸發exception的指令)在處理器pileline執行完後,才發出change control flow的event,然後才進入exception handler中執行。Ex:在讀取資料時,所發生的bus error。

2007年4月10日 星期二

有關Cache的read/write through/back/allocate的意義

所謂的read/write cache的hit/miss,指的是CPU要read/write某一位址的資料,若此時cache裡的資料剛好是該位址的資料,則稱為cache hit,若此時cache裡的資料不是該位址的資料,則稱為cache miss。
.當cache hit時,若CPU要讀取某一位址的資料時,會直接從cache中讀取資料。
.當cache miss時,若CPU要讀取某一位址的資料時,又可分為二種方式:一種是read through,這種方式會直接將資料從主記憶體端讀進CPU;另一種是no read through,這種方式會先將資料從主記憶體端讀進cache,然後再從cache讀進CPU。
.當cache hit時,若CPU要寫入資料到某一位址時,可分為二種方式:一種是write through,此種方式資料會立刻寫到cache及主記憶體中;另一種是write back,此種方式會先將資料寫入cache中,然後再將同一位址的資料整批一起寫入主記憶體中(非立即寫入)。
.當cache miss時,若CPU要寫入資料到某一位址時,可分為二種方式:一種是no write allocate,此種方式會直接將資料寫到主記憶體中,不會再從記憶體中載入到cache,另一種方式是write allocate,此種方式會先將資料從主記憶體中載入到cache,然後再依cache hit的規則,將資料寫出。