head_64.S 13 KB

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