head.S 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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. #include <linux/linkage.h>
  10. #include <linux/threads.h>
  11. #include <linux/init.h>
  12. #include <asm/desc.h>
  13. #include <asm/segment.h>
  14. #include <asm/pgtable.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. .section .bootstrap.text
  25. .code32
  26. .globl startup_32
  27. /* %bx: 1 if coming from smp trampoline on secondary cpu */
  28. startup_32:
  29. /*
  30. * At this point the CPU runs in 32bit protected mode (CS.D = 1) with
  31. * paging disabled and the point of this file is to switch to 64bit
  32. * long mode with a kernel mapping for kerneland to jump into the
  33. * kernel virtual addresses.
  34. * There is no stack until we set one up.
  35. */
  36. /* Initialize the %ds segment register */
  37. movl $__KERNEL_DS,%eax
  38. movl %eax,%ds
  39. /* Load new GDT with the 64bit segments using 32bit descriptor */
  40. lgdt pGDT32 - __START_KERNEL_map
  41. /* If the CPU doesn't support CPUID this will double fault.
  42. * Unfortunately it is hard to check for CPUID without a stack.
  43. */
  44. /* Check if extended functions are implemented */
  45. movl $0x80000000, %eax
  46. cpuid
  47. cmpl $0x80000000, %eax
  48. jbe no_long_mode
  49. /* Check if long mode is implemented */
  50. mov $0x80000001, %eax
  51. cpuid
  52. btl $29, %edx
  53. jnc no_long_mode
  54. /*
  55. * Prepare for entering 64bits mode
  56. */
  57. /* Enable PAE mode */
  58. xorl %eax, %eax
  59. btsl $5, %eax
  60. movl %eax, %cr4
  61. /* Setup early boot stage 4 level pagetables */
  62. movl $(boot_level4_pgt - __START_KERNEL_map), %eax
  63. movl %eax, %cr3
  64. /* Setup EFER (Extended Feature Enable Register) */
  65. movl $MSR_EFER, %ecx
  66. rdmsr
  67. /* Enable Long Mode */
  68. btsl $_EFER_LME, %eax
  69. /* Make changes effective */
  70. wrmsr
  71. xorl %eax, %eax
  72. btsl $31, %eax /* Enable paging and in turn activate Long Mode */
  73. btsl $0, %eax /* Enable protected mode */
  74. /* Make changes effective */
  75. movl %eax, %cr0
  76. /*
  77. * At this point we're in long mode but in 32bit compatibility mode
  78. * with EFER.LME = 1, CS.L = 0, CS.D = 1 (and in turn
  79. * EFER.LMA = 1). Now we want to jump in 64bit mode, to do that we use
  80. * the new gdt/idt that has __KERNEL_CS with CS.L = 1.
  81. */
  82. ljmp $__KERNEL_CS, $(startup_64 - __START_KERNEL_map)
  83. .code64
  84. .org 0x100
  85. .globl startup_64
  86. startup_64:
  87. ENTRY(secondary_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. /* set up data segments. actually 0 would do too */
  141. movl $__KERNEL_DS,%eax
  142. movl %eax,%ds
  143. movl %eax,%ss
  144. movl %eax,%es
  145. /*
  146. * We don't really need to load %fs or %gs, but load them anyway
  147. * to kill any stale realmode selectors. This allows execution
  148. * under VT hardware.
  149. */
  150. movl %eax,%fs
  151. movl %eax,%gs
  152. /*
  153. * Setup up a dummy PDA. this is just for some early bootup code
  154. * that does in_interrupt()
  155. */
  156. movl $MSR_GS_BASE,%ecx
  157. movq $empty_zero_page,%rax
  158. movq %rax,%rdx
  159. shrq $32,%rdx
  160. wrmsr
  161. /* esi is pointer to real mode structure with interesting info.
  162. pass it to C */
  163. movl %esi, %edi
  164. /* Finally jump to run C code and to be on real kernel address
  165. * Since we are running on identity-mapped space we have to jump
  166. * to the full 64bit address, this is only possible as indirect
  167. * jump. In addition we need to ensure %cs is set so we make this
  168. * a far return.
  169. */
  170. movq initial_code(%rip),%rax
  171. pushq $0 # fake return address to stop unwinder
  172. pushq $__KERNEL_CS # set correct cs
  173. pushq %rax # target address in negative space
  174. lretq
  175. /* SMP bootup changes these two */
  176. .align 8
  177. .globl initial_code
  178. initial_code:
  179. .quad x86_64_start_kernel
  180. .globl init_rsp
  181. init_rsp:
  182. .quad init_thread_union+THREAD_SIZE-8
  183. ENTRY(early_idt_handler)
  184. cmpl $2,early_recursion_flag(%rip)
  185. jz 1f
  186. incl early_recursion_flag(%rip)
  187. xorl %eax,%eax
  188. movq 8(%rsp),%rsi # get rip
  189. movq (%rsp),%rdx
  190. movq %cr2,%rcx
  191. leaq early_idt_msg(%rip),%rdi
  192. call early_printk
  193. cmpl $2,early_recursion_flag(%rip)
  194. jz 1f
  195. call dump_stack
  196. #ifdef CONFIG_KALLSYMS
  197. leaq early_idt_ripmsg(%rip),%rdi
  198. movq 8(%rsp),%rsi # get rip again
  199. call __print_symbol
  200. #endif
  201. 1: hlt
  202. jmp 1b
  203. early_recursion_flag:
  204. .long 0
  205. early_idt_msg:
  206. .asciz "PANIC: early exception rip %lx error %lx cr2 %lx\n"
  207. early_idt_ripmsg:
  208. .asciz "RIP %s\n"
  209. .code32
  210. ENTRY(no_long_mode)
  211. /* This isn't an x86-64 CPU so hang */
  212. 1:
  213. jmp 1b
  214. .org 0xf00
  215. .globl pGDT32
  216. pGDT32:
  217. .word gdt_end-cpu_gdt_table-1
  218. .long cpu_gdt_table-__START_KERNEL_map
  219. .org 0xf10
  220. ljumpvector:
  221. .long startup_64-__START_KERNEL_map
  222. .word __KERNEL_CS
  223. ENTRY(stext)
  224. ENTRY(_stext)
  225. #define NEXT_PAGE(name) \
  226. .balign PAGE_SIZE; \
  227. ENTRY(name)
  228. /* Automate the creation of 1 to 1 mapping pmd entries */
  229. #define PMDS(START, PERM, COUNT) \
  230. i = 0 ; \
  231. .rept (COUNT) ; \
  232. .quad (START) + (i << 21) + (PERM) ; \
  233. i = i + 1 ; \
  234. .endr
  235. NEXT_PAGE(init_level4_pgt)
  236. /* This gets initialized in x86_64_start_kernel */
  237. .fill 512,8,0
  238. NEXT_PAGE(level3_ident_pgt)
  239. .quad level2_ident_pgt - __START_KERNEL_map + _KERNPG_TABLE
  240. .fill 511,8,0
  241. NEXT_PAGE(level3_kernel_pgt)
  242. .fill 510,8,0
  243. /* (2^48-(2*1024*1024*1024)-((2^39)*511))/(2^30) = 510 */
  244. .quad level2_kernel_pgt - __START_KERNEL_map + _KERNPG_TABLE
  245. .fill 1,8,0
  246. NEXT_PAGE(level2_ident_pgt)
  247. /* Since I easily can, map the first 1G.
  248. * Don't set NX because code runs from these pages.
  249. */
  250. PMDS(0x0000000000000000, __PAGE_KERNEL_LARGE_EXEC, PTRS_PER_PMD)
  251. NEXT_PAGE(level2_kernel_pgt)
  252. /* 40MB kernel mapping. The kernel code cannot be bigger than that.
  253. When you change this change KERNEL_TEXT_SIZE in page.h too. */
  254. /* (2^48-(2*1024*1024*1024)-((2^39)*511)-((2^30)*510)) = 0 */
  255. PMDS(0x0000000000000000, __PAGE_KERNEL_LARGE_EXEC|_PAGE_GLOBAL,
  256. KERNEL_TEXT_SIZE/PMD_SIZE)
  257. /* Module mapping starts here */
  258. .fill (PTRS_PER_PMD - (KERNEL_TEXT_SIZE/PMD_SIZE)),8,0
  259. #undef PMDS
  260. #undef NEXT_PAGE
  261. .data
  262. #ifdef CONFIG_ACPI_SLEEP
  263. .align PAGE_SIZE
  264. ENTRY(wakeup_level4_pgt)
  265. .quad level3_ident_pgt - __START_KERNEL_map + _KERNPG_TABLE
  266. .fill 510,8,0
  267. /* (2^48-(2*1024*1024*1024))/(2^39) = 511 */
  268. .quad level3_kernel_pgt - __START_KERNEL_map + _KERNPG_TABLE
  269. #endif
  270. #ifndef CONFIG_HOTPLUG_CPU
  271. __INITDATA
  272. #endif
  273. /*
  274. * This default setting generates an ident mapping at address 0x100000
  275. * and a mapping for the kernel that precisely maps virtual address
  276. * 0xffffffff80000000 to physical address 0x000000. (always using
  277. * 2Mbyte large pages provided by PAE mode)
  278. */
  279. .align PAGE_SIZE
  280. ENTRY(boot_level4_pgt)
  281. .quad level3_ident_pgt - __START_KERNEL_map + _KERNPG_TABLE
  282. .fill 257,8,0
  283. .quad level3_ident_pgt - __START_KERNEL_map + _KERNPG_TABLE
  284. .fill 252,8,0
  285. /* (2^48-(2*1024*1024*1024))/(2^39) = 511 */
  286. .quad level3_kernel_pgt - __START_KERNEL_map + _PAGE_TABLE
  287. .data
  288. .align 16
  289. .globl cpu_gdt_descr
  290. cpu_gdt_descr:
  291. .word gdt_end-cpu_gdt_table-1
  292. gdt:
  293. .quad cpu_gdt_table
  294. #ifdef CONFIG_SMP
  295. .rept NR_CPUS-1
  296. .word 0
  297. .quad 0
  298. .endr
  299. #endif
  300. /* We need valid kernel segments for data and code in long mode too
  301. * IRET will check the segment types kkeil 2000/10/28
  302. * Also sysret mandates a special GDT layout
  303. */
  304. .section .data.page_aligned, "aw"
  305. .align PAGE_SIZE
  306. /* The TLS descriptors are currently at a different place compared to i386.
  307. Hopefully nobody expects them at a fixed place (Wine?) */
  308. ENTRY(cpu_gdt_table)
  309. .quad 0x0000000000000000 /* NULL descriptor */
  310. .quad 0x00cf9b000000ffff /* __KERNEL32_CS */
  311. .quad 0x00af9b000000ffff /* __KERNEL_CS */
  312. .quad 0x00cf93000000ffff /* __KERNEL_DS */
  313. .quad 0x00cffb000000ffff /* __USER32_CS */
  314. .quad 0x00cff3000000ffff /* __USER_DS, __USER32_DS */
  315. .quad 0x00affb000000ffff /* __USER_CS */
  316. .quad 0x0 /* unused */
  317. .quad 0,0 /* TSS */
  318. .quad 0,0 /* LDT */
  319. .quad 0,0,0 /* three TLS descriptors */
  320. .quad 0x0000f40000000000 /* node/CPU stored in limit */
  321. gdt_end:
  322. /* asm/segment.h:GDT_ENTRIES must match this */
  323. /* This should be a multiple of the cache line size */
  324. /* GDTs of other CPUs are now dynamically allocated */
  325. /* zero the remaining page */
  326. .fill PAGE_SIZE / 8 - GDT_ENTRIES,8,0
  327. .section .bss, "aw", @nobits
  328. .align L1_CACHE_BYTES
  329. ENTRY(idt_table)
  330. .skip 256 * 16
  331. .section .bss.page_aligned, "aw", @nobits
  332. .align PAGE_SIZE
  333. ENTRY(empty_zero_page)
  334. .skip PAGE_SIZE