模块行为
用户空间工具和内核的模块实现之间的结构,包括两个系统调用。
init_module:将一个新模块插入到内核中。用户空间工具只需提供二进制数据。所有其他工作(特别是重定位和解决引用)由内核自身完成。
delete_module:从内核溢出一个模块。当然,前提是该模块的代码不再使用,并且其他模块也不再使用该模块导出的函数。
还有一个request_module函数(不是系统调用),用于从内核端加载模块。他不经用于加载模块。还用于实现热插拔功能。
模块在内核中用下面数据结构表示:
- struct module
- {
- enum module_state state;
- /* Member of list of modules */
- /*链表的表头为一个module类型的全局变量
- modules*/
- struct list_head list;
- /* Unique handle for this module */
- char name[MODULE_NAME_LEN];
- /* Sysfs stuff. */
- struct module_kobject mkobj;
- struct module_attribute *modinfo_attrs;
- const char *version;
- const char *srcversion;
- struct kobject *holders_dir;
- /* Exported symbols */
- /*syms为一个数组,共有num_syms项,
- 类型kernel_symbol负责将标识符(name字段)
- 分配到内存地址(value字段)*/
- const struct kernel_symbol *syms;
- /*也是num_syms个项的数组,存放了到处富豪的校验和
- 用于实现版本控制*/
- const unsigned long *crcs;
- unsigned int num_syms;
- /* Kernel parameters. */
- struct kernel_param *kp;
- unsigned int num_kp;
- /* GPL-only exported symbols. */
- unsigned int num_gpl_syms;
- const struct kernel_symbol *gpl_syms;
- const unsigned long *gpl_crcs;
- #ifdef CONFIG_UNUSED_SYMBOLS
- /* unused exported symbols. */
- const struct kernel_symbol *unused_syms;
- const unsigned long *unused_crcs;
- unsigned int num_unused_syms;
- /* GPL-only, unused exported symbols. */
- unsigned int num_unused_gpl_syms;
- const struct kernel_symbol *unused_gpl_syms;
- const unsigned long *unused_gpl_crcs;
- #endif
- /* symbols that will be GPL-only in the near future. */
- const struct kernel_symbol *gpl_future_syms;
- const unsigned long *gpl_future_crcs;
- unsigned int num_gpl_future_syms;
- /* Exception table */
- unsigned int num_exentries;
- struct exception_table_entry *extable;
- /* Startup function. */
- int (*init)(void);
- /* If this is non-NULL, vfree after init() returns */
- /*模块二进制数据分为两个部分:初始化部分和核心部分
- 前者包含的东西在装载结束后都可以丢弃
- 后者包含了正常运行期间需要的所有数据
- 初始化部分的起始地址保存在module_init*/
- void *module_init;
- /* Here is the actual code + data, vfree'd on unload. */
- void *module_core;
- /* Here are the sizes of the init and core sections */
- unsigned int init_size, core_size;
- /* The size of the executable code in each section. */
- unsigned int init_text_size, core_text_size;
- /* Arch-specific module values */
- struct mod_arch_specific arch;
- unsigned int taints; /* same bits as kernel:tainted */
- #ifdef CONFIG_GENERIC_BUG
- /* Support for BUG */
- unsigned num_bugs;
- struct list_head bug_list;
- struct bug_entry *bug_table;
- #endif
- #ifdef CONFIG_KALLSYMS
- /*
- * We keep the symbol and string tables for kallsyms.
- * The core_* fields below are temporary, loader-only (they
- * could really be discarded after module init).
- */
- Elf_Sym *symtab, *core_symtab;
- unsigned int num_symtab, core_num_syms;
- char *strtab, *core_strtab;
- /* Section attributes */
- struct module_sect_attrs *sect_attrs;
- /* Notes attributes */
- struct module_notes_attrs *notes_attrs;
- #endif
- /* Per-cpu data. */
- void *percpu;
- /* The command line arguments (may be mangled). People like
- keeping pointers to this stuff */
- char *args;
- #ifdef CONFIG_TRACEPOINTS
- struct tracepoint *tracepoints;
- unsigned int num_tracepoints;
- #endif
- #ifdef CONFIG_TRACING
- const char **trace_bprintk_fmt_start;
- unsigned int num_trace_bprintk_fmt;
- #endif
- #ifdef CONFIG_EVENT_TRACING
- struct ftrace_event_call *trace_events;
- unsigned int num_trace_events;
- #endif
- #ifdef CONFIG_FTRACE_MCOUNT_RECORD
- unsigned long *ftrace_callsites;
- unsigned int num_ftrace_callsites;
- #endif
- #ifdef CONFIG_MODULE_UNLOAD
- /* What modules depend on me? */
- /*将依赖本模块的模块用module_use数据结构链接
- 起来*/
- struct list_head modules_which_use_me;
- /* Who is waiting for us to be unloaded */
- struct task_struct *waiter;
- /* Destruction function. */
- void (*exit)(void);
- #ifdef CONFIG_SMP
- char *refptr;
- #else
- local_t ref;
- #endif
- #endif
- #ifdef CONFIG_CONSTRUCTORS
- /* Constructor functions. */
- ctor_fn_t *ctors;
- unsigned int num_ctors;
- #endif
- };
module_state状态:
- enum module_state
- {
- /*正常运行*/
- MODULE_STATE_LIVE,
- /*装载期间*/
- MODULE_STATE_COMING,
- /*正在移除*/
- MODULE_STATE_GOING,
- };
- struct kernel_symbol
- {
- unsigned long value;
- const char *name;
- };
