head_64.S 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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/segment.h>
  14. #include <asm/pgtable.h>
  15. #include <asm/page.h>
  16. #include <asm/msr.h>
  17. #include <asm/cache.h>
  18. #include <asm/processor-flags.h>
  19. #include <asm/percpu.h>
  20. #include <asm/nops.h>
  21. #ifdef CONFIG_PARAVIRT
  22. #include <asm/asm-offsets.h>
  23. #include <asm/paravirt.h>
  24. #define GET_CR2_INTO(reg) GET_CR2_INTO_RAX ; movq %rax, reg
  25. #else
  26. #define GET_CR2_INTO(reg) movq %cr2, reg
  27. #define INTERRUPT_RETURN iretq
  28. #endif
  29. /* we are not able to switch in one step to the final KERNEL ADDRESS SPACE
  30. * because we need identity-mapped pages.
  31. *
  32. */
  33. #define pud_index(x) (((x) >> PUD_SHIFT) & (PTRS_PER_PUD-1))
  34. L4_PAGE_OFFSET = pgd_index(__PAGE_OFFSET)
  35. L3_PAGE_OFFSET = pud_index(__PAGE_OFFSET)
  36. L4_START_KERNEL = pgd_index(__START_KERNEL_map)
  37. L3_START_KERNEL = pud_index(__START_KERNEL_map)
  38. .text
  39. __HEAD
  40. .code64
  41. .globl startup_64
  42. startup_64:
  43. /*
  44. * At this point the CPU runs in 64bit mode CS.L = 1 CS.D = 0,
  45. * and someone has loaded an identity mapped page table
  46. * for us. These identity mapped page tables map all of the
  47. * kernel pages and possibly all of memory.
  48. *
  49. * %rsi holds a physical pointer to real_mode_data.
  50. *
  51. * We come here either directly from a 64bit bootloader, or from
  52. * arch/x86_64/boot/compressed/head.S.
  53. *
  54. * We only come here initially at boot nothing else comes here.
  55. *
  56. * Since we may be loaded at an address different from what we were
  57. * compiled to run at we first fixup the physical addresses in our page
  58. * tables and then reload them.
  59. */
  60. /*
  61. * Compute the delta between the address I am compiled to run at and the
  62. * address I am actually running at.
  63. */
  64. leaq _text(%rip), %rbp
  65. subq $_text - __START_KERNEL_map, %rbp
  66. /* Is the address not 2M aligned? */
  67. movq %rbp, %rax
  68. andl $~PMD_PAGE_MASK, %eax
  69. testl %eax, %eax
  70. jnz bad_address
  71. /*
  72. * Is the address too large?
  73. */
  74. leaq _text(%rip), %rax
  75. shrq $MAX_PHYSMEM_BITS, %rax
  76. jnz bad_address
  77. /*
  78. * Fixup the physical addresses in the page table
  79. */
  80. addq %rbp, early_level4_pgt + (L4_START_KERNEL*8)(%rip)
  81. addq %rbp, level3_kernel_pgt + (510*8)(%rip)
  82. addq %rbp, level3_kernel_pgt + (511*8)(%rip)
  83. addq %rbp, level2_fixmap_pgt + (506*8)(%rip)
  84. /*
  85. * Set up the identity mapping for the switchover. These
  86. * entries should *NOT* have the global bit set! This also
  87. * creates a bunch of nonsense entries but that is fine --
  88. * it avoids problems around wraparound.
  89. */
  90. leaq _text(%rip), %rdi
  91. leaq early_level4_pgt(%rip), %rbx
  92. movq %rdi, %rax
  93. shrq $PGDIR_SHIFT, %rax
  94. leaq (4096 + _KERNPG_TABLE)(%rbx), %rdx
  95. movq %rdx, 0(%rbx,%rax,8)
  96. movq %rdx, 8(%rbx,%rax,8)
  97. addq $4096, %rdx
  98. movq %rdi, %rax
  99. shrq $PUD_SHIFT, %rax
  100. andl $(PTRS_PER_PUD-1), %eax
  101. movq %rdx, (4096+0)(%rbx,%rax,8)
  102. movq %rdx, (4096+8)(%rbx,%rax,8)
  103. addq $8192, %rbx
  104. movq %rdi, %rax
  105. shrq $PMD_SHIFT, %rdi
  106. addq $(__PAGE_KERNEL_LARGE_EXEC & ~_PAGE_GLOBAL), %rax
  107. leaq (_end - 1)(%rip), %rcx
  108. shrq $PMD_SHIFT, %rcx
  109. subq %rdi, %rcx
  110. incl %ecx
  111. 1:
  112. andq $(PTRS_PER_PMD - 1), %rdi
  113. movq %rax, (%rbx,%rdi,8)
  114. incq %rdi
  115. addq $PMD_SIZE, %rax
  116. decl %ecx
  117. jnz 1b
  118. /*
  119. * Fixup the kernel text+data virtual addresses. Note that
  120. * we might write invalid pmds, when the kernel is relocated
  121. * cleanup_highmap() fixes this up along with the mappings
  122. * beyond _end.
  123. */
  124. leaq level2_kernel_pgt(%rip), %rdi
  125. leaq 4096(%rdi), %r8
  126. /* See if it is a valid page table entry */
  127. 1: testq $1, 0(%rdi)
  128. jz 2f
  129. addq %rbp, 0(%rdi)
  130. /* Go to the next page */
  131. 2: addq $8, %rdi
  132. cmp %r8, %rdi
  133. jne 1b
  134. /* Fixup phys_base */
  135. addq %rbp, phys_base(%rip)
  136. movq $(early_level4_pgt - __START_KERNEL_map), %rax
  137. jmp 1f
  138. ENTRY(secondary_startup_64)
  139. /*
  140. * At this point the CPU runs in 64bit mode CS.L = 1 CS.D = 0,
  141. * and someone has loaded a mapped page table.
  142. *
  143. * %rsi holds a physical pointer to real_mode_data.
  144. *
  145. * We come here either from startup_64 (using physical addresses)
  146. * or from trampoline.S (using virtual addresses).
  147. *
  148. * Using virtual addresses from trampoline.S removes the need
  149. * to have any identity mapped pages in the kernel page table
  150. * after the boot processor executes this code.
  151. */
  152. movq $(init_level4_pgt - __START_KERNEL_map), %rax
  153. 1:
  154. /* Enable PAE mode and PGE */
  155. movl $(X86_CR4_PAE | X86_CR4_PGE), %ecx
  156. movq %rcx, %cr4
  157. /* Setup early boot stage 4 level pagetables. */
  158. addq phys_base(%rip), %rax
  159. movq %rax, %cr3
  160. /* Ensure I am executing from virtual addresses */
  161. movq $1f, %rax
  162. jmp *%rax
  163. 1:
  164. /* Check if nx is implemented */
  165. movl $0x80000001, %eax
  166. cpuid
  167. movl %edx,%edi
  168. /* Setup EFER (Extended Feature Enable Register) */
  169. movl $MSR_EFER, %ecx
  170. rdmsr
  171. btsl $_EFER_SCE, %eax /* Enable System Call */
  172. btl $20,%edi /* No Execute supported? */
  173. jnc 1f
  174. btsl $_EFER_NX, %eax
  175. 1: wrmsr /* Make changes effective */
  176. /* Setup cr0 */
  177. #define CR0_STATE (X86_CR0_PE | X86_CR0_MP | X86_CR0_ET | \
  178. X86_CR0_NE | X86_CR0_WP | X86_CR0_AM | \
  179. X86_CR0_PG)
  180. movl $CR0_STATE, %eax
  181. /* Make changes effective */
  182. movq %rax, %cr0
  183. /* Setup a boot time stack */
  184. movq stack_start(%rip), %rsp
  185. /* zero EFLAGS after setting rsp */
  186. pushq $0
  187. popfq
  188. /*
  189. * We must switch to a new descriptor in kernel space for the GDT
  190. * because soon the kernel won't have access anymore to the userspace
  191. * addresses where we're currently running on. We have to do that here
  192. * because in 32bit we couldn't load a 64bit linear address.
  193. */
  194. lgdt early_gdt_descr(%rip)
  195. /* set up data segments */
  196. xorl %eax,%eax
  197. movl %eax,%ds
  198. movl %eax,%ss
  199. movl %eax,%es
  200. /*
  201. * We don't really need to load %fs or %gs, but load them anyway
  202. * to kill any stale realmode selectors. This allows execution
  203. * under VT hardware.
  204. */
  205. movl %eax,%fs
  206. movl %eax,%gs
  207. /* Set up %gs.
  208. *
  209. * The base of %gs always points to the bottom of the irqstack
  210. * union. If the stack protector canary is enabled, it is
  211. * located at %gs:40. Note that, on SMP, the boot cpu uses
  212. * init data section till per cpu areas are set up.
  213. */
  214. movl $MSR_GS_BASE,%ecx
  215. movl initial_gs(%rip),%eax
  216. movl initial_gs+4(%rip),%edx
  217. wrmsr
  218. /* rsi is pointer to real mode structure with interesting info.
  219. pass it to C */
  220. movq %rsi, %rdi
  221. /* Finally jump to run C code and to be on real kernel address
  222. * Since we are running on identity-mapped space we have to jump
  223. * to the full 64bit address, this is only possible as indirect
  224. * jump. In addition we need to ensure %cs is set so we make this
  225. * a far return.
  226. *
  227. * Note: do not change to far jump indirect with 64bit offset.
  228. *
  229. * AMD does not support far jump indirect with 64bit offset.
  230. * AMD64 Architecture Programmer's Manual, Volume 3: states only
  231. * JMP FAR mem16:16 FF /5 Far jump indirect,
  232. * with the target specified by a far pointer in memory.
  233. * JMP FAR mem16:32 FF /5 Far jump indirect,
  234. * with the target specified by a far pointer in memory.
  235. *
  236. * Intel64 does support 64bit offset.
  237. * Software Developer Manual Vol 2: states:
  238. * FF /5 JMP m16:16 Jump far, absolute indirect,
  239. * address given in m16:16
  240. * FF /5 JMP m16:32 Jump far, absolute indirect,
  241. * address given in m16:32.
  242. * REX.W + FF /5 JMP m16:64 Jump far, absolute indirect,
  243. * address given in m16:64.
  244. */
  245. movq initial_code(%rip),%rax
  246. pushq $0 # fake return address to stop unwinder
  247. pushq $__KERNEL_CS # set correct cs
  248. pushq %rax # target address in negative space
  249. lretq
  250. #ifdef CONFIG_HOTPLUG_CPU
  251. /*
  252. * Boot CPU0 entry point. It's called from play_dead(). Everything has been set
  253. * up already except stack. We just set up stack here. Then call
  254. * start_secondary().
  255. */
  256. ENTRY(start_cpu0)
  257. movq stack_start(%rip),%rsp
  258. movq initial_code(%rip),%rax
  259. pushq $0 # fake return address to stop unwinder
  260. pushq $__KERNEL_CS # set correct cs
  261. pushq %rax # target address in negative space
  262. lretq
  263. ENDPROC(start_cpu0)
  264. #endif
  265. /* SMP bootup changes these two */
  266. __REFDATA
  267. .balign 8
  268. GLOBAL(initial_code)
  269. .quad x86_64_start_kernel
  270. GLOBAL(initial_gs)
  271. .quad INIT_PER_CPU_VAR(irq_stack_union)
  272. GLOBAL(stack_start)
  273. .quad init_thread_union+THREAD_SIZE-8
  274. .word 0
  275. __FINITDATA
  276. bad_address:
  277. jmp bad_address
  278. __INIT
  279. .globl early_idt_handlers
  280. early_idt_handlers:
  281. # 104(%rsp) %rflags
  282. # 96(%rsp) %cs
  283. # 88(%rsp) %rip
  284. # 80(%rsp) error code
  285. i = 0
  286. .rept NUM_EXCEPTION_VECTORS
  287. .if (EXCEPTION_ERRCODE_MASK >> i) & 1
  288. ASM_NOP2
  289. .else
  290. pushq $0 # Dummy error code, to make stack frame uniform
  291. .endif
  292. pushq $i # 72(%rsp) Vector number
  293. jmp early_idt_handler
  294. i = i + 1
  295. .endr
  296. /* This is global to keep gas from relaxing the jumps */
  297. ENTRY(early_idt_handler)
  298. cld
  299. cmpl $2,early_recursion_flag(%rip)
  300. jz 1f
  301. incl early_recursion_flag(%rip)
  302. pushq %rax # 64(%rsp)
  303. pushq %rcx # 56(%rsp)
  304. pushq %rdx # 48(%rsp)
  305. pushq %rsi # 40(%rsp)
  306. pushq %rdi # 32(%rsp)
  307. pushq %r8 # 24(%rsp)
  308. pushq %r9 # 16(%rsp)
  309. pushq %r10 # 8(%rsp)
  310. pushq %r11 # 0(%rsp)
  311. cmpl $__KERNEL_CS,96(%rsp)
  312. jne 11f
  313. cmpl $14,72(%rsp) # Page fault?
  314. jnz 10f
  315. GET_CR2_INTO(%rdi) # can clobber any volatile register if pv
  316. call early_make_pgtable
  317. andl %eax,%eax
  318. jz 20f # All good
  319. 10:
  320. leaq 88(%rsp),%rdi # Pointer to %rip
  321. call early_fixup_exception
  322. andl %eax,%eax
  323. jnz 20f # Found an exception entry
  324. 11:
  325. #ifdef CONFIG_EARLY_PRINTK
  326. GET_CR2_INTO(%r9) # can clobber any volatile register if pv
  327. movl 80(%rsp),%r8d # error code
  328. movl 72(%rsp),%esi # vector number
  329. movl 96(%rsp),%edx # %cs
  330. movq 88(%rsp),%rcx # %rip
  331. xorl %eax,%eax
  332. leaq early_idt_msg(%rip),%rdi
  333. call early_printk
  334. cmpl $2,early_recursion_flag(%rip)
  335. jz 1f
  336. call dump_stack
  337. #ifdef CONFIG_KALLSYMS
  338. leaq early_idt_ripmsg(%rip),%rdi
  339. movq 40(%rsp),%rsi # %rip again
  340. call __print_symbol
  341. #endif
  342. #endif /* EARLY_PRINTK */
  343. 1: hlt
  344. jmp 1b
  345. 20: # Exception table entry found or page table generated
  346. popq %r11
  347. popq %r10
  348. popq %r9
  349. popq %r8
  350. popq %rdi
  351. popq %rsi
  352. popq %rdx
  353. popq %rcx
  354. popq %rax
  355. addq $16,%rsp # drop vector number and error code
  356. decl early_recursion_flag(%rip)
  357. INTERRUPT_RETURN
  358. ENDPROC(early_idt_handler)
  359. __INITDATA
  360. .balign 4
  361. early_recursion_flag:
  362. .long 0
  363. #ifdef CONFIG_EARLY_PRINTK
  364. early_idt_msg:
  365. .asciz "PANIC: early exception %02lx rip %lx:%lx error %lx cr2 %lx\n"
  366. early_idt_ripmsg:
  367. .asciz "RIP %s\n"
  368. #endif /* CONFIG_EARLY_PRINTK */
  369. #define NEXT_PAGE(name) \
  370. .balign PAGE_SIZE; \
  371. GLOBAL(name)
  372. /* Automate the creation of 1 to 1 mapping pmd entries */
  373. #define PMDS(START, PERM, COUNT) \
  374. i = 0 ; \
  375. .rept (COUNT) ; \
  376. .quad (START) + (i << PMD_SHIFT) + (PERM) ; \
  377. i = i + 1 ; \
  378. .endr
  379. __INITDATA
  380. NEXT_PAGE(early_level4_pgt)
  381. .fill 511,8,0
  382. .quad level3_kernel_pgt - __START_KERNEL_map + _PAGE_TABLE
  383. NEXT_PAGE(early_dynamic_pgts)
  384. .fill 512*EARLY_DYNAMIC_PAGE_TABLES,8,0
  385. .data
  386. #ifndef CONFIG_XEN
  387. NEXT_PAGE(init_level4_pgt)
  388. .fill 512,8,0
  389. #else
  390. NEXT_PAGE(init_level4_pgt)
  391. .quad level3_ident_pgt - __START_KERNEL_map + _KERNPG_TABLE
  392. .org init_level4_pgt + L4_PAGE_OFFSET*8, 0
  393. .quad level3_ident_pgt - __START_KERNEL_map + _KERNPG_TABLE
  394. .org init_level4_pgt + L4_START_KERNEL*8, 0
  395. /* (2^48-(2*1024*1024*1024))/(2^39) = 511 */
  396. .quad level3_kernel_pgt - __START_KERNEL_map + _PAGE_TABLE
  397. NEXT_PAGE(level3_ident_pgt)
  398. .quad level2_ident_pgt - __START_KERNEL_map + _KERNPG_TABLE
  399. .fill 511, 8, 0
  400. NEXT_PAGE(level2_ident_pgt)
  401. /* Since I easily can, map the first 1G.
  402. * Don't set NX because code runs from these pages.
  403. */
  404. PMDS(0, __PAGE_KERNEL_IDENT_LARGE_EXEC, PTRS_PER_PMD)
  405. #endif
  406. NEXT_PAGE(level3_kernel_pgt)
  407. .fill L3_START_KERNEL,8,0
  408. /* (2^48-(2*1024*1024*1024)-((2^39)*511))/(2^30) = 510 */
  409. .quad level2_kernel_pgt - __START_KERNEL_map + _KERNPG_TABLE
  410. .quad level2_fixmap_pgt - __START_KERNEL_map + _PAGE_TABLE
  411. NEXT_PAGE(level2_kernel_pgt)
  412. /*
  413. * 512 MB kernel mapping. We spend a full page on this pagetable
  414. * anyway.
  415. *
  416. * The kernel code+data+bss must not be bigger than that.
  417. *
  418. * (NOTE: at +512MB starts the module area, see MODULES_VADDR.
  419. * If you want to increase this then increase MODULES_VADDR
  420. * too.)
  421. */
  422. PMDS(0, __PAGE_KERNEL_LARGE_EXEC,
  423. KERNEL_IMAGE_SIZE/PMD_SIZE)
  424. NEXT_PAGE(level2_fixmap_pgt)
  425. .fill 506,8,0
  426. .quad level1_fixmap_pgt - __START_KERNEL_map + _PAGE_TABLE
  427. /* 8MB reserved for vsyscalls + a 2MB hole = 4 + 1 entries */
  428. .fill 5,8,0
  429. NEXT_PAGE(level1_fixmap_pgt)
  430. .fill 512,8,0
  431. #undef PMDS
  432. .data
  433. .align 16
  434. .globl early_gdt_descr
  435. early_gdt_descr:
  436. .word GDT_ENTRIES*8-1
  437. early_gdt_descr_base:
  438. .quad INIT_PER_CPU_VAR(gdt_page)
  439. ENTRY(phys_base)
  440. /* This must match the first entry in level2_kernel_pgt */
  441. .quad 0x0000000000000000
  442. #include "../../x86/xen/xen-head.S"
  443. .section .bss, "aw", @nobits
  444. .align L1_CACHE_BYTES
  445. ENTRY(idt_table)
  446. .skip IDT_ENTRIES * 16
  447. .align L1_CACHE_BYTES
  448. ENTRY(nmi_idt_table)
  449. .skip IDT_ENTRIES * 16
  450. __PAGE_ALIGNED_BSS
  451. NEXT_PAGE(empty_zero_page)
  452. .skip PAGE_SIZE