Hello world module Hello world hello c include

  • Slides: 22
Download presentation
“Hello world” module 潘仁義

“Hello world” module 潘仁義

Hello, world! 原始程式 hello. c (進一步說明可見後面) #include <linux/printk. h> int init_module(void) { printk(“<1>Hello, wordn”);

Hello, world! 原始程式 hello. c (進一步說明可見後面) #include <linux/printk. h> int init_module(void) { printk(“<1>Hello, wordn”); return 0; } void cleanup_module(void) { printk(“<1>Goodby cruel worldn”); } 編譯模組 Makefile obj-m : = hello. o 下命令 make -C /lib/modules/`uname -r`/build M=`pwd` modules uname –r pwd

資源的運用 大部分驅動程式的 作大概就在讀寫I/O port或I/O memory(統稱 I/O region) 核心都應該保證驅動程式能獨占存取其I/O port以免受到其他驅動程式 的干擾 /proc/ioports與/proc/iomem檔案可取得以註冊的系統資源 檢查, 取得特定一段的I/O region

資源的運用 大部分驅動程式的 作大概就在讀寫I/O port或I/O memory(統稱 I/O region) 核心都應該保證驅動程式能獨占存取其I/O port以免受到其他驅動程式 的干擾 /proc/ioports與/proc/iomem檔案可取得以註冊的系統資源 檢查, 取得特定一段的I/O region int check_region(unsigned long start, unsigned long len); 用來檢查某段範圍的I/O位址是否被佔用. struct resource *request_region(unsigned long start, unsigned long len , char *nam); 要求註冊該位址區. 若核心同意, 此函式會回傳一個non-NULL指標. 取得, 釋放特定 一段的I/O memory region 取得, 釋放特定一段的I/O memory region int check_mem_region(usigned long start, unsigned long len); int request_mem_region(unsigned long start, unsigned long len, char *name); int release_mem_region(unsigned long start, unsigned long len);

□I/O port: 可從/proc/iomem檔案取得I/O memory的資訊

□I/O port: 可從/proc/iomem檔案取得I/O memory的資訊

rmmod [root @test /root]# rmmod modules_name 參數說明: 範例: [root @test /root]# rmmod 8139 too

rmmod [root @test /root]# rmmod modules_name 參數說明: 範例: [root @test /root]# rmmod 8139 too 輸入『 rmmod 模組名稱』就可以移除模組

範例 hello. c //#define MODULE //#include <linux/kernel. h> #include <linux/module. h> int init_module(void) {

範例 hello. c //#define MODULE //#include <linux/kernel. h> #include <linux/module. h> int init_module(void) { printk(“<1>Hello, wordn”); return 0; } void cleanup_module(void) { printk(“<1>Goodby cruel worldn”); } 模組由insmod載入後模組就連接到核心因而能存取核心的公共符號 public symbol( 函式與變數 ). printk()函式位於linux核心內部, 類似標準c函式庫printf(). 核心需要自己的列印函式不能求助於c函式庫. <1>代表訊息的優先等級 數值越低等級越高. 目前都採用 KERN_ALERT …這些來替代 #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 */ #define KERN_INFO "<6>" /* informational */ #define KERN_DEBUG "<7>" /* debug-level messages */

範例 hello. c □指令 # insmod. /hello. o 顯示 Hello, world # rmmod hello

範例 hello. c □指令 # insmod. /hello. o 顯示 Hello, world # rmmod hello 顯示 Goodbye cruel world □訊息會被紀錄在 /var/log /message 檔案. 教材 http: //www. oreilly. com. tw/bookcode/ldd 2 -samples-1. 0. 2. tar. gz https: //github. com/martinezjavier/ldd 3. git