head_64.S 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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. * Copyright (C) 2005 Eric Biederman <ebiederm@xmission.com>
  9. */
  10. #include <linux/linkage.h>
  11. #include <linux/threads.h>
  12. #include <linux/init.h>
  13. #include <asm/desc.h>
  14. #include <asm/segment.h>
  15. #include <asm/pgtable.h>
  16. #include <asm/page.h>
  17. #include <asm/msr.h>
  18. #include <asm/cache.h>
  19. #include <asm/processor-flags.h>
  20. #ifdef CONFIG_PARAVIRT
  21. #include <asm/asm-offsets.h>
  22. #include <asm/paravirt.h>
  23. #else
  24. #define GET_CR2_INTO_RCX movq %cr2, %rcx
  25. #endif
  26. /* we are not able to switch in one step to the final KERNEL ADRESS SPACE
  27. * because we need identity-mapped pages.
  28. *
  29. */
  30. #define pud_index(x) (((x) >> PUD_SHIFT) & (PTRS_PER_PUD-1))
  31. L4_PAGE_OFFSET = pgd_index(__PAGE_OFFSET)
  32. L3_PAGE_OFFSET = pud_index(__PAGE_OFFSET)
  33. L4_START_KERNEL = pgd_index(__START_KERNEL_map)
  34. L3_START_KERNEL = pud_index(__START_KERNEL_map)
  35. .text
  36. .section .text.head
  37. .code64
  38. .globl startup_64
  39. startup_64:
  40. /*
  41. * At this point the CPU runs in 64bit mode CS.L = 1 CS.D = 1,
  42. * and someone has loaded an identity mapped page table
  43. * for us. These identity mapped page tables map all of the
  44. * kernel pages and possibly all of memory.
  45. *
  46. * %esi holds a physical pointer to real_mode_data.
  47. *
  48. * We come here either directly from a 64bit bootloader, or from
  49. * arch/x86_64/boot/compressed/head.S.
  50. *
  51. * We only come here initially at boot nothing else comes here.
  52. *
  53. * Since we may be loaded at an address different from what we were
  54. * compiled to run at we first fixup the physical addresses in our page
  55. * tables and then reload them.
  56. */
  57. /* Compute the delta between the address I am compiled to run at and the
  58. * address I am actually running at.
  59. */
  60. leaq _text(%rip), %rbp
  61. subq $_text - __START_KERNEL_map, %rbp
  62. /* Is the address not 2M aligned? */
  63. movq %rbp, %rax
  64. andl $~PMD_PAGE_MASK, %eax
  65. testl %eax, %eax
  66. jnz bad_address
  67. /* Is the address too large? */
  68. leaq _text(%rip), %rdx
  69. movq $PGDIR_SIZE, %rax
  70. cmpq %rax, %rdx
  71. jae bad_address
  72. /* Fixup the physical addresses in the page table
  73. */
  74. addq %rbp, init_level4_pgt + 0(%rip)
  75. addq %rbp, init_level4_pgt + (L4_PAGE_OFFSET*8)(%rip)
  76. addq %rbp, init_level4_pgt + (L4_START_KERNEL*8)(%rip)
  77. addq %rbp, level3_ident_pgt + 0(%rip)
  78. addq %rbp, level3_kernel_pgt + (510*8)(%rip)
  79. addq %rbp, level3_kernel_pgt + (511*8)(%rip)
  80. addq %rbp, level2_fixmap_pgt + (506*8)(%rip)
  81. /* Add an Identity mapping if I am above 1G */
  82. leaq _text(%rip), %rdi
  83. andq $PMD_PAGE_MASK, %rdi
  84. movq %rdi, %rax
  85. shrq $PUD_SHIFT, %rax
  86. andq $(PTRS_PER_PUD - 1), %rax
  87. jz ident_complete
  88. leaq (level2_spare_pgt - __START_KERNEL_map + _KERNPG_TABLE)(%rbp), %rdx
  89. leaq level3_ident_pgt(%rip), %rbx
  90. movq %rdx, 0(%rbx, %rax, 8)
  91. movq %rdi, %rax
  92. shrq $PMD_SHIFT, %rax
  93. andq $(PTRS_PER_PMD - 1), %rax
  94. leaq __PAGE_KERNEL_IDENT_LARGE_EXEC(%rdi), %rdx
  95. leaq level2_spare_pgt(%rip), %rbx
  96. movq %rdx, 0(%rbx, %rax, 8)
  97. ident_complete:
  98. /*
  99. * Fixup the kernel text+data virtual addresses. Note that
  100. * we might write invalid pmds, when the kernel is relocated
  101. * cleanup_highmap() fixes this up along with the mappings
  102. * beyond _end.
  103. */
  104. leaq level2_kernel_pgt(%rip), %rdi
  105. leaq 4096(%rdi), %r8
  106. /* See if it is a valid page table entry */
  107. 1: testq $1, 0(%rdi)
  108. jz 2f
  109. addq %rbp, 0(%rdi)
  110. /* Go to the next page */
  111. 2: addq $8, %rdi
  112. cmp %r8, %rdi
  113. jne 1b
  114. /* Fixup phys_base */
  115. addq %rbp, phys_base(%rip)
  116. #ifdef CONFIG_X86_TRAMPOLINE
  117. addq %rbp, trampoline_level4_pgt + 0(%rip)
  118. addq %rbp, trampoline_level4_pgt + (511*8)(%rip)
  119. #endif
  120. /* Due to ENTRY(), sometimes the empty space gets filled with
  121. * zeros. Better take a jmp than relying on empty space being
  122. * filled with 0x90 (nop)
  123. */
  124. jmp secondary_startup_64
  125. ENTRY(secondary_startup_64)
  126. /*
  127. * At this point the CPU runs in 64bit mode CS.L = 1 CS.D = 1,
  128. * and someone has loaded a mapped page table.
  129. *
  130. * %esi holds a physical pointer to real_mode_data.
  131. *
  132. * We come here either from startup_64 (using physical addresses)
  133. * or from trampoline.S (using virtual addresses).
  134. *
  135. * Using virtual addresses from trampoline.S removes the need
  136. * to have any identity mapped pages in the kernel page table
  137. * after the boot processor executes this code.
  138. */
  139. /* Enable PAE mode and PGE */
  140. movl $(X86_CR4_PAE | X86_CR4_PGE), %eax
  141. movq %rax, %cr4
  142. /* Setup early boot stage 4 level pagetables. */
  143. movq $(init_level4_pgt - __START_KERNEL_map), %rax
  144. addq phys_base(%rip), %rax
  145. movq %rax, %cr3
  146. /* Ensure I am executing from virtual addresses */
  147. movq $1f, %rax
  148. jmp *%rax
  149. 1:
  150. /* Check if nx is implemented */
  151. movl $0x80000001, %eax
  152. cpuid
  153. movl %edx,%edi
  154. /* Setup EFER (Extended Feature Enable Register) */
  155. movl $MSR_EFER, %ecx
  156. rdmsr
  157. btsl $_EFER_SCE, %eax /* Enable System Call */
  158. btl $20,%edi /* No Execute supported? */
  159. jnc 1f
  160. btsl $_EFER_NX, %eax
  161. 1: wrmsr /* Make changes effective */
  162. /* Setup cr0 */
  163. #define CR0_STATE (X86_CR0_PE | X86_CR0_MP | X86_CR0_ET | \
  164. X86_CR0_NE | X86_CR0_WP | X86_CR0_AM | \
  165. X86_CR0_PG)
  166. movl $CR0_STATE, %eax
  167. /* Make changes effective */
  168. movq %rax, %cr0
  169. /* Setup a boot time stack */
  170. movq stack_start(%rip),%rsp
  171. /* zero EFLAGS after setting rsp */
  172. pushq $0
  173. popfq
  174. #ifdef CONFIG_SMP
  175. /*
  176. * early_gdt_base should point to the gdt_page in static percpu init
  177. * data area. Computing this requires two symbols - __per_cpu_load
  178. * and per_cpu__gdt_page. As linker can't do no such relocation, do
  179. * it by hand. As early_gdt_descr is manipulated by C code for
  180. * secondary CPUs, this should be done only once for the boot CPU
  181. * when early_gdt_descr_base contains zero.
  182. */
  183. movq early_gdt_descr_base(%rip), %rax
  184. testq %rax, %rax
  185. jnz 1f
  186. movq $__per_cpu_load, %rax
  187. addq $per_cpu__gdt_page, %rax
  188. movq %rax, early_gdt_descr_base(%rip)
  189. 1:
  190. #endif
  191. /*
  192. * We must switch to a new descriptor in kernel space for the GDT
  193. * because soon the kernel won't have access anymore to the userspace
  194. * addresses where we're currently running on. We have to do that here
  195. * because in 32bit we couldn't load a 64bit linear address.
  196. */
  197. lgdt early_gdt_descr(%rip)
  198. /* set up data segments. actually 0 would do too */
  199. movl $__KERNEL_DS,%eax
  200. movl %eax,%ds
  201. movl %eax,%ss
  202. movl %eax,%es
  203. /*
  204. * We don't really need to load %fs or %gs, but load them anyway
  205. * to kill any stale realmode selectors. This allows execution
  206. * under VT hardware.
  207. */
  208. movl %eax,%fs
  209. movl %eax,%gs
  210. /* Set up %gs.
  211. *
  212. * %gs should point to the pda. For initial boot, make %gs point
  213. * to the _boot_cpu_pda in data section. For a secondary CPU,
  214. * initial_gs should be set to its pda address before the CPU runs
  215. * this code.
  216. */
  217. movl $MSR_GS_BASE,%ecx
  218. movq initial_gs(%rip),%rax
  219. movq %rax,%rdx
  220. shrq $32,%rdx
  221. wrmsr
  222. /* esi is pointer to real mode structure with interesting info.
  223. pass it to C */
  224. movl %esi, %edi
  225. /* Finally jump to run C code and to be on real kernel address
  226. * Since we are running on identity-mapped space we have to jump
  227. * to the full 64bit address, this is only possible as indirect
  228. * jump. In addition we need to ensure %cs is set so we make this
  229. * a far return.
  230. */
  231. movq initial_code(%rip),%rax
  232. pushq $0 # fake return address to stop unwinder
  233. pushq $__KERNEL_CS # set correct cs
  234. pushq %rax # target address in negative space
  235. lretq
  236. /* SMP bootup changes these two */
  237. __REFDATA
  238. .align 8
  239. ENTRY(initial_code)
  240. .quad x86_64_start_kernel
  241. ENTRY(initial_gs)
  242. .quad _boot_cpu_pda
  243. __FINITDATA
  244. ENTRY(stack_start)
  245. .quad init_thread_union+THREAD_SIZE-8
  246. .word 0
  247. bad_address:
  248. jmp bad_address
  249. .section ".init.text","ax"
  250. #ifdef CONFIG_EARLY_PRINTK
  251. .globl early_idt_handlers
  252. early_idt_handlers:
  253. i = 0
  254. .rept NUM_EXCEPTION_VECTORS
  255. movl $i, %esi
  256. jmp early_idt_handler
  257. i = i + 1
  258. .endr
  259. #endif
  260. ENTRY(early_idt_handler)
  261. #ifdef CONFIG_EARLY_PRINTK
  262. cmpl $2,early_recursion_flag(%rip)
  263. jz 1f
  264. incl early_recursion_flag(%rip)
  265. GET_CR2_INTO_RCX
  266. movq %rcx,%r9
  267. xorl %r8d,%r8d # zero for error code
  268. movl %esi,%ecx # get vector number
  269. # Test %ecx against mask of vectors that push error code.
  270. cmpl $31,%ecx
  271. ja 0f
  272. movl $1,%eax
  273. salq %cl,%rax
  274. testl $0x27d00,%eax
  275. je 0f
  276. popq %r8 # get error code
  277. 0: movq 0(%rsp),%rcx # get ip
  278. movq 8(%rsp),%rdx # get cs
  279. xorl %eax,%eax
  280. leaq early_idt_msg(%rip),%rdi
  281. call early_printk
  282. cmpl $2,early_recursion_flag(%rip)
  283. jz 1f
  284. call dump_stack
  285. #ifdef CONFIG_KALLSYMS
  286. leaq early_idt_ripmsg(%rip),%rdi
  287. movq 0(%rsp),%rsi # get rip again
  288. call __print_symbol
  289. #endif
  290. #endif /* EARLY_PRINTK */
  291. 1: hlt
  292. jmp 1b
  293. #ifdef CONFIG_EARLY_PRINTK
  294. early_recursion_flag:
  295. .long 0
  296. early_idt_msg:
  297. .asciz "PANIC: early exception %02lx rip %lx:%lx error %lx cr2 %lx\n"
  298. early_idt_ripmsg:
  299. .asciz "RIP %s\n"
  300. #endif /* CONFIG_EARLY_PRINTK */
  301. .previous
  302. .balign PAGE_SIZE
  303. #define NEXT_PAGE(name) \
  304. .balign PAGE_SIZE; \
  305. ENTRY(name)
  306. /* Automate the creation of 1 to 1 mapping pmd entries */
  307. #define PMDS(START, PERM, COUNT) \
  308. i = 0 ; \
  309. .rept (COUNT) ; \
  310. .quad (START) + (i << PMD_SHIFT) + (PERM) ; \
  311. i = i + 1 ; \
  312. .endr
  313. /*
  314. * This default setting generates an ident mapping at address 0x100000
  315. * and a mapping for the kernel that precisely maps virtual address
  316. * 0xffffffff80000000 to physical address 0x000000. (always using
  317. * 2Mbyte large pages provided by PAE mode)
  318. */
  319. NEXT_PAGE(init_level4_pgt)
  320. .quad level3_ident_pgt - __START_KERNEL_map + _KERNPG_TABLE
  321. .org init_level4_pgt + L4_PAGE_OFFSET*8, 0
  322. .quad level3_ident_pgt - __START_KERNEL_map + _KERNPG_TABLE
  323. .org init_level4_pgt + L4_START_KERNEL*8, 0
  324. /* (2^48-(2*1024*1024*1024))/(2^39) = 511 */
  325. .quad level3_kernel_pgt - __START_KERNEL_map + _PAGE_TABLE
  326. NEXT_PAGE(level3_ident_pgt)
  327. .quad level2_ident_pgt - __START_KERNEL_map + _KERNPG_TABLE
  328. .fill 511,8,0
  329. NEXT_PAGE(level3_kernel_pgt)
  330. .fill L3_START_KERNEL,8,0
  331. /* (2^48-(2*1024*1024*1024)-((2^39)*511))/(2^30) = 510 */
  332. .quad level2_kernel_pgt - __START_KERNEL_map + _KERNPG_TABLE
  333. .quad level2_fixmap_pgt - __START_KERNEL_map + _PAGE_TABLE
  334. NEXT_PAGE(level2_fixmap_pgt)
  335. .fill 506,8,0
  336. .quad level1_fixmap_pgt - __START_KERNEL_map + _PAGE_TABLE
  337. /* 8MB reserved for vsyscalls + a 2MB hole = 4 + 1 entries */
  338. .fill 5,8,0
  339. NEXT_PAGE(level1_fixmap_pgt)
  340. .fill 512,8,0
  341. NEXT_PAGE(level2_ident_pgt)
  342. /* Since I easily can, map the first 1G.
  343. * Don't set NX because code runs from these pages.
  344. */
  345. PMDS(0, __PAGE_KERNEL_IDENT_LARGE_EXEC, PTRS_PER_PMD)
  346. NEXT_PAGE(level2_kernel_pgt)
  347. /*
  348. * 512 MB kernel mapping. We spend a full page on this pagetable
  349. * anyway.
  350. *
  351. * The kernel code+data+bss must not be bigger than that.
  352. *
  353. * (NOTE: at +512MB starts the module area, see MODULES_VADDR.
  354. * If you want to increase this then increase MODULES_VADDR
  355. * too.)
  356. */
  357. PMDS(0, __PAGE_KERNEL_LARGE_EXEC,
  358. KERNEL_IMAGE_SIZE/PMD_SIZE)
  359. NEXT_PAGE(level2_spare_pgt)
  360. .fill 512, 8, 0
  361. #undef PMDS
  362. #undef NEXT_PAGE
  363. .data
  364. .align 16
  365. .globl early_gdt_descr
  366. early_gdt_descr:
  367. .word GDT_ENTRIES*8-1
  368. #ifdef CONFIG_SMP
  369. early_gdt_descr_base:
  370. .quad 0x0000000000000000
  371. #else
  372. .quad per_cpu__gdt_page
  373. #endif
  374. ENTRY(phys_base)
  375. /* This must match the first entry in level2_kernel_pgt */
  376. .quad 0x0000000000000000
  377. #include "../../x86/xen/xen-head.S"
  378. .section .bss, "aw", @nobits
  379. .align L1_CACHE_BYTES
  380. ENTRY(idt_table)
  381. .skip 256 * 16
  382. .section .bss.page_aligned, "aw", @nobits
  383. .align PAGE_SIZE
  384. ENTRY(empty_zero_page)
  385. .skip PAGE_SIZE