head.S 10 KB

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