Introduction 我希望透過寫一個kernel module,讓user在user space去顯示kernel space某個變數值的實體記憶體位置與數值。 Note: 由於這程式僅用於測試,因此不會特別去考慮更好的寫法;會補充這點是由於看書有看到更好的寫法。 How to? Source Code 包含以下內容: 在init時,我建立一個字元裝置用以接受user space的操作,並初始hello_buf。 當使用者執行cat /dev/hello時,會觸發file_operations的open與release,這裡我只實做open的對應行為: dump_physical_address。 dump_physical_address則是取得hello_buf的實體記憶體位置並dump出來。 exit就是釋放所有用到的資源。 #include <linux/init.h> #include <linux/module.h> #include <linux/slab.h> #include <linux/string.h> #include <asm/io.h> #include <linux/fs.h> MODULE_LICENSE ( "Dual BSD/GPL" ) ; static char * hello_buf ; static int dump_physical_address ( struct file * filp, struct vm_area_struct * vma ) { unsigned long virt_addr ; phys_addr_t phy_addr ; printk ( "damn \n " ) ; virt_addr = ( unsigned long ) hello_buf ; printk ( "Hello kernel: %lx, value: %c \n " , virt_addr, hello_buf [ 0 ] ) ; phy_addr = virt_to_phys ( ( void * )...