LECTURE 1 ¥«¡¼¥Í¥ë¥â¥¸¥å¡¼¥ë¤òºî¤ë

¡û ¤Ï¤¸¤á¤Ë

Linux® ¤Ç¤Î¥Ç¥Ð¥¤¥¹¥É¥é¥¤¥Ð¤Ï¡¢¥«¡¼¥Í¥ë¤ËÀÅŪ¤Ë¥ê¥ó¥¯¤¹¤ë·Á¼°¤È¡¢Æ°Åª¤Ë¥í¡¼¥É¤¹¤ë¤³¤È¤¬¤Ç¤­¤ë¥â¥¸¥å¡¼¥ë·Á¼°¤ÇºîÀ®¤¹¤ë¤³¤È¤¬¤Ç¤­¤Þ¤¹¡£ËÜ Web¤Ç¤Ï¥«¡¼¥Í¥ë¥â¥¸¥å¡¼¥ë¤ÎºîÀ®ÊýË¡¤òÀâÌÀ¤·¤Þ¤¹¡£

"C ¸À¸ì¤Ë¤è¤ë¥×¥í¥°¥é¥ß¥ó¥°¤Ç¤Ï main() ´Ø¿ô¤«¤é¼Â¹Ô¤µ¤ì¤ë" ¤È¤¤¤¦Ìó«»ö¤¬¤¢¤ë¤è¤¦¤Ë¡¢¥â¥¸¥å¡¼¥ë¥×¥í¥°¥é¥ß¥ó¥°¤Ë¤â¤³¤¦¤·¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¤È¤¤¤¦Ìó«»ö¤¬¤¢¤ê¤Þ¤¹¡£

¤³¤³¤Ç¤Ï¡¢´Êñ¤Ê¥â¥¸¥å¡¼¥ë¤òºî¤Ã¤Æ¤ß¤ë¤³¤È¤Ç¥â¥¸¥å¡¼¥ë¥×¥í¥°¥é¥ß¥ó¥°¤ËºÇÄã¸ÂɬÍפÊÃ챤òÀâÌÀ¤·¤Þ¤¹¡£

¡û ¥µ¥ó¥×¥ë¥×¥í¥°¥é¥à

¥«¡¼¥Í¥ë¤Ø¤Î¥í¡¼¥É¤ª¤è¤Ó¥«¡¼¥Í¥ë¤«¤é¤Î¥¢¥ó¥í¡¼¥É¤Î¤ß¤ËÂбþ¤·¤¿¥µ¥ó¥×¥ë¥×¥í¥°¥é¥à message.c ¤Ç¤¹¡£¤Ï¤¸¤á¤Ë ¤Î¥Ú¡¼¥¸¤«¤é¡¢Á´¤Æ¤Î¥µ¥ó¥×¥ë¥×¥í¥°¥é¥à¤ò¸Ç¤á¤¿ tar ¥Ü¡¼¥ë¤ò¥À¥¦¥ó¥í¡¼¥É¤¹¤ë¤³¤È¤â¤Ç¤­¤Þ¤¹¡£

¥«¡¼¥Í¥ë¥â¥¸¥å¡¼¥ë¤ò¥³¥ó¥Ñ¥¤¥ë¤¹¤ëºÝ¤Ë¤Ï¡¢linux/module.h ¤ò¥¤¥ó¥¯¥ë¡¼¥É¤·¡¢module.h ¤Î¥¤¥ó¥¯¥ë¡¼¥É¤è¤ê¤âÁ°¤Ë MODULE ¥Þ¥¯¥í¤òÄêµÁ¤·¤Ê¤±¤ì¤Ð¤¤¤±¤Þ¤»¤ó¡£

#define MODULE
#include <linux/module.h>

¤ÈÀèƬ¤Ëµ­½Ò¤¹¤ë¤«¡¢¥³¥ó¥Ñ¥¤¥ë»þ¤Ë -DMODULE ¤È¤·¤ÆÄêµÁ¤·¤Æ²¼¤µ¤¤¡£ËÜ Web ¤Ç¤Ï¥³¥ó¥Ñ¥¤¥ë¥¹¥¤¥Ã¥Á¤ò»ÈÍѤ·¤Þ¤¹¡£

gcc -DMODULE -c message.c

¤Þ¤¿¡¢SMP ¥·¥¹¥Æ¥àÍѤ˥³¥ó¥Ñ¥¤¥ë¤¹¤ëºÝ¤Ï¡¢¥«¡¼¥Í¥ë¥Ø¥Ã¥À¤ò¥¤¥ó¥¯¥ë¡¼¥É¤¹¤ëÁ°¤Ë __SMP__ ¥Þ¥¯¥í¤òÄêµÁ¤¹¤ëɬÍפ¬¤¢¤ê¤Þ¤¹¡£

ºîÀ®¤µ¤ì¤¿ message.o ¤¬¥«¡¼¥Í¥ë¥â¥¸¥å¡¼¥ë¤Ç¤¹¡£¥«¡¼¥Í¥ë¤Ø¤ÎÁȹþ¤ß¤Ïinsmod ¥³¥Þ¥ó¥É¤ò¼Â¹Ô¤·¤Þ¤¹¡£

/sbin/insmod message.o

²¿¤âɽ¼¨¤µ¤ì¤Ê¤±¤ì¤Ð¡¢¥«¡¼¥Í¥ë¤Ø̵»öÁȹþ¤Þ¤ì¤¿¤Ï¤º¤Ç¤¹¡£¥«¡¼¥Í¥ë¥í¥°¤ò¸«¤ë¤È

<6>module [message.o] : loaded  into kernel

¤ÎÍͤËɽ¼¨¤µ¤ì¤Æ¤¤¤ë¤È»×¤¤¤Þ¤¹ ( ÉáÄÌ¤Ï /var/log/messages ¤¢¤¿¤ê¤Ë½ÐÎϤµ¤ì¤ë¤È»×¤¤¤Þ¤¹¤¬¡¢É®¼Ô¤Î´Ä¶­¤Ç¤Ï klogd ¤ËľÀܽÐÎÏ¥Õ¥¡¥¤¥ë¤ò»ØÄꤷ¤Æ¤¤¤ë¤¿¤á¡¢¤½¤Î¥Õ¥¡¥¤¥ë¤Ë½ÐÎϤµ¤ì¤Æ¤¤¤Þ¤¹ ¡Ë¡£¤³¤Î¾õÂÖ¤Ç /sbin/lsmod ¤ò¼Â¹Ô¤¹¤ë¤È¡¢ÁȤ߹þ¤Þ¤ì¤Æ¤¤¤ë¥â¥¸¥å¡¼¥ë¤Î°ìÍ÷¤Ë message ¤¬Æþ¤Ã¤Æ¤¤¤ë¤È»×¤¤¤Þ¤¹¡£

¥«¡¼¥Í¥ë¤«¤é¥â¥¸¥å¡¼¥ë¤ò¥¢¥ó¥í¡¼¥É¤¹¤ë¾ì¹ç¤Ë¤Ï¡¢rmmod ¥³¥Þ¥ó¥É¤ò¼Â¹Ô¤·¤Þ¤¹¡£

/sbin/rmmod message.o

¤³¤Î¾ì¹ç¡¢¥«¡¼¥Í¥ë¥í¥°¤Ë¤Ï

<6>module [message.o] : removed from kernel

¤ÎÍͤËɽ¼¨¤µ¤ì¤Þ¤¹¡£

--- message.c ---

    1    #include <linux/module.h> /* module ºîÀ®¤Ë¤Ïɬ¿Ü */
    2    #include <linux/kernel.h> /* printk */
    3    
    4    MODULE_LICENSE( "GPL" );
    5    
    6    static char* msg = "module [message.o]";
    7    
    8    
    9    /*
   10     * ¥â¥¸¥å¡¼¥ë¤Î½é´ü½èÍý
   11     * insmod »þ¤Ë¸Æ¤Ð¤ì¤ë
   12     */
   13    int 
   14    init_module( void )
   15    {
   16        printk( KERN_INFO "%s : loaded  into kernel\n", msg );
   17    
   18        /* ¥â¥¸¥å¡¼¥ë½é´ü²½¤ËÀ®¸ù¤·¤¿¤Î¤Ç 0 ¤òÊÖ¤¹
   19           0 °Ê³°¤ÎÃͤòÊÖ¤»¤Ð½é´ü²½¼ºÇԤȤߤʤµ¤ì¥â¥¸¥å¡¼¥ë¤Ï
   20           ¥«¡¼¥Í¥ë¤Ë¤ÏÁȤ߹þ¤Þ¤ì¤Ê¤¤ */
   21        return 0;
   22    }
   23    
   24    
   25    /*
   26     * ¥â¥¸¥å¡¼¥ë¤Î²òÊü½èÍý
   27     * ¥â¥¸¥å¡¼¥ë¤Î»²¾È¿ô¤¬ 0 ¤Ç¤¢¤ì¤Ð¡¢rmmod »þ¤Ë¸Æ¤Ð¤ì¤ë
   28     */
   29    void 
   30    cleanup_module( void )
   31    {
   32        printk( KERN_INFO "%s : removed from kernel\n", msg );
   33    }
   34    
   35    
   36    /* End of message.c */

¡û ¥µ¥ó¥×¥ë¥×¥í¥°¥é¥à¤ÎÀâÌÀ

¤Ç¤Ï¡¢¥µ¥ó¥×¥ë¥×¥í¥°¥é¥à¤Ë¤Ä¤¤¤ÆÀâÌÀ¤·¤Æ¤¤¤­¤Þ¤¹¡£

  • 1 ¡Á 2 ¹ÔÌÜ

    ¥«¡¼¥Í¥ë¥â¥¸¥å¡¼¥ë¥×¥í¥°¥é¥ß¥ó¥°¤ËɬÍפʥإåÀ¥Õ¥¡¥¤¥ë <linux/module.h> ¤ò¥¤¥ó¥¯¥ë¡¼¥É¤·¤Æ¤¤¤Þ¤¹¡£¤³¤Î¥Ø¥Ã¥À¥Õ¥¡¥¤¥ë¤Ï¡¢ ¥«¡¼¥Í¥ë¥â¥¸¥å¡¼¥ë¤ò¥³¥ó¥Ñ¥¤¥ë¤¹¤ëºÝ¤Ë¤Ï MODULE ¥Þ¥¯¥í¤òÄêµÁ¤·¤¿¾å¤Ç ɬ¤º¥¤¥ó¥¯¥ë¡¼¥É¤¹¤ëɬÍפ¬¤¢¤ê¤Þ¤¹¡£

    <linux/kernel.h> ¤Ï¡¢printk ¤ò»È¤¦¤¿¤á¤Ë¥¤¥ó¥¯¥ë¡¼¥É¤·¤Æ¤¤¤Þ¤¹ ¡Ê ¼ÂºÝ¤Ë¤Ï module.h ¤ò¥¤¥ó¥¯¥ë¡¼¥É¤¹¤ë¤À¤±¤Ç»È¤¨¤Þ¤¹¤¬ )¡£

  • 4 ¹ÔÌÜ

    MODULE_LICENSE ¥Þ¥¯¥í¤Ï¡¢¥«¡¼¥Í¥ë 2.4.10 ¤«¤é¿·¤·¤¯ÍÑ°Õ¤µ¤ì¤¿¥Þ¥¯¥í ¤Ç¤¹¡£¤³¤Î¥Þ¥¯¥í¤Ë¤Ï¡¢¥«¡¼¥Í¥ë¥â¥¸¥å¡¼¥ë¤Î¥é¥¤¥»¥ó¥¹¤òɽ¤¹Ê¸»úÎó¤ò °ú¿ô¤È¤·¤Æ»ØÄꤷ¤Þ¤¹¡£»ØÄê¤Ç¤­¤ëʸ»úÎó¤Ï module.h Æâ¤ËÍÑ°Õ¤µ¤ì¤Æ¤¤¤Þ¤¹ ¡ÊËÜ Web ¤Ç¤Ï GPL ¤ò»ÈÍѤ·¤Þ¤¹ ¡Ë¡£

    ¤³¤Î¥Þ¥¯¥í¤ò²¿¤Î¤¿¤á¤Ë»È¤Ã¤Æ¤¤¤ë¤«¤Ë¤Ä¤¤¤Æ¤Ç¤¹¤¬¡¢¤³¤ì¤ò»È¤ï¤Ê¤¤¤Ç ¥³¥ó¥Ñ¥¤¥ë¤·¤Æ insmod ¤ò¼Â¹Ô¤¹¤ë¤È¡¢¥«¡¼¥Í¥ë 2.4.10 °Ê¹ß¤Ç¤Ï°Ê²¼¤ÎÍͤÊɽ¼¨¤¬½Ð¤Þ¤¹¡£

    Warning: loading message.o will taint the kernel: no license
      See http://www.tux.org/lkml/#export-tainted for information about tainted modules
    Module message loaded, with warnings
          

    Í×Ì󤹤ë¤È¡¢"¥é¥¤¥»¥ó¥¹¤¬Ìµ¤¤¥«¡¼¥Í¥ë¥â¥¸¥å¡¼¥ë message.o ¤Ï¡¢ ¥«¡¼¥Í¥ë¤ò±øÀ÷¤¹¤ë¤È»×¤ï¤ì¤Þ¤¹¡£¾ÜºÙ¤Ï http://www.tux.org/lkml/#export-tainted ¤ò»²¾È¤·¤Æ²¼¤µ¤¤¡£" ¤È¤¤¤¦¤³¤È¤Ç¤¹¡£ ¤³¤ì¤ÏÆ°ºî¾åÌäÂ꤬¤¢¤ë¤È¤¤¤¦°ÕÌ£¤Ç¤Ï¤Ê¤¯¡¢³ºÅö¥â¥¸¥å¡¼¥ë¤Ï ( module.h ¤ÇÄêµÁ¤µ¤ì¤Æ¤¤¤ë ) ¥ª¡¼¥×¥ó¥½¡¼¥¹¥é¥¤¥»¥ó¥¹¤ÇÄ󶡤µ¤ì¤Æ¤¤¤Ê¤¤¤¿¤á¡¢ ²¿¤«ÌäÂ꤬À¸¤¸¤¿¾ì¹ç¤Ï Linux ¥«¡¼¥Í¥ë³«È¯¼Ô¤Ç¤Ï¤Ê¤¯¥â¥¸¥å¡¼¥ëÄó¶¡¼Ô¤Ë Ì䤤¹ç¤ï¤»¤Æ¤¯¤ì¡¢¤È¤¤¤¦¤³¤È¤ò¸À¤¤¤¿¤¤¤é¤·¤¤¤Ç¤¹¡£

  • 9 ¡Á 22 ¹ÔÌÜ ( init_module )

    ¥«¡¼¥Í¥ë¥â¥¸¥å¡¼¥ë¤òÁȤ߹þ¤àºÝ¤Ë¡¢É¬¤º¼Â¹Ô¤µ¤ì¤ë´Ø¿ô¤Ç¤¹¡£Ä̾ï¤Ï¡¢ ¤³¤Î´Ø¿ô¤ÎÃæ¤ÇɬÍפʽé´ü²½Åù¤ò¹Ô¤¤¤Þ¤¹¡£0 ¤òÊÖ¤¹¤È½é´ü²½À®¸ù¤òɽ¤·¡¢ 0 °Ê³°¤òÊÖ¤¹¤È¼ºÇԤȤߤʤ·¤Æ¥«¡¼¥Í¥ë¤Ë¤ÏÁȤ߹þ¤Þ¤ì¤Þ¤»¤ó¡£

    message.c ¤Ç¤Ï¡¢printk ¤ò»È¤Ã¤Æ ¥«¡¼¥Í¥ë¥í¥°¤Ë¥á¥Ã¥»¡¼¥¸¤òɽ¼¨¤·¤¿¸å¡¢0 ¤òÊÖ¤·¤Æ¥«¡¼¥Í¥ë¤ËÀµ¾ï½ªÎ»¤ò ÄÌÃΤ·¤Æ¤¤¤ë¤À¤±¤Ç¤¹¡£

    printk ¤Ï¡¢¥«¡¼¥Í¥ëÈÇ printf ¤Ç¤¹¡£printk ¤Ë¤Ä¤¤¤Æ¤Ï¡¢ printk ¤Î»È¤¤Êý ¤ò»²¾È¤·¤Æ¤¯¤À¤µ¤¤¡£

  • 25 ¡Á 33 ¹ÔÌÜ ( cleanup_module )

    ¥«¡¼¥Í¥ë¥â¥¸¥å¡¼¥ë¤Î²òÊü½èÍý¤ò¹Ô¤¦´Ø¿ô¤Ç¤¹¡£¤³¤Î´Ø¿ô¤Ï rmmod ¤ò¹Ô¤¦Å٤˼¹Ԥµ¤ì¤ë¤ï¤±¤Ç¤Ï¤¢¤ê¤Þ¤»¤ó¡£Ä̾¥æ¡¼¥¶¥×¥í¥»¥¹¤¬ ¥Ç¥Ð¥¤¥¹¥Õ¥¡¥¤¥ë¤òÄ̤·¤Æ¥«¡¼¥Í¥ë¥â¥¸¥å¡¼¥ë¤ò open ¤·¤¿¤ê¤·¤Æ»ÈÍÑ ¤¹¤ë¤Î¤Ç¤¹¤¬¡¢¤½¤ÎÅ٤˥«¡¼¥Í¥ë¥â¥¸¥å¡¼¥ë¤Î»²¾È¥«¥¦¥ó¥¿¤¬Áý²Ã¤·¤Æ¤¤¤­¤Þ¤¹¡£

    »²¾È¤·¤Æ¤¤¤¿¥×¥í¥»¥¹¤¬¥Ç¥Ð¥¤¥¹¥Õ¥¡¥¤¥ë¤òÊĤ¸¤ë¤È¡¢¥«¡¼¥Í¥ë¥â¥¸¥å¡¼¥ë ¤Î»²¾È¥«¥¦¥ó¥¿¤¬¸º¾¯¤·¤Æ¤¤¤¯¤Î¤Ç¤¹¤¬¡¢¤³¤Î»²¾È¥«¥¦¥ó¥¿¤¬ 0 ¤Ë¤Ê¤Ã¤¿»þÅÀ¤Ç cleanup_module ¤¬¼Â¹Ô¤µ¤ì¤Þ¤¹¡£

    message.c ¤Ç¤Ï¥×¥í¥»¥¹¤«¤é »²¾È¤¹¤ë¤¿¤á¤Î¸ý¤òÍÑ°Õ¤·¤Æ¤¤¤Ê¤¤¤¿¤á¡¢»²¾È¥«¥¦¥ó¥¿¤Ï¾ï¤Ë 0 ¤Ç¤¹¡£ ¤½¤Î¤¿¤á¡¢rmmod ¤ò¼Â¹Ô¤¹¤ëÅÙ¤Ë cleanup_module ¤¬¼Â¹Ô¤µ¤ì¤Þ¤¹¡£

°Ê¾å¤Ç message.c ¤Î²òÀâ¤Ï½ª¤ï¤ê¤Ç¤¹¡£¤È¤Ã¤Æ¤âñ½ã¤Ê¥«¡¼¥Í¥ë¥â¥¸¥å¡¼¥ë ¤Ç¤·¤¿¤¬¡¢¤Þ¤¬¤ê¤Ê¤ê¤Ë¤â¥«¡¼¥Í¥ëÆâÉô¤ÇÆ°ºî¤µ¤»¤ë¤³¤È¤¬½ÐÍè¤Þ¤·¤¿¡ª ¤³¤ì¤«¤é¡¢¾¯¤·¤Å¤Ä»È¤¨¤ëµ¡Ç½¤òÁý¤ä¤·¤Æ¤¤¤³¤¦¤È»×¤¤¤Þ¤¹¤Î¤Ç¡¢message.c ¤ÎÆâÍƤϤ·¤Ã¤«¤êÍý²ò¤·¤Æ¤ª¤¤¤Æ¤¯¤À¤µ¤¤¡£

¡û Ê£¿ô¥Õ¥¡¥¤¥ë¤Ëʬ³ä¤·¤ÆºîÀ®¤¹¤ë

º£¸å¤·¤Ð¤é¤¯¤Ï¡¢°ì¤Ä¤Î¥Õ¥¡¥¤¥ë¤Ç¥«¡¼¥Í¥ë¥â¥¸¥å¡¼¥ë¤Î¥µ¥ó¥×¥ë¥×¥í¥°¥é¥à¤òºî¤Ã¤Æ¤¤¤¯Í½Äê¤Ç¤¹¤¬¡¢ÀâÌÀÍѤäÎý½¬ÍѤǤϤʤ¯¤Á¤ã¤ó¤È¤·¤¿¥×¥í¥°¥é¥à¤òºîÀ®¤¹¤ë¾ì¹ç¤Ï¡¢¥Õ¥¡¥¤¥ë¤òŬÀÚ¤Ëʬ³ä¤¹¤ë¤Î¤¬ÉáÄ̤À¤È»×¤¤¤Þ¤¹¡£

¤³¤³¤Ç¤¢¤é¤«¤¸¤á¡¢Ê£¿ô¥Õ¥¡¥¤¥ë¤Ëʬ³ä¤·¤Æ¥«¡¼¥Í¥ë¥â¥¸¥å¡¼¥ë¤òºîÀ®¤¹¤ëÊýË¡¤Ë¤Ä¤¤¤ÆÀâÌÀ¤·¤Æ¤ª¤­¤Þ¤¹¡£

sample.o ¤È¤¤¤¦¥«¡¼¥Í¥ë¥â¥¸¥å¡¼¥ë¤ò sample_entry.c¡¢sample_func.c¡¢sample_func.h ¤È¤¤¤¦ 3 ¤Ä¤Î¥Õ¥¡¥¤¥ë¤ÇºîÀ®¤¹¤ë¤È¤·¤Þ¤¹¡£¤Þ¤¿¡¢sample_entry.c ¤Ë¤Ï init_module ¤ª¤è¤Ó cleanup_module ¤¬ÄêµÁ¤µ¤ì¤Æ¤ª¤ê¡¢init_module Æâ¤Ç sample_func.c Æâ¤Î´Ø¿ô¤ò¼Â¹Ô¤·¤Æ½ªÎ»¤¹¤ë¤Î¤ß¤È¤·¤Þ¤¹¡£

³Æ¥Õ¥¡¥¤¥ë¤ÎÎã¤ò°Ê²¼¤Ëµó¤²¤Þ¤¹¡£

--- sample_func.c ---

#include "sample_func.h"

static int inner_add( int a, int b );

int 
sample_add( int a, int b )
{
    return inner_add( a, b );
}

static int
inner_add( int a, int b )
{
    return a + b;
}    

--- sample_func.h ---

#ifndef ___SAMPLE_FUNC_H_INCLUDED___
#define ___SAMPLE_FUNC_H_INCLUDED___

int
sample_add( int a, int b );


#endif

--- sample_entry.c ---

#include <linux/module.h>
#include <linux/kernel.h>
#include "sample_func.h"    

    
int
init_module( void )
{    
    EXPORT_NO_SYMBOLS;

    printk( KERN_INFO "loaded into kernel\n" );
    printk( KERN_INFO "value = %i\n", sample_add( 1, 2 ) );

    return 0;
}
    
    
void    
cleanup_module( void )    
{
    printk( KERN_INFO "removed from kernel\n" );
}

¥Ý¥¤¥ó¥È¤Ï¡¢sample_entry.c ¤Î init_module Æâ¤Ë¤¢¤ë EXPORT_NO_SYMBOLS¥Þ¥¯¥í¤Ç¤¹¡£sample_func.c ¤Ç¤Ï sample_add ´Ø¿ô¤Î¤ß¤ò³°Éô¤Ë¸ø³«¤·¤Æ¤¤¤Þ¤¹¡£¤³¤Î¤ª¤«¤²¤Ç sample_entry.c ¤«¤é sample_add ´Ø¿ô¤ò»²¾È¤¹¤ë¤³¤È¤¬¤Ç¤­¤Þ¤¹¡£

¤È¤³¤í¤Ç¡¢EXPORT_NO_SYMBOLS ¥Þ¥¯¥í¤¬Ìµ¤¤¤È¤É¤¦¤Ê¤ë¤«¤È¤¤¤¦¤È¡¢¥«¡¼¥Í¥ë¤ËÁȤ߹þ¤Þ¤ì¤¿ºÝ¤Ë static °Ê³°¤Î¥·¥ó¥Ü¥ë ( ´Ø¿ô¤À¤±¤Ç¤Ê¤¯¡¢ÊÑ¿ôÅù¤â ) ¤¬Á´¤Æ¾¤Î¥«¡¼¥Í¥ëÆâ ( ¥«¡¼¥Í¥ë¥â¥¸¥å¡¼¥ë¤ò´Þ¤à ) ¤«¤é»²¾È²Äǽ¤Ë¤Ê¤Ã¤Æ¤·¤Þ¤¤¤Þ¤¹¡£¤Ä¤Þ¤ê¡¢Á´Á³´Ø·¸Ìµ¤¤¾ì½ê¤«¤é sample_add ´Ø¿ô¤ò¼Â¹Ô¤Ç¤­¤Æ¤·¤Þ¤¦¤Î¤Ç¤¹¡£

EXPORT_NO_SYMBOLS ¥Þ¥¯¥í¤ò¼Â¹Ô¤¹¤ë¤È¡¢static ¤Ç¤Ï¤Ê¤¤¥·¥ó¥Ü¥ë¤¬³°Éô¤«¤é»²¾È¤µ¤ì¤ë¤³¤È¤¬Ìµ¤¯¤Ê¤ê¤Þ¤¹¤Î¤Ç¡¢Â¾¤Î¥â¥¸¥å¡¼¥ë¤Ëµ¡Ç½¤òÄ󶡤¹¤ë¤Î¤Ç¤Ê¤¤¾ì¹ç¤Ï¤³¤Î¥Þ¥¯¥í¤ò¼Â¹Ô¤·¤Æ¤¯¤À¤µ¤¤¡£¤Ê¤ª¡¢¼Â¹Ô¤¹¤ë¾ì½ê¤Ï¤É¤³¤Ç¤âÎɤ¤ÍͤǤ¹¤¬¡¢É®¼Ô¤Ï init_module Æâ¤Ç¹Ô¤¦Íͤˤ·¤Æ¤¤¤Þ¤¹¡£

¤Þ¤¿Ê£¿ô¥Õ¥¡¥¤¥ë¤ÇºîÀ®¤¹¤ë¾ì¹ç¤Ï¡¢linux/module.h ¤Î¥¤¥ó¥¯¥ë¡¼¥É¤Ï¤¤¤º¤ì¤«°ì¤Ä¤Î¥Õ¥¡¥¤¥ë¤Ç¹Ô¤¦Íͤˤ·¤Æ¤¯¤À¤µ¤¤¡£

¼¡¤Ë¥³¥ó¥Ñ¥¤¥ë / ¥ê¥ó¥¯¤Ë¤Ä¤¤¤Æ¤Ç¤¹¡£³Æ¥Õ¥¡¥¤¥ë¤ò¥³¥ó¥Ñ¥¤¥ë¤·¤Æ¥ª¥Ö¥¸¥§¥¯¥È¥Õ¥¡¥¤¥ë¤Ë¤·¤¿¸å¡¢ld ¤Ç¥ê¥ó¥¯¤ò¹Ô¤¤¤Þ¤¹¡£

gcc -DMODULE -D__KERNEL__ -O -c sample_func.c
gcc -DMODULE -D__KERNEL__ -O -c sample_entry.c
ld -r -o sample.o sample_entry.c sample_func.c

¤³¤ì¤Ç sample.o ¤È¤¤¤¦¥«¡¼¥Í¥ë¥â¥¸¥å¡¼¥ë¤¬ºîÀ®¤µ¤ì¤Þ¤¹¡£__KERNEL__ ¥Þ¥¯¥í¤ª¤è¤Ó -O ¥ª¥×¥·¥ç¥ó¤Ë¤Ä¤¤¤Æ¤Ï¡¢º£¸å¥­¥ã¥é¥¯¥¿¥Ç¥Ð¥¤¥¹¥É¥é¥¤¥Ð¤òºîÀ®¤·¤Æ¤¤¤¯ºÝ¤Ë¿¨¤ì¤Þ¤¹¡£¤Ê¤ª ld ¤Î -r ¥ª¥×¥·¥ç¥ó¤Ï¡¢ºÆÇÛÃÖ²Äǽ·Á¼°¤òºîÀ®¤¹¤ëÍÍ¤Ë ld ¤Ë»Ø¼¨¤¹¤ë¤¿¤á¤Î¥ª¥×¥·¥ç¥ó¤Ç¤¹¡£¥«¡¼¥Í¥ë¥â¥¸¥å¡¼¥ë¤ÏưŪ¤Ë¥í¡¼¥É¤µ¤ì¤ë¤¿¤á¡¢¤³¤Î¥ª¥×¥·¥ç¥ó¤¬É¬Íפˤʤê¤Þ¤¹¡£

¡û printk ¤Î»È¤¤Êý

¥«¡¼¥Í¥ëÆâ¤Ç´Êñ¤Ë¥á¥Ã¥»¡¼¥¸¤ò½ÐÎϤ¹¤ë¤¿¤á¤Ë¡¢printk ¤È¤¤¤¦´Ø¿ô¤ò»È¤¦¤³¤È¤¬¤Ç¤­¤Þ¤¹¡£printk ¤Ï¡¢¥æ¡¼¥¶¶õ´Ö¤Ç»È¤¨¤ë printf ¤ÎÍͤ˻Ȥ¦¤³¤È¤¬¤Ç¤­¤Þ¤¹¡£

#include <kernel.h>
int printk( const char* format, ... );

½ÐÎϤ·¤¿Ê¸»ú¿ô¤òÊÖ¤·¤Þ¤¹¡£printf ¤È°Û¤Ê¤ëÅÀ¤Ï¥í¥°¥ì¥Ù¥ë¤ò»ØÄꤹ¤ëɬÍפ¬¤¢¤ë¤³¤È¤Ç¤¹¡£¥í¥°¥ì¥Ù¥ë¤Ï¡¢°Ê²¼¤ÎÄê¿ô¤Î¤¤¤º¤ì¤«¤ò format ʸ»úÎó¤ÎÀèƬ¤Ë»ØÄꤷ¤Þ¤¹¡£

¥í¥°¥ì¥Ù¥ëÄê¿ô ÀâÌÀ
KERN_EMERG ¥·¥¹¥Æ¥à¤¬»È¤¤¤â¤Î¤Ë¤Ê¤é¤Ê¤¤¾õÂÖ
KERN_ALERT ÁáµÞ¤ËÂн褷¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤»ö¾Ý
KERN_CRIT Ã×̿Ū¤Ê»ö¾ÝȯÀ¸¡¢¥Ï¡¼¥É¥¦¥§¥¢ / ¥½¥Õ¥È¥¦¥§¥¢¤Ë½ÅÂç¤ÊÌäÂêȯÀ¸
KERN_ERR ¥¨¥é¡¼È¯À¸
KERN_WARNING ¿¼¹ï¤ÊÌäÂê¤Ç¤Ï¤Ê¤¤¤¬Ä̾ï¤Ç¤Ï¤Ê¤¤»ö¾ÝȯÀ¸¤ËÂФ¹¤ë·Ù¹ð
KERN_NOTICE Ãí°Õ¤òÂ¥¤¹»ö¾ÝȯÀ¸
KERN_INFO ¾ðÊóɽ¼¨
KERN_DEBUG ¥Ç¥Ð¥Ã¥°¥á¥Ã¥»¡¼¥¸

°Ê²¼¤ÎÍͤ˻ØÄꤷ¤Æ»È¤¤¤Þ¤¹¡£

printk( KERN_ERR "Something happened.\n" );

¥ì¥Ù¥ëÄê¿ô¤È¸å³¤Îʸ»úÎó¤¬Ä¾ÀÜÏ¢·ë¤µ¤ì¤Æ¤¤¤ëÅÀ¤ËÃí°Õ¤·¤Æ¤¯¤À¤µ¤¤¡£¤Ê¤ª¡¢¥ì¥Ù¥ë¤ò»ØÄꤷ¤Ê¤¤¾ì¹ç¤Ï¡¢kernel/printk.c ¤ÇÄêµÁ¤µ¤ì¤Æ¤¤¤ë¥Ç¥Õ¥©¥ë¥È¤Î¥í¥°¥ì¥Ù¥ë¤¬»ÈÍѤµ¤ì¤Þ¤¹¡£

printk ¤Ë¤è¤Ã¤Æ½ÐÎϤµ¤ì¤¿¥á¥Ã¥»¡¼¥¸¤Ï¡¢¤Þ¤º¥«¡¼¥Í¥ëÆâ¤Î¥ê¥ó¥°¥Ð¥Ã¥Õ¥¡¤Ë³ÊǼ¤µ¤ì¤Þ¤¹¡£¤½¤Î¸å¡¢klogd ¤¬Æ°ºî¤·¤Æ¤¤¤ë¾ì¹ç¤Ï klogd ¤¬¥ê¥ó¥°¥Ð¥Ã¥Õ¥¡¤«¤é¥á¥Ã¥»¡¼¥¸¤òÆɤ߹þ¤ß¡¢ÀßÄê¤Ë±þ¤¸¤Æ¥³¥ó¥½¡¼¥ë¤Ë½ÐÎϤ·¤¿¤ê syslogd ¤Ë¥á¥Ã¥»¡¼¥¸¤òÁ÷¤Ã¤¿¤ê¤·¤Þ¤¹¡£

klogd ¤«¤é¥³¥ó¥½¡¼¥ë¤Ë½ÐÎϤµ¤ì¤ë¥á¥Ã¥»¡¼¥¸¤Ï¡¢Í½¤áÀßÄꤵ¤ì¤Æ¤¤¤ë¥í¥°¥ì¥Ù¥ë¤è¤êÂ礭¤¤¥í¥°¥ì¥Ù¥ë¤Î¤ß¤È¤Ê¤ê¤Þ¤¹¡£Î㤨¤Ð¡¢klogd ¤ò -c ¥ª¥×¥·¥ç¥óÉÕ¤­¤Ç¼Â¹Ô¤·¤Æ¤¤¤ë¾ì¹ç¤Ï¡¢¤½¤ì¤è¤ê¾®¤µ¤Ê¥í¥°¥ì¥Ù¥ë¤Î¥á¥Ã¥»¡¼¥¸¤Î¤ß¤¬¥³¥ó¥½¡¼¥ë¤Ë½ÐÎϤµ¤ì¤Þ¤¹¡£¾Ü¤·¤¯¤Ï man klogd ¤ò»²¾È¤·¤Æ¤¯¤À¤µ¤¤¡£

¥Õ¥©¡¼¥Þ¥Ã¥Èʸ»ú¤Ï¡¢°Ê²¼¤ò»È¤¦¤³¤È¤¬½ÐÍè¤Þ¤¹ ( kernel 2.4.19 »þÅÀ )¡£

¥Õ¥©¡¼¥Þ¥Ã¥Èʸ»ú ÀâÌÀ
c 1 ʸ»ú
s ʸ»úÎó
p ¥Ý¥¤¥ó¥¿
n ľÁ°¤Þ¤Ç¤Îʸ»ú¿ô¤òÊÑ¿ô¤Ë³ÊǼ¤¹¤ë
o 8¿Ê¿ô
X Âçʸ»úɽµ­ 16¿Ê¿ô
x 16¿Ê¿ô
d Éä¹æÉÕ¤­À°¿ô
i Éä¹æÉÕ¤­À°¿ô
u Éä¹æ̵¤·À°¿ô

¡û kmalloc / kfree ¤Ë¤è¤ë¥á¥â¥ê¤Î³ÎÊÝ / ²òÊü

¥«¡¼¥Í¥ëÆâ¤ÇưŪ¤Ë¥á¥â¥ê¤ò³ÎÊÝ / ²òÊü¤¹¤ë¤¿¤á¤Ë¤Ï¤¤¤¯¤Ä¤«¤ÎÊýË¡¤¬¤¢¤ê¤Þ¤¹¤¬¡¢´Êñ¤ÊÊýË¡¤Ï¡¢¥æ¡¼¥¶¶õ´Ö¤Ç malloc / free ¤ò»È¤¦´¶³Ð¤Ç kmalloc / kfree ¤ò»È¤¦¤³¤È¤Ç¤¹¡£

#include <slab.h>
void* kmalloc( size_t size, int flags );
void kfree( void* ptr );

size ¤Ë¤Ï³ÎÊݤ·¤¿¤¤¥Ð¥¤¥È¿ô¤ò»ØÄꤷ¤Þ¤¹¡£ÊªÍý¥á¥â¥ê¤Ï¤¢¤ëñ°Ì¤Ç¤Þ¤È¤á¤Æ°·¤¦É¬Íפ¬¤¢¤ë¤¿¤á¡¢kmalloc ¤Ç¼ÂºÝ¤Ë³ÎÊݤ¹¤ë¥Ð¥¤¥È¿ô¤Ï size °Ê¾å¤Ë¤Ê¤ë²ÄǽÀ­¤¬¤¢¤ê¤Þ¤¹¡£¤Ê¤ª¡¢¼«Ê¬¤Ç¤Ï¤Þ¤ÀÎɤ¯Ê¬¤«¤Ã¤Æ¤¤¤Ê¤¤¤Î¤Ç¤¹¤¬¡¢kmalloc ¤Ç³ÎÊݤǤ­¤ëºÇÂç¥Ð¥¤¥È¿ô¤Ï 128 KB¤é¤·¤¤¤Ç¤¹¤Î¤Çµ¤¤òÉÕ¤±¤Æ¤¯¤À¤µ¤¤¡£

flags ¤Ë¤Ï¡¢linux/mm.h ¤ÇÄêµÁ¤µ¤ì¤Æ¤¤¤ë°Ê²¼¤Î¤¤¤º¤ì¤«¤òÄ̾ï¤Ï»ØÄꤷ¤Þ¤¹¡£

flag Äê¿ô ÀâÌÀ
GFP_KERNEL ¨ºÂ¤Ë¥á¥â¥êÎΰè¤òÍѰդǤ­¤Ê¤¤¾ì¹ç¡¢¥á¥â¥ê´ÉÍýµ¡Ç½¤¬¥¹¥ï¥Ã¥×¥¢¥¦¥È Åù¤Ë¤è¤Ã¤ÆÎΰè¤ò³ÎÊݤ¹¤ë¤Þ¤Ç¥¹¥ê¡¼¥×¤¹¤ë²ÄǽÀ­¤¬¤¢¤ë
GFP_ATOMIC ¥¹¥ê¡¼¥×¤»¤º¤Ë¥á¥â¥êÎΰè¤ò³ÎÊݤ·¤è¤¦¤È¤¹¤ë¡£ ɬÍפÊÎΰ褬³ÎÊݤǤ­¤Ê¤¤¾ì¹ç¤Ï¼ºÇÔ¤¹¤ë¡£ ³ä¤ê¹þ¤ß¥Ï¥ó¥É¥é¤ä¥¿¥¹¥¯¥ì¥Ã¥È¡¢¥¿¥¤¥à¥¢¥¦¥È¥Ï¥ó¥É¥éÅù¤Î ¥×¥í¥»¥¹¥³¥ó¥Æ¥­¥¹¥È¤È̵´Ø·¸¤Ë¼Â¹Ô¤µ¤ì¤ë²Õ½ê¤Ç¤Ï¤³¤ì¤ò»È¤¦

¤Þ¤¿¡¢DMA ¤Ë»ÈÍѤǤ­¤ë¥á¥â¥êÎΰ褬ɬÍפʾì¹ç¤Ï¡¢¾å¤Î¤¤¤º¤ì¤«¤Î¥Õ¥é¥°¤È GFP_DMA ¤Î or ¤ò¤È¤Ã¤¿¥Ó¥Ã¥È¥Þ¥¹¥¯¤ò»ØÄꤷ¤Þ¤¹¡£

¡û module_init / module_exit ¥Þ¥¯¥í¤Ë¤Ä¤¤¤Æ

Á°½Ò¤ÎÀâÌÀ¤Ç¤Ï¡¢init_module() ¤¬¥â¥¸¥å¡¼¥ë¤Î½é´ü²½½èÍý¤ò¹Ô¤¤¡¢cleanup_module() ¤¬²òÊü½èÍý¤ò¹Ô¤¦¤È½ñ¤­¤Þ¤·¤¿¡£¤³¤ÎÆó¤Ä¤Î´Ø¿ô̾¤Ï·è¤á¤é¤ì¤Æ¤¤¤ë̾¾Î¤Ç¤¹¤¬¡¢¡É¼«Ê¬¤Ç¼«Í³¤Ë̾Á°¤òÉÕ¤±¤¿¤¤¡É¤È¤«¡É̾Á°¤Î¾×Æͤ¬µ¯¤­¤ë¤Î¤¬Éݤ¤¡É¤È¤¤¤¦¾ì¹ç¤Ë¤Ï¡¢module_init / module_exit ¥Þ¥¯¥í¤ò»ÈÍѤ¹¤ë¤³¤È¤Ç½é´ü²½½èÍý¡¿²òÊü½èÍý¤ò¹Ô¤¦´Ø¿ô̾¤òÊѤ¨¤ë¤³¤È¤¬¤Ç¤­¤Þ¤¹¡£

Î㤨¤Ð¡¢½é´ü²½½èÍý¤ò initialize_kernel_module() ¤È¤¤¤¦´Ø¿ô¤Ç¹Ô¤¤¡¢²òÊü½èÍý¤ò shutdown_kernel_module() ¤È¤¤¤¦´Ø¿ô¤Ç¹Ô¤¤¤¿¤¤¤È¤·¤Þ¤¹¡£

#include <linux/init.h>

static int
initialize_kernel_module( void )
{
    /* ½é´ü²½½èÍý¤òµ­½Ò¤¹¤ë */
}


static int
shutdown_kernel_module( void )
{
    /* ²òÊü½èÍý¤òµ­½Ò¤¹¤ë */
}


module_init( initialize_kernel_module );
module_exit( shutdown_kernel_module );

¤³¤ÎÍͤˤ¹¤ë¤³¤È¤Ç¡¢¥â¥¸¥å¡¼¥ëÁȤ߹þ¤ß»þ¤Ë¤Ï initialize_kernel_module() ¤¬¡¢²òÊü»þ¤Ë¤Ï shutdown_kernel_module() ¤¬¼Â¹Ô¤µ¤ì¤ëÍͤˤʤê¤Þ¤¹¡£