head_64.S 14 KB

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