2005-06-26 05:57:56 +08:00
|
|
|
/*
|
2007-10-13 09:10:53 +08:00
|
|
|
* handle transition of Linux booting another kernel
|
2005-06-26 05:57:56 +08:00
|
|
|
* Copyright (C) 2002-2005 Eric Biederman <ebiederm@xmission.com>
|
|
|
|
*
|
|
|
|
* This source code is licensed under the GNU General Public License,
|
|
|
|
* Version 2. See the file COPYING for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/mm.h>
|
|
|
|
#include <linux/kexec.h>
|
|
|
|
#include <linux/delay.h>
|
2006-09-26 16:52:32 +08:00
|
|
|
#include <linux/init.h>
|
2007-10-17 14:27:27 +08:00
|
|
|
#include <linux/numa.h>
|
2008-05-13 03:20:43 +08:00
|
|
|
#include <linux/ftrace.h>
|
2008-08-15 15:40:26 +08:00
|
|
|
#include <linux/suspend.h>
|
2008-10-31 09:48:08 +08:00
|
|
|
#include <linux/gfp.h>
|
2008-05-13 03:20:43 +08:00
|
|
|
|
2005-06-26 05:57:56 +08:00
|
|
|
#include <asm/pgtable.h>
|
|
|
|
#include <asm/pgalloc.h>
|
|
|
|
#include <asm/tlbflush.h>
|
|
|
|
#include <asm/mmu_context.h>
|
|
|
|
#include <asm/io.h>
|
|
|
|
#include <asm/apic.h>
|
|
|
|
#include <asm/cpufeature.h>
|
2005-07-30 03:01:18 +08:00
|
|
|
#include <asm/desc.h>
|
2005-09-04 06:56:36 +08:00
|
|
|
#include <asm/system.h>
|
2008-07-26 10:45:07 +08:00
|
|
|
#include <asm/cacheflush.h>
|
2005-06-26 05:57:56 +08:00
|
|
|
|
|
|
|
static void set_idt(void *newidt, __u16 limit)
|
|
|
|
{
|
2008-01-30 20:31:12 +08:00
|
|
|
struct desc_ptr curidt;
|
2005-06-26 05:57:56 +08:00
|
|
|
|
|
|
|
/* ia32 supports unaliged loads & stores */
|
2005-07-30 03:01:18 +08:00
|
|
|
curidt.size = limit;
|
|
|
|
curidt.address = (unsigned long)newidt;
|
2005-06-26 05:57:56 +08:00
|
|
|
|
2005-09-04 06:56:42 +08:00
|
|
|
load_idt(&curidt);
|
2008-06-24 23:21:18 +08:00
|
|
|
}
|
2005-06-26 05:57:56 +08:00
|
|
|
|
|
|
|
|
|
|
|
static void set_gdt(void *newgdt, __u16 limit)
|
|
|
|
{
|
2008-01-30 20:31:12 +08:00
|
|
|
struct desc_ptr curgdt;
|
2005-06-26 05:57:56 +08:00
|
|
|
|
|
|
|
/* ia32 supports unaligned loads & stores */
|
2005-07-30 03:01:18 +08:00
|
|
|
curgdt.size = limit;
|
|
|
|
curgdt.address = (unsigned long)newgdt;
|
2005-06-26 05:57:56 +08:00
|
|
|
|
2005-09-04 06:56:42 +08:00
|
|
|
load_gdt(&curgdt);
|
2008-06-24 23:21:18 +08:00
|
|
|
}
|
2005-06-26 05:57:56 +08:00
|
|
|
|
|
|
|
static void load_segments(void)
|
|
|
|
{
|
|
|
|
#define __STR(X) #X
|
|
|
|
#define STR(X) __STR(X)
|
|
|
|
|
|
|
|
__asm__ __volatile__ (
|
|
|
|
"\tljmp $"STR(__KERNEL_CS)",$1f\n"
|
|
|
|
"\t1:\n"
|
2006-03-08 13:55:48 +08:00
|
|
|
"\tmovl $"STR(__KERNEL_DS)",%%eax\n"
|
|
|
|
"\tmovl %%eax,%%ds\n"
|
|
|
|
"\tmovl %%eax,%%es\n"
|
|
|
|
"\tmovl %%eax,%%fs\n"
|
|
|
|
"\tmovl %%eax,%%gs\n"
|
|
|
|
"\tmovl %%eax,%%ss\n"
|
|
|
|
::: "eax", "memory");
|
2005-06-26 05:57:56 +08:00
|
|
|
#undef STR
|
|
|
|
#undef __STR
|
|
|
|
}
|
|
|
|
|
2008-10-31 09:48:08 +08:00
|
|
|
static void machine_kexec_free_page_tables(struct kimage *image)
|
|
|
|
{
|
|
|
|
free_page((unsigned long)image->arch.pgd);
|
|
|
|
#ifdef CONFIG_X86_PAE
|
|
|
|
free_page((unsigned long)image->arch.pmd0);
|
|
|
|
free_page((unsigned long)image->arch.pmd1);
|
|
|
|
#endif
|
|
|
|
free_page((unsigned long)image->arch.pte0);
|
|
|
|
free_page((unsigned long)image->arch.pte1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int machine_kexec_alloc_page_tables(struct kimage *image)
|
|
|
|
{
|
|
|
|
image->arch.pgd = (pgd_t *)get_zeroed_page(GFP_KERNEL);
|
|
|
|
#ifdef CONFIG_X86_PAE
|
|
|
|
image->arch.pmd0 = (pmd_t *)get_zeroed_page(GFP_KERNEL);
|
|
|
|
image->arch.pmd1 = (pmd_t *)get_zeroed_page(GFP_KERNEL);
|
|
|
|
#endif
|
|
|
|
image->arch.pte0 = (pte_t *)get_zeroed_page(GFP_KERNEL);
|
|
|
|
image->arch.pte1 = (pte_t *)get_zeroed_page(GFP_KERNEL);
|
|
|
|
if (!image->arch.pgd ||
|
|
|
|
#ifdef CONFIG_X86_PAE
|
|
|
|
!image->arch.pmd0 || !image->arch.pmd1 ||
|
|
|
|
#endif
|
|
|
|
!image->arch.pte0 || !image->arch.pte1) {
|
|
|
|
machine_kexec_free_page_tables(image);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-10-31 09:48:15 +08:00
|
|
|
static void machine_kexec_page_table_set_one(
|
|
|
|
pgd_t *pgd, pmd_t *pmd, pte_t *pte,
|
|
|
|
unsigned long vaddr, unsigned long paddr)
|
|
|
|
{
|
|
|
|
pud_t *pud;
|
|
|
|
|
|
|
|
pgd += pgd_index(vaddr);
|
|
|
|
#ifdef CONFIG_X86_PAE
|
|
|
|
if (!(pgd_val(*pgd) & _PAGE_PRESENT))
|
|
|
|
set_pgd(pgd, __pgd(__pa(pmd) | _PAGE_PRESENT));
|
|
|
|
#endif
|
|
|
|
pud = pud_offset(pgd, vaddr);
|
|
|
|
pmd = pmd_offset(pud, vaddr);
|
|
|
|
if (!(pmd_val(*pmd) & _PAGE_PRESENT))
|
|
|
|
set_pmd(pmd, __pmd(__pa(pte) | _PAGE_TABLE));
|
|
|
|
pte = pte_offset_kernel(pmd, vaddr);
|
|
|
|
set_pte(pte, pfn_pte(paddr >> PAGE_SHIFT, PAGE_KERNEL_EXEC));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void machine_kexec_prepare_page_tables(struct kimage *image)
|
|
|
|
{
|
|
|
|
void *control_page;
|
2009-02-22 08:00:57 +08:00
|
|
|
pmd_t *pmd = NULL;
|
2008-10-31 09:48:15 +08:00
|
|
|
|
|
|
|
control_page = page_address(image->control_code_page);
|
|
|
|
#ifdef CONFIG_X86_PAE
|
|
|
|
pmd = image->arch.pmd0;
|
|
|
|
#endif
|
|
|
|
machine_kexec_page_table_set_one(
|
|
|
|
image->arch.pgd, pmd, image->arch.pte0,
|
|
|
|
(unsigned long)control_page, __pa(control_page));
|
|
|
|
#ifdef CONFIG_X86_PAE
|
|
|
|
pmd = image->arch.pmd1;
|
|
|
|
#endif
|
|
|
|
machine_kexec_page_table_set_one(
|
|
|
|
image->arch.pgd, pmd, image->arch.pte1,
|
|
|
|
__pa(control_page), __pa(control_page));
|
|
|
|
}
|
|
|
|
|
2005-06-26 05:57:56 +08:00
|
|
|
/*
|
|
|
|
* A architecture hook called to validate the
|
|
|
|
* proposed image and prepare the control pages
|
2008-08-15 15:40:22 +08:00
|
|
|
* as needed. The pages for KEXEC_CONTROL_PAGE_SIZE
|
2005-06-26 05:57:56 +08:00
|
|
|
* have been allocated, but the segments have yet
|
|
|
|
* been copied into the kernel.
|
|
|
|
*
|
|
|
|
* Do what every setup is needed on image and the
|
|
|
|
* reboot code buffer to allow us to avoid allocations
|
|
|
|
* later.
|
|
|
|
*
|
2008-10-31 09:48:08 +08:00
|
|
|
* - Make control page executable.
|
|
|
|
* - Allocate page tables
|
2008-10-31 09:48:15 +08:00
|
|
|
* - Setup page tables
|
2005-06-26 05:57:56 +08:00
|
|
|
*/
|
|
|
|
int machine_kexec_prepare(struct kimage *image)
|
|
|
|
{
|
2008-10-31 09:48:15 +08:00
|
|
|
int error;
|
|
|
|
|
2008-07-26 10:45:07 +08:00
|
|
|
if (nx_enabled)
|
|
|
|
set_pages_x(image->control_code_page, 1);
|
2008-10-31 09:48:15 +08:00
|
|
|
error = machine_kexec_alloc_page_tables(image);
|
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
machine_kexec_prepare_page_tables(image);
|
|
|
|
return 0;
|
2005-06-26 05:57:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Undo anything leftover by machine_kexec_prepare
|
|
|
|
* when an image is freed.
|
|
|
|
*/
|
|
|
|
void machine_kexec_cleanup(struct kimage *image)
|
|
|
|
{
|
2008-07-26 10:45:07 +08:00
|
|
|
if (nx_enabled)
|
|
|
|
set_pages_nx(image->control_code_page, 1);
|
2008-10-31 09:48:08 +08:00
|
|
|
machine_kexec_free_page_tables(image);
|
2005-06-26 05:57:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Do not allocate memory (or fail in any way) in machine_kexec().
|
|
|
|
* We are past the point of no return, committed to rebooting now.
|
|
|
|
*/
|
2008-07-26 10:45:07 +08:00
|
|
|
void machine_kexec(struct kimage *image)
|
2005-06-26 05:57:56 +08:00
|
|
|
{
|
2006-09-26 16:52:38 +08:00
|
|
|
unsigned long page_list[PAGES_NR];
|
|
|
|
void *control_page;
|
2008-08-15 15:40:26 +08:00
|
|
|
int save_ftrace_enabled;
|
2008-07-26 10:45:07 +08:00
|
|
|
asmlinkage unsigned long
|
|
|
|
(*relocate_kernel_ptr)(unsigned long indirection_page,
|
|
|
|
unsigned long control_page,
|
|
|
|
unsigned long start_address,
|
|
|
|
unsigned int has_pae,
|
|
|
|
unsigned int preserve_context);
|
2005-06-26 05:57:56 +08:00
|
|
|
|
2008-08-15 15:40:26 +08:00
|
|
|
#ifdef CONFIG_KEXEC_JUMP
|
|
|
|
if (kexec_image->preserve_context)
|
|
|
|
save_processor_state();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
save_ftrace_enabled = __ftrace_enabled_save();
|
2008-05-13 03:20:43 +08:00
|
|
|
|
2005-06-26 05:57:56 +08:00
|
|
|
/* Interrupts aren't acceptable while we reboot */
|
|
|
|
local_irq_disable();
|
|
|
|
|
kexec jump: save/restore device state
This patch implements devices state save/restore before after kexec.
This patch together with features in kexec_jump patch can be used for
following:
- A simple hibernation implementation without ACPI support. You can kexec a
hibernating kernel, save the memory image of original system and shutdown
the system. When resuming, you restore the memory image of original system
via ordinary kexec load then jump back.
- Kernel/system debug through making system snapshot. You can make system
snapshot, jump back, do some thing and make another system snapshot.
- Cooperative multi-kernel/system. With kexec jump, you can switch between
several kernels/systems quickly without boot process except the first time.
This appears like swap a whole kernel/system out/in.
- A general method to call program in physical mode (paging turning
off). This can be used to invoke BIOS code under Linux.
The following user-space tools can be used with kexec jump:
- kexec-tools needs to be patched to support kexec jump. The patches
and the precompiled kexec can be download from the following URL:
source: http://khibernation.sourceforge.net/download/release_v10/kexec-tools/kexec-tools-src_git_kh10.tar.bz2
patches: http://khibernation.sourceforge.net/download/release_v10/kexec-tools/kexec-tools-patches_git_kh10.tar.bz2
binary: http://khibernation.sourceforge.net/download/release_v10/kexec-tools/kexec_git_kh10
- makedumpfile with patches are used as memory image saving tool, it
can exclude free pages from original kernel memory image file. The
patches and the precompiled makedumpfile can be download from the
following URL:
source: http://khibernation.sourceforge.net/download/release_v10/makedumpfile/makedumpfile-src_cvs_kh10.tar.bz2
patches: http://khibernation.sourceforge.net/download/release_v10/makedumpfile/makedumpfile-patches_cvs_kh10.tar.bz2
binary: http://khibernation.sourceforge.net/download/release_v10/makedumpfile/makedumpfile_cvs_kh10
- An initramfs image can be used as the root file system of kexeced
kernel. An initramfs image built with "BuildRoot" can be downloaded
from the following URL:
initramfs image: http://khibernation.sourceforge.net/download/release_v10/initramfs/rootfs_cvs_kh10.gz
All user space tools above are included in the initramfs image.
Usage example of simple hibernation:
1. Compile and install patched kernel with following options selected:
CONFIG_X86_32=y
CONFIG_RELOCATABLE=y
CONFIG_KEXEC=y
CONFIG_CRASH_DUMP=y
CONFIG_PM=y
CONFIG_HIBERNATION=y
CONFIG_KEXEC_JUMP=y
2. Build an initramfs image contains kexec-tool and makedumpfile, or
download the pre-built initramfs image, called rootfs.gz in
following text.
3. Prepare a partition to save memory image of original kernel, called
hibernating partition in following text.
4. Boot kernel compiled in step 1 (kernel A).
5. In the kernel A, load kernel compiled in step 1 (kernel B) with
/sbin/kexec. The shell command line can be as follow:
/sbin/kexec --load-preserve-context /boot/bzImage --mem-min=0x100000
--mem-max=0xffffff --initrd=rootfs.gz
6. Boot the kernel B with following shell command line:
/sbin/kexec -e
7. The kernel B will boot as normal kexec. In kernel B the memory
image of kernel A can be saved into hibernating partition as
follow:
jump_back_entry=`cat /proc/cmdline | tr ' ' '\n' | grep kexec_jump_back_entry | cut -d '='`
echo $jump_back_entry > kexec_jump_back_entry
cp /proc/vmcore dump.elf
Then you can shutdown the machine as normal.
8. Boot kernel compiled in step 1 (kernel C). Use the rootfs.gz as
root file system.
9. In kernel C, load the memory image of kernel A as follow:
/sbin/kexec -l --args-none --entry=`cat kexec_jump_back_entry` dump.elf
10. Jump back to the kernel A as follow:
/sbin/kexec -e
Then, kernel A is resumed.
Implementation point:
To support jumping between two kernels, before jumping to (executing)
the new kernel and jumping back to the original kernel, the devices
are put into quiescent state, and the state of devices and CPU is
saved. After jumping back from kexeced kernel and jumping to the new
kernel, the state of devices and CPU are restored accordingly. The
devices/CPU state save/restore code of software suspend is called to
implement corresponding function.
Known issues:
- Because the segment number supported by sys_kexec_load is limited,
hibernation image with many segments may not be load. This is
planned to be eliminated by adding a new flag to sys_kexec_load to
make a image can be loaded with multiple sys_kexec_load invoking.
Now, only the i386 architecture is supported.
Signed-off-by: Huang Ying <ying.huang@intel.com>
Acked-by: Vivek Goyal <vgoyal@redhat.com>
Cc: "Eric W. Biederman" <ebiederm@xmission.com>
Cc: Pavel Machek <pavel@ucw.cz>
Cc: Nigel Cunningham <nigel@nigel.suspend2.net>
Cc: "Rafael J. Wysocki" <rjw@sisk.pl>
Cc: Ingo Molnar <mingo@elte.hu>
Cc: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2008-07-26 10:45:10 +08:00
|
|
|
if (image->preserve_context) {
|
|
|
|
#ifdef CONFIG_X86_IO_APIC
|
|
|
|
/* We need to put APICs in legacy mode so that we can
|
|
|
|
* get timer interrupts in second kernel. kexec/kdump
|
|
|
|
* paths already have calls to disable_IO_APIC() in
|
|
|
|
* one form or other. kexec jump path also need
|
|
|
|
* one.
|
|
|
|
*/
|
|
|
|
disable_IO_APIC();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2006-09-26 16:52:38 +08:00
|
|
|
control_page = page_address(image->control_code_page);
|
2008-08-15 15:40:23 +08:00
|
|
|
memcpy(control_page, relocate_kernel, KEXEC_CONTROL_CODE_MAX_SIZE);
|
2006-09-26 16:52:38 +08:00
|
|
|
|
2008-07-26 10:45:07 +08:00
|
|
|
relocate_kernel_ptr = control_page;
|
2006-09-26 16:52:38 +08:00
|
|
|
page_list[PA_CONTROL_PAGE] = __pa(control_page);
|
2008-07-26 10:45:07 +08:00
|
|
|
page_list[VA_CONTROL_PAGE] = (unsigned long)control_page;
|
2008-10-31 09:48:08 +08:00
|
|
|
page_list[PA_PGD] = __pa(image->arch.pgd);
|
2008-10-20 12:51:52 +08:00
|
|
|
|
|
|
|
if (image->type == KEXEC_TYPE_DEFAULT)
|
|
|
|
page_list[PA_SWAP_PAGE] = (page_to_pfn(image->swap_page)
|
|
|
|
<< PAGE_SHIFT);
|
2005-06-26 05:57:56 +08:00
|
|
|
|
2006-07-30 18:03:20 +08:00
|
|
|
/* The segment registers are funny things, they have both a
|
|
|
|
* visible and an invisible part. Whenever the visible part is
|
|
|
|
* set to a specific selector, the invisible part is loaded
|
|
|
|
* with from a table in memory. At no other time is the
|
|
|
|
* descriptor table in memory accessed.
|
2005-06-26 05:57:56 +08:00
|
|
|
*
|
|
|
|
* I take advantage of this here by force loading the
|
|
|
|
* segments, before I zap the gdt with an invalid value.
|
|
|
|
*/
|
|
|
|
load_segments();
|
|
|
|
/* The gdt & idt are now invalid.
|
|
|
|
* If you want to load them you must set up your own idt & gdt.
|
|
|
|
*/
|
|
|
|
set_gdt(phys_to_virt(0),0);
|
|
|
|
set_idt(phys_to_virt(0),0);
|
|
|
|
|
|
|
|
/* now call it */
|
2008-07-26 10:45:07 +08:00
|
|
|
image->start = relocate_kernel_ptr((unsigned long)image->head,
|
|
|
|
(unsigned long)page_list,
|
|
|
|
image->start, cpu_has_pae,
|
|
|
|
image->preserve_context);
|
2008-08-15 15:40:26 +08:00
|
|
|
|
|
|
|
#ifdef CONFIG_KEXEC_JUMP
|
|
|
|
if (kexec_image->preserve_context)
|
|
|
|
restore_processor_state();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
__ftrace_enabled_restore(save_ftrace_enabled);
|
2005-06-26 05:57:56 +08:00
|
|
|
}
|
2006-09-26 16:52:32 +08:00
|
|
|
|
2007-10-17 14:27:27 +08:00
|
|
|
void arch_crash_save_vmcoreinfo(void)
|
|
|
|
{
|
2008-02-07 16:15:23 +08:00
|
|
|
#ifdef CONFIG_NUMA
|
2007-10-17 14:27:30 +08:00
|
|
|
VMCOREINFO_SYMBOL(node_data);
|
|
|
|
VMCOREINFO_LENGTH(node_data, MAX_NUMNODES);
|
2007-10-17 14:27:27 +08:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_X86_PAE
|
2007-10-17 14:27:30 +08:00
|
|
|
VMCOREINFO_CONFIG(X86_PAE);
|
2007-10-17 14:27:27 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|