gpt4 book ai didi

c - Linux内核文件重启

转载 作者:行者123 更新时间:2023-11-30 20:02:08 26 4
gpt4 key购买 nike

我正在查看这个文件http://lxr.free-electrons.com/source/kernel/reboot.c

谁能告诉我这段代码的作用吗?

 void (*pm_power_off_prepare)(void);

这个文件是用来重启电脑的吧?

但是我发现了另一个重启文件

http://lxr.free-electrons.com/source/arch/x86/kernel/reboot.c

这个更大,有人能告诉我它们之间的区别吗?当您想重新启动电脑时,使用这两者中的哪一个。

最佳答案

kernel/reboot.c 文件是重启过程中与体系结构无关的部分。

arch/x86/kernel/reboot.c 和所有其他 arch/*/kernel/reboot.c 是某些函数的特定于体系结构的版本,由 kernel/reboot.c.

例如,在arch/x86/kernel/reboot.c中有machine_real_restart() function (假设我们使用 BIOS 启动,实际选择是在 native_machine_emergency_restart 中),这是最后调用的内核函数。它要求 BIOS 进行实际的重新启动:

 69 void __noreturn machine_real_restart(unsigned int type)
73 /*
74 * Write zero to CMOS register number 0x0f, which the BIOS POST
75 * routine will recognize as telling it to do a proper reboot. (Well
76 * that's what this book in front of me says -- it may only apply to
77 * the Phoenix BIOS though, it's not clear). At the same time,
78 * disable NMIs by setting the top bit in the CMOS address register,
79 * as we're about to do peculiar things to the CPU. I'm not sure if
80 * `outb_p' is needed instead of just `outb'. Use it to be on the
81 * safe side. (Yes, CMOS_WRITE does outb_p's. - Paul G.)
82 */

84 CMOS_WRITE(0x00, 0x8f);

96 /* Jump to the identity-mapped low memory code */
97 #ifdef CONFIG_X86_32
98 asm volatile("jmpl *%0" : :
99 "rm" (real_mode_header->machine_real_restart_asm),
100 "a" (type));

该函数是从 native_machine_emergency_restart() function 调用的,即registered作为machine_ops structure*emergency_restart函数指针。这个指针被 machine_emergency_restart 调用这是从架构独立部分 kernel/reboot.c 到 x86 特定部分的入口点:

emergency_restart() function from kernel/reboot.c :

 53 /**
54 * emergency_restart - reboot the system
55 *
56 * Without shutting down any hardware or taking any locks
57 * reboot the system. This is called when we know we are in
58 * trouble so this is our best effort to reboot. This is
59 * safe to call in interrupt context.
60 */
61 void emergency_restart(void)
62 {
63 kmsg_dump(KMSG_DUMP_EMERG);
64 machine_emergency_restart();
65 }
66 EXPORT_SYMBOL_GPL(emergency_restart);

对于正常关闭(不是像^^^^这样的紧急关闭),会构建类似的调用链。让我们从basic kernel/reboot.c, kernel_restart function开始:

125 /**
126 * kernel_restart - reboot the system
130 * Shutdown everything and perform a clean reboot.
131 * This is not safe to call in interrupt context.
132 */
133 void kernel_restart(char *cmd)
134 {
135 kernel_restart_prepare(cmd);
...
143 machine_restart(cmd);
144 }
145 EXPORT_SYMBOL_GPL(kernel_restart);

我们可以看到这个通用函数调用特定于体系结构的machine_restart来执行所有需要的操作来告诉硬件我们正在重新启动。在 x86 中它会做...是的,它会调用 restart function pointer from machine_ops :

658 void machine_restart(char *cmd)
659 {
660 machine_ops.restart(cmd);
661 }

X86 的 restart points to native_machine_restart 再次调用 __machine_emergency_restart,这将调用相同的 machine_real_restart

关于c - Linux内核文件重启,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23591971/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com