How module init function is called when the module is loaded

It depends on whether the driver is a built-in module or compiled as a loadable module. I'll be talking about a built-in module in this answer (one with y in .config):

module_init expands to
#define module_init(x)  __initcall(x);


which then expands to

#define __initcall(fn) device_initcall(fn)
#define device_initcall(fn)             __define_initcall(fn, 6)

If your driver's initialization routine is called strtdrv then

#define device_initcall(fn) __define_initcall(fn, 6)
becomes
#define device_initcall(fn) __define_initcall(strtdrv, 6)


_define_initcall expands to

#define __define_initcall(fn, id) \
static initcall_t __initcall_##fn##id __used \
__attribute__((__section__(".initcall" #id ".init"))) = fn

which means we now have

static initcall_t __initcall_strtdrv6 __used __attribute__((__section__(".initcall6.init"))) = strtdrv;

A new symbol is created, called __initcall_strtdrv6, which is inserted into the ELF section called .initcall6.init, which points to a routine called strtdrv.

If we take a look at init/main.c, do_basic_setup() has a call to do_initcalls().

static void __init do_initcalls(void) {
int level;
for (level = 0; level < ARRAY_SIZE(initcall_levels) - 1; level++)
do_initcall_level(level); }

The purpose of this loop is to execute each of the init functions corresponding to each of the initcall levels (All built-in modules initialized with module_init () are represented by initcall level 6).

We will now expand do_initcall_level(level) and focus on a particular chunk of code:

static void __init do_initcall_level(int level) {
/* some code */
initcall_t *fn;
for (fn = initcall_levels[level]; fn < initcall_levels[level+1]; fn++)
do_one_initcall(*fn);
/* some code */
}

The function pointer *fn is pointed to first function pointer registered within each of the ELF sections and is incremented by the size of fn* (sizeof ( initcall_t *))until the end of the ELF section is reached and for each step taken, the pointer is called and the init function is executed, so in our case, do_one_initcall() will simply call the driver's initialization routine strtdrv()


int __init_or_module do_one_initcall(initcall_t fn) {
/* some code */
ret = fn(); // which in our case is strtdrv(), so ret = strtdrv();
/* some code */

and what happens next depends on the routine's code. After the initialization is done, an architecture specific function called free_initmem is called to clean up the memory pages consumed by the init functions and its data.

http://www.quora.com/Init-Call-Mechanism-in-the-Linux-Kernel-When-the-module-in-loaded-in-the-kernel-then-module-init-function-gets-called-How-this-call-happen-internally

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值