head_64.S 10 KB

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