head_64.S 9.9 KB

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