_ Setup the most commonly used macro in Linux kernel is to define the function and data structure for processing startup parameters of the kernel. The macro is defined as follows:
# DEFINE _ setup (STR, FN )\
_ Setup_param (STR, FN, FN, 0)
# DEFINE _ setup_param (STR, unique_id, FN, early )\
Static char _ setup_str _ # unique_id [] _ initdata _ aligned (1) = STR ;\
Static struct obs_kernel_param _ setup _ # unique_id \
_ Used _ Section (. init. Setup )\
_ Attribute _ (aligned (sizeof (long )))))\
= {_ Setup_str _ # unique_id, FN, early}
Use the example in the kernel to analyze the two definitions:
_ Setup ("root =", root_dev_setup );
This statement appears in init/do_mounts.c. It is used to process parameters such as root =/dev/mtdblock3 during kernel startup.
Break down this statement and first change it:
_ Setup_param ("root =", root_dev_setup, root_dev_setup, 0 );
Continue decomposition. Will the following generation be obtained:
Static char _ setup_str_root_dev_setup_id [] _ initdata _ aligned (1) = "root = ";
Static struct obs_kernel_param _ setup_root_dev_setup_id
_ Used _ Section (. init. Setup)
_ Attribute _ (aligned (sizeof (long )))))
= {_ Setup_str_root_dev_setup_id, root_dev_setup, 0 };
This Code defines two variables: The character array Variable _ setup_str_root_dev_setup_id, whose initialization content is "root =". Because the variable is modified with _ initdata, It will be placed. init. the data input segment. The other variable is the Structure Variable _ setup_root_dev_setup_id. Its type is struct obs_kernel_param, which is put into the input segment. init. setup. The structure struct obs_kernel_param is also defined in this file as follows:
Struct obs_kernel_param {
Const char * STR;
INT (* setup_func) (char *);
Int early;
};
The three members of the Variable _ setup_root_dev_setup_id are initialized as follows:
_ Setup_str_root_dev_setup_id --> the character array variable defined earlier. The initial content is "root = ".
Root_dev_setup --> the processing function passed through the macro.
0 --> constant 0, which will be analyzed later.
Now it's hard to imagine how to handle the startup parameters when the kernel is started: the obs_kernel_param structure variables defined through the _ setup macro are put. init. in the setup section. init. the setup section is changed to a table. When the kernel processes every startup parameter, it searches for this table and compares it with the STR member in each data item. If it is identical, the function pointer member setup_func of the data item is called to point to the function (this function is a function parameter passed in when the _ setup macro is used to define the variable ), and pass the content after the startup parameter such as root = to the processing function.