This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / arch / x86_64 / kernel / machine_kexec.c
1 /*
2  * machine_kexec.c - handle transition of Linux booting another kernel
3  * Copyright (C) 2002-2004 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/string.h>
13 #include <linux/reboot.h>
14 #include <asm/pda.h>
15 #include <asm/pgtable.h>
16 #include <asm/pgalloc.h>
17 #include <asm/tlbflush.h>
18 #include <asm/mmu_context.h>
19 #include <asm/io.h>
20 #include <asm/apic.h>
21 #include <asm/cpufeature.h>
22 #include <asm/hw_irq.h>
23
24 #define LEVEL0_SIZE (1UL << 12UL)
25 #define LEVEL1_SIZE (1UL << 21UL)
26 #define LEVEL2_SIZE (1UL << 30UL)
27 #define LEVEL3_SIZE (1UL << 39UL)
28 #define LEVEL4_SIZE (1UL << 48UL)
29
30 #define L0_ATTR (_PAGE_PRESENT | _PAGE_RW | _PAGE_ACCESSED | _PAGE_DIRTY)
31 #define L1_ATTR (_PAGE_PRESENT | _PAGE_RW | _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE)
32 #define L2_ATTR (_PAGE_PRESENT | _PAGE_RW | _PAGE_ACCESSED | _PAGE_DIRTY)
33 #define L3_ATTR (_PAGE_PRESENT | _PAGE_RW | _PAGE_ACCESSED | _PAGE_DIRTY)
34
35 static void init_level2_page(
36         uint64_t *level2p, unsigned long addr)
37 {
38         unsigned long end_addr;
39         addr &= PAGE_MASK;
40         end_addr = addr + LEVEL2_SIZE;
41         while(addr < end_addr) {
42                 *(level2p++) = addr | L1_ATTR;
43                 addr += LEVEL1_SIZE;
44         }
45 }
46
47 static int init_level3_page(struct kimage *image,
48         uint64_t *level3p, unsigned long addr, unsigned long last_addr)
49 {
50         unsigned long end_addr;
51         int result;
52         result = 0;
53         addr &= PAGE_MASK;
54         end_addr = addr + LEVEL3_SIZE;
55         while((addr < last_addr) && (addr < end_addr)) {
56                 struct page *page;
57                 uint64_t *level2p;
58                 page = kimage_alloc_control_pages(image, 0);
59                 if (!page) {
60                         result = -ENOMEM;
61                         goto out;
62                 }
63                 level2p = (uint64_t *)page_address(page);
64                 init_level2_page(level2p, addr);
65                 *(level3p++) = __pa(level2p) | L2_ATTR;
66                 addr += LEVEL2_SIZE;
67         }
68         /* clear the unused entries */
69         while(addr < end_addr) {
70                 *(level3p++) = 0;
71                 addr += LEVEL2_SIZE;
72         }
73 out:
74         return result;
75 }
76
77
78 static int init_level4_page(struct kimage *image,
79         uint64_t *level4p, unsigned long addr, unsigned long last_addr)
80 {
81         unsigned long end_addr;
82         int result;
83         result = 0;
84         addr &= PAGE_MASK;
85         end_addr = addr + LEVEL4_SIZE;
86         while((addr < last_addr) && (addr < end_addr)) {
87                 struct page *page;
88                 uint64_t *level3p;
89                 page = kimage_alloc_control_pages(image, 0);
90                 if (!page) {
91                         result = -ENOMEM;
92                         goto out;
93                 }
94                 level3p = (uint64_t *)page_address(page);
95                 result = init_level3_page(image, level3p, addr, last_addr);
96                 if (result) {
97                         goto out;
98                 }
99                 *(level4p++) = __pa(level3p) | L3_ATTR;
100                 addr += LEVEL3_SIZE;
101         }
102         /* clear the unused entries */
103         while(addr < end_addr) {
104                 *(level4p++) = 0;
105                 addr += LEVEL3_SIZE;
106         }
107  out:
108         return result;
109 }
110
111
112 static int init_pgtable(struct kimage *image, unsigned long start_pgtable)
113 {
114         uint64_t *level4p;
115         level4p = (uint64_t *)__va(start_pgtable);
116         return init_level4_page(image, level4p, 0, end_pfn << PAGE_SHIFT);
117 }
118
119 static void set_idt(void *newidt, __u16 limit)
120 {
121         unsigned char curidt[10];
122
123         /* x86-64 supports unaliged loads & stores */
124         (*(__u16 *)(curidt)) = limit;
125         (*(__u64 *)(curidt +2)) = (unsigned long)(newidt);
126
127         __asm__ __volatile__ (
128                 "lidt %0\n"
129                 : "=m" (curidt)
130                 );
131 };
132
133
134 static void set_gdt(void *newgdt, __u16 limit)
135 {
136         unsigned char curgdt[10];
137
138         /* x86-64 supports unaligned loads & stores */
139         (*(__u16 *)(curgdt)) = limit;
140         (*(__u64 *)(curgdt +2)) = (unsigned long)(newgdt);
141
142         __asm__ __volatile__ (
143                 "lgdt %0\n"
144                 : "=m" (curgdt)
145                 );
146 };
147
148 static void load_segments(void)
149 {
150         __asm__ __volatile__ (
151                 "\tmovl $"STR(__KERNEL_DS)",%eax\n"
152                 "\tmovl %eax,%ds\n"
153                 "\tmovl %eax,%es\n"
154                 "\tmovl %eax,%ss\n"
155                 "\tmovl %eax,%fs\n"
156                 "\tmovl %eax,%gs\n"
157                 );
158 #undef STR
159 #undef __STR
160 }
161
162 typedef void (*relocate_new_kernel_t)(
163         unsigned long indirection_page, unsigned long control_code_buffer,
164         unsigned long start_address, unsigned long pgtable);
165
166 const extern unsigned char relocate_new_kernel[];
167 extern void relocate_new_kernel_end(void);
168 const extern unsigned long relocate_new_kernel_size;
169
170 int machine_kexec_prepare(struct kimage *image)
171 {
172         unsigned long start_pgtable, control_code_buffer;
173         int result;
174
175         /* Calculate the offsets */
176         start_pgtable       = page_to_pfn(image->control_code_page) << PAGE_SHIFT;
177         control_code_buffer = start_pgtable + 4096UL;
178
179         /* Setup the identity mapped 64bit page table */
180         result = init_pgtable(image, start_pgtable);
181         if (result) {
182                 return result;
183         }
184
185         /* Place the code in the reboot code buffer */
186         memcpy(__va(control_code_buffer), relocate_new_kernel, relocate_new_kernel_size);
187
188         return 0;
189 }
190
191 void machine_kexec_cleanup(struct kimage *image)
192 {
193         return;
194 }
195
196 /*
197  * Do not allocate memory (or fail in any way) in machine_kexec().
198  * We are past the point of no return, committed to rebooting now.
199  */
200 void machine_kexec(struct kimage *image)
201 {
202         unsigned long indirection_page;
203         unsigned long control_code_buffer;
204         unsigned long start_pgtable;
205         relocate_new_kernel_t rnk;
206
207         /* Interrupts aren't acceptable while we reboot */
208         local_irq_disable();
209
210         /* Calculate the offsets */
211         indirection_page    = image->head & PAGE_MASK;
212         start_pgtable       = page_to_pfn(image->control_code_page) << PAGE_SHIFT;
213         control_code_buffer = start_pgtable + 4096UL;
214
215         /* Set the low half of the page table to my identity mapped
216          * page table for kexec.  Leave the high half pointing at the
217          * kernel pages.   Don't bother to flush the global pages
218          * as that will happen when I fully switch to my identity mapped
219          * page table anyway.
220          */
221         memcpy((void *)read_pda(level4_pgt), __va(start_pgtable), PAGE_SIZE/2);
222         __flush_tlb();
223
224
225         /* The segment registers are funny things, they are
226          * automatically loaded from a table, in memory wherever you
227          * set them to a specific selector, but this table is never
228          * accessed again unless you set the segment to a different selector.
229          *
230          * The more common model are caches where the behide
231          * the scenes work is done, but is also dropped at arbitrary
232          * times.
233          *
234          * I take advantage of this here by force loading the
235          * segments, before I zap the gdt with an invalid value.
236          */
237         load_segments();
238         /* The gdt & idt are now invalid.
239          * If you want to load them you must set up your own idt & gdt.
240          */
241         set_gdt(phys_to_virt(0),0);
242         set_idt(phys_to_virt(0),0);
243         /* now call it */
244         rnk = (relocate_new_kernel_t) control_code_buffer;
245         (*rnk)(indirection_page, control_code_buffer, image->start, start_pgtable);
246 }