blob: 8864230d55afd11476e96e13429c4a1ba03ce8cd [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>
Ingo Molnarf43fdad2008-05-12 21:20:43 +020014#include <linux/ftrace.h>
15
Eric W. Biederman5033cba2005-06-25 14:57:56 -070016#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>
Eric W. Biedermane7b47cc2005-07-29 13:01:18 -060023#include <asm/desc.h>
Zachary Amsden4bb0d3e2005-09-03 15:56:36 -070024#include <asm/system.h>
Eric W. Biederman5033cba2005-06-25 14:57:56 -070025
26#define PAGE_ALIGNED __attribute__ ((__aligned__(PAGE_SIZE)))
Magnus Damm35665612006-09-26 10:52:38 +020027static u32 kexec_pgd[1024] PAGE_ALIGNED;
28#ifdef CONFIG_X86_PAE
29static u32 kexec_pmd0[1024] PAGE_ALIGNED;
30static u32 kexec_pmd1[1024] PAGE_ALIGNED;
Eric W. Biederman5033cba2005-06-25 14:57:56 -070031#endif
Magnus Damm35665612006-09-26 10:52:38 +020032static u32 kexec_pte0[1024] PAGE_ALIGNED;
33static u32 kexec_pte1[1024] PAGE_ALIGNED;
Eric W. Biederman5033cba2005-06-25 14:57:56 -070034
Eric W. Biederman5033cba2005-06-25 14:57:56 -070035static void set_idt(void *newidt, __u16 limit)
36{
Glauber de Oliveira Costa6b68f012008-01-30 13:31:12 +010037 struct desc_ptr curidt;
Eric W. Biederman5033cba2005-06-25 14:57:56 -070038
39 /* ia32 supports unaliged loads & stores */
Eric W. Biedermane7b47cc2005-07-29 13:01:18 -060040 curidt.size = limit;
41 curidt.address = (unsigned long)newidt;
Eric W. Biederman5033cba2005-06-25 14:57:56 -070042
Zachary Amsdenf2ab4462005-09-03 15:56:42 -070043 load_idt(&curidt);
WANG Cong378fc6e2008-06-24 16:21:18 +010044}
Eric W. Biederman5033cba2005-06-25 14:57:56 -070045
46
47static void set_gdt(void *newgdt, __u16 limit)
48{
Glauber de Oliveira Costa6b68f012008-01-30 13:31:12 +010049 struct desc_ptr curgdt;
Eric W. Biederman5033cba2005-06-25 14:57:56 -070050
51 /* ia32 supports unaligned loads & stores */
Eric W. Biedermane7b47cc2005-07-29 13:01:18 -060052 curgdt.size = limit;
53 curgdt.address = (unsigned long)newgdt;
Eric W. Biederman5033cba2005-06-25 14:57:56 -070054
Zachary Amsdenf2ab4462005-09-03 15:56:42 -070055 load_gdt(&curgdt);
WANG Cong378fc6e2008-06-24 16:21:18 +010056}
Eric W. Biederman5033cba2005-06-25 14:57:56 -070057
58static 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"
Michael Matz2ec5e3a2006-03-07 21:55:48 -080066 "\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");
Eric W. Biederman5033cba2005-06-25 14:57:56 -070073#undef STR
74#undef __STR
75}
76
Eric W. Biederman5033cba2005-06-25 14:57:56 -070077/*
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 */
90int 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 */
99void 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 */
107NORET_TYPE void machine_kexec(struct kimage *image)
108{
Magnus Damm35665612006-09-26 10:52:38 +0200109 unsigned long page_list[PAGES_NR];
110 void *control_page;
Eric W. Biederman5033cba2005-06-25 14:57:56 -0700111
Ingo Molnarf43fdad2008-05-12 21:20:43 +0200112 tracer_disable();
113
Eric W. Biederman5033cba2005-06-25 14:57:56 -0700114 /* Interrupts aren't acceptable while we reboot */
115 local_irq_disable();
116
Magnus Damm35665612006-09-26 10:52:38 +0200117 control_page = page_address(image->control_code_page);
118 memcpy(control_page, relocate_kernel, PAGE_SIZE);
Eric W. Biederman5033cba2005-06-25 14:57:56 -0700119
Magnus Damm35665612006-09-26 10:52:38 +0200120 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;
Eric W. Biederman5033cba2005-06-25 14:57:56 -0700134
Eric W. Biederman2a8a3d52006-07-30 03:03:20 -0700135 /* 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.
Eric W. Biederman5033cba2005-06-25 14:57:56 -0700140 *
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 */
Magnus Damm35665612006-09-26 10:52:38 +0200152 relocate_kernel((unsigned long)image->head, (unsigned long)page_list,
153 image->start, cpu_has_pae);
Eric W. Biederman5033cba2005-06-25 14:57:56 -0700154}
Rusty Russell1a3f2392006-09-26 10:52:32 +0200155
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -0700156void arch_crash_save_vmcoreinfo(void)
157{
Ken'ichi Ohmichi92df5c32008-02-07 00:15:23 -0800158#ifdef CONFIG_NUMA
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -0700159 VMCOREINFO_SYMBOL(node_data);
160 VMCOREINFO_LENGTH(node_data, MAX_NUMNODES);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -0700161#endif
162#ifdef CONFIG_X86_PAE
Ken'ichi Ohmichibcbba6c2007-10-16 23:27:30 -0700163 VMCOREINFO_CONFIG(X86_PAE);
Ken'ichi Ohmichifd59d232007-10-16 23:27:27 -0700164#endif
165}
166