[Linux][init] Module init

Linux 2018. 1. 25. 14:23

_


1. subsys_initcall()과 module_init()의 차이점


https://kldp.org/node/109872

http://cy.cyworld.com/home/22134629/post/3448394


/kernel/include/linux/init.h 파일을 보면 이를 위한 매크로가 선언되어 있다.


subsys_initcall 과 module_init 의 차이는


(Ref. https://m.blog.naver.com/PostView.nhn?blogId=kazama10&logNo=50174820319&proxyReferer=https%3A%2F%2Fwww.google.co.kr%2F 를 참고하였습니다.)


(1) Kernel에 정적 link를 하는 경우

 - module_init(x) 는 __initcall(x) 로 정의

 - __define_initcall(fn, 6)으로 define

 - .initcall6.init section에 등록된다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/**
 * module_init() - driver initialization entry point
 * @x: function to be run at kernel boot time or module insertion
 * 
 * module_init() will either be called during do_initcalls() (if
 * builtin) or at module insertion time (if a module).  There can only
 * be one per module.
 */
#define module_init(x)    __initcall(x);
 
#define __initcall(fn) device_initcall(fn)

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

/* initcalls are now grouped by functionality into separate 
 * subsections. Ordering inside the subsections is determined
 * by link order. 
 * For backwards compatibility, initcall() puts the call in 
 * the device init subsection.
 *
 * The `id' arg to __define_initcall() is needed so that multiple initcalls
 * can point at the same handler without causing duplicate-symbol build errors.
 */
#define __define_initcall(fn, id) \
    static initcall_t __initcall_##fn##id __used \
    __attribute__((__section__(".initcall" #id ".init"))) = fn; \
    LTO_REFERENCE_INITCALL(__initcall_##fn##id)
 
cs





(2) Module로 Compile 된 경우

  - (똑같음) subsys_initcall 이 결국 module_init으로 define되어 있음

  - ___inittest 라는 static inline 함수를 정의하여 해당 device driver의 초기화 함수 포인터를 반환.

 - init_module이라는 함수를 __attribute__((alias)) 속성을 주어 정의

 --> 이 의미는,

initfn 이라는 함수의 alias로 init module 함수를 정의함으로써, 

Device driver를 insmod 시킬 때, 공통적으로 init_module() 함수를 호출하여

해당 device driver의 초기화 함수가 호출되는 mechanism.

1
2
3
4
5
6
7
#define subsys_initcall(fn)        module_init(fn)
 
/* Each module must use one module_init(). */
#define module_init(initfn)                    \
    static inline initcall_t __inittest(void)        \
    { return initfn; }                    \
    int init_module(void) __attribute__((alias(#initfn)));
cs



'Linux' 카테고리의 다른 글

[TMUX]  (0) 2019.02.26
[Linux][Debug] Hung task  (0) 2018.01.31
[Linux] Prompt 관리  (0) 2017.12.05
htaccess 에서 extention 없애기  (0) 2016.02.22
아파치 rewrite module 켜서 .htaccess 활성화하기(우분투 기준)  (0) 2016.02.22
Posted by kissuu
,