head_64.S 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  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. #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/boot/compressed/head_64.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(%rbx,%rax,8)
  102. incl %eax
  103. andl $(PTRS_PER_PUD-1), %eax
  104. movq %rdx, 4096(%rbx,%rax,8)
  105. addq $8192, %rbx
  106. movq %rdi, %rax
  107. shrq $PMD_SHIFT, %rdi
  108. addq $(__PAGE_KERNEL_LARGE_EXEC & ~_PAGE_GLOBAL), %rax
  109. leaq (_end - 1)(%rip), %rcx
  110. shrq $PMD_SHIFT, %rcx
  111. subq %rdi, %rcx
  112. incl %ecx
  113. 1:
  114. andq $(PTRS_PER_PMD - 1), %rdi
  115. movq %rax, (%rbx,%rdi,8)
  116. incq %rdi
  117. addq $PMD_SIZE, %rax
  118. decl %ecx
  119. jnz 1b
  120. /*
  121. * Fixup the kernel text+data virtual addresses. Note that
  122. * we might write invalid pmds, when the kernel is relocated
  123. * cleanup_highmap() fixes this up along with the mappings
  124. * beyond _end.
  125. */
  126. leaq level2_kernel_pgt(%rip), %rdi
  127. leaq 4096(%rdi), %r8
  128. /* See if it is a valid page table entry */
  129. 1: testb $1, 0(%rdi)
  130. jz 2f
  131. addq %rbp, 0(%rdi)
  132. /* Go to the next page */
  133. 2: addq $8, %rdi
  134. cmp %r8, %rdi
  135. jne 1b
  136. /* Fixup phys_base */
  137. addq %rbp, phys_base(%rip)
  138. movq $(early_level4_pgt - __START_KERNEL_map), %rax
  139. jmp 1f
  140. ENTRY(secondary_startup_64)
  141. /*
  142. * At this point the CPU runs in 64bit mode CS.L = 1 CS.D = 0,
  143. * and someone has loaded a mapped page table.
  144. *
  145. * %rsi holds a physical pointer to real_mode_data.
  146. *
  147. * We come here either from startup_64 (using physical addresses)
  148. * or from trampoline.S (using virtual addresses).
  149. *
  150. * Using virtual addresses from trampoline.S removes the need
  151. * to have any identity mapped pages in the kernel page table
  152. * after the boot processor executes this code.
  153. */
  154. movq $(init_level4_pgt - __START_KERNEL_map), %rax
  155. 1:
  156. /* Enable PAE mode and PGE */
  157. movl $(X86_CR4_PAE | X86_CR4_PGE), %ecx
  158. movq %rcx, %cr4
  159. /* Setup early boot stage 4 level pagetables. */
  160. addq phys_base(%rip), %rax
  161. movq %rax, %cr3
  162. /* Ensure I am executing from virtual addresses */
  163. movq $1f, %rax
  164. jmp *%rax
  165. 1:
  166. /* Check if nx is implemented */
  167. movl $0x80000001, %eax
  168. cpuid
  169. movl %edx,%edi
  170. /* Setup EFER (Extended Feature Enable Register) */
  171. movl $MSR_EFER, %ecx
  172. rdmsr
  173. btsl $_EFER_SCE, %eax /* Enable System Call */
  174. btl $20,%edi /* No Execute supported? */
  175. jnc 1f
  176. btsl $_EFER_NX, %eax
  177. btsq $_PAGE_BIT_NX,early_pmd_flags(%rip)
  178. 1: wrmsr /* Make changes effective */
  179. /* Setup cr0 */
  180. #define CR0_STATE (X86_CR0_PE | X86_CR0_MP | X86_CR0_ET | \
  181. X86_CR0_NE | X86_CR0_WP | X86_CR0_AM | \
  182. X86_CR0_PG)
  183. movl $CR0_STATE, %eax
  184. /* Make changes effective */
  185. movq %rax, %cr0
  186. /* Setup a boot time stack */
  187. movq stack_start(%rip), %rsp
  188. /* zero EFLAGS after setting rsp */
  189. pushq $0
  190. popfq
  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 */
  199. xorl %eax,%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. * The base of %gs always points to the bottom of the irqstack
  213. * union. If the stack protector canary is enabled, it is
  214. * located at %gs:40. Note that, on SMP, the boot cpu uses
  215. * init data section till per cpu areas are set up.
  216. */
  217. movl $MSR_GS_BASE,%ecx
  218. movl initial_gs(%rip),%eax
  219. movl initial_gs+4(%rip),%edx
  220. wrmsr
  221. /* rsi is pointer to real mode structure with interesting info.
  222. pass it to C */
  223. movq %rsi, %rdi
  224. /* Finally jump to run C code and to be on real kernel address
  225. * Since we are running on identity-mapped space we have to jump
  226. * to the full 64bit address, this is only possible as indirect
  227. * jump. In addition we need to ensure %cs is set so we make this
  228. * a far return.
  229. *
  230. * Note: do not change to far jump indirect with 64bit offset.
  231. *
  232. * AMD does not support far jump indirect with 64bit offset.
  233. * AMD64 Architecture Programmer's Manual, Volume 3: states only
  234. * JMP FAR mem16:16 FF /5 Far jump indirect,
  235. * with the target specified by a far pointer in memory.
  236. * JMP FAR mem16:32 FF /5 Far jump indirect,
  237. * with the target specified by a far pointer in memory.
  238. *
  239. * Intel64 does support 64bit offset.
  240. * Software Developer Manual Vol 2: states:
  241. * FF /5 JMP m16:16 Jump far, absolute indirect,
  242. * address given in m16:16
  243. * FF /5 JMP m16:32 Jump far, absolute indirect,
  244. * address given in m16:32.
  245. * REX.W + FF /5 JMP m16:64 Jump far, absolute indirect,
  246. * address given in m16:64.
  247. */
  248. movq initial_code(%rip),%rax
  249. pushq $0 # fake return address to stop unwinder
  250. pushq $__KERNEL_CS # set correct cs
  251. pushq %rax # target address in negative space
  252. lretq
  253. #ifdef CONFIG_HOTPLUG_CPU
  254. /*
  255. * Boot CPU0 entry point. It's called from play_dead(). Everything has been set
  256. * up already except stack. We just set up stack here. Then call
  257. * start_secondary().
  258. */
  259. ENTRY(start_cpu0)
  260. movq stack_start(%rip),%rsp
  261. movq initial_code(%rip),%rax
  262. pushq $0 # fake return address to stop unwinder
  263. pushq $__KERNEL_CS # set correct cs
  264. pushq %rax # target address in negative space
  265. lretq
  266. ENDPROC(start_cpu0)
  267. #endif
  268. /* SMP bootup changes these two */
  269. __REFDATA
  270. .balign 8
  271. GLOBAL(initial_code)
  272. .quad x86_64_start_kernel
  273. GLOBAL(initial_gs)
  274. .quad INIT_PER_CPU_VAR(irq_stack_union)
  275. GLOBAL(stack_start)
  276. .quad init_thread_union+THREAD_SIZE-8
  277. .word 0
  278. __FINITDATA
  279. bad_address:
  280. jmp bad_address
  281. __INIT
  282. ENTRY(early_idt_handler_array)
  283. # 104(%rsp) %rflags
  284. # 96(%rsp) %cs
  285. # 88(%rsp) %rip
  286. # 80(%rsp) error code
  287. i = 0
  288. .rept NUM_EXCEPTION_VECTORS
  289. .ifeq (EXCEPTION_ERRCODE_MASK >> i) & 1
  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_common
  294. i = i + 1
  295. .fill early_idt_handler_array + i*EARLY_IDT_HANDLER_SIZE - ., 1, 0xcc
  296. .endr
  297. ENDPROC(early_idt_handler_array)
  298. early_idt_handler_common:
  299. /*
  300. * The stack is the hardware frame, an error code or zero, and the
  301. * vector number.
  302. */
  303. cld
  304. cmpl $2,(%rsp) # X86_TRAP_NMI
  305. je is_nmi # Ignore NMI
  306. cmpl $2,early_recursion_flag(%rip)
  307. jz 1f
  308. incl early_recursion_flag(%rip)
  309. pushq %rax # 64(%rsp)
  310. pushq %rcx # 56(%rsp)
  311. pushq %rdx # 48(%rsp)
  312. pushq %rsi # 40(%rsp)
  313. pushq %rdi # 32(%rsp)
  314. pushq %r8 # 24(%rsp)
  315. pushq %r9 # 16(%rsp)
  316. pushq %r10 # 8(%rsp)
  317. pushq %r11 # 0(%rsp)
  318. cmpl $__KERNEL_CS,96(%rsp)
  319. jne 11f
  320. cmpl $14,72(%rsp) # Page fault?
  321. jnz 10f
  322. GET_CR2_INTO(%rdi) # can clobber any volatile register if pv
  323. call early_make_pgtable
  324. andl %eax,%eax
  325. jz 20f # All good
  326. 10:
  327. leaq 88(%rsp),%rdi # Pointer to %rip
  328. call early_fixup_exception
  329. andl %eax,%eax
  330. jnz 20f # Found an exception entry
  331. 11:
  332. #ifdef CONFIG_EARLY_PRINTK
  333. GET_CR2_INTO(%r9) # can clobber any volatile register if pv
  334. movl 80(%rsp),%r8d # error code
  335. movl 72(%rsp),%esi # vector number
  336. movl 96(%rsp),%edx # %cs
  337. movq 88(%rsp),%rcx # %rip
  338. xorl %eax,%eax
  339. leaq early_idt_msg(%rip),%rdi
  340. call early_printk
  341. cmpl $2,early_recursion_flag(%rip)
  342. jz 1f
  343. call dump_stack
  344. #ifdef CONFIG_KALLSYMS
  345. leaq early_idt_ripmsg(%rip),%rdi
  346. movq 40(%rsp),%rsi # %rip again
  347. call __print_symbol
  348. #endif
  349. #endif /* EARLY_PRINTK */
  350. 1: hlt
  351. jmp 1b
  352. 20: # Exception table entry found or page table generated
  353. popq %r11
  354. popq %r10
  355. popq %r9
  356. popq %r8
  357. popq %rdi
  358. popq %rsi
  359. popq %rdx
  360. popq %rcx
  361. popq %rax
  362. decl early_recursion_flag(%rip)
  363. is_nmi:
  364. addq $16,%rsp # drop vector number and error code
  365. INTERRUPT_RETURN
  366. ENDPROC(early_idt_handler_common)
  367. __INITDATA
  368. .balign 4
  369. early_recursion_flag:
  370. .long 0
  371. #ifdef CONFIG_EARLY_PRINTK
  372. early_idt_msg:
  373. .asciz "PANIC: early exception %02lx rip %lx:%lx error %lx cr2 %lx\n"
  374. early_idt_ripmsg:
  375. .asciz "RIP %s\n"
  376. #endif /* CONFIG_EARLY_PRINTK */
  377. #define NEXT_PAGE(name) \
  378. .balign PAGE_SIZE; \
  379. GLOBAL(name)
  380. /* Automate the creation of 1 to 1 mapping pmd entries */
  381. #define PMDS(START, PERM, COUNT) \
  382. i = 0 ; \
  383. .rept (COUNT) ; \
  384. .quad (START) + (i << PMD_SHIFT) + (PERM) ; \
  385. i = i + 1 ; \
  386. .endr
  387. __INITDATA
  388. NEXT_PAGE(early_level4_pgt)
  389. .fill 511,8,0
  390. .quad level3_kernel_pgt - __START_KERNEL_map + _PAGE_TABLE
  391. NEXT_PAGE(early_dynamic_pgts)
  392. .fill 512*EARLY_DYNAMIC_PAGE_TABLES,8,0
  393. .data
  394. #ifndef CONFIG_XEN
  395. NEXT_PAGE(init_level4_pgt)
  396. .fill 512,8,0
  397. #else
  398. NEXT_PAGE(init_level4_pgt)
  399. .quad level3_ident_pgt - __START_KERNEL_map + _KERNPG_TABLE
  400. .org init_level4_pgt + L4_PAGE_OFFSET*8, 0
  401. .quad level3_ident_pgt - __START_KERNEL_map + _KERNPG_TABLE
  402. .org init_level4_pgt + L4_START_KERNEL*8, 0
  403. /* (2^48-(2*1024*1024*1024))/(2^39) = 511 */
  404. .quad level3_kernel_pgt - __START_KERNEL_map + _PAGE_TABLE
  405. NEXT_PAGE(level3_ident_pgt)
  406. .quad level2_ident_pgt - __START_KERNEL_map + _KERNPG_TABLE
  407. .fill 511, 8, 0
  408. NEXT_PAGE(level2_ident_pgt)
  409. /* Since I easily can, map the first 1G.
  410. * Don't set NX because code runs from these pages.
  411. */
  412. PMDS(0, __PAGE_KERNEL_IDENT_LARGE_EXEC, PTRS_PER_PMD)
  413. #endif
  414. NEXT_PAGE(level3_kernel_pgt)
  415. .fill L3_START_KERNEL,8,0
  416. /* (2^48-(2*1024*1024*1024)-((2^39)*511))/(2^30) = 510 */
  417. .quad level2_kernel_pgt - __START_KERNEL_map + _KERNPG_TABLE
  418. .quad level2_fixmap_pgt - __START_KERNEL_map + _PAGE_TABLE
  419. NEXT_PAGE(level2_kernel_pgt)
  420. /*
  421. * 512 MB kernel mapping. We spend a full page on this pagetable
  422. * anyway.
  423. *
  424. * The kernel code+data+bss must not be bigger than that.
  425. *
  426. * (NOTE: at +512MB starts the module area, see MODULES_VADDR.
  427. * If you want to increase this then increase MODULES_VADDR
  428. * too.)
  429. */
  430. PMDS(0, __PAGE_KERNEL_LARGE_EXEC,
  431. KERNEL_IMAGE_SIZE/PMD_SIZE)
  432. NEXT_PAGE(level2_fixmap_pgt)
  433. .fill 506,8,0
  434. .quad level1_fixmap_pgt - __START_KERNEL_map + _PAGE_TABLE
  435. /* 8MB reserved for vsyscalls + a 2MB hole = 4 + 1 entries */
  436. .fill 5,8,0
  437. NEXT_PAGE(level1_fixmap_pgt)
  438. .fill 512,8,0
  439. #undef PMDS
  440. .data
  441. .align 16
  442. .globl early_gdt_descr
  443. early_gdt_descr:
  444. .word GDT_ENTRIES*8-1
  445. early_gdt_descr_base:
  446. .quad INIT_PER_CPU_VAR(gdt_page)
  447. ENTRY(phys_base)
  448. /* This must match the first entry in level2_kernel_pgt */
  449. .quad 0x0000000000000000
  450. #ifdef CONFIG_KASAN
  451. #define FILL(VAL, COUNT) \
  452. .rept (COUNT) ; \
  453. .quad (VAL) ; \
  454. .endr
  455. NEXT_PAGE(kasan_zero_pte)
  456. FILL(kasan_zero_page - __START_KERNEL_map + _KERNPG_TABLE, 512)
  457. NEXT_PAGE(kasan_zero_pmd)
  458. FILL(kasan_zero_pte - __START_KERNEL_map + _KERNPG_TABLE, 512)
  459. NEXT_PAGE(kasan_zero_pud)
  460. FILL(kasan_zero_pmd - __START_KERNEL_map + _KERNPG_TABLE, 512)
  461. #undef FILL
  462. #endif
  463. #include "../../x86/xen/xen-head.S"
  464. __PAGE_ALIGNED_BSS
  465. NEXT_PAGE(empty_zero_page)
  466. .skip PAGE_SIZE
  467. #ifdef CONFIG_KASAN
  468. /*
  469. * This page used as early shadow. We don't use empty_zero_page
  470. * at early stages, stack instrumentation could write some garbage
  471. * to this page.
  472. * Latter we reuse it as zero shadow for large ranges of memory
  473. * that allowed to access, but not instrumented by kasan
  474. * (vmalloc/vmemmap ...).
  475. */
  476. NEXT_PAGE(kasan_zero_page)
  477. .skip PAGE_SIZE
  478. #endif