blob: 0497e3bd5bfff8718547a1a270d3ee36d59c74c1 [file] [log] [blame]
Eric W. Biederman5234f5e2005-06-25 14:58:02 -07001/*
2 * machine_kexec.c - 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>
Eric W. Biederman5234f5e2005-06-25 14:58:02 -070011#include <linux/string.h>
12#include <linux/reboot.h>
Eric W. Biederman5234f5e2005-06-25 14:58:02 -070013#include <asm/pgtable.h>
Eric W. Biederman5234f5e2005-06-25 14:58:02 -070014#include <asm/tlbflush.h>
15#include <asm/mmu_context.h>
16#include <asm/io.h>
Eric W. Biederman5234f5e2005-06-25 14:58:02 -070017
Magnus Damm4bfaaef2006-09-26 10:52:38 +020018#define PAGE_ALIGNED __attribute__ ((__aligned__(PAGE_SIZE)))
19static u64 kexec_pgd[512] PAGE_ALIGNED;
20static u64 kexec_pud0[512] PAGE_ALIGNED;
21static u64 kexec_pmd0[512] PAGE_ALIGNED;
22static u64 kexec_pte0[512] PAGE_ALIGNED;
23static u64 kexec_pud1[512] PAGE_ALIGNED;
24static u64 kexec_pmd1[512] PAGE_ALIGNED;
25static u64 kexec_pte1[512] PAGE_ALIGNED;
26
Eric W. Biederman8bf27552005-07-29 13:25:28 -060027static void init_level2_page(pmd_t *level2p, unsigned long addr)
Eric W. Biederman5234f5e2005-06-25 14:58:02 -070028{
29 unsigned long end_addr;
Maneesh Soni72414d32005-06-25 14:58:28 -070030
Eric W. Biederman5234f5e2005-06-25 14:58:02 -070031 addr &= PAGE_MASK;
Eric W. Biederman8bf27552005-07-29 13:25:28 -060032 end_addr = addr + PUD_SIZE;
Maneesh Soni72414d32005-06-25 14:58:28 -070033 while (addr < end_addr) {
Eric W. Biederman8bf27552005-07-29 13:25:28 -060034 set_pmd(level2p++, __pmd(addr | __PAGE_KERNEL_LARGE_EXEC));
35 addr += PMD_SIZE;
Eric W. Biederman5234f5e2005-06-25 14:58:02 -070036 }
37}
38
Eric W. Biederman8bf27552005-07-29 13:25:28 -060039static int init_level3_page(struct kimage *image, pud_t *level3p,
Maneesh Soni72414d32005-06-25 14:58:28 -070040 unsigned long addr, unsigned long last_addr)
Eric W. Biederman5234f5e2005-06-25 14:58:02 -070041{
42 unsigned long end_addr;
43 int result;
Maneesh Soni72414d32005-06-25 14:58:28 -070044
Eric W. Biederman5234f5e2005-06-25 14:58:02 -070045 result = 0;
46 addr &= PAGE_MASK;
Eric W. Biederman8bf27552005-07-29 13:25:28 -060047 end_addr = addr + PGDIR_SIZE;
Maneesh Soni72414d32005-06-25 14:58:28 -070048 while ((addr < last_addr) && (addr < end_addr)) {
Eric W. Biederman5234f5e2005-06-25 14:58:02 -070049 struct page *page;
Eric W. Biederman8bf27552005-07-29 13:25:28 -060050 pmd_t *level2p;
Maneesh Soni72414d32005-06-25 14:58:28 -070051
Eric W. Biederman5234f5e2005-06-25 14:58:02 -070052 page = kimage_alloc_control_pages(image, 0);
53 if (!page) {
54 result = -ENOMEM;
55 goto out;
56 }
Eric W. Biederman8bf27552005-07-29 13:25:28 -060057 level2p = (pmd_t *)page_address(page);
Eric W. Biederman5234f5e2005-06-25 14:58:02 -070058 init_level2_page(level2p, addr);
Eric W. Biederman8bf27552005-07-29 13:25:28 -060059 set_pud(level3p++, __pud(__pa(level2p) | _KERNPG_TABLE));
60 addr += PUD_SIZE;
Eric W. Biederman5234f5e2005-06-25 14:58:02 -070061 }
62 /* clear the unused entries */
Maneesh Soni72414d32005-06-25 14:58:28 -070063 while (addr < end_addr) {
Eric W. Biederman8bf27552005-07-29 13:25:28 -060064 pud_clear(level3p++);
65 addr += PUD_SIZE;
Eric W. Biederman5234f5e2005-06-25 14:58:02 -070066 }
67out:
68 return result;
69}
70
71
Eric W. Biederman8bf27552005-07-29 13:25:28 -060072static int init_level4_page(struct kimage *image, pgd_t *level4p,
Maneesh Soni72414d32005-06-25 14:58:28 -070073 unsigned long addr, unsigned long last_addr)
Eric W. Biederman5234f5e2005-06-25 14:58:02 -070074{
75 unsigned long end_addr;
76 int result;
Maneesh Soni72414d32005-06-25 14:58:28 -070077
Eric W. Biederman5234f5e2005-06-25 14:58:02 -070078 result = 0;
79 addr &= PAGE_MASK;
Eric W. Biederman8bf27552005-07-29 13:25:28 -060080 end_addr = addr + (PTRS_PER_PGD * PGDIR_SIZE);
Maneesh Soni72414d32005-06-25 14:58:28 -070081 while ((addr < last_addr) && (addr < end_addr)) {
Eric W. Biederman5234f5e2005-06-25 14:58:02 -070082 struct page *page;
Eric W. Biederman8bf27552005-07-29 13:25:28 -060083 pud_t *level3p;
Maneesh Soni72414d32005-06-25 14:58:28 -070084
Eric W. Biederman5234f5e2005-06-25 14:58:02 -070085 page = kimage_alloc_control_pages(image, 0);
86 if (!page) {
87 result = -ENOMEM;
88 goto out;
89 }
Eric W. Biederman8bf27552005-07-29 13:25:28 -060090 level3p = (pud_t *)page_address(page);
Eric W. Biederman5234f5e2005-06-25 14:58:02 -070091 result = init_level3_page(image, level3p, addr, last_addr);
92 if (result) {
93 goto out;
94 }
Eric W. Biederman8bf27552005-07-29 13:25:28 -060095 set_pgd(level4p++, __pgd(__pa(level3p) | _KERNPG_TABLE));
96 addr += PGDIR_SIZE;
Eric W. Biederman5234f5e2005-06-25 14:58:02 -070097 }
98 /* clear the unused entries */
Maneesh Soni72414d32005-06-25 14:58:28 -070099 while (addr < end_addr) {
Eric W. Biederman8bf27552005-07-29 13:25:28 -0600100 pgd_clear(level4p++);
101 addr += PGDIR_SIZE;
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700102 }
Maneesh Soni72414d32005-06-25 14:58:28 -0700103out:
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700104 return result;
105}
106
107
108static int init_pgtable(struct kimage *image, unsigned long start_pgtable)
109{
Eric W. Biederman8bf27552005-07-29 13:25:28 -0600110 pgd_t *level4p;
111 level4p = (pgd_t *)__va(start_pgtable);
Maneesh Soni72414d32005-06-25 14:58:28 -0700112 return init_level4_page(image, level4p, 0, end_pfn << PAGE_SHIFT);
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700113}
114
115static void set_idt(void *newidt, u16 limit)
116{
Eric W. Biederman36c4fd22005-07-29 13:02:09 -0600117 struct desc_ptr curidt;
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700118
119 /* x86-64 supports unaliged loads & stores */
Eric W. Biederman36c4fd22005-07-29 13:02:09 -0600120 curidt.size = limit;
121 curidt.address = (unsigned long)newidt;
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700122
123 __asm__ __volatile__ (
Eric W. Biederman36c4fd22005-07-29 13:02:09 -0600124 "lidtq %0\n"
125 : : "m" (curidt)
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700126 );
127};
128
129
130static void set_gdt(void *newgdt, u16 limit)
131{
Eric W. Biederman36c4fd22005-07-29 13:02:09 -0600132 struct desc_ptr curgdt;
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700133
134 /* x86-64 supports unaligned loads & stores */
Eric W. Biederman36c4fd22005-07-29 13:02:09 -0600135 curgdt.size = limit;
136 curgdt.address = (unsigned long)newgdt;
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700137
138 __asm__ __volatile__ (
Eric W. Biederman36c4fd22005-07-29 13:02:09 -0600139 "lgdtq %0\n"
140 : : "m" (curgdt)
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700141 );
142};
143
144static void load_segments(void)
145{
146 __asm__ __volatile__ (
Eric W. Biederman36c4fd22005-07-29 13:02:09 -0600147 "\tmovl %0,%%ds\n"
148 "\tmovl %0,%%es\n"
149 "\tmovl %0,%%ss\n"
150 "\tmovl %0,%%fs\n"
151 "\tmovl %0,%%gs\n"
Michael Matz2ec5e3a2006-03-07 21:55:48 -0800152 : : "a" (__KERNEL_DS) : "memory"
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700153 );
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700154}
155
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700156int machine_kexec_prepare(struct kimage *image)
157{
Magnus Damm4bfaaef2006-09-26 10:52:38 +0200158 unsigned long start_pgtable;
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700159 int result;
160
161 /* Calculate the offsets */
Maneesh Soni72414d32005-06-25 14:58:28 -0700162 start_pgtable = page_to_pfn(image->control_code_page) << PAGE_SHIFT;
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700163
164 /* Setup the identity mapped 64bit page table */
165 result = init_pgtable(image, start_pgtable);
Maneesh Soni72414d32005-06-25 14:58:28 -0700166 if (result)
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700167 return result;
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700168
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700169 return 0;
170}
171
172void machine_kexec_cleanup(struct kimage *image)
173{
174 return;
175}
176
177/*
178 * Do not allocate memory (or fail in any way) in machine_kexec().
179 * We are past the point of no return, committed to rebooting now.
180 */
181NORET_TYPE void machine_kexec(struct kimage *image)
182{
Magnus Damm4bfaaef2006-09-26 10:52:38 +0200183 unsigned long page_list[PAGES_NR];
184 void *control_page;
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700185
186 /* Interrupts aren't acceptable while we reboot */
187 local_irq_disable();
188
Magnus Damm4bfaaef2006-09-26 10:52:38 +0200189 control_page = page_address(image->control_code_page) + PAGE_SIZE;
190 memcpy(control_page, relocate_kernel, PAGE_SIZE);
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700191
Magnus Damm4bfaaef2006-09-26 10:52:38 +0200192 page_list[PA_CONTROL_PAGE] = __pa(control_page);
193 page_list[VA_CONTROL_PAGE] = (unsigned long)relocate_kernel;
194 page_list[PA_PGD] = __pa(kexec_pgd);
195 page_list[VA_PGD] = (unsigned long)kexec_pgd;
196 page_list[PA_PUD_0] = __pa(kexec_pud0);
197 page_list[VA_PUD_0] = (unsigned long)kexec_pud0;
198 page_list[PA_PMD_0] = __pa(kexec_pmd0);
199 page_list[VA_PMD_0] = (unsigned long)kexec_pmd0;
200 page_list[PA_PTE_0] = __pa(kexec_pte0);
201 page_list[VA_PTE_0] = (unsigned long)kexec_pte0;
202 page_list[PA_PUD_1] = __pa(kexec_pud1);
203 page_list[VA_PUD_1] = (unsigned long)kexec_pud1;
204 page_list[PA_PMD_1] = __pa(kexec_pmd1);
205 page_list[VA_PMD_1] = (unsigned long)kexec_pmd1;
206 page_list[PA_PTE_1] = __pa(kexec_pte1);
207 page_list[VA_PTE_1] = (unsigned long)kexec_pte1;
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700208
Magnus Damm4bfaaef2006-09-26 10:52:38 +0200209 page_list[PA_TABLE_PAGE] =
210 (unsigned long)__pa(page_address(image->control_code_page));
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700211
Eric W. Biederman2a8a3d52006-07-30 03:03:20 -0700212 /* The segment registers are funny things, they have both a
213 * visible and an invisible part. Whenever the visible part is
214 * set to a specific selector, the invisible part is loaded
215 * with from a table in memory. At no other time is the
216 * descriptor table in memory accessed.
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700217 *
218 * I take advantage of this here by force loading the
219 * segments, before I zap the gdt with an invalid value.
220 */
221 load_segments();
222 /* The gdt & idt are now invalid.
223 * If you want to load them you must set up your own idt & gdt.
224 */
225 set_gdt(phys_to_virt(0),0);
226 set_idt(phys_to_virt(0),0);
Magnus Damm4bfaaef2006-09-26 10:52:38 +0200227
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700228 /* now call it */
Magnus Damm4bfaaef2006-09-26 10:52:38 +0200229 relocate_kernel((unsigned long)image->head, (unsigned long)page_list,
230 image->start);
Eric W. Biederman5234f5e2005-06-25 14:58:02 -0700231}
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200232
233/* crashkernel=size@addr specifies the location to reserve for
234 * a crash kernel. By reserving this memory we guarantee
235 * that linux never set's it up as a DMA target.
236 * Useful for holding code to do something appropriate
237 * after a kernel panic.
238 */
239static int __init setup_crashkernel(char *arg)
240{
241 unsigned long size, base;
242 char *p;
243 if (!arg)
244 return -EINVAL;
245 size = memparse(arg, &p);
246 if (arg == p)
247 return -EINVAL;
248 if (*p == '@') {
249 base = memparse(p+1, &p);
250 /* FIXME: Do I want a sanity check to validate the
251 * memory range? Yes you do, but it's too early for
252 * e820 -AK */
253 crashk_res.start = base;
254 crashk_res.end = base + size - 1;
255 }
256 return 0;
257}
258early_param("crashkernel", setup_crashkernel);
259