head.S 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. #include <linux/linkage.h>
  12. #include <linux/threads.h>
  13. #include <linux/init.h>
  14. #include <asm/desc.h>
  15. #include <asm/segment.h>
  16. #include <asm/page.h>
  17. #include <asm/msr.h>
  18. #include <asm/cache.h>
  19. /* we are not able to switch in one step to the final KERNEL ADRESS SPACE
  20. * because we need identity-mapped pages on setup so define __START_KERNEL to
  21. * 0x100000 for this stage
  22. *
  23. */
  24. .text
  25. .section .bootstrap.text
  26. .code32
  27. .globl startup_32
  28. /* %bx: 1 if coming from smp trampoline on secondary cpu */
  29. startup_32:
  30. /*
  31. * At this point the CPU runs in 32bit protected mode (CS.D = 1) with
  32. * paging disabled and the point of this file is to switch to 64bit
  33. * long mode with a kernel mapping for kerneland to jump into the
  34. * kernel virtual addresses.
  35. * There is no stack until we set one up.
  36. */
  37. /* Initialize the %ds segment register */
  38. movl $__KERNEL_DS,%eax
  39. movl %eax,%ds
  40. /* Load new GDT with the 64bit segments using 32bit descriptor */
  41. lgdt pGDT32 - __START_KERNEL_map
  42. /* If the CPU doesn't support CPUID this will double fault.
  43. * Unfortunately it is hard to check for CPUID without a stack.
  44. */
  45. /* Check if extended functions are implemented */
  46. movl $0x80000000, %eax
  47. cpuid
  48. cmpl $0x80000000, %eax
  49. jbe no_long_mode
  50. /* Check if long mode is implemented */
  51. mov $0x80000001, %eax
  52. cpuid
  53. btl $29, %edx
  54. jnc no_long_mode
  55. /*
  56. * Prepare for entering 64bits mode
  57. */
  58. /* Enable PAE mode */
  59. xorl %eax, %eax
  60. btsl $5, %eax
  61. movl %eax, %cr4
  62. /* Setup early boot stage 4 level pagetables */
  63. movl $(boot_level4_pgt - __START_KERNEL_map), %eax
  64. movl %eax, %cr3
  65. /* Setup EFER (Extended Feature Enable Register) */
  66. movl $MSR_EFER, %ecx
  67. rdmsr
  68. /* Enable Long Mode */
  69. btsl $_EFER_LME, %eax
  70. /* Make changes effective */
  71. wrmsr
  72. xorl %eax, %eax
  73. btsl $31, %eax /* Enable paging and in turn activate Long Mode */
  74. btsl $0, %eax /* Enable protected mode */
  75. /* Make changes effective */
  76. movl %eax, %cr0
  77. /*
  78. * At this point we're in long mode but in 32bit compatibility mode
  79. * with EFER.LME = 1, CS.L = 0, CS.D = 1 (and in turn
  80. * EFER.LMA = 1). Now we want to jump in 64bit mode, to do that we use
  81. * the new gdt/idt that has __KERNEL_CS with CS.L = 1.
  82. */
  83. ljmp $__KERNEL_CS, $(startup_64 - __START_KERNEL_map)
  84. .code64
  85. .org 0x100
  86. .globl startup_64
  87. startup_64:
  88. /* We come here either from startup_32
  89. * or directly from a 64bit bootloader.
  90. * Since we may have come directly from a bootloader we
  91. * reload the page tables here.
  92. */
  93. /* Enable PAE mode and PGE */
  94. xorq %rax, %rax
  95. btsq $5, %rax
  96. btsq $7, %rax
  97. movq %rax, %cr4
  98. /* Setup early boot stage 4 level pagetables. */
  99. movq $(boot_level4_pgt - __START_KERNEL_map), %rax
  100. movq %rax, %cr3
  101. /* Check if nx is implemented */
  102. movl $0x80000001, %eax
  103. cpuid
  104. movl %edx,%edi
  105. /* Setup EFER (Extended Feature Enable Register) */
  106. movl $MSR_EFER, %ecx
  107. rdmsr
  108. /* Enable System Call */
  109. btsl $_EFER_SCE, %eax
  110. /* No Execute supported? */
  111. btl $20,%edi
  112. jnc 1f
  113. btsl $_EFER_NX, %eax
  114. 1:
  115. /* Make changes effective */
  116. wrmsr
  117. /* Setup cr0 */
  118. #define CR0_PM 1 /* protected mode */
  119. #define CR0_MP (1<<1)
  120. #define CR0_ET (1<<4)
  121. #define CR0_NE (1<<5)
  122. #define CR0_WP (1<<16)
  123. #define CR0_AM (1<<18)
  124. #define CR0_PAGING (1<<31)
  125. movl $CR0_PM|CR0_MP|CR0_ET|CR0_NE|CR0_WP|CR0_AM|CR0_PAGING,%eax
  126. /* Make changes effective */
  127. movq %rax, %cr0
  128. /* Setup a boot time stack */
  129. movq init_rsp(%rip),%rsp
  130. /* zero EFLAGS after setting rsp */
  131. pushq $0
  132. popfq
  133. /*
  134. * We must switch to a new descriptor in kernel space for the GDT
  135. * because soon the kernel won't have access anymore to the userspace
  136. * addresses where we're currently running on. We have to do that here
  137. * because in 32bit we couldn't load a 64bit linear address.
  138. */
  139. lgdt cpu_gdt_descr
  140. /*
  141. * Setup up a dummy PDA. this is just for some early bootup code
  142. * that does in_interrupt()
  143. */
  144. movl $MSR_GS_BASE,%ecx
  145. movq $empty_zero_page,%rax
  146. movq %rax,%rdx
  147. shrq $32,%rdx
  148. wrmsr
  149. /* set up data segments. actually 0 would do too */
  150. movl $__KERNEL_DS,%eax
  151. movl %eax,%ds
  152. movl %eax,%ss
  153. movl %eax,%es
  154. /* esi is pointer to real mode structure with interesting info.
  155. pass it to C */
  156. movl %esi, %edi
  157. /* Finally jump to run C code and to be on real kernel address
  158. * Since we are running on identity-mapped space we have to jump
  159. * to the full 64bit address , this is only possible as indirect
  160. * jump
  161. */
  162. movq initial_code(%rip),%rax
  163. jmp *%rax
  164. /* SMP bootup changes these two */
  165. .align 8
  166. .globl initial_code
  167. initial_code:
  168. .quad x86_64_start_kernel
  169. .globl init_rsp
  170. init_rsp:
  171. .quad init_thread_union+THREAD_SIZE-8
  172. ENTRY(early_idt_handler)
  173. cmpl $2,early_recursion_flag(%rip)
  174. jz 1f
  175. incl early_recursion_flag(%rip)
  176. xorl %eax,%eax
  177. movq 8(%rsp),%rsi # get rip
  178. movq (%rsp),%rdx
  179. movq %cr2,%rcx
  180. leaq early_idt_msg(%rip),%rdi
  181. call early_printk
  182. cmpl $2,early_recursion_flag(%rip)
  183. jz 1f
  184. call dump_stack
  185. #ifdef CONFIG_KALLSYMS
  186. leaq early_idt_ripmsg(%rip),%rdi
  187. movq 8(%rsp),%rsi # get rip again
  188. call __print_symbol
  189. #endif
  190. 1: hlt
  191. jmp 1b
  192. early_recursion_flag:
  193. .long 0
  194. early_idt_msg:
  195. .asciz "PANIC: early exception rip %lx error %lx cr2 %lx\n"
  196. early_idt_ripmsg:
  197. .asciz "RIP %s\n"
  198. .code32
  199. ENTRY(no_long_mode)
  200. /* This isn't an x86-64 CPU so hang */
  201. 1:
  202. jmp 1b
  203. .org 0xf00
  204. .globl pGDT32
  205. pGDT32:
  206. .word gdt_end-cpu_gdt_table-1
  207. .long cpu_gdt_table-__START_KERNEL_map
  208. .org 0xf10
  209. ljumpvector:
  210. .long startup_64-__START_KERNEL_map
  211. .word __KERNEL_CS
  212. ENTRY(stext)
  213. ENTRY(_stext)
  214. $page = 0
  215. #define NEXT_PAGE(name) \
  216. $page = $page + 1; \
  217. .org $page * 0x1000; \
  218. phys_/**/name = $page * 0x1000 + __PHYSICAL_START; \
  219. ENTRY(name)
  220. NEXT_PAGE(init_level4_pgt)
  221. /* This gets initialized in x86_64_start_kernel */
  222. .fill 512,8,0
  223. NEXT_PAGE(level3_ident_pgt)
  224. .quad phys_level2_ident_pgt | 0x007
  225. .fill 511,8,0
  226. NEXT_PAGE(level3_kernel_pgt)
  227. .fill 510,8,0
  228. /* (2^48-(2*1024*1024*1024)-((2^39)*511))/(2^30) = 510 */
  229. .quad phys_level2_kernel_pgt | 0x007
  230. .fill 1,8,0
  231. NEXT_PAGE(level2_ident_pgt)
  232. /* 40MB for bootup. */
  233. i = 0
  234. .rept 20
  235. .quad i << 21 | 0x083
  236. i = i + 1
  237. .endr
  238. /* Temporary mappings for the super early allocator in arch/x86_64/mm/init.c */
  239. .globl temp_boot_pmds
  240. temp_boot_pmds:
  241. .fill 492,8,0
  242. NEXT_PAGE(level2_kernel_pgt)
  243. /* 40MB kernel mapping. The kernel code cannot be bigger than that.
  244. When you change this change KERNEL_TEXT_SIZE in page.h too. */
  245. /* (2^48-(2*1024*1024*1024)-((2^39)*511)-((2^30)*510)) = 0 */
  246. i = 0
  247. .rept 20
  248. .quad i << 21 | 0x183
  249. i = i + 1
  250. .endr
  251. /* Module mapping starts here */
  252. .fill 492,8,0
  253. NEXT_PAGE(level3_physmem_pgt)
  254. .quad phys_level2_kernel_pgt | 0x007 /* so that __va works even before pagetable_init */
  255. .fill 511,8,0
  256. #undef NEXT_PAGE
  257. .data
  258. #ifdef CONFIG_ACPI_SLEEP
  259. .align PAGE_SIZE
  260. ENTRY(wakeup_level4_pgt)
  261. .quad phys_level3_ident_pgt | 0x007
  262. .fill 255,8,0
  263. .quad phys_level3_physmem_pgt | 0x007
  264. .fill 254,8,0
  265. /* (2^48-(2*1024*1024*1024))/(2^39) = 511 */
  266. .quad phys_level3_kernel_pgt | 0x007
  267. #endif
  268. #ifndef CONFIG_HOTPLUG_CPU
  269. __INITDATA
  270. #endif
  271. /*
  272. * This default setting generates an ident mapping at address 0x100000
  273. * and a mapping for the kernel that precisely maps virtual address
  274. * 0xffffffff80000000 to physical address 0x000000. (always using
  275. * 2Mbyte large pages provided by PAE mode)
  276. */
  277. .align PAGE_SIZE
  278. ENTRY(boot_level4_pgt)
  279. .quad phys_level3_ident_pgt | 0x007
  280. .fill 255,8,0
  281. .quad phys_level3_physmem_pgt | 0x007
  282. .fill 254,8,0
  283. /* (2^48-(2*1024*1024*1024))/(2^39) = 511 */
  284. .quad phys_level3_kernel_pgt | 0x007
  285. .data
  286. .align 16
  287. .globl cpu_gdt_descr
  288. cpu_gdt_descr:
  289. .word gdt_end-cpu_gdt_table-1
  290. gdt:
  291. .quad cpu_gdt_table
  292. #ifdef CONFIG_SMP
  293. .rept NR_CPUS-1
  294. .word 0
  295. .quad 0
  296. .endr
  297. #endif
  298. /* We need valid kernel segments for data and code in long mode too
  299. * IRET will check the segment types kkeil 2000/10/28
  300. * Also sysret mandates a special GDT layout
  301. */
  302. .section .data.page_aligned, "aw"
  303. .align PAGE_SIZE
  304. /* The TLS descriptors are currently at a different place compared to i386.
  305. Hopefully nobody expects them at a fixed place (Wine?) */
  306. ENTRY(cpu_gdt_table)
  307. .quad 0x0000000000000000 /* NULL descriptor */
  308. .quad 0x0 /* unused */
  309. .quad 0x00af9a000000ffff /* __KERNEL_CS */
  310. .quad 0x00cf92000000ffff /* __KERNEL_DS */
  311. .quad 0x00cffa000000ffff /* __USER32_CS */
  312. .quad 0x00cff2000000ffff /* __USER_DS, __USER32_DS */
  313. .quad 0x00affa000000ffff /* __USER_CS */
  314. .quad 0x00cf9a000000ffff /* __KERNEL32_CS */
  315. .quad 0,0 /* TSS */
  316. .quad 0,0 /* LDT */
  317. .quad 0,0,0 /* three TLS descriptors */
  318. .quad 0 /* unused */
  319. gdt_end:
  320. /* asm/segment.h:GDT_ENTRIES must match this */
  321. /* This should be a multiple of the cache line size */
  322. /* GDTs of other CPUs are now dynamically allocated */
  323. /* zero the remaining page */
  324. .fill PAGE_SIZE / 8 - GDT_ENTRIES,8,0
  325. .section .bss, "aw", @nobits
  326. .align L1_CACHE_BYTES
  327. ENTRY(idt_table)
  328. .skip 256 * 16
  329. .section .bss.page_aligned, "aw", @nobits
  330. .align PAGE_SIZE
  331. ENTRY(empty_zero_page)
  332. .skip PAGE_SIZE