head.S 9.9 KB

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