【发布时间】:2012-02-08 13:36:29
【问题描述】:
在Linux内核源代码中我找到了这个函数:
static int __init clk_disable_unused(void)
{
// some code
}
这里看不懂__init是什么意思。
【问题讨论】:
在Linux内核源代码中我找到了这个函数:
static int __init clk_disable_unused(void)
{
// some code
}
这里看不懂__init是什么意思。
【问题讨论】:
这些只是将 linux 代码的某些部分定位到特殊位置的宏
最终执行二进制文件中的区域。
__init,例如(或者更好的是 __attribute__ ((__section__
(".init.text"))) 这个宏扩展为)指示编译器标记这个
以特殊的方式发挥作用。最后,链接器收集所有功能
在二进制文件的结尾(或开头)有这个标记。
当内核启动时,这段代码只运行一次(初始化)。运行后, 内核可以释放此内存以重用它,您将看到内核 留言:
释放未使用的内核内存:释放 108k
要使用此功能,您需要一个特殊的链接描述文件,它告诉 链接器在哪里找到所有标记的函数。
【讨论】:
阅读linux/init.h 中的评论(和文档同时)。
您还应该知道 gcc 有一些专门为 linux 内核代码制作的扩展,看起来这个宏使用了其中之一。
【讨论】:
include/linux/init.h
/* These macros are used to mark some functions or
* initialized data (doesn't apply to uninitialized data)
* as `initialization' functions. The kernel can take this
* as hint that the function is used only during the initialization
* phase and free up used memory resources after
*
* Usage:
* For functions:
*
* You should add __init immediately before the function name, like:
*
* static void __init initme(int x, int y)
* {
* extern int z; z = x * y;
* }
*
* If the function has a prototype somewhere, you can also add
* __init between closing brace of the prototype and semicolon:
*
* extern int initialize_foobar_device(int, int, int) __init;
*
* For initialized data:
* You should insert __initdata between the variable name and equal
* sign followed by value, e.g.:
*
* static int init_variable __initdata = 0;
* static const char linux_logo[] __initconst = { 0x32, 0x36, ... };
*
* Don't forget to initialize data not at file scope, i.e. within a function,
* as gcc otherwise puts the data into the bss section and not into the init
* section.
*
* Also note, that this data cannot be "const".
*/
/* These are for everybody (although not all archs will actually
discard it in modules) */
#define __init __section(.init.text) __cold notrace
#define __initdata __section(.init.data)
#define __initconst __section(.init.rodata)
#define __exitdata __section(.exit.data)
#define __exit_call __used __section(.exitcall.exit)
【讨论】:
__init 是在 ./include/linux/init.h 中定义的宏,它扩展为 __attribute__ ((__section__(".init.text")))。
它指示编译器以特殊方式标记此函数。最后,链接器在二进制文件的末尾(或开头)收集所有带有此标记的函数。当内核启动时,这段代码只运行一次(初始化)。在它运行之后,内核可以释放这块内存来重用它,你会看到内核
【讨论】:
这演示了内核 2.2 及更高版本的一个特性。注意init 和cleanup 函数定义的变化。 __init 宏会导致 init 函数被丢弃,并在 init 函数完成后释放其内存以用于内置驱动程序,但不可加载模块。如果您考虑一下何时调用 init 函数,这很有意义。
【讨论】:
当你编译一个 Linux 内核模块并将其插入内核时,要执行的第一个函数是 __init。这个函数基本上是用来在你执行注册设备驱动程序等主要操作之前执行初始化,还有另一个函数相反的效果 __exit 当您删除内核模块时调用,该内核模块再次用于删除某些已注册的设备或任何类似的功能
【讨论】: