head.S 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. pushq $0 # fake return address
  164. jmp *%rax
  165. /* SMP bootup changes these two */
  166. .align 8
  167. .globl initial_code
  168. initial_code:
  169. .quad x86_64_start_kernel
  170. .globl init_rsp
  171. init_rsp:
  172. .quad init_thread_union+THREAD_SIZE-8
  173. ENTRY(early_idt_handler)
  174. cmpl $2,early_recursion_flag(%rip)
  175. jz 1f
  176. incl early_recursion_flag(%rip)
  177. xorl %eax,%eax
  178. movq 8(%rsp),%rsi # get rip
  179. movq (%rsp),%rdx
  180. movq %cr2,%rcx
  181. leaq early_idt_msg(%rip),%rdi
  182. call early_printk
  183. cmpl $2,early_recursion_flag(%rip)
  184. jz 1f
  185. call dump_stack
  186. #ifdef CONFIG_KALLSYMS
  187. leaq early_idt_ripmsg(%rip),%rdi
  188. movq 8(%rsp),%rsi # get rip again
  189. call __print_symbol
  190. #endif
  191. 1: hlt
  192. jmp 1b
  193. early_recursion_flag:
  194. .long 0
  195. early_idt_msg:
  196. .asciz "PANIC: early exception rip %lx error %lx cr2 %lx\n"
  197. early_idt_ripmsg:
  198. .asciz "RIP %s\n"
  199. .code32
  200. ENTRY(no_long_mode)
  201. /* This isn't an x86-64 CPU so hang */
  202. 1:
  203. jmp 1b
  204. .org 0xf00
  205. .globl pGDT32
  206. pGDT32:
  207. .word gdt_end-cpu_gdt_table-1
  208. .long cpu_gdt_table-__START_KERNEL_map
  209. .org 0xf10
  210. ljumpvector:
  211. .long startup_64-__START_KERNEL_map
  212. .word __KERNEL_CS
  213. ENTRY(stext)
  214. ENTRY(_stext)
  215. $page = 0
  216. #define NEXT_PAGE(name) \
  217. $page = $page + 1; \
  218. .org $page * 0x1000; \
  219. phys_/**/name = $page * 0x1000 + __PHYSICAL_START; \
  220. ENTRY(name)
  221. NEXT_PAGE(init_level4_pgt)
  222. /* This gets initialized in x86_64_start_kernel */
  223. .fill 512,8,0
  224. NEXT_PAGE(level3_ident_pgt)
  225. .quad phys_level2_ident_pgt | 0x007
  226. .fill 511,8,0
  227. NEXT_PAGE(level3_kernel_pgt)
  228. .fill 510,8,0
  229. /* (2^48-(2*1024*1024*1024)-((2^39)*511))/(2^30) = 510 */
  230. .quad phys_level2_kernel_pgt | 0x007
  231. .fill 1,8,0
  232. NEXT_PAGE(level2_ident_pgt)
  233. /* 40MB for bootup. */
  234. i = 0
  235. .rept 20
  236. .quad i << 21 | 0x083
  237. i = i + 1
  238. .endr
  239. /* Temporary mappings for the super early allocator in arch/x86_64/mm/init.c */
  240. .globl temp_boot_pmds
  241. temp_boot_pmds:
  242. .fill 492,8,0
  243. NEXT_PAGE(level2_kernel_pgt)
  244. /* 40MB kernel mapping. The kernel code cannot be bigger than that.
  245. When you change this change KERNEL_TEXT_SIZE in page.h too. */
  246. /* (2^48-(2*1024*1024*1024)-((2^39)*511)-((2^30)*510)) = 0 */
  247. i = 0
  248. .rept 20
  249. .quad i << 21 | 0x183
  250. i = i + 1
  251. .endr
  252. /* Module mapping starts here */
  253. .fill 492,8,0
  254. NEXT_PAGE(level3_physmem_pgt)
  255. .quad phys_level2_kernel_pgt | 0x007 /* so that __va works even before pagetable_init */
  256. .fill 511,8,0
  257. #undef NEXT_PAGE
  258. .data
  259. #ifdef CONFIG_ACPI_SLEEP
  260. .align PAGE_SIZE
  261. ENTRY(wakeup_level4_pgt)
  262. .quad phys_level3_ident_pgt | 0x007
  263. .fill 255,8,0
  264. .quad phys_level3_physmem_pgt | 0x007
  265. .fill 254,8,0
  266. /* (2^48-(2*1024*1024*1024))/(2^39) = 511 */
  267. .quad phys_level3_kernel_pgt | 0x007
  268. #endif
  269. #ifndef CONFIG_HOTPLUG_CPU
  270. __INITDATA
  271. #endif
  272. /*
  273. * This default setting generates an ident mapping at address 0x100000
  274. * and a mapping for the kernel that precisely maps virtual address
  275. * 0xffffffff80000000 to physical address 0x000000. (always using
  276. * 2Mbyte large pages provided by PAE mode)
  277. */
  278. .align PAGE_SIZE
  279. ENTRY(boot_level4_pgt)
  280. .quad phys_level3_ident_pgt | 0x007
  281. .fill 255,8,0
  282. .quad phys_level3_physmem_pgt | 0x007
  283. .fill 254,8,0
  284. /* (2^48-(2*1024*1024*1024))/(2^39) = 511 */
  285. .quad phys_level3_kernel_pgt | 0x007
  286. .data
  287. .align 16
  288. .globl cpu_gdt_descr
  289. cpu_gdt_descr:
  290. .word gdt_end-cpu_gdt_table-1
  291. gdt:
  292. .quad cpu_gdt_table
  293. #ifdef CONFIG_SMP
  294. .rept NR_CPUS-1
  295. .word 0
  296. .quad 0
  297. .endr
  298. #endif
  299. /* We need valid kernel segments for data and code in long mode too
  300. * IRET will check the segment types kkeil 2000/10/28
  301. * Also sysret mandates a special GDT layout
  302. */
  303. .section .data.page_aligned, "aw"
  304. .align PAGE_SIZE
  305. /* The TLS descriptors are currently at a different place compared to i386.
  306. Hopefully nobody expects them at a fixed place (Wine?) */
  307. ENTRY(cpu_gdt_table)
  308. .quad 0x0000000000000000 /* NULL descriptor */
  309. .quad 0x0 /* unused */
  310. .quad 0x00af9a000000ffff /* __KERNEL_CS */
  311. .quad 0x00cf92000000ffff /* __KERNEL_DS */
  312. .quad 0x00cffa000000ffff /* __USER32_CS */
  313. .quad 0x00cff2000000ffff /* __USER_DS, __USER32_DS */
  314. .quad 0x00affa000000ffff /* __USER_CS */
  315. .quad 0x00cf9a000000ffff /* __KERNEL32_CS */
  316. .quad 0,0 /* TSS */
  317. .quad 0,0 /* LDT */
  318. .quad 0,0,0 /* three TLS descriptors */
  319. .quad 0 /* unused */
  320. gdt_end:
  321. /* asm/segment.h:GDT_ENTRIES must match this */
  322. /* This should be a multiple of the cache line size */
  323. /* GDTs of other CPUs are now dynamically allocated */
  324. /* zero the remaining page */
  325. .fill PAGE_SIZE / 8 - GDT_ENTRIES,8,0
  326. .section .bss, "aw", @nobits
  327. .align L1_CACHE_BYTES
  328. ENTRY(idt_table)
  329. .skip 256 * 16
  330. .section .bss.page_aligned, "aw", @nobits
  331. .align PAGE_SIZE
  332. ENTRY(empty_zero_page)
  333. .skip PAGE_SIZE