head_64.S 8.4 KB

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