head_64.S 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /*
  2. * linux/boot/head.S
  3. *
  4. * Copyright (C) 1991, 1992, 1993 Linus Torvalds
  5. */
  6. /*
  7. * head.S contains the 32-bit startup code.
  8. *
  9. * NOTE!!! Startup happens at absolute address 0x00001000, which is also where
  10. * the page directory will exist. The startup code will be overwritten by
  11. * the page directory. [According to comments etc elsewhere on a compressed
  12. * kernel it will end up at 0x1000 + 1Mb I hope so as I assume this. - AC]
  13. *
  14. * Page 0 is deliberately kept safe, since System Management Mode code in
  15. * laptops may need to access the BIOS data stored there. This is also
  16. * useful for future device drivers that either access the BIOS via VM86
  17. * mode.
  18. */
  19. /*
  20. * High loaded stuff by Hans Lermen & Werner Almesberger, Feb. 1996
  21. */
  22. .code32
  23. .text
  24. #include <linux/init.h>
  25. #include <linux/linkage.h>
  26. #include <asm/segment.h>
  27. #include <asm/pgtable_types.h>
  28. #include <asm/page_types.h>
  29. #include <asm/boot.h>
  30. #include <asm/msr.h>
  31. #include <asm/processor-flags.h>
  32. #include <asm/asm-offsets.h>
  33. __HEAD
  34. .code32
  35. ENTRY(startup_32)
  36. cld
  37. /*
  38. * Test KEEP_SEGMENTS flag to see if the bootloader is asking
  39. * us to not reload segments
  40. */
  41. testb $(1<<6), BP_loadflags(%esi)
  42. jnz 1f
  43. cli
  44. movl $(__KERNEL_DS), %eax
  45. movl %eax, %ds
  46. movl %eax, %es
  47. movl %eax, %ss
  48. 1:
  49. /*
  50. * Calculate the delta between where we were compiled to run
  51. * at and where we were actually loaded at. This can only be done
  52. * with a short local call on x86. Nothing else will tell us what
  53. * address we are running at. The reserved chunk of the real-mode
  54. * data at 0x1e4 (defined as a scratch field) are used as the stack
  55. * for this calculation. Only 4 bytes are needed.
  56. */
  57. leal (BP_scratch+4)(%esi), %esp
  58. call 1f
  59. 1: popl %ebp
  60. subl $1b, %ebp
  61. /* setup a stack and make sure cpu supports long mode. */
  62. movl $boot_stack_end, %eax
  63. addl %ebp, %eax
  64. movl %eax, %esp
  65. call verify_cpu
  66. testl %eax, %eax
  67. jnz no_longmode
  68. /*
  69. * Compute the delta between where we were compiled to run at
  70. * and where the code will actually run at.
  71. *
  72. * %ebp contains the address we are loaded at by the boot loader and %ebx
  73. * contains the address where we should move the kernel image temporarily
  74. * for safe in-place decompression.
  75. */
  76. #ifdef CONFIG_RELOCATABLE
  77. movl %ebp, %ebx
  78. movl BP_kernel_alignment(%esi), %eax
  79. decl %eax
  80. addl %eax, %ebx
  81. notl %eax
  82. andl %eax, %ebx
  83. #else
  84. movl $LOAD_PHYSICAL_ADDR, %ebx
  85. #endif
  86. /* Target address to relocate to for decompression */
  87. addl $z_extract_offset, %ebx
  88. /*
  89. * Prepare for entering 64 bit mode
  90. */
  91. /* Load new GDT with the 64bit segments using 32bit descriptor */
  92. leal gdt(%ebp), %eax
  93. movl %eax, gdt+2(%ebp)
  94. lgdt gdt(%ebp)
  95. /* Enable PAE mode */
  96. movl $(X86_CR4_PAE), %eax
  97. movl %eax, %cr4
  98. /*
  99. * Build early 4G boot pagetable
  100. */
  101. /* Initialize Page tables to 0 */
  102. leal pgtable(%ebx), %edi
  103. xorl %eax, %eax
  104. movl $((4096*6)/4), %ecx
  105. rep stosl
  106. /* Build Level 4 */
  107. leal pgtable + 0(%ebx), %edi
  108. leal 0x1007 (%edi), %eax
  109. movl %eax, 0(%edi)
  110. /* Build Level 3 */
  111. leal pgtable + 0x1000(%ebx), %edi
  112. leal 0x1007(%edi), %eax
  113. movl $4, %ecx
  114. 1: movl %eax, 0x00(%edi)
  115. addl $0x00001000, %eax
  116. addl $8, %edi
  117. decl %ecx
  118. jnz 1b
  119. /* Build Level 2 */
  120. leal pgtable + 0x2000(%ebx), %edi
  121. movl $0x00000183, %eax
  122. movl $2048, %ecx
  123. 1: movl %eax, 0(%edi)
  124. addl $0x00200000, %eax
  125. addl $8, %edi
  126. decl %ecx
  127. jnz 1b
  128. /* Enable the boot page tables */
  129. leal pgtable(%ebx), %eax
  130. movl %eax, %cr3
  131. /* Enable Long mode in EFER (Extended Feature Enable Register) */
  132. movl $MSR_EFER, %ecx
  133. rdmsr
  134. btsl $_EFER_LME, %eax
  135. wrmsr
  136. /*
  137. * Setup for the jump to 64bit mode
  138. *
  139. * When the jump is performend we will be in long mode but
  140. * in 32bit compatibility mode with EFER.LME = 1, CS.L = 0, CS.D = 1
  141. * (and in turn EFER.LMA = 1). To jump into 64bit mode we use
  142. * the new gdt/idt that has __KERNEL_CS with CS.L = 1.
  143. * We place all of the values on our mini stack so lret can
  144. * used to perform that far jump.
  145. */
  146. pushl $__KERNEL_CS
  147. leal startup_64(%ebp), %eax
  148. pushl %eax
  149. /* Enter paged protected Mode, activating Long Mode */
  150. movl $(X86_CR0_PG | X86_CR0_PE), %eax /* Enable Paging and Protected mode */
  151. movl %eax, %cr0
  152. /* Jump from 32bit compatibility mode into 64bit mode. */
  153. lret
  154. ENDPROC(startup_32)
  155. no_longmode:
  156. /* This isn't an x86-64 CPU so hang */
  157. 1:
  158. hlt
  159. jmp 1b
  160. #include "../../kernel/verify_cpu.S"
  161. /*
  162. * Be careful here startup_64 needs to be at a predictable
  163. * address so I can export it in an ELF header. Bootloaders
  164. * should look at the ELF header to find this address, as
  165. * it may change in the future.
  166. */
  167. .code64
  168. .org 0x200
  169. ENTRY(startup_64)
  170. /*
  171. * We come here either from startup_32 or directly from a
  172. * 64bit bootloader. If we come here from a bootloader we depend on
  173. * an identity mapped page table being provied that maps our
  174. * entire text+data+bss and hopefully all of memory.
  175. */
  176. #ifdef CONFIG_EFI_STUB
  177. /*
  178. * The entry point for the PE/COFF executable is 0x210, so only
  179. * legacy boot loaders will execute this jmp.
  180. */
  181. jmp preferred_addr
  182. .org 0x210
  183. mov %rcx, %rdi
  184. mov %rdx, %rsi
  185. call efi_main
  186. movq %rax,%rsi
  187. cmpq $0,%rax
  188. jne 2f
  189. 1:
  190. /* EFI init failed, so hang. */
  191. hlt
  192. jmp 1b
  193. 2:
  194. call 3f
  195. 3:
  196. popq %rax
  197. subq $3b, %rax
  198. subq BP_pref_address(%rsi), %rax
  199. add BP_code32_start(%esi), %eax
  200. leaq preferred_addr(%rax), %rax
  201. jmp *%rax
  202. preferred_addr:
  203. #endif
  204. /* Setup data segments. */
  205. xorl %eax, %eax
  206. movl %eax, %ds
  207. movl %eax, %es
  208. movl %eax, %ss
  209. movl %eax, %fs
  210. movl %eax, %gs
  211. lldt %ax
  212. movl $0x20, %eax
  213. ltr %ax
  214. /*
  215. * Compute the decompressed kernel start address. It is where
  216. * we were loaded at aligned to a 2M boundary. %rbp contains the
  217. * decompressed kernel start address.
  218. *
  219. * If it is a relocatable kernel then decompress and run the kernel
  220. * from load address aligned to 2MB addr, otherwise decompress and
  221. * run the kernel from LOAD_PHYSICAL_ADDR
  222. *
  223. * We cannot rely on the calculation done in 32-bit mode, since we
  224. * may have been invoked via the 64-bit entry point.
  225. */
  226. /* Start with the delta to where the kernel will run at. */
  227. #ifdef CONFIG_RELOCATABLE
  228. leaq startup_32(%rip) /* - $startup_32 */, %rbp
  229. movl BP_kernel_alignment(%rsi), %eax
  230. decl %eax
  231. addq %rax, %rbp
  232. notq %rax
  233. andq %rax, %rbp
  234. #else
  235. movq $LOAD_PHYSICAL_ADDR, %rbp
  236. #endif
  237. /* Target address to relocate to for decompression */
  238. leaq z_extract_offset(%rbp), %rbx
  239. /* Set up the stack */
  240. leaq boot_stack_end(%rbx), %rsp
  241. /* Zero EFLAGS */
  242. pushq $0
  243. popfq
  244. /*
  245. * Copy the compressed kernel to the end of our buffer
  246. * where decompression in place becomes safe.
  247. */
  248. pushq %rsi
  249. leaq (_bss-8)(%rip), %rsi
  250. leaq (_bss-8)(%rbx), %rdi
  251. movq $_bss /* - $startup_32 */, %rcx
  252. shrq $3, %rcx
  253. std
  254. rep movsq
  255. cld
  256. popq %rsi
  257. /*
  258. * Jump to the relocated address.
  259. */
  260. leaq relocated(%rbx), %rax
  261. jmp *%rax
  262. .text
  263. relocated:
  264. /*
  265. * Clear BSS (stack is currently empty)
  266. */
  267. xorl %eax, %eax
  268. leaq _bss(%rip), %rdi
  269. leaq _ebss(%rip), %rcx
  270. subq %rdi, %rcx
  271. shrq $3, %rcx
  272. rep stosq
  273. /*
  274. * Adjust our own GOT
  275. */
  276. leaq _got(%rip), %rdx
  277. leaq _egot(%rip), %rcx
  278. 1:
  279. cmpq %rcx, %rdx
  280. jae 2f
  281. addq %rbx, (%rdx)
  282. addq $8, %rdx
  283. jmp 1b
  284. 2:
  285. /*
  286. * Do the decompression, and jump to the new kernel..
  287. */
  288. pushq %rsi /* Save the real mode argument */
  289. movq %rsi, %rdi /* real mode address */
  290. leaq boot_heap(%rip), %rsi /* malloc area for uncompression */
  291. leaq input_data(%rip), %rdx /* input_data */
  292. movl $z_input_len, %ecx /* input_len */
  293. movq %rbp, %r8 /* output target address */
  294. call decompress_kernel
  295. popq %rsi
  296. /*
  297. * Jump to the decompressed kernel.
  298. */
  299. jmp *%rbp
  300. .data
  301. gdt:
  302. .word gdt_end - gdt
  303. .long gdt
  304. .word 0
  305. .quad 0x0000000000000000 /* NULL descriptor */
  306. .quad 0x00af9a000000ffff /* __KERNEL_CS */
  307. .quad 0x00cf92000000ffff /* __KERNEL_DS */
  308. .quad 0x0080890000000000 /* TS descriptor */
  309. .quad 0x0000000000000000 /* TS continued */
  310. gdt_end:
  311. /*
  312. * Stack and heap for uncompression
  313. */
  314. .bss
  315. .balign 4
  316. boot_heap:
  317. .fill BOOT_HEAP_SIZE, 1, 0
  318. boot_stack:
  319. .fill BOOT_STACK_SIZE, 1, 0
  320. boot_stack_end:
  321. /*
  322. * Space for page tables (not in .bss so not zeroed)
  323. */
  324. .section ".pgtable","a",@nobits
  325. .balign 4096
  326. pgtable:
  327. .fill 6*4096, 1, 0