head_64.S 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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. /*
  62. * Setup stack for verify_cpu(). "-8" because initial_stack is defined
  63. * this way, see below. Our best guess is a NULL ptr for stack
  64. * termination heuristics and we don't want to break anything which
  65. * might depend on it (kgdb, ...).
  66. */
  67. leaq (__end_init_task - 8)(%rip), %rsp
  68. /* Sanitize CPU configuration */
  69. call verify_cpu
  70. /*
  71. * Compute the delta between the address I am compiled to run at and the
  72. * address I am actually running at.
  73. */
  74. leaq _text(%rip), %rbp
  75. subq $_text - __START_KERNEL_map, %rbp
  76. /* Is the address not 2M aligned? */
  77. testl $~PMD_PAGE_MASK, %ebp
  78. jnz bad_address
  79. /*
  80. * Is the address too large?
  81. */
  82. leaq _text(%rip), %rax
  83. shrq $MAX_PHYSMEM_BITS, %rax
  84. jnz bad_address
  85. /*
  86. * Fixup the physical addresses in the page table
  87. */
  88. addq %rbp, early_level4_pgt + (L4_START_KERNEL*8)(%rip)
  89. addq %rbp, level3_kernel_pgt + (510*8)(%rip)
  90. addq %rbp, level3_kernel_pgt + (511*8)(%rip)
  91. addq %rbp, level2_fixmap_pgt + (506*8)(%rip)
  92. /*
  93. * Set up the identity mapping for the switchover. These
  94. * entries should *NOT* have the global bit set! This also
  95. * creates a bunch of nonsense entries but that is fine --
  96. * it avoids problems around wraparound.
  97. */
  98. leaq _text(%rip), %rdi
  99. leaq early_level4_pgt(%rip), %rbx
  100. movq %rdi, %rax
  101. shrq $PGDIR_SHIFT, %rax
  102. leaq (4096 + _KERNPG_TABLE)(%rbx), %rdx
  103. movq %rdx, 0(%rbx,%rax,8)
  104. movq %rdx, 8(%rbx,%rax,8)
  105. addq $4096, %rdx
  106. movq %rdi, %rax
  107. shrq $PUD_SHIFT, %rax
  108. andl $(PTRS_PER_PUD-1), %eax
  109. movq %rdx, 4096(%rbx,%rax,8)
  110. incl %eax
  111. andl $(PTRS_PER_PUD-1), %eax
  112. movq %rdx, 4096(%rbx,%rax,8)
  113. addq $8192, %rbx
  114. movq %rdi, %rax
  115. shrq $PMD_SHIFT, %rdi
  116. addq $(__PAGE_KERNEL_LARGE_EXEC & ~_PAGE_GLOBAL), %rax
  117. leaq (_end - 1)(%rip), %rcx
  118. shrq $PMD_SHIFT, %rcx
  119. subq %rdi, %rcx
  120. incl %ecx
  121. 1:
  122. andq $(PTRS_PER_PMD - 1), %rdi
  123. movq %rax, (%rbx,%rdi,8)
  124. incq %rdi
  125. addq $PMD_SIZE, %rax
  126. decl %ecx
  127. jnz 1b
  128. /*
  129. * Fixup the kernel text+data virtual addresses. Note that
  130. * we might write invalid pmds, when the kernel is relocated
  131. * cleanup_highmap() fixes this up along with the mappings
  132. * beyond _end.
  133. */
  134. leaq level2_kernel_pgt(%rip), %rdi
  135. leaq 4096(%rdi), %r8
  136. /* See if it is a valid page table entry */
  137. 1: testb $1, 0(%rdi)
  138. jz 2f
  139. addq %rbp, 0(%rdi)
  140. /* Go to the next page */
  141. 2: addq $8, %rdi
  142. cmp %r8, %rdi
  143. jne 1b
  144. /* Fixup phys_base */
  145. addq %rbp, phys_base(%rip)
  146. movq $(early_level4_pgt - __START_KERNEL_map), %rax
  147. jmp 1f
  148. ENTRY(secondary_startup_64)
  149. /*
  150. * At this point the CPU runs in 64bit mode CS.L = 1 CS.D = 0,
  151. * and someone has loaded a mapped page table.
  152. *
  153. * %rsi holds a physical pointer to real_mode_data.
  154. *
  155. * We come here either from startup_64 (using physical addresses)
  156. * or from trampoline.S (using virtual addresses).
  157. *
  158. * Using virtual addresses from trampoline.S removes the need
  159. * to have any identity mapped pages in the kernel page table
  160. * after the boot processor executes this code.
  161. */
  162. /* Sanitize CPU configuration */
  163. call verify_cpu
  164. movq $(init_level4_pgt - __START_KERNEL_map), %rax
  165. 1:
  166. /* Enable PAE mode and PGE */
  167. movl $(X86_CR4_PAE | X86_CR4_PGE), %ecx
  168. movq %rcx, %cr4
  169. /* Setup early boot stage 4 level pagetables. */
  170. addq phys_base(%rip), %rax
  171. movq %rax, %cr3
  172. /* Ensure I am executing from virtual addresses */
  173. movq $1f, %rax
  174. jmp *%rax
  175. 1:
  176. /* Check if nx is implemented */
  177. movl $0x80000001, %eax
  178. cpuid
  179. movl %edx,%edi
  180. /* Setup EFER (Extended Feature Enable Register) */
  181. movl $MSR_EFER, %ecx
  182. rdmsr
  183. btsl $_EFER_SCE, %eax /* Enable System Call */
  184. btl $20,%edi /* No Execute supported? */
  185. jnc 1f
  186. btsl $_EFER_NX, %eax
  187. btsq $_PAGE_BIT_NX,early_pmd_flags(%rip)
  188. 1: wrmsr /* Make changes effective */
  189. /* Setup cr0 */
  190. #define CR0_STATE (X86_CR0_PE | X86_CR0_MP | X86_CR0_ET | \
  191. X86_CR0_NE | X86_CR0_WP | X86_CR0_AM | \
  192. X86_CR0_PG)
  193. movl $CR0_STATE, %eax
  194. /* Make changes effective */
  195. movq %rax, %cr0
  196. /* Setup a boot time stack */
  197. movq initial_stack(%rip), %rsp
  198. /* zero EFLAGS after setting rsp */
  199. pushq $0
  200. popfq
  201. /*
  202. * We must switch to a new descriptor in kernel space for the GDT
  203. * because soon the kernel won't have access anymore to the userspace
  204. * addresses where we're currently running on. We have to do that here
  205. * because in 32bit we couldn't load a 64bit linear address.
  206. */
  207. lgdt early_gdt_descr(%rip)
  208. /* set up data segments */
  209. xorl %eax,%eax
  210. movl %eax,%ds
  211. movl %eax,%ss
  212. movl %eax,%es
  213. /*
  214. * We don't really need to load %fs or %gs, but load them anyway
  215. * to kill any stale realmode selectors. This allows execution
  216. * under VT hardware.
  217. */
  218. movl %eax,%fs
  219. movl %eax,%gs
  220. /* Set up %gs.
  221. *
  222. * The base of %gs always points to the bottom of the irqstack
  223. * union. If the stack protector canary is enabled, it is
  224. * located at %gs:40. Note that, on SMP, the boot cpu uses
  225. * init data section till per cpu areas are set up.
  226. */
  227. movl $MSR_GS_BASE,%ecx
  228. movl initial_gs(%rip),%eax
  229. movl initial_gs+4(%rip),%edx
  230. wrmsr
  231. /* rsi is pointer to real mode structure with interesting info.
  232. pass it to C */
  233. movq %rsi, %rdi
  234. /* Finally jump to run C code and to be on real kernel address
  235. * Since we are running on identity-mapped space we have to jump
  236. * to the full 64bit address, this is only possible as indirect
  237. * jump. In addition we need to ensure %cs is set so we make this
  238. * a far return.
  239. *
  240. * Note: do not change to far jump indirect with 64bit offset.
  241. *
  242. * AMD does not support far jump indirect with 64bit offset.
  243. * AMD64 Architecture Programmer's Manual, Volume 3: states only
  244. * JMP FAR mem16:16 FF /5 Far jump indirect,
  245. * with the target specified by a far pointer in memory.
  246. * JMP FAR mem16:32 FF /5 Far jump indirect,
  247. * with the target specified by a far pointer in memory.
  248. *
  249. * Intel64 does support 64bit offset.
  250. * Software Developer Manual Vol 2: states:
  251. * FF /5 JMP m16:16 Jump far, absolute indirect,
  252. * address given in m16:16
  253. * FF /5 JMP m16:32 Jump far, absolute indirect,
  254. * address given in m16:32.
  255. * REX.W + FF /5 JMP m16:64 Jump far, absolute indirect,
  256. * address given in m16:64.
  257. */
  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(secondary_startup_64)
  264. #include "verify_cpu.S"
  265. #ifdef CONFIG_HOTPLUG_CPU
  266. /*
  267. * Boot CPU0 entry point. It's called from play_dead(). Everything has been set
  268. * up already except stack. We just set up stack here. Then call
  269. * start_secondary().
  270. */
  271. ENTRY(start_cpu0)
  272. movq initial_stack(%rip),%rsp
  273. movq initial_code(%rip),%rax
  274. pushq $0 # fake return address to stop unwinder
  275. pushq $__KERNEL_CS # set correct cs
  276. pushq %rax # target address in negative space
  277. lretq
  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. .quad init_thread_union+THREAD_SIZE-8
  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. EXPORT_SYMBOL(phys_base)
  424. #include "../../x86/xen/xen-head.S"
  425. __PAGE_ALIGNED_BSS
  426. NEXT_PAGE(empty_zero_page)
  427. .skip PAGE_SIZE
  428. EXPORT_SYMBOL(empty_zero_page)