blob: 36aa98a6d15cd3d1759b8d3d61d22028c7f8fa72 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Vivek Goyal1ab60e02007-05-02 19:27:07 +02008 * Copyright (C) 2005 Eric Biederman <ebiederm@xmission.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
11
12#include <linux/linkage.h>
13#include <linux/threads.h>
Siddha, Suresh Bf6c2e332005-11-05 17:25:53 +010014#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <asm/desc.h>
16#include <asm/segment.h>
Vivek Goyal67dcbb62007-05-02 19:27:06 +020017#include <asm/pgtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <asm/page.h>
19#include <asm/msr.h>
20#include <asm/cache.h>
Vivek Goyal1ab60e02007-05-02 19:27:07 +020021
Linus Torvalds1da177e2005-04-16 15:20:36 -070022/* we are not able to switch in one step to the final KERNEL ADRESS SPACE
Vivek Goyal1ab60e02007-05-02 19:27:07 +020023 * because we need identity-mapped pages.
24 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 */
26
27 .text
Arjan van de Veneaeae0c2006-03-25 16:30:28 +010028 .section .bootstrap.text
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 .code64
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 .globl startup_64
31startup_64:
Vivek Goyal1ab60e02007-05-02 19:27:07 +020032
33 /*
34 * At this point the CPU runs in 64bit mode CS.L = 1 CS.D = 1,
35 * and someone has loaded an identity mapped page table
36 * for us. These identity mapped page tables map all of the
37 * kernel pages and possibly all of memory.
38 *
39 * %esi holds a physical pointer to real_mode_data.
40 *
41 * We come here either directly from a 64bit bootloader, or from
42 * arch/x86_64/boot/compressed/head.S.
43 *
44 * We only come here initially at boot nothing else comes here.
45 *
46 * Since we may be loaded at an address different from what we were
47 * compiled to run at we first fixup the physical addresses in our page
48 * tables and then reload them.
49 */
50
51 /* Compute the delta between the address I am compiled to run at and the
52 * address I am actually running at.
53 */
54 leaq _text(%rip), %rbp
55 subq $_text - __START_KERNEL_map, %rbp
56
57 /* Is the address not 2M aligned? */
58 movq %rbp, %rax
59 andl $~LARGE_PAGE_MASK, %eax
60 testl %eax, %eax
61 jnz bad_address
62
63 /* Is the address too large? */
64 leaq _text(%rip), %rdx
65 movq $PGDIR_SIZE, %rax
66 cmpq %rax, %rdx
67 jae bad_address
68
69 /* Fixup the physical addresses in the page table
70 */
71 addq %rbp, init_level4_pgt + 0(%rip)
72 addq %rbp, init_level4_pgt + (258*8)(%rip)
73 addq %rbp, init_level4_pgt + (511*8)(%rip)
74
75 addq %rbp, level3_ident_pgt + 0(%rip)
76 addq %rbp, level3_kernel_pgt + (510*8)(%rip)
77
78 /* Add an Identity mapping if I am above 1G */
79 leaq _text(%rip), %rdi
80 andq $LARGE_PAGE_MASK, %rdi
81
82 movq %rdi, %rax
83 shrq $PUD_SHIFT, %rax
84 andq $(PTRS_PER_PUD - 1), %rax
85 jz ident_complete
86
87 leaq (level2_spare_pgt - __START_KERNEL_map + _KERNPG_TABLE)(%rbp), %rdx
88 leaq level3_ident_pgt(%rip), %rbx
89 movq %rdx, 0(%rbx, %rax, 8)
90
91 movq %rdi, %rax
92 shrq $PMD_SHIFT, %rax
93 andq $(PTRS_PER_PMD - 1), %rax
94 leaq __PAGE_KERNEL_LARGE_EXEC(%rdi), %rdx
95 leaq level2_spare_pgt(%rip), %rbx
96 movq %rdx, 0(%rbx, %rax, 8)
97ident_complete:
98
99 /* Fixup the kernel text+data virtual addresses
100 */
101 leaq level2_kernel_pgt(%rip), %rdi
102 leaq 4096(%rdi), %r8
103 /* See if it is a valid page table entry */
1041: testq $1, 0(%rdi)
105 jz 2f
106 addq %rbp, 0(%rdi)
107 /* Go to the next page */
1082: addq $8, %rdi
109 cmp %r8, %rdi
110 jne 1b
111
112 /* Fixup phys_base */
113 addq %rbp, phys_base(%rip)
114
115#ifdef CONFIG_SMP
116 addq %rbp, trampoline_level4_pgt + 0(%rip)
117 addq %rbp, trampoline_level4_pgt + (511*8)(%rip)
118#endif
119#ifdef CONFIG_ACPI_SLEEP
120 addq %rbp, wakeup_level4_pgt + 0(%rip)
121 addq %rbp, wakeup_level4_pgt + (511*8)(%rip)
122#endif
123
124 /* Due to ENTRY(), sometimes the empty space gets filled with
125 * zeros. Better take a jmp than relying on empty space being
126 * filled with 0x90 (nop)
127 */
128 jmp secondary_startup_64
Vivek Goyal90b1c202007-05-02 19:27:07 +0200129ENTRY(secondary_startup_64)
Vivek Goyal1ab60e02007-05-02 19:27:07 +0200130 /*
131 * At this point the CPU runs in 64bit mode CS.L = 1 CS.D = 1,
132 * and someone has loaded a mapped page table.
133 *
134 * %esi holds a physical pointer to real_mode_data.
135 *
136 * We come here either from startup_64 (using physical addresses)
137 * or from trampoline.S (using virtual addresses).
138 *
139 * Using virtual addresses from trampoline.S removes the need
140 * to have any identity mapped pages in the kernel page table
141 * after the boot processor executes this code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 */
143
144 /* Enable PAE mode and PGE */
145 xorq %rax, %rax
146 btsq $5, %rax
147 btsq $7, %rax
148 movq %rax, %cr4
149
150 /* Setup early boot stage 4 level pagetables. */
Vivek Goyalcfd243d2007-05-02 19:27:07 +0200151 movq $(init_level4_pgt - __START_KERNEL_map), %rax
Vivek Goyal1ab60e02007-05-02 19:27:07 +0200152 addq phys_base(%rip), %rax
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 movq %rax, %cr3
154
Vivek Goyal1ab60e02007-05-02 19:27:07 +0200155 /* Ensure I am executing from virtual addresses */
156 movq $1f, %rax
157 jmp *%rax
1581:
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 /* Check if nx is implemented */
161 movl $0x80000001, %eax
162 cpuid
163 movl %edx,%edi
164
165 /* Setup EFER (Extended Feature Enable Register) */
166 movl $MSR_EFER, %ecx
167 rdmsr
Vivek Goyal1ab60e02007-05-02 19:27:07 +0200168 btsl $_EFER_SCE, %eax /* Enable System Call */
169 btl $20,%edi /* No Execute supported? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 jnc 1f
171 btsl $_EFER_NX, %eax
Vivek Goyal1ab60e02007-05-02 19:27:07 +02001721: wrmsr /* Make changes effective */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 /* Setup cr0 */
Andi Kleen3829ee62005-07-28 21:15:48 -0700175#define CR0_PM 1 /* protected mode */
176#define CR0_MP (1<<1)
177#define CR0_ET (1<<4)
178#define CR0_NE (1<<5)
179#define CR0_WP (1<<16)
180#define CR0_AM (1<<18)
181#define CR0_PAGING (1<<31)
182 movl $CR0_PM|CR0_MP|CR0_ET|CR0_NE|CR0_WP|CR0_AM|CR0_PAGING,%eax
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 /* Make changes effective */
184 movq %rax, %cr0
185
186 /* Setup a boot time stack */
187 movq init_rsp(%rip),%rsp
188
189 /* zero EFLAGS after setting rsp */
190 pushq $0
191 popfq
192
193 /*
194 * We must switch to a new descriptor in kernel space for the GDT
195 * because soon the kernel won't have access anymore to the userspace
196 * addresses where we're currently running on. We have to do that here
197 * because in 32bit we couldn't load a 64bit linear address.
198 */
Vivek Goyal1ab60e02007-05-02 19:27:07 +0200199 lgdt cpu_gdt_descr(%rip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Zachary Amsdenffb60172007-02-13 13:26:24 +0100201 /* set up data segments. actually 0 would do too */
202 movl $__KERNEL_DS,%eax
203 movl %eax,%ds
204 movl %eax,%ss
205 movl %eax,%es
206
207 /*
208 * We don't really need to load %fs or %gs, but load them anyway
209 * to kill any stale realmode selectors. This allows execution
210 * under VT hardware.
211 */
212 movl %eax,%fs
213 movl %eax,%gs
214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 /*
216 * Setup up a dummy PDA. this is just for some early bootup code
217 * that does in_interrupt()
218 */
219 movl $MSR_GS_BASE,%ecx
220 movq $empty_zero_page,%rax
221 movq %rax,%rdx
222 shrq $32,%rdx
223 wrmsr
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 /* esi is pointer to real mode structure with interesting info.
226 pass it to C */
227 movl %esi, %edi
228
229 /* Finally jump to run C code and to be on real kernel address
230 * Since we are running on identity-mapped space we have to jump
Eric W. Biederman26374c72006-09-26 10:52:38 +0200231 * to the full 64bit address, this is only possible as indirect
232 * jump. In addition we need to ensure %cs is set so we make this
233 * a far return.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 */
235 movq initial_code(%rip),%rax
Eric W. Biederman26374c72006-09-26 10:52:38 +0200236 pushq $0 # fake return address to stop unwinder
237 pushq $__KERNEL_CS # set correct cs
238 pushq %rax # target address in negative space
239 lretq
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Jan Beuliche57113b2006-03-25 16:30:01 +0100241 /* SMP bootup changes these two */
242 .align 8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 .globl initial_code
244initial_code:
245 .quad x86_64_start_kernel
246 .globl init_rsp
247init_rsp:
248 .quad init_thread_union+THREAD_SIZE-8
249
Vivek Goyal1ab60e02007-05-02 19:27:07 +0200250bad_address:
251 jmp bad_address
252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253ENTRY(early_idt_handler)
Andi Kleenb9575912005-04-16 15:25:00 -0700254 cmpl $2,early_recursion_flag(%rip)
255 jz 1f
256 incl early_recursion_flag(%rip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 xorl %eax,%eax
258 movq 8(%rsp),%rsi # get rip
259 movq (%rsp),%rdx
260 movq %cr2,%rcx
261 leaq early_idt_msg(%rip),%rdi
262 call early_printk
Andi Kleenb9575912005-04-16 15:25:00 -0700263 cmpl $2,early_recursion_flag(%rip)
264 jz 1f
265 call dump_stack
Andi Kleen6574ffd2006-02-16 23:42:10 +0100266#ifdef CONFIG_KALLSYMS
267 leaq early_idt_ripmsg(%rip),%rdi
268 movq 8(%rsp),%rsi # get rip again
269 call __print_symbol
270#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711: hlt
272 jmp 1b
Andi Kleenb9575912005-04-16 15:25:00 -0700273early_recursion_flag:
274 .long 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276early_idt_msg:
277 .asciz "PANIC: early exception rip %lx error %lx cr2 %lx\n"
Andi Kleen6574ffd2006-02-16 23:42:10 +0100278early_idt_ripmsg:
279 .asciz "RIP %s\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Vivek Goyal1ab60e02007-05-02 19:27:07 +0200281.balign PAGE_SIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282ENTRY(stext)
283ENTRY(_stext)
284
Jan Beulichf0cf5d12006-01-17 07:03:32 +0100285#define NEXT_PAGE(name) \
Vivek Goyal67dcbb62007-05-02 19:27:06 +0200286 .balign PAGE_SIZE; \
Jan Beulichf0cf5d12006-01-17 07:03:32 +0100287ENTRY(name)
288
Vivek Goyal67dcbb62007-05-02 19:27:06 +0200289/* Automate the creation of 1 to 1 mapping pmd entries */
290#define PMDS(START, PERM, COUNT) \
291 i = 0 ; \
292 .rept (COUNT) ; \
293 .quad (START) + (i << 21) + (PERM) ; \
294 i = i + 1 ; \
295 .endr
296
Vivek Goyalcfd243d2007-05-02 19:27:07 +0200297 /*
298 * This default setting generates an ident mapping at address 0x100000
299 * and a mapping for the kernel that precisely maps virtual address
300 * 0xffffffff80000000 to physical address 0x000000. (always using
301 * 2Mbyte large pages provided by PAE mode)
302 */
Jan Beulichf0cf5d12006-01-17 07:03:32 +0100303NEXT_PAGE(init_level4_pgt)
Vivek Goyalcfd243d2007-05-02 19:27:07 +0200304 .quad level3_ident_pgt - __START_KERNEL_map + _KERNPG_TABLE
305 .fill 257,8,0
306 .quad level3_ident_pgt - __START_KERNEL_map + _KERNPG_TABLE
307 .fill 252,8,0
308 /* (2^48-(2*1024*1024*1024))/(2^39) = 511 */
309 .quad level3_kernel_pgt - __START_KERNEL_map + _PAGE_TABLE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Jan Beulichf0cf5d12006-01-17 07:03:32 +0100311NEXT_PAGE(level3_ident_pgt)
Vivek Goyal67dcbb62007-05-02 19:27:06 +0200312 .quad level2_ident_pgt - __START_KERNEL_map + _KERNPG_TABLE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 .fill 511,8,0
314
Jan Beulichf0cf5d12006-01-17 07:03:32 +0100315NEXT_PAGE(level3_kernel_pgt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 .fill 510,8,0
317 /* (2^48-(2*1024*1024*1024)-((2^39)*511))/(2^30) = 510 */
Vivek Goyal67dcbb62007-05-02 19:27:06 +0200318 .quad level2_kernel_pgt - __START_KERNEL_map + _KERNPG_TABLE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 .fill 1,8,0
320
Jan Beulichf0cf5d12006-01-17 07:03:32 +0100321NEXT_PAGE(level2_ident_pgt)
Vivek Goyal67dcbb62007-05-02 19:27:06 +0200322 /* Since I easily can, map the first 1G.
323 * Don't set NX because code runs from these pages.
324 */
325 PMDS(0x0000000000000000, __PAGE_KERNEL_LARGE_EXEC, PTRS_PER_PMD)
Vivek Goyal1ab60e02007-05-02 19:27:07 +0200326
Jan Beulichf0cf5d12006-01-17 07:03:32 +0100327NEXT_PAGE(level2_kernel_pgt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 /* 40MB kernel mapping. The kernel code cannot be bigger than that.
329 When you change this change KERNEL_TEXT_SIZE in page.h too. */
330 /* (2^48-(2*1024*1024*1024)-((2^39)*511)-((2^30)*510)) = 0 */
Vivek Goyal67dcbb62007-05-02 19:27:06 +0200331 PMDS(0x0000000000000000, __PAGE_KERNEL_LARGE_EXEC|_PAGE_GLOBAL,
332 KERNEL_TEXT_SIZE/PMD_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 /* Module mapping starts here */
Vivek Goyal67dcbb62007-05-02 19:27:06 +0200334 .fill (PTRS_PER_PMD - (KERNEL_TEXT_SIZE/PMD_SIZE)),8,0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Vivek Goyal1ab60e02007-05-02 19:27:07 +0200336NEXT_PAGE(level2_spare_pgt)
337 .fill 512,8,0
338
Vivek Goyal67dcbb62007-05-02 19:27:06 +0200339#undef PMDS
Jan Beulichf0cf5d12006-01-17 07:03:32 +0100340#undef NEXT_PAGE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Jan Beulichf0cf5d12006-01-17 07:03:32 +0100342 .data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 .align 16
344 .globl cpu_gdt_descr
345cpu_gdt_descr:
Jan Beuliche57113b2006-03-25 16:30:01 +0100346 .word gdt_end-cpu_gdt_table-1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347gdt:
348 .quad cpu_gdt_table
349#ifdef CONFIG_SMP
350 .rept NR_CPUS-1
351 .word 0
352 .quad 0
353 .endr
354#endif
355
Vivek Goyal1ab60e02007-05-02 19:27:07 +0200356ENTRY(phys_base)
357 /* This must match the first entry in level2_kernel_pgt */
358 .quad 0x0000000000000000
359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360/* We need valid kernel segments for data and code in long mode too
361 * IRET will check the segment types kkeil 2000/10/28
362 * Also sysret mandates a special GDT layout
363 */
364
Jan Beuliche57113b2006-03-25 16:30:01 +0100365 .section .data.page_aligned, "aw"
366 .align PAGE_SIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368/* The TLS descriptors are currently at a different place compared to i386.
369 Hopefully nobody expects them at a fixed place (Wine?) */
370
371ENTRY(cpu_gdt_table)
372 .quad 0x0000000000000000 /* NULL descriptor */
Vivek Goyal30f47282007-05-02 19:27:07 +0200373 .quad 0x00cf9b000000ffff /* __KERNEL32_CS */
374 .quad 0x00af9b000000ffff /* __KERNEL_CS */
375 .quad 0x00cf93000000ffff /* __KERNEL_DS */
376 .quad 0x00cffb000000ffff /* __USER32_CS */
377 .quad 0x00cff3000000ffff /* __USER_DS, __USER32_DS */
378 .quad 0x00affb000000ffff /* __USER_CS */
Andi Kleencdc4b9c2006-01-11 22:46:24 +0100379 .quad 0x0 /* unused */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 .quad 0,0 /* TSS */
381 .quad 0,0 /* LDT */
382 .quad 0,0,0 /* three TLS descriptors */
Vojtech Pavlikc08c8202006-09-26 10:52:28 +0200383 .quad 0x0000f40000000000 /* node/CPU stored in limit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384gdt_end:
385 /* asm/segment.h:GDT_ENTRIES must match this */
386 /* This should be a multiple of the cache line size */
Ravikiran G Thirumalaic11efdf2006-01-11 22:43:57 +0100387 /* GDTs of other CPUs are now dynamically allocated */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Ravikiran G Thirumalaic11efdf2006-01-11 22:43:57 +0100389 /* zero the remaining page */
390 .fill PAGE_SIZE / 8 - GDT_ENTRIES,8,0
391
Jan Beuliche57113b2006-03-25 16:30:01 +0100392 .section .bss, "aw", @nobits
393 .align L1_CACHE_BYTES
394ENTRY(idt_table)
395 .skip 256 * 16
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Jan Beuliche57113b2006-03-25 16:30:01 +0100397 .section .bss.page_aligned, "aw", @nobits
398 .align PAGE_SIZE
399ENTRY(empty_zero_page)
400 .skip PAGE_SIZE