C 在Linux内核代码中,init是什么意思?
在Linux内核源代码中,我找到了以下函数:C 在Linux内核代码中,init是什么意思?,c,linux,C,Linux,在Linux内核源代码中,我找到了以下函数: static int __init clk_disable_unused(void) { // some code } 在这里,我无法理解\uu init是什么意思。这些只是将linux代码的某些部分定位到特殊位置的宏 最终执行二进制文件中的区域。 \uuuu init,例如(或者更好的是\uuuu属性)(\uuu部分__ (“.init.text”)此宏扩展为)指示编译器标记此宏 以一种特殊的方式发挥作用。最后,链接器收集所有函数 此标记
static int __init clk_disable_unused(void)
{
// some code
}
在这里,我无法理解
\uu init
是什么意思。这些只是将linux代码的某些部分定位到特殊位置的宏
最终执行二进制文件中的区域。
\uuuu init
,例如(或者更好的是\uuuu属性)(\uuu部分__
(“.init.text”)
此宏扩展为)指示编译器标记此宏
以一种特殊的方式发挥作用。最后,链接器收集所有函数
此标记位于二进制文件的末尾(或开头)
当内核启动时,此代码只运行一次(初始化)。在它运行之后
内核可以释放这个内存以重用它,您将看到内核
信息:
释放未使用的内核内存:释放108k
要使用此功能,您需要一个特殊的链接器脚本文件,该文件告诉
链接器,用于定位所有标记的函数。在中读取注释(同时读取文档)
您还应该知道,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中定义的宏,它扩展为
\uuuuuuu属性((uuu section\uuuuu(“.init.text”)))
它指示编译器以特殊方式标记此函数。最后,链接器收集二进制文件结尾(或开始)处带有此标记的所有函数。当内核启动时,此代码只运行一次(初始化)。运行后,内核可以释放内存以重用它,您将看到内核,这展示了内核2.2及更高版本的一个特性。注意
init
和cleanup
函数定义的变化。\uuuu init
宏会导致内置驱动程序的init
函数完成后,丢弃init
函数并释放其内存,但不会加载模块。如果您考虑一下何时调用init
函数,这是非常有意义的
编译Linux内核模块并将其插入内核时,要执行的第一个函数是u init。该函数基本上用于在执行注册设备驱动程序等主要操作之前执行初始化,另一个函数的作用与此相反uu exit,当您删除内核模块时会调用该函数,内核模块再次用于删除某些已注册的设备或任何类似的函数聪明!这就是“释放未使用的内核内存:释放108k”的意思。:-)这些年来我一直在想。我认为这是某种缓冲区或其他东西,而不是代码。