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/numa.h>
13 #include <linux/ftrace.h>
14 #include <linux/suspend.h>
15 #include <linux/gfp.h>
16 #include <linux/io.h>
17 
18 #include <asm/pgtable.h>
19 #include <asm/pgalloc.h>
20 #include <asm/tlbflush.h>
21 #include <asm/mmu_context.h>
22 #include <asm/apic.h>
23 #include <asm/io_apic.h>
24 #include <asm/cpufeature.h>
25 #include <asm/desc.h>
26 #include <asm/set_memory.h>
27 #include <asm/debugreg.h>
28 
29 static void set_idt(void *newidt, __u16 limit)
30 {
31 	struct desc_ptr curidt;
32 
33 	/* ia32 supports unaliged loads & stores */
34 	curidt.size    = limit;
35 	curidt.address = (unsigned long)newidt;
36 
37 	load_idt(&curidt);
38 }
39 
40 
41 static void set_gdt(void *newgdt, __u16 limit)
42 {
43 	struct desc_ptr curgdt;
44 
45 	/* ia32 supports unaligned loads & stores */
46 	curgdt.size    = limit;
47 	curgdt.address = (unsigned long)newgdt;
48 
49 	load_gdt(&curgdt);
50 }
51 
52 static void load_segments(void)
53 {
54 #define __STR(X) #X
55 #define STR(X) __STR(X)
56 
57 	__asm__ __volatile__ (
58 		"\tljmp $"STR(__KERNEL_CS)",$1f\n"
59 		"\t1:\n"
60 		"\tmovl $"STR(__KERNEL_DS)",%%eax\n"
61 		"\tmovl %%eax,%%ds\n"
62 		"\tmovl %%eax,%%es\n"
63 		"\tmovl %%eax,%%fs\n"
64 		"\tmovl %%eax,%%gs\n"
65 		"\tmovl %%eax,%%ss\n"
66 		: : : "eax", "memory");
67 #undef STR
68 #undef __STR
69 }
70 
71 static void machine_kexec_free_page_tables(struct kimage *image)
72 {
73 	free_page((unsigned long)image->arch.pgd);
74 #ifdef CONFIG_X86_PAE
75 	free_page((unsigned long)image->arch.pmd0);
76 	free_page((unsigned long)image->arch.pmd1);
77 #endif
78 	free_page((unsigned long)image->arch.pte0);
79 	free_page((unsigned long)image->arch.pte1);
80 }
81 
82 static int machine_kexec_alloc_page_tables(struct kimage *image)
83 {
84 	image->arch.pgd = (pgd_t *)get_zeroed_page(GFP_KERNEL);
85 #ifdef CONFIG_X86_PAE
86 	image->arch.pmd0 = (pmd_t *)get_zeroed_page(GFP_KERNEL);
87 	image->arch.pmd1 = (pmd_t *)get_zeroed_page(GFP_KERNEL);
88 #endif
89 	image->arch.pte0 = (pte_t *)get_zeroed_page(GFP_KERNEL);
90 	image->arch.pte1 = (pte_t *)get_zeroed_page(GFP_KERNEL);
91 	if (!image->arch.pgd ||
92 #ifdef CONFIG_X86_PAE
93 	    !image->arch.pmd0 || !image->arch.pmd1 ||
94 #endif
95 	    !image->arch.pte0 || !image->arch.pte1) {
96 		machine_kexec_free_page_tables(image);
97 		return -ENOMEM;
98 	}
99 	return 0;
100 }
101 
102 static void machine_kexec_page_table_set_one(
103 	pgd_t *pgd, pmd_t *pmd, pte_t *pte,
104 	unsigned long vaddr, unsigned long paddr)
105 {
106 	p4d_t *p4d;
107 	pud_t *pud;
108 
109 	pgd += pgd_index(vaddr);
110 #ifdef CONFIG_X86_PAE
111 	if (!(pgd_val(*pgd) & _PAGE_PRESENT))
112 		set_pgd(pgd, __pgd(__pa(pmd) | _PAGE_PRESENT));
113 #endif
114 	p4d = p4d_offset(pgd, vaddr);
115 	pud = pud_offset(p4d, vaddr);
116 	pmd = pmd_offset(pud, vaddr);
117 	if (!(pmd_val(*pmd) & _PAGE_PRESENT))
118 		set_pmd(pmd, __pmd(__pa(pte) | _PAGE_TABLE));
119 	pte = pte_offset_kernel(pmd, vaddr);
120 	set_pte(pte, pfn_pte(paddr >> PAGE_SHIFT, PAGE_KERNEL_EXEC));
121 }
122 
123 static void machine_kexec_prepare_page_tables(struct kimage *image)
124 {
125 	void *control_page;
126 	pmd_t *pmd = NULL;
127 
128 	control_page = page_address(image->control_code_page);
129 #ifdef CONFIG_X86_PAE
130 	pmd = image->arch.pmd0;
131 #endif
132 	machine_kexec_page_table_set_one(
133 		image->arch.pgd, pmd, image->arch.pte0,
134 		(unsigned long)control_page, __pa(control_page));
135 #ifdef CONFIG_X86_PAE
136 	pmd = image->arch.pmd1;
137 #endif
138 	machine_kexec_page_table_set_one(
139 		image->arch.pgd, pmd, image->arch.pte1,
140 		__pa(control_page), __pa(control_page));
141 }
142 
143 /*
144  * A architecture hook called to validate the
145  * proposed image and prepare the control pages
146  * as needed.  The pages for KEXEC_CONTROL_PAGE_SIZE
147  * have been allocated, but the segments have yet
148  * been copied into the kernel.
149  *
150  * Do what every setup is needed on image and the
151  * reboot code buffer to allow us to avoid allocations
152  * later.
153  *
154  * - Make control page executable.
155  * - Allocate page tables
156  * - Setup page tables
157  */
158 int machine_kexec_prepare(struct kimage *image)
159 {
160 	int error;
161 
162 	set_pages_x(image->control_code_page, 1);
163 	error = machine_kexec_alloc_page_tables(image);
164 	if (error)
165 		return error;
166 	machine_kexec_prepare_page_tables(image);
167 	return 0;
168 }
169 
170 /*
171  * Undo anything leftover by machine_kexec_prepare
172  * when an image is freed.
173  */
174 void machine_kexec_cleanup(struct kimage *image)
175 {
176 	set_pages_nx(image->control_code_page, 1);
177 	machine_kexec_free_page_tables(image);
178 }
179 
180 /*
181  * Do not allocate memory (or fail in any way) in machine_kexec().
182  * We are past the point of no return, committed to rebooting now.
183  */
184 void machine_kexec(struct kimage *image)
185 {
186 	unsigned long page_list[PAGES_NR];
187 	void *control_page;
188 	int save_ftrace_enabled;
189 	asmlinkage unsigned long
190 		(*relocate_kernel_ptr)(unsigned long indirection_page,
191 				       unsigned long control_page,
192 				       unsigned long start_address,
193 				       unsigned int has_pae,
194 				       unsigned int preserve_context);
195 
196 #ifdef CONFIG_KEXEC_JUMP
197 	if (image->preserve_context)
198 		save_processor_state();
199 #endif
200 
201 	save_ftrace_enabled = __ftrace_enabled_save();
202 
203 	/* Interrupts aren't acceptable while we reboot */
204 	local_irq_disable();
205 	hw_breakpoint_disable();
206 
207 	if (image->preserve_context) {
208 #ifdef CONFIG_X86_IO_APIC
209 		/*
210 		 * We need to put APICs in legacy mode so that we can
211 		 * get timer interrupts in second kernel. kexec/kdump
212 		 * paths already have calls to disable_IO_APIC() in
213 		 * one form or other. kexec jump path also need
214 		 * one.
215 		 */
216 		disable_IO_APIC();
217 #endif
218 	}
219 
220 	control_page = page_address(image->control_code_page);
221 	memcpy(control_page, relocate_kernel, KEXEC_CONTROL_CODE_MAX_SIZE);
222 
223 	relocate_kernel_ptr = control_page;
224 	page_list[PA_CONTROL_PAGE] = __pa(control_page);
225 	page_list[VA_CONTROL_PAGE] = (unsigned long)control_page;
226 	page_list[PA_PGD] = __pa(image->arch.pgd);
227 
228 	if (image->type == KEXEC_TYPE_DEFAULT)
229 		page_list[PA_SWAP_PAGE] = (page_to_pfn(image->swap_page)
230 						<< PAGE_SHIFT);
231 
232 	/*
233 	 * The segment registers are funny things, they have both a
234 	 * visible and an invisible part.  Whenever the visible part is
235 	 * set to a specific selector, the invisible part is loaded
236 	 * with from a table in memory.  At no other time is the
237 	 * descriptor table in memory accessed.
238 	 *
239 	 * I take advantage of this here by force loading the
240 	 * segments, before I zap the gdt with an invalid value.
241 	 */
242 	load_segments();
243 	/*
244 	 * The gdt & idt are now invalid.
245 	 * If you want to load them you must set up your own idt & gdt.
246 	 */
247 	set_gdt(phys_to_virt(0), 0);
248 	set_idt(phys_to_virt(0), 0);
249 
250 	/* now call it */
251 	image->start = relocate_kernel_ptr((unsigned long)image->head,
252 					   (unsigned long)page_list,
253 					   image->start,
254 					   boot_cpu_has(X86_FEATURE_PAE),
255 					   image->preserve_context);
256 
257 #ifdef CONFIG_KEXEC_JUMP
258 	if (image->preserve_context)
259 		restore_processor_state();
260 #endif
261 
262 	__ftrace_enabled_restore(save_ftrace_enabled);
263 }
264 
265 void arch_crash_save_vmcoreinfo(void)
266 {
267 #ifdef CONFIG_NUMA
268 	VMCOREINFO_SYMBOL(node_data);
269 	VMCOREINFO_LENGTH(node_data, MAX_NUMNODES);
270 #endif
271 #ifdef CONFIG_X86_PAE
272 	VMCOREINFO_CONFIG(X86_PAE);
273 #endif
274 }
275 
276