Linux shutdown and restart Process Analysis

Source: Internet
Author: User
Tags 0xc0 apm
Linux shutdown and restart process analysis-general Linux technology-Linux programming and kernel information. The following is a detailed description. 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.

References

Linux-2.4.20 source code


About the author

Fan xiaoju, an embedded R & D Engineer at the software design center of Lenovo (Beijing) Co., Ltd., is interested in Linux kernel, network security, XWindow system, Linux desktop applications, and artificial intelligence systems. You can contact him via xiaoju_f@263.net.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.