The shutdown and restart process in Linux is not important for general desktop applications and network servers, but it has some research significance in the User-Defined embedded system kernel, by understanding the process of Linux shutdown and restart, We can modify and customize it, and even develop new functions based on this.
1. Overview
In Linux, shutdown and restart may be caused by two actions: User Programming and system-generated messages. There are also two ways for users to interact with the system. One is System Call: sys_reboot, and the other is the APM or ACPI device file. You can also shut down or restart the system by performing operations on the file.
2. Restart sys_reboot by calling the system
This system call defines a series of magic_number. At the beginning of the call, check whether magic_number is correct. Only when it is correct can it continue to run downward. Switch to branch when restarting
Case linux_reboot_cmd_restart:
First, use notifier_call_chain to send a restart message to other parts, and then call the machine_restart function to complete the restart.
The start part of the machine_restart function has a piece of SMP-related code. When multiple CPUs are used, one CPU restarts and the other CPUs are waiting. The system then determines the Restart Method Based on the reboot_thru_bios variable. By reading reboot_setup, we can know that the content of this parameter is specified during system startup and determines whether to use BIOS, in fact, the program is restarted at the entry address (FFFF: 0000) after the system is reset. Without restarting the bios, the system first sets the restart flag, and then writes the number 0x64 to the port 0xfe. The specific principle of this restart is not clear yet, it seems to have simulated a reset key press. I hope you can discuss it with me. When the BIOS is restarted, the system first sets the restart mode and then switches to the actual mode. The restart is completed through an ljmp $0 xFFFF and $0x0.
3. shutdown by calling sys_reboot.
In the processing branch of the system call, we can see that the magic_number is checked first, and then
Case linux_reboot_cmd_power_off:
In the execution process, notifier_call_chain is used to send a message to turn off the computer power, and then the machine_power_off function is executed. We can see in the machine_power_off function that if the pointer of the pm_power_off function is not empty, the system will call this function to shut down. When APM has been loaded (except SMP), The pm_power_off function actually points to APM. apm_power_off in C. In this function, the system uses the value in the apm_info structure to switch to the actual mode for shutdown, or uses the apm_bios_call_simple function to call the APM interface in protection mode for shutdown.
4. APM driver Shutdown Process
APM uses the ioctl interface of its registered device to perform operations on APM. The processing branch can be seen in the do_ioctl function of APM. C. There is only suspend and standby code here, so we cannot use IOCTL to shut down APM.
When the user presses the power switch, if there is an APM module, the shutdown process is handled by APM. The APM driver starts an APM kernel thread: apm_mainloop during initialization. The system will detect the poweroff key message and name it apm_sys_suspend, to distinguish the apm_user_suspend mode set by APM-s. The next step is to enter the apm_event_handler function, and from the apm_event_handler function to the check_events function, which is the case branch corresponding to the processing function. The system also uses the suspend function for shutdown. However, due to other parameters, suspend finally calls the shutdown process.
5. Solve the Problem instance
1) some motherboard crashes when you press the power key
It is found that this problem occurs only after some specific drivers are loaded, and this problem does not occur when sys_reboot is called by the shutdown system. Analysis of the APM processing process, it is suspected that the driver did not properly process the inquiry message sent by the APM before the shutdown. Because some drivers do not have the source code, they decide to hack the shutdown part of APM. C, so that the shutdown of the two methods follows the same process. Therefore, the apm_sys_suspend part in the check_events function of APM. C is rewritten as the following code:
Ret = exec_usermodehelper (poweroff_helper_path, argv, envp );
If (RET ){
Printk (kern_err
"APM. C: failed to Exec % s, errno = % d // n ",
Poweroff_helper_path, errno );
}
Break;
For fast reboot support
Static unsigned char fast_reboot_switch [] =
{
0x66, 0x0f, 0x20, 0xc0,/* movl % Cr0, % eax */
0x66, 0x25, 0x10, 0x11, 0x11, 0x11,/* andl $0x11111110, % eax */
0x66, 0x0f, 0x22, 0xc0,/* movl % eax, % Cr0 */
0xea, 0x00, 0x00, 0x00, 0x70/* ljmp $0x7000, $0x0000 */
};
The system can switch to the real mode, and then jump to the 7000 H: 0 position to start execution.
6. ACPI Overview
In the 2.4.20 kernel, The ACPI module is marked as trial and incomplete, and some functions may not be implemented. If the APM and APCI modules are compiled into the kernel at the same time, the APM is loaded before the ACPI, And the APM function causes the ACPI to exit. The daemon program acpid is used to support system power usage and Power Supply Practice (mainly used in laptops.
There is no function similar to the application switching status of APM. The ACPI program only queries the status of ACPI. You can write numbers directly to the/proc/ACPI/Sleep file to implement the S0-S4 function. By reading the content in Cat, you can know which modes The system supports.
The source code of the ACPI module is in Linux/Drivers/ACPI/driver. in C, if you write something to the Sleep file, it is transferred to the sm_osl_proc_write_sleep function in the Linux/Drivers/ACPI/ospm/system/sm_osl.c file. This function then calls the sm_osl_suspend function. This function provides various functions, including protection of various States. The real sleep is completed by calling acpi_enter_sleep_state. This function is available in Linux/Drivers/ACPI/hardware/hwsleep. in the c file, the ACPI register is written here to bring the system into sleep state. The instructions for writing registers are in hwregs. C under this directory.
7. Summary
This article briefly introduces ACPI. In fact, ACPI will become the preferred power management method in Linux kernel in the future. Because the ACPI version in the official code is relatively low, it is not discussed in detail, and we hope that the future of nuclear energy will change.