Linux shutdown restart process analysis

  

Linux shutdown and restart process is not important for general desktop applications and network servers, but in the user-defined embedded system kernel has a certain research significance, through understanding The process of Linux shutdown and restart, we can modify and customize it, and even develop new features based on this.
1. Overview

shutdown and restart under linux may be caused by two acts, one is programmed by the user, one is the message generated by the system itself. There are two ways for the user to interact with the system. One is the system call: sys_reboot, and the other is the device file of apm or acpi. The system can also be shut down or restarted by operating it.

2. sys_reboot calls through the system restart

This system call defines a set of MAGIC_NUMBER, first check MAGIC_NUMBER is correct at the beginning of the call, only to run down the right before continuing. When the steering restart branch

case LINUX_REBOOT_CMD_RESTART:

use notifier_call_chain first message sent to the other parts of the reboot, and then calls the function machine_restart reboot.

machine_restart beginning of the function codes associated with some SMP, multiple CPU upon completion of a main CPU restart after the other CPU is in a wait state. After that, the system judges the restart mode according to the content of a variable reboot_thru_bios. By reading reboot_setup, we can know that the content of this parameter is specified at system startup, and determines whether to use bios. In fact, it is the entry after system reset (FFFF: 0000). The program of the address is restarted. In the case of restart without bios, the system first sets the restart flag, and then writes the number 0x64 to port 0xfe. The specific principle of this restart is not clear, it seems to simulate a press of a reset button. I hope everyone will discuss with me. In the case of restarting through bios, the system also sets the restart mode first, then switches to the real mode, and restarts through a ljmp $0xffff, $0x0.

3. sys_reboot calls through the system to shut down

on processing branch system calls, we can see that the same is first checked MAGIC_NUMBER, then LINUX_REBOOT_CMD_POWER_OFF in

case:

execution flow inside, and is used to turn off the computer notifier_call_chain sent message, the machine_power_off functions performed immediately. We can see in the machine_power_off function, if the pm_power_off function pointer is not empty, then the system will call this function to shut down. In the case that apm has been loaded (except SMP), the pm_power_off function actually points to apm_power_off in apm.c. In this function, the system uses the value in the apm_info structure to switch to real mode shutdown, or use the apm_bios_call_simple function. Call the apm interface in protected mode to shut down two methods.

shutdown process itself driven 4.apm

apm registered device using the ioctl interface apm completion operations in the do_ioctl apm.c function can be seen in the branch processing. There are only suspend and standby code, so we can't use apm shutdown via ioctl.

When the user presses a POWER switch, if there is apm module, the shutdown process is handled apm. The apm driver starts an apm kernel thread at initialization: apm_mainloop, where the system detects the POWEROFF button message and names it APM_SYS_SUSPEND to distinguish the APM_USER_SUSPEND mode set by apm -s. Then enter the apm_event_handler function, and enter the check_events function from the apm_event_handler function, on the case branch corresponding to the processing function. The system also uses the suspend function to shut down, but due to other parameters, suspend finally calls the shutdown process. Example 5. The problem



. 1)

certain motherboards crash investigation this case will appear only after certain time by POWER key driving load, And there is no such problem when using the shutdown system to call sys_reboot. Analyze the processing flow of apm, suspected that the driver did not correctly process the inquiry message sent by apm before shutting down. Since some drivers do not have source code, it is decided to hack the shutdown part of apm.c and let the two methods shut down the same process. Thus the function of apm.c of check_events APM_SYS_SUSPEND rewritten as the following code section:


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 * /

};



system can switch to real mode, and then jumps to 7000H: 0 begin position.

6.ACPI

outlined in ACPI 2.4.20 kernel module and is noted as the test is not completed, there may not be part of the functions realized. If both APM and APCI modules are compiled into the kernel at the same time, APM is loaded before ACPI, and APM acts to cause ACPI to exit. Support for system power, power practice, etc. (mainly useful on laptops) relies on the acpid daemon.

not have a feature similar to the apm application switching state, acpi program just completed inquiries on acpi state. The user can implement the function of S0-S4 by directly writing the number to the /proc/acpi/sleep file. By reading (cat) the contents, you can know which modes the system supports.

acpi module source code for the main program in linux /drivers /acpi /driver.c, if you write something to sleep file, go to the linux /drivers /acpi /ospm /system /sm_osl.c file In the sm_osl_proc_write_sleep function, this function later calls the sm_osl_suspend function. Various functions are implemented in this function, including protection of various states. The final real sleep is done by calling acpi_enter_sleep_state. This function is in the linux/drivers/acpi/hardware/hwsleep.c file, where the acpi register is written to put the system into sleep state. The instructions for writing registers are in hwregs.c below this directory.

7. summary

This article describes acpi is very simple, in fact, ACPI will become the first choice for future linux kernel power management modes. Due to the low ACPI version in the official code, there is no detailed discussion and I hope that the kernel will change in the future.

Copyright © Windows knowledge All Rights Reserved