xref: /linux/arch/x86/kernel/machine_kexec_32.c (revision 2d6ffcca623a9a16df6cdfbe8250b7a5904a5f5e)
1 /*
2  * handle transition of Linux booting another kernel
3  * 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>
12 #include <linux/init.h>
13 #include <linux/numa.h>
14 #include <linux/ftrace.h>
15 
16 #include <asm/pgtable.h>
17 #include <asm/pgalloc.h>
18 #include <asm/tlbflush.h>
19 #include <asm/mmu_context.h>
20 #include <asm/io.h>
21 #include <asm/apic.h>
22 #include <asm/cpufeature.h>
23 #include <asm/desc.h>
24 #include <asm/system.h>
25 
26 #define PAGE_ALIGNED __attribute__ ((__aligned__(PAGE_SIZE)))
27 static u32 kexec_pgd[1024] PAGE_ALIGNED;
28 #ifdef CONFIG_X86_PAE
29 static u32 kexec_pmd0[1024] PAGE_ALIGNED;
30 static u32 kexec_pmd1[1024] PAGE_ALIGNED;
31 #endif
32 static u32 kexec_pte0[1024] PAGE_ALIGNED;
33 static u32 kexec_pte1[1024] PAGE_ALIGNED;
34 
35 static void set_idt(void *newidt, __u16 limit)
36 {
37 	struct desc_ptr curidt;
38 
39 	/* ia32 supports unaliged loads & stores */
40 	curidt.size    = limit;
41 	curidt.address = (unsigned long)newidt;
42 
43 	load_idt(&curidt);
44 }
45 
46 
47 static void set_gdt(void *newgdt, __u16 limit)
48 {
49 	struct desc_ptr curgdt;
50 
51 	/* ia32 supports unaligned loads & stores */
52 	curgdt.size    = limit;
53 	curgdt.address = (unsigned long)newgdt;
54 
55 	load_gdt(&curgdt);
56 }
57 
58 static void load_segments(void)
59 {
60 #define __STR(X) #X
61 #define STR(X) __STR(X)
62 
63 	__asm__ __volatile__ (
64 		"\tljmp $"STR(__KERNEL_CS)",$1f\n"
65 		"\t1:\n"
66 		"\tmovl $"STR(__KERNEL_DS)",%%eax\n"
67 		"\tmovl %%eax,%%ds\n"
68 		"\tmovl %%eax,%%es\n"
69 		"\tmovl %%eax,%%fs\n"
70 		"\tmovl %%eax,%%gs\n"
71 		"\tmovl %%eax,%%ss\n"
72 		::: "eax", "memory");
73 #undef STR
74 #undef __STR
75 }
76 
77 /*
78  * A architecture hook called to validate the
79  * proposed image and prepare the control pages
80  * as needed.  The pages for KEXEC_CONTROL_CODE_SIZE
81  * have been allocated, but the segments have yet
82  * been copied into the kernel.
83  *
84  * Do what every setup is needed on image and the
85  * reboot code buffer to allow us to avoid allocations
86  * later.
87  *
88  * Currently nothing.
89  */
90 int machine_kexec_prepare(struct kimage *image)
91 {
92 	return 0;
93 }
94 
95 /*
96  * Undo anything leftover by machine_kexec_prepare
97  * when an image is freed.
98  */
99 void machine_kexec_cleanup(struct kimage *image)
100 {
101 }
102 
103 /*
104  * Do not allocate memory (or fail in any way) in machine_kexec().
105  * We are past the point of no return, committed to rebooting now.
106  */
107 NORET_TYPE void machine_kexec(struct kimage *image)
108 {
109 	unsigned long page_list[PAGES_NR];
110 	void *control_page;
111 
112 	tracer_disable();
113 
114 	/* Interrupts aren't acceptable while we reboot */
115 	local_irq_disable();
116 
117 	control_page = page_address(image->control_code_page);
118 	memcpy(control_page, relocate_kernel, PAGE_SIZE);
119 
120 	page_list[PA_CONTROL_PAGE] = __pa(control_page);
121 	page_list[VA_CONTROL_PAGE] = (unsigned long)relocate_kernel;
122 	page_list[PA_PGD] = __pa(kexec_pgd);
123 	page_list[VA_PGD] = (unsigned long)kexec_pgd;
124 #ifdef CONFIG_X86_PAE
125 	page_list[PA_PMD_0] = __pa(kexec_pmd0);
126 	page_list[VA_PMD_0] = (unsigned long)kexec_pmd0;
127 	page_list[PA_PMD_1] = __pa(kexec_pmd1);
128 	page_list[VA_PMD_1] = (unsigned long)kexec_pmd1;
129 #endif
130 	page_list[PA_PTE_0] = __pa(kexec_pte0);
131 	page_list[VA_PTE_0] = (unsigned long)kexec_pte0;
132 	page_list[PA_PTE_1] = __pa(kexec_pte1);
133 	page_list[VA_PTE_1] = (unsigned long)kexec_pte1;
134 
135 	/* The segment registers are funny things, they have both a
136 	 * visible and an invisible part.  Whenever the visible part is
137 	 * set to a specific selector, the invisible part is loaded
138 	 * with from a table in memory.  At no other time is the
139 	 * descriptor table in memory accessed.
140 	 *
141 	 * I take advantage of this here by force loading the
142 	 * segments, before I zap the gdt with an invalid value.
143 	 */
144 	load_segments();
145 	/* The gdt & idt are now invalid.
146 	 * If you want to load them you must set up your own idt & gdt.
147 	 */
148 	set_gdt(phys_to_virt(0),0);
149 	set_idt(phys_to_virt(0),0);
150 
151 	/* now call it */
152 	relocate_kernel((unsigned long)image->head, (unsigned long)page_list,
153 			image->start, cpu_has_pae);
154 }
155 
156 void arch_crash_save_vmcoreinfo(void)
157 {
158 #ifdef CONFIG_NUMA
159 	VMCOREINFO_SYMBOL(node_data);
160 	VMCOREINFO_LENGTH(node_data, MAX_NUMNODES);
161 #endif
162 #ifdef CONFIG_X86_PAE
163 	VMCOREINFO_CONFIG(X86_PAE);
164 #endif
165 }
166 
167