head_64.S 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /*
  2. * linux/arch/x86_64/kernel/head.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/desc.h>
  14. #include <asm/segment.h>
  15. #include <asm/pgtable.h>
  16. #include <asm/page.h>
  17. #include <asm/msr.h>
  18. #include <asm/cache.h>
  19. #ifdef CONFIG_PARAVIRT
  20. #include <asm/asm-offsets.h>
  21. #include <asm/paravirt.h>
  22. #else
  23. #define GET_CR2_INTO_RCX movq %cr2, %rcx
  24. #endif
  25. /* we are not able to switch in one step to the final KERNEL ADRESS SPACE
  26. * because we need identity-mapped pages.
  27. *
  28. */
  29. .text
  30. .section .text.head
  31. .code64
  32. .globl startup_64
  33. startup_64:
  34. /*
  35. * At this point the CPU runs in 64bit mode CS.L = 1 CS.D = 1,
  36. * and someone has loaded an identity mapped page table
  37. * for us. These identity mapped page tables map all of the
  38. * kernel pages and possibly all of memory.
  39. *
  40. * %esi holds a physical pointer to real_mode_data.
  41. *
  42. * We come here either directly from a 64bit bootloader, or from
  43. * arch/x86_64/boot/compressed/head.S.
  44. *
  45. * We only come here initially at boot nothing else comes here.
  46. *
  47. * Since we may be loaded at an address different from what we were
  48. * compiled to run at we first fixup the physical addresses in our page
  49. * tables and then reload them.
  50. */
  51. /* Compute the delta between the address I am compiled to run at and the
  52. * address I am actually running at.
  53. */
  54. leaq _text(%rip), %rbp
  55. subq $_text - __START_KERNEL_map, %rbp
  56. /* Is the address not 2M aligned? */
  57. movq %rbp, %rax
  58. andl $~PMD_PAGE_MASK, %eax
  59. testl %eax, %eax
  60. jnz bad_address
  61. /* Is the address too large? */
  62. leaq _text(%rip), %rdx
  63. movq $PGDIR_SIZE, %rax
  64. cmpq %rax, %rdx
  65. jae bad_address
  66. /* Fixup the physical addresses in the page table
  67. */
  68. addq %rbp, init_level4_pgt + 0(%rip)
  69. addq %rbp, init_level4_pgt + (258*8)(%rip)
  70. addq %rbp, init_level4_pgt + (511*8)(%rip)
  71. addq %rbp, level3_ident_pgt + 0(%rip)
  72. addq %rbp, level3_kernel_pgt + (510*8)(%rip)
  73. addq %rbp, level3_kernel_pgt + (511*8)(%rip)
  74. addq %rbp, level2_fixmap_pgt + (506*8)(%rip)
  75. /* Add an Identity mapping if I am above 1G */
  76. leaq _text(%rip), %rdi
  77. andq $PMD_PAGE_MASK, %rdi
  78. movq %rdi, %rax
  79. shrq $PUD_SHIFT, %rax
  80. andq $(PTRS_PER_PUD - 1), %rax
  81. jz ident_complete
  82. leaq (level2_spare_pgt - __START_KERNEL_map + _KERNPG_TABLE)(%rbp), %rdx
  83. leaq level3_ident_pgt(%rip), %rbx
  84. movq %rdx, 0(%rbx, %rax, 8)
  85. movq %rdi, %rax
  86. shrq $PMD_SHIFT, %rax
  87. andq $(PTRS_PER_PMD - 1), %rax
  88. leaq __PAGE_KERNEL_LARGE_EXEC(%rdi), %rdx
  89. leaq level2_spare_pgt(%rip), %rbx
  90. movq %rdx, 0(%rbx, %rax, 8)
  91. ident_complete:
  92. /* Fixup the kernel text+data virtual addresses
  93. */
  94. leaq level2_kernel_pgt(%rip), %rdi
  95. leaq 4096(%rdi), %r8
  96. /* See if it is a valid page table entry */
  97. 1: testq $1, 0(%rdi)
  98. jz 2f
  99. addq %rbp, 0(%rdi)
  100. /* Go to the next page */
  101. 2: addq $8, %rdi
  102. cmp %r8, %rdi
  103. jne 1b
  104. /* Fixup phys_base */
  105. addq %rbp, phys_base(%rip)
  106. #ifdef CONFIG_SMP
  107. addq %rbp, trampoline_level4_pgt + 0(%rip)
  108. addq %rbp, trampoline_level4_pgt + (511*8)(%rip)
  109. #endif
  110. #ifdef CONFIG_ACPI_SLEEP
  111. addq %rbp, wakeup_level4_pgt + 0(%rip)
  112. addq %rbp, wakeup_level4_pgt + (511*8)(%rip)
  113. #endif
  114. /* Due to ENTRY(), sometimes the empty space gets filled with
  115. * zeros. Better take a jmp than relying on empty space being
  116. * filled with 0x90 (nop)
  117. */
  118. jmp secondary_startup_64
  119. ENTRY(secondary_startup_64)
  120. /*
  121. * At this point the CPU runs in 64bit mode CS.L = 1 CS.D = 1,
  122. * and someone has loaded a mapped page table.
  123. *
  124. * %esi holds a physical pointer to real_mode_data.
  125. *
  126. * We come here either from startup_64 (using physical addresses)
  127. * or from trampoline.S (using virtual addresses).
  128. *
  129. * Using virtual addresses from trampoline.S removes the need
  130. * to have any identity mapped pages in the kernel page table
  131. * after the boot processor executes this code.
  132. */
  133. /* Enable PAE mode and PGE */
  134. xorq %rax, %rax
  135. btsq $5, %rax
  136. btsq $7, %rax
  137. movq %rax, %cr4
  138. /* Setup early boot stage 4 level pagetables. */
  139. movq $(init_level4_pgt - __START_KERNEL_map), %rax
  140. addq phys_base(%rip), %rax
  141. movq %rax, %cr3
  142. /* Ensure I am executing from virtual addresses */
  143. movq $1f, %rax
  144. jmp *%rax
  145. 1:
  146. /* Check if nx is implemented */
  147. movl $0x80000001, %eax
  148. cpuid
  149. movl %edx,%edi
  150. /* Setup EFER (Extended Feature Enable Register) */
  151. movl $MSR_EFER, %ecx
  152. rdmsr
  153. btsl $_EFER_SCE, %eax /* Enable System Call */
  154. btl $20,%edi /* No Execute supported? */
  155. jnc 1f
  156. btsl $_EFER_NX, %eax
  157. 1: wrmsr /* Make changes effective */
  158. /* Setup cr0 */
  159. #define CR0_PM 1 /* protected mode */
  160. #define CR0_MP (1<<1)
  161. #define CR0_ET (1<<4)
  162. #define CR0_NE (1<<5)
  163. #define CR0_WP (1<<16)
  164. #define CR0_AM (1<<18)
  165. #define CR0_PAGING (1<<31)
  166. movl $CR0_PM|CR0_MP|CR0_ET|CR0_NE|CR0_WP|CR0_AM|CR0_PAGING,%eax
  167. /* Make changes effective */
  168. movq %rax, %cr0
  169. /* Setup a boot time stack */
  170. movq init_rsp(%rip),%rsp
  171. /* zero EFLAGS after setting rsp */
  172. pushq $0
  173. popfq
  174. /*
  175. * We must switch to a new descriptor in kernel space for the GDT
  176. * because soon the kernel won't have access anymore to the userspace
  177. * addresses where we're currently running on. We have to do that here
  178. * because in 32bit we couldn't load a 64bit linear address.
  179. */
  180. lgdt cpu_gdt_descr(%rip)
  181. /* set up data segments. actually 0 would do too */
  182. movl $__KERNEL_DS,%eax
  183. movl %eax,%ds
  184. movl %eax,%ss
  185. movl %eax,%es
  186. /*
  187. * We don't really need to load %fs or %gs, but load them anyway
  188. * to kill any stale realmode selectors. This allows execution
  189. * under VT hardware.
  190. */
  191. movl %eax,%fs
  192. movl %eax,%gs
  193. /*
  194. * Setup up a dummy PDA. this is just for some early bootup code
  195. * that does in_interrupt()
  196. */
  197. movl $MSR_GS_BASE,%ecx
  198. movq $empty_zero_page,%rax
  199. movq %rax,%rdx
  200. shrq $32,%rdx
  201. wrmsr
  202. /* esi is pointer to real mode structure with interesting info.
  203. pass it to C */
  204. movl %esi, %edi
  205. /* Finally jump to run C code and to be on real kernel address
  206. * Since we are running on identity-mapped space we have to jump
  207. * to the full 64bit address, this is only possible as indirect
  208. * jump. In addition we need to ensure %cs is set so we make this
  209. * a far return.
  210. */
  211. movq initial_code(%rip),%rax
  212. pushq $0 # fake return address to stop unwinder
  213. pushq $__KERNEL_CS # set correct cs
  214. pushq %rax # target address in negative space
  215. lretq
  216. /* SMP bootup changes these two */
  217. #ifndef CONFIG_HOTPLUG_CPU
  218. .pushsection .init.data
  219. #endif
  220. .align 8
  221. .globl initial_code
  222. initial_code:
  223. .quad x86_64_start_kernel
  224. #ifndef CONFIG_HOTPLUG_CPU
  225. .popsection
  226. #endif
  227. .globl init_rsp
  228. init_rsp:
  229. .quad init_thread_union+THREAD_SIZE-8
  230. bad_address:
  231. jmp bad_address
  232. #ifdef CONFIG_EARLY_PRINTK
  233. .macro early_idt_tramp first, last
  234. .ifgt \last-\first
  235. early_idt_tramp \first, \last-1
  236. .endif
  237. movl $\last,%esi
  238. jmp early_idt_handler
  239. .endm
  240. .globl early_idt_handlers
  241. early_idt_handlers:
  242. early_idt_tramp 0, 63
  243. early_idt_tramp 64, 127
  244. early_idt_tramp 128, 191
  245. early_idt_tramp 192, 255
  246. #endif
  247. ENTRY(early_idt_handler)
  248. #ifdef CONFIG_EARLY_PRINTK
  249. cmpl $2,early_recursion_flag(%rip)
  250. jz 1f
  251. incl early_recursion_flag(%rip)
  252. GET_CR2_INTO_RCX
  253. movq %rcx,%r9
  254. xorl %r8d,%r8d # zero for error code
  255. movl %esi,%ecx # get vector number
  256. # Test %ecx against mask of vectors that push error code.
  257. cmpl $31,%ecx
  258. ja 0f
  259. movl $1,%eax
  260. salq %cl,%rax
  261. testl $0x27d00,%eax
  262. je 0f
  263. popq %r8 # get error code
  264. 0: movq 0(%rsp),%rcx # get ip
  265. movq 8(%rsp),%rdx # get cs
  266. xorl %eax,%eax
  267. leaq early_idt_msg(%rip),%rdi
  268. call early_printk
  269. cmpl $2,early_recursion_flag(%rip)
  270. jz 1f
  271. call dump_stack
  272. #ifdef CONFIG_KALLSYMS
  273. leaq early_idt_ripmsg(%rip),%rdi
  274. movq 8(%rsp),%rsi # get rip again
  275. call __print_symbol
  276. #endif
  277. #endif /* EARLY_PRINTK */
  278. 1: hlt
  279. jmp 1b
  280. #ifdef CONFIG_EARLY_PRINTK
  281. early_recursion_flag:
  282. .long 0
  283. early_idt_msg:
  284. .asciz "PANIC: early exception %02lx rip %lx:%lx error %lx cr2 %lx\n"
  285. early_idt_ripmsg:
  286. .asciz "RIP %s\n"
  287. #endif /* CONFIG_EARLY_PRINTK */
  288. .balign PAGE_SIZE
  289. #define NEXT_PAGE(name) \
  290. .balign PAGE_SIZE; \
  291. ENTRY(name)
  292. /* Automate the creation of 1 to 1 mapping pmd entries */
  293. #define PMDS(START, PERM, COUNT) \
  294. i = 0 ; \
  295. .rept (COUNT) ; \
  296. .quad (START) + (i << 21) + (PERM) ; \
  297. i = i + 1 ; \
  298. .endr
  299. /*
  300. * This default setting generates an ident mapping at address 0x100000
  301. * and a mapping for the kernel that precisely maps virtual address
  302. * 0xffffffff80000000 to physical address 0x000000. (always using
  303. * 2Mbyte large pages provided by PAE mode)
  304. */
  305. NEXT_PAGE(init_level4_pgt)
  306. .quad level3_ident_pgt - __START_KERNEL_map + _KERNPG_TABLE
  307. .fill 257,8,0
  308. .quad level3_ident_pgt - __START_KERNEL_map + _KERNPG_TABLE
  309. .fill 252,8,0
  310. /* (2^48-(2*1024*1024*1024))/(2^39) = 511 */
  311. .quad level3_kernel_pgt - __START_KERNEL_map + _PAGE_TABLE
  312. NEXT_PAGE(level3_ident_pgt)
  313. .quad level2_ident_pgt - __START_KERNEL_map + _KERNPG_TABLE
  314. .fill 511,8,0
  315. NEXT_PAGE(level3_kernel_pgt)
  316. .fill 510,8,0
  317. /* (2^48-(2*1024*1024*1024)-((2^39)*511))/(2^30) = 510 */
  318. .quad level2_kernel_pgt - __START_KERNEL_map + _KERNPG_TABLE
  319. .quad level2_fixmap_pgt - __START_KERNEL_map + _PAGE_TABLE
  320. NEXT_PAGE(level2_fixmap_pgt)
  321. .fill 506,8,0
  322. .quad level1_fixmap_pgt - __START_KERNEL_map + _PAGE_TABLE
  323. /* 8MB reserved for vsyscalls + a 2MB hole = 4 + 1 entries */
  324. .fill 5,8,0
  325. NEXT_PAGE(level1_fixmap_pgt)
  326. .fill 512,8,0
  327. NEXT_PAGE(level2_ident_pgt)
  328. /* Since I easily can, map the first 1G.
  329. * Don't set NX because code runs from these pages.
  330. */
  331. PMDS(0x0000000000000000, __PAGE_KERNEL_LARGE_EXEC, PTRS_PER_PMD)
  332. NEXT_PAGE(level2_kernel_pgt)
  333. /* 40MB kernel mapping. The kernel code cannot be bigger than that.
  334. When you change this change KERNEL_TEXT_SIZE in page.h too. */
  335. /* (2^48-(2*1024*1024*1024)-((2^39)*511)-((2^30)*510)) = 0 */
  336. PMDS(0x0000000000000000, __PAGE_KERNEL_LARGE_EXEC|_PAGE_GLOBAL, KERNEL_TEXT_SIZE/PMD_SIZE)
  337. /* Module mapping starts here */
  338. .fill (PTRS_PER_PMD - (KERNEL_TEXT_SIZE/PMD_SIZE)),8,0
  339. NEXT_PAGE(level2_spare_pgt)
  340. .fill 512,8,0
  341. #undef PMDS
  342. #undef NEXT_PAGE
  343. .data
  344. .align 16
  345. .globl cpu_gdt_descr
  346. cpu_gdt_descr:
  347. .word gdt_end-cpu_gdt_table-1
  348. gdt:
  349. .quad cpu_gdt_table
  350. #ifdef CONFIG_SMP
  351. .rept NR_CPUS-1
  352. .word 0
  353. .quad 0
  354. .endr
  355. #endif
  356. ENTRY(phys_base)
  357. /* This must match the first entry in level2_kernel_pgt */
  358. .quad 0x0000000000000000
  359. /* We need valid kernel segments for data and code in long mode too
  360. * IRET will check the segment types kkeil 2000/10/28
  361. * Also sysret mandates a special GDT layout
  362. */
  363. .section .data.page_aligned, "aw"
  364. .align PAGE_SIZE
  365. /* The TLS descriptors are currently at a different place compared to i386.
  366. Hopefully nobody expects them at a fixed place (Wine?) */
  367. ENTRY(cpu_gdt_table)
  368. .quad 0x0000000000000000 /* NULL descriptor */
  369. .quad 0x00cf9b000000ffff /* __KERNEL32_CS */
  370. .quad 0x00af9b000000ffff /* __KERNEL_CS */
  371. .quad 0x00cf93000000ffff /* __KERNEL_DS */
  372. .quad 0x00cffb000000ffff /* __USER32_CS */
  373. .quad 0x00cff3000000ffff /* __USER_DS, __USER32_DS */
  374. .quad 0x00affb000000ffff /* __USER_CS */
  375. .quad 0x0 /* unused */
  376. .quad 0,0 /* TSS */
  377. .quad 0,0 /* LDT */
  378. .quad 0,0,0 /* three TLS descriptors */
  379. .quad 0x0000f40000000000 /* node/CPU stored in limit */
  380. gdt_end:
  381. /* asm/segment.h:GDT_ENTRIES must match this */
  382. /* This should be a multiple of the cache line size */
  383. /* GDTs of other CPUs are now dynamically allocated */
  384. /* zero the remaining page */
  385. .fill PAGE_SIZE / 8 - GDT_ENTRIES,8,0
  386. .section .bss, "aw", @nobits
  387. .align L1_CACHE_BYTES
  388. ENTRY(idt_table)
  389. .skip 256 * 16
  390. .section .bss.page_aligned, "aw", @nobits
  391. .align PAGE_SIZE
  392. ENTRY(empty_zero_page)
  393. .skip PAGE_SIZE