head_64.S 13 KB

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