手机版
你好,游客 登录 注册
背景:
阅读新闻

Linux模块编程机制之hello kernel

[日期:2012-03-13] 来源:Linux社区  作者:bullbat [字体:大 中 小]

模块行为

      用户空间工具和内核的模块实现之间的结构,包括两个系统调用。

init_module:将一个新模块插入到内核中。用户空间工具只需提供二进制数据。所有其他工作(特别是重定位和解决引用)由内核自身完成。

 

delete_module:从内核溢出一个模块。当然,前提是该模块的代码不再使用,并且其他模块也不再使用该模块导出的函数。

 

还有一个request_module函数(不是系统调用),用于从内核端加载模块。他不经用于加载模块。还用于实现热插拔功能。

 

模块在内核中用下面数据结构表示:

 
  1. struct module  
  2. {  
  3.     enum module_state state;  
  4.   
  5.     /* Member of list of modules */  
  6.     /*链表的表头为一个module类型的全局变量 
  7.     modules*/  
  8.     struct list_head list;  
  9.   
  10.     /* Unique handle for this module */  
  11.     char name[MODULE_NAME_LEN];  
  12.   
  13.     /* Sysfs stuff. */  
  14.     struct module_kobject mkobj;  
  15.     struct module_attribute *modinfo_attrs;  
  16.     const char *version;  
  17.     const char *srcversion;  
  18.     struct kobject *holders_dir;  
  19.   
  20.     /* Exported symbols */  
  21.     /*syms为一个数组,共有num_syms项, 
  22.     类型kernel_symbol负责将标识符(name字段) 
  23.     分配到内存地址(value字段)*/  
  24.     const struct kernel_symbol *syms;  
  25.     /*也是num_syms个项的数组,存放了到处富豪的校验和 
  26.     用于实现版本控制*/  
  27.     const unsigned long *crcs;  
  28.     unsigned int num_syms;  
  29.   
  30.     /* Kernel parameters. */  
  31.     struct kernel_param *kp;  
  32.     unsigned int num_kp;  
  33.   
  34.     /* GPL-only exported symbols. */  
  35.     unsigned int num_gpl_syms;  
  36.     const struct kernel_symbol *gpl_syms;  
  37.     const unsigned long *gpl_crcs;  
  38.   
  39. #ifdef CONFIG_UNUSED_SYMBOLS   
  40.     /* unused exported symbols. */  
  41.     const struct kernel_symbol *unused_syms;  
  42.     const unsigned long *unused_crcs;  
  43.     unsigned int num_unused_syms;  
  44.   
  45.     /* GPL-only, unused exported symbols. */  
  46.     unsigned int num_unused_gpl_syms;  
  47.     const struct kernel_symbol *unused_gpl_syms;  
  48.     const unsigned long *unused_gpl_crcs;  
  49. #endif   
  50.   
  51.     /* symbols that will be GPL-only in the near future. */  
  52.     const struct kernel_symbol *gpl_future_syms;  
  53.     const unsigned long *gpl_future_crcs;  
  54.     unsigned int num_gpl_future_syms;  
  55.   
  56.     /* Exception table */  
  57.     unsigned int num_exentries;  
  58.     struct exception_table_entry *extable;  
  59.   
  60.     /* Startup function. */  
  61.     int (*init)(void);  
  62.   
  63.     /* If this is non-NULL, vfree after init() returns */  
  64.     /*模块二进制数据分为两个部分:初始化部分和核心部分 
  65.     前者包含的东西在装载结束后都可以丢弃 
  66.     后者包含了正常运行期间需要的所有数据 
  67.     初始化部分的起始地址保存在module_init*/  
  68.     void *module_init;  
  69.   
  70.     /* Here is the actual code + data, vfree'd on unload. */  
  71.     void *module_core;  
  72.   
  73.     /* Here are the sizes of the init and core sections */  
  74.     unsigned int init_size, core_size;  
  75.   
  76.     /* The size of the executable code in each section.  */  
  77.     unsigned int init_text_size, core_text_size;  
  78.   
  79.     /* Arch-specific module values */  
  80.     struct mod_arch_specific arch;  
  81.   
  82.     unsigned int taints;    /* same bits as kernel:tainted */  
  83.   
  84. #ifdef CONFIG_GENERIC_BUG   
  85.     /* Support for BUG */  
  86.     unsigned num_bugs;  
  87.     struct list_head bug_list;  
  88.     struct bug_entry *bug_table;  
  89. #endif   
  90.   
  91. #ifdef CONFIG_KALLSYMS   
  92.     /* 
  93.      * We keep the symbol and string tables for kallsyms. 
  94.      * The core_* fields below are temporary, loader-only (they 
  95.      * could really be discarded after module init). 
  96.      */  
  97.     Elf_Sym *symtab, *core_symtab;  
  98.     unsigned int num_symtab, core_num_syms;  
  99.     char *strtab, *core_strtab;  
  100.   
  101.     /* Section attributes */  
  102.     struct module_sect_attrs *sect_attrs;  
  103.   
  104.     /* Notes attributes */  
  105.     struct module_notes_attrs *notes_attrs;  
  106. #endif   
  107.   
  108.     /* Per-cpu data. */  
  109.     void *percpu;  
  110.   
  111.     /* The command line arguments (may be mangled).  People like 
  112.        keeping pointers to this stuff */  
  113.     char *args;  
  114. #ifdef CONFIG_TRACEPOINTS   
  115.     struct tracepoint *tracepoints;  
  116.     unsigned int num_tracepoints;  
  117. #endif   
  118.   
  119. #ifdef CONFIG_TRACING   
  120.     const char **trace_bprintk_fmt_start;  
  121.     unsigned int num_trace_bprintk_fmt;  
  122. #endif   
  123. #ifdef CONFIG_EVENT_TRACING   
  124.     struct ftrace_event_call *trace_events;  
  125.     unsigned int num_trace_events;  
  126. #endif   
  127. #ifdef CONFIG_FTRACE_MCOUNT_RECORD   
  128.     unsigned long *ftrace_callsites;  
  129.     unsigned int num_ftrace_callsites;  
  130. #endif   
  131.   
  132. #ifdef CONFIG_MODULE_UNLOAD   
  133.     /* What modules depend on me? */  
  134.     /*将依赖本模块的模块用module_use数据结构链接 
  135.     起来*/  
  136.     struct list_head modules_which_use_me;  
  137.   
  138.     /* Who is waiting for us to be unloaded */  
  139.     struct task_struct *waiter;  
  140.   
  141.     /* Destruction function. */  
  142.     void (*exit)(void);  
  143.   
  144. #ifdef CONFIG_SMP   
  145.     char *refptr;  
  146. #else   
  147.     local_t ref;  
  148. #endif   
  149. #endif   
  150.   
  151. #ifdef CONFIG_CONSTRUCTORS   
  152.     /* Constructor functions. */  
  153.     ctor_fn_t *ctors;  
  154.     unsigned int num_ctors;  
  155. #endif   
  156. };  

module_state状态:

 
  1. enum module_state  
  2. {  
  3.     /*正常运行*/  
  4.     MODULE_STATE_LIVE,  
  5.     /*装载期间*/  
  6.     MODULE_STATE_COMING,  
  7.     /*正在移除*/  
  8.     MODULE_STATE_GOING,  
  9. };  
 
  1. struct kernel_symbol  
  2. {  
  3.     unsigned long value;  
  4.     const char *name;  
  5. };  
linux
相关资讯       Linux编程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款