试图理解函数指针的用法 [英] Trying to understand the usage of function pointer
问题描述
这是在 U中的作用从那里内核启动-boot的bootm.c :
/* Subcommand: GO */
static void boot_jump_linux(bootm_headers_t *images, int flag)
{
#ifdef CONFIG_ARM64
void (*kernel_entry)(void *fdt_addr);
int fake = (flag & BOOTM_STATE_OS_FAKE_GO);
kernel_entry = (void (*)(void *fdt_addr))images->ep;
debug("## Transferring control to Linux (at address %lx)...\n",
(ulong) kernel_entry);
bootstage_mark(BOOTSTAGE_ID_RUN_OS);
announce_and_cleanup(fake);
if (!fake)
kernel_entry(images->ft_addr);
#else
unsigned long machid = gd->bd->bi_arch_number;
char *s;
void (*kernel_entry)(int zero, int arch, uint params);
unsigned long r2;
int fake = (flag & BOOTM_STATE_OS_FAKE_GO);
kernel_entry = (void (*)(int, int, uint))images->ep;
s = getenv("machid");
if (s) {
strict_strtoul(s, 16, &machid);
printf("Using machid 0x%lx from environment\n", machid);
}
debug("## Transferring control to Linux (at address %08lx)" \
"...\n", (ulong) kernel_entry);
bootstage_mark(BOOTSTAGE_ID_RUN_OS);
announce_and_cleanup(fake);
if (IMAGE_ENABLE_OF_LIBFDT && images->ft_len)
r2 = (unsigned long)images->ft_addr;
else
r2 = gd->bd->bi_boot_params;
if (!fake)
kernel_entry(0, machid, r2);
#endif
}
我对着了解如何 kernel_entry
正在这里工作的难度。特别是在第二最后一行它被用作
I am facing difficulty in understanding how kernel_entry
is working here. Especially in the second-last line it is being used as:
kernel_entry(0, machid, r2);
那么,是 kernel_entry的定义()
?我失败了整个U-boot和内核源代码code找到。
So where is the definition of kernel_entry()
? I failed to find in entire u-boot and kernel source code.
更新
我在这里改写我的问题:
I am rephrasing my question here:
假设 kernel_entry
是指向一个功能被定义为:
Suppose kernel_entry
is a pointer to a function and is being defined as:
bootm_headers_t *images
kernel_entry = (void (*)(int, int, uint))images->ep;
然后程序中某个位置它被称为:
Then somewhere in the program it is being called as:
kernel_entry(0, machid, r2);
我理解所的指针, kernel_entry
应存储函数的地址。但我想了解什么操作都将针对三个参数进行。为什么我们这些争论?
I understand being a pointer, kernel_entry
should store an address of a function. But I want to understand what operations will be performed on the three arguments. Why do we have those arguments?
推荐答案
kernel_entry
的声明变量及其类型,它是采用指针给一个函数 INT
, INT
, UINT
并返回无效
(可能是最令人困惑的部分),是在这里:
The declaration of kernel_entry
variable and its type, which is a pointer to a function taking int
, int
, uint
and returning void
(probably the most confusing part), is here:
void (*kernel_entry)(int zero, int arch, uint params);
任务,图像 - > EP
被强制转换成所需的签名函数指针并投入变量:
Assignment, images->ep
is cast into desired signature function pointer and put into the variable:
kernel_entry = (void (*)(int, int, uint))images->ep;
最后,调用该函数:
Finally, the function is called:
kernel_entry(0, machid, r2);
请注意,如果CONFIG_ARM64定义,则函数 kernel_entry
点有不同的签名:
Please note that if CONFIG_ARM64 is defined, then the function kernel_entry
points to has different signature:
void (*kernel_entry)(void *fdt_addr); //takes one void* param and returns void
这篇关于试图理解函数指针的用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!