head_64.S 8.3 KB

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