blob: f4960171bc66625ebac582a172990b0531031fcb [file] [log] [blame]
Eric W. Biederman5033cba2005-06-25 14:57:56 -07001/*
Dave Jones835c34a2007-10-12 21:10:53 -04002 * handle transition of Linux booting another kernel
Eric W. Biederman5033cba2005-06-25 14:57:56 -07003 * Copyright (C) 2002-2005 Eric Biederman <ebiederm@xmission.com>
4 *
5 * This source code is licensed under the GNU General Public License,
6 * Version 2. See the file COPYING for more details.
7 */
8
9#include <linux/mm.h>
10#include <linux/kexec.h>
11#include <linux/delay.h>
Rusty Russell1a3f2392006-09-26 10:52:32 +020012#include <linux/init.h>
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -070013#include <linux/numa.h>
Eric W. Biederman5033cba2005-06-25 14:57:56 -070014#include <asm/pgtable.h>
15#include <asm/pgalloc.h>
16#include <asm/tlbflush.h>
17#include <asm/mmu_context.h>
18#include <asm/io.h>
19#include <asm/apic.h>
20#include <asm/cpufeature.h>
Eric W. Biedermane7b47cc2005-07-29 13:01:18 -060021#include <asm/desc.h>
Zachary Amsden4bb0d3e2005-09-03 15:56:36 -070022#include <asm/system.h>
Eric W. Biederman5033cba2005-06-25 14:57:56 -070023
24#define PAGE_ALIGNED __attribute__ ((__aligned__(PAGE_SIZE)))
Magnus Damm35665612006-09-26 10:52:38 +020025static u32 kexec_pgd[1024] PAGE_ALIGNED;
26#ifdef CONFIG_X86_PAE
27static u32 kexec_pmd0[1024] PAGE_ALIGNED;
28static u32 kexec_pmd1[1024] PAGE_ALIGNED;
Eric W. Biederman5033cba2005-06-25 14:57:56 -070029#endif
Magnus Damm35665612006-09-26 10:52:38 +020030static u32 kexec_pte0[1024] PAGE_ALIGNED;
31static u32 kexec_pte1[1024] PAGE_ALIGNED;
Eric W. Biederman5033cba2005-06-25 14:57:56 -070032
Eric W. Biederman5033cba2005-06-25 14:57:56 -070033static void set_idt(void *newidt, __u16 limit)
34{
Glauber de Oliveira Costa6b68f012008-01-30 13:31:12 +010035 struct desc_ptr curidt;
Eric W. Biederman5033cba2005-06-25 14:57:56 -070036
37 /* ia32 supports unaliged loads & stores */
Eric W. Biedermane7b47cc2005-07-29 13:01:18 -060038 curidt.size = limit;
39 curidt.address = (unsigned long)newidt;
Eric W. Biederman5033cba2005-06-25 14:57:56 -070040
Zachary Amsdenf2ab4462005-09-03 15:56:42 -070041 load_idt(&curidt);
WANG Cong378fc6e2008-06-24 16:21:18 +010042}
Eric W. Biederman5033cba2005-06-25 14:57:56 -070043
44
45static void set_gdt(void *newgdt, __u16 limit)
46{
Glauber de Oliveira Costa6b68f012008-01-30 13:31:12 +010047 struct desc_ptr curgdt;
Eric W. Biederman5033cba2005-06-25 14:57:56 -070048
49 /* ia32 supports unaligned loads & stores */
Eric W. Biedermane7b47cc2005-07-29 13:01:18 -060050 curgdt.size = limit;
51 curgdt.address = (unsigned long)newgdt;
Eric W. Biederman5033cba2005-06-25 14:57:56 -070052
Zachary Amsdenf2ab4462005-09-03 15:56:42 -070053 load_gdt(&curgdt);
WANG Cong378fc6e2008-06-24 16:21:18 +010054}
Eric W. Biederman5033cba2005-06-25 14:57:56 -070055
56static void load_segments(void)
57{
58#define __STR(X) #X
59#define STR(X) __STR(X)
60
61 __asm__ __volatile__ (
62 "\tljmp $"STR(__KERNEL_CS)",$1f\n"
63 "\t1:\n"
Michael Matz2ec5e3a2006-03-07 21:55:48 -080064 "\tmovl $"STR(__KERNEL_DS)",%%eax\n"
65 "\tmovl %%eax,%%ds\n"
66 "\tmovl %%eax,%%es\n"
67 "\tmovl %%eax,%%fs\n"
68 "\tmovl %%eax,%%gs\n"
69 "\tmovl %%eax,%%ss\n"
70 ::: "eax", "memory");
Eric W. Biederman5033cba2005-06-25 14:57:56 -070071#undef STR
72#undef __STR
73}
74
Eric W. Biederman5033cba2005-06-25 14:57:56 -070075/*
76 * A architecture hook called to validate the
77 * proposed image and prepare the control pages
78 * as needed. The pages for KEXEC_CONTROL_CODE_SIZE
79 * have been allocated, but the segments have yet
80 * been copied into the kernel.
81 *
82 * Do what every setup is needed on image and the
83 * reboot code buffer to allow us to avoid allocations
84 * later.
85 *
86 * Currently nothing.
87 */
88int machine_kexec_prepare(struct kimage *image)
89{
90 return 0;
91}
92
93/*
94 * Undo anything leftover by machine_kexec_prepare
95 * when an image is freed.
96 */
97void machine_kexec_cleanup(struct kimage *image)
98{
99}
100
101/*
102 * Do not allocate memory (or fail in any way) in machine_kexec().
103 * We are past the point of no return, committed to rebooting now.
104 */
105NORET_TYPE void machine_kexec(struct kimage *image)
106{
Magnus Damm35665612006-09-26 10:52:38 +0200107 unsigned long page_list[PAGES_NR];
108 void *control_page;
Eric W. Biederman5033cba2005-06-25 14:57:56 -0700109
110 /* Interrupts aren't acceptable while we reboot */
111 local_irq_disable();
112
Magnus Damm35665612006-09-26 10:52:38 +0200113 control_page = page_address(image->control_code_page);
114 memcpy(control_page, relocate_kernel, PAGE_SIZE);
Eric W. Biederman5033cba2005-06-25 14:57:56 -0700115
Magnus Damm35665612006-09-26 10:52:38 +0200116 page_list[PA_CONTROL_PAGE] = __pa(control_page);
117 page_list[VA_CONTROL_PAGE] = (unsigned long)relocate_kernel;
118 page_list[PA_PGD] = __pa(kexec_pgd);
119 page_list[VA_PGD] = (unsigned long)kexec_pgd;
120#ifdef CONFIG_X86_PAE
121 page_list[PA_PMD_0] = __pa(kexec_pmd0);
122 page_list[VA_PMD_0] = (unsigned long)kexec_pmd0;
123 page_list[PA_PMD_1] = __pa(kexec_pmd1);
124 page_list[VA_PMD_1] = (unsigned long)kexec_pmd1;
125#endif
126 page_list[PA_PTE_0] = __pa(kexec_pte0);
127 page_list[VA_PTE_0] = (unsigned long)kexec_pte0;
128 page_list[PA_PTE_1] = __pa(kexec_pte1);
129 page_list[VA_PTE_1] = (unsigned long)kexec_pte1;
Eric W. Biederman5033cba2005-06-25 14:57:56 -0700130
Eric W. Biederman2a8a3d52006-07-30 03:03:20 -0700131 /* The segment registers are funny things, they have both a
132 * visible and an invisible part. Whenever the visible part is
133 * set to a specific selector, the invisible part is loaded
134 * with from a table in memory. At no other time is the
135 * descriptor table in memory accessed.
Eric W. Biederman5033cba2005-06-25 14:57:56 -0700136 *
137 * I take advantage of this here by force loading the
138 * segments, before I zap the gdt with an invalid value.
139 */
140 load_segments();
141 /* The gdt & idt are now invalid.
142 * If you want to load them you must set up your own idt & gdt.
143 */
144 set_gdt(phys_to_virt(0),0);
145 set_idt(phys_to_virt(0),0);
146
147 /* now call it */
Magnus Damm35665612006-09-26 10:52:38 +0200148 relocate_kernel((unsigned long)image->head, (unsigned long)page_list,
149 image->start, cpu_has_pae);
Eric W. Biederman5033cba2005-06-25 14:57:56 -0700150}
Rusty Russell1a3f2392006-09-26 10:52:32 +0200151
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -0700152void arch_crash_save_vmcoreinfo(void)
153{
Ken'ichi Ohmichi92df5c32008-02-07 00:15:23 -0800154#ifdef CONFIG_NUMA
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -0700155 VMCOREINFO_SYMBOL(node_data);
156 VMCOREINFO_LENGTH(node_data, MAX_NUMNODES);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -0700157#endif
158#ifdef CONFIG_X86_PAE
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -0700159 VMCOREINFO_CONFIG(X86_PAE);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -0700160#endif
161}
162