Linux Kexec Introduction
The function of Kexec is to run a new kernel with a running kernel, just like running an application. This mechanism enables a fast restart of the system because it skips the bootloader. In addition Kdump is based on the KEXEC implementation (schematic below).
The implementation of Kexec has several difficulties:
- In the context of the current kernel, how do I replace the existing kernel with the new kernel?
- During normal reset startup, the device is reset (or initialized) to a known state. Skipping the reset phase, how to ensure that the device status is reliable when the new kernel kexec is started?
Examples of the use of kexec are divided into 2 parts: kexec kernel loading and kexec kernel execution.
Kexec-l/bzimage--initrd=/initrd.img.gz--append= "ro nosmap loglevel=4 console=ttys0,9600n8 acpi_rsdp=0x7b7fe014" KEXEC-E
Kexec Kernel Load
- Loading kernel image files, root file system, command line parameters and other segment into user-state memory;
- Segment SHA256 Check to ensure that the kernel data is not corrupted, if it is kdump, back up the relevant data to the backup area (such as i386, the initial 640K configuration data for the SMP kernel boot, need to backup, or PowerPC, fixed location of the exception vector information such as need to backup, etc.). This link is called purgatory, the process can be implemented in the user State control, but also in the Kernel State control implementation (such as enable--kexec-file-syscall option);
- Allocate the Kernel State page and copy the segment from the user-state memory to the kernel-state page, and if the Enable--kexec-file-syscall option, skip the first two steps and load the segment directly into the kernel page and purgatory.
- Assign the page and initialize the Image->control_code_page, create a page table for it, and for the next time overwrite the current kernel with the page table mappings for code addressing. Initializes the LEVEL4/3/2/1 4-tier page table and establishes a mapping relationship, as shown in.
Kexec kernel operation
- Call the device driver shutdown interface to turn off the device;
- Shut down interrupts, such as Io-apic, local IRQ, Lapic;
- Turn off non-No. 0 CPU cores;
- Clear the TLB (here the following code for the Assembly implementation, concrete implementation of arch-related, generally called relocate_new_kernel);
- Set up segment register, GDT, IDT, etc.;
- Create a new stack and press the entry address of the new kernel into the stack;
- Set CR0 Register: Enable paging function and page protection function;
- Set CR4 Register: Enable to extend the address;
- Set the CR3 register so that CR3 points to the new page table root, and after that, it bye bye with the old kernel;
- Copy the kernel segment page to the specified location, overwriting the current kernel;
- Call the RET instruction, the new kernel bzimage the entry address of the stack before it pops out, and enter the new kernel boot;
KEXEC Debugging Note implementation
- Kexec does not synchronize or unmount the file system, this process requires the user to ensure;
- From the above process can be seen, kexec does not reset the CPU or device, but the system restarts the process will call Reboot_notifier_list, so register_reboot_notifier registered interface do not have to perform CPU reset operations;
- To ensure that the device is in a stable state during kexec, Kexec invokes the device-driven shutdown interface to shut down, ensuring that the user's own device driver provides the correct shutdown interface, or that the user is otherwise closed;
--eof--
Linux kexec kernel Boot