vserver 1.9.5.x5
[linux-2.6.git] / arch / x86_64 / kernel / head.S
1 /*
2  *  linux/arch/x86_64/kernel/head.S -- start in 32bit and switch to 64bit
3  *
4  *  Copyright (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE
5  *  Copyright (C) 2000 Pavel Machek <pavel@suse.cz>
6  *  Copyright (C) 2000 Karsten Keil <kkeil@suse.de>
7  *  Copyright (C) 2001,2002 Andi Kleen <ak@suse.de>
8  *
9  *  $Id: head.S,v 1.49 2002/03/19 17:39:25 ak Exp $
10  */
11
12
13 #include <linux/linkage.h>
14 #include <linux/threads.h>
15 #include <asm/desc.h>
16 #include <asm/segment.h>
17 #include <asm/page.h>
18 #include <asm/msr.h>
19 #include <asm/cache.h>
20         
21 /* we are not able to switch in one step to the final KERNEL ADRESS SPACE
22  * because we need identity-mapped pages on setup so define __START_KERNEL to
23  * 0x100000 for this stage
24  * 
25  */
26
27         .text
28         .code32
29 /* %bx:  1 if coming from smp trampoline on secondary cpu */ 
30 startup_32:
31         
32         /*
33          * At this point the CPU runs in 32bit protected mode (CS.D = 1) with
34          * paging disabled and the point of this file is to switch to 64bit
35          * long mode with a kernel mapping for kerneland to jump into the
36          * kernel virtual addresses.
37          * There is no stack until we set one up.
38          */
39
40         movl %ebx,%ebp  /* Save trampoline flag */
41         
42         movl $__KERNEL_DS,%eax
43         movl %eax,%ds
44         
45         /* If the CPU doesn't support CPUID this will double fault.
46          * Unfortunately it is hard to check for CPUID without a stack. 
47          */
48         
49         /* Check if extended functions are implemented */               
50         movl    $0x80000000, %eax
51         cpuid
52         cmpl    $0x80000000, %eax
53         jbe     no_long_mode
54         /* Check if long mode is implemented */
55         mov     $0x80000001, %eax
56         cpuid
57         btl     $29, %edx
58         jnc     no_long_mode
59
60         movl    %edx,%edi
61         
62         /*
63          * Prepare for entering 64bits mode
64          */
65
66         /* Enable PAE mode and PGE */
67         xorl    %eax, %eax
68         btsl    $5, %eax
69         btsl    $7, %eax
70         movl    %eax, %cr4
71
72         /* Setup early boot stage 4 level pagetables */
73         movl    $(init_level4_pgt - __START_KERNEL_map), %eax
74         movl    %eax, %cr3
75
76         /* Setup EFER (Extended Feature Enable Register) */
77         movl    $MSR_EFER, %ecx
78         rdmsr
79
80         /* Enable Long Mode */
81         btsl    $_EFER_LME, %eax
82         /* Enable System Call */
83         btsl    $_EFER_SCE, %eax
84
85         /* No Execute supported? */     
86         btl     $20,%edi
87         jnc     1f
88         btsl    $_EFER_NX, %eax
89 1:      
90                                 
91         /* Make changes effective */
92         wrmsr
93
94         xorl    %eax, %eax
95         btsl    $31, %eax                       /* Enable paging and in turn activate Long Mode */
96         btsl    $0, %eax                        /* Enable protected mode */
97         btsl    $1, %eax                        /* Enable MP */
98         btsl    $4, %eax                        /* Enable ET */
99         btsl    $5, %eax                        /* Enable NE */
100         btsl    $16, %eax                       /* Enable WP */
101         btsl    $18, %eax                       /* Enable AM */
102         /* Make changes effective */
103         movl    %eax, %cr0
104         jmp     reach_compatibility_mode
105 reach_compatibility_mode:
106         
107         /*
108          * At this point we're in long mode but in 32bit compatibility mode
109          * with EFER.LME = 1, CS.L = 0, CS.D = 1 (and in turn
110          * EFER.LMA = 1). Now we want to jump in 64bit mode, to do that we load
111          * the new gdt/idt that has __KERNEL_CS with CS.L = 1.
112          */
113
114         testw %bp,%bp   /* secondary CPU? */ 
115         jnz   second    
116         
117         /* Load new GDT with the 64bit segment using 32bit descriptor */
118         movl    $(pGDT32 - __START_KERNEL_map), %eax
119         lgdt    (%eax)
120
121 second: 
122         movl    $(ljumpvector - __START_KERNEL_map), %eax
123         /* Finally jump in 64bit mode */
124         ljmp    *(%eax)
125
126         .code64
127         .org 0x100      
128 reach_long64:
129         movq init_rsp(%rip),%rsp
130
131         /* zero EFLAGS after setting rsp */
132         pushq $0
133         popfq
134
135         /*
136          * We must switch to a new descriptor in kernel space for the GDT
137          * because soon the kernel won't have access anymore to the userspace
138          * addresses where we're currently running on. We have to do that here
139          * because in 32bit we couldn't load a 64bit linear address.
140          */
141         lgdt    cpu_gdt_descr
142
143         /* 
144          * Setup up a dummy PDA. this is just for some early bootup code
145          * that does in_interrupt() 
146          */ 
147         movl    $MSR_GS_BASE,%ecx
148         movq    $empty_zero_page,%rax
149         movq    %rax,%rdx
150         shrq    $32,%rdx
151         wrmsr   
152
153         /* set up data segments. actually 0 would do too */
154         movl $__KERNEL_DS,%eax
155         movl %eax,%ds   
156         movl %eax,%ss
157         movl %eax,%es
158                         
159         /* esi is pointer to real mode structure with interesting info.
160            pass it to C */
161         movl    %esi, %edi
162         
163         /* Finally jump to run C code and to be on real kernel address
164          * Since we are running on identity-mapped space we have to jump
165          * to the full 64bit address , this is only possible as indirect
166          * jump
167          */
168         movq    initial_code(%rip),%rax
169         jmp     *%rax
170
171         /* SMP bootup changes these two */      
172         .globl  initial_code
173 initial_code:
174         .quad   x86_64_start_kernel
175         .globl init_rsp
176 init_rsp:
177         .quad  init_thread_union+THREAD_SIZE-8
178
179 ENTRY(early_idt_handler)
180         xorl %eax,%eax
181         movq 8(%rsp),%rsi       # get rip
182         movq (%rsp),%rdx
183         movq %cr2,%rcx
184         leaq early_idt_msg(%rip),%rdi
185         call early_printk
186 1:      hlt
187         jmp 1b
188
189 early_idt_msg:
190         .asciz "PANIC: early exception rip %lx error %lx cr2 %lx\n"
191
192 .code32
193 ENTRY(no_long_mode)
194         /* This isn't an x86-64 CPU so hang */
195 1:
196         jmp     1b
197
198 .org 0xf00
199         .globl pGDT32
200 pGDT32:
201         .word   gdt32_end-gdt_table32
202         .long   gdt_table32-__START_KERNEL_map
203
204 .org 0xf10      
205 ljumpvector:
206         .long   reach_long64-__START_KERNEL_map
207         .word   __KERNEL_CS
208
209 ENTRY(stext)
210 ENTRY(_stext)
211
212         /*
213          * This default setting generates an ident mapping at address 0x100000
214          * and a mapping for the kernel that precisely maps virtual address
215          * 0xffffffff80000000 to physical address 0x000000. (always using
216          * 2Mbyte large pages provided by PAE mode)
217          */
218 .org 0x1000
219 ENTRY(init_level4_pgt)
220         .quad   0x0000000000102007              /* -> level3_ident_pgt */
221         .fill   255,8,0
222         .quad   0x000000000010a007
223         .fill   254,8,0
224         /* (2^48-(2*1024*1024*1024))/(2^39) = 511 */
225         .quad   0x0000000000103007              /* -> level3_kernel_pgt */
226
227 .org 0x2000
228 ENTRY(level3_ident_pgt)
229         .quad   0x0000000000104007
230         .fill   511,8,0
231
232 .org 0x3000
233 ENTRY(level3_kernel_pgt)
234         .fill   510,8,0
235         /* (2^48-(2*1024*1024*1024)-((2^39)*511))/(2^30) = 510 */
236         .quad   0x0000000000105007              /* -> level2_kernel_pgt */
237         .fill   1,8,0
238
239 .org 0x4000
240 ENTRY(level2_ident_pgt)
241         /* 40MB for bootup.     */
242         .quad   0x0000000000000283
243         .quad   0x0000000000200183
244         .quad   0x0000000000400183
245         .quad   0x0000000000600183
246         .quad   0x0000000000800183
247         .quad   0x0000000000A00183
248         .quad   0x0000000000C00183
249         .quad   0x0000000000E00183
250         .quad   0x0000000001000183
251         .quad   0x0000000001200183
252         .quad   0x0000000001400183
253         .quad   0x0000000001600183
254         .quad   0x0000000001800183
255         .quad   0x0000000001A00183
256         .quad   0x0000000001C00183
257         .quad   0x0000000001E00183
258         .quad   0x0000000002000183
259         .quad   0x0000000002200183
260         .quad   0x0000000002400183
261         .quad   0x0000000002600183
262         /* Temporary mappings for the super early allocator in arch/x86_64/mm/init.c */
263         .globl temp_boot_pmds
264 temp_boot_pmds:
265         .fill   492,8,0
266         
267 .org 0x5000
268 ENTRY(level2_kernel_pgt)
269         /* 40MB kernel mapping. The kernel code cannot be bigger than that.
270            When you change this change KERNEL_TEXT_SIZE in page.h too. */
271         /* (2^48-(2*1024*1024*1024)-((2^39)*511)-((2^30)*510)) = 0 */
272         .quad   0x0000000000000183
273         .quad   0x0000000000200183
274         .quad   0x0000000000400183
275         .quad   0x0000000000600183
276         .quad   0x0000000000800183
277         .quad   0x0000000000A00183
278         .quad   0x0000000000C00183
279         .quad   0x0000000000E00183
280         .quad   0x0000000001000183
281         .quad   0x0000000001200183
282         .quad   0x0000000001400183
283         .quad   0x0000000001600183
284         .quad   0x0000000001800183
285         .quad   0x0000000001A00183
286         .quad   0x0000000001C00183
287         .quad   0x0000000001E00183
288         .quad   0x0000000002000183
289         .quad   0x0000000002200183
290         .quad   0x0000000002400183
291         .quad   0x0000000002600183
292         /* Module mapping starts here */
293         .fill   492,8,0
294
295 .org 0x6000
296 ENTRY(empty_zero_page)
297
298 .org 0x7000
299 ENTRY(empty_bad_page)
300
301 .org 0x8000
302 ENTRY(empty_bad_pte_table)
303
304 .org 0x9000
305 ENTRY(empty_bad_pmd_table)
306
307 .org 0xa000
308 ENTRY(level3_physmem_pgt)
309         .quad   0x0000000000105007              /* -> level2_kernel_pgt (so that __va works even before pagetable_init) */
310
311         .org 0xb000
312 #ifdef CONFIG_ACPI_SLEEP
313 ENTRY(wakeup_level4_pgt)
314         .quad   0x0000000000102007              /* -> level3_ident_pgt */
315         .fill   255,8,0
316         .quad   0x000000000010a007
317         .fill   254,8,0
318         /* (2^48-(2*1024*1024*1024))/(2^39) = 511 */
319         .quad   0x0000000000103007              /* -> level3_kernel_pgt */
320 #endif
321
322         .data
323
324         .align 16
325         .globl cpu_gdt_descr
326 cpu_gdt_descr:
327         .word   gdt_end-cpu_gdt_table
328 gdt:
329         .quad   cpu_gdt_table
330 #ifdef CONFIG_SMP
331         .rept   NR_CPUS-1
332         .word   0
333         .quad   0
334         .endr
335 #endif
336
337 ENTRY(gdt_table32)
338         .quad   0x0000000000000000      /* This one is magic */
339         .quad   0x0000000000000000      /* unused */
340         .quad   0x00af9a000000ffff      /* __KERNEL_CS */
341 gdt32_end:      
342         
343 /* We need valid kernel segments for data and code in long mode too
344  * IRET will check the segment types  kkeil 2000/10/28
345  * Also sysret mandates a special GDT layout 
346  */
347                                 
348 .align L1_CACHE_BYTES
349
350 /* The TLS descriptors are currently at a different place compared to i386.
351    Hopefully nobody expects them at a fixed place (Wine?) */
352         
353 ENTRY(cpu_gdt_table)
354         .quad   0x0000000000000000      /* NULL descriptor */
355         .quad   0x008f9a000000ffff      /* __KERNEL_COMPAT32_CS */      
356         .quad   0x00af9a000000ffff      /* __KERNEL_CS */
357         .quad   0x00cf92000000ffff      /* __KERNEL_DS */
358         .quad   0x00cffa000000ffff      /* __USER32_CS */
359         .quad   0x00cff2000000ffff      /* __USER_DS, __USER32_DS  */           
360         .quad   0x00affa000000ffff      /* __USER_CS */
361         .quad   0x00cf9a000000ffff      /* __KERNEL32_CS */
362         .quad   0,0                     /* TSS */
363         .quad   0                       /* LDT */
364         .quad   0,0,0                   /* three TLS descriptors */ 
365         .quad   0                       /* unused now */
366         .quad   0x00009a000000ffff      /* __KERNEL16_CS - 16bit PM for S3 wakeup. */
367                                         /* base must be patched for real base address. */
368 gdt_end:        
369         /* asm/segment.h:GDT_ENTRIES must match this */ 
370         /* This should be a multiple of the cache line size */
371         /* GDTs of other CPUs: */       
372         .fill (GDT_SIZE * NR_CPUS) - (gdt_end - cpu_gdt_table)
373
374         .align  L1_CACHE_BYTES
375 ENTRY(idt_table)        
376         .rept   256
377         .quad   0
378         .quad   0
379         .endr
380