最終更新:2017-06-16 (金) 15:42:23 (2499d)  

Linux/カーネルモジュール/開発

ドキュメント

kernel.org

LXR (Linux Cross Reference)

PDF

CentOS Wiki

メモ

  • Web上の日本語の情報は古いのが多い。

参考図書

  •  


     


Linuxカーネルのバージョンによる違い

  • Linux 2.4の時代の情報が結構引っかかるので一応メモ。

Linux 2.6

Linux 2.4

ビルドの仕方

デバイスの種類

基本動作

ロード・アンロード時に呼ばれる関数

上のを別名にするマクロ

パラメータ

マクロ

modinfoした時に表示される情報を設定

ヘッダファイル

関数

変数

  • When writing kernel code, even the smallest module will be linked against the entire kernel, so this is definitely an issue. The best way to deal with this is to declare all your variables as static and to use a well-defined prefix for your symbols. By convention, all kernel prefixes are lowercase. If you don't want to declare everything as static, another option is to declare a symbol table and register it with a kernel.

チュートリアル

下記をhello-1.cとして保存

  • /*  
     *  hello-1.c - The simplest kernel module.
     */
    #include <linux/module.h>	/* Needed by all modules */
    #include <linux/kernel.h>	/* Needed for KERN_INFO */
    
    int init_module(void)
    {
    	printk(KERN_INFO "Hello world 1.\n");
    
    	/* 
    	 * A non 0 return means init_module failed; module can't be loaded. 
    	 */
    	return 0;
    }
    
    void cleanup_module(void)
    {
    	printk(KERN_INFO "Goodbye world 1.\n");
    }

Makefileを作る

  • obj-m += hello-1.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
  • the build process for external loadable modules is now fully integrated into the standard kernel build mechanism. (kbuild)

できたファイルの確認

  • modinfo hello-1.ko

モジュールのロード

  • insmod hello-1.ko

ロード後の確認

  • lsmod
    dmesg

make *configに出す設定

Eclipseでコードを読む

関連

参考